app: Check for root privileges where required early

Rather than sending a dbus message that gets denied, which
is ugly.

Closes: #565

Closes: #570
Approved by: jlebon
This commit is contained in:
Colin Walters 2017-01-13 14:23:33 -05:00 committed by Atomic Bot
parent 185fba0551
commit 4a511fa011
8 changed files with 22 additions and 14 deletions

View File

@ -125,6 +125,14 @@ rpmostree_option_context_parse (GOptionContext *context,
use_daemon = ((flags & RPM_OSTREE_BUILTIN_FLAG_LOCAL_CMD) == 0);
if ((flags & RPM_OSTREE_BUILTIN_FLAG_REQUIRES_ROOT) > 0
&& getuid () != 0)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
"This command requires root privileges");
goto out;
}
if (main_entries != NULL)
g_option_context_add_main_entries (context, main_entries, NULL);

View File

@ -81,7 +81,7 @@ rpmostree_builtin_deploy (int argc,
if (!rpmostree_option_context_parse (context,
option_entries,
&argc, &argv,
RPM_OSTREE_BUILTIN_FLAG_NONE,
RPM_OSTREE_BUILTIN_FLAG_REQUIRES_ROOT,
cancellable,
&sysroot_proxy,
error))

View File

@ -63,7 +63,7 @@ rpmostree_builtin_rollback (int argc,
if (!rpmostree_option_context_parse (context,
option_entries,
&argc, &argv,
RPM_OSTREE_BUILTIN_FLAG_NONE,
RPM_OSTREE_BUILTIN_FLAG_REQUIRES_ROOT,
cancellable,
&sysroot_proxy,
error))

View File

@ -79,7 +79,7 @@ rpmostree_builtin_upgrade (int argc,
if (!rpmostree_option_context_parse (context,
option_entries,
&argc, &argv,
RPM_OSTREE_BUILTIN_FLAG_NONE,
RPM_OSTREE_BUILTIN_FLAG_REQUIRES_ROOT,
cancellable,
&sysroot_proxy,
error))

View File

@ -31,7 +31,8 @@ G_BEGIN_DECLS
typedef enum {
RPM_OSTREE_BUILTIN_FLAG_NONE = 0,
RPM_OSTREE_BUILTIN_FLAG_LOCAL_CMD = 1 << 0
RPM_OSTREE_BUILTIN_FLAG_LOCAL_CMD = 1 << 0,
RPM_OSTREE_BUILTIN_FLAG_REQUIRES_ROOT = 1 << 1,
} RpmOstreeBuiltinFlags;
typedef struct {

View File

@ -626,7 +626,7 @@ rpmostree_compose_builtin_tree (int argc,
if (!rpmostree_option_context_parse (context,
option_entries,
&argc, &argv,
RPM_OSTREE_BUILTIN_FLAG_LOCAL_CMD,
RPM_OSTREE_BUILTIN_FLAG_LOCAL_CMD | RPM_OSTREE_BUILTIN_FLAG_REQUIRES_ROOT,
cancellable,
NULL,
error))
@ -644,13 +644,6 @@ rpmostree_compose_builtin_tree (int argc,
goto out;
}
if (getuid () != 0)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
"compose tree must presently be run as uid 0 (root)");
goto out;
}
/* Test whether or not bwrap is going to work - we will fail inside e.g. a Docker
* container without --privileged or userns exposed.
*/

View File

@ -144,7 +144,7 @@ rpmostree_builtin_pkg_add (int argc,
if (!rpmostree_option_context_parse (context,
option_entries,
&argc, &argv,
RPM_OSTREE_BUILTIN_FLAG_NONE,
RPM_OSTREE_BUILTIN_FLAG_REQUIRES_ROOT,
cancellable,
&sysroot_proxy,
error))
@ -178,7 +178,7 @@ rpmostree_builtin_pkg_remove (int argc,
if (!rpmostree_option_context_parse (context,
option_entries,
&argc, &argv,
RPM_OSTREE_BUILTIN_FLAG_NONE,
RPM_OSTREE_BUILTIN_FLAG_REQUIRES_ROOT,
cancellable,
&sysroot_proxy,
error))

View File

@ -34,6 +34,12 @@ vm_send_test_repo
# make sure the package is not already layered
vm_assert_layered_pkg foo absent
# Be sure an unprivileged user exists
vm_cmd getent passwd bin
if vm_cmd "runuser -u bin rpm-ostree pkg-add foo-1.0"; then
assert_not_reached "Was able to install a package as non-root!"
fi
vm_cmd rpm-ostree pkg-add foo-1.0
echo "ok pkg-add foo"