managetaint.in 8.33 KB
Newer Older
1 2
#!/usr/bin/perl -wT
#
3
# Copyright (c) 2001- 2016 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
#

#
# This utility manages setting, removing and displaying taint states for
# any Emulab objects that support them (e.g., nodes and OS descriptors).
#

use strict;
use English;
use Getopt::Std;

#
# Configure variables
#
my $TB		 = "@prefix@";

#
# Testbed Support libraries
#
use lib "@prefix@/lib";
Kirk Webb's avatar
Kirk Webb committed
43
use libdb;
44 45 46 47 48 49 50 51 52
use EmulabConstants;
use User;

# Protos
sub usage();
sub fatal($);
sub ParseArgs(@);
sub DoAction($$$@);
sub GetObject($$);
Kirk Webb's avatar
Kirk Webb committed
53
sub PrintAllObjects($);
54 55 56 57 58 59

#
# Top level constants
#
my @ALLTAINTS = TB_TAINTSTATE_ALL();
my @TAINT_OBJECT_TYPES = ("node","os");
Kirk Webb's avatar
Kirk Webb committed
60
my @TAINT_ACTIONS = ("add","remove","set","clear","show","showall");
61 62 63 64 65 66 67 68 69 70

#
# Global variables
#

#
# Get, set, and display taint states for supported objects
#
sub usage()
{
Kirk Webb's avatar
Kirk Webb committed
71
    print STDERR "Usage: managetaint <action> <objtype> [<OID> <taint> ...]\n";
72
    print STDERR "  -h       This message\n";
Kirk Webb's avatar
Kirk Webb committed
73 74 75 76 77 78 79 80
    print STDERR " Actions:\n";
    print STDERR "  add      Apply one or more taint modes to an object.\n";
    print STDERR "  remove   Remove one or more taint modes from an object.\n";
    print STDERR "  set      Set a taint mode on specified object.\n";
    print STDERR "  clear    Clear all taint states from an object.\n";
    print STDERR "  show     Show all taint modes set for an object.\n";
    print STDERR "  showall  Display all taint modes across all objects.\n";
    print STDERR " Object Types (and associated object identifiers):\n";
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
    print STDERR "  Node     Testbed Node\n";
    print STDERR "   OID     node_id\n";
    print STDERR "  OS       OS Descriptor\n";
    print STDERR "   OID     <project>/<osname>";
    print STDERR " Taint States: ". join(",",@ALLTAINTS) ."\n";
    exit(-1);
}
my $optlist  = "h";
my $debug    = 0;

#
# Please do not run as root. Hard to track what has happened.
#
if ($UID == 0) {
    die("*** $0:\n".
	"    Please do not run this as root!\n");
}

#
# Turn off line buffering on output
#
$| = 1;

#
# Untaint the path
# 
$ENV{'PATH'} = "/bin:/sbin:/usr/bin:";

#
# Verify user, must be admin.
#
my $this_user = User->ThisUser();
if (! defined($this_user)) {
    fatal("You ($UID) do not exist!");
}
# Probably will need to relax this so that regular users can add taint states.
if (!$this_user->IsAdmin()) {
    fatal("You are not a testbed administrator!");
}

#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
my %options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{h})) {
    usage();
}
usage()
Kirk Webb's avatar
Kirk Webb committed
133
    if (@ARGV < 2);
134

Kirk Webb's avatar
Kirk Webb committed
135
my ($action, $objtype, $oid, @in_states) = ParseArgs(@ARGV);
136 137 138 139

fatal("Argument parsing failed!") 
    if (!defined($objtype));

Kirk Webb's avatar
Kirk Webb committed
140
DoAction($action, $objtype, $oid, @in_states) or
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
    fatal("Failed to perform $action on $objtype object $oid");

exit 0;

#
# Input argument parsing and sanity checking.
#
sub ParseArgs(@) {
    my @UARGV = ();

    # Untaint arguments
    foreach my $targ (@_) {
	if ($targ =~ /^[-\w_\/]+$/) {
	    push @UARGV, lc($targ);
	} else {
	    fatal("Malformed argument provided on command line: $targ");
	}
    }

Kirk Webb's avatar
Kirk Webb committed
160
    my ($action, $objtype, $oid, @in_states) = @UARGV;
161 162 163 164 165 166

    # Argument sanity checks.
    if (!grep {$_ eq $action} @TAINT_ACTIONS) {
	fatal("Invalid action: $action");
    }

Kirk Webb's avatar
Kirk Webb committed
167 168 169 170
    if (!grep {$_ eq $objtype} @TAINT_OBJECT_TYPES) {
	fatal("Invalid taint object type: $objtype");
    }

171 172 173 174 175 176 177 178
    if (@in_states) {
	foreach my $tstate (@in_states) {
	    if (!grep {$_ eq $tstate} @ALLTAINTS) {
		fatal("Unknown taint state: $tstate");
	    }
	}
    }

Kirk Webb's avatar
Kirk Webb committed
179 180 181 182
    if (!defined($oid) && $action ne "showall") {
	fatal("You must supply an OID for this operation");
    }

183 184 185 186 187 188
    if (($action eq "set" || $action eq "remove" || $action eq "add") 
	&& !scalar(@in_states)) {
	fatal("You must supply one or more taint states when using the ".
	      "'add', 'remove', or 'set' actions");
    }

Kirk Webb's avatar
Kirk Webb committed
189
    return ($action, $objtype, $oid, @in_states);
190 191 192 193 194 195 196
}

