netrc.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*****************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2000, Daniel Stenberg, <[email protected]>, et al.
  9. *
  10. * In order to be useful for every potential user, curl and libcurl are
  11. * dual-licensed under the MPL and the MIT/X-derivate licenses.
  12. *
  13. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  14. * copies of the Software, and permit persons to whom the Software is
  15. * furnished to do so, under the terms of the MPL or the MIT/X-derivate
  16. * licenses. You may pick one of these licenses.
  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. char *home = NULL;
  69. int state=NOTHING;
  70. char state_login=0;
  71. char state_password=0;
  72. #define NETRC DOT_CHAR "netrc"
  73. #if defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
  74. struct passwd *pw;
  75. pw= getpwuid(geteuid());
  76. if (pw) {
  77. #ifdef VMS
  78. home = decc$translate_vms(pw->pw_dir);
  79. #else
  80. home = pw->pw_dir;
  81. #endif
  82. }
  83. #else
  84. void *pw=NULL;
  85. #endif
  86. if(NULL == pw) {
  87. home = curl_getenv("HOME"); /* portable environment reader */
  88. if(!home) {
  89. return -1;
  90. }
  91. }
  92. if(strlen(home)>(sizeof(netrcbuffer)-strlen(NETRC))) {
  93. if(NULL==pw)
  94. free(home);
  95. return -1;
  96. }
  97. sprintf(netrcbuffer, "%s%s%s", home, DIR_CHAR, NETRC);
  98. file = fopen(netrcbuffer, "r");
  99. if(file) {
  100. char *tok;
  101. char *tok_buf;
  102. while(fgets(netrcbuffer, sizeof(netrcbuffer), file)) {
  103. tok=strtok_r(netrcbuffer, " \t\n", &tok_buf);
  104. while(tok) {
  105. switch(state) {
  106. case NOTHING:
  107. if(strequal("machine", tok)) {
  108. /* the next tok is the machine name, this is in itself the
  109. delimiter that starts the stuff entered for this machine,
  110. after this we need to search for 'login' and
  111. 'password'. */
  112. state=HOSTFOUND;
  113. }
  114. break;
  115. case HOSTFOUND:
  116. if(strequal(host, tok)) {
  117. /* and yes, this is our host! */
  118. state=HOSTVALID;
  119. #ifdef _NETRC_DEBUG
  120. printf("HOST: %s\n", tok);
  121. #endif
  122. retcode=0; /* we did find our host */
  123. }
  124. else
  125. /* not our host */
  126. state=NOTHING;
  127. break;
  128. case HOSTVALID:
  129. /* we are now parsing sub-keywords concerning "our" host */
  130. if(state_login) {
  131. strncpy(login, tok, LOGINSIZE-1);
  132. #ifdef _NETRC_DEBUG
  133. printf("LOGIN: %s\n", login);
  134. #endif
  135. state_login=0;
  136. }
  137. else if(state_password) {
  138. strncpy(password, tok, PASSWORDSIZE-1);
  139. #ifdef _NETRC_DEBUG
  140. printf("PASSWORD: %s\n", password);
  141. #endif
  142. state_password=0;
  143. }
  144. else if(strequal("login", tok))
  145. state_login=1;
  146. else if(strequal("password", tok))
  147. state_password=1;
  148. else if(strequal("machine", tok)) {
  149. /* ok, there's machine here go => */
  150. state = HOSTFOUND;
  151. }
  152. break;
  153. } /* switch (state) */
  154. tok = strtok_r(NULL, " \t\n", &tok_buf);
  155. } /* while (tok) */
  156. } /* while fgets() */
  157. fclose(file);
  158. }
  159. if(NULL==pw)
  160. free(home);
  161. return retcode;
  162. }
  163. #ifdef _NETRC_DEBUG
  164. int main(int argc, char **argv)
  165. {
  166. char login[64]="";
  167. char password[64]="";
  168. if(argc<2)
  169. return -1;
  170. if(0 == ParseNetrc(argv[1], login, password)) {
  171. printf("HOST: %s LOGIN: %s PASSWORD: %s\n",
  172. argv[1], login, password);
  173. }
  174. }
  175. #endif
  176. /*
  177. * local variables:
  178. * eval: (load-file "../curl-mode.el")
  179. * end:
  180. * vim600: fdm=marker
  181. * vim: et sw=2 ts=2 sts=2 tw=78
  182. */