1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2015, 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>
35 #include "curl_printf.h"
37 /* The last #include files should be: */
38 #include "curl_memory.h"
41 /* Get user and password from .netrc when given a machine name */
43 enum host_lookup_state {
45 HOSTFOUND, /* the 'machine' keyword was found */
46 HOSTVALID /* this is "our" machine! */
52 * *loginp and *passwordp MUST be allocated if they aren't NULL when passed
55 int Curl_parsenetrc(const char *host,
62 int specific_login = (*loginp && **loginp != 0);
63 bool netrc_alloc = FALSE;
64 enum host_lookup_state state=NOTHING;
66 char state_login=0; /* Found a login keyword */
67 char state_password=0; /* Found a password keyword */
68 int state_our_login=FALSE; /* With specific_login, found *our* login name */
70 #define NETRC DOT_CHAR "netrc"
73 bool home_alloc = FALSE;
74 char *home = curl_getenv("HOME"); /* portable environment reader */
77 #if defined(HAVE_GETPWUID_R) && defined(HAVE_GETEUID)
80 struct passwd pw, *pw_res;
82 if(!getpwuid_r(geteuid(), &pw, pwbuf, sizeof(pwbuf), &pw_res)
84 home = strdup(pw.pw_dir);
86 return CURLE_OUT_OF_MEMORY;
89 #elif defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
93 pw= getpwuid(geteuid());
101 return retcode; /* no home directory found (or possibly out of memory) */
103 netrcfile = curl_maprintf("%s%s%s", home, DIR_CHAR, NETRC);
112 file = fopen(netrcfile, FOPEN_READTEXT);
119 char netrcbuffer[256];
120 int netrcbuffsize = (int)sizeof(netrcbuffer);
122 while(!done && fgets(netrcbuffer, netrcbuffsize, file)) {
123 tok=strtok_r(netrcbuffer, " \t\n", &tok_buf);
124 while(!done && tok) {
126 if((*loginp && **loginp) && (*passwordp && **passwordp)) {
133 if(Curl_raw_equal("machine", tok)) {
134 /* the next tok is the machine name, this is in itself the
135 delimiter that starts the stuff entered for this machine,
136 after this we need to search for 'login' and
140 else if(Curl_raw_equal("default", tok)) {
142 retcode=0; /* we did find our host */
146 if(Curl_raw_equal(host, tok)) {
147 /* and yes, this is our host! */
149 retcode=0; /* we did find our host */
156 /* we are now parsing sub-keywords concerning "our" host */
159 state_our_login = Curl_raw_equal(*loginp, tok);
163 *loginp = strdup(tok);
165 retcode = -1; /* allocation failed */
171 else if(state_password) {
172 if(state_our_login || !specific_login) {
174 *passwordp = strdup(tok);
176 retcode = -1; /* allocation failed */
182 else if(Curl_raw_equal("login", tok))
184 else if(Curl_raw_equal("password", tok))
186 else if(Curl_raw_equal("machine", tok)) {
187 /* ok, there's machine here go => */
189 state_our_login = FALSE;
192 } /* switch (state) */
194 tok = strtok_r(NULL, " \t\n", &tok_buf);
196 } /* while fgets() */