mkproj.in 12.8 KB
Newer Older
1
#!/usr/bin/perl -wT
Leigh B. Stoller's avatar
Leigh B. Stoller committed
2 3
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2000-2007 University of Utah and the Flux Group.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
5 6
# All rights reserved.
#
7
use English;
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
use Getopt::Std;

#
# Perform project approval. Does a lot of stuff, see below!
#
sub usage()
{
    print(STDERR
	  "Usage: mkproj [-s] [-h leader_uid] [-m <message> | -f <file>] ".
	  "<pid>\n".
	  "switches and arguments:\n".
	  "-s         - silent; do not send approval email to leader\n".
	  "-h <uid>   - switch project leader to specified uid\n".
	  "-m <text>  - Include text in approval email message\n".
	  "-f <file>  - Include text from file in approval email message\n".
	  "<pid>      - project to approve.\n");
    exit(-1);
}
my $optlist    = "qsh:m:f:";
my $quiet      = 0;
my $silent     = 0;
29
my $newleader_uid;
30 31 32 33 34
my $message;
my $mfilename;
my $pid;

# Protos
35 36
sub fatal($);

37 38 39
#
# Configure variables
#
40 41
my $TB            = "@prefix@";
my $TBOPS         = "@TBOPSEMAIL@";
42
my $TBAUDIT	  = "@TBAUDITEMAIL@";
43
my $TBBASE        = "@TBBASE@";
44
my $TBWWW         = "@TBWWW@";
45 46 47 48 49 50
my $MKGROUP       = "$TB/sbin/mkgroup";
my $MODGROUPS     = "$TB/sbin/modgroups";
my $MKACCT        = "$TB/sbin/tbacct add";
my $CVSBIN        = "/usr/bin/cvs";
my $CHOWN         = "/usr/sbin/chown";
my $GRANTTYPE     = "$TB/sbin/grantnodetype -d";
51 52
my $WIKISUPPORT   = @WIKISUPPORT@;
my $BUGDBSUPPORT  = @BUGDBSUPPORT@;
53
my $OPSDBSUPPORT  = @OPSDBSUPPORT@;
54 55
my $CVSSUPPORT    = @CVSSUPPORT@;
my $MAILMANSUPPORT= @MAILMANSUPPORT@;
56 57 58 59
my $ADDWIKIPROJ   = "$TB/sbin/addwikiproj";
my $ADDBUGDBPROJ  = "$TB/sbin/addbugdbproj";
my $ADDMMLIST     = "$TB/sbin/addmmlist";
my $OPSDBCONTROL  = "$TB/sbin/opsdb_control";
60
	  
61
my @DIRLIST  = ("exp", "images", "logs", "deltas", "tarfiles", "rpms",
62
		"groups", "tiplogs", "images/sigs", "templates");
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

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

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

#
# Load the Testbed support stuff. 
#
use lib "@prefix@/lib";
79
use libaudit;
80 81
use libdb;
use libtestbed;
82 83
use User;
use Project;
84

85 86 87 88 89 90 91 92 93 94
my $PROJROOT     = PROJROOT();
my $GRPROOT      = GROUPROOT();
my $SCRATCHROOT  = SCRATCHROOT();

#
# XXX semi-hardwired, oddball paths
#
my $TFTPDIR  = "/tftpboot/$PROJROOT";
my $CVSREPOS = "$PROJROOT/cvsrepos";

95 96
# Locals
my $leader;
97
my $oldleader;
98

99
#
100
# We do not want to run this script unless its the real version.
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
#
if ($EUID != 0) {
    die("*** $0:\n".
	"    Must be setuid! Maybe its a development version?\n");
}

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

#
# Check args.
#
119 120 121 122 123 124
my %options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"q"})) {
    $quiet = 1;
125
}
126 127 128 129 130 131 132 133 134 135 136 137
if (defined($options{"s"})) {
    $silent = 1;
}
if (defined($options{"m"})) {
    $message = $options{"m"};
}
if (defined($options{"f"})) {
    $mfilename = $options{"f"};
    fatal("$mfilename does not exist!")
	if (! -e $mfilename);
}
if (defined($options{"h"})) {
138
    $newleader_uid = $options{"h"};
139 140 141 142 143
}
usage()
    if (! @ARGV);

$pid = $ARGV[0];
144 145 146 147

#
# Untaint the argument.
#
148
if ($pid =~ /^([-\w]+)$/) {
149 150 151 152 153 154
    $pid = $1;
}
else {
    die("Invalid pid '$pid' contains illegal characters.\n");
}

