1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2005, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at http://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 * $Id: netrc.c,v 1.2 2007-03-15 19:22:13 andy Exp $
22 ***************************************************************************/
30 #ifdef HAVE_SYS_TYPES_H
31 #include <sys/types.h>
43 #include <curl/curl.h>
50 #define _MPRINTF_REPLACE /* use our functions only */
51 #include <curl/mprintf.h>
53 /* The last #include file should be: */
56 /* Debug this single source file with:
57 'make netrc' then run './netrc'!
59 Oh, make sure you have a .netrc file too ;-)
62 /* Get user and password from .netrc when given a machine name */
66 HOSTFOUND
, /* the 'machine' keyword was found */
67 HOSTCOMPLETE
, /* the machine name following the keyword was found too */
68 HOSTVALID
, /* this is "our" machine! */
70 HOSTEND
/* LAST enum */
73 /* make sure we have room for at least this size: */
75 #define PASSWORDSIZE 64
77 /* returns -1 on failure, 0 if the host is found, 1 is the host isn't found */
78 int Curl_parsenetrc(char *host
,
85 int specific_login
= (login
[0] != 0);
87 bool home_alloc
= FALSE
;
88 bool netrc_alloc
= FALSE
;
91 char state_login
=0; /* Found a login keyword */
92 char state_password
=0; /* Found a password keyword */
93 int state_our_login
=FALSE
; /* With specific_login, found *our* login name */
95 #define NETRC DOT_CHAR "netrc"
99 /* This is a hack to allow testing.
100 * If compiled with --enable-debug and CURL_DEBUG_NETRC is defined,
101 * then it's the path to a substitute .netrc for testing purposes *only* */
103 char *override
= curl_getenv("CURL_DEBUG_NETRC");
106 fprintf(stderr
, "NETRC: overridden " NETRC
" file: %s\n", override
);
107 netrcfile
= override
;
111 #endif /* CURLDEBUG */
113 home
= curl_getenv("HOME"); /* portable environment reader */
116 #if defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
120 pw
= getpwuid(geteuid());
123 home
= decc$
translate_vms(pw
->pw_dir
);
134 netrcfile
= curl_maprintf("%s%s%s", home
, DIR_CHAR
, NETRC
);
143 file
= fopen(netrcfile
, "r");
148 char netrcbuffer
[256];
150 while(!done
&& fgets(netrcbuffer
, sizeof(netrcbuffer
), file
)) {
151 tok
=strtok_r(netrcbuffer
, " \t\n", &tok_buf
);
152 while(!done
&& tok
) {
154 if (login
[0] && password
[0]) {
161 if(strequal("machine", tok
)) {
162 /* the next tok is the machine name, this is in itself the
163 delimiter that starts the stuff entered for this machine,
164 after this we need to search for 'login' and
170 if(strequal(host
, tok
)) {
171 /* and yes, this is our host! */
174 fprintf(stderr
, "HOST: %s\n", tok
);
176 retcode
=0; /* we did find our host */
183 /* we are now parsing sub-keywords concerning "our" host */
185 if (specific_login
) {
186 state_our_login
= strequal(login
, tok
);
189 strncpy(login
, tok
, LOGINSIZE
-1);
191 fprintf(stderr
, "LOGIN: %s\n", login
);
196 else if(state_password
) {
197 if (state_our_login
|| !specific_login
) {
198 strncpy(password
, tok
, PASSWORDSIZE
-1);
200 fprintf(stderr
, "PASSWORD: %s\n", password
);
205 else if(strequal("login", tok
))
207 else if(strequal("password", tok
))
209 else if(strequal("machine", tok
)) {
210 /* ok, there's machine here go => */
212 state_our_login
= FALSE
;
215 } /* switch (state) */
217 tok
= strtok_r(NULL
, " \t\n", &tok_buf
);
219 } /* while fgets() */
233 int main(int argc
, char **argv
)
236 char password
[64]="";
241 if(0 == ParseNetrc(argv
[1], login
, password
)) {
242 printf("HOST: %s LOGIN: %s PASSWORD: %s\n",
243 argv
[1], login
, password
);