1
0

netrc.c 6.2 KB

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