#
# Perform $action on the object of type $objtype identified by $oid.
# @in_states are taint states to be applied / removed (based on $action).
#
sub DoAction($$$@) {
Kirk Webb's avatar
Kirk Webb committed
197
    my ($action, $objtype, $oid, @in_states) = @_;
198 199 200

    # Any objects returned are expected to implement the 'libTaintStates'
    # interface.
Kirk Webb's avatar
Kirk Webb committed
201 202 203 204 205 206
    my $obj;
    if ($action ne "showall") {
	$obj = GetObject($objtype,$oid) or
	    fatal("Could not lookup object with type '$objtype' ".
		  "and identity '$oid'");
    }
207 208

  ACTION: for ($action) {
Kirk Webb's avatar
Kirk Webb committed
209
      /^add$/ && do {
210 211 212 213 214 215 216 217
	  foreach my $tstate (@in_states) {
	      $obj->AddTaintState($tstate) == 0 or
		  fatal("Failed to apply '$tstate' taint to '$oid'");

	  }	  
	  last ACTION;
      };

Kirk Webb's avatar
Kirk Webb committed
218
      /^remove$/ && do {
219 220 221 222 223 224 225 226
	  foreach my $tstate (@in_states) {
	      $obj->RemoveTaintState($tstate) == 0 or
		  fatal("Failed to remove '$tstate' taint from '$oid'");

	  }
	  last ACTION;
      };

Kirk Webb's avatar
Kirk Webb committed
227
      /^set$/ && do {
228 229 230 231 232
	  $obj->SetTaintStates(@in_states) == 0 or
	      fatal("Failed to set taint states for '$oid'");
	  last ACTION;
      };

Kirk Webb's avatar
Kirk Webb committed
233
      /^clear$/ && do {
234 235 236 237 238
	  $obj->SetTaintStates(()) == 0 or
	      fatal("Failed to clear taint states from '$oid'");
	  last ACTION;
      };

Kirk Webb's avatar
Kirk Webb committed
239
      /^show$/ && do {
240 241 242 243 244 245 246 247 248 249
	  my @taint_states = $obj->GetTaintStates();
	  if (@taint_states) {
	      my $pstr = join(", ", @taint_states);
	      print "Taint states on '$oid': $pstr\n";
	  } else {
	      print "No taint states set on '$oid'\n";
	  }
	  last ACTION;
      };

Kirk Webb's avatar
Kirk Webb committed
250 251 252 253 254
      /^showall$/ && do {
	  PrintAllObjects($objtype);
	  last ACTION;
      };

255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
      # Default
      fatal("Unknown action: $action");
  }

    return 1;
}

#
# Return the Emulab DB abstraction object based on the object type
# specified and the lookup id provided.  Returns undef if not found.
#
sub GetObject($$) {
    my ($objtype, $oid) = @_;
    
    my $retobj;

  TYPE: for ($objtype) {
Kirk Webb's avatar
Kirk Webb committed
272
      /^node$/ && do {
273 274 275 276 277
	  require Node;
	  $retobj = Node->Lookup($oid);
	  last TYPE;
      };

Kirk Webb's avatar
Kirk Webb committed
278
      /^os$/ && do {
279
	  require OSImage;
280 281 282
	  my ($ospid, $osname) = split("/", $oid);
	  fatal("OS identifier must be specified as '<project_id>/<os_name>'") 
	      if (!defined($osname));
283
	  $retobj = OSImage->Lookup($ospid, $osname);
284 285
	  last TYPE;
      };
Kirk Webb's avatar
Kirk Webb committed
286 287 288

      # Default
      fatal("Unknown object type: $objtype");
289 290 291 292 293
  }

    return $retobj;
}

Kirk Webb's avatar
Kirk Webb committed
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
#
# Grab all objects of the given type that have taint states.  Return them
# as an array of arrays.  The elements of the contained arrays are the
# object identifier and the list of taint states for the associated object.
#
sub PrintAllObjects($) {
    my ($objtype) = @_;

  TYPE: for ($objtype) {
      /^node$/ && do {
	  my $query_result =
	      DBQueryWarn("select node_id, taint_states from nodes ".
			  "where taint_states is not null ".
			  "and taint_states != ''");

	  fatal("Database lookup error.")
	      if (!$query_result);

	  print "Node_id\tTaint_States\n";
	  print "----------------------------------------------------------\n";

	  while (my ($nodeid, $tstates) = $query_result->fetchrow_array()) {
	      print "$nodeid\t$tstates\n";
	  }

	  last TYPE;
      };

      /^os$/ && do {
	  my $query_result =
Kirk Webb's avatar
Kirk Webb committed
324
	      DBQueryWarn("select pid, osname, taint_states from os_info_versions ".
Kirk Webb's avatar
Kirk Webb committed
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
			  "where taint_states is not null ".
			  "and taint_states != ''");

	  fatal("Database lookup error.")
	      if (!$query_result);

	  print "OS(pid/name)\tTaint_States\n";
	  print "----------------------------------------------------------\n";

	  while (my ($pid, $osname, $tstates) = 
		 $query_result->fetchrow_array()) {
	      print "$pid/$osname\t$tstates\n";
	  }

	  last TYPE;
      };

      # Default
      fatal("Unknown object type: $objtype");
  }

    return;
}

349 350 351 352 353 354 355
sub fatal($)
{
    my ($mesg) = $_[0];

    die("*** $0:\n".
	"    $mesg\n");
}