netrc.c 6.0 KB

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