netrc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <[email protected]>, 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 MAX_NETRC_LINE 16384
  54. #define MAX_NETRC_FILE (128*1024)
  55. #define MAX_NETRC_TOKEN 4096
  56. /* convert a dynbuf call CURLcode error to a NETRCcode error */
  57. #define curl2netrc(result) \
  58. (((result) == CURLE_OUT_OF_MEMORY) ? \
  59. NETRC_OUT_OF_MEMORY : NETRC_SYNTAX_ERROR)
  60. static NETRCcode file2memory(const char *filename, struct dynbuf *filebuf)
  61. {
  62. NETRCcode ret = NETRC_FILE_MISSING; /* if it cannot open the file */
  63. FILE *file = fopen(filename, FOPEN_READTEXT);
  64. struct dynbuf linebuf;
  65. Curl_dyn_init(&linebuf, MAX_NETRC_LINE);
  66. if(file) {
  67. ret = NETRC_OK;
  68. while(Curl_get_line(&linebuf, file)) {
  69. CURLcode result;
  70. const char *line = Curl_dyn_ptr(&linebuf);
  71. /* skip comments on load */
  72. while(ISBLANK(*line))
  73. line++;
  74. if(*line == '#')
  75. continue;
  76. result = Curl_dyn_add(filebuf, line);
  77. if(result) {
  78. ret = curl2netrc(result);
  79. goto done;
  80. }
  81. }
  82. }
  83. done:
  84. Curl_dyn_free(&linebuf);
  85. if(file)
  86. fclose(file);
  87. return ret;
  88. }
  89. /*
  90. * Returns zero on success.
  91. */
  92. static NETRCcode parsenetrc(struct store_netrc *store,
  93. const char *host,
  94. char **loginp, /* might point to a username */
  95. char **passwordp,
  96. const char *netrcfile)
  97. {
  98. NETRCcode retcode = NETRC_NO_MATCH;
  99. char *login = *loginp;
  100. char *password = NULL;
  101. bool specific_login = !!login; /* points to something */
  102. enum host_lookup_state state = NOTHING;
  103. enum found_state keyword = NONE;
  104. unsigned char found = 0; /* login + password found bits, as they can come in
  105. any order */
  106. bool our_login = FALSE; /* found our login name */
  107. bool done = FALSE;
  108. char *netrcbuffer;
  109. struct dynbuf token;
  110. struct dynbuf *filebuf = &store->filebuf;
  111. DEBUGASSERT(!*passwordp);
  112. Curl_dyn_init(&token, MAX_NETRC_TOKEN);
  113. if(!store->loaded) {
  114. NETRCcode ret = file2memory(netrcfile, filebuf);
  115. if(ret)
  116. return ret;
  117. store->loaded = TRUE;
  118. }
  119. netrcbuffer = Curl_dyn_ptr(filebuf);
  120. while(!done) {
  121. char *tok = netrcbuffer;
  122. while(tok && !done) {
  123. char *tok_end;
  124. bool quoted;
  125. Curl_dyn_reset(&token);
  126. while(ISBLANK(*tok))
  127. tok++;
  128. /* tok is first non-space letter */
  129. if(state == MACDEF) {
  130. if((*tok == '\n') || (*tok == '\r'))
  131. state = NOTHING; /* end of macro definition */
  132. }
  133. if(!*tok || (*tok == '\n'))
  134. /* end of line */
  135. break;
  136. /* leading double-quote means quoted string */
  137. quoted = (*tok == '\"');
  138. tok_end = tok;
  139. if(!quoted) {
  140. size_t len = 0;
  141. CURLcode result;
  142. while(!ISSPACE(*tok_end)) {
  143. tok_end++;
  144. len++;
  145. }
  146. if(!len) {
  147. retcode = NETRC_SYNTAX_ERROR;
  148. goto out;
  149. }
  150. result = Curl_dyn_addn(&token, tok, len);
  151. if(result) {
  152. retcode = curl2netrc(result);
  153. goto out;
  154. }
  155. }
  156. else {
  157. bool escape = FALSE;
  158. bool endquote = FALSE;
  159. tok_end++; /* pass the leading quote */
  160. while(*tok_end) {
  161. CURLcode result;
  162. char s = *tok_end;
  163. if(escape) {
  164. escape = FALSE;
  165. switch(s) {
  166. case 'n':
  167. s = '\n';
  168. break;
  169. case 'r':
  170. s = '\r';
  171. break;
  172. case 't':
  173. s = '\t';
  174. break;
  175. }
  176. }
  177. else if(s == '\\') {
  178. escape = TRUE;
  179. tok_end++;
  180. continue;
  181. }
  182. else if(s == '\"') {
  183. tok_end++; /* pass the ending quote */
  184. endquote = TRUE;
  185. break;
  186. }
  187. result = Curl_dyn_addn(&token, &s, 1);
  188. if(result) {
  189. retcode = curl2netrc(result);
  190. goto out;
  191. }
  192. tok_end++;
  193. }
  194. if(escape || !endquote) {
  195. /* bad syntax, get out */
  196. retcode = NETRC_SYNTAX_ERROR;
  197. goto out;
  198. }
  199. }
  200. tok = Curl_dyn_ptr(&token);
  201. switch(state) {
  202. case NOTHING:
  203. if(strcasecompare("macdef", tok))
  204. /* Define a macro. A macro is defined with the specified name; its
  205. contents begin with the next .netrc line and continue until a
  206. null line (consecutive new-line characters) is encountered. */
  207. state = MACDEF;
  208. else if(strcasecompare("machine", tok)) {
  209. /* the next tok is the machine name, this is in itself the delimiter
  210. that starts the stuff entered for this machine, after this we
  211. need to search for 'login' and 'password'. */
  212. state = HOSTFOUND;
  213. keyword = NONE;
  214. found = 0;
  215. our_login = FALSE;
  216. Curl_safefree(password);
  217. if(!specific_login)
  218. Curl_safefree(login);
  219. }
  220. else if(strcasecompare("default", tok)) {
  221. state = HOSTVALID;
  222. retcode = NETRC_OK; /* we did find our host */
  223. }
  224. break;
  225. case MACDEF:
  226. if(!*tok)
  227. state = NOTHING;
  228. break;
  229. case HOSTFOUND:
  230. if(strcasecompare(host, tok)) {
  231. /* and yes, this is our host! */
  232. state = HOSTVALID;
  233. retcode = NETRC_OK; /* we did find our host */
  234. }
  235. else
  236. /* not our host */
  237. state = NOTHING;
  238. break;
  239. case HOSTVALID:
  240. /* we are now parsing sub-keywords concerning "our" host */
  241. if(keyword == LOGIN) {
  242. if(specific_login)
  243. our_login = !Curl_timestrcmp(login, tok);
  244. else {
  245. our_login = TRUE;
  246. free(login);
  247. login = strdup(tok);
  248. if(!login) {
  249. retcode = NETRC_OUT_OF_MEMORY; /* allocation failed */
  250. goto out;
  251. }
  252. }
  253. found |= FOUND_LOGIN;
  254. keyword = NONE;
  255. }
  256. else if(keyword == PASSWORD) {
  257. free(password);
  258. password = strdup(tok);
  259. if(!password) {
  260. retcode = NETRC_OUT_OF_MEMORY; /* allocation failed */
  261. goto out;
  262. }
  263. if(!specific_login || our_login)
  264. found |= FOUND_PASSWORD;
  265. keyword = NONE;
  266. }
  267. else if(strcasecompare("login", tok))
  268. keyword = LOGIN;
  269. else if(strcasecompare("password", tok))
  270. keyword = PASSWORD;
  271. else if(strcasecompare("machine", tok)) {
  272. /* a new machine here */
  273. if(found & FOUND_PASSWORD) {
  274. done = TRUE;
  275. break;
  276. }
  277. state = HOSTFOUND;
  278. keyword = NONE;
  279. found = 0;
  280. Curl_safefree(password);
  281. if(!specific_login)
  282. Curl_safefree(login);
  283. }
  284. else if(strcasecompare("default", tok)) {
  285. state = HOSTVALID;
  286. retcode = NETRC_OK; /* we did find our host */
  287. Curl_safefree(password);
  288. if(!specific_login)
  289. Curl_safefree(login);
  290. }
  291. if((found == (FOUND_PASSWORD|FOUND_LOGIN)) && our_login) {
  292. done = TRUE;
  293. break;
  294. }
  295. break;
  296. } /* switch (state) */
  297. tok = ++tok_end;
  298. }
  299. if(!done) {
  300. char *nl = NULL;
  301. if(tok)
  302. nl = strchr(tok, '\n');
  303. if(!nl)
  304. break;
  305. /* point to next line */
  306. netrcbuffer = &nl[1];
  307. }
  308. } /* while !done */
  309. out:
  310. Curl_dyn_free(&token);
  311. if(!retcode) {
  312. if(!password && our_login) {
  313. /* success without a password, set a blank one */
  314. password = strdup("");
  315. if(!password)
  316. retcode = NETRC_OUT_OF_MEMORY; /* out of memory */
  317. }
  318. else if(!login && !password)
  319. /* a default with no credentials */
  320. retcode = NETRC_NO_MATCH;
  321. }
  322. if(!retcode) {
  323. /* success */
  324. if(!specific_login)
  325. *loginp = login;
  326. *passwordp = password;
  327. }
  328. else {
  329. Curl_dyn_free(filebuf);
  330. if(!specific_login)
  331. free(login);
  332. free(password);
  333. }
  334. return retcode;
  335. }
  336. const char *Curl_netrc_strerror(NETRCcode ret)
  337. {
  338. switch(ret) {
  339. default:
  340. return ""; /* not a legit error */
  341. case NETRC_FILE_MISSING:
  342. return "no such file";
  343. case NETRC_NO_MATCH:
  344. return "no matching entry";
  345. case NETRC_OUT_OF_MEMORY:
  346. return "out of memory";
  347. case NETRC_SYNTAX_ERROR:
  348. return "syntax error";
  349. }
  350. /* never reached */
  351. }
  352. /*
  353. * @unittest: 1304
  354. *
  355. * *loginp and *passwordp MUST be allocated if they are not NULL when passed
  356. * in.
  357. */
  358. NETRCcode Curl_parsenetrc(struct store_netrc *store, const char *host,
  359. char **loginp, char **passwordp,
  360. char *netrcfile)
  361. {
  362. NETRCcode retcode = NETRC_OK;
  363. char *filealloc = NULL;
  364. if(!netrcfile) {
  365. #if defined(HAVE_GETPWUID_R) && defined(HAVE_GETEUID)
  366. char pwbuf[1024];
  367. #endif
  368. char *home = NULL;
  369. char *homea = curl_getenv("HOME"); /* portable environment reader */
  370. if(homea) {
  371. home = homea;
  372. #if defined(HAVE_GETPWUID_R) && defined(HAVE_GETEUID)
  373. }
  374. else {
  375. struct passwd pw, *pw_res;
  376. if(!getpwuid_r(geteuid(), &pw, pwbuf, sizeof(pwbuf), &pw_res)
  377. && pw_res) {
  378. home = pw.pw_dir;
  379. }
  380. #elif defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
  381. }
  382. else {
  383. struct passwd *pw;
  384. pw = getpwuid(geteuid());
  385. if(pw) {
  386. home = pw->pw_dir;
  387. }
  388. #elif defined(_WIN32)
  389. }
  390. else {
  391. homea = curl_getenv("USERPROFILE");
  392. if(homea) {
  393. home = homea;
  394. }
  395. #endif
  396. }
  397. if(!home)
  398. return NETRC_FILE_MISSING; /* no home directory found (or possibly out
  399. of memory) */
  400. filealloc = aprintf("%s%s.netrc", home, DIR_CHAR);
  401. if(!filealloc) {
  402. free(homea);
  403. return NETRC_OUT_OF_MEMORY;
  404. }
  405. retcode = parsenetrc(store, host, loginp, passwordp, filealloc);
  406. free(filealloc);
  407. #ifdef _WIN32
  408. if(retcode == NETRC_FILE_MISSING) {
  409. /* fallback to the old-style "_netrc" file */
  410. filealloc = aprintf("%s%s_netrc", home, DIR_CHAR);
  411. if(!filealloc) {
  412. free(homea);
  413. return NETRC_OUT_OF_MEMORY;
  414. }
  415. retcode = parsenetrc(store, host, loginp, passwordp, filealloc);
  416. free(filealloc);
  417. }
  418. #endif
  419. free(homea);
  420. }
  421. else
  422. retcode = parsenetrc(store, host, loginp, passwordp, netrcfile);
  423. return retcode;
  424. }
  425. void Curl_netrc_init(struct store_netrc *s)
  426. {
  427. Curl_dyn_init(&s->filebuf, MAX_NETRC_FILE);
  428. s->loaded = FALSE;
  429. }
  430. void Curl_netrc_cleanup(struct store_netrc *s)
  431. {
  432. Curl_dyn_free(&s->filebuf);
  433. s->loaded = FALSE;
  434. }
  435. #endif