]> git.saurik.com Git - apt.git/blame_incremental - apt-pkg/contrib/netrc.cc
merge patch from Daniel Hartwig to Show a error message if {,dist-}upgrade is used...
[apt.git] / apt-pkg / contrib / netrc.cc
... / ...
CommitLineData
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
3// $Id: netrc.c,v 1.38 2007-11-07 09:21:35 bagder Exp $
4/* ######################################################################
5
6 netrc file parser - returns the login and password of a give host in
7 a specified netrc-type file
8
9 Originally written by Daniel Stenberg, <daniel@haxx.se>, et al. and
10 placed into the Public Domain, do with it what you will.
11
12 ##################################################################### */
13 /*}}}*/
14#include <config.h>
15
16#include <apt-pkg/configuration.h>
17#include <apt-pkg/strutl.h>
18#include <apt-pkg/error.h>
19#include <apt-pkg/fileutl.h>
20
21#include <iostream>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <unistd.h>
26#include <pwd.h>
27
28#include "netrc.h"
29
30using std::string;
31
32/* Get user and password from .netrc when given a machine name */
33
34enum {
35 NOTHING,
36 HOSTFOUND, /* the 'machine' keyword was found */
37 HOSTCOMPLETE, /* the machine name following the keyword was found too */
38 HOSTVALID, /* this is "our" machine! */
39 HOSTEND /* LAST enum */
40};
41
42/* make sure we have room for at least this size: */
43#define LOGINSIZE 256
44#define PASSWORDSIZE 256
45#define NETRC DOT_CHAR "netrc"
46
47/* returns -1 on failure, 0 if the host is found, 1 is the host isn't found */
48static int parsenetrc_string (char *host, std::string &login, std::string &password, char *netrcfile = NULL)
49{
50 FILE *file;
51 int retcode = 1;
52 int specific_login = (login.empty() == false);
53 bool netrc_alloc = false;
54
55 if (!netrcfile) {
56 char const * home = getenv ("HOME"); /* portable environment reader */
57
58 if (!home) {
59 struct passwd *pw;
60 pw = getpwuid (geteuid ());
61 if(pw)
62 home = pw->pw_dir;
63 }
64
65 if (!home)
66 return -1;
67
68 if (asprintf (&netrcfile, "%s%s%s", home, DIR_CHAR, NETRC) == -1 || netrcfile == NULL)
69 return -1;
70 else
71 netrc_alloc = true;
72 }
73
74 file = fopen (netrcfile, "r");
75 if(file) {
76 char *tok;
77 char *tok_buf;
78 bool done = false;
79 char *netrcbuffer = NULL;
80 size_t netrcbuffer_size = 0;
81
82 int state = NOTHING;
83 char state_login = 0; /* Found a login keyword */
84 char state_password = 0; /* Found a password keyword */
85 int state_our_login = false; /* With specific_login,
86 found *our* login name */
87
88 while (!done && getline(&netrcbuffer, &netrcbuffer_size, file) != -1) {
89 tok = strtok_r (netrcbuffer, " \t\n", &tok_buf);
90 while (!done && tok) {
91 if(login.empty() == false && password.empty() == false) {
92 done = true;
93 break;
94 }
95
96 switch(state) {
97 case NOTHING:
98 if (!strcasecmp ("machine", tok)) {
99 /* the next tok is the machine name, this is in itself the
100 delimiter that starts the stuff entered for this machine,
101 after this we need to search for 'login' and
102 'password'. */
103 state = HOSTFOUND;
104 }
105 break;
106 case HOSTFOUND:
107 /* extended definition of a "machine" if we have a "/"
108 we match the start of the string (host.startswith(token) */
109 if ((strchr(host, '/') && strstr(host, tok) == host) ||
110 (!strcasecmp (host, tok))) {
111 /* and yes, this is our host! */
112 state = HOSTVALID;
113 retcode = 0; /* we did find our host */
114 }
115 else
116 /* not our host */
117 state = NOTHING;
118 break;
119 case HOSTVALID:
120 /* we are now parsing sub-keywords concerning "our" host */
121 if (state_login) {
122 if (specific_login)
123 state_our_login = !strcasecmp (login.c_str(), tok);
124 else
125 login = tok;
126 state_login = 0;
127 } else if (state_password) {
128 if (state_our_login || !specific_login)
129 password = tok;
130 state_password = 0;
131 } else if (!strcasecmp ("login", tok))
132 state_login = 1;
133 else if (!strcasecmp ("password", tok))
134 state_password = 1;
135 else if(!strcasecmp ("machine", tok)) {
136 /* ok, there's machine here go => */
137 state = HOSTFOUND;
138 state_our_login = false;
139 }
140 break;
141 } /* switch (state) */
142
143 tok = strtok_r (NULL, " \t\n", &tok_buf);
144 } /* while(tok) */
145 } /* while getline() */
146
147 free(netrcbuffer);
148 fclose(file);
149 }
150
151 if (netrc_alloc)
152 free(netrcfile);
153
154 return retcode;
155}
156
157void maybe_add_auth (URI &Uri, string NetRCFile)
158{
159 if (_config->FindB("Debug::Acquire::netrc", false) == true)
160 std::clog << "maybe_add_auth: " << (string)Uri
161 << " " << NetRCFile << std::endl;
162 if (Uri.Password.empty () == true || Uri.User.empty () == true)
163 {
164 if (NetRCFile.empty () == false)
165 {
166 std::string login, password;
167 char *netrcfile = strdup(NetRCFile.c_str());
168
169 // first check for a generic host based netrc entry
170 char *host = strdup(Uri.Host.c_str());
171 if (host && parsenetrc_string(host, login, password, netrcfile) == 0)
172 {
173 if (_config->FindB("Debug::Acquire::netrc", false) == true)
174 std::clog << "host: " << host
175 << " user: " << login
176 << " pass-size: " << password.size()
177 << std::endl;
178 Uri.User = login;
179 Uri.Password = password;
180 free(netrcfile);
181 free(host);
182 return;
183 }
184 free(host);
185
186 // if host did not work, try Host+Path next, this will trigger
187 // a lookup uri.startswith(host) in the netrc file parser (because
188 // of the "/"
189 char *hostpath = strdup(string(Uri.Host+Uri.Path).c_str());
190 if (hostpath && parsenetrc_string(hostpath, login, password, netrcfile) == 0)
191 {
192 if (_config->FindB("Debug::Acquire::netrc", false) == true)
193 std::clog << "hostpath: " << hostpath
194 << " user: " << login
195 << " pass-size: " << password.size()
196 << std::endl;
197 Uri.User = login;
198 Uri.Password = password;
199 }
200 free(netrcfile);
201 free(hostpath);
202 }
203 }
204}
205
206#ifdef DEBUG
207int main(int argc, char* argv[])
208{
209 char login[64] = "";
210 char password[64] = "";
211
212 if(argc < 2)
213 return -1;
214
215 if(0 == parsenetrc (argv[1], login, password, argv[2])) {
216 printf("HOST: %s LOGIN: %s PASSWORD: %s\n", argv[1], login, password);
217 }
218}
219#endif