From owner-svn-src-all@freebsd.org Mon Feb 20 00:14:32 2017 Return-Path: Delivered-To: svn-src-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id E9D22CE5A00; Mon, 20 Feb 2017 00:14:32 +0000 (UTC) (envelope-from bapt@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 mx1.freebsd.org (Postfix) with ESMTPS id 9DBD2D3E; Mon, 20 Feb 2017 00:14:32 +0000 (UTC) (envelope-from bapt@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id v1K0EVxu069385; Mon, 20 Feb 2017 00:14:31 GMT (envelope-from bapt@FreeBSD.org) Received: (from bapt@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id v1K0EVcF069382; Mon, 20 Feb 2017 00:14:31 GMT (envelope-from bapt@FreeBSD.org) Message-Id: <201702200014.v1K0EVcF069382@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: bapt set sender to bapt@FreeBSD.org using -f From: Baptiste Daroussin Date: Mon, 20 Feb 2017 00:14:31 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r313974 - head/lib/libfetch X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 20 Feb 2017 00:14:33 -0000 Author: bapt Date: Mon Feb 20 00:14:31 2017 New Revision: 313974 URL: https://svnweb.freebsd.org/changeset/base/313974 Log: Add a file descriptor in struct url for netrc When using libfetch in an application that drops privileges when fetching like pkg(8) then user complain because the application does not read anymore ${HOME}/.netrc. Now a caller can prepare a fd to the said file and manually assign it to the structure. It is also a first step to allow to capsicumize libfetch applications Reviewed by: allanjude, des Approved by: des Differential Revision: https://reviews.freebsd.org/D9678 Modified: head/lib/libfetch/common.c head/lib/libfetch/fetch.c head/lib/libfetch/fetch.h Modified: head/lib/libfetch/common.c ============================================================================== --- head/lib/libfetch/common.c Sun Feb 19 22:00:11 2017 (r313973) +++ head/lib/libfetch/common.c Mon Feb 20 00:14:31 2017 (r313974) @@ -1339,16 +1339,11 @@ fetch_read_word(FILE *f) return (word); } -/* - * Get authentication data for a URL from .netrc - */ -int -fetch_netrc_auth(struct url *url) +static int +fetch_netrc_open(void) { + const char *p; char fn[PATH_MAX]; - const char *word; - char *p; - FILE *f; if ((p = getenv("NETRC")) != NULL) { if (snprintf(fn, sizeof(fn), "%s", p) >= (int)sizeof(fn)) { @@ -1368,8 +1363,25 @@ fetch_netrc_auth(struct url *url) return (-1); } - if ((f = fopen(fn, "r")) == NULL) + return (open(fn, O_RDONLY)); +} + +/* + * Get authentication data for a URL from .netrc + */ +int +fetch_netrc_auth(struct url *url) +{ + const char *word; + FILE *f; + + if (url->netrcfd == -2) + url->netrcfd = fetch_netrc_open(); + if (url->netrcfd < 0) + return (-1); + if ((f = fdopen(url->netrcfd, "r")) == NULL) return (-1); + rewind(f); while ((word = fetch_read_word(f)) != NULL) { if (strcmp(word, "default") == 0) { DEBUG(fetch_info("Using default .netrc settings")); Modified: head/lib/libfetch/fetch.c ============================================================================== --- head/lib/libfetch/fetch.c Sun Feb 19 22:00:11 2017 (r313973) +++ head/lib/libfetch/fetch.c Mon Feb 20 00:14:31 2017 (r313974) @@ -284,6 +284,7 @@ fetchMakeURL(const char *scheme, const c seturl(pwd); #undef seturl u->port = port; + u->netrcfd = -2; return (u); } Modified: head/lib/libfetch/fetch.h ============================================================================== --- head/lib/libfetch/fetch.h Sun Feb 19 22:00:11 2017 (r313973) +++ head/lib/libfetch/fetch.h Mon Feb 20 00:14:31 2017 (r313974) @@ -47,6 +47,7 @@ struct url { off_t offset; size_t length; time_t ims_time; + int netrcfd; }; struct url_stat {