Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve pkgdb_access API #2313

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions libpkg/pkg.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -846,17 +846,28 @@ void pkg_repo_create_set_groups(struct pkg_repo_create *prc, const char *);
int pkg_repo_create(struct pkg_repo_create *, char *path);

/**
* Test if the EUID has sufficient privilege to carry out some
* operation (mode is a bitmap indicating READ, WRITE, CREATE) on the
* databases indicated in the database bitmap.
* Flags for accessing pkg database
*/
#define PKGDB_MODE_READ (0x1<<0)
#define PKGDB_MODE_WRITE (0x1<<1)
#define PKGDB_MODE_CREATE (0x1<<2)

/**
* What repos should we open: local or remote
*/
#define PKGDB_DB_LOCAL (0x1<<0)
#define PKGDB_DB_REPO (0x1<<1)
int pkgdb_access(unsigned mode, unsigned database);
/**
* Test if the EUID has sufficient privilege to carry out some
* operation (mode is a bitmap indicating READ, WRITE, CREATE) on the
* databases indicated in the database bitmap.
*
* It also accepts a number of arguments representing pkg repositories requested
* to be opened. This list *MUST* be terminated with `NULL` repo.
* If no repositories are specified (e.g. when NULL is the first argument,
* then all repositories are tried to be accessed).
*/
int pkgdb_access(unsigned mode, unsigned database, ...);

/**
* Open the local package database.
Expand Down
30 changes: 27 additions & 3 deletions libpkg/pkgdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ pkgdb_check_access(unsigned mode, const char *dbname)
}

int
pkgdb_access(unsigned mode, unsigned database)
pkgdb_access(unsigned mode, unsigned database, ...)
{
int retval = EPKG_OK;

Expand All @@ -735,6 +735,7 @@ pkgdb_access(unsigned mode, unsigned database)
* EPKG_OK: We can go ahead
*/


if ((mode & ~(PKGDB_MODE_READ|PKGDB_MODE_WRITE|PKGDB_MODE_CREATE))
!= 0)
return (EPKG_FATAL); /* EINVAL */
Expand Down Expand Up @@ -763,22 +764,45 @@ pkgdb_access(unsigned mode, unsigned database)
}

if ((database & PKGDB_DB_REPO) != 0) {
va_list ap;
struct pkg_repo *r = NULL;
const char *dbname;
ucl_object_t *repos_seen = ucl_object_typed_new(UCL_OBJECT);

va_start(ap, database);
while((dbname = va_arg(ap, const char *)) != NULL) {
ucl_object_insert_key(repos_seen, ucl_object_typed_new(UCL_BOOLEAN),
dbname, strlen(dbname), false);
}
va_end(ap);

while (pkg_repos(&r) == EPKG_OK) {
/* Ignore any repos marked as inactive */
if (!pkg_repo_enabled(r))
continue;

if (repos_seen->len > 0 && r->name &&
!ucl_object_lookup(repos_seen, r->name)) {
/* Skip what is not needed */
continue;
}

retval = r->ops->access(r, mode);
if (retval != EPKG_OK) {
if (retval == EPKG_ENODB &&
mode == PKGDB_MODE_READ)
(mode & PKGDB_MODE_READ) != PKGDB_MODE_READ) {
pkg_emit_error("Repository %s missing."
" 'pkg update' required", r->name);
" 'pkg update' required",
r->name);
}

ucl_object_unref(repos_seen);

return (retval);
}
}

ucl_object_unref(repos_seen);
}
return (retval);
}
Expand Down
8 changes: 4 additions & 4 deletions src/add.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (c) 2011-2012 Baptiste Daroussin <[email protected]>
* Copyright (c) 2011-2012 Julien Laffaye <[email protected]>
* All rights reserved.
*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
Expand All @@ -12,7 +12,7 @@
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
Expand Down Expand Up @@ -121,7 +121,7 @@ exec_add(int argc, char **argv)
retcode = pkgdb_access(PKGDB_MODE_READ |
PKGDB_MODE_WRITE |
PKGDB_MODE_CREATE,
PKGDB_DB_LOCAL);
PKGDB_DB_LOCAL, NULL);
if (retcode == EPKG_ENOACCESS) {
warnx("Insufficient privileges to add packages");
return (EXIT_FAILURE);
Expand Down Expand Up @@ -188,7 +188,7 @@ exec_add(int argc, char **argv)
}
pkgdb_release_lock(db, PKGDB_LOCK_EXCLUSIVE);
pkgdb_close(db);

if(failedpkgcount > 0) {
fflush(failedpkgs->fp);
printf("\nFailed to install the following %d package(s): %s\n", failedpkgcount, failedpkgs->buf);
Expand Down
8 changes: 4 additions & 4 deletions src/annotate.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*-
* Copyright (c) 2013-2014 Matthew Seaman <[email protected]>
* All rights reserved.
*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
Expand All @@ -11,7 +11,7 @@
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
Expand Down Expand Up @@ -110,7 +110,7 @@ do_modify(struct pkgdb *db, struct pkg *pkg, const char *tag, const char *value)
}
}
return (ret);
}
}

static int
do_delete(struct pkgdb *db, struct pkg *pkg, const char *tag)
Expand All @@ -136,7 +136,7 @@ do_delete(struct pkgdb *db, struct pkg *pkg, const char *tag)
}
}
return (ret);
}
}

static int
do_show(struct pkg *pkg, const char *tag)
Expand Down
2 changes: 1 addition & 1 deletion src/clean.c
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ exec_clean(int argc, char **argv)
return (errno == ENOENT ? EXIT_SUCCESS : EXIT_FAILURE);
}

