1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2012, 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 ***************************************************************************/
23 #include "curl_setup.h"
29 #include <curl/curl.h>
34 #include "curl_memory.h"
37 #define _MPRINTF_REPLACE /* use our functions only */
38 #include <curl/mprintf.h>
40 /* The last #include file should be: */
43 /* Get user and password from .netrc when given a machine name */
45 enum host_lookup_state {
47 HOSTFOUND, /* the 'machine' keyword was found */
48 HOSTVALID /* this is "our" machine! */
54 int Curl_parsenetrc(const char *host,
61 int specific_login = (login[0] != 0);
63 bool home_alloc = FALSE;
64 bool netrc_alloc = FALSE;
65 enum host_lookup_state state=NOTHING;
67 char state_login=0; /* Found a login keyword */
68 char state_password=0; /* Found a password keyword */
69 int state_our_login=FALSE; /* With specific_login, found *our* login name */
71 #define NETRC DOT_CHAR "netrc"
74 home = curl_getenv("HOME"); /* portable environment reader */
77 #if defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
81 pw= getpwuid(geteuid());
91 netrcfile = curl_maprintf("%s%s%s", home, DIR_CHAR, NETRC);
100 file = fopen(netrcfile, "r");
105 char netrcbuffer[256];
106 int netrcbuffsize = (int)sizeof(netrcbuffer);
108 while(!done && fgets(netrcbuffer, netrcbuffsize, file)) {
109 tok=strtok_r(netrcbuffer, " \t\n", &tok_buf);
110 while(!done && tok) {
112 if(login[0] && password[0]) {
119 if(Curl_raw_equal("machine", tok)) {
120 /* the next tok is the machine name, this is in itself the
121 delimiter that starts the stuff entered for this machine,
122 after this we need to search for 'login' and
128 if(Curl_raw_equal(host, tok)) {
129 /* and yes, this is our host! */
131 retcode=0; /* we did find our host */
138 /* we are now parsing sub-keywords concerning "our" host */
141 state_our_login = Curl_raw_equal(login, tok);
144 strncpy(login, tok, LOGINSIZE-1);
148 else if(state_password) {
149 if(state_our_login || !specific_login) {
150 strncpy(password, tok, PASSWORDSIZE-1);
154 else if(Curl_raw_equal("login", tok))
156 else if(Curl_raw_equal("password", tok))
158 else if(Curl_raw_equal("machine", tok)) {
159 /* ok, there's machine here go => */
161 state_our_login = FALSE;
164 } /* switch (state) */
166 tok = strtok_r(NULL, " \t\n", &tok_buf);
168 } /* while fgets() */