netrc.c 11 KB

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