]>
git.saurik.com Git - apt.git/blob - apt-pkg/contrib/netrc.cc
9aa1376dc7ae83600d0c471226c42336fe53bb96
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: netrc.c,v 1.38 2007-11-07 09:21:35 bagder Exp $
4 /* ######################################################################
6 netrc file parser - returns the login and password of a give host in
7 a specified netrc-type file
9 Originally written by Daniel Stenberg, <daniel@haxx.se>, et al. and
10 placed into the Public Domain, do with it what you will.
12 ##################################################################### */
16 #include <apt-pkg/configuration.h>
17 #include <apt-pkg/fileutl.h>
29 /* Get user and password from .netrc when given a machine name */
33 HOSTFOUND
, /* the 'machine' keyword was found */
34 HOSTCOMPLETE
, /* the machine name following the keyword was found too */
35 HOSTVALID
, /* this is "our" machine! */
36 HOSTEND
/* LAST enum */
39 /* make sure we have room for at least this size: */
41 #define PASSWORDSIZE 64
42 #define NETRC DOT_CHAR "netrc"
44 /* returns -1 on failure, 0 if the host is found, 1 is the host isn't found */
45 int parsenetrc (char *host
, char *login
, char *password
, char *netrcfile
= NULL
)
49 int specific_login
= (login
[0] != 0);
51 bool netrc_alloc
= false;
53 int state_our_login
= false; /* With specific_login,
54 found *our* login name */
57 home
= getenv ("HOME"); /* portable environment reader */
61 pw
= getpwuid (geteuid ());
69 asprintf (&netrcfile
, "%s%s%s", home
, DIR_CHAR
, NETRC
);
76 file
= fopen (netrcfile
, "r");
81 char netrcbuffer
[256];
84 char state_login
= 0; /* Found a login keyword */
85 char state_password
= 0; /* Found a password keyword */
87 while (!done
&& fgets(netrcbuffer
, sizeof (netrcbuffer
), file
)) {
88 tok
= strtok_r (netrcbuffer
, " \t\n", &tok_buf
);
89 while (!done
&& tok
) {
90 if(login
[0] && password
[0]) {
97 if (!strcasecmp ("machine", tok
)) {
98 /* the next tok is the machine name, this is in itself the
99 delimiter that starts the stuff entered for this machine,
100 after this we need to search for 'login' and
106 /* extended definition of a "machine" if we have a "/"
107 we match the start of the string (host.startswith(token) */
108 if ((strchr(host
, '/') && strstr(host
, tok
) == host
) ||
109 (!strcasecmp (host
, tok
))) {
110 /* and yes, this is our host! */
112 retcode
= 0; /* we did find our host */
119 /* we are now parsing sub-keywords concerning "our" host */
122 state_our_login
= !strcasecmp (login
, tok
);
124 strncpy (login
, tok
, LOGINSIZE
- 1);
126 } else if (state_password
) {
127 if (state_our_login
|| !specific_login
)
128 strncpy (password
, tok
, PASSWORDSIZE
- 1);
130 } else if (!strcasecmp ("login", tok
))
132 else if (!strcasecmp ("password", tok
))
134 else if(!strcasecmp ("machine", tok
)) {
135 /* ok, there's machine here go => */
137 state_our_login
= false;
140 } /* switch (state) */
142 tok
= strtok_r (NULL
, " \t\n", &tok_buf
);
144 } /* while fgets() */
155 void maybe_add_auth (URI
&Uri
, string NetRCFile
)
157 if (_config
->FindB("Debug::Acquire::netrc", false) == true)
158 std::clog
<< "maybe_add_auth: " << (string
)Uri
159 << " " << NetRCFile
<< std::endl
;
160 if (Uri
.Password
.empty () == true || Uri
.User
.empty () == true)
162 if (NetRCFile
.empty () == false)
165 char password
[64] = "";
166 char *netrcfile
= strdup(NetRCFile
.c_str());
168 // first check for a generic host based netrc entry
169 char *host
= strdup(Uri
.Host
.c_str());
170 if (host
&& parsenetrc (host
, login
, password
, netrcfile
) == 0)
172 if (_config
->FindB("Debug::Acquire::netrc", false) == true)
173 std::clog
<< "host: " << host
174 << " user: " << login
175 << " pass-size: " << strlen(password
)
177 Uri
.User
= string (login
);
178 Uri
.Password
= string (password
);
185 // if host did not work, try Host+Path next, this will trigger
186 // a lookup uri.startswith(host) in the netrc file parser (because
188 char *hostpath
= strdup(string(Uri
.Host
+Uri
.Path
).c_str());
189 if (hostpath
&& parsenetrc (hostpath
, login
, password
, netrcfile
) == 0)
191 if (_config
->FindB("Debug::Acquire::netrc", false) == true)
192 std::clog
<< "hostpath: " << hostpath
193 << " user: " << login
194 << " pass-size: " << strlen(password
)
196 Uri
.User
= string (login
);
197 Uri
.Password
= string (password
);
206 int main(int argc
, char* argv
[])
209 char password
[64] = "";
214 if(0 == parsenetrc (argv
[1], login
, password
, argv
[2])) {
215 printf("HOST: %s LOGIN: %s PASSWORD: %s\n", argv
[1], login
, password
);