netrc: use the NETRC environment variable (first) if set

Add test 755 to verify.

Proposed-by: Berthin Torres Callañaupa
URL: https://curl.se/mail/lib-2025-06/0015.html

Closes #17712
This commit is contained in:
Daniel Stenberg 2025-06-23 00:09:18 +02:00
parent cb9b1a4c4e
commit cb2a48bf5b
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
6 changed files with 111 additions and 39 deletions

View file

@ -396,47 +396,51 @@ NETRCcode Curl_parsenetrc(struct store_netrc *store, const char *host,
char *filealloc = NULL;
if(!netrcfile) {
char *home = NULL;
char *homea = NULL;
#if defined(HAVE_GETPWUID_R) && defined(HAVE_GETEUID)
char pwbuf[1024];
#endif
char *home = NULL;
char *homea = curl_getenv("HOME"); /* portable environment reader */
if(homea) {
home = homea;
#if defined(HAVE_GETPWUID_R) && defined(HAVE_GETEUID)
}
else {
struct passwd pw, *pw_res;
if(!getpwuid_r(geteuid(), &pw, pwbuf, sizeof(pwbuf), &pw_res)
&& pw_res) {
home = pw.pw_dir;
}
#elif defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
}
else {
struct passwd *pw;
pw = getpwuid(geteuid());
if(pw) {
home = pw->pw_dir;
}
#elif defined(_WIN32)
}
else {
homea = curl_getenv("USERPROFILE");
filealloc = curl_getenv("NETRC");
if(!filealloc) {
homea = curl_getenv("HOME"); /* portable environment reader */
if(homea) {
home = homea;
#if defined(HAVE_GETPWUID_R) && defined(HAVE_GETEUID)
}
else {
struct passwd pw, *pw_res;
if(!getpwuid_r(geteuid(), &pw, pwbuf, sizeof(pwbuf), &pw_res)
&& pw_res) {
home = pw.pw_dir;
}
#elif defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
}
else {
struct passwd *pw;
pw = getpwuid(geteuid());
if(pw) {
home = pw->pw_dir;
}
#elif defined(_WIN32)
}
else {
homea = curl_getenv("USERPROFILE");
if(homea) {
home = homea;
}
#endif
}
}
if(!home)
return NETRC_FILE_MISSING; /* no home directory found (or possibly out
of memory) */
if(!home)
return NETRC_FILE_MISSING; /* no home directory found (or possibly out
of memory) */
filealloc = aprintf("%s%s.netrc", home, DIR_CHAR);
if(!filealloc) {
free(homea);
return NETRC_OUT_OF_MEMORY;
filealloc = aprintf("%s%s.netrc", home, DIR_CHAR);
if(!filealloc) {
free(homea);
return NETRC_OUT_OF_MEMORY;
}
}
retcode = parsenetrc(store, host, loginp, passwordp, filealloc);
free(filealloc);