netrc.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 "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. int Curl_parsenetrc(const char *host,
  46. char *login,
  47. char *password,
  48. char *netrcfile)
  49. {
  50. FILE *file;
  51. int retcode=1;
  52. int specific_login = (login[0] != 0);
  53. char *home = NULL;
  54. bool home_alloc = FALSE;
  55. bool netrc_alloc = FALSE;
  56. enum host_lookup_state state=NOTHING;
  57. char state_login=0; /* Found a login keyword */
  58. char state_password=0; /* Found a password keyword */
  59. int state_our_login=FALSE; /* With specific_login, found *our* login name */
  60. #define NETRC DOT_CHAR "netrc"
  61. if(!netrcfile) {
  62. home = curl_getenv("HOME"); /* portable environment reader */
  63. if(home) {
  64. home_alloc = TRUE;
  65. #if defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
  66. }
  67. else {
  68. struct passwd *pw;
  69. pw= getpwuid(geteuid());
  70. if(pw) {
  71. home = pw->pw_dir;
  72. }
  73. #endif
  74. }
  75. if(!home)
  76. return -1;
  77. netrcfile = curl_maprintf("%s%s%s", home, DIR_CHAR, NETRC);
  78. if(!netrcfile) {
  79. if(home_alloc)
  80. free(home);
  81. return -1;
  82. }
  83. netrc_alloc = TRUE;
  84. }
  85. file = fopen(netrcfile, "r");
  86. if(file) {
  87. char *tok;
  88. char *tok_buf;
  89. bool done=FALSE;
  90. char netrcbuffer[256];
  91. int netrcbuffsize = (int)sizeof(netrcbuffer);
  92. while(!done && fgets(netrcbuffer, netrcbuffsize, file)) {
  93. tok=strtok_r(netrcbuffer, " \t\n", &tok_buf);
  94. while(!done && tok) {
  95. if(login[0] && password[0]) {
  96. done=TRUE;
  97. break;
  98. }
  99. switch(state) {
  100. case NOTHING:
  101. if(Curl_raw_equal("machine", tok)) {
  102. /* the next tok is the machine name, this is in itself the
  103. delimiter that starts the stuff entered for this machine,
  104. after this we need to search for 'login' and
  105. 'password'. */
  106. state=HOSTFOUND;
  107. }
  108. break;
  109. case HOSTFOUND:
  110. if(Curl_raw_equal(host, tok)) {
  111. /* and yes, this is our host! */
  112. state=HOSTVALID;
  113. retcode=0; /* we did find our host */
  114. }
  115. else
  116. /* not our host */
  117. state=NOTHING;
  118. break;
  119. case HOSTVALID:
  120. /* we are now parsing sub-keywords concerning "our" host */
  121. if(state_login) {
  122. if(specific_login) {
  123. state_our_login = Curl_raw_equal(login, tok);
  124. }
  125. else {
  126. strncpy(login, tok, LOGINSIZE-1);
  127. }
  128. state_login=0;
  129. }
  130. else if(state_password) {
  131. if(state_our_login || !specific_login) {
  132. strncpy(password, tok, PASSWORDSIZE-1);
  133. }
  134. state_password=0;
  135. }
  136. else if(Curl_raw_equal("login", tok))
  137. state_login=1;
  138. else if(Curl_raw_equal("password", tok))
  139. state_password=1;
  140. else if(Curl_raw_equal("machine", tok)) {
  141. /* ok, there's machine here go => */
  142. state = HOSTFOUND;
  143. state_our_login = FALSE;
  144. }
  145. break;
  146. } /* switch (state) */
  147. tok = strtok_r(NULL, " \t\n", &tok_buf);
  148. } /* while(tok) */
  149. } /* while fgets() */
  150. fclose(file);
  151. }
  152. if(home_alloc)
  153. free(home);
  154. if(netrc_alloc)
  155. free(netrcfile);
  156. return retcode;
  157. }