netrc.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, 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. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #ifndef CURL_DISABLE_NETRC
  24. #ifdef HAVE_PWD_H
  25. #include <pwd.h>
  26. #endif
  27. #include <curl/curl.h>
  28. #include "netrc.h"
  29. #include "strtok.h"
  30. #include "strcase.h"
  31. /* The last 3 #include files should be in this order */
  32. #include "curl_printf.h"
  33. #include "curl_memory.h"
  34. #include "memdebug.h"
  35. /* Get user and password from .netrc when given a machine name */
  36. enum host_lookup_state {
  37. NOTHING,
  38. HOSTFOUND, /* the 'machine' keyword was found */
  39. HOSTVALID, /* this is "our" machine! */
  40. MACDEF
  41. };
  42. #define NETRC_FILE_MISSING 1
  43. #define NETRC_FAILED -1
  44. #define NETRC_SUCCESS 0
  45. /*
  46. * Returns zero on success.
  47. */
  48. static int parsenetrc(const char *host,
  49. char **loginp,
  50. char **passwordp,
  51. bool *login_changed,
  52. bool *password_changed,
  53. char *netrcfile)
  54. {
  55. FILE *file;
  56. int retcode = NETRC_FILE_MISSING;
  57. char *login = *loginp;
  58. char *password = *passwordp;
  59. bool specific_login = (login && *login != 0);
  60. bool login_alloc = FALSE;
  61. bool password_alloc = FALSE;
  62. enum host_lookup_state state = NOTHING;
  63. char state_login = 0; /* Found a login keyword */
  64. char state_password = 0; /* Found a password keyword */
  65. int state_our_login = FALSE; /* With specific_login, found *our* login
  66. name */
  67. DEBUGASSERT(netrcfile);
  68. file = fopen(netrcfile, FOPEN_READTEXT);
  69. if(file) {
  70. char *tok;
  71. char *tok_buf;
  72. bool done = FALSE;
  73. char netrcbuffer[4096];
  74. int netrcbuffsize = (int)sizeof(netrcbuffer);
  75. while(!done && fgets(netrcbuffer, netrcbuffsize, file)) {
  76. if(state == MACDEF) {
  77. if((netrcbuffer[0] == '\n') || (netrcbuffer[0] == '\r'))
  78. state = NOTHING;
  79. else
  80. continue;
  81. }
  82. tok = strtok_r(netrcbuffer, " \t\n", &tok_buf);
  83. if(tok && *tok == '#')
  84. /* treat an initial hash as a comment line */
  85. continue;
  86. while(tok) {
  87. if((login && *login) && (password && *password)) {
  88. done = TRUE;
  89. break;
  90. }
  91. switch(state) {
  92. case NOTHING:
  93. if(strcasecompare("macdef", tok)) {
  94. /* Define a macro. A macro is defined with the specified name; its
  95. contents begin with the next .netrc line and continue until a
  96. null line (consecutive new-line characters) is encountered. */
  97. state = MACDEF;
  98. }
  99. else if(strcasecompare("machine", tok)) {
  100. /* the next tok is the machine name, this is in itself the
  101. delimiter that starts the stuff entered for this machine,
  102. after this we need to search for 'login' and
  103. 'password'. */
  104. state = HOSTFOUND;
  105. }
  106. else if(strcasecompare("default", tok)) {
  107. state = HOSTVALID;
  108. retcode = NETRC_SUCCESS; /* we did find our host */
  109. }
  110. break;
  111. case MACDEF:
  112. if(!strlen(tok)) {
  113. state = NOTHING;
  114. }
  115. break;
  116. case HOSTFOUND:
  117. if(strcasecompare(host, tok)) {
  118. /* and yes, this is our host! */
  119. state = HOSTVALID;
  120. retcode = NETRC_SUCCESS; /* we did find our host */
  121. }
  122. else
  123. /* not our host */
  124. state = NOTHING;
  125. break;
  126. case HOSTVALID:
  127. /* we are now parsing sub-keywords concerning "our" host */
  128. if(state_login) {
  129. if(specific_login) {
  130. state_our_login = strcasecompare(login, tok);
  131. }
  132. else if(!login || strcmp(login, tok)) {
  133. if(login_alloc) {
  134. free(login);
  135. login_alloc = FALSE;
  136. }
  137. login = strdup(tok);
  138. if(!login) {
  139. retcode = NETRC_FAILED; /* allocation failed */
  140. goto out;
  141. }
  142. login_alloc = TRUE;
  143. }
  144. state_login = 0;
  145. }
  146. else if(state_password) {
  147. if((state_our_login || !specific_login)
  148. && (!password || strcmp(password, tok))) {
  149. if(password_alloc) {
  150. free(password);
  151. password_alloc = FALSE;
  152. }
  153. password = strdup(tok);
  154. if(!password) {
  155. retcode = NETRC_FAILED; /* allocation failed */
  156. goto out;
  157. }
  158. password_alloc = TRUE;
  159. }
  160. state_password = 0;
  161. }
  162. else if(strcasecompare("login", tok))
  163. state_login = 1;
  164. else if(strcasecompare("password", tok))
  165. state_password = 1;
  166. else if(strcasecompare("machine", tok)) {
  167. /* ok, there's machine here go => */
  168. state = HOSTFOUND;
  169. state_our_login = FALSE;
  170. }
  171. break;
  172. } /* switch (state) */
  173. tok = strtok_r(NULL, " \t\n", &tok_buf);
  174. } /* while(tok) */
  175. } /* while fgets() */
  176. out:
  177. if(!retcode) {
  178. /* success */
  179. *login_changed = FALSE;
  180. *password_changed = FALSE;
  181. if(login_alloc) {
  182. if(*loginp)
  183. free(*loginp);
  184. *loginp = login;
  185. *login_changed = TRUE;
  186. }
  187. if(password_alloc) {
  188. if(*passwordp)
  189. free(*passwordp);
  190. *passwordp = password;
  191. *password_changed = TRUE;
  192. }
  193. }
  194. else {
  195. if(login_alloc)
  196. free(login);
  197. if(password_alloc)
  198. free(password);
  199. }
  200. fclose(file);
  201. }
  202. return retcode;
  203. }
  204. /*
  205. * @unittest: 1304
  206. *
  207. * *loginp and *passwordp MUST be allocated if they aren't NULL when passed
  208. * in.
  209. */
  210. int Curl_parsenetrc(const char *host,
  211. char **loginp,
  212. char **passwordp,
  213. bool *login_changed,
  214. bool *password_changed,
  215. char *netrcfile)
  216. {
  217. int retcode = 1;
  218. char *filealloc = NULL;
  219. if(!netrcfile) {
  220. #if defined(HAVE_GETPWUID_R) && defined(HAVE_GETEUID)
  221. char pwbuf[1024];
  222. #endif
  223. char *home = NULL;
  224. char *homea = curl_getenv("HOME"); /* portable environment reader */
  225. if(homea) {
  226. home = homea;
  227. #if defined(HAVE_GETPWUID_R) && defined(HAVE_GETEUID)
  228. }
  229. else {
  230. struct passwd pw, *pw_res;
  231. if(!getpwuid_r(geteuid(), &pw, pwbuf, sizeof(pwbuf), &pw_res)
  232. && pw_res) {
  233. home = pw.pw_dir;
  234. }
  235. #elif defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
  236. }
  237. else {
  238. struct passwd *pw;
  239. pw = getpwuid(geteuid());
  240. if(pw) {
  241. home = pw->pw_dir;
  242. }
  243. #endif
  244. }
  245. if(!home)
  246. return retcode; /* no home directory found (or possibly out of
  247. memory) */
  248. filealloc = curl_maprintf("%s%s.netrc", home, DIR_CHAR);
  249. if(!filealloc) {
  250. free(homea);
  251. return -1;
  252. }
  253. retcode = parsenetrc(host, loginp, passwordp, login_changed,
  254. password_changed, filealloc);
  255. free(filealloc);
  256. #ifdef WIN32
  257. if(retcode == NETRC_FILE_MISSING) {
  258. /* fallback to the old-style "_netrc" file */
  259. filealloc = curl_maprintf("%s%s_netrc", home, DIR_CHAR);
  260. if(!filealloc) {
  261. free(homea);
  262. return -1;
  263. }
  264. retcode = parsenetrc(host, loginp, passwordp, login_changed,
  265. password_changed, filealloc);
  266. free(filealloc);
  267. }
  268. #endif
  269. free(homea);
  270. }
  271. else
  272. retcode = parsenetrc(host, loginp, passwordp, login_changed,
  273. password_changed, netrcfile);
  274. return retcode;
  275. }
  276. #endif