You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

232 lines
6.7 KiB

  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. bool netrc_alloc = false;
  43. if (!netrcfile) {
  44. char const * home = getenv ("HOME"); /* portable environment reader */
  45. if (!home) {
  46. struct passwd *pw;
  47. pw = getpwuid (geteuid ());
  48. if(pw)
  49. home = pw->pw_dir;
  50. }
  51. if (!home)
  52. return -1;
  53. if (asprintf (&netrcfile, "%s%s%s", home, DIR_CHAR, NETRC) == -1 || netrcfile == NULL)
  54. return -1;
  55. else
  56. netrc_alloc = true;
  57. }
  58. file = fopen (netrcfile, "r");
  59. if(file) {
  60. char *tok;
  61. char *tok_buf;
  62. bool done = false;
  63. char *netrcbuffer = NULL;
  64. size_t netrcbuffer_size = 0;
  65. int state = NOTHING;
  66. char state_login = 0; /* Found a login keyword */
  67. char state_password = 0; /* Found a password keyword */
  68. int state_our_login = false; /* With specific_login,
  69. found *our* login name */
  70. while (!done && getline(&netrcbuffer, &netrcbuffer_size, file) != -1) {
  71. tok = strtok_r (netrcbuffer, " \t\n", &tok_buf);
  72. while (!done && tok) {
  73. if(login.empty() == false && password.empty() == false) {
  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.c_str(), tok);
  105. else
  106. login = tok;
  107. state_login = 0;
  108. } else if (state_password) {
  109. if (state_our_login || !specific_login)
  110. password = tok;
  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 getline() */
  126. free(netrcbuffer);
  127. fclose(file);
  128. }
  129. if (netrc_alloc)
  130. free(netrcfile);
  131. return retcode;
  132. }
  133. // for some unknown reason this method is exported so keep a compatible interface for now …
  134. int parsenetrc (char *host, char *login, char *password, char *netrcfile = NULL)
  135. {
  136. std::string login_string, password_string;
  137. int const ret = parsenetrc_string(host, login_string, password_string, netrcfile);
  138. if (ret < 0)
  139. return ret;
  140. strncpy(login, login_string.c_str(), LOGINSIZE - 1);
  141. strncpy(password, password_string.c_str(), PASSWORDSIZE - 1);
  142. return ret;
  143. }
  144. void maybe_add_auth (URI &Uri, string NetRCFile)
  145. {
  146. if (_config->FindB("Debug::Acquire::netrc", false) == true)
  147. std::clog << "maybe_add_auth: " << (string)Uri
  148. << " " << NetRCFile << std::endl;
  149. if (Uri.Password.empty () == true || Uri.User.empty () == true)
  150. {
  151. if (NetRCFile.empty () == false)
  152. {
  153. std::string login, password;
  154. char *netrcfile = strdup(NetRCFile.c_str());
  155. // first check for a generic host based netrc entry
  156. char *host = strdup(Uri.Host.c_str());
  157. if (host && parsenetrc_string(host, login, password, netrcfile) == 0)
  158. {
  159. if (_config->FindB("Debug::Acquire::netrc", false) == true)
  160. std::clog << "host: " << host
  161. << " user: " << login
  162. << " pass-size: " << password.size()
  163. << std::endl;
  164. Uri.User = login;
  165. Uri.Password = password;
  166. free(netrcfile);
  167. free(host);
  168. return;
  169. }
  170. free(host);
  171. // if host did not work, try Host+Path next, this will trigger
  172. // a lookup uri.startswith(host) in the netrc file parser (because
  173. // of the "/"
  174. char *hostpath = strdup((Uri.Host + Uri.Path).c_str());
  175. if (hostpath && parsenetrc_string(hostpath, login, password, netrcfile) == 0)
  176. {
  177. if (_config->FindB("Debug::Acquire::netrc", false) == true)
  178. std::clog << "hostpath: " << hostpath
  179. << " user: " << login
  180. << " pass-size: " << password.size()
  181. << std::endl;
  182. Uri.User = login;
  183. Uri.Password = password;
  184. }
  185. free(netrcfile);
  186. free(hostpath);
  187. }
  188. }
  189. }
  190. #ifdef DEBUG
  191. int main(int argc, char* argv[])
  192. {
  193. char login[64] = "";
  194. char password[64] = "";
  195. if(argc < 2)
  196. return -1;
  197. if(0 == parsenetrc (argv[1], login, password, argv[2])) {
  198. printf("HOST: %s LOGIN: %s PASSWORD: %s\n", argv[1], login, password);
  199. }
  200. }
  201. #endif