grantnodetype.in 4.51 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-2010 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
# 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()
{
20
    print STDERR "Usage: grantnodetype [-h] [-r] -p <pid> <type>\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
21
    print STDERR "	-h   This message\n";
22
    print STDERR "	-r   Revoke access instead of grant\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
23 24
    exit(-1);
}
25
my $optlist  = "hp:dnr";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
26 27
my $impotent = 0;
my $debug    = 0;
28
my $revoke   = 0;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
29 30 31 32 33 34 35 36
my %newtypes = ();
my $pid;

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

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

#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use libdb;
use libtestbed;
52 53
use Project;
use Group;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
54

55 56 57
# Be careful not to exit on transient error
$libdb::DBQUERY_MAXTRIES = 30;

Leigh B. Stoller's avatar
Leigh B. Stoller committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
#
# 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;
}
82 83 84
if (defined($options{r})) {
    $revoke = 1;
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
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
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");
}

119 120 121 122 123 124 125 126
my $project = Project->Lookup($pid);
if (!defined($project)) {
    die("*** $0:\n".
	"    Cannot map project $pid to its object\n");
}
# Need this below.
my $pid_idx = $project->pid_idx();

Leigh B. Stoller's avatar
Leigh B. Stoller committed
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
#
# 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 ".
151
			 "where type='$nodetype' or type='$nodeclass'");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170

	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)) {
171 172 173
    print STDERR
	($revoke ? "Revoking" : "Granting") . " " .
	"permission to use type $newtype\n"
Leigh B. Stoller's avatar
Leigh B. Stoller committed
174
	if ($debug);
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195

    #
    # 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. 
196 197 198
    #
    my $count = ($revoke ? 0 : 999999);
    
199
    DBQueryFatal("replace into group_policies ".
200 201
		 "(pid_idx, gid_idx, pid, gid, policy, auxdata, count) ".
		 "values ($pid_idx, $pid_idx, '$pid', '$pid', ".
202
		 "        'type', '$newtype', $count)")
Leigh B. Stoller's avatar
Leigh B. Stoller committed
203 204 205
	if (!$impotent);
}

206 207 208 209 210 211
#
# Now update the permissions table. 
#
system($update_perms);
exit($? >> 8)
    if ($?);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
212
exit(0);