From c75b0a0311e077523ac9f79180185377e1af2176 Mon Sep 17 00:00:00 2001 From: Leigh B Stoller Date: Tue, 12 Apr 2016 12:56:39 -0600 Subject: [PATCH] Add a "check" option to emulabfeature, and make it callabled from the web interface; I do not want to reimplement any feature stuff in PHP, and its fast enough to call out to the perl script. --- utils/GNUmakefile.in | 2 +- utils/emulabfeature.in | 65 +++++++++++++++++++++++++++++------------- www/defs.php3.in | 21 ++++++++++++++ 3 files changed, 67 insertions(+), 21 deletions(-) diff --git a/utils/GNUmakefile.in b/utils/GNUmakefile.in index e7c365988..966ec7543 100644 --- a/utils/GNUmakefile.in +++ b/utils/GNUmakefile.in @@ -59,7 +59,7 @@ SBIN_SCRIPTS = vlandiff vlansync withadminprivs export_tables cvsupd.pl \ addrfdevice addrfpath WEB_SBIN_SCRIPTS= webnewnode webdeletenode webspewconlog webarchive_list \ - webwanodecheckin webspewimage webdumpdescriptor \ + webwanodecheckin webspewimage webdumpdescriptor webemulabfeature \ webdelete_image websitecheckin webclone_image webgrantimage WEB_BIN_SCRIPTS = webcreate_image websetdest weblinkmon_ctl webspewevents \ webdelay_config webcreatedataset webdeletelease \ diff --git a/utils/emulabfeature.in b/utils/emulabfeature.in index 8008db737..fd95d77d2 100644 --- a/utils/emulabfeature.in +++ b/utils/emulabfeature.in @@ -1,6 +1,6 @@ #!/usr/bin/perl -w # -# Copyright (c) 2003-2011 University of Utah and the Flux Group. +# Copyright (c) 2003-2016 University of Utah and the Flux Group. # # {{{EMULAB-LICENSE # @@ -38,6 +38,7 @@ sub usage() print STDERR " disable Globally disable feature. Provide onoff flag.\n"; print STDERR " show Show details of feature.\n"; print STDERR " list List of all features.\n"; + print STDERR " check Check a feature for a user/group (-u/-g).\n"; exit(-1); } my $optlist = "h"; @@ -67,6 +68,8 @@ use emdb; use EmulabFeatures; use libtestbed; use User; +use Group; +$EmulabFeatures::verbose = 0; # # Turn off line buffering on output @@ -90,21 +93,9 @@ if (defined($options{h})) { usage(); } usage() - if (@ARGV < 1 || @ARGV > 3); + if (@ARGV < 1); -my $action = $ARGV[0]; - -# -# Verify user, must be admin. -# -my $this_user = User->ThisUser(); -if (! defined($this_user)) { - fatal("You ($UID) do not exist!"); -} -if (!$this_user->IsAdmin()) { - fatal("You are not a testbed administrator!"); - -} +my $action = shift(@ARGV); sub ShowFeature($) { @@ -141,18 +132,18 @@ if ($action eq "list") { # Everything else needs a featurename. # usage() - if (@ARGV < 2); + if (@ARGV < 1); -my $featurename = $ARGV[1]; +my $featurename = shift(@ARGV); my $feature = EmulabFeatures->Lookup($featurename); if ($action eq "create") { usage() - if (@ARGV != 3); + if (@ARGV != 1); fatal("Feature $featurename already exists!") if (defined($feature)); - my $description = $ARGV[2]; + my $description = shift(@ARGV); $feature = EmulabFeatures->Create($featurename, $description); fatal("Could not create new feature $featurename!") @@ -175,7 +166,7 @@ elsif ($action eq "enable" || $action eq "disable") { # By default, we are setting the global enable/disable flag. # Override on command line. # - my $onoff = (@ARGV == 3 ? ($ARGV[2] ? 1 : 0) : 1); + my $onoff = (@ARGV ? ($ARGV[0] ? 1 : 0) : 1); if ($action eq "enable") { $result = $feature->SetGlobalEnable($onoff); @@ -221,6 +212,40 @@ elsif ($action eq "show") { print "\n"; } } +elsif ($action eq "check") { + fatal("Feature $featurename does not exist!") + if (!defined($feature)); + usage() + if (!@ARGV); + + my $user; + my $group; + + while (@ARGV) { + my $opt = shift(@ARGV); + usage() + if (!@ARGV); + my $val = shift(@ARGV); + + if ($opt eq "-u") { + $user = User->Lookup($val); + fatal("No such user") + if (!defined($user)); + } + elsif ($opt eq "-g") { + $group = Group->Lookup($val); + fatal("No such group") + if (!defined($group)); + } + else { + usage(); + } + } + my $enabled = EmulabFeatures->FeatureEnabled($featurename, + $user, $group, undef); + print "Feature is " . ($enabled ? "enabled" : "disabled") . "\n"; + exit($enabled); +} else { fatal("Unknown action $action!"); } diff --git a/www/defs.php3.in b/www/defs.php3.in index 8736cf52d..3db99b03c 100644 --- a/www/defs.php3.in +++ b/www/defs.php3.in @@ -521,6 +521,27 @@ function ADDPUBKEY($cmdandargs) { SUEXEC_ACTION_CONTINUE); } +# +# Check an emulab feature, calling out to the perl code. +# +function FeatureEnabled($feature, $user, $group) +{ + $command = "webemulabfeature check $feature "; + if ($user) { + $command .= "-u " . $user->uid() . " "; + } + if ($group) { + $command .= "-g " . $group->pid() . "," . $group->gid(); + } + $retval = SUEXEC("nobody", "nobody", $command, + SUEXEC_ACTION_IGNORE); + if ($retval < 0) { + SUEXECERROR(SUEXEC_ACTION_CONTINUE); + return 0; + } + return $retval; +} + # # Verify a URL. # -- GitLab