netrc.cc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: netrc.c,v 1.38 2007-11-07 09:21:35 bagder Exp $
  4. /* ######################################################################
  5. netrc file parser - returns the login and password of a give host in
  6. a specified netrc-type file
  7. Originally written by Daniel Stenberg, <daniel@haxx.se>, et al. and
  8. placed into the Public Domain, do with it what you will.
  9. ##################################################################### */
  10. /*}}}*/
  11. #include <apt-pkg/configuration.h>
  12. #include <apt-pkg/fileutl.h>
  13. #include <iostream>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <unistd.h>
  18. #include <pwd.h>
  19. #include "netrc.h"
  20. /* Get user and password from .netrc when given a machine name */
  21. enum {
  22. NOTHING,
  23. HOSTFOUND, /* the 'machine' keyword was found */
  24. HOSTCOMPLETE, /* the machine name following the keyword was found too */
  25. HOSTVALID, /* this is "our" machine! */
  26. HOSTEND /* LAST enum */
  27. };
  28. /* make sure we have room for at least this size: */
  29. #define LOGINSIZE 64
  30. #define PASSWORDSIZE 64
  31. #define NETRC DOT_CHAR "netrc"
  32. /* returns -1 on failure, 0 if the host is found, 1 is the host isn't found */
  33. int parsenetrc (char *host, char *login, char *password, char *netrcfile = NULL)
  34. {
  35. FILE *file;
  36. int retcode = 1;
  37. int specific_login = (login[0] != 0);
  38. char *home = NULL;
  39. bool netrc_alloc = false;
  40. int state = NOTHING;
  41. char state_login = 0; /* Found a login keyword */
  42. char state_password = 0; /* Found a password keyword */
  43. int state_our_login = false; /* With specific_login,
  44. found *our* login name */
  45. if (!netrcfile) {
  46. home = getenv ("HOME"); /* portable environment reader */
  47. if (!home) {
  48. struct passwd *pw;
  49. pw = getpwuid (geteuid ());
  50. if(pw)
  51. home = pw->pw_dir;
  52. }
  53. if (!home)
  54. return -1;
  55. asprintf (&netrcfile, "%s%s%s", home, DIR_CHAR, NETRC);
  56. if(!netrcfile)
  57. return -1;
  58. else
  59. netrc_alloc = true;
  60. }
  61. file = fopen (netrcfile, "r");
  62. if(file) {
  63. char *tok;
  64. char *tok_buf;
  65. bool done = false;
  66. char netrcbuffer[256];
  67. while (!done && fgets(netrcbuffer, sizeof (netrcbuffer), file)) {
  68. tok = strtok_r (netrcbuffer, " \t\n", &tok_buf);
  69. while (!done && tok) {
  70. if(login[0] && password[0]) {
  71. done = true;
  72. break;
  73. }
  74. switch(state) {
  75. case NOTHING:
  76. if (!strcasecmp ("machine", tok)) {
  77. /* the next tok is the machine name, this is in itself the
  78. delimiter that starts the stuff entered for this machine,
  79. after this we need to search for 'login' and
  80. 'password'. */
  81. state = HOSTFOUND;
  82. }
  83. break;
  84. case HOSTFOUND:
  85. /* extended definition of a "machine" if we have a "/"
  86. we match the start of the string (host.startswith(token) */
  87. if ((strchr(host, '/') && strstr(host, tok) == host) ||
  88. (!strcasecmp (host, tok))) {
  89. /* and yes, this is our host! */
  90. state = HOSTVALID;
  91. retcode = 0; /* we did find our host */
  92. }
  93. else
  94. /* not our host */
  95. state = NOTHING;
  96. break;
  97. case HOSTVALID:
  98. /* we are now parsing sub-keywords concerning "our" host */
  99. if (state_login) {
  100. if (specific_login)
  101. state_our_login = !strcasecmp (login, tok);
  102. else
  103. strncpy (login, tok, LOGINSIZE - 1);
  104. state_login = 0;
  105. } else if (state_password) {
  106. if (state_our_login || !specific_login)
  107. strncpy (password, tok, PASSWORDSIZE - 1);
  108. state_password = 0;
  109. } else if (!strcasecmp ("login", tok))
  110. state_login = 1;
  111. else if (!strcasecmp ("password", tok))
  112. state_password = 1;
  113. else if(!strcasecmp ("machine", tok)) {
  114. /* ok, there's machine here go => */
  115. state = HOSTFOUND;
  116. state_our_login = false;
  117. }
  118. break;
  119. } /* switch (state) */
  120. tok = strtok_r (NULL, " \t\n", &tok_buf);
  121. } /* while(tok) */
  122. } /* while fgets() */
  123. fclose(file);
  124. }
  125. if (netrc_alloc)
  126. free(netrcfile);
  127. return retcode;
  128. }
  129. void maybe_add_auth (URI &Uri, string NetRCFile)
  130. {
  131. if (_config->FindB("Debug::Acquire::netrc", false) == true)
  132. std::clog << "maybe_add_auth: " << (string)Uri
  133. << " " << NetRCFile << std::endl;
  134. if (Uri.Password.empty () == true || Uri.User.empty () == true)
  135. {
  136. if (NetRCFile.empty () == false)
  137. {
  138. char login[64] = "";
  139. char password[64] = "";
  140. char *netrcfile = strdupa (NetRCFile.c_str ());
  141. // first check for a generic host based netrc entry
  142. char *host = strdupa (Uri.Host.c_str ());
  143. if (host && parsenetrc (host, login, password, netrcfile) == 0)
  144. {
  145. if (_config->FindB("Debug::Acquire::netrc", false) == true)
  146. std::clog << "host: " << host
  147. << " user: " << login
  148. << " pass-size: " << strlen(password)
  149. << std::endl;
  150. Uri.User = string (login);
  151. Uri.Password = string (password);
  152. return;
  153. }
  154. // if host did not work, try Host+Path next, this will trigger
  155. // a lookup uri.startswith(host) in the netrc file parser (because
  156. // of the "/"
  157. char *hostpath = strdupa (string(Uri.Host+Uri.Path).c_str ());
  158. if (hostpath && parsenetrc (hostpath, login, password, netrcfile) == 0)
  159. {
  160. if (_config->FindB("Debug::Acquire::netrc", false) == true)
  161. std::clog << "hostpath: " << hostpath
  162. << " user: " << login
  163. << " pass-size: " << strlen(password)
  164. << std::endl;
  165. Uri.User = string (login);
  166. Uri.Password = string (password);
  167. return;
  168. }
  169. }
  170. }
  171. }
  172. #ifdef DEBUG
  173. int main(int argc, char* argv[])
  174. {
  175. char login[64] = "";
  176. char password[64] = "";
  177. if(argc < 2)
  178. return -1;
  179. if(0 == parsenetrc (argv[1], login, password, argv[2])) {
  180. printf("HOST: %s LOGIN: %s PASSWORD: %s\n", argv[1], login, password);
  181. }
  182. }
  183. #endif