netrc.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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
  59. name */
  60. #define NETRC DOT_CHAR "netrc"
  61. if(!netrcfile) {
  62. bool home_alloc = FALSE;
  63. char *home = curl_getenv("HOME"); /* portable environment reader */
  64. if(home) {
  65. home_alloc = TRUE;
  66. #if defined(HAVE_GETPWUID_R) && defined(HAVE_GETEUID)
  67. }
  68. else {
  69. struct passwd pw, *pw_res;
  70. char pwbuf[1024];
  71. if(!getpwuid_r(geteuid(), &pw, pwbuf, sizeof(pwbuf), &pw_res)
  72. && pw_res) {
  73. home = strdup(pw.pw_dir);
  74. if(!home)
  75. return CURLE_OUT_OF_MEMORY;
  76. home_alloc = TRUE;
  77. }
  78. #elif defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
  79. }
  80. else {
  81. struct passwd *pw;
  82. pw = getpwuid(geteuid());
  83. if(pw) {
  84. home = pw->pw_dir;
  85. }
  86. #endif
  87. }
  88. if(!home)
  89. return retcode; /* no home directory found (or possibly out of memory) */
  90. netrcfile = curl_maprintf("%s%s%s", home, DIR_CHAR, NETRC);
  91. if(home_alloc)
  92. free(home);
  93. if(!netrcfile) {
  94. return -1;
  95. }
  96. netrc_alloc = TRUE;
  97. }
  98. file = fopen(netrcfile, FOPEN_READTEXT);
  99. if(netrc_alloc)
  100. free(netrcfile);
  101. if(file) {
  102. char *tok;
  103. char *tok_buf;
  104. bool done = FALSE;
  105. char netrcbuffer[256];
  106. int netrcbuffsize = (int)sizeof(netrcbuffer);
  107. while(!done && fgets(netrcbuffer, netrcbuffsize, file)) {
  108. tok = strtok_r(netrcbuffer, " \t\n", &tok_buf);
  109. if(tok && *tok == '#')
  110. /* treat an initial hash as a comment line */
  111. continue;
  112. while(!done && tok) {
  113. if((*loginp && **loginp) && (*passwordp && **passwordp)) {
  114. done = TRUE;
  115. break;
  116. }
  117. switch(state) {
  118. case NOTHING:
  119. if(strcasecompare("machine", tok)) {
  120. /* the next tok is the machine name, this is in itself the
  121. delimiter that starts the stuff entered for this machine,
  122. after this we need to search for 'login' and
  123. 'password'. */
  124. state = HOSTFOUND;
  125. }
  126. else if(strcasecompare("default", tok)) {
  127. state = HOSTVALID;
  128. retcode = 0; /* we did find our host */
  129. }
  130. break;
  131. case HOSTFOUND:
  132. if(strcasecompare(host, tok)) {
  133. /* and yes, this is our host! */
  134. state = HOSTVALID;
  135. retcode = 0; /* we did find our host */
  136. }
  137. else
  138. /* not our host */
  139. state = NOTHING;
  140. break;
  141. case HOSTVALID:
  142. /* we are now parsing sub-keywords concerning "our" host */
  143. if(state_login) {
  144. if(specific_login) {
  145. state_our_login = strcasecompare(*loginp, tok);
  146. }
  147. else {
  148. free(*loginp);
  149. *loginp = strdup(tok);
  150. if(!*loginp) {
  151. retcode = -1; /* allocation failed */
  152. goto out;
  153. }
  154. }
  155. state_login = 0;
  156. }
  157. else if(state_password) {
  158. if(state_our_login || !specific_login) {
  159. free(*passwordp);
  160. *passwordp = strdup(tok);
  161. if(!*passwordp) {
  162. retcode = -1; /* allocation failed */
  163. goto out;
  164. }
  165. }
  166. state_password = 0;
  167. }
  168. else if(strcasecompare("login", tok))
  169. state_login = 1;
  170. else if(strcasecompare("password", tok))
  171. state_password = 1;
  172. else if(strcasecompare("machine", tok)) {
  173. /* ok, there's machine here go => */
  174. state = HOSTFOUND;
  175. state_our_login = FALSE;
  176. }
  177. break;
  178. } /* switch (state) */
  179. tok = strtok_r(NULL, " \t\n", &tok_buf);
  180. } /* while(tok) */
  181. } /* while fgets() */
  182. out:
  183. fclose(file);
  184. }
  185. return retcode;
  186. }