Fix an inverted conditional in the netrc code, which would ignore the

value of $HOME and always use the home directory from the passwd
database, unless $HOME was unset, in which case it would use (null).

While there, clean up handling of netrcfd and add debugging aids.

MFC after:	3 weeks
This commit is contained in:
Dag-Erling Smørgrav 2018-05-29 13:07:36 +00:00
parent 12e7376216
commit 5f04ebd4d3
3 changed files with 34 additions and 14 deletions

View File

@ -1361,19 +1361,20 @@ fetch_read_word(FILE *f)
static int
fetch_netrc_open(void)
{
const char *p;
struct passwd *pwd;
char fn[PATH_MAX];
const char *p;
int fd, serrno;
if ((p = getenv("NETRC")) != NULL) {
DEBUGF("NETRC=%s\n", p);
if (snprintf(fn, sizeof(fn), "%s", p) >= (int)sizeof(fn)) {
fetch_info("$NETRC specifies a file name "
"longer than PATH_MAX");
return (-1);
}
} else {
if ((p = getenv("HOME")) != NULL) {
struct passwd *pwd;
if ((p = getenv("HOME")) == NULL) {
if ((pwd = getpwuid(getuid())) == NULL ||
(p = pwd->pw_dir) == NULL)
return (-1);
@ -1382,7 +1383,12 @@ fetch_netrc_open(void)
return (-1);
}
return (open(fn, O_RDONLY));
if ((fd = open(fn, O_RDONLY)) < 0) {
serrno = errno;
DEBUGF("%s: %s\n", fn, strerror(serrno));
errno = serrno;
}
return (fd);
}
/*
@ -1392,24 +1398,32 @@ int
fetch_netrc_auth(struct url *url)
{
const char *word;
int serrno;
FILE *f;
if (url->netrcfd == -2)
if (url->netrcfd < 0)
url->netrcfd = fetch_netrc_open();
if (url->netrcfd < 0)
return (-1);
if ((f = fdopen(url->netrcfd, "r")) == NULL)
if ((f = fdopen(url->netrcfd, "r")) == NULL) {
serrno = errno;
DEBUGF("fdopen(netrcfd): %s", strerror(errno));
close(url->netrcfd);
url->netrcfd = -1;
errno = serrno;
return (-1);
}
rewind(f);
DEBUGF("searching netrc for %s\n", url->host);
while ((word = fetch_read_word(f)) != NULL) {
if (strcmp(word, "default") == 0) {
DEBUGF("Using default .netrc settings");
DEBUGF("using default netrc settings\n");
break;
}
if (strcmp(word, "machine") == 0 &&
(word = fetch_read_word(f)) != NULL &&
strcasecmp(word, url->host) == 0) {
DEBUGF("Using .netrc settings for %s", word);
DEBUGF("using netrc settings for %s\n", word);
break;
}
}
@ -1441,9 +1455,13 @@ fetch_netrc_auth(struct url *url)
}
}
fclose(f);
url->netrcfd = -1;
return (0);
ferr:
ferr:
serrno = errno;
fclose(f);
url->netrcfd = -1;
errno = serrno;
return (-1);
}

View File

@ -272,6 +272,7 @@ fetchMakeURL(const char *scheme, const char *host, int port, const char *doc,
fetch_syserr();
return (NULL);
}
u->netrcfd = -1;
if ((u->doc = strdup(doc ? doc : "/")) == NULL) {
fetch_syserr();
@ -286,7 +287,6 @@ fetchMakeURL(const char *scheme, const char *host, int port, const char *doc,
seturl(pwd);
#undef seturl
u->port = port;
u->netrcfd = -2;
return (u);
}
@ -352,7 +352,7 @@ fetchParseURL(const char *URL)
fetch_syserr();
return (NULL);
}
u->netrcfd = -2;
u->netrcfd = -1;
/* scheme name */
if ((p = strstr(URL, ":/"))) {

View File

@ -914,7 +914,8 @@ ftp_authenticate(conn_t *conn, struct url *url, struct url *purl)
fetch_netrc_auth(url);
user = url->user;
if (*user == '\0')
user = getenv("FTP_LOGIN");
if ((user = getenv("FTP_LOGIN")) != NULL)
DEBUGF("FTP_LOGIN=%s\n", user);
if (user == NULL || *user == '\0')
user = FTP_ANONYMOUS_USER;
if (purl && url->port == fetch_default_port(url->scheme))
@ -928,7 +929,8 @@ ftp_authenticate(conn_t *conn, struct url *url, struct url *purl)
if (e == FTP_NEED_PASSWORD) {
pwd = url->pwd;
if (*pwd == '\0')
pwd = getenv("FTP_PASSWORD");
if ((pwd = getenv("FTP_PASSWORD")) != NULL)
DEBUGF("FTP_PASSWORD=%s\n", pwd);
if (pwd == NULL || *pwd == '\0') {
if ((logname = getlogin()) == NULL)
logname = FTP_ANONYMOUS_USER;