netrc.c 6.3 KB

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