mkprojdir 2.15 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
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
#!/usr/local/bin/perl -wT
use Mysql;
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: mkprojdir <pid>
#

# un-taint path
$ENV{'PATH'} = '/bin:/usr/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

my $PROJROOT = "/proj";
my $dbh      = Mysql->connect("localhost","tbdb","script","none");
my $db_result= "";

#
# 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 root, tbroot, or people with admin status
# in the DB can run this script.
#
if ($UID != 0) {
    my ($me) = getpwuid($UID)
	or die "$UID not in passwd file";
    
    $db_result = $dbh->query("select admin from users where uid='$me'");
    my @row = $db_result->fetchrow_array();
    if ($row[0] != 1) {
	die("mkprojdir: You must be root or a TB administrator\n");
    }
}

#
# We need several bits of info from the database.
#
# * The project gid.
# * The project leader uid.
#
$db_result = $dbh->query("select unix_gid,head_uid ".
			 "from projects where pid='$pid'");
if ($db_result->numrows < 1) {	
    die("There is no project '$pid'.\n");
}
@row = $db_result->fetchrow_array();
my $gid  = $row[0];
my $head = $row[1];

70
71
72
73
74
75
#
# This acts as check (and we need the numeric uid) in case mkacct failed!
# 
my ($login,$pass,$uid) = getpwnam($head)
    or die "$head not in passwd file";

76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#
# Okay, do it.
#
if (! mkdir("$PROJROOT/$pid", 0770)) {
    die("Could not make directory $PROJROOT/$pid: $!\n");
}

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

#
# Make a few subdirs. If this gets long, make it a loop.
# 
if (! mkdir("$PROJROOT/$pid/exp", 0770)) {
    die("Could not make directory $PROJROOT/$pid/exp: $!\n");
}
if (! chown($uid, $gid, "$PROJROOT/$pid/exp")) {
    die("Could not chown $PROJROOT/$pid/exp to $uid/$gid: $!\n");
}

exit(0);