sharevlan.in 4.03 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#!/usr/bin/perl -w
#
# EMULAB-COPYRIGHT
# Copyright (c) 2003-2012 University of Utah and the Flux Group.
# All rights reserved.
#
use strict;
use English;
use Getopt::Std;

#
# Grant and revoke shared vlan access.
#
sub usage()
{
    print STDERR "Usage: sharevlan [-o] eid lanname token\n";
17
    print STDERR "       sharevlan -r token\n";
18 19
    print STDERR " -r    Revoke sharing instead\n";
    print STDERR " -o    Sharing is open to everyone.\n";
20
    print STDERR " -l    List all shared vlans\n";
21 22
    exit(-1);
}
23
my $optlist  = "hrol";
24 25
my $revoke   = 0;
my $open     = 0;
26
my $list     = 0;
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

# 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 libtestbed;
use Experiment;
use EmulabConstants;
use Lan;
use User;

#
# 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{'o'})) {
    $open = 1;
}
79 80 81 82
if (defined($options{'l'})) {
    $list = 1;
}
elsif (defined($options{'r'})) {
83 84 85
    $revoke = 1;

    usage()
86
	if (@ARGV != 1);
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
}
else {
    usage()
	if (@ARGV != 3);
}

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

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
if ($list) {
    my $query_result =
	DBQueryFatal("select * from shared_vlans order by token");
    exit(0)
	if (! $query_result->numrows);

    printf("%-15s %-15s %-15s %-15s %s\n",
	   "PID", "EID", "Token", "Lan", "Who");
    print "-------------------------------------------------------------\n";
	
    while (my $row = $query_result->fetchrow_hashref()) {
	my $pid      = $row->{'pid'};
	my $eid      = $row->{'eid'};
	my $token    = $row->{'token'};
	my $lan      = $row->{'vname'};
	my $created  = $row->{'created'};
	my $creator  = $row->{'creator'};

	printf("%-15s %-15s %-15s %-15s %s\n",
	       $pid, $eid, $token, $lan, $creator);
    }
    exit(0);
}

if ($revoke) {
    my $token = $ARGV[0];
    
    DBQueryFatal("delete from shared_vlans where token='$token'");
    exit(0);
}

my $eid     = $ARGV[0];
my $lanname = $ARGV[1];
my $token   = $ARGV[2];

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
my $experiment = Experiment->Lookup($eid);
if (!defined($experiment)) {
    fatal("No such experiment $eid");
}
if ($experiment->state() ne EXPTSTATE_ACTIVE()) {
    fatal("Experiment is not active");
}
my $vlan = VLan->Lookup($experiment, $lanname);
if (!defined($vlan)) {
    fatal("No such lan in $experiment");
}
my $lanid = $vlan->lanid();

my $query_result =
    DBQueryFatal("select * from shared_vlans where lanid='$lanid'");
if ($query_result->numrows) {
    fatal("$lanname is already shared");
}
if (! ($token =~ /^[-\w]*$/)) {
    fatal("Illegal characters in token"); 
}
$query_result =
    DBQueryFatal("select * from shared_vlans where token='token'");
if ($query_result->numrows) {
    fatal("$token is already used in the shared vlans table");
}
my $pid = $experiment->pid();
$eid    = $experiment->eid();
my $idx = $experiment->idx();
my $uid = $this_user->uid();
my $uid_idx = $this_user->uid_idx();

DBQueryFatal("insert into shared_vlans set open='$open', ".
	     "  pid='$pid', eid='$eid', exptidx='$idx', ".
	     "  token='$token', lanid='$lanid', vname='$lanname', ".
	     "  created=now(), creator='$uid', creator_idx='$uid_idx'");
175 176 177 178 179 180 181 182 183

#
# Since that worked, clear the switch path. Whatever the mapper
# computed will be wrong as soon as we start adding ports. By
# clearing the path, we are telling snmpit to compute a new path
# as ports are added or removed from the vlan.
#
$vlan->ClrSwitchPath();

184 185 186 187 188 189 190 191 192 193
exit(0);

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

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