newproj.in 8.12 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
#!/usr/bin/perl -wT
#
# EMULAB-COPYRIGHT
# Copyright (c) 2000-2007 University of Utah and the Flux Group.
# All rights reserved.
#
use English;
use strict;
use Getopt::Std;
use XML::Simple;
use Data::Dumper;

#
# Create a new user from a XML description. 
#
sub usage()
{
    print("Usage: newproj <xmlfile>\n");
    print("       newproj -m <pid_idx>\n");
    exit(-1);
}
my $optlist = "dm:";
23
my $debug   = 0;
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
my $resend;

#
# Configure variables
#
my $TB		= "@prefix@";
my $TBOPS       = "@TBOPSEMAIL@";
my $TBAPPROVAL  = "@TBAPPROVALEMAIL@";
my $TBAUDIT	= "@TBAUDITEMAIL@";
my $TBBASE      = "@TBBASE@";
my $TBWWW       = "@TBWWW@";

#
# This script is setuid, so please do not run it as root. Hard to track
# what has happened.
#
if ($EUID == 0) {
    die("*** $0:\n".
	"    Please do not run this as root!\n");
}

#
# Untaint the path
#
$ENV{'PATH'} = "$TB/bin:$TB/sbin:/bin:/usr/bin:/usr/bin:/usr/sbin";
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

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

#
# Load the Testbed support stuff.
#
use lib "@prefix@/lib";
use libdb;
use libtestbed;
use Project;
use User;

# Protos
sub fatal($);
sub UserError($);

# Locals
my $SAVEUID	= $UID;
my $xmlfile;

#
# 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{"d"})) {
    $debug = 1;
}
if (defined($options{"m"})) {
    $resend = $options{"m"};

    if ($resend =~ /^(\d*)$/) {
	$resend = $1;
    }
    else {
	fatal("Bad characters in -m option: $resend");
    }
}
else {
    usage()
	if (@ARGV != 1);

    $xmlfile  = shift(@ARGV);
}

#
# Map invoking user to object. 
# If invoked as "nobody" we are coming from the web interface and there
# is no current user context.
#
my $this_user;

if (getpwuid($UID) ne "nobody") {
    $this_user = User->ThisUser();

    if (! defined($this_user)) {
	fatal("You ($UID) do not exist!");
    }
    fatal("You must have admin privledges to create new projects!")
	if (!TBAdmin());
}

#
# Resend email message and exit.
#
if (defined($resend)) {
    my $project = Project->Lookup($resend);
    fatal("Could not map project $resend to object!")
	if (!defined($project));

    exit($project->SendNewProjectEmail());
}

#
# Check the filename when invoked from the web interface; must be a
# file in /tmp.
#
if (! defined($this_user)) {
    if ($xmlfile =~ /^([-\w\.\/]+)$/) {
	$xmlfile = $1;
    }
    else {
	fatal("Bad data in pathname: $xmlfile");
    }

    # Use realpath to resolve any symlinks.
    my $translated = `realpath $xmlfile`;
    if ($translated =~ /^(\/tmp\/[-\w\.\/]+)$/) {
	$xmlfile = $1;
    }
    else {
	fatal("Bad data in translated pathname: $xmlfile");
    }
}

#
# These are the fields that we allow to come in from the XMLfile.
#
my %required = ("name"		    => "pid",
		"short description" => "name",
		"URL"		    => "URL",
		"funders"	    => "funders",
		"long description"  => "why",
		"public"	    => "public",
		"num_pcs"	    => "num_pcs",
		"linkedtous"	    => "linked_to_us");
		
163 164 165
my %optional = ("newuser_xml"    => "newuser_xml",
		"leader"	 => "head_uid",	# May be chosen by newuser.
		"members"	 => "num_members",
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
		"ron"            => "num_ron",
		"plab"           => "num_pcplab",
		"whynotpublic"	 => "public_whynot",
		"user_interface" => "default_user_interface");

#
# This script is not audited cause we want the output to be sent back
# to the web interface. Thats okay since we send email from the script
# anyway.
#

#
# Must wrap the parser in eval since it exits on error.
#
my $xmlparse = eval { XMLin($xmlfile,
			    VarAttr => 'name',
			    ContentKey => '-content',
			    SuppressEmpty => undef); };
fatal($@)
    if ($@);


#
# Make sure all the required arguments were provided.
#
foreach my $key (keys(%required)) {
    
    fatal("Missing required attribute '$key'")
	if (! exists($xmlparse->{'attribute'}->{"$key"}));
}

#
198
# We build up an array of arguments to pass to Project->Create() as we check
199 200 201 202 203 204
# the attributes.
#
my %newproj_args = ();

