]>
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/error.h>
19 #include <apt-pkg/fileutl.h>
32 /* Get user and password from .netrc when given a machine name */
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 */
42 /* make sure we have room for at least this size: */
44 #define PASSWORDSIZE 256
45 #define NETRC DOT_CHAR "netrc"
47 /* returns -1 on failure, 0 if the host is found, 1 is the host isn't found */
48 static int parsenetrc_string (char *host
, std::string
&login
, std::string
&password
, char *netrcfile
= NULL
)
52 int specific_login
= (login
.empty() == false);
53 bool netrc_alloc
= false;
56 char const * home
= getenv ("HOME"); /* portable environment reader */
60 pw
= getpwuid (geteuid ());
68 if (asprintf (&netrcfile
, "%s%s%s", home
, DIR_CHAR
, NETRC
) == -1 || netrcfile
== NULL
)
74 file
= fopen (netrcfile
, "r");
79 char *netrcbuffer
= NULL
;
80 size_t netrcbuffer_size
= 0;
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 */
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) {
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
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! */
113 retcode
= 0; /* we did find our host */
120 /* we are now parsing sub-keywords concerning "our" host */
123 state_our_login
= !strcasecmp (login
.c_str(), tok
);
127 } else if (state_password
) {
128 if (state_our_login
|| !specific_login
)
131 } else if (!strcasecmp ("login", tok
))
133 else if (!strcasecmp ("password", tok
))
135 else if(!strcasecmp ("machine", tok
)) {
136 /* ok, there's machine here go => */
138 state_our_login
= false;
141 } /* switch (state) */
143 tok
= strtok_r (NULL
, " \t\n", &tok_buf
);
145 } /* while getline() */
156 // for some unknown reason this method is exported so keep a compatible interface for now …
157 int parsenetrc (char *host
, char *login
, char *password
, char *netrcfile
= NULL
)
159 std::string login_string
, password_string
;
160 int const ret
= parsenetrc_string(host
, login_string
, password_string
, netrcfile
);
163 strncpy(login
, login_string
.c_str(), LOGINSIZE
- 1);
164 strncpy(password
, password_string
.c_str(), PASSWORDSIZE
- 1);
169 void maybe_add_auth (URI
&Uri
, string NetRCFile
)
171 if (_config
->FindB("Debug::Acquire::netrc", false) == true)
172 std::clog
<< "maybe_add_auth: " << (string
)Uri
173 << " " << NetRCFile
<< std::endl
;
174 if (Uri
.Password
.empty () == true || Uri
.User
.empty () == true)
176 if (NetRCFile
.empty () == false)
178 std::string login
, password
;
179 char *netrcfile
= strdup(NetRCFile
.c_str());
181 // first check for a generic host based netrc entry
182 char *host
= strdup(Uri
.Host
.c_str());
183 if (host
&& parsenetrc_string(host
, login
, password
, netrcfile
) == 0)
185 if (_config
->FindB("Debug::Acquire::netrc", false) == true)
186 std::clog
<< "host: " << host
187 << " user: " << login
188 << " pass-size: " << password
.size()
191 Uri
.Password
= password
;
198 // if host did not work, try Host+Path next, this will trigger
199 // a lookup uri.startswith(host) in the netrc file parser (because
201 char *hostpath
= strdup((Uri
.Host
+ Uri
.Path
).c_str());
202 if (hostpath
&& parsenetrc_string(hostpath
, login
, password
, netrcfile
) == 0)
204 if (_config
->FindB("Debug::Acquire::netrc", false) == true)
205 std::clog
<< "hostpath: " << hostpath
206 << " user: " << login
207 << " pass-size: " << password
.size()
210 Uri
.Password
= password
;
219 int main(int argc
, char* argv
[])
222 char password
[64] = "";
227 if(0 == parsenetrc (argv
[1], login
, password
, argv
[2])) {
228 printf("HOST: %s LOGIN: %s PASSWORD: %s\n", argv
[1], login
, password
);