All new accounts created on Gitlab now require administrator approval. If you invite any collaborators, please let Flux staff know so they can approve the accounts.

mkproj.in 6.48 KB
Newer Older
1
#!/usr/bin/perl -wT
Leigh B. Stoller's avatar
Leigh B. Stoller committed
2 3 4

#
# EMULAB-COPYRIGHT
5
# Copyright (c) 2000-2003, 2005 University of Utah and the Flux Group.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
6 7 8
# All rights reserved.
#

9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
use English;

#
# Make a project directory hierarchy. Must be called as tbroot.
# Creates a directory rooted /proj/pid. The directory is setuid
# to the project leader, and setgid to the project gid. We get
# this info from the database.
#
# usage: mkproj <pid>
#

#
# Configure variables
#
my $TB       = "@prefix@";
my $TBOPS    = "@TBOPSEMAIL@";
my $MKGROUP  = "$TB/sbin/mkgroup";
26
my $SETGROUPS= "$TB/sbin/setgroups";
27
my $MKACCT   = "$TB/sbin/tbacct add";
28
my $GRANTTYPE= "$TB/sbin/grantnodetype -d";
29 30
my $WIKISUPPORT = @WIKISUPPORT@;
my $ADDWIKIPROJ = "$TB/sbin/addwikiproj";
31 32

my $PROJROOT = "/proj";
33
my $GRPROOT  = "/groups";
34 35 36
my $TFTPROOT = "/tftpboot";
my @DIRLIST  = ("exp", "images", "logs", "deltas", "tarfiles", "rpms",
		"groups", "tiplogs");
37
my $projhead;
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

#
# 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";
54
use libaudit;
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
use libdb;
use libtestbed;

#
# We don't want to run this script unless its the real version.
#
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.
#
if ($#ARGV < 0) {
    die("Usage: mkprojdir <pid>\n");
}
my $pid = $ARGV[0];

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

#
# Figure out who called us. Only with admin status in the DB can run
# this script.
#
if (!TBAdmin($UID)) {
    die("*** $0:\n".
	"    You must be a TB administrator to run this script!\n");
}

#
# We need the project leader name.
#
if (! ($projhead = ProjLeader($pid))) {
    die("*** $0:\n".
	"    Could not get project leader for project $pid!\n");
}

110 111 112 113 114 115 116 117 118 119
#
# This script is always audited. Mail is sent automatically upon exit.
#
if (AuditStart(0)) {
    #
    # Parent exits normally
    #
    exit(0);
}

120 121 122 123
#
# 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
124
# our UID (perl sillyness).
125
#
126
$EUID = $UID;
127

128 129
system("$MKGROUP $pid $pid") == 0 or
    fatal("$MKGROUP $pid failed!");
130

131 132 133 134 135
if ($WIKISUPPORT) {
    system("$ADDWIKIPROJ $pid") == 0 or
	fatal("$ADDWIKIPROJ $pid failed!");
}

136 137 138
system("$MKACCT $projhead") == 0 or
    fatal("$MKACCT $projhead failed!");

139 140 141
system("$SETGROUPS $projhead") == 0 or
    fatal("$SETGROUPS $projhead failed!");

142
$EUID = 0;
143 144 145 146 147 148 149 150 151 152 153 154 155

#
# This acts as check (and we need the numeric uid) in case mkacct failed!
# 
my (undef,undef,$uid) = getpwnam($projhead)
    or fatal("$projhead not in passwd file");

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

#
# Okay, do it.
#
156 157 158 159
if (! -e "$PROJROOT/$pid") {
    if (! mkdir("$PROJROOT/$pid", 0770)) {
	fatal("Could not make directory $PROJROOT/$pid: $!");
    }
160

161 162 163
    if (! chmod(0770, "$PROJROOT/$pid")) {
	fatal("Could not chmod directory $PROJROOT/$pid: $!");
    }
164

165 166 167
    if (! chown($uid, $gid, "$PROJROOT/$pid")) {
	fatal("Could not chown $PROJROOT/$pid to $uid/$gid: $!");
    }
168 169 170 171 172 173
}

#
# Make project subdirs.
#
foreach my $dir (@DIRLIST) {
174 175 176 177 178 179 180 181 182 183
    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: $!");
	}
184 185 186 187 188
    }
}

#
# Create a tftp directory for oskit kernels.
189 190 191 192 193 194 195 196 197 198 199
#
if (! -e "$TFTPROOT/proj/$pid") {
    if (! mkdir("$TFTPROOT/proj/$pid", 0770)) {
	fatal("Could not make directory $TFTPROOT/proj/$pid: $!");
    }
    if (! chmod(0777, "$TFTPROOT/proj/$pid")) {
	fatal("Could not chmod directory $TFTPROOT/proj/$pid: $!");
    }
    if (! chown($uid, $gid, "$TFTPROOT/proj/$pid")) {
	fatal("Could not chown $TFTPROOT/proj/$pid to $uid/$gid: $!");
    }
200 201
}

202 203
#
# Create groups directory.
204 205 206 207 208
#
if (! -e "$GRPROOT/$pid") {
    if (! mkdir("$GRPROOT/$pid", 0770)) {
	fatal("Could not make directory $GRPROOT/$pid: $!");
    }
209
    if (! chmod(0770, "$GRPROOT/$pid")) {
210 211 212 213 214
	fatal("Could not chmod directory $GRPROOT/$pid: $!");
    }
    if (! chown($uid, $gid, "$GRPROOT/$pid")) {
	fatal("Could not chown $GRPROOT/$pid to $uid/$gid: $!");
    }
215 216 217 218 219 220 221

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

224 225 226
#
# Create experiment working directory.
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
227
my $workdir = TBDB_EXPT_WORKDIR() . "/$pid";
228

229
if (! -e $workdir) {
230
    if (! mkdir("$workdir", 0775)) {
231 232
	fatal("Could not make directory $workdir: $!");
    }
233
    if (! chmod(0775, "$workdir")) {
234 235 236 237 238
	fatal("Could not chmod directory $workdir: $!");
    }
    if (! chown($uid, $gid, "$workdir")) {
	fatal("Could not chown $workdir to $uid/$gid: $!");
    }
239 240
}

241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
#
# 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";
273 274 275

	    $EUID = $UID;

276 277
	    system("$GRANTTYPE -p $pid $type") == 0 or
		fatal("Could not grant permission to use type $type!");
278 279

	    $EUID = 0;
280 281 282 283
	}
    }
}

284
print "Project Creation Completed!\n";
285 286
exit(0);

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

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