]>
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 ##################################################################### */
15 #include <apt-pkg/configuration.h>
16 #include <apt-pkg/fileutl.h>
27 /* Get user and password from .netrc when given a machine name */
31 HOSTFOUND
, /* the 'machine' keyword was found */
32 HOSTCOMPLETE
, /* the machine name following the keyword was found too */
33 HOSTVALID
, /* this is "our" machine! */
34 HOSTEND
/* LAST enum */
37 /* make sure we have room for at least this size: */
39 #define PASSWORDSIZE 64
40 #define NETRC DOT_CHAR "netrc"
42 /* returns -1 on failure, 0 if the host is found, 1 is the host isn't found */
43 int parsenetrc (char *host
, char *login
, char *password
, char *netrcfile
= NULL
)
47 int specific_login
= (login
[0] != 0);
49 bool netrc_alloc
= false;
51 int state_our_login
= false; /* With specific_login,
52 found *our* login name */
55 home
= getenv ("HOME"); /* portable environment reader */
59 pw
= getpwuid (geteuid ());
67 asprintf (&netrcfile
, "%s%s%s", home
, DIR_CHAR
, NETRC
);
74 file
= fopen (netrcfile
, "r");
79 char netrcbuffer
[256];
82 char state_login
= 0; /* Found a login keyword */
83 char state_password
= 0; /* Found a password keyword */
85 while (!done
&& fgets(netrcbuffer
, sizeof (netrcbuffer
), file
)) {
86 tok
= strtok_r (netrcbuffer
, " \t\n", &tok_buf
);
87 while (!done
&& tok
) {
88 if(login
[0] && password
[0]) {
95 if (!strcasecmp ("machine", tok
)) {
96 /* the next tok is the machine name, this is in itself the
97 delimiter that starts the stuff entered for this machine,
98 after this we need to search for 'login' and
104 /* extended definition of a "machine" if we have a "/"
105 we match the start of the string (host.startswith(token) */
106 if ((strchr(host
, '/') && strstr(host
, tok
) == host
) ||
107 (!strcasecmp (host
, tok
))) {
108 /* and yes, this is our host! */
110 retcode
= 0; /* we did find our host */
117 /* we are now parsing sub-keywords concerning "our" host */
120 state_our_login
= !strcasecmp (login
, tok
);
122 strncpy (login
, tok
, LOGINSIZE
- 1);
124 } else if (state_password
) {
125 if (state_our_login
|| !specific_login
)
126 strncpy (password
, tok
, PASSWORDSIZE
- 1);
128 } else if (!strcasecmp ("login", tok
))
130 else if (!strcasecmp ("password", tok
))
132 else if(!strcasecmp ("machine", tok
)) {
133 /* ok, there's machine here go => */
135 state_our_login
= false;
138 } /* switch (state) */
140 tok
= strtok_r (NULL
, " \t\n", &tok_buf
);
142 } /* while fgets() */
153 void maybe_add_auth (URI
&Uri
, string NetRCFile
)
155 if (_config
->FindB("Debug::Acquire::netrc", false) == true)
156 std::clog
<< "maybe_add_auth: " << (string
)Uri
157 << " " << NetRCFile
<< std::endl
;
158 if (Uri
.Password
.empty () == true || Uri
.User
.empty () == true)
160 if (NetRCFile
.empty () == false)
163 char password
[64] = "";
164 char *netrcfile
= strdup(NetRCFile
.c_str());
166 // first check for a generic host based netrc entry
167 char *host
= strdup(Uri
.Host
.c_str());
168 if (host
&& parsenetrc (host
, login
, password
, netrcfile
) == 0)
170 if (_config
->FindB("Debug::Acquire::netrc", false) == true)
171 std::clog
<< "host: " << host
172 << " user: " << login
173 << " pass-size: " << strlen(password
)
175 Uri
.User
= string (login
);
176 Uri
.Password
= string (password
);
183 // if host did not work, try Host+Path next, this will trigger
184 // a lookup uri.startswith(host) in the netrc file parser (because
186 char *hostpath
= strdup(string(Uri
.Host
+Uri
.Path
).c_str());
187 if (hostpath
&& parsenetrc (hostpath
, login
, password
, netrcfile
) == 0)
189 if (_config
->FindB("Debug::Acquire::netrc", false) == true)
190 std::clog
<< "hostpath: " << hostpath
191 << " user: " << login
192 << " pass-size: " << strlen(password
)
194 Uri
.User
= string (login
);
195 Uri
.Password
= string (password
);
204 int main(int argc
, char* argv
[])
207 char password
[64] = "";
212 if(0 == parsenetrc (argv
[1], login
, password
, argv
[2])) {
213 printf("HOST: %s LOGIN: %s PASSWORD: %s\n", argv
[1], login
, password
);