1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2019, 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"
24 #ifndef CURL_DISABLE_NETRC
30 #include <curl/curl.h>
35 /* The last 3 #include files should be in this order */
36 #include "curl_printf.h"
37 #include "curl_memory.h"
40 /* Get user and password from .netrc when given a machine name */
42 enum host_lookup_state {
44 HOSTFOUND, /* the 'machine' keyword was found */
45 HOSTVALID /* this is "our" machine! */
48 #define NETRC_FILE_MISSING 1
49 #define NETRC_FAILED -1
50 #define NETRC_SUCCESS 0
53 * Returns zero on success.
55 static int parsenetrc(const char *host,
59 bool *password_changed,
63 int retcode = NETRC_FILE_MISSING;
64 char *login = *loginp;
65 char *password = *passwordp;
66 bool specific_login = (login && *login != 0);
67 bool login_alloc = FALSE;
68 bool password_alloc = FALSE;
69 enum host_lookup_state state = NOTHING;
71 char state_login = 0; /* Found a login keyword */
72 char state_password = 0; /* Found a password keyword */
73 int state_our_login = FALSE; /* With specific_login, found *our* login
76 DEBUGASSERT(netrcfile);
78 file = fopen(netrcfile, FOPEN_READTEXT);
83 char netrcbuffer[4096];
84 int netrcbuffsize = (int)sizeof(netrcbuffer);
86 while(!done && fgets(netrcbuffer, netrcbuffsize, file)) {
87 tok = strtok_r(netrcbuffer, " \t\n", &tok_buf);
88 if(tok && *tok == '#')
89 /* treat an initial hash as a comment line */
93 if((login && *login) && (password && *password)) {
100 if(strcasecompare("machine", tok)) {
101 /* the next tok is the machine name, this is in itself the
102 delimiter that starts the stuff entered for this machine,
103 after this we need to search for 'login' and
107 else if(strcasecompare("default", tok)) {
109 retcode = NETRC_SUCCESS; /* we did find our host */
113 if(strcasecompare(host, tok)) {
114 /* and yes, this is our host! */
116 retcode = NETRC_SUCCESS; /* we did find our host */
123 /* we are now parsing sub-keywords concerning "our" host */
126 state_our_login = strcasecompare(login, tok);
128 else if(!login || strcmp(login, tok)) {
135 retcode = NETRC_FAILED; /* allocation failed */
142 else if(state_password) {
143 if((state_our_login || !specific_login)
144 && (!password || strcmp(password, tok))) {
147 password_alloc = FALSE;
149 password = strdup(tok);
151 retcode = NETRC_FAILED; /* allocation failed */
154 password_alloc = TRUE;
158 else if(strcasecompare("login", tok))
160 else if(strcasecompare("password", tok))
162 else if(strcasecompare("machine", tok)) {
163 /* ok, there's machine here go => */
165 state_our_login = FALSE;
168 } /* switch (state) */
170 tok = strtok_r(NULL, " \t\n", &tok_buf);
172 } /* while fgets() */
177 *login_changed = FALSE;
178 *password_changed = FALSE;
183 *login_changed = TRUE;
188 *passwordp = password;
189 *password_changed = TRUE;
207 * *loginp and *passwordp MUST be allocated if they aren't NULL when passed
210 int Curl_parsenetrc(const char *host,
214 bool *password_changed,
218 char *filealloc = NULL;
222 char *homea = curl_getenv("HOME"); /* portable environment reader */
225 #if defined(HAVE_GETPWUID_R) && defined(HAVE_GETEUID)
228 struct passwd pw, *pw_res;
230 if(!getpwuid_r(geteuid(), &pw, pwbuf, sizeof(pwbuf), &pw_res)
234 #elif defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
238 pw = getpwuid(geteuid());
246 return retcode; /* no home directory found (or possibly out of
249 filealloc = curl_maprintf("%s%s.netrc", home, DIR_CHAR);
254 retcode = parsenetrc(host, loginp, passwordp, login_changed,
255 password_changed, filealloc);
258 if(retcode == NETRC_FILE_MISSING) {
259 /* fallback to the old-style "_netrc" file */
260 filealloc = curl_maprintf("%s%s_netrc", home, DIR_CHAR);
265 retcode = parsenetrc(host, loginp, passwordp, login_changed,
266 password_changed, filealloc);
273 retcode = parsenetrc(host, loginp, passwordp, login_changed,
274 password_changed, netrcfile);