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.
21 ***************************************************************************/
39 #include <curl/curl.h>
44 #include "curl_memory.h"
47 #define _MPRINTF_REPLACE /* use our functions only */
48 #include <curl/mprintf.h>
50 /* The last #include file should be: */
53 /* Debug this single source file with:
54 'make netrc' then run './netrc'!
56 Oh, make sure you have a .netrc file too ;-)
59 /* Get user and password from .netrc when given a machine name */
63 HOSTFOUND, /* the 'machine' keyword was found */
64 HOSTCOMPLETE, /* the machine name following the keyword was found too */
65 HOSTVALID, /* this is "our" machine! */
67 HOSTEND /* LAST enum */
70 /* make sure we have room for at least this size: */
72 #define PASSWORDSIZE 64
74 /* returns -1 on failure, 0 if the host is found, 1 is the host isn't found */
75 int Curl_parsenetrc(const char *host,
82 int specific_login = (login[0] != 0);
84 bool home_alloc = FALSE;
85 bool netrc_alloc = FALSE;
88 char state_login=0; /* Found a login keyword */
89 char state_password=0; /* Found a password keyword */
90 int state_our_login=FALSE; /* With specific_login, found *our* login name */
92 #define NETRC DOT_CHAR "netrc"
96 /* This is a hack to allow testing.
97 * If compiled with --enable-debug and CURL_DEBUG_NETRC is defined,
98 * then it's the path to a substitute .netrc for testing purposes *only* */
100 char *override = curl_getenv("CURL_DEBUG_NETRC");
103 fprintf(stderr, "NETRC: overridden " NETRC " file: %s\n", override);
104 netrcfile = override;
108 #endif /* DEBUGBUILD */
110 home = curl_getenv("HOME"); /* portable environment reader */
113 #if defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
117 pw= getpwuid(geteuid());
120 home = decc_translate_vms(pw->pw_dir);
131 netrcfile = curl_maprintf("%s%s%s", home, DIR_CHAR, NETRC);
140 file = fopen(netrcfile, "r");
145 char netrcbuffer[256];
146 int netrcbuffsize = (int)sizeof(netrcbuffer);
148 while(!done && fgets(netrcbuffer, netrcbuffsize, file)) {
149 tok=strtok_r(netrcbuffer, " \t\n", &tok_buf);
150 while(!done && tok) {
152 if(login[0] && password[0]) {
159 if(Curl_raw_equal("machine", tok)) {
160 /* the next tok is the machine name, this is in itself the
161 delimiter that starts the stuff entered for this machine,
162 after this we need to search for 'login' and
168 if(Curl_raw_equal(host, tok)) {
169 /* and yes, this is our host! */
172 fprintf(stderr, "HOST: %s\n", tok);
174 retcode=0; /* we did find our host */
181 /* we are now parsing sub-keywords concerning "our" host */
184 state_our_login = Curl_raw_equal(login, tok);
187 strncpy(login, tok, LOGINSIZE-1);
189 fprintf(stderr, "LOGIN: %s\n", login);
194 else if(state_password) {
195 if(state_our_login || !specific_login) {
196 strncpy(password, tok, PASSWORDSIZE-1);
198 fprintf(stderr, "PASSWORD: %s\n", password);
203 else if(Curl_raw_equal("login", tok))
205 else if(Curl_raw_equal("password", tok))
207 else if(Curl_raw_equal("machine", tok)) {
208 /* ok, there's machine here go => */
210 state_our_login = FALSE;
213 } /* switch (state) */
215 tok = strtok_r(NULL, " \t\n", &tok_buf);
217 } /* while fgets() */
231 int main(int argc, argv_item_t argv[])
234 char password[64]="";
239 if(0 == ParseNetrc(argv[1], login, password)) {
240 printf("HOST: %s LOGIN: %s PASSWORD: %s\n",
241 argv[1], login, password);