1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2004, 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.30 2004/02/19 09:22:00 bagder Exp $
22 ***************************************************************************/
30 #ifdef HAVE_SYS_TYPES_H
31 #include <sys/types.h>
43 #include <curl/curl.h>
49 #define _MPRINTF_REPLACE /* use our functions only */
50 #include <curl/mprintf.h>
52 /* The last #include file should be: */
57 /* Debug this single source file with:
58 'make netrc' then run './netrc'!
60 Oh, make sure you have a .netrc file too ;-)
63 /* Get user and password from .netrc when given a machine name */
67 HOSTFOUND, /* the 'machine' keyword was found */
68 HOSTCOMPLETE, /* the machine name following the keyword was found too */
69 HOSTVALID, /* this is "our" machine! */
71 HOSTEND /* LAST enum */
74 /* make sure we have room for at least this size: */
76 #define PASSWORDSIZE 64
78 /* returns -1 on failure, 0 if the host is found, 1 is the host isn't found */
79 int Curl_parsenetrc(char *host,
86 int specific_login = (login[0] != 0);
88 bool home_alloc = FALSE;
89 bool netrc_alloc = FALSE;
92 char state_login=0; /* Found a login keyword */
93 char state_password=0; /* Found a password keyword */
94 char state_our_login=0; /* With specific_login, found *our* login name */
96 #define NETRC DOT_CHAR "netrc"
100 /* This is a hack to allow testing.
101 * If compiled with --enable-debug and CURL_DEBUG_NETRC is defined,
102 * then it's the path to a substitute .netrc for testing purposes *only* */
104 char *override = curl_getenv("CURL_DEBUG_NETRC");
107 printf("NETRC: overridden " NETRC " file: %s\n", home);
108 netrcfile = override;
112 #endif /* CURLDEBUG */
114 home = curl_getenv("HOME"); /* portable environment reader */
117 #if defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
121 pw= getpwuid(geteuid());
124 home = decc$translate_vms(pw->pw_dir);
135 netrcfile = curl_maprintf("%s%s%s", home, DIR_CHAR, NETRC);
144 file = fopen(netrcfile, "r");
149 char netrcbuffer[256];
151 while(!done && fgets(netrcbuffer, sizeof(netrcbuffer), file)) {
152 tok=strtok_r(netrcbuffer, " \t\n", &tok_buf);
153 while(!done && tok) {
155 if (login[0] && password[0]) {
162 if(strequal("machine", tok)) {
163 /* the next tok is the machine name, this is in itself the
164 delimiter that starts the stuff entered for this machine,
165 after this we need to search for 'login' and
171 if(strequal(host, tok)) {
172 /* and yes, this is our host! */
175 printf("HOST: %s\n", tok);
177 retcode=0; /* we did find our host */
184 /* we are now parsing sub-keywords concerning "our" host */
186 if (specific_login) {
187 state_our_login = strequal(login, tok);
190 strncpy(login, tok, LOGINSIZE-1);
192 printf("LOGIN: %s\n", login);
197 else if(state_password) {
198 if (state_our_login || !specific_login) {
199 strncpy(password, tok, PASSWORDSIZE-1);
201 printf("PASSWORD: %s\n", password);
206 else if(strequal("login", tok))
208 else if(strequal("password", tok))
210 else if(strequal("machine", tok)) {
211 /* ok, there's machine here go => */
216 } /* switch (state) */
218 tok = strtok_r(NULL, " \t\n", &tok_buf);
220 } /* while fgets() */
234 int main(int argc, char **argv)
237 char password[64]="";
242 if(0 == ParseNetrc(argv[1], login, password)) {
243 printf("HOST: %s LOGIN: %s PASSWORD: %s\n",
244 argv[1], login, password);