1
/*****************************************************************************
1
/***************************************************************************
3
3
* Project ___| | | | _ \| |
4
4
* / __| | | | |_) | |
5
5
* | (__| |_| | _ <| |___
6
6
* \___|\___/|_| \_\_____|
8
* Copyright (C) 2000, Daniel Stenberg, <daniel@haxx.se>, et al.
10
* In order to be useful for every potential user, curl and libcurl are
11
* dual-licensed under the MPL and the MIT/X-derivate licenses.
8
* Copyright (C) 1998 - 2004, 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.
13
14
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
14
15
* copies of the Software, and permit persons to whom the Software is
15
* furnished to do so, under the terms of the MPL or the MIT/X-derivate
16
* licenses. You may pick one of these licenses.
16
* furnished to do so, under the terms of the COPYING file.
18
18
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
19
* KIND, either express or implied.
21
* $Id: netrc.c,v 1.18 2002/01/18 13:04:48 bagder Exp $
22
*****************************************************************************/
21
* $Id: netrc.c,v 1.31 2004/03/23 15:30:12 bagder Exp $
22
***************************************************************************/
71
75
#define LOGINSIZE 64
72
76
#define PASSWORDSIZE 64
78
/* returns -1 on failure, 0 if the host is found, 1 is the host isn't found */
74
79
int Curl_parsenetrc(char *host,
79
char netrcbuffer[256];
86
int specific_login = (login[0] != 0);
88
bool home_alloc = FALSE;
89
bool netrc_alloc = FALSE;
86
char state_password=0;
92
char state_login=0; /* Found a login keyword */
93
char state_password=0; /* Found a password keyword */
94
int state_our_login=FALSE; /* With specific_login, found *our* login name */
88
96
#define NETRC DOT_CHAR "netrc"
100
/* This is a hack to allow testing.
101
* If compiled with --enable-debug and CURL_DEBUG_NETRC is defined,
102
* then it's the path to a substitute .netrc for testing purposes *only* */
104
char *override = curl_getenv("CURL_DEBUG_NETRC");
107
printf("NETRC: overridden " NETRC " file: %s\n", home);
108
netrcfile = override;
112
#endif /* CURLDEBUG */
114
home = curl_getenv("HOME"); /* portable environment reader */
90
117
#if defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
92
pw= getpwuid(geteuid());
121
pw= getpwuid(geteuid());
95
home = decc$translate_vms(pw->pw_dir);
105
home = curl_getenv("HOME"); /* portable environment reader */
111
if(strlen(home)>(sizeof(netrcbuffer)-strlen(NETRC))) {
117
sprintf(netrcbuffer, "%s%s%s", home, DIR_CHAR, NETRC);
119
file = fopen(netrcbuffer, "r");
124
home = decc$translate_vms(pw->pw_dir);
135
netrcfile = curl_maprintf("%s%s%s", home, DIR_CHAR, NETRC);
144
file = fopen(netrcfile, "r");
123
while(fgets(netrcbuffer, sizeof(netrcbuffer), file)) {
149
char netrcbuffer[256];
151
while(!done && fgets(netrcbuffer, sizeof(netrcbuffer), file)) {
124
152
tok=strtok_r(netrcbuffer, " \t\n", &tok_buf);
153
while(!done && tok) {
155
if (login[0] && password[0]) {
128
162
if(strequal("machine", tok)) {
150
184
/* we are now parsing sub-keywords concerning "our" host */
151
185
if(state_login) {
152
strncpy(login, tok, LOGINSIZE-1);
186
if (specific_login) {
187
state_our_login = strequal(login, tok);
190
strncpy(login, tok, LOGINSIZE-1);
153
191
#ifdef _NETRC_DEBUG
154
printf("LOGIN: %s\n", login);
192
printf("LOGIN: %s\n", login);
158
197
else if(state_password) {
159
strncpy(password, tok, PASSWORDSIZE-1);
198
if (state_our_login || !specific_login) {
199
strncpy(password, tok, PASSWORDSIZE-1);
160
200
#ifdef _NETRC_DEBUG
161
printf("PASSWORD: %s\n", password);
201
printf("PASSWORD: %s\n", password);
163
204
state_password=0;
165
206
else if(strequal("login", tok))
169
210
else if(strequal("machine", tok)) {
170
211
/* ok, there's machine here go => */
171
212
state = HOSTFOUND;
213
state_our_login = FALSE;
174
216
} /* switch (state) */
175
218
tok = strtok_r(NULL, " \t\n", &tok_buf);
176
219
} /* while (tok) */
177
220
} /* while fgets() */