1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2014, 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 http://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 #include "curl_memory.h"
37 #define _MPRINTF_REPLACE /* use our functions only */
38 #include <curl/mprintf.h>
40 /* The last #include file should be: */
43 /* Get user and password from .netrc when given a machine name */
45 enum host_lookup_state {
47 HOSTFOUND, /* the 'machine' keyword was found */
48 HOSTVALID /* this is "our" machine! */
54 * *loginp and *passwordp MUST be allocated if they aren't NULL when passed
57 int Curl_parsenetrc(const char *host,
64 int specific_login = (*loginp && **loginp != 0);
65 bool netrc_alloc = FALSE;
66 enum host_lookup_state state=NOTHING;
68 char state_login=0; /* Found a login keyword */
69 char state_password=0; /* Found a password keyword */
70 int state_our_login=FALSE; /* With specific_login, found *our* login name */
72 #define NETRC DOT_CHAR "netrc"
75 bool home_alloc = FALSE;
76 char *home = curl_getenv("HOME"); /* portable environment reader */
79 #if defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
83 pw= getpwuid(geteuid());
91 return retcode; /* no home directory found (or possibly out of memory) */
93 netrcfile = curl_maprintf("%s%s%s", home, DIR_CHAR, NETRC);
102 file = fopen(netrcfile, "r");
104 Curl_safefree(netrcfile);
109 char netrcbuffer[256];
110 int netrcbuffsize = (int)sizeof(netrcbuffer);
112 while(!done && fgets(netrcbuffer, netrcbuffsize, file)) {
113 tok=strtok_r(netrcbuffer, " \t\n", &tok_buf);
114 while(!done && tok) {
116 if((*loginp && **loginp) && (*passwordp && **passwordp)) {
123 if(Curl_raw_equal("machine", tok)) {
124 /* the next tok is the machine name, this is in itself the
125 delimiter that starts the stuff entered for this machine,
126 after this we need to search for 'login' and
132 if(Curl_raw_equal(host, tok)) {
133 /* and yes, this is our host! */
135 retcode=0; /* we did find our host */
142 /* we are now parsing sub-keywords concerning "our" host */
145 state_our_login = Curl_raw_equal(*loginp, tok);
149 *loginp = strdup(tok);
151 retcode = -1; /* allocation failed */
157 else if(state_password) {
158 if(state_our_login || !specific_login) {
160 *passwordp = strdup(tok);
162 retcode = -1; /* allocation failed */
168 else if(Curl_raw_equal("login", tok))
170 else if(Curl_raw_equal("password", tok))
172 else if(Curl_raw_equal("machine", tok)) {
173 /* ok, there's machine here go => */
175 state_our_login = FALSE;
178 } /* switch (state) */
180 tok = strtok_r(NULL, " \t\n", &tok_buf);
182 } /* while fgets() */