foreach my $key (keys(%{ $xmlparse->{'attribute'} })) {
    my $value = $xmlparse->{'attribute'}->{"$key"}->{'value'};
205 206 207
    if (!defined($value)) {	# Empty string comes from XML as an undef value.
	$xmlparse->{'attribute'}->{"$key"}->{'value'} = $value = "";
    }
208 209

    if ($debug) {
210 211 212
	my $prval = (defined($value) ? $value : "NULL");
		     
	print STDERR "Project attribute: '$key' -> '$prval'\n";
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
    }

    my $dbslot;

    # Must be in the allowed lists above, with exceptions handled below
    if (exists($required{$key})) {
	$dbslot = $required{$key};
	next
	    if (!defined($dbslot));
	fatal("Null value for required field $key")
	    if (!defined($value));
    }
    elsif (exists($optional{$key})) {
	$dbslot = $optional{$key};
	next
	    if (!defined($dbslot) || !defined($value));
    }
    else {
	fatal("Invalid attribute in XML: '$key' -> '$value'\n");
    }

    # Now check that the value is legal.
    if (! TBcheck_dbslot($value, "projects", $dbslot,
			 TBDB_CHECKDBSLOT_WARN|TBDB_CHECKDBSLOT_ERROR)) {
	UserError("Illegal data: $key - $value");
    }

    #
    # Do a taint check to avoid warnings, since the values are abviously okay.
    #
    if ($value =~ /^(.*)$/) {
	$value = $1;
    }
    $newproj_args{$dbslot} = $value;
}

249 250 251
#
# Create the user if an XML file for newuser was supplied.
#
252
my $newuser_xml;
253
if (exists($newproj_args{'newuser_xml'})) {
254
    $newuser_xml = $newproj_args{'newuser_xml'};
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
    delete($newproj_args{'newuser_xml'});

    #
    # Check the filename when invoked from the web interface; must be a
    # file in /tmp.
    #
    if (! defined($this_user)) {
	if ($newuser_xml =~ /^([-\w\.\/]+)$/) {
	    $newuser_xml = $1;
	}
	else {
	    fatal("Bad data in pathname: $newuser_xml");
	}

	# Use realpath to resolve any symlinks.
	my $translated = `realpath $newuser_xml`;
	if ($translated =~ /^(\/tmp\/[-\w\.\/]+)$/) {
	    $newuser_xml = $1;
	}
	else {
	    fatal("Bad data in translated pathname: $newuser_xml");
	}
    }

    #
    # We can't pass the user type to the newuser script in xml, because it
    # determines which args are required or optional in the xml data!  The
    # user type is always "project leader" for new users under newproj.
    #
    my $cmd = "newuser -t leader $newuser_xml";
    print $cmd . "\n"
	if ($debug);
    my $cmd_out = `$cmd`;
    UserError("Transient Error: (3, $?, $cmd) $cmd_out")
	if ($?);

    #
    # Parse the last line of output. Ick.
    #
    my @out_lines = split(/^/, $cmd_out);
    if (!( $out_lines[@out_lines-1] =~ /^User\s+(\w+)\/(\d+)\s+/)) {
	UserError("Transient error: (4, $cmd) $cmd_out");
    }
    $newproj_args{'head_uid'} = $2;
}

301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
#
# Now do special checks.
#
UserError("Project already exists; pick another name!")
    if (Project->Lookup($newproj_args{'pid'}));

my $leader = User->Lookup($newproj_args{'head_uid'});
UserError("Project leader does not exist!")
    if (!defined($leader));
   
#
# Now safe to create the project. Move the pid out of the argument array
# since its actually an argument to the Create() routine. Ditto for the
# project leader.
#
my $new_pid = $newproj_args{'pid'};
delete($newproj_args{'pid'});
delete($newproj_args{'head_uid'});

my $newproj = Project->Create($new_pid, $leader, \%newproj_args);
if (!defined($newproj)) {
    fatal("Could not create new project!");
}
my $new_idx = $newproj->pid_idx();

#
# See if we are in an initial Emulab setup. If so, no email sent.
#
my $firstinitstate;
TBGetSiteVar("general/firstinit/state", \$firstinitstate);

#
# Send the email notification.
#
$newproj->SendNewProjectEmail($firstinitstate eq "createproject");

337 338 339 340 341
# Unlink this here, so that the newuser file is left behind in case of error.
# We can then create the user by hand from the xmlfile, if desired.
unlink($newuser_xml)
    if (defined($newuser_xml));

342
# The web interface requires this line to be printed!
343
print "Project $new_pid/$new_idx has been created\n";
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
exit(0);

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

    print STDERR "*** $0:\n".
	         "    $mesg\n";
    exit(-1);
}
sub UserError($) {
    my($mesg) = $_[0];

    print $mesg;
    exit(1);
}

sub escapeshellarg($)
{
    my ($str) = @_;

    $str =~ s/(')/'\\''/g;
    return $str;
}