grantnodetype.in 4.04 KB
Newer Older
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1 2 3 4
#!/usr/bin/perl -wT

#
# EMULAB-COPYRIGHT
5
# Copyright (c) 2003, 2005 University of Utah and the Flux Group.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
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
# All rights reserved.
#

use English;
use Getopt::Std;

#
# Grant permission to use types/class of nodes. If given a specific
# "type", then grant permission to use just that type. If its a class, then
# permission to use all of the types in that class, the class itself, and any
# aux nodetypes for the type/class (node_types_auxtypes table). 
#
sub usage()
{
    print STDERR "Usage: grantnodetype [-h] -p <pid> <type>\n";
    print STDERR "	-h   This message\n";
    exit(-1);
}
my $optlist  = "hp:dn";
my $impotent = 0;
my $debug    = 0;
my %newtypes = ();
my $pid;

#
# Please do not run as root. Hard to track what has happened.
#
if ($UID == 0) {
    die("*** $0:\n".
35
	"    Please do not run this as root!\n");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
36 37 38 39 40
}

#
# Configure variables
#
41 42
my $TB		 = "@prefix@";
my $update_perms = "$TB/sbin/update_permissions";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
43 44 45 46 47 48 49 50

#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use libdb;
use libtestbed;

51 52 53
# Be careful not to exit on transient error
$libdb::DBQUERY_MAXTRIES = 30;

Leigh B. Stoller's avatar
Leigh B. Stoller committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 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 133 134 135
#
# Turn off line buffering on output
#
$| = 1;

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

#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
%options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{h})) {
    usage();
}
if (defined($options{n})) {
    $impotent = 1;
}
if (defined($options{d})) {
    $debug = 1;
}
if (defined($options{p})) {
    $pid = $options{p};
}
if (scalar(@ARGV) != 1 || !defined($pid)) {
    usage();
}
my $type = $ARGV[0];

#
# Untaint the arguments.
#
if ($type =~ /^([-\w]+)$/) {
    $type = $1;
}
else {
    die("Tainted type name: $type");
}

if ($pid =~ /^([-\w]+)$/) {
    $pid = $1;
}
else {
    die("Tainted pid name: $pid");
}

# Only admin types can do this.
if (!TBAdmin($UID)) {
    die("*** $0:\n".
	"    You are not allowed to grant permission to use node types!\n");
}

#
# Check to make sure a valid type in the node_types table.
#
my $query_result =
    DBQueryFatal("select distinct type,class from node_types ".
		 "where type='$type' or class='$type'");

if (! $query_result->num_rows) {
    die("*** $0:\n".
	"    No such type/class $type in the types table!\n");
}

while (my ($nodetype,$nodeclass) = $query_result->fetchrow_array()) {
    if ($nodeclass eq $type) {
	#
	# Grant permission to this class/type, and all of the aux types for it.
	#
	$newtypes{$nodetype} = 1
	    if (!defined($newtypes{$nodetype}));
	$newtypes{$nodeclass} = 1
	    if ($nodeclass ne $nodetype && !defined($newtypes{$nodeclass}));

	my $aux_result =
	    DBQueryFatal("select auxtype from node_types_auxtypes ".
136
			 "where type='$nodetype' or type='$nodeclass'");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157

	while (my ($auxtype) = $aux_result->fetchrow_array()) {
	    $newtypes{$auxtype} = 1
		if (!defined($newtypes{$auxtype}));
	}
    }
    else {
	#
	# A single type. Just grant permission to the type itself.
	#
	$newtypes{$type} = 1
	    if (!defined($newtypes{$type}));
    }
}

#
# Run the queries.
# 
foreach my $newtype (keys(%newtypes)) {
    print STDERR "Granting permission to use type $newtype\n"
	if ($debug);
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182

    #
    # Make sure there is a minus policy in the group_policies table;
    # If not, then granting permission to use this type is silly, and
    # probably means that the group_policies table needs updating.
    #
    $query_result =
	DBQueryFatal("select * from group_policies ".
		     "where pid='-' and policy='type' and auxdata='$newtype'");
    
    exit(0)
	if (!$query_result);

    if (!$query_result->num_rows) {
	die("*** $0:\n".
	    "    There is no '-' policy for node type $newtype! Stopping.\n");
    }

    #
    # Add generic rules that say the project is allowed to use "infinite"
    # number of nodes of each type. 
    # 
    DBQueryFatal("replace into group_policies ".
		 "(pid, gid, policy, auxdata, count) ".
		 "values ('$pid', '$pid', 'type', '$newtype', 999999)")
Leigh B. Stoller's avatar
Leigh B. Stoller committed
183 184 185
	if (!$impotent);
}

186 187 188 189 190 191
#
# Now update the permissions table. 
#
system($update_perms);
exit($? >> 8)
    if ($?);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
192
exit(0);