From b043ed4db4f0974c94decc348c8132a4f22190c2 Mon Sep 17 00:00:00 2001 From: "Joshua M. Boniface" Date: Fri, 5 Jul 2024 17:13:33 -0400 Subject: [PATCH] Add no-root flag to initialization Allows rootless operation in cases where the configured state and database paths are writable by the initializing user, bypassing both the root check and disabling the chmod/chown later in the initialization. --- rffmpeg | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/rffmpeg b/rffmpeg index 46fa525..6d6703a 100755 --- a/rffmpeg +++ b/rffmpeg @@ -611,6 +611,13 @@ def run_control(config): ) @click.command(name="init", short_help="Initialize the system.") + @click.option( + "--no-root", + "no_root_flag", + is_flag=True, + default=False, + help="Bypass root check and permission adjustments; if destinations aren't writable, init will fail.", + ) @click.option( "-y", "--yes", @@ -619,13 +626,15 @@ def run_control(config): default=False, help="Confirm initialization.", ) - def rffmpeg_click_init(confirm_flag): + def rffmpeg_click_init(no_root_flag, confirm_flag): """ Initialize the rffmpeg system and database; this will erase all hosts and current state. - This command should be run as "sudo" before any attempts to use rffmpeg. + By default this command should be run as "sudo" before any attempts to use rffmpeg. If you + do not require root permissions to write to your configured state and database paths, you + may pass the "--no-root" option. """ - if os.getuid() != 0: + if not no_root_flag and os.getuid() != 0: click.echo("Error: This command requires root privileges.") exit(1) @@ -658,19 +667,21 @@ def run_control(config): except Exception as e: fail(f"Failed to create database: {e}") - os.chown( - config["state_dir"], - getpwnam(config["dir_owner"]).pw_uid, - getgrnam(config["dir_group"]).gr_gid, - ) - os.chmod(config["state_dir"], 0o770) - if DB_TYPE == "SQLITE": + if not no_root_flag: os.chown( - config["db_path"], + config["state_dir"], getpwnam(config["dir_owner"]).pw_uid, getgrnam(config["dir_group"]).gr_gid, ) - os.chmod(config["db_path"], 0o660) + os.chmod(config["state_dir"], 0o770) + + if DB_TYPE == "SQLITE": + os.chown( + config["db_path"], + getpwnam(config["dir_owner"]).pw_uid, + getgrnam(config["dir_group"]).gr_gid, + ) + os.chmod(config["db_path"], 0o660) rffmpeg_click.add_command(rffmpeg_click_init)