Skip to content

Commit

Permalink
Add a file descriptor in struct url for netrc
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bapt committed Feb 20, 2017
1 parent d893c36 commit d8713bf
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
30 changes: 21 additions & 9 deletions lib/libfetch/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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"));
Expand Down
1 change: 1 addition & 0 deletions lib/libfetch/fetch.c
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ fetchMakeURL(const char *scheme, const char *host, int port, const char *doc,
seturl(pwd);
#undef seturl
u->port = port;
u->netrcfd = -2;

return (u);
}
Expand Down
1 change: 1 addition & 0 deletions lib/libfetch/fetch.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ struct url {
off_t offset;
size_t length;
time_t ims_time;
int netrcfd;
};

struct url_stat {
Expand Down

0 comments on commit d8713bf

Please sign in to comment.