1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2016, 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 name */
68 #define NETRC DOT_CHAR "netrc"
71 bool home_alloc = FALSE;
72 char *home = curl_getenv("HOME"); /* portable environment reader */
75 #if defined(HAVE_GETPWUID_R) && defined(HAVE_GETEUID)
78 struct passwd pw, *pw_res;
80 if(!getpwuid_r(geteuid(), &pw, pwbuf, sizeof(pwbuf), &pw_res)
82 home = strdup(pw.pw_dir);
84 return CURLE_OUT_OF_MEMORY;
87 #elif defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
91 pw= getpwuid(geteuid());
99 return retcode; /* no home directory found (or possibly out of memory) */
101 netrcfile = curl_maprintf("%s%s%s", home, DIR_CHAR, NETRC);
110 file = fopen(netrcfile, FOPEN_READTEXT);
117 char netrcbuffer[256];
118 int netrcbuffsize = (int)sizeof(netrcbuffer);
120 while(!done && fgets(netrcbuffer, netrcbuffsize, file)) {
121 tok=strtok_r(netrcbuffer, " \t\n", &tok_buf);
122 while(!done && tok) {
124 if((*loginp && **loginp) && (*passwordp && **passwordp)) {
131 if(strcasecompare("machine", tok)) {
132 /* the next tok is the machine name, this is in itself the
133 delimiter that starts the stuff entered for this machine,
134 after this we need to search for 'login' and
138 else if(strcasecompare("default", tok)) {
140 retcode=0; /* we did find our host */
144 if(strcasecompare(host, tok)) {
145 /* and yes, this is our host! */
147 retcode=0; /* we did find our host */
154 /* we are now parsing sub-keywords concerning "our" host */
157 state_our_login = strcasecompare(*loginp, tok);
161 *loginp = strdup(tok);
163 retcode = -1; /* allocation failed */
169 else if(state_password) {
170 if(state_our_login || !specific_login) {
172 *passwordp = strdup(tok);
174 retcode = -1; /* allocation failed */
180 else if(strcasecompare("login", tok))
182 else if(strcasecompare("password", tok))
184 else if(strcasecompare("machine", tok)) {
185 /* ok, there's machine here go => */
187 state_our_login = FALSE;
190 } /* switch (state) */
192 tok = strtok_r(NULL, " \t\n", &tok_buf);
194 } /* while fgets() */