155 156 157 158
# Map invoking user to object.
my $this_user = User->ThisUser();
if (! defined($this_user)) {
    fatal("You ($UID) do not exist!");
159 160 161
}

#
162
# Figure out who called us. Must have admin status to do this.
163
#
164 165
if (!TBAdmin()) {
    fatal("You must be a TB administrator to run this script!");
166 167
}

168 169 170 171 172 173 174 175 176 177
#
# This script is always audited. Mail is sent automatically upon exit.
#
if (AuditStart(0)) {
    #
    # Parent exits normally
    #
    exit(0);
}

178 179 180 181 182 183 184
#
# Map project name to object.
#
my $target_project = Project->Lookup($pid);
if (! defined($target_project)) {
    fatal("Could not map project $pid to its object!");
}
185 186
my $pid_idx = $target_project->pid_idx();
my $gid_idx = $target_project->gid_idx();
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206

#
# The welcome message ...
#
if (defined($mfilename)) {
    open(MFILE, $mfilename) or
	fatal("Could not open $mfilename");

    $message = "";
    while (<MFILE>) {
	$message .= $_;
    }
    close(MFILE);
}

#
# If a leader uid was provided on the command line, we are changing the
# leader. Note that this is allowed *only* for projects that have not
# been approved yet. 
#
207 208
if (defined($newleader_uid)) {
    $leader = User->Lookup($newleader_uid);
209
    if (!defined($leader)) {
210
	fatal("Could not map user $newleader_uid to its object!");
211 212 213 214 215 216 217 218 219 220
    }
    # See if already did this; is so skip the following checks.
    my $curleader = $target_project->GetLeader();
    if (!defined($curleader)) {
	fatal("Could not map current leader of project $pid to its object!");
    }
    if (! $curleader->SameUser($leader)) {
	fatal("Not allowed to change the leader of an approved project!")
	    if ($target_project->approved());

221 222 223
	# Save for email below.
	$oldleader = $curleader;

224 225 226 227
	# Update the project structure with the new leader. We are going
	# to set the approved bit below, so this is the last chance to do
	# this until we have code in place to change it later.
	$target_project->ChangeLeader($leader) == 0 or
228
	    fatal("Could not change leader for $pid to $newleader_uid!");
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
    }
}
else {
    $leader = $target_project->GetLeader();
    if (!defined($leader)) {
	fatal("Could not map current leader of project $pid to its object!");
    }
}
# Avoid taint check problem.
$leader_uid = $leader->uid();

# Approve the project; we are committed to the leader.
$target_project->SetApproved(1) == 0 or
    fatal("Could not set the approval bit on project $target_project!");

#
# Leader needs to have his approved bit set. Eventually this should be done
# in mkaccount when that code moves from the web interface.
#
$leader->SetStatus(USERSTATUS_ACTIVE()) == 0 or
    fatal("Could not change $leader to active!");

251 252 253 254
#
# Before we can proceed, we need to create the project (unix) group
# and then create an account for the project leader. We pass this off
# to sub scripts, but because they are also setuid, we need to flip
255
# our UID (perl sillyness).
256
#
257
$EUID = $UID;
258

259
system("$MKGROUP $gid_idx") == 0 or
260
    fatal("$MKGROUP $pid failed!");
261

262 263 264 265
if ($WIKISUPPORT) {
    system("$ADDWIKIPROJ $pid") == 0 or
	fatal("$ADDWIKIPROJ $pid failed!");
}
266 267 268 269
if ($BUGDBSUPPORT) {
    system("$ADDBUGDBPROJ $pid") == 0 or
	fatal("$ADDBUGDBPROJ $pid failed!");
}
270 271 272 273
if ($OPSDBSUPPORT) {
    system("$OPSDBCONTROL addproj $pid") == 0 or
	fatal("$OPSDBCONTROL addproj $pid failed!");
}
274 275 276 277
if ($MAILMANSUPPORT) {
    system("$ADDMMLIST -a ${pid}-users") == 0 or
	fatal("$ADDMMLIST -a ${pid}-users failed!");
}
278

279 280
system("$MKACCT $leader_uid") == 0 or
    fatal("$MKACCT $leader_uid failed!");
281

282 283
system("$MODGROUPS -a $pid:$pid:project_root $leader_uid") == 0 or
    fatal("$MODGROUPS -a $pid:$pid:project_root $leader_uid failed!");
