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 ##################################################################### */
25 /* Get user and password from .netrc when given a machine name */
29 HOSTFOUND
, /* the 'machine' keyword was found */
30 HOSTCOMPLETE
, /* the machine name following the keyword was found too */
31 HOSTVALID
, /* this is "our" machine! */
32 HOSTEND
/* LAST enum */
35 /* make sure we have room for at least this size: */
37 #define PASSWORDSIZE 64
38 #define NETRC DOT_CHAR "netrc"
40 /* returns -1 on failure, 0 if the host is found, 1 is the host isn't found */
41 int parsenetrc (char *host
, char *login
, char *password
, char *netrcfile
= NULL
)
45 int specific_login
= (login
[0] != 0);
47 bool netrc_alloc
= false;
50 char state_login
= 0; /* Found a login keyword */
51 char state_password
= 0; /* Found a password keyword */
52 int state_our_login
= false; /* With specific_login,
53 found *our* login name */
56 home
= getenv ("HOME"); /* portable environment reader */
60 pw
= getpwuid (geteuid ());
68 asprintf (&netrcfile
, "%s%s%s", home
, DIR_CHAR
, NETRC
);
75 file
= fopen (netrcfile
, "r");
80 char netrcbuffer
[256];
82 while (!done
&& fgets(netrcbuffer
, sizeof (netrcbuffer
), file
)) {
83 tok
= strtok_r (netrcbuffer
, " \t\n", &tok_buf
);
84 while (!done
&& tok
) {
85 if(login
[0] && password
[0]) {
92 if (!strcasecmp ("machine", tok
)) {
93 /* the next tok is the machine name, this is in itself the
94 delimiter that starts the stuff entered for this machine,
95 after this we need to search for 'login' and
101 if (!strcasecmp (host
, tok
)) {
102 /* and yes, this is our host! */
104 retcode
= 0; /* we did find our host */
111 /* we are now parsing sub-keywords concerning "our" host */
114 state_our_login
= !strcasecmp (login
, tok
);
116 strncpy (login
, tok
, LOGINSIZE
- 1);
118 } else if (state_password
) {
119 if (state_our_login
|| !specific_login
)
120 strncpy (password
, tok
, PASSWORDSIZE
- 1);
122 } else if (!strcasecmp ("login", tok
))
124 else if (!strcasecmp ("password", tok
))
126 else if(!strcasecmp ("machine", tok
)) {
127 /* ok, there's machine here go => */
129 state_our_login
= false;
132 } /* switch (state) */
134 tok
= strtok_r (NULL
, " \t\n", &tok_buf
);
136 } /* while fgets() */
147 void maybe_add_auth (URI
&Uri
, string NetRCFile
)
149 if (Uri
.Password
.empty () == true && Uri
.User
.empty () == true)
151 if (NetRCFile
.empty () == false)
154 char password
[64] = "";
155 char *netrcfile
= strdup (NetRCFile
.c_str ());
156 char *host
= strdup (Uri
.Host
.c_str ());
158 if (host
&& 0 == parsenetrc (host
, login
, password
, netrcfile
))
160 Uri
.User
= string (login
);
161 Uri
.Password
= string (password
);
172 int main(int argc
, char* argv
[])
175 char password
[64] = "";
180 if(0 == parsenetrc (argv
[1], login
, password
, argv
[2])) {
181 printf("HOST: %s LOGIN: %s PASSWORD: %s\n", argv
[1], login
, password
);