1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2017, 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 https://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 /* The last 3 #include files should be in this order */
35 #include "curl_printf.h"
36 #include "curl_memory.h"
39 /* Get user and password from .netrc when given a machine name */
41 enum host_lookup_state {
43 HOSTFOUND, /* the 'machine' keyword was found */
44 HOSTVALID /* this is "our" machine! */
50 * *loginp and *passwordp MUST be allocated if they aren't NULL when passed
53 int Curl_parsenetrc(const char *host,
60 int specific_login = (*loginp && **loginp != 0);
61 bool netrc_alloc = FALSE;
62 enum host_lookup_state state = NOTHING;
64 char state_login = 0; /* Found a login keyword */
65 char state_password = 0; /* Found a password keyword */
66 int state_our_login = FALSE; /* With specific_login, found *our* login
69 #define NETRC DOT_CHAR "netrc"
72 bool home_alloc = FALSE;
73 char *home = curl_getenv("HOME"); /* portable environment reader */
76 #if defined(HAVE_GETPWUID_R) && defined(HAVE_GETEUID)
79 struct passwd pw, *pw_res;
81 if(!getpwuid_r(geteuid(), &pw, pwbuf, sizeof(pwbuf), &pw_res)
83 home = strdup(pw.pw_dir);
85 return CURLE_OUT_OF_MEMORY;
88 #elif defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
92 pw = getpwuid(geteuid());
100 return retcode; /* no home directory found (or possibly out of memory) */
102 netrcfile = curl_maprintf("%s%s%s", home, DIR_CHAR, NETRC);
111 file = fopen(netrcfile, FOPEN_READTEXT);
118 char netrcbuffer[256];
119 int netrcbuffsize = (int)sizeof(netrcbuffer);
121 while(!done && fgets(netrcbuffer, netrcbuffsize, file)) {
122 tok = strtok_r(netrcbuffer, " \t\n", &tok_buf);
123 if(tok && *tok == '#')
124 /* treat an initial hash as a comment line */
126 while(!done && tok) {
128 if((*loginp && **loginp) && (*passwordp && **passwordp)) {
135 if(strcasecompare("machine", tok)) {
136 /* the next tok is the machine name, this is in itself the
137 delimiter that starts the stuff entered for this machine,
138 after this we need to search for 'login' and
142 else if(strcasecompare("default", tok)) {
144 retcode = 0; /* we did find our host */
148 if(strcasecompare(host, tok)) {
149 /* and yes, this is our host! */
151 retcode = 0; /* we did find our host */
158 /* we are now parsing sub-keywords concerning "our" host */
161 state_our_login = strcasecompare(*loginp, tok);
165 *loginp = strdup(tok);
167 retcode = -1; /* allocation failed */
173 else if(state_password) {
174 if(state_our_login || !specific_login) {
176 *passwordp = strdup(tok);
178 retcode = -1; /* allocation failed */
184 else if(strcasecompare("login", tok))
186 else if(strcasecompare("password", tok))
188 else if(strcasecompare("machine", tok)) {
189 /* ok, there's machine here go => */
191 state_our_login = FALSE;
194 } /* switch (state) */
196 tok = strtok_r(NULL, " \t\n", &tok_buf);
198 } /* while fgets() */