netrc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. found |= FOUND_PASSWORD;
  250. keyword = NONE;
  251. }
  252. else if(strcasecompare("login", tok))
  253. keyword = LOGIN;
  254. else if(strcasecompare("password", tok))
  255. keyword = PASSWORD;
  256. else if(strcasecompare("machine", tok)) {
  257. /* a new machine here */
  258. state = HOSTFOUND;
  259. keyword = NONE;
  260. found = 0;
  261. Curl_safefree(password);
  262. if(!specific_login)
  263. Curl_safefree(login);
  264. }
  265. else if(strcasecompare("default", tok)) {
  266. state = HOSTVALID;
  267. retcode = NETRC_SUCCESS; /* we did find our host */
  268. Curl_safefree(password);
  269. if(!specific_login)
  270. Curl_safefree(login);
  271. }
  272. if((found == (FOUND_PASSWORD|FOUND_LOGIN)) && our_login) {
  273. done = TRUE;
  274. break;
  275. }
  276. break;
  277. } /* switch (state) */
  278. tok = ++tok_end;
  279. }
  280. if(!done) {
  281. char *nl = NULL;
  282. if(tok)
  283. nl = strchr(tok, '\n');
  284. if(!nl)
  285. break;
  286. /* point to next line */
  287. netrcbuffer = &nl[1];
  288. }
  289. } /* while !done */
  290. out:
  291. Curl_dyn_free(&token);
  292. if(!retcode && !password && our_login) {
  293. /* success without a password, set a blank one */
  294. password = strdup("");
  295. if(!password)
  296. retcode = 1; /* out of memory */
  297. }
  298. if(!retcode) {
  299. /* success */
  300. if(!specific_login)
  301. *loginp = login;
  302. *passwordp = password;
  303. }
  304. else {
  305. Curl_dyn_free(filebuf);
  306. if(!specific_login)
  307. free(login);
  308. free(password);
  309. }
  310. return retcode;
  311. }
  312. /*
  313. * @unittest: 1304
  314. *
  315. * *loginp and *passwordp MUST be allocated if they are not NULL when passed
  316. * in.
  317. */
  318. int Curl_parsenetrc(struct store_netrc *store, const char *host,
  319. char **loginp, char **passwordp,
  320. char *netrcfile)
  321. {
  322. int retcode = 1;
  323. char *filealloc = NULL;
  324. if(!netrcfile) {
  325. #if defined(HAVE_GETPWUID_R) && defined(HAVE_GETEUID)
  326. char pwbuf[1024];
  327. #endif
  328. char *home = NULL;
  329. char *homea = curl_getenv("HOME"); /* portable environment reader */
  330. if(homea) {
  331. home = homea;
  332. #if defined(HAVE_GETPWUID_R) && defined(HAVE_GETEUID)
  333. }
  334. else {
  335. struct passwd pw, *pw_res;
  336. if(!getpwuid_r(geteuid(), &pw, pwbuf, sizeof(pwbuf), &pw_res)
  337. && pw_res) {
  338. home = pw.pw_dir;
  339. }
  340. #elif defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
  341. }
  342. else {
  343. struct passwd *pw;
  344. pw = getpwuid(geteuid());
  345. if(pw) {
  346. home = pw->pw_dir;
  347. }
  348. #elif defined(_WIN32)
  349. }
  350. else {
  351. homea = curl_getenv("USERPROFILE");
  352. if(homea) {
  353. home = homea;
  354. }
  355. #endif
  356. }
  357. if(!home)
  358. return retcode; /* no home directory found (or possibly out of
  359. memory) */
  360. filealloc = aprintf("%s%s.netrc", home, DIR_CHAR);
  361. if(!filealloc) {
  362. free(homea);
  363. return -1;
  364. }
  365. retcode = parsenetrc(store, host, loginp, passwordp, filealloc);
  366. free(filealloc);
  367. #ifdef _WIN32
  368. if((retcode == NETRC_FILE_MISSING) || (retcode == NETRC_FAILED)) {
  369. /* fallback to the old-style "_netrc" file */
  370. filealloc = aprintf("%s%s_netrc", home, DIR_CHAR);
  371. if(!filealloc) {
  372. free(homea);
  373. return -1;
  374. }
  375. retcode = parsenetrc(store, host, loginp, passwordp, filealloc);
  376. free(filealloc);
  377. }
  378. #endif
  379. free(homea);
  380. }
  381. else
  382. retcode = parsenetrc(store, host, loginp, passwordp, netrcfile);
  383. return retcode;
  384. }
  385. void Curl_netrc_init(struct store_netrc *s)
  386. {
  387. Curl_dyn_init(&s->filebuf, MAX_NETRC_FILE);
  388. s->loaded = FALSE;
  389. }
  390. void Curl_netrc_cleanup(struct store_netrc *s)
  391. {
  392. Curl_dyn_free(&s->filebuf);
  393. s->loaded = FALSE;
  394. }
  395. #endif