netrc.cc 6.1 KB

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