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/strutl.h>
  14. #include <iostream>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <unistd.h>
  19. #include <stddef.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 256
  33. #define PASSWORDSIZE 256
  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. static int parsenetrc_string (char *host, std::string &login, std::string &password, char *netrcfile = NULL)
  37. {
  38. FILE *file;
  39. int retcode = 1;
  40. int specific_login = (login.empty() == false);
  41. bool netrc_alloc = false;
  42. if (!netrcfile) {
  43. char const * home = getenv ("HOME"); /* portable environment reader */
  44. if (!home) {
  45. struct passwd *pw;
  46. pw = getpwuid (geteuid ());
  47. if(pw)
  48. home = pw->pw_dir;
  49. }
  50. if (!home)
  51. return -1;
  52. if (asprintf (&netrcfile, "%s%s%s", home, DIR_CHAR, NETRC) == -1 || netrcfile == NULL)
  53. return -1;
  54. else
  55. netrc_alloc = true;
  56. }
  57. file = fopen (netrcfile, "r");
  58. if(file) {
  59. char *tok;
  60. char *tok_buf;
  61. bool done = false;
  62. char *netrcbuffer = NULL;
  63. size_t netrcbuffer_size = 0;
  64. int state = NOTHING;
  65. char state_login = 0; /* Found a login keyword */
  66. char state_password = 0; /* Found a password keyword */
  67. int state_our_login = false; /* With specific_login,
  68. found *our* login name */
  69. while (!done && getline(&netrcbuffer, &netrcbuffer_size, file) != -1) {
  70. tok = strtok_r (netrcbuffer, " \t\n", &tok_buf);
  71. while (!done && tok) {
  72. if(login.empty() == false && password.empty() == false) {
  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 regarding "our" host */
  101. if (state_login) {
  102. if (specific_login)
  103. state_our_login = !strcasecmp (login.c_str(), tok);
  104. else
  105. login = tok;
  106. state_login = 0;
  107. } else if (state_password) {
  108. if (state_our_login || !specific_login)
  109. password = tok;
  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 getline() */
  125. free(netrcbuffer);
  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. std::string login, password;
  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_string(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: " << password.size()
  151. << std::endl;
  152. Uri.User = login;
  153. Uri.Password = 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((Uri.Host + Uri.Path).c_str());
  163. if (hostpath && parsenetrc_string(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: " << password.size()
  169. << std::endl;
  170. Uri.User = login;
  171. Uri.Password = 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