netrc.cc 6.2 KB

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