2
0

netrc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #ifndef CURL_DISABLE_NETRC
  26. #ifdef HAVE_PWD_H
  27. #undef __NO_NET_API /* required for AmigaOS to declare getpwuid() */
  28. #include <pwd.h>
  29. #define __NO_NET_API
  30. #endif
  31. #include <curl/curl.h>
  32. #include "netrc.h"
  33. #include "strcase.h"
  34. #include "curl_get_line.h"
  35. /* The last 3 #include files should be in this order */
  36. #include "curl_printf.h"
  37. #include "curl_memory.h"
  38. #include "memdebug.h"
  39. /* Get user and password from .netrc when given a machine name */
  40. enum host_lookup_state {
  41. NOTHING,
  42. HOSTFOUND, /* the 'machine' keyword was found */
  43. HOSTVALID, /* this is "our" machine! */
  44. MACDEF
  45. };
  46. enum found_state {
  47. NONE,
  48. LOGIN,
  49. PASSWORD
  50. };
  51. #define FOUND_LOGIN 1
  52. #define FOUND_PASSWORD 2
  53. #define NETRC_FILE_MISSING 1
  54. #define NETRC_FAILED -1
  55. #define NETRC_SUCCESS 0
  56. #define MAX_NETRC_LINE 16384
  57. #define MAX_NETRC_FILE (128*1024)
  58. #define MAX_NETRC_TOKEN 4096
  59. static CURLcode file2memory(const char *filename, struct dynbuf *filebuf)
  60. {
  61. CURLcode result = CURLE_OK;
  62. FILE *file = fopen(filename, FOPEN_READTEXT);
  63. struct dynbuf linebuf;
  64. Curl_dyn_init(&linebuf, MAX_NETRC_LINE);
  65. if(file) {
  66. while(Curl_get_line(&linebuf, file)) {
  67. const char *line = Curl_dyn_ptr(&linebuf);
  68. /* skip comments on load */
  69. while(ISBLANK(*line))
  70. line++;
  71. if(*line == '#')
  72. continue;
  73. result = Curl_dyn_add(filebuf, line);
  74. if(result)
  75. goto done;
  76. }
  77. }
  78. done:
  79. Curl_dyn_free(&linebuf);
  80. if(file)
  81. fclose(file);
  82. return result;
  83. }
  84. /*
  85. * Returns zero on success.
  86. */
  87. static int parsenetrc(struct store_netrc *store,
  88. const char *host,
  89. char **loginp, /* might point to a username */
  90. char **passwordp,
  91. const char *netrcfile)
  92. {
  93. int retcode = NETRC_FILE_MISSING;
  94. char *login = *loginp;
  95. char *password = NULL;
  96. bool specific_login = !!login; /* points to something */
  97. enum host_lookup_state state = NOTHING;
  98. enum found_state keyword = NONE;
  99. unsigned char found = 0; /* login + password found bits, as they can come in
  100. any order */
  101. bool our_login = FALSE; /* found our login name */
  102. bool done = FALSE;
  103. char *netrcbuffer;
  104. struct dynbuf token;
  105. struct dynbuf *filebuf = &store->filebuf;
  106. DEBUGASSERT(!*passwordp);
  107. Curl_dyn_init(&token, MAX_NETRC_TOKEN);
  108. if(!store->loaded) {
  109. if(file2memory(netrcfile, filebuf))
  110. return NETRC_FAILED;
  111. store->loaded = TRUE;
  112. }
  113. netrcbuffer = Curl_dyn_ptr(filebuf);
  114. while(!done) {
  115. char *tok = netrcbuffer;
  116. while(tok && !done) {
  117. char *tok_end;
  118. bool quoted;
  119. Curl_dyn_reset(&token);
  120. while(ISBLANK(*tok))
  121. tok++;
  122. /* tok is first non-space letter */
  123. if(state == MACDEF) {
  124. if((*tok == '\n') || (*tok == '\r'))
  125. state = NOTHING; /* end of macro definition */
  126. }
  127. if(!*tok || (*tok == '\n'))
  128. /* end of line */
  129. break;
  130. /* leading double-quote means quoted string */
  131. quoted = (*tok == '\"');
  132. tok_end = tok;
  133. if(!quoted) {
  134. size_t len = 0;
  135. while(!ISSPACE(*tok_end)) {
  136. tok_end++;
  137. len++;
  138. }
  139. if(!len || Curl_dyn_addn(&token, tok, len)) {
  140. retcode = NETRC_FAILED;
  141. goto out;
  142. }
  143. }
  144. else {
  145. bool escape = FALSE;
  146. bool endquote = FALSE;
  147. tok_end++; /* pass the leading quote */
  148. while(*tok_end) {
  149. char s = *tok_end;
  150. if(escape) {
  151. escape = FALSE;
  152. switch(s) {
  153. case 'n':
  154. s = '\n';
  155. break;
  156. case 'r':
  157. s = '\r';
  158. break;
  159. case 't':
  160. s = '\t';
  161. break;
  162. }
  163. }
  164. else if(s == '\\') {
  165. escape = TRUE;
  166. tok_end++;
  167. continue;
  168. }
  169. else if(s == '\"') {
  170. tok_end++; /* pass the ending quote */
  171. endquote = TRUE;
  172. break;
  173. }
  174. if(Curl_dyn_addn(&token, &s, 1)) {
  175. retcode = NETRC_FAILED;
  176. goto out;
  177. }
  178. tok_end++;
  179. }
  180. if(escape || !endquote) {
  181. /* bad syntax, get out */
  182. retcode = NETRC_FAILED;
  183. goto out;
  184. }
  185. }
  186. tok = Curl_dyn_ptr(&token);
  187. switch(state) {
  188. case NOTHING:
  189. if(strcasecompare("macdef", tok))
  190. /* Define a macro. A macro is defined with the specified name; its
  191. contents begin with the next .netrc line and continue until a
  192. null line (consecutive new-line characters) is encountered. */
  193. state = MACDEF;
  194. else if(strcasecompare("machine", tok)) {
  195. /* the next tok is the machine name, this is in itself the delimiter
  196. that starts the stuff entered for this machine, after this we
  197. need to search for 'login' and 'password'. */
  198. state = HOSTFOUND;
  199. keyword = NONE;
  200. found = 0;
  201. our_login = FALSE;
  202. Curl_safefree(password);
  203. if(!specific_login)
  204. Curl_safefree(login);
  205. }
  206. else if(strcasecompare("default", tok)) {
  207. state = HOSTVALID;
  208. retcode = NETRC_SUCCESS; /* we did find our host */
  209. }
  210. break;
  211. case MACDEF:
  212. if(!*tok)
  213. state = NOTHING;
  214. break;
  215. case HOSTFOUND:
  216. if(strcasecompare(host, tok)) {
  217. /* and yes, this is our host! */
  218. state = HOSTVALID;
  219. retcode = NETRC_SUCCESS; /* we did find our host */
  220. }
  221. else
  222. /* not our host */
  223. state = NOTHING;
  224. break;
  225. case HOSTVALID:
  226. /* we are now parsing sub-keywords concerning "our" host */
  227. if(keyword == LOGIN) {
  228. if(specific_login)
  229. our_login = !Curl_timestrcmp(login, tok);
  230. else {
  231. our_login = TRUE;
  232. free(login);
  233. login = strdup(tok);
  234. if(!login) {
  235. retcode = NETRC_FAILED; /* allocation failed */
  236. goto out;
  237. }
  238. }
  239. found |= FOUND_LOGIN;
  240. keyword = NONE;
  241. }
  242. else if(keyword == PASSWORD) {
  243. free(password);
  244. password = strdup(tok);
  245. if(!password) {
  246. retcode = NETRC_FAILED; /* allocation failed */
  247. goto out;
  248. }
  249. if(!specific_login || our_login)
  250. found |= FOUND_PASSWORD;
  251. keyword = NONE;
  252. }
  253. else if(strcasecompare("login", tok))
  254. keyword = LOGIN;
  255. else if(strcasecompare("password", tok))
  256. keyword = PASSWORD;
  257. else if(strcasecompare("machine", tok)) {
  258. /* a new machine here */
  259. if(found & FOUND_PASSWORD) {
  260. done = TRUE;
  261. break;
  262. }
  263. state = HOSTFOUND;
  264. keyword = NONE;
  265. found = 0;
  266. Curl_safefree(password);
  267. if(!specific_login)
  268. Curl_safefree(login);
  269. }
  270. else if(strcasecompare("default", tok)) {
  271. state = HOSTVALID;
  272. retcode = NETRC_SUCCESS; /* we did find our host */
  273. Curl_safefree(password);
  274. if(!specific_login)
  275. Curl_safefree(login);
  276. }
  277. if((found == (FOUND_PASSWORD|FOUND_LOGIN)) && our_login) {
  278. done = TRUE;
  279. break;
  280. }
  281. break;
  282. } /* switch (state) */
  283. tok = ++tok_end;
  284. }
  285. if(!done) {
  286. char *nl = NULL;
  287. if(tok)
  288. nl = strchr(tok, '\n');
  289. if(!nl)
  290. break;
  291. /* point to next line */
  292. netrcbuffer = &nl[1];
  293. }
  294. } /* while !done */
  295. out:
  296. Curl_dyn_free(&token);
  297. if(!retcode && !password && our_login) {
  298. /* success without a password, set a blank one */
  299. password = strdup("");
  300. if(!password)
  301. retcode = 1; /* out of memory */
  302. }
  303. if(!retcode) {
  304. /* success */
  305. if(!specific_login)
  306. *loginp = login;
  307. *passwordp = password;
  308. }
  309. else {
  310. Curl_dyn_free(filebuf);
  311. if(!specific_login)
  312. free(login);
  313. free(password);
  314. }
  315. return retcode;
  316. }
  317. /*
  318. * @unittest: 1304
  319. *
  320. * *loginp and *passwordp MUST be allocated if they are not NULL when passed
  321. * in.
  322. */
  323. int Curl_parsenetrc(struct store_netrc *store, const char *host,
  324. char **loginp, char **passwordp,
  325. char *netrcfile)
  326. {
  327. int retcode = 1;
  328. char *filealloc = NULL;
  329. if(!netrcfile) {
  330. #if defined(HAVE_GETPWUID_R) && defined(HAVE_GETEUID)
  331. char pwbuf[1024];
  332. #endif
  333. char *home = NULL;
  334. char *homea = curl_getenv("HOME"); /* portable environment reader */
  335. if(homea) {
  336. home = homea;
  337. #if defined(HAVE_GETPWUID_R) && defined(HAVE_GETEUID)
  338. }
  339. else {
  340. struct passwd pw, *pw_res;
  341. if(!getpwuid_r(geteuid(), &pw, pwbuf, sizeof(pwbuf), &pw_res)
  342. && pw_res) {
  343. home = pw.pw_dir;
  344. }
  345. #elif defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
  346. }
  347. else {
  348. struct passwd *pw;
  349. pw = getpwuid(geteuid());
  350. if(pw) {
  351. home = pw->pw_dir;
  352. }
  353. #elif defined(_WIN32)
  354. }
  355. else {
  356. homea = curl_getenv("USERPROFILE");
  357. if(homea) {
  358. home = homea;
  359. }
  360. #endif
  361. }
  362. if(!home)
  363. return retcode; /* no home directory found (or possibly out of
  364. memory) */
  365. filealloc = aprintf("%s%s.netrc", home, DIR_CHAR);
  366. if(!filealloc) {
  367. free(homea);
  368. return -1;
  369. }
  370. retcode = parsenetrc(store, host, loginp, passwordp, filealloc);
  371. free(filealloc);
  372. #ifdef _WIN32
  373. if((retcode == NETRC_FILE_MISSING) || (retcode == NETRC_FAILED)) {
  374. /* fallback to the old-style "_netrc" file */
  375. filealloc = aprintf("%s%s_netrc", home, DIR_CHAR);
  376. if(!filealloc) {
  377. free(homea);
  378. return -1;
  379. }
  380. retcode = parsenetrc(store, host, loginp, passwordp, filealloc);
  381. free(filealloc);
  382. }
  383. #endif
  384. free(homea);
  385. }
  386. else
  387. retcode = parsenetrc(store, host, loginp, passwordp, netrcfile);
  388. return retcode;
  389. }
  390. void Curl_netrc_init(struct store_netrc *s)
  391. {
  392. Curl_dyn_init(&s->filebuf, MAX_NETRC_FILE);
  393. s->loaded = FALSE;
  394. }
  395. void Curl_netrc_cleanup(struct store_netrc *s)
  396. {
  397. Curl_dyn_free(&s->filebuf);
  398. s->loaded = FALSE;
  399. }
  400. #endif