retcode = pkgdb_access(PKGDB_MODE_READ, PKGDB_DB_REPO);
retcode = pkgdb_access(PKGDB_MODE_READ, PKGDB_DB_REPO, NULL);

if (retcode == EPKG_ENOACCESS) {
warnx("Insufficient privileges to clean old packages");
Expand Down
2 changes: 1 addition & 1 deletion src/fetch.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ exec_fetch(int argc, char **argv)
else
mode = PKGDB_MODE_READ;

retcode = pkgdb_access(mode, PKGDB_DB_REPO);
retcode = pkgdb_access(mode, PKGDB_DB_REPO, reponame, NULL);

if (retcode == EPKG_ENOACCESS) {
warnx("Insufficient privileges to access repo catalogue");
Expand Down
5 changes: 3 additions & 2 deletions src/install.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,13 @@ exec_install(int argc, char **argv)
else
repo_type = PKGDB_DB_LOCAL|PKGDB_DB_REPO;

retcode = pkgdb_access(mode, repo_type);
/* It is safe to use both `reponame` and `NULL` here */
retcode = pkgdb_access(mode, repo_type, reponame, NULL);

if (retcode == EPKG_ENOACCESS && dry_run) {
auto_update = false;
retcode = pkgdb_access(PKGDB_MODE_READ,
repo_type);
repo_type, reponame, NULL);
}

if (retcode == EPKG_ENOACCESS) {
Expand Down
2 changes: 1 addition & 1 deletion src/rquery.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ exec_rquery(int argc, char **argv)
}
}

ret = pkgdb_access(PKGDB_MODE_READ, PKGDB_DB_REPO);
ret = pkgdb_access(PKGDB_MODE_READ, PKGDB_DB_REPO, reponame, NULL);
if (ret == EPKG_ENOACCESS) {
warnx("Insufficient privileges to query the package database");
xstring_free(sqlcond);
Expand Down
10 changes: 5 additions & 5 deletions src/search.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Copyright (c) 2012-2013 Bryan Drewery <[email protected]>
* Copyright (c) 2014 Matthew Seaman <[email protected]>
* All rights reserved.
*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
Expand All @@ -15,7 +15,7 @@
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
Expand Down Expand Up @@ -44,7 +44,7 @@ typedef struct _cliopt {
char key;
} cliopt;

/* an option string should not be a prefix of any other option string */
/* an option string should not be a prefix of any other option string */
static const cliopt search_label[] = {
{ "comment", 'c' },
{ "description", 'd' },
Expand Down Expand Up @@ -77,7 +77,7 @@ static const cliopt modifiers[] = {
{ "version", 'v' },
{ "www", 'w' },
{ NULL, '\0' },
};
};

static char
match_optarg(const cliopt *optlist, const char *opt)
Expand Down Expand Up @@ -415,7 +415,7 @@ exec_search(int argc, char **argv)
quiet = false;
}

ret = pkgdb_access(PKGDB_MODE_READ, PKGDB_DB_REPO);
ret = pkgdb_access(PKGDB_MODE_READ, PKGDB_DB_REPO, reponame, NULL);
switch(ret) {
case EPKG_ENOACCESS:
warnx("Insufficient privileges to query the package database");
Expand Down
8 changes: 4 additions & 4 deletions src/update.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Copyright (c) 2011-2012 Marin Atanasov Nikolov <[email protected]>
* Copyright (c) 2014 Matthew Seaman <[email protected]>
* All rights reserved.
*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
Expand All @@ -14,7 +14,7 @@
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
Expand Down Expand Up @@ -52,7 +52,7 @@ pkgcli_update(bool force, bool strict, const char *reponame)

/* Only auto update if the user has write access. */
if (pkgdb_access(PKGDB_MODE_READ|PKGDB_MODE_WRITE|PKGDB_MODE_CREATE,
PKGDB_DB_REPO) == EPKG_ENOACCESS)
PKGDB_DB_REPO, reponame, NULL) == EPKG_ENOACCESS)
return (EPKG_OK);

if (pkg_repos_total_count() == 0) {
Expand Down Expand Up @@ -166,7 +166,7 @@ exec_update(int argc, char **argv)
}

ret = pkgdb_access(PKGDB_MODE_WRITE|PKGDB_MODE_CREATE,
PKGDB_DB_REPO);
PKGDB_DB_REPO, reponame, NULL);
if (ret == EPKG_ENOACCESS) {
warnx("Insufficient privileges to update the repository "
"catalogue.");
Expand Down
6 changes: 3 additions & 3 deletions src/upgrade.c
Original file line number Diff line number Diff line change
Expand Up @@ -322,16 +322,16 @@ exec_upgrade(int argc, char **argv)

if (dry_run && !auto_update)
retcode = pkgdb_access(PKGDB_MODE_READ,
PKGDB_DB_LOCAL|PKGDB_DB_REPO);
PKGDB_DB_LOCAL|PKGDB_DB_REPO, reponame, NULL);
else
retcode = pkgdb_access(PKGDB_MODE_READ |
PKGDB_MODE_WRITE |
PKGDB_MODE_CREATE,
PKGDB_DB_LOCAL|PKGDB_DB_REPO);
PKGDB_DB_LOCAL|PKGDB_DB_REPO, reponame, NULL);
if (retcode == EPKG_ENOACCESS && dry_run) {
auto_update = false;
retcode = pkgdb_access(PKGDB_MODE_READ,
PKGDB_DB_LOCAL|PKGDB_DB_REPO);
PKGDB_DB_LOCAL|PKGDB_DB_REPO, reponame, NULL);
bapt marked this conversation as resolved.
Show resolved Hide resolved
}

if (retcode == EPKG_ENOACCESS) {
Expand Down