1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2021, 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.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! */
49 #define NETRC_FILE_MISSING 1
50 #define NETRC_FAILED -1
51 #define NETRC_SUCCESS 0
54 * Returns zero on success.
56 static int parsenetrc(const char *host,
60 bool *password_changed,
64 int retcode = NETRC_FILE_MISSING;
65 char *login = *loginp;
66 char *password = *passwordp;
67 bool specific_login = (login && *login != 0);
68 bool login_alloc = FALSE;
69 bool password_alloc = FALSE;
70 enum host_lookup_state state = NOTHING;
72 char state_login = 0; /* Found a login keyword */
73 char state_password = 0; /* Found a password keyword */
74 int state_our_login = FALSE; /* With specific_login, found *our* login
77 DEBUGASSERT(netrcfile);
79 file = fopen(netrcfile, FOPEN_READTEXT);
84 char netrcbuffer[4096];
85 int netrcbuffsize = (int)sizeof(netrcbuffer);
87 while(!done && fgets(netrcbuffer, netrcbuffsize, file)) {
89 if((netrcbuffer[0] == '\n') || (netrcbuffer[0] == '\r'))
94 tok = strtok_r(netrcbuffer, " \t\n", &tok_buf);
95 if(tok && *tok == '#')
96 /* treat an initial hash as a comment line */
99 if((login && *login) && (password && *password)) {
106 if(strcasecompare("macdef", tok)) {
107 /* Define a macro. A macro is defined with the specified name; its
108 contents begin with the next .netrc line and continue until a
109 null line (consecutive new-line characters) is encountered. */
112 else if(strcasecompare("machine", tok)) {
113 /* the next tok is the machine name, this is in itself the
114 delimiter that starts the stuff entered for this machine,
115 after this we need to search for 'login' and
119 else if(strcasecompare("default", tok)) {
121 retcode = NETRC_SUCCESS; /* we did find our host */
130 if(strcasecompare(host, tok)) {
131 /* and yes, this is our host! */
133 retcode = NETRC_SUCCESS; /* we did find our host */
140 /* we are now parsing sub-keywords concerning "our" host */
143 state_our_login = strcasecompare(login, tok);
145 else if(!login || strcmp(login, tok)) {
152 retcode = NETRC_FAILED; /* allocation failed */
159 else if(state_password) {
160 if((state_our_login || !specific_login)
161 && (!password || strcmp(password, tok))) {
164 password_alloc = FALSE;
166 password = strdup(tok);
168 retcode = NETRC_FAILED; /* allocation failed */
171 password_alloc = TRUE;
175 else if(strcasecompare("login", tok))
177 else if(strcasecompare("password", tok))
179 else if(strcasecompare("machine", tok)) {
180 /* ok, there's machine here go => */
182 state_our_login = FALSE;
185 } /* switch (state) */
187 tok = strtok_r(NULL, " \t\n", &tok_buf);
189 } /* while fgets() */
194 *login_changed = FALSE;
195 *password_changed = FALSE;
200 *login_changed = TRUE;
205 *passwordp = password;
206 *password_changed = TRUE;
224 * *loginp and *passwordp MUST be allocated if they aren't NULL when passed
227 int Curl_parsenetrc(const char *host,
231 bool *password_changed,
235 char *filealloc = NULL;
239 char *homea = curl_getenv("HOME"); /* portable environment reader */
242 #if defined(HAVE_GETPWUID_R) && defined(HAVE_GETEUID)
245 struct passwd pw, *pw_res;
247 if(!getpwuid_r(geteuid(), &pw, pwbuf, sizeof(pwbuf), &pw_res)
251 #elif defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
255 pw = getpwuid(geteuid());
263 return retcode; /* no home directory found (or possibly out of
266 filealloc = curl_maprintf("%s%s.netrc", home, DIR_CHAR);
271 retcode = parsenetrc(host, loginp, passwordp, login_changed,
272 password_changed, filealloc);
275 if(retcode == NETRC_FILE_MISSING) {
276 /* fallback to the old-style "_netrc" file */
277 filealloc = curl_maprintf("%s%s_netrc", home, DIR_CHAR);
282 retcode = parsenetrc(host, loginp, passwordp, login_changed,
283 password_changed, filealloc);
290 retcode = parsenetrc(host, loginp, passwordp, login_changed,
291 password_changed, netrcfile);