1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2010, 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.
22 ***************************************************************************/
40 #include <curl/curl.h>
45 #include "curl_memory.h"
48 #define _MPRINTF_REPLACE /* use our functions only */
49 #include <curl/mprintf.h>
51 /* The last #include file should be: */
54 /* Debug this single source file with:
55 'make netrc' then run './netrc'!
57 Oh, make sure you have a .netrc file too ;-)
60 /* Get user and password from .netrc when given a machine name */
64 HOSTFOUND, /* the 'machine' keyword was found */
65 HOSTCOMPLETE, /* the machine name following the keyword was found too */
66 HOSTVALID, /* this is "our" machine! */
68 HOSTEND /* LAST enum */
71 /* make sure we have room for at least this size: */
73 #define PASSWORDSIZE 64
75 /* returns -1 on failure, 0 if the host is found, 1 is the host isn't found */
76 int Curl_parsenetrc(const char *host,
83 int specific_login = (login[0] != 0);
85 bool home_alloc = FALSE;
86 bool netrc_alloc = FALSE;
89 char state_login=0; /* Found a login keyword */
90 char state_password=0; /* Found a password keyword */
91 int state_our_login=FALSE; /* With specific_login, found *our* login name */
93 #define NETRC DOT_CHAR "netrc"
97 /* This is a hack to allow testing.
98 * If compiled with --enable-debug and CURL_DEBUG_NETRC is defined,
99 * then it's the path to a substitute .netrc for testing purposes *only* */
101 char *override = curl_getenv("CURL_DEBUG_NETRC");
104 fprintf(stderr, "NETRC: overridden " NETRC " file: %s\n", override);
105 netrcfile = override;
109 #endif /* DEBUGBUILD */
111 home = curl_getenv("HOME"); /* portable environment reader */
114 #if defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
118 pw= getpwuid(geteuid());
121 home = decc_translate_vms(pw->pw_dir);
132 netrcfile = curl_maprintf("%s%s%s", home, DIR_CHAR, NETRC);
141 file = fopen(netrcfile, "r");
146 char netrcbuffer[256];
147 int netrcbuffsize = (int)sizeof(netrcbuffer);
149 while(!done && fgets(netrcbuffer, netrcbuffsize, file)) {
150 tok=strtok_r(netrcbuffer, " \t\n", &tok_buf);
151 while(!done && tok) {
153 if(login[0] && password[0]) {
160 if(Curl_raw_equal("machine", tok)) {
161 /* the next tok is the machine name, this is in itself the
162 delimiter that starts the stuff entered for this machine,
163 after this we need to search for 'login' and
169 if(Curl_raw_equal(host, tok)) {
170 /* and yes, this is our host! */
173 fprintf(stderr, "HOST: %s\n", tok);
175 retcode=0; /* we did find our host */
182 /* we are now parsing sub-keywords concerning "our" host */
185 state_our_login = Curl_raw_equal(login, tok);
188 strncpy(login, tok, LOGINSIZE-1);
190 fprintf(stderr, "LOGIN: %s\n", login);
195 else if(state_password) {
196 if(state_our_login || !specific_login) {
197 strncpy(password, tok, PASSWORDSIZE-1);
199 fprintf(stderr, "PASSWORD: %s\n", password);
204 else if(Curl_raw_equal("login", tok))
206 else if(Curl_raw_equal("password", tok))
208 else if(Curl_raw_equal("machine", tok)) {
209 /* ok, there's machine here go => */
211 state_our_login = FALSE;
214 } /* switch (state) */
216 tok = strtok_r(NULL, " \t\n", &tok_buf);
218 } /* while fgets() */
232 int main(int argc, argv_item_t argv[])
235 char password[64]="";
240 if(0 == ParseNetrc(argv[1], login, password)) {
241 printf("HOST: %s LOGIN: %s PASSWORD: %s\n",
242 argv[1], login, password);