From 34a324be57c7ca1fe5a2def3de2079d86c3e6fac Mon Sep 17 00:00:00 2001 From: des Date: Tue, 29 May 2018 13:07:36 +0000 Subject: [PATCH] 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 --- lib/libfetch/common.c | 38 ++++++++++++++++++++++++++++---------- lib/libfetch/fetch.c | 4 ++-- lib/libfetch/ftp.c | 6 ++++-- 3 files changed, 34 insertions(+), 14 deletions(-) diff --git a/lib/libfetch/common.c b/lib/libfetch/common.c index 6b62f4397eb..6ea92f1d459 100644 --- a/lib/libfetch/common.c +++ b/lib/libfetch/common.c @@ -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); } diff --git a/lib/libfetch/fetch.c b/lib/libfetch/fetch.c index f1c0c287aeb..ea8c3386470 100644 --- a/lib/libfetch/fetch.c +++ b/lib/libfetch/fetch.c @@ -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, ":/"))) { diff --git a/lib/libfetch/ftp.c b/lib/libfetch/ftp.c index d3f69e65f7b..72165d63fd1 100644 --- a/lib/libfetch/ftp.c +++ b/lib/libfetch/ftp.c @@ -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; -- 2.45.0