netrc.c 5.2 KB

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