2
0

netrc.c 7.8 KB

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