]>
git.saurik.com Git - apt.git/blob - apt-pkg/contrib/netrc.cc
43abc62ce70c89afbc86e8b2bb916d7db6a58fd2
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: */
43 #define LOGINSIZE 1024
44 #define PASSWORDSIZE 1024
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 int parsenetrc (char *host
, char *login
, char *password
, char *netrcfile
= NULL
)
52 int specific_login
= (login
[0] != 0);
54 bool netrc_alloc
= false;
56 int state_our_login
= false; /* With specific_login,
57 found *our* login name */
60 home
= getenv ("HOME"); /* portable environment reader */
64 pw
= getpwuid (geteuid ());
72 asprintf (&netrcfile
, "%s%s%s", home
, DIR_CHAR
, NETRC
);
79 file
= fopen (netrcfile
, "r");
84 char netrcbuffer
[256];
87 char state_login
= 0; /* Found a login keyword */
88 char state_password
= 0; /* Found a password keyword */
90 while (!done
&& fgets(netrcbuffer
, sizeof (netrcbuffer
), file
)) {
91 tok
= strtok_r (netrcbuffer
, " \t\n", &tok_buf
);
92 while (!done
&& tok
) {
93 if(login
[0] && password
[0]) {
100 if (!strcasecmp ("machine", tok
)) {
101 /* the next tok is the machine name, this is in itself the
102 delimiter that starts the stuff entered for this machine,
103 after this we need to search for 'login' and
109 /* extended definition of a "machine" if we have a "/"
110 we match the start of the string (host.startswith(token) */
111 if ((strchr(host
, '/') && strstr(host
, tok
) == host
) ||
112 (!strcasecmp (host
, tok
))) {
113 /* and yes, this is our host! */
115 retcode
= 0; /* we did find our host */
122 /* we are now parsing sub-keywords concerning "our" host */
125 state_our_login
= !strcasecmp (login
, tok
);
128 if (strlen(tok
) > LOGINSIZE
)
129 _error
->Error("login token too long %i (max: %i)",
130 strlen(tok
), LOGINSIZE
);
131 strncpy (login
, tok
, LOGINSIZE
- 1);
134 } else if (state_password
) {
135 if (state_our_login
|| !specific_login
)
137 if (strlen(tok
) > PASSWORDSIZE
)
138 _error
->Error("password token too long %i (max %i)",
139 strlen(tok
), PASSWORDSIZE
);
140 strncpy (password
, tok
, PASSWORDSIZE
- 1);
143 } else if (!strcasecmp ("login", tok
))
145 else if (!strcasecmp ("password", tok
))
147 else if(!strcasecmp ("machine", tok
)) {
148 /* ok, there's machine here go => */
150 state_our_login
= false;
153 } /* switch (state) */
155 tok
= strtok_r (NULL
, " \t\n", &tok_buf
);
157 } /* while fgets() */
168 void maybe_add_auth (URI
&Uri
, string NetRCFile
)
170 if (_config
->FindB("Debug::Acquire::netrc", false) == true)
171 std::clog
<< "maybe_add_auth: " << (string
)Uri
172 << " " << NetRCFile
<< std::endl
;
173 if (Uri
.Password
.empty () == true || Uri
.User
.empty () == true)
175 if (NetRCFile
.empty () == false)
177 char login
[LOGINSIZE
] = "";
178 char password
[PASSWORDSIZE
] = "";
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 (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: " << strlen(password
)
190 Uri
.User
= string (login
);
191 Uri
.Password
= string (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(string(Uri
.Host
+Uri
.Path
).c_str());
202 if (hostpath
&& parsenetrc (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: " << strlen(password
)
209 Uri
.User
= string (login
);
210 Uri
.Password
= string (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
);