]>
git.saurik.com Git - apt.git/blob - apt-pkg/contrib/netrc.cc
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/strutl.h>
18 #include <apt-pkg/fileutl.h>
31 /* Get user and password from .netrc when given a machine name */
35 HOSTFOUND
, /* the 'machine' keyword was found */
36 HOSTCOMPLETE
, /* the machine name following the keyword was found too */
37 HOSTVALID
, /* this is "our" machine! */
38 HOSTEND
/* LAST enum */
41 /* make sure we have room for at least this size: */
43 #define PASSWORDSIZE 64
44 #define NETRC DOT_CHAR "netrc"
46 /* returns -1 on failure, 0 if the host is found, 1 is the host isn't found */
47 int parsenetrc (char *host
, char *login
, char *password
, char *netrcfile
= NULL
)
51 int specific_login
= (login
[0] != 0);
53 bool netrc_alloc
= false;
55 int state_our_login
= false; /* With specific_login,
56 found *our* login name */
59 home
= getenv ("HOME"); /* portable environment reader */
63 pw
= getpwuid (geteuid ());
71 asprintf (&netrcfile
, "%s%s%s", home
, DIR_CHAR
, NETRC
);
78 file
= fopen (netrcfile
, "r");
83 char netrcbuffer
[256];
86 char state_login
= 0; /* Found a login keyword */
87 char state_password
= 0; /* Found a password keyword */
89 while (!done
&& fgets(netrcbuffer
, sizeof (netrcbuffer
), file
)) {
90 tok
= strtok_r (netrcbuffer
, " \t\n", &tok_buf
);
91 while (!done
&& tok
) {
92 if(login
[0] && password
[0]) {
99 if (!strcasecmp ("machine", tok
)) {
100 /* the next tok is the machine name, this is in itself the
101 delimiter that starts the stuff entered for this machine,
102 after this we need to search for 'login' and
108 /* extended definition of a "machine" if we have a "/"
109 we match the start of the string (host.startswith(token) */
110 if ((strchr(host
, '/') && strstr(host
, tok
) == host
) ||
111 (!strcasecmp (host
, tok
))) {
112 /* and yes, this is our host! */
114 retcode
= 0; /* we did find our host */
121 /* we are now parsing sub-keywords concerning "our" host */
124 state_our_login
= !strcasecmp (login
, tok
);
126 strncpy (login
, tok
, LOGINSIZE
- 1);
128 } else if (state_password
) {
129 if (state_our_login
|| !specific_login
)
130 strncpy (password
, tok
, PASSWORDSIZE
- 1);
132 } else if (!strcasecmp ("login", tok
))
134 else if (!strcasecmp ("password", tok
))
136 else if(!strcasecmp ("machine", tok
)) {
137 /* ok, there's machine here go => */
139 state_our_login
= false;
142 } /* switch (state) */
144 tok
= strtok_r (NULL
, " \t\n", &tok_buf
);
146 } /* while fgets() */
157 void maybe_add_auth (URI
&Uri
, string NetRCFile
)
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)
164 if (NetRCFile
.empty () == false)
167 char password
[64] = "";
168 char *netrcfile
= strdup(NetRCFile
.c_str());
170 // first check for a generic host based netrc entry
171 char *host
= strdup(Uri
.Host
.c_str());
172 if (host
&& parsenetrc (host
, login
, password
, netrcfile
) == 0)
174 if (_config
->FindB("Debug::Acquire::netrc", false) == true)
175 std::clog
<< "host: " << host
176 << " user: " << login
177 << " pass-size: " << strlen(password
)
179 Uri
.User
= string (login
);
180 Uri
.Password
= string (password
);
187 // if host did not work, try Host+Path next, this will trigger
188 // a lookup uri.startswith(host) in the netrc file parser (because
190 char *hostpath
= strdup(string(Uri
.Host
+Uri
.Path
).c_str());
191 if (hostpath
&& parsenetrc (hostpath
, login
, password
, netrcfile
) == 0)
193 if (_config
->FindB("Debug::Acquire::netrc", false) == true)
194 std::clog
<< "hostpath: " << hostpath
195 << " user: " << login
196 << " pass-size: " << strlen(password
)
198 Uri
.User
= string (login
);
199 Uri
.Password
= string (password
);
208 int main(int argc
, char* argv
[])
211 char password
[64] = "";
216 if(0 == parsenetrc (argv
[1], login
, password
, argv
[2])) {
217 printf("HOST: %s LOGIN: %s PASSWORD: %s\n", argv
[1], login
, password
);