netrc.c 10 KB

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