netrc.cc 6.7 KB

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