Skip to content

Commit f9c7ba9

Browse files
committed
netrc: make the code try ".netrc" on Windows as well
... but fall back and try "_netrc" too if the dot version didn't work. Co-Authored-By: Steve Holme
1 parent 1a0dc6f commit f9c7ba9

File tree

1 file changed

+87
-59
lines changed

1 file changed

+87
-59
lines changed

lib/netrc.c

Lines changed: 87 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -45,79 +45,37 @@ enum host_lookup_state {
4545
HOSTVALID /* this is "our" machine! */
4646
};
4747

48+
#define NETRC_FILE_MISSING 1
49+
#define NETRC_FAILED -1
50+
#define NETRC_SUCCESS 0
51+
4852
/*
49-
* @unittest: 1304
50-
*
51-
* *loginp and *passwordp MUST be allocated if they aren't NULL when passed
52-
* in.
53+
* Returns zero on success.
5354
*/
54-
int Curl_parsenetrc(const char *host,
55-
char **loginp,
56-
char **passwordp,
57-
bool *login_changed,
58-
bool *password_changed,
59-
char *netrcfile)
55+
static int parsenetrc(const char *host,
56+
char **loginp,
57+
char **passwordp,
58+
bool *login_changed,
59+
bool *password_changed,
60+
char *netrcfile)
6061
{
6162
FILE *file;
62-
int retcode = 1;
63+
int retcode = NETRC_FILE_MISSING;
6364
char *login = *loginp;
6465
char *password = *passwordp;
6566
bool specific_login = (login && *login != 0);
6667
bool login_alloc = FALSE;
6768
bool password_alloc = FALSE;
68-
bool netrc_alloc = FALSE;
6969
enum host_lookup_state state = NOTHING;
7070

7171
char state_login = 0; /* Found a login keyword */
7272
char state_password = 0; /* Found a password keyword */
7373
int state_our_login = FALSE; /* With specific_login, found *our* login
7474
name */
7575

76-
#define NETRC DOT_CHAR "netrc"
77-
78-
if(!netrcfile) {
79-
bool home_alloc = FALSE;
80-
char *home = curl_getenv("HOME"); /* portable environment reader */
81-
if(home) {
82-
home_alloc = TRUE;
83-
#if defined(HAVE_GETPWUID_R) && defined(HAVE_GETEUID)
84-
}
85-
else {
86-
struct passwd pw, *pw_res;
87-
char pwbuf[1024];
88-
if(!getpwuid_r(geteuid(), &pw, pwbuf, sizeof(pwbuf), &pw_res)
89-
&& pw_res) {
90-
home = strdup(pw.pw_dir);
91-
if(!home)
92-
return -1;
93-
home_alloc = TRUE;
94-
}
95-
#elif defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
96-
}
97-
else {
98-
struct passwd *pw;
99-
pw = getpwuid(geteuid());
100-
if(pw) {
101-
home = pw->pw_dir;
102-
}
103-
#endif
104-
}
105-
106-
if(!home)
107-
return retcode; /* no home directory found (or possibly out of memory) */
108-
109-
netrcfile = curl_maprintf("%s%s%s", home, DIR_CHAR, NETRC);
110-
if(home_alloc)
111-
free(home);
112-
if(!netrcfile) {
113-
return -1;
114-
}
115-
netrc_alloc = TRUE;
116-
}
76+
DEBUGASSERT(netrcfile);
11777

11878
file = fopen(netrcfile, FOPEN_READTEXT);
119-
if(netrc_alloc)
120-
free(netrcfile);
12179
if(file) {
12280
char *tok;
12381
char *tok_buf;
@@ -148,14 +106,14 @@ int Curl_parsenetrc(const char *host,
148106
}
149107
else if(strcasecompare("default", tok)) {
150108
state = HOSTVALID;
151-
retcode = 0; /* we did find our host */
109+
retcode = NETRC_SUCCESS; /* we did find our host */
152110
}
153111
break;
154112
case HOSTFOUND:
155113
if(strcasecompare(host, tok)) {
156114
/* and yes, this is our host! */
157115
state = HOSTVALID;
158-
retcode = 0; /* we did find our host */
116+
retcode = NETRC_SUCCESS; /* we did find our host */
159117
}
160118
else
161119
/* not our host */
@@ -174,7 +132,7 @@ int Curl_parsenetrc(const char *host,
174132
}
175133
login = strdup(tok);
176134
if(!login) {
177-
retcode = -1; /* allocation failed */
135+
retcode = NETRC_FAILED; /* allocation failed */
178136
goto out;
179137
}
180138
login_alloc = TRUE;
@@ -190,7 +148,7 @@ int Curl_parsenetrc(const char *host,
190148
}
191149
password = strdup(tok);
192150
if(!password) {
193-
retcode = -1; /* allocation failed */
151+
retcode = NETRC_FAILED; /* allocation failed */
194152
goto out;
195153
}
196154
password_alloc = TRUE;
@@ -215,6 +173,7 @@ int Curl_parsenetrc(const char *host,
215173

216174
out:
217175
if(!retcode) {
176+
/* success */
218177
*login_changed = FALSE;
219178
*password_changed = FALSE;
220179
if(login_alloc) {
@@ -242,4 +201,73 @@ int Curl_parsenetrc(const char *host,
242201
return retcode;
243202
}
244203

204+
/*
205+
* @unittest: 1304
206+
*
207+
* *loginp and *passwordp MUST be allocated if they aren't NULL when passed
208+
* in.
209+
*/
210+
int Curl_parsenetrc(const char *host,
211+
char **loginp,
212+
char **passwordp,
213+
bool *login_changed,
214+
bool *password_changed,
215+
char *netrcfile)
216+
{
217+
int retcode = 1;
218+
char *filealloc = NULL;
219+
220+
if(!netrcfile) {
221+
char *home = curl_getenv("HOME"); /* portable environment reader */
222+
if(home) {
223+
#if defined(HAVE_GETPWUID_R) && defined(HAVE_GETEUID)
224+
}
225+
else {
226+
struct passwd pw, *pw_res;
227+
char pwbuf[1024];
228+
if(!getpwuid_r(geteuid(), &pw, pwbuf, sizeof(pwbuf), &pw_res)
229+
&& pw_res) {
230+
home = strdup(pw.pw_dir);
231+
if(!home)
232+
return -1;
233+
}
234+
#elif defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
235+
}
236+
else {
237+
struct passwd *pw;
238+
pw = getpwuid(geteuid());
239+
if(pw) {
240+
home = pw->pw_dir;
241+
}
242+
#endif
243+
}
244+
245+
if(!home)
246+
return retcode; /* no home directory found (or possibly out of
247+
memory) */
248+
249+
filealloc = curl_maprintf("%s%s.netrc", home, DIR_CHAR);
250+
if(!filealloc)
251+
return -1;
252+
retcode = parsenetrc(host, loginp, passwordp, login_changed,
253+
password_changed, filealloc);
254+
free(filealloc);
255+
#ifdef WIN32
256+
if(retcode == NETRC_FILE_MISSING) {
257+
/* fallback to the old-style "_netrc" file */
258+
filealloc = curl_maprintf("%s%s_netrc", home, DIR_CHAR);
259+
if(!filealloc)
260+
return -1;
261+
retcode = parsenetrc(host, loginp, passwordp, login_changed,
262+
password_changed, filealloc);
263+
free(filealloc);
264+
}
265+
#endif
266+
}
267+
else
268+
retcode = parsenetrc(host, loginp, passwordp, login_changed,
269+
password_changed, netrcfile);
270+
return retcode;
271+
}
272+
245273
#endif

0 commit comments

Comments
 (0)