grantlease.in 5.75 KB
Newer Older
1 2 3 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 43 44 45 46 47 48 49 50 51 52 53 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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
#!/usr/bin/perl -w
#
# Copyright (c) 2003-2015 University of Utah and the Flux Group.
# 
# {{{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/>.
# 
# }}}
#
use strict;
use English;
use Getopt::Std;

#
# Grant and revoke permission to use specific leases (datasets).
#
sub usage()
{
    print STDERR "Usage: grantlease [-r] [-w] ";
    print STDERR "[-g <gid> | -u <uid> | -a] <lease>\n";
    print STDERR "       grantlease -l <lease>\n";
    print STDERR "       grantlease [-R acl | -W acl] <lease>\n";
    print STDERR "  -h   This message\n";
    print STDERR "  -l   List permissions\n";
    print STDERR "  -w   Grant write permission; defaults to read only\n";
    print STDERR "  -r   Revoke access instead of grant\n";
    print STDERR "  -u   Grant access to a specific user\n";
    print STDERR "  -g   Grant access to a specific group (project)\n";
    print STDERR "  -a   Grant global read-only access\n";
    print STDERR "Alternate form:\n";
    print STDERR "  -R acl  Grant project|global read-only access\n";
    print STDERR "  -W acl  Grant creator|project write access\n";
    exit(-1);
}
my $optlist  = "hg:dru:wlaR:W:";
my $debug    = 0;
my $revoke   = 0;
my $writable = 0;
my $listonly = 0;
my $global   = 0;
my $gid;
my $uid;
my $target;
my $read_access;
my $write_access;

# Protos
sub fatal($);

#
# 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");
}

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

#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use emdb;
use EmulabConstants;
use libtestbed;
use Experiment;
use Project;
use Group;
use User;
use Lease;

#
# 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.
#
my %options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{h})) {
    usage();
}
if (defined($options{l})) {
    $listonly = 1;
}
if (defined($options{a})) {
    $global = 1;
}
if (defined($options{r})) {
    $revoke = 1;
}
if (defined($options{d})) {
    $debug = 1;
}
if (defined($options{w})) {
    $writable = 1;
}
if (defined($options{g})) {
    $gid = $options{g};
}
if (defined($options{u})) {
    $uid = $options{u};
}
if (defined($options{"R"})) {
    $read_access = $options{"R"};
    usage()
	if ($read_access !~ /^(global|project)$/);
}
if (defined($options{"W"})) {
    $write_access = $options{"W"};
    usage()
	if ($write_access !~ /^(creator|project)$/);
}
usage()
    if (@ARGV != 1);
usage()
    if (! ($listonly || $global || defined($gid) || defined($uid) ||
	   defined($read_access) || defined($write_access)));

#
# Verify user.
#
my $this_user = User->ThisUser();
if (! defined($this_user)) {
    fatal("You ($UID) do not exist!");
}

if (defined($gid)) {
    $target = Group->Lookup($gid);
    if (!defined($target)) {
	fatal("No such project or group $gid\n");
    }
}
elsif (defined($uid)) {
    $target = User->Lookup($uid);
    if (!defined($target)) {
	fatal("No such user $uid\n");
    }
}
my $lease = Lease->Lookup($ARGV[0]);
if (!defined($lease)) {
    fatal("No such lease exists");
}
my $lease_id  = $lease->lease_id();
my $lease_idx = $lease->lease_idx();
    
# User must have permission.
if (! $lease->AccessCheck($this_user, LEASE_ACCESS_MODIFY())) {
    fatal("You do not have permission to change the permissions");
}

if ($listonly) {
    my $query_result =
	DBQueryFatal("select * from lease_permissions ".
		     "where lease_idx='$lease_idx'");

    while (my $row = $query_result->fetchrow_hashref()) {
	my $perm_type = $row->{'permission_type'};
	my $perm_id   = $row->{'permission_id'};
	my $perm_idx  = $row->{'permission_idx'};
	my $write     = $row->{'allow_write'};

	print "$perm_type: $perm_id ($perm_idx) ";
	print "writable" if ($write);
	print "\n";
    }
    exit(0);
}
elsif ($global) {
    if ($revoke) {
	$lease->RevokeAccess(GLOBAL_PERM_ANON_RO) == 0
	    or fatal("Could not grant global permission to $target");
    }
    else {
	$lease->GrantAccess(GLOBAL_PERM_ANON_RO(), 0) == 0
	    or fatal("Could not revoke global permission from $target");
    }
}
elsif ($revoke) {
    $lease->RevokeAccess($target) == 0
	or fatal("Could not revoke permission for $target");

    print "Revoking ALL access to lease from $target\n";
}
elsif (defined($read_access) || defined($write_access)) {
    my $project = $lease->GetProject();
    
    if (defined($read_access)) {
	if ($read_access eq "project") {
	    $lease->RevokeAccess(GLOBAL_PERM_ANON_RO);
	    $lease->GrantAccess($project, 0);
	}
	else {
	    $lease->GrantAccess(GLOBAL_PERM_ANON_RO(), 0);
	}
    }
    if (defined($write_access)) {
	if ($write_access eq "creator") {
	    $lease->RevokeAccess($project);
	    $lease->GrantAccess($project, 0);
	}
	else {
	    $lease->RevokeAccess($project);
	    $lease->GrantAccess($project, 1);
	}
    }
}
else {
    $lease->GrantAccess($target, $writable) == 0
	or fatal("Could not grant permission for $target");
}
exit(0);

sub fatal($)
{
    my ($mesg) = $_[0];

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