netrc.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2016, Daniel Stenberg, <[email protected]>, 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. while(!done && tok) {
  109. if((*loginp && **loginp) && (*passwordp && **passwordp)) {
  110. done=TRUE;
  111. break;
  112. }
  113. switch(state) {
  114. case NOTHING:
  115. if(strcasecompare("machine", tok)) {
  116. /* the next tok is the machine name, this is in itself the
  117. delimiter that starts the stuff entered for this machine,
  118. after this we need to search for 'login' and
  119. 'password'. */
  120. state=HOSTFOUND;
  121. }
  122. else if(strcasecompare("default", tok)) {
  123. state=HOSTVALID;
  124. retcode=0; /* we did find our host */
  125. }
  126. break;
  127. case HOSTFOUND:
  128. if(strcasecompare(host, tok)) {
  129. /* and yes, this is our host! */
  130. state=HOSTVALID;
  131. retcode=0; /* we did find our host */
  132. }
  133. else
  134. /* not our host */
  135. state=NOTHING;
  136. break;
  137. case HOSTVALID:
  138. /* we are now parsing sub-keywords concerning "our" host */
  139. if(state_login) {
  140. if(specific_login) {
  141. state_our_login = strcasecompare(*loginp, tok);
  142. }
  143. else {
  144. free(*loginp);
  145. *loginp = strdup(tok);
  146. if(!*loginp) {
  147. retcode = -1; /* allocation failed */
  148. goto out;
  149. }
  150. }
  151. state_login=0;
  152. }
  153. else if(state_password) {
  154. if(state_our_login || !specific_login) {
  155. free(*passwordp);
  156. *passwordp = strdup(tok);
  157. if(!*passwordp) {
  158. retcode = -1; /* allocation failed */
  159. goto out;
  160. }
  161. }
  162. state_password=0;
  163. }
  164. else if(strcasecompare("login", tok))
  165. state_login=1;
  166. else if(strcasecompare("password", tok))
  167. state_password=1;
  168. else if(strcasecompare("machine", tok)) {
  169. /* ok, there's machine here go => */
  170. state = HOSTFOUND;
  171. state_our_login = FALSE;
  172. }
  173. break;
  174. } /* switch (state) */
  175. tok = strtok_r(NULL, " \t\n", &tok_buf);
  176. } /* while(tok) */
  177. } /* while fgets() */
  178. out:
  179. fclose(file);
  180. }
  181. return retcode;
  182. }