]>
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/fileutl.h>
28 /* Get user and password from .netrc when given a machine name */
32 HOSTFOUND
, /* the 'machine' keyword was found */
33 HOSTCOMPLETE
, /* the machine name following the keyword was found too */
34 HOSTVALID
, /* this is "our" machine! */
35 HOSTEND
/* LAST enum */
38 /* make sure we have room for at least this size: */
40 #define PASSWORDSIZE 64
41 #define NETRC DOT_CHAR "netrc"
43 /* returns -1 on failure, 0 if the host is found, 1 is the host isn't found */
44 int parsenetrc (char *host
, char *login
, char *password
, char *netrcfile
= NULL
)
48 int specific_login
= (login
[0] != 0);
50 bool netrc_alloc
= false;
52 int state_our_login
= false; /* With specific_login,
53 found *our* login name */
56 home
= getenv ("HOME"); /* portable environment reader */
60 pw
= getpwuid (geteuid ());
68 asprintf (&netrcfile
, "%s%s%s", home
, DIR_CHAR
, NETRC
);
75 file
= fopen (netrcfile
, "r");
80 char netrcbuffer
[256];
83 char state_login
= 0; /* Found a login keyword */
84 char state_password
= 0; /* Found a password keyword */
86 while (!done
&& fgets(netrcbuffer
, sizeof (netrcbuffer
), file
)) {
87 tok
= strtok_r (netrcbuffer
, " \t\n", &tok_buf
);
88 while (!done
&& tok
) {
89 if(login
[0] && password
[0]) {
96 if (!strcasecmp ("machine", tok
)) {
97 /* the next tok is the machine name, this is in itself the
98 delimiter that starts the stuff entered for this machine,
99 after this we need to search for 'login' and
105 /* extended definition of a "machine" if we have a "/"
106 we match the start of the string (host.startswith(token) */
107 if ((strchr(host
, '/') && strstr(host
, tok
) == host
) ||
108 (!strcasecmp (host
, tok
))) {
109 /* and yes, this is our host! */
111 retcode
= 0; /* we did find our host */
118 /* we are now parsing sub-keywords concerning "our" host */
121 state_our_login
= !strcasecmp (login
, tok
);
123 strncpy (login
, tok
, LOGINSIZE
- 1);
125 } else if (state_password
) {
126 if (state_our_login
|| !specific_login
)
127 strncpy (password
, tok
, PASSWORDSIZE
- 1);
129 } else if (!strcasecmp ("login", tok
))
131 else if (!strcasecmp ("password", tok
))
133 else if(!strcasecmp ("machine", tok
)) {
134 /* ok, there's machine here go => */
136 state_our_login
= false;
139 } /* switch (state) */
141 tok
= strtok_r (NULL
, " \t\n", &tok_buf
);
143 } /* while fgets() */
154 void maybe_add_auth (URI
&Uri
, string NetRCFile
)
156 if (_config
->FindB("Debug::Acquire::netrc", false) == true)
157 std::clog
<< "maybe_add_auth: " << (string
)Uri
158 << " " << NetRCFile
<< std::endl
;
159 if (Uri
.Password
.empty () == true || Uri
.User
.empty () == true)
161 if (NetRCFile
.empty () == false)
164 char password
[64] = "";
165 char *netrcfile
= strdup(NetRCFile
.c_str());
167 // first check for a generic host based netrc entry
168 char *host
= strdup(Uri
.Host
.c_str());
169 if (host
&& parsenetrc (host
, login
, password
, netrcfile
) == 0)
171 if (_config
->FindB("Debug::Acquire::netrc", false) == true)
172 std::clog
<< "host: " << host
173 << " user: " << login
174 << " pass-size: " << strlen(password
)
176 Uri
.User
= string (login
);
177 Uri
.Password
= string (password
);
184 // if host did not work, try Host+Path next, this will trigger
185 // a lookup uri.startswith(host) in the netrc file parser (because
187 char *hostpath
= strdup(string(Uri
.Host
+Uri
.Path
).c_str());
188 if (hostpath
&& parsenetrc (hostpath
, login
, password
, netrcfile
) == 0)
190 if (_config
->FindB("Debug::Acquire::netrc", false) == true)
191 std::clog
<< "hostpath: " << hostpath
192 << " user: " << login
193 << " pass-size: " << strlen(password
)
195 Uri
.User
= string (login
);
196 Uri
.Password
= string (password
);
205 int main(int argc
, char* argv
[])
208 char password
[64] = "";
213 if(0 == parsenetrc (argv
[1], login
, password
, argv
[2])) {
214 printf("HOST: %s LOGIN: %s PASSWORD: %s\n", argv
[1], login
, password
);