]>
git.saurik.com Git - apt-legacy.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 (const char *host
, char *login
, char *password
, const char *netrcfile
= NULL
)
47 int specific_login
= (login
[0] != 0);
49 char *netrc_alloc
= NULL
;
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 (&netrc_alloc
, "%s%s%s", home
, DIR_CHAR
, NETRC
);
74 netrcfile
= netrc_alloc
;
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 /* extended definition of a "machine" if we have a "/"
104 we match the start of the string (host.startswith(token) */
105 if ((strchr(host
, '/') && strstr(host
, tok
) == host
) ||
106 (!strcasecmp (host
, tok
))) {
107 /* and yes, this is our host! */
109 retcode
= 0; /* we did find our host */
116 /* we are now parsing sub-keywords concerning "our" host */
119 state_our_login
= !strcasecmp (login
, tok
);
121 strncpy (login
, tok
, LOGINSIZE
- 1);
123 } else if (state_password
) {
124 if (state_our_login
|| !specific_login
)
125 strncpy (password
, tok
, PASSWORDSIZE
- 1);
127 } else if (!strcasecmp ("login", tok
))
129 else if (!strcasecmp ("password", tok
))
131 else if(!strcasecmp ("machine", tok
)) {
132 /* ok, there's machine here go => */
134 state_our_login
= false;
137 } /* switch (state) */
139 tok
= strtok_r (NULL
, " \t\n", &tok_buf
);
141 } /* while fgets() */
152 void maybe_add_auth (URI
&Uri
, string NetRCFile
)
154 if (_config
->FindB("Debug::Acquire::netrc", false) == true)
155 std::clog
<< "maybe_add_auth: " << (string
)Uri
156 << " " << NetRCFile
<< std::endl
;
157 if (Uri
.Password
.empty () == true || Uri
.User
.empty () == true)
159 if (NetRCFile
.empty () == false)
162 char password
[64] = "";
163 const char *netrcfile
= NetRCFile
.c_str ();
165 // first check for a generic host based netrc entry
166 const char *host
= Uri
.Host
.c_str ();
167 if (parsenetrc (host
, login
, password
, netrcfile
) == 0)
169 if (_config
->FindB("Debug::Acquire::netrc", false) == true)
170 std::clog
<< "host: " << host
171 << " user: " << login
172 << " pass-size: " << strlen(password
)
174 Uri
.User
= string (login
);
175 Uri
.Password
= string (password
);
179 // if host did not work, try Host+Path next, this will trigger
180 // a lookup uri.startswith(host) in the netrc file parser (because
182 std::string
temp(Uri
.Host
+Uri
.Path
);
183 const char *hostpath
= temp
.c_str ();
184 if (hostpath
&& parsenetrc (hostpath
, login
, password
, netrcfile
) == 0)
186 if (_config
->FindB("Debug::Acquire::netrc", false) == true)
187 std::clog
<< "hostpath: " << hostpath
188 << " user: " << login
189 << " pass-size: " << strlen(password
)
191 Uri
.User
= string (login
);
192 Uri
.Password
= string (password
);
200 int main(int argc
, char* argv
[])
203 char password
[64] = "";
208 if(0 == parsenetrc (argv
[1], login
, password
, argv
[2])) {
209 printf("HOST: %s LOGIN: %s PASSWORD: %s\n", argv
[1], login
, password
);