From owner-svn-src-head@freebsd.org Tue May 29 13:07:38 2018 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 0788EF7B57C; Tue, 29 May 2018 13:07:38 +0000 (UTC) (envelope-from des@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id B160469728; Tue, 29 May 2018 13:07:37 +0000 (UTC) (envelope-from des@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 921531C578; Tue, 29 May 2018 13:07:37 +0000 (UTC) (envelope-from des@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id w4TD7bX5021302; Tue, 29 May 2018 13:07:37 GMT (envelope-from des@FreeBSD.org) Received: (from des@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id w4TD7a9H021300; Tue, 29 May 2018 13:07:36 GMT (envelope-from des@FreeBSD.org) Message-Id: <201805291307.w4TD7a9H021300@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: des set sender to des@FreeBSD.org using -f From: =?UTF-8?Q?Dag-Erling_Sm=c3=b8rgrav?= Date: Tue, 29 May 2018 13:07:36 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r334326 - head/lib/libfetch X-SVN-Group: head X-SVN-Commit-Author: des X-SVN-Commit-Paths: head/lib/libfetch X-SVN-Commit-Revision: 334326 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.26 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 29 May 2018 13:07:38 -0000 Author: des Date: Tue May 29 13:07:36 2018 New Revision: 334326 URL: https://svnweb.freebsd.org/changeset/base/334326 Log: 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 Modified: head/lib/libfetch/common.c head/lib/libfetch/fetch.c head/lib/libfetch/ftp.c Modified: head/lib/libfetch/common.c ============================================================================== --- head/lib/libfetch/common.c Tue May 29 12:43:03 2018 (r334325) +++ head/lib/libfetch/common.c Tue May 29 13:07:36 2018 (r334326) @@ -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); } Modified: head/lib/libfetch/fetch.c ============================================================================== --- head/lib/libfetch/fetch.c Tue May 29 12:43:03 2018 (r334325) +++ head/lib/libfetch/fetch.c Tue May 29 13:07:36 2018 (r334326) @@ -272,6 +272,7 @@ fetchMakeURL(const char *scheme, const char *host, int 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 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, ":/"))) { Modified: head/lib/libfetch/ftp.c ============================================================================== --- head/lib/libfetch/ftp.c Tue May 29 12:43:03 2018 (r334325) +++ head/lib/libfetch/ftp.c Tue May 29 13:07:36 2018 (r334326) @@ -914,7 +914,8 @@ ftp_authenticate(conn_t *conn, struct url *url, struct 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 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;