From 921e9a76f1ca47d30d765f2f2bf61954e8ac9c9c Mon Sep 17 00:00:00 2001 From: Slack Coder Date: Tue, 21 Jan 2025 16:11:42 -0500 Subject: Avoid requiring root access Make temporary folder generation lazy to postpone requiring root access until needed. --- SBO-Lib/lib/SBO/App/Remove.pm | 7 ++++++- SBO-Lib/lib/SBO/App/Snap.pm | 7 ++++++- SBO-Lib/lib/SBO/Lib.pm | 5 ----- SBO-Lib/lib/SBO/Lib/Build.pm | 10 ++-------- SBO-Lib/lib/SBO/Lib/Repo.pm | 4 ++-- sbocheck | 7 ++++++- sboclean | 7 ++++++- sboconfig | 7 ++++++- sboinstall | 7 ++++++- sboupgrade | 7 ++++++- t/07-fail.t | 2 +- t/16-clean.t | 2 ++ 12 files changed, 49 insertions(+), 23 deletions(-) diff --git a/SBO-Lib/lib/SBO/App/Remove.pm b/SBO-Lib/lib/SBO/App/Remove.pm index 4c7dd31..b97cdf8 100644 --- a/SBO-Lib/lib/SBO/App/Remove.pm +++ b/SBO-Lib/lib/SBO/App/Remove.pm @@ -10,7 +10,7 @@ package SBO::App::Remove; use 5.16.0; use strict; use warnings FATAL => 'all'; -use SBO::Lib qw/ get_inst_names get_installed_packages get_sbo_location get_build_queue merge_queues get_requires get_readme_contents prompt show_version in /; +use SBO::Lib qw/ _ERR_USAGE get_inst_names get_installed_packages get_sbo_location get_build_queue merge_queues get_requires get_readme_contents prompt show_version in /; use Getopt::Long qw(GetOptionsFromArray :config bundling); use parent 'SBO::App'; @@ -41,6 +41,11 @@ sub run { if ($self->{vers}) { $self->show_version(); return 0; } if (!@{ $self->{args} }) { $self->show_usage(); return 1; } + unless ($< == 0) { + warn "This script requires root privileges.\n"; + exit _ERR_USAGE; + } + # current workflow: # * get names of all installed SBo packages # * compare commandline args to SBo packages as well as installed SBo packages diff --git a/SBO-Lib/lib/SBO/App/Snap.pm b/SBO-Lib/lib/SBO/App/Snap.pm index d2b0f57..f9e44a6 100644 --- a/SBO-Lib/lib/SBO/App/Snap.pm +++ b/SBO-Lib/lib/SBO/App/Snap.pm @@ -13,7 +13,7 @@ package SBO::App::Snap; use 5.16.0; use strict; use warnings FATAL => 'all'; -use SBO::Lib qw/ fetch_tree import_gpg_key update_tree %config show_version /; +use SBO::Lib qw/ _ERR_USAGE fetch_tree import_gpg_key update_tree %config show_version /; use Getopt::Long qw/ GetOptionsFromArray /; use parent 'SBO::App'; @@ -64,6 +64,11 @@ sub run { if ($self->{help}) { $self->show_usage(); return 0 } if ($self->{vers}) { $self->show_version(); return 0 } + unless ($< == 0) { + warn "This script requires root privileges.\n"; + exit _ERR_USAGE; + } + # check for a command and, if found, execute it $args[0] //= ''; diff --git a/SBO-Lib/lib/SBO/Lib.pm b/SBO-Lib/lib/SBO/Lib.pm index edb418a..fe96cde 100644 --- a/SBO-Lib/lib/SBO/Lib.pm +++ b/SBO-Lib/lib/SBO/Lib.pm @@ -94,11 +94,6 @@ our %EXPORT_TAGS = ( config => $SBO::Lib::Util::EXPORT_TAGS{config}, ); -unless ($< == 0) { - warn "This script requires root privileges.\n"; - exit _ERR_USAGE; -} - 'ok'; __END__ diff --git a/SBO-Lib/lib/SBO/Lib/Build.pm b/SBO-Lib/lib/SBO/Lib/Build.pm index e6aa66b..ad760b7 100644 --- a/SBO-Lib/lib/SBO/Lib/Build.pm +++ b/SBO-Lib/lib/SBO/Lib/Build.pm @@ -37,7 +37,6 @@ our @EXPORT_OK = qw{ rewrite_slackbuild run_tee - $tempdir $tmpd $env_tmp }; @@ -71,11 +70,6 @@ set. Will be the same as C<$TMP> if it is set, otherwise it will be C. -=head2 $tempdir - -This is a temporary directory created for sbotools' use, and it should be -cleaned up when sbotools exits. - =cut # get $TMP from the env, if defined - we use two variables here because there @@ -85,8 +79,6 @@ our $env_tmp = $ENV{TMP}; our $tmpd = $env_tmp ? $env_tmp : '/tmp/SBo'; make_path($tmpd) unless -d $tmpd; -our $tempdir = tempdir(CLEANUP => 1, DIR => $tmpd); - =head1 SUBROUTINES =cut @@ -729,6 +721,8 @@ output will be C and the exit status will hold a true value. sub run_tee { my $cmd = shift; + my $tempdir = tempdir(CLEANUP => 1, DIR => $tmpd); + my $out_fh = tempfile(DIR => $tempdir); my $out_fn = get_tmp_extfn($out_fh); return undef, _ERR_F_SETFD if not defined $out_fn; diff --git a/SBO-Lib/lib/SBO/Lib/Repo.pm b/SBO-Lib/lib/SBO/Lib/Repo.pm index 6c7babb..5337ea8 100644 --- a/SBO-Lib/lib/SBO/Lib/Repo.pm +++ b/SBO-Lib/lib/SBO/Lib/Repo.pm @@ -442,10 +442,10 @@ C<$repo_path>, and if not, offer to run C for you. sub slackbuilds_or_fetch { unless (chk_slackbuilds_txt()) { say 'It looks like you haven\'t run "sbosnap fetch" yet.'; - if (prompt("Would you like me to do this now?", default => 'yes')) { + if (($< == 0) && prompt("Would you like me to do this now?", default => 'yes')) { fetch_tree(); } else { - say 'Please run "sbosnap fetch"'; + say 'Please run "sbosnap fetch" as root'; exit 0; } } diff --git a/sbocheck b/sbocheck index f10ced7..bf2ddff 100755 --- a/sbocheck +++ b/sbocheck @@ -13,7 +13,7 @@ use 5.16.0; use strict; use warnings FATAL => 'all'; -use SBO::Lib qw/ update_tree get_available_updates script_error open_fh is_local show_version get_local_outdated_versions /; +use SBO::Lib qw/ _ERR_USAGE update_tree get_available_updates script_error open_fh is_local show_version get_local_outdated_versions /; use Getopt::Long; use File::Basename; use List::Util 'max'; @@ -42,6 +42,11 @@ GetOptions('help|h' => \$help, 'version|v' => \$vers); if ($help) { show_usage(); exit 0 } if ($vers) { show_version(); exit 0 } +unless ($< == 0) { + warn "This script requires root privileges.\n"; + exit +} + update_tree(); # retrieve and format list of available updates diff --git a/sboclean b/sboclean index f1eb695..980de44 100755 --- a/sboclean +++ b/sboclean @@ -13,7 +13,7 @@ use 5.16.0; use strict; use warnings FATAL => 'all'; -use SBO::Lib qw/ prompt usage_error script_error in show_version %config /; +use SBO::Lib qw/ _ERR_USAGE prompt usage_error script_error in show_version %config /; use File::Basename; use Getopt::Long qw(:config bundling); use File::Path qw(remove_tree); @@ -56,6 +56,11 @@ if ($vers) { show_version(); exit 0 } usage_error("You must specify at least one of -d or -w.") unless ($dist || $work); +unless ($< == 0) { + warn "This script requires root privileges.\n"; + exit _ERR_USAGE; +} + sub rm_full { script_error('rm_full requires an argument.') unless @_ == 1; my $full = shift; diff --git a/sboconfig b/sboconfig index b245d24..c7ea9b0 100755 --- a/sboconfig +++ b/sboconfig @@ -13,7 +13,7 @@ use 5.16.0; use strict; use warnings FATAL => 'all'; -use SBO::Lib qw/ slurp usage_error script_error $tempdir open_fh %config $conf_dir $conf_file show_version /; +use SBO::Lib qw/ _ERR_USAGE slurp usage_error script_error open_fh %config $conf_dir $conf_file show_version /; use File::Basename; use Getopt::Long qw(:config no_ignore_case_always); use File::Copy; @@ -136,6 +136,11 @@ if (exists $changes{SLACKWARE_VERSION}) { usage_error("$warn -V") unless $changes{SLACKWARE_VERSION} =~ m/^(\d+\.\d+|FALSE)$/; } +unless ($< == 0) { + warn "This script requires root privileges.\n"; + exit _ERR_USAGE; +} + sub config_write { script_error('config_write requires at least two arguments.') unless @_ >= 2; diff --git a/sboinstall b/sboinstall index 156b41b..73fb2f4 100755 --- a/sboinstall +++ b/sboinstall @@ -13,7 +13,7 @@ use 5.16.0; use strict; use warnings FATAL => 'all'; -use SBO::Lib qw/ %config _ERR_OPENFH get_arch get_build_queue get_installed_cpans get_installed_packages get_sbo_location get_sbo_locations in merge_queues open_fh print_failures process_sbos prompt show_version slackbuilds_or_fetch slurp usage_error user_prompt /; +use SBO::Lib qw/ %config _ERR_USAGE _ERR_OPENFH get_arch get_build_queue get_installed_cpans get_installed_packages get_sbo_location get_sbo_locations in merge_queues open_fh print_failures process_sbos prompt show_version slackbuilds_or_fetch slurp usage_error user_prompt /; use Getopt::Long qw(:config bundling); use File::Basename; use JSON::PP; @@ -95,6 +95,11 @@ if ($compat32) { usage_error("compat32 only works on x86_64.") unless get_arch eq 'x86_64'; } +unless ($< == 0) { + warn "This script requires root privileges.\n"; + exit _ERR_USAGE; +} + # if we can't find SLACKBUILDS.TXT in $config{HOME}, prompt to fetch the tree slackbuilds_or_fetch(); diff --git a/sboupgrade b/sboupgrade index 37fb245..81a9a3f 100755 --- a/sboupgrade +++ b/sboupgrade @@ -13,7 +13,7 @@ use 5.16.0; use strict; use warnings FATAL => 'all'; -use SBO::Lib qw/ get_available_updates prompt usage_error slackbuilds_or_fetch uniq get_sbo_location get_inst_names get_installed_packages get_build_queue get_sbo_locations in merge_queues user_prompt process_sbos print_failures %config show_version /; +use SBO::Lib qw/ _ERR_USAGE get_available_updates prompt usage_error slackbuilds_or_fetch uniq get_sbo_location get_inst_names get_installed_packages get_build_queue get_sbo_locations in merge_queues user_prompt process_sbos print_failures %config show_version /; use Getopt::Long qw(:config bundling); use File::Basename; use File::Copy; @@ -71,6 +71,11 @@ GetOptions( if ($help) { show_usage(); exit 0 } if ($vers) { show_version(); exit 0 } +unless ($< == 0) { + warn "This script requires root privileges.\n"; + exit _ERR_USAGE; +} + my $updates; if ($all) { slackbuilds_or_fetch(); diff --git a/t/07-fail.t b/t/07-fail.t index cd1fa7c..fd1db42 100755 --- a/t/07-fail.t +++ b/t/07-fail.t @@ -11,7 +11,7 @@ use Test::Sbotools qw/ make_slackbuilds_txt set_lo set_repo sboconfig sboinstall use File::Temp 'tempdir'; if ($ENV{TEST_INSTALL}) { - plan tests => 24; + plan tests => 26; } else { plan skip_all => 'Only run these tests if TEST_INSTALL=1'; } diff --git a/t/16-clean.t b/t/16-clean.t index b695ae8..f756963 100755 --- a/t/16-clean.t +++ b/t/16-clean.t @@ -86,6 +86,8 @@ sboclean '-d', { exit => 0, expected => "Nothing to do.\n" }; # 13-15: sboclean -w [-i] with TMP set { local $ENV{TMP} = tempdir(CLEANUP => 1); + set_sbo_home("/usr/sbo"); + sboinstall qw/ -r nonexistentslackbuild /, { test => 0 }; sboclean qw/ -w -i /, { input => "n", expected => qr!\QRemove $ENV{TMP}/\E.*\Q? [n]\E! }; sboclean qw/ -w -i /, { input => "y\ny", expected => qr!\QRemove $ENV{TMP}/\E.*\Q? [n]\E! }; sboclean '-w', { input => "y", expected => qr/This will remove the entire contents of \Q$ENV{TMP}\E/ }; -- cgit v1.2.3