netrc.c 5.0 KB

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