netrc.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2010, 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. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #ifdef HAVE_UNISTD_H
  27. #include <unistd.h>
  28. #endif
  29. #ifdef HAVE_PWD_H
  30. #include <pwd.h>
  31. #endif
  32. #ifdef __VMS
  33. #include <unixlib.h>
  34. #endif
  35. #include <curl/curl.h>
  36. #include "netrc.h"
  37. #include "strequal.h"
  38. #include "strtok.h"
  39. #include "curl_memory.h"
  40. #include "rawstr.h"
  41. #define _MPRINTF_REPLACE /* use our functions only */
  42. #include <curl/mprintf.h>
  43. /* The last #include file should be: */
  44. #include "memdebug.h"
  45. /* Debug this single source file with:
  46. 'make netrc' then run './netrc'!
  47. Oh, make sure you have a .netrc file too ;-)
  48. */
  49. /* Get user and password from .netrc when given a machine name */
  50. enum {
  51. NOTHING,
  52. HOSTFOUND, /* the 'machine' keyword was found */
  53. HOSTCOMPLETE, /* the machine name following the keyword was found too */
  54. HOSTVALID, /* this is "our" machine! */
  55. HOSTEND /* LAST enum */
  56. };
  57. /* make sure we have room for at least this size: */
  58. #define LOGINSIZE 64
  59. #define PASSWORDSIZE 64
  60. /* returns -1 on failure, 0 if the host is found, 1 is the host isn't found */
  61. int Curl_parsenetrc(const char *host,
  62. char *login,
  63. char *password,
  64. char *netrcfile)
  65. {
  66. FILE *file;
  67. int retcode=1;
  68. int specific_login = (login[0] != 0);
  69. char *home = NULL;
  70. bool home_alloc = FALSE;
  71. bool netrc_alloc = FALSE;
  72. int state=NOTHING;
  73. char state_login=0; /* Found a login keyword */
  74. char state_password=0; /* Found a password keyword */
  75. int state_our_login=FALSE; /* With specific_login, found *our* login name */
  76. #define NETRC DOT_CHAR "netrc"
  77. #ifdef DEBUGBUILD
  78. {
  79. /* This is a hack to allow testing.
  80. * If compiled with --enable-debug and CURL_DEBUG_NETRC is defined,
  81. * then it's the path to a substitute .netrc for testing purposes *only* */
  82. char *override = curl_getenv("CURL_DEBUG_NETRC");
  83. if(override) {
  84. fprintf(stderr, "NETRC: overridden " NETRC " file: %s\n", override);
  85. netrcfile = override;
  86. netrc_alloc = TRUE;
  87. }
  88. }
  89. #endif /* DEBUGBUILD */
  90. if(!netrcfile) {
  91. home = curl_getenv("HOME"); /* portable environment reader */
  92. if(home) {
  93. home_alloc = TRUE;
  94. #if defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
  95. }
  96. else {
  97. struct passwd *pw;
  98. pw= getpwuid(geteuid());
  99. if(pw) {
  100. #ifdef __VMS
  101. home = decc_translate_vms(pw->pw_dir);
  102. #else
  103. home = pw->pw_dir;
  104. #endif
  105. }
  106. #endif
  107. }
  108. if(!home)
  109. return -1;
  110. netrcfile = curl_maprintf("%s%s%s", home, DIR_CHAR, NETRC);
  111. if(!netrcfile) {
  112. if(home_alloc)
  113. free(home);
  114. return -1;
  115. }
  116. netrc_alloc = TRUE;
  117. }
  118. file = fopen(netrcfile, "r");
  119. if(file) {
  120. char *tok;
  121. char *tok_buf;
  122. bool done=FALSE;
  123. char netrcbuffer[256];
  124. int netrcbuffsize = (int)sizeof(netrcbuffer);
  125. while(!done && fgets(netrcbuffer, netrcbuffsize, file)) {
  126. tok=strtok_r(netrcbuffer, " \t\n", &tok_buf);
  127. while(!done && tok) {
  128. if(login[0] && password[0]) {
  129. done=TRUE;
  130. break;
  131. }
  132. switch(state) {
  133. case NOTHING:
  134. if(Curl_raw_equal("machine", tok)) {
  135. /* the next tok is the machine name, this is in itself the
  136. delimiter that starts the stuff entered for this machine,
  137. after this we need to search for 'login' and
  138. 'password'. */
  139. state=HOSTFOUND;
  140. }
  141. break;
  142. case HOSTFOUND:
  143. if(Curl_raw_equal(host, tok)) {
  144. /* and yes, this is our host! */
  145. state=HOSTVALID;
  146. #ifdef _NETRC_DEBUG
  147. fprintf(stderr, "HOST: %s\n", tok);
  148. #endif
  149. retcode=0; /* we did find our host */
  150. }
  151. else
  152. /* not our host */
  153. state=NOTHING;
  154. break;
  155. case HOSTVALID:
  156. /* we are now parsing sub-keywords concerning "our" host */
  157. if(state_login) {
  158. if(specific_login) {
  159. state_our_login = Curl_raw_equal(login, tok);
  160. }
  161. else {
  162. strncpy(login, tok, LOGINSIZE-1);
  163. #ifdef _NETRC_DEBUG
  164. fprintf(stderr, "LOGIN: %s\n", login);
  165. #endif
  166. }
  167. state_login=0;
  168. }
  169. else if(state_password) {
  170. if(state_our_login || !specific_login) {
  171. strncpy(password, tok, PASSWORDSIZE-1);
  172. #ifdef _NETRC_DEBUG
  173. fprintf(stderr, "PASSWORD: %s\n", password);
  174. #endif
  175. }
  176. state_password=0;
  177. }
  178. else if(Curl_raw_equal("login", tok))
  179. state_login=1;
  180. else if(Curl_raw_equal("password", tok))
  181. state_password=1;
  182. else if(Curl_raw_equal("machine", tok)) {
  183. /* ok, there's machine here go => */
  184. state = HOSTFOUND;
  185. state_our_login = FALSE;
  186. }
  187. break;
  188. } /* switch (state) */
  189. tok = strtok_r(NULL, " \t\n", &tok_buf);
  190. } /* while(tok) */
  191. } /* while fgets() */
  192. fclose(file);
  193. }
  194. if(home_alloc)
  195. free(home);
  196. if(netrc_alloc)
  197. free(netrcfile);
  198. return retcode;
  199. }
  200. #ifdef _NETRC_DEBUG
  201. int main(int argc, argv_item_t argv[])
  202. {
  203. char login[64]="";
  204. char password[64]="";
  205. if(argc<2)
  206. return -1;
  207. if(0 == ParseNetrc(argv[1], login, password)) {
  208. printf("HOST: %s LOGIN: %s PASSWORD: %s\n",
  209. argv[1], login, password);
  210. }
  211. }
  212. #endif