284

285
$EUID = 0;
286 287 288 289

#
# This acts as check (and we need the numeric uid) in case mkacct failed!
# 
290 291
my (undef,undef,$uid) = getpwnam($leader_uid)
    or fatal("$leader_uid not in passwd file");
292 293 294 295 296 297 298

my (undef,undef,$gid) = getgrnam($pid)
    or fatal("$pid not in group file");

#
# Okay, do it.
#
299 300 301 302
if (! -e "$PROJROOT/$pid") {
    if (! mkdir("$PROJROOT/$pid", 0770)) {
	fatal("Could not make directory $PROJROOT/$pid: $!");
    }
303

304 305 306
    if (! chmod(0770, "$PROJROOT/$pid")) {
	fatal("Could not chmod directory $PROJROOT/$pid: $!");
    }
307

308 309 310
    if (! chown($uid, $gid, "$PROJROOT/$pid")) {
	fatal("Could not chown $PROJROOT/$pid to $uid/$gid: $!");
    }
311
}
312 313 314 315 316 317 318 319 320 321 322 323 324
if ($SCRATCHROOT && ! -e "$SCRATCHROOT/$pid") {
    if (! mkdir("$SCRATCHROOT/$pid", 0770)) {
	fatal("Could not make directory $SCRATCHROOT/$pid: $!");
    }

    if (! chmod(0770, "$SCRATCHROOT/$pid")) {
	fatal("Could not chmod directory $SCRATCHROOT/$pid: $!");
    }

    if (! chown($uid, $gid, "$SCRATCHROOT/$pid")) {
	fatal("Could not chown $SCRATCHROOT/$pid to $uid/$gid: $!");
    }
}
325 326 327 328 329

#
# Make project subdirs.
#
foreach my $dir (@DIRLIST) {
330 331 332 333 334 335 336 337 338 339
    if (! -e "$PROJROOT/$pid/$dir") {
	if (! mkdir("$PROJROOT/$pid/$dir", 0770)) {
	    fatal("Could not make directory $PROJROOT/$pid/$dir: $!");
	}
	if (! chmod(0770, "$PROJROOT/$pid/$dir")) {
	    fatal("Could not chmod directory $PROJROOT/$pid/$dir: $!");
	}
	if (! chown($uid, $gid, "$PROJROOT/$pid/$dir")) {
	    fatal("Could not chown $PROJROOT/$pid/$dir to $uid/$gid: $!");
	}
340 341 342 343 344
    }
}

#
# Create a tftp directory for oskit kernels.
345
#
346 347 348
if (! -e "$TFTPDIR/$pid") {
    if (! mkdir("$TFTPDIR/$pid", 0770)) {
	fatal("Could not make directory $TFTPDIR/$pid: $!");
349
    }
350 351
    if (! chmod(0777, "$TFTPDIR/$pid")) {
	fatal("Could not chmod directory $TFTPDIR/$pid: $!");
352
    }
353 354
    if (! chown($uid, $gid, "$TFTPDIR/$pid")) {
	fatal("Could not chown $TFTPDIR/$pid to $uid/$gid: $!");
355
    }
356 357
}

358 359 360
#
# Do the CVS stuff if its turned on.
#
361 362
if ($CVSSUPPORT) {
    my $CVSDIR = "$CVSREPOS/$pid";
363

364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
    if (! -e "$CVSDIR") {
	if (! mkdir("$CVSDIR", 0770)) {
	    fatal("Could not make directory $CVSDIR: $!");
	}
    }
    if (! chmod(0770, "$CVSDIR")) {
	fatal("Could not chmod directory $CVSDIR: $!");
    }
    if (! chown($uid, $gid, "$CVSDIR")) {
	fatal("Could not chown $CVSDIR to $uid/$gid: $!");
    }
    if (! -e "$CVSDIR/CVSROOT") {
	system("$CVSBIN -d $CVSDIR init");
	if ($?) {
	    fatal("Could not cvs init $CVSDIR!");
	}
380 381 382 383 384 385 386 387
    }
    # Chown the tree.
    system("$CHOWN -R ${uid}:${gid} $CVSDIR");
    if ($?) {
	fatal("Could not chown ${uid}:${gid} $CVSDIR!");
    }
}

