mkproj.in 4.24 KB
Newer Older
1
#!/usr/bin/perl -wT
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
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";
my $MKACCT   = "$TB/sbin/mkacct";

my $PROJROOT = "/proj";
22
my $GRPROOT  = "/groups";
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
my $TFTPROOT = "/tftpboot";
my @DIRLIST  = ("exp", "images", "logs", "deltas", "tarfiles", "rpms",
		"groups", "tiplogs");

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

my $projhead;

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

#
# 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
# our UID (perl sillyness). Do that in a child process cause we need
# root UID to finish off.
#
if (my $childpid = fork()) {
    #
    # Parent waits for child.
    # 
    waitpid($childpid, 0);
    if ($?) {
	exit($? >> 8);
    }
}
else {
    $EUID = $UID;

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

    system("$MKACCT $projhead") == 0 or
	fatal("$MKACCT $projhead failed!");
    
    exit(0);
}

#
# 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.
#
if (! mkdir("$PROJROOT/$pid", 0770)) {
    fatal("Could not make directory $PROJROOT/$pid: $!");
}

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

if (! chown($uid, $gid, "$PROJROOT/$pid")) {
    fatal("Could not chown $PROJROOT/$pid to $uid/$gid: $!");
}

#
# Make project subdirs.
#
foreach my $dir (@DIRLIST) {

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

#
# Create a tftp directory for oskit kernels.
# 
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: $!");
}

180 181 182 183 184 185
#
# Create groups directory.
# 
if (! mkdir("$GRPROOT/$pid", 0770)) {
    fatal("Could not make directory $GRPROOT/$pid: $!");
}
186
if (! chmod(0777, "$GRPROOT/$pid")) {
187 188 189 190 191 192
    fatal("Could not chmod directory $GRPROOT/$pid: $!");
}
if (! chown($uid, $gid, "$GRPROOT/$pid")) {
    fatal("Could not chown $GRPROOT/$pid to $uid/$gid: $!");
}

193 194 195 196 197 198 199 200
exit(0);

sub fatal {
    local($msg) = $_[0];

    SENDMAIL($TBOPS, "TESTBED: mkproj $pid Failed", $msg);
    die("$0: $msg\n");
}