]>
git.saurik.com Git - apt.git/blob - apt-pkg/contrib/netrc.cc
91fc7dfd70c2ae1e3da781b9de60f128d09d8ab8
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;
52 char state_login
= 0; /* Found a login keyword */
53 char state_password
= 0; /* Found a password keyword */
54 int state_our_login
= false; /* With specific_login,
55 found *our* login name */
58 home
= getenv ("HOME"); /* portable environment reader */
62 pw
= getpwuid (geteuid ());
70 asprintf (&netrcfile
, "%s%s%s", home
, DIR_CHAR
, NETRC
);
77 file
= fopen (netrcfile
, "r");
82 char netrcbuffer
[256];
84 while (!done
&& fgets(netrcbuffer
, sizeof (netrcbuffer
), file
)) {
85 tok
= strtok_r (netrcbuffer
, " \t\n", &tok_buf
);
86 while (!done
&& tok
) {
87 if(login
[0] && password
[0]) {
94 if (!strcasecmp ("machine", tok
)) {
95 /* the next tok is the machine name, this is in itself the
96 delimiter that starts the stuff entered for this machine,
97 after this we need to search for 'login' and
103 if (!strcasecmp (host
, tok
)) {
104 /* and yes, this is our host! */
106 retcode
= 0; /* we did find our host */
113 /* we are now parsing sub-keywords concerning "our" host */
116 state_our_login
= !strcasecmp (login
, tok
);
118 strncpy (login
, tok
, LOGINSIZE
- 1);
120 } else if (state_password
) {
121 if (state_our_login
|| !specific_login
)
122 strncpy (password
, tok
, PASSWORDSIZE
- 1);
124 } else if (!strcasecmp ("login", tok
))
126 else if (!strcasecmp ("password", tok
))
128 else if(!strcasecmp ("machine", tok
)) {
129 /* ok, there's machine here go => */
131 state_our_login
= false;
134 } /* switch (state) */
136 tok
= strtok_r (NULL
, " \t\n", &tok_buf
);
138 } /* while fgets() */
149 void maybe_add_auth (URI
&Uri
, string NetRCFile
)
151 if (_config
->FindB("Debug::Acquire::netrc", false) == true)
152 std::clog
<< "maybe_add_auth: " << (string
)Uri
153 << " " << NetRCFile
<< std::endl
;
154 if (Uri
.Password
.empty () == true || Uri
.User
.empty () == true)
156 if (NetRCFile
.empty () == false)
159 char password
[64] = "";
160 char *netrcfile
= strdupa (NetRCFile
.c_str ());
162 // first check for a generic host based netrc entry
163 char *host
= strdupa (Uri
.Host
.c_str ());
164 if (host
&& parsenetrc (host
, login
, password
, netrcfile
) == 0)
166 if (_config
->FindB("Debug::Acquire::netrc", false) == true)
167 std::clog
<< "host: " << host
168 << " user: " << login
169 << " pass-size: " << strlen(password
)
171 Uri
.User
= string (login
);
172 Uri
.Password
= string (password
);
176 // if host did not work, try Host+Path next
177 // FIXME: with host+path we need to match url.startswith(host+path)
178 char *hostpath
= strdupa (flNotFile(Uri
.Host
+Uri
.Path
).c_str ());
179 if (hostpath
&& parsenetrc (hostpath
, login
, password
, netrcfile
) == 0)
181 if (_config
->FindB("Debug::Acquire::netrc", false) == true)
182 std::clog
<< "hostpath: " << hostpath
183 << " user: " << login
184 << " pass-size: " << strlen(password
)
186 Uri
.User
= string (login
);
187 Uri
.Password
= string (password
);
195 int main(int argc
, char* argv
[])
198 char password
[64] = "";
203 if(0 == parsenetrc (argv
[1], login
, password
, argv
[2])) {
204 printf("HOST: %s LOGIN: %s PASSWORD: %s\n", argv
[1], login
, password
);