netrc.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2002, Daniel Stenberg, <[email protected]>, 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 "strequal.h"
  41. #include "strtok.h"
  42. /* The last #include file should be: */
  43. #ifdef MALLOCDEBUG
  44. #include "memdebug.h"
  45. #endif
  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. int Curl_parsenetrc(char *host,
  62. char *login,
  63. char *password)
  64. {
  65. FILE *file;
  66. char netrcbuffer[256];
  67. int retcode=1;
  68. int specific_login = (login[0] != 0);
  69. char *home = NULL;
  70. int state=NOTHING;
  71. char state_login=0; /* Found a login keyword */
  72. char state_password=0; /* Found a password keyword */
  73. char state_our_login=0; /* With specific_login, found *our* login name */
  74. #define NETRC DOT_CHAR "netrc"
  75. #if defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
  76. struct passwd *pw;
  77. pw= getpwuid(geteuid());
  78. if (pw) {
  79. #ifdef VMS
  80. home = decc$translate_vms(pw->pw_dir);
  81. #else
  82. home = pw->pw_dir;
  83. #endif
  84. }
  85. #else
  86. void *pw=NULL;
  87. #endif
  88. if(NULL == pw) {
  89. home = curl_getenv("HOME"); /* portable environment reader */
  90. if(!home) {
  91. return -1;
  92. }
  93. }
  94. if(strlen(home)>(sizeof(netrcbuffer)-strlen(NETRC))) {
  95. if(NULL==pw)
  96. free(home);
  97. return -1;
  98. }
  99. sprintf(netrcbuffer, "%s%s%s", home, DIR_CHAR, NETRC);
  100. #ifdef MALLOCDEBUG
  101. {
  102. /* This is a hack to allow testing.
  103. * If compiled with --enable-debug and CURL_DEBUG_NETRC is defined,
  104. * then it's the path to a substitute .netrc for testing purposes *only* */
  105. char *override = curl_getenv("CURL_DEBUG_NETRC");
  106. if (override != NULL) {
  107. printf("NETRC: overridden .netrc file: %s\n", home);
  108. if (strlen(override)+1 > sizeof(netrcbuffer)) {
  109. free(override);
  110. if(NULL==pw)
  111. free(home);
  112. return -1;
  113. }
  114. strcpy(netrcbuffer, override);
  115. free(override);
  116. }
  117. }
  118. #endif /* MALLOCDEBUG */
  119. file = fopen(netrcbuffer, "r");
  120. if(file) {
  121. char *tok;
  122. char *tok_buf;
  123. while(fgets(netrcbuffer, sizeof(netrcbuffer), file)) {
  124. tok=strtok_r(netrcbuffer, " \t\n", &tok_buf);
  125. while(tok) {
  126. if (login[0] && password[0])
  127. goto done;
  128. switch(state) {
  129. case NOTHING:
  130. if(strequal("machine", tok)) {
  131. /* the next tok is the machine name, this is in itself the
  132. delimiter that starts the stuff entered for this machine,
  133. after this we need to search for 'login' and
  134. 'password'. */
  135. state=HOSTFOUND;
  136. }
  137. break;
  138. case HOSTFOUND:
  139. if(strequal(host, tok)) {
  140. /* and yes, this is our host! */
  141. state=HOSTVALID;
  142. #ifdef _NETRC_DEBUG
  143. printf("HOST: %s\n", tok);
  144. #endif
  145. retcode=0; /* we did find our host */
  146. }
  147. else
  148. /* not our host */
  149. state=NOTHING;
  150. break;
  151. case HOSTVALID:
  152. /* we are now parsing sub-keywords concerning "our" host */
  153. if(state_login) {
  154. if (specific_login) {
  155. state_our_login = (char)strequal(login, tok);
  156. }else{
  157. strncpy(login, tok, LOGINSIZE-1);
  158. #ifdef _NETRC_DEBUG
  159. printf("LOGIN: %s\n", login);
  160. #endif
  161. }
  162. state_login=0;
  163. }
  164. else if(state_password) {
  165. if (state_our_login || !specific_login) {
  166. strncpy(password, tok, PASSWORDSIZE-1);
  167. #ifdef _NETRC_DEBUG
  168. printf("PASSWORD: %s\n", password);
  169. #endif
  170. }
  171. state_password=0;
  172. }
  173. else if(strequal("login", tok))
  174. state_login=1;
  175. else if(strequal("password", tok))
  176. state_password=1;
  177. else if(strequal("machine", tok)) {
  178. /* ok, there's machine here go => */
  179. state = HOSTFOUND;
  180. state_our_login = 0;
  181. }
  182. break;
  183. } /* switch (state) */
  184. tok = strtok_r(NULL, " \t\n", &tok_buf);
  185. } /* while (tok) */
  186. } /* while fgets() */
  187. done:
  188. fclose(file);
  189. }
  190. if(NULL==pw)
  191. free(home);
  192. return retcode;
  193. }
  194. #ifdef _NETRC_DEBUG
  195. int main(int argc, char **argv)
  196. {
  197. char login[64]="";
  198. char password[64]="";
  199. if(argc<2)
  200. return -1;
  201. if(0 == ParseNetrc(argv[1], login, password)) {
  202. printf("HOST: %s LOGIN: %s PASSWORD: %s\n",
  203. argv[1], login, password);
  204. }
  205. }
  206. #endif
  207. /*
  208. * local variables:
  209. * eval: (load-file "../curl-mode.el")
  210. * end:
  211. * vim600: fdm=marker
  212. * vim: et sw=2 ts=2 sts=2 tw=78
  213. */