388 389
#
# Create groups directory.
390 391 392 393 394
#
if (! -e "$GRPROOT/$pid") {
    if (! mkdir("$GRPROOT/$pid", 0770)) {
	fatal("Could not make directory $GRPROOT/$pid: $!");
    }
395
    if (! chmod(0770, "$GRPROOT/$pid")) {
396 397 398 399 400
	fatal("Could not chmod directory $GRPROOT/$pid: $!");
    }
    if (! chown($uid, $gid, "$GRPROOT/$pid")) {
	fatal("Could not chown $GRPROOT/$pid to $uid/$gid: $!");
    }
401 402 403 404 405 406 407

    # Create a group link for the default group.
    if (! -e "$GRPROOT/$pid/$pid") {    
	if (system("ln -s $PROJROOT/$pid $GRPROOT/$pid/$pid")) {
	    fatal("Could not symlink $PROJROOT/$pid to $GRPROOT/$pid/$pid");
	}
    }
408 409
}

410 411 412
#
# Create experiment working directory.
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
413
my $workdir = TBDB_EXPT_WORKDIR() . "/$pid";
414

415
if (! -e $workdir) {
416
    if (! mkdir("$workdir", 0775)) {
417 418
	fatal("Could not make directory $workdir: $!");
    }
419
    if (! chmod(0775, "$workdir")) {
420 421 422 423 424
	fatal("Could not chmod directory $workdir: $!");
    }
    if (! chown($uid, $gid, "$workdir")) {
	fatal("Could not chown $workdir to $uid/$gid: $!");
    }
425 426
}

427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
#
# If approved to use remote nodes, then grant permission to use the
# specific types of virtual nodes on those remote physical nodes.
# Unfortunately, the node_types table does not store a relationship
# between the phys type and the virtual types that are hosted on them.
# Need to add that I guess, but in the meantime we have just 3 remote
# phys types to worry about. 
#
my $query_result =
    DBQueryFatal("select pcremote_ok from projects where pid='$pid'");
if ($query_result->num_rows) {
    my ($pcremote) = $query_result->fetchrow_array();

    if (defined($pcremote)) {
	print "$pcremote\n";
	
	foreach my $type (split(",", $pcremote)) {
	    print "$type\n";
	    
	    if ($type eq "pcplabphys") {
		$type = "pcplab";
	    }
	    elsif ($type eq "pcron") {
		$type = "pcvwa";
	    }
	    elsif ($type eq "pcwa") {
		$type = "pcvwa";
	    }
	    else {
		fatal("Unknown remote type $type!");
	    }
	    print "$type\n";
459 460 461

	    $EUID = $UID;

462 463
	    system("$GRANTTYPE -p $pid $type") == 0 or
		fatal("Could not grant permission to use type $type!");
464 465

	    $EUID = 0;
466 467 468 469
	}
    }
}

470 471 472 473 474
# Send email, unless silent option given.
if (!$silent) {
    my $leader_name  = $leader->name();
    my $leader_email = $leader->email();
    
475 476 477 478 479 480 481 482 483 484 485 486 487 488
    SendProjAdminMail
	($pid, "ADMIN", "$leader_name <$leader_email>",
	 "Project '$pid' Approval",
	 "\n".
	 "This message is to notify you that your project '$pid'\n".
	 "has been approved.  We recommend that you save this link so that\n".
	 "you can send it to people you wish to have join your project.\n".
	 "Otherwise, tell them to go to ${TBBASE} and join it.\n".
	 "\n".
	 "    ${TBBASE}/joinproject.php3?target_pid=$pid\n".
	 (defined($message) ? "\n${message}\n" : "") .
	 "\n".
	 "Thanks,\n".
	 "Testbed Operations\n");
489 490 491 492 493 494 495 496 497

    #
    # If the leader was switched, then generate a second message to the
    # new leader telling him to approve the original leader to the project.
    #
    if (defined($oldleader)) {
	my $oldleader_uid  = $oldleader->uid();
	my $oldleader_name = $oldleader->name();
	
498 499 500 501 502 503 504 505 506 507 508
	SendProjAdminMail
	    ($pid, "ADMIN", "$leader_name <$leader_email>",
	     "$oldleader_uid $pid Project Join Request",
	     "$oldleader_name wants to join project $pid.\n".
	     "\n".
	     "Please return to $TBWWW,\n".
	     "log in, select the 'New User Approval' page, and enter\n".
	     "your decision regarding ${oldleader_name}'s membership.\n".
	     "\n".
	     "Thanks,\n".
	     "Testbed Operations\n");
509
    }
510 511
}

512
print "Project Creation Completed!\n";
513 514
exit(0);

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

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