netrc.c 9.5 KB

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