2
0

netrc.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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 "strtok.h"
  32. #include "strcase.h"
  33. #include "curl_get_line.h"
  34. /* The last 3 #include files should be in this order */
  35. #include "curl_printf.h"
  36. #include "curl_memory.h"
  37. #include "memdebug.h"
  38. /* Get user and password from .netrc when given a machine name */
  39. enum host_lookup_state {
  40. NOTHING,
  41. HOSTFOUND, /* the 'machine' keyword was found */
  42. HOSTVALID, /* this is "our" machine! */
  43. MACDEF
  44. };
  45. #define NETRC_FILE_MISSING 1
  46. #define NETRC_FAILED -1
  47. #define NETRC_SUCCESS 0
  48. #define MAX_NETRC_LINE 4096
  49. /*
  50. * Returns zero on success.
  51. */
  52. static int parsenetrc(const char *host,
  53. char **loginp,
  54. char **passwordp,
  55. char *netrcfile)
  56. {
  57. FILE *file;
  58. int retcode = NETRC_FILE_MISSING;
  59. char *login = *loginp;
  60. char *password = *passwordp;
  61. bool specific_login = (login && *login != 0);
  62. bool login_alloc = FALSE;
  63. bool password_alloc = FALSE;
  64. enum host_lookup_state state = NOTHING;
  65. char state_login = 0; /* Found a login keyword */
  66. char state_password = 0; /* Found a password keyword */
  67. bool state_our_login = TRUE; /* With specific_login, found *our* login
  68. name (or login-less line) */
  69. DEBUGASSERT(netrcfile);
  70. file = fopen(netrcfile, FOPEN_READTEXT);
  71. if(file) {
  72. bool done = FALSE;
  73. struct dynbuf buf;
  74. Curl_dyn_init(&buf, MAX_NETRC_LINE);
  75. while(!done && Curl_get_line(&buf, file)) {
  76. char *tok;
  77. char *tok_end;
  78. bool quoted;
  79. char *netrcbuffer = Curl_dyn_ptr(&buf);
  80. if(state == MACDEF) {
  81. if((netrcbuffer[0] == '\n') || (netrcbuffer[0] == '\r'))
  82. state = NOTHING;
  83. else
  84. continue;
  85. }
  86. tok = netrcbuffer;
  87. while(tok) {
  88. while(ISBLANK(*tok))
  89. tok++;
  90. /* tok is first non-space letter */
  91. if(!*tok || (*tok == '#'))
  92. /* end of line or the rest is a comment */
  93. break;
  94. /* leading double-quote means quoted string */
  95. quoted = (*tok == '\"');
  96. tok_end = tok;
  97. if(!quoted) {
  98. while(!ISSPACE(*tok_end))
  99. tok_end++;
  100. *tok_end = 0;
  101. }
  102. else {
  103. bool escape = FALSE;
  104. bool endquote = FALSE;
  105. char *store = tok;
  106. tok_end++; /* pass the leading quote */
  107. while(*tok_end) {
  108. char s = *tok_end;
  109. if(escape) {
  110. escape = FALSE;
  111. switch(s) {
  112. case 'n':
  113. s = '\n';
  114. break;
  115. case 'r':
  116. s = '\r';
  117. break;
  118. case 't':
  119. s = '\t';
  120. break;
  121. }
  122. }
  123. else if(s == '\\') {
  124. escape = TRUE;
  125. tok_end++;
  126. continue;
  127. }
  128. else if(s == '\"') {
  129. tok_end++; /* pass the ending quote */
  130. endquote = TRUE;
  131. break;
  132. }
  133. *store++ = s;
  134. tok_end++;
  135. }
  136. *store = 0;
  137. if(escape || !endquote) {
  138. /* bad syntax, get out */
  139. retcode = NETRC_FAILED;
  140. goto out;
  141. }
  142. }
  143. if((login && *login) && (password && *password)) {
  144. done = TRUE;
  145. break;
  146. }
  147. switch(state) {
  148. case NOTHING:
  149. if(strcasecompare("macdef", tok)) {
  150. /* Define a macro. A macro is defined with the specified name; its
  151. contents begin with the next .netrc line and continue until a
  152. null line (consecutive new-line characters) is encountered. */
  153. state = MACDEF;
  154. }
  155. else if(strcasecompare("machine", tok)) {
  156. /* the next tok is the machine name, this is in itself the
  157. delimiter that starts the stuff entered for this machine,
  158. after this we need to search for 'login' and
  159. 'password'. */
  160. state = HOSTFOUND;
  161. }
  162. else if(strcasecompare("default", tok)) {
  163. state = HOSTVALID;
  164. retcode = NETRC_SUCCESS; /* we did find our host */
  165. }
  166. break;
  167. case MACDEF:
  168. if(!strlen(tok)) {
  169. state = NOTHING;
  170. }
  171. break;
  172. case HOSTFOUND:
  173. if(strcasecompare(host, tok)) {
  174. /* and yes, this is our host! */
  175. state = HOSTVALID;
  176. retcode = NETRC_SUCCESS; /* we did find our host */
  177. }
  178. else
  179. /* not our host */
  180. state = NOTHING;
  181. break;
  182. case HOSTVALID:
  183. /* we are now parsing sub-keywords concerning "our" host */
  184. if(state_login) {
  185. if(specific_login) {
  186. state_our_login = !Curl_timestrcmp(login, tok);
  187. }
  188. else if(!login || Curl_timestrcmp(login, tok)) {
  189. if(login_alloc) {
  190. free(login);
  191. login_alloc = FALSE;
  192. }
  193. login = strdup(tok);
  194. if(!login) {
  195. retcode = NETRC_FAILED; /* allocation failed */
  196. goto out;
  197. }
  198. login_alloc = TRUE;
  199. }
  200. state_login = 0;
  201. }
  202. else if(state_password) {
  203. if((state_our_login || !specific_login)
  204. && (!password || Curl_timestrcmp(password, tok))) {
  205. if(password_alloc) {
  206. free(password);
  207. password_alloc = FALSE;
  208. }
  209. password = strdup(tok);
  210. if(!password) {
  211. retcode = NETRC_FAILED; /* allocation failed */
  212. goto out;
  213. }
  214. password_alloc = TRUE;
  215. }
  216. state_password = 0;
  217. }
  218. else if(strcasecompare("login", tok))
  219. state_login = 1;
  220. else if(strcasecompare("password", tok))
  221. state_password = 1;
  222. else if(strcasecompare("machine", tok)) {
  223. /* ok, there is machine here go => */
  224. state = HOSTFOUND;
  225. state_our_login = FALSE;
  226. }
  227. break;
  228. } /* switch (state) */
  229. tok = ++tok_end;
  230. }
  231. } /* while Curl_get_line() */
  232. out:
  233. Curl_dyn_free(&buf);
  234. if(!retcode) {
  235. /* success */
  236. if(login_alloc) {
  237. if(*loginp)
  238. free(*loginp);
  239. *loginp = login;
  240. }
  241. if(password_alloc) {
  242. if(*passwordp)
  243. free(*passwordp);
  244. *passwordp = password;
  245. }
  246. }
  247. else {
  248. if(login_alloc)
  249. free(login);
  250. if(password_alloc)
  251. free(password);
  252. }
  253. fclose(file);
  254. }
  255. return retcode;
  256. }
  257. /*
  258. * @unittest: 1304
  259. *
  260. * *loginp and *passwordp MUST be allocated if they are not NULL when passed
  261. * in.
  262. */
  263. int Curl_parsenetrc(const char *host, char **loginp, char **passwordp,
  264. char *netrcfile)
  265. {
  266. int retcode = 1;
  267. char *filealloc = NULL;
  268. if(!netrcfile) {
  269. #if defined(HAVE_GETPWUID_R) && defined(HAVE_GETEUID)
  270. char pwbuf[1024];
  271. #endif
  272. char *home = NULL;
  273. char *homea = curl_getenv("HOME"); /* portable environment reader */
  274. if(homea) {
  275. home = homea;
  276. #if defined(HAVE_GETPWUID_R) && defined(HAVE_GETEUID)
  277. }
  278. else {
  279. struct passwd pw, *pw_res;
  280. if(!getpwuid_r(geteuid(), &pw, pwbuf, sizeof(pwbuf), &pw_res)
  281. && pw_res) {
  282. home = pw.pw_dir;
  283. }
  284. #elif defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
  285. }
  286. else {
  287. struct passwd *pw;
  288. pw = getpwuid(geteuid());
  289. if(pw) {
  290. home = pw->pw_dir;
  291. }
  292. #elif defined(_WIN32)
  293. }
  294. else {
  295. homea = curl_getenv("USERPROFILE");
  296. if(homea) {
  297. home = homea;
  298. }
  299. #endif
  300. }
  301. if(!home)
  302. return retcode; /* no home directory found (or possibly out of
  303. memory) */
  304. filealloc = aprintf("%s%s.netrc", home, DIR_CHAR);
  305. if(!filealloc) {
  306. free(homea);
  307. return -1;
  308. }
  309. retcode = parsenetrc(host, loginp, passwordp, filealloc);
  310. free(filealloc);
  311. #ifdef _WIN32
  312. if(retcode == NETRC_FILE_MISSING) {
  313. /* fallback to the old-style "_netrc" file */
  314. filealloc = aprintf("%s%s_netrc", home, DIR_CHAR);
  315. if(!filealloc) {
  316. free(homea);
  317. return -1;
  318. }
  319. retcode = parsenetrc(host, loginp, passwordp, filealloc);
  320. free(filealloc);
  321. }
  322. #endif
  323. free(homea);
  324. }
  325. else
  326. retcode = parsenetrc(host, loginp, passwordp, netrcfile);
  327. return retcode;
  328. }
  329. #endif