netrc.cc 6.2 KB

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