netrc.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2017, 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. #ifdef HAVE_PWD_H
  24. #include <pwd.h>
  25. #endif
  26. #include <curl/curl.h>
  27. #include "netrc.h"
  28. #include "strtok.h"
  29. #include "strcase.h"
  30. /* The last 3 #include files should be in this order */
  31. #include "curl_printf.h"
  32. #include "curl_memory.h"
  33. #include "memdebug.h"
  34. /* Get user and password from .netrc when given a machine name */
  35. enum host_lookup_state {
  36. NOTHING,
  37. HOSTFOUND, /* the 'machine' keyword was found */
  38. HOSTVALID /* this is "our" machine! */
  39. };
  40. /*
  41. * @unittest: 1304
  42. *
  43. * *loginp and *passwordp MUST be allocated if they aren't NULL when passed
  44. * in.
  45. */
  46. int Curl_parsenetrc(const char *host,
  47. char **loginp,
  48. char **passwordp,
  49. char *netrcfile)
  50. {
  51. FILE *file;
  52. int retcode=1;
  53. int specific_login = (*loginp && **loginp != 0);
  54. bool netrc_alloc = FALSE;
  55. enum host_lookup_state state=NOTHING;
  56. char state_login=0; /* Found a login keyword */
  57. char state_password=0; /* Found a password keyword */
  58. int state_our_login=FALSE; /* With specific_login, found *our* login name */
  59. #define NETRC DOT_CHAR "netrc"
  60. if(!netrcfile) {
  61. bool home_alloc = FALSE;
  62. char *home = curl_getenv("HOME"); /* portable environment reader */
  63. if(home) {
  64. home_alloc = TRUE;
  65. #if defined(HAVE_GETPWUID_R) && defined(HAVE_GETEUID)
  66. }
  67. else {
  68. struct passwd pw, *pw_res;
  69. char pwbuf[1024];
  70. if(!getpwuid_r(geteuid(), &pw, pwbuf, sizeof(pwbuf), &pw_res)
  71. && pw_res) {
  72. home = strdup(pw.pw_dir);
  73. if(!home)
  74. return CURLE_OUT_OF_MEMORY;
  75. home_alloc = TRUE;
  76. }
  77. #elif defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
  78. }
  79. else {
  80. struct passwd *pw;
  81. pw= getpwuid(geteuid());
  82. if(pw) {
  83. home = pw->pw_dir;
  84. }
  85. #endif
  86. }
  87. if(!home)
  88. return retcode; /* no home directory found (or possibly out of memory) */
  89. netrcfile = curl_maprintf("%s%s%s", home, DIR_CHAR, NETRC);
  90. if(home_alloc)
  91. free(home);
  92. if(!netrcfile) {
  93. return -1;
  94. }
  95. netrc_alloc = TRUE;
  96. }
  97. file = fopen(netrcfile, FOPEN_READTEXT);
  98. if(netrc_alloc)
  99. free(netrcfile);
  100. if(file) {
  101. char *tok;
  102. char *tok_buf;
  103. bool done=FALSE;
  104. char netrcbuffer[256];
  105. int netrcbuffsize = (int)sizeof(netrcbuffer);
  106. while(!done && fgets(netrcbuffer, netrcbuffsize, file)) {
  107. tok=strtok_r(netrcbuffer, " \t\n", &tok_buf);
  108. if(tok && *tok == '#')
  109. /* treat an initial hash as a comment line */
  110. continue;
  111. while(!done && tok) {
  112. if((*loginp && **loginp) && (*passwordp && **passwordp)) {
  113. done=TRUE;
  114. break;
  115. }
  116. switch(state) {
  117. case NOTHING:
  118. if(strcasecompare("machine", tok)) {
  119. /* the next tok is the machine name, this is in itself the
  120. delimiter that starts the stuff entered for this machine,
  121. after this we need to search for 'login' and
  122. 'password'. */
  123. state=HOSTFOUND;
  124. }
  125. else if(strcasecompare("default", tok)) {
  126. state=HOSTVALID;
  127. retcode=0; /* we did find our host */
  128. }
  129. break;
  130. case HOSTFOUND:
  131. if(strcasecompare(host, tok)) {
  132. /* and yes, this is our host! */
  133. state=HOSTVALID;
  134. retcode=0; /* we did find our host */
  135. }
  136. else
  137. /* not our host */
  138. state=NOTHING;
  139. break;
  140. case HOSTVALID:
  141. /* we are now parsing sub-keywords concerning "our" host */
  142. if(state_login) {
  143. if(specific_login) {
  144. state_our_login = strcasecompare(*loginp, tok);
  145. }
  146. else {
  147. free(*loginp);
  148. *loginp = strdup(tok);
  149. if(!*loginp) {
  150. retcode = -1; /* allocation failed */
  151. goto out;
  152. }
  153. }
  154. state_login=0;
  155. }
  156. else if(state_password) {
  157. if(state_our_login || !specific_login) {
  158. free(*passwordp);
  159. *passwordp = strdup(tok);
  160. if(!*passwordp) {
  161. retcode = -1; /* allocation failed */
  162. goto out;
  163. }
  164. }
  165. state_password=0;
  166. }
  167. else if(strcasecompare("login", tok))
  168. state_login=1;
  169. else if(strcasecompare("password", tok))
  170. state_password=1;
  171. else if(strcasecompare("machine", tok)) {
  172. /* ok, there's machine here go => */
  173. state = HOSTFOUND;
  174. state_our_login = FALSE;
  175. }
  176. break;
  177. } /* switch (state) */
  178. tok = strtok_r(NULL, " \t\n", &tok_buf);
  179. } /* while(tok) */
  180. } /* while fgets() */
  181. out:
  182. fclose(file);
  183. }
  184. return retcode;
  185. }