]>
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 ##################################################################### */
15 #include <apt-pkg/configuration.h>
26 /* Get user and password from .netrc when given a machine name */
30 HOSTFOUND
, /* the 'machine' keyword was found */
31 HOSTCOMPLETE
, /* the machine name following the keyword was found too */
32 HOSTVALID
, /* this is "our" machine! */
33 HOSTEND
/* LAST enum */
36 /* make sure we have room for at least this size: */
38 #define PASSWORDSIZE 64
39 #define NETRC DOT_CHAR "netrc"
41 /* returns -1 on failure, 0 if the host is found, 1 is the host isn't found */
42 int parsenetrc (char *host
, char *login
, char *password
, char *netrcfile
= NULL
)
46 int specific_login
= (login
[0] != 0);
48 bool netrc_alloc
= false;
51 char state_login
= 0; /* Found a login keyword */
52 char state_password
= 0; /* Found a password keyword */
53 int state_our_login
= false; /* With specific_login,
54 found *our* login name */
57 home
= getenv ("HOME"); /* portable environment reader */
61 pw
= getpwuid (geteuid ());
69 asprintf (&netrcfile
, "%s%s%s", home
, DIR_CHAR
, NETRC
);
76 file
= fopen (netrcfile
, "r");
81 char netrcbuffer
[256];
83 while (!done
&& fgets(netrcbuffer
, sizeof (netrcbuffer
), file
)) {
84 tok
= strtok_r (netrcbuffer
, " \t\n", &tok_buf
);
85 while (!done
&& tok
) {
86 if(login
[0] && password
[0]) {
93 if (!strcasecmp ("machine", tok
)) {
94 /* the next tok is the machine name, this is in itself the
95 delimiter that starts the stuff entered for this machine,
96 after this we need to search for 'login' and
102 if (!strcasecmp (host
, tok
)) {
103 /* and yes, this is our host! */
105 retcode
= 0; /* we did find our host */
112 /* we are now parsing sub-keywords concerning "our" host */
115 state_our_login
= !strcasecmp (login
, tok
);
117 strncpy (login
, tok
, LOGINSIZE
- 1);
119 } else if (state_password
) {
120 if (state_our_login
|| !specific_login
)
121 strncpy (password
, tok
, PASSWORDSIZE
- 1);
123 } else if (!strcasecmp ("login", tok
))
125 else if (!strcasecmp ("password", tok
))
127 else if(!strcasecmp ("machine", tok
)) {
128 /* ok, there's machine here go => */
130 state_our_login
= false;
133 } /* switch (state) */
135 tok
= strtok_r (NULL
, " \t\n", &tok_buf
);
137 } /* while fgets() */
148 void maybe_add_auth (URI
&Uri
, string NetRCFile
)
150 if (_config
->FindB("Debug::Acquire::netrc", false) == true)
151 std::clog
<< "maybe_add_auth: " << NetRCFile
<< std::endl
;
152 if (Uri
.Password
.empty () == true || Uri
.User
.empty () == true)
154 if (NetRCFile
.empty () == false)
157 char password
[64] = "";
158 char *netrcfile
= strdup (NetRCFile
.c_str ());
159 char *host
= strdup (Uri
.Host
.c_str ());
161 if (host
&& 0 == parsenetrc (host
, login
, password
, netrcfile
))
163 Uri
.User
= string (login
);
164 Uri
.Password
= string (password
);
175 int main(int argc
, char* argv
[])
178 char password
[64] = "";
183 if(0 == parsenetrc (argv
[1], login
, password
, argv
[2])) {
184 printf("HOST: %s LOGIN: %s PASSWORD: %s\n", argv
[1], login
, password
);