netrc.c 6.3 KB

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