newgroup.php3 4.99 KB
Newer Older
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1
<?php
Leigh B. Stoller's avatar
Leigh B. Stoller committed
2
3
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2000-2003 University of Utah and the Flux Group.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
5
6
# All rights reserved.
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
7
include("defs.php3");
8
include("showstuff.php3");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
9
10
11
12

#
# Standard Testbed Header
#
13
PAGEHEADER("Create a Project Group");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
14
15
16

$mydebug = 0;

17
18
ignore_user_abort(1);

Leigh B. Stoller's avatar
Leigh B. Stoller committed
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
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
#
# First off, sanity check the form to make sure all the required fields
# were provided. I do this on a per field basis so that we can be
# informative. Be sure to correlate these checks with any changes made to
# the project form. 
#
if (!isset($group_pid) ||
    strcmp($group_pid, "") == 0) {
  FORMERROR("Select Project");
}
if (!isset($group_id) ||
    strcmp($group_id, "") == 0) {
  FORMERROR("Group Name");
}
if (!isset($group_description) ||
    strcmp($group_description, "") == 0) {
  FORMERROR("Group Description");
}
if (!isset($group_leader) ||
    strcmp($group_leader, "") == 0) {
  FORMERROR("Group leader");
}

#
# Only known and logged in users.
#
$uid = GETLOGIN();
LOGGEDINORDIE($uid);

#
# Check ID for sillyness.
#
if (! ereg("^[-_a-zA-Z0-9]+$", $group_id)) {
    USERERROR("The group name must be alphanumeric characters only!", 1);
}

#
# Database limits
#
if (strlen($group_id) > $TBDB_GIDLEN) {
    USERERROR("The Group name is too long! Please select another.", 1);
}

#
# Certain of these values must be escaped or otherwise sanitized.
# 
$group_description = addslashes($group_description);

#
# Verify permission.
#
if (! TBProjAccessCheck($uid, $group_pid, 0, $TB_PROJECT_MAKEGROUP)) {
    USERERROR("You do not have permission to create groups in project ".
	      "$group_pid!", 1);
}

#
# Verify project and leader. That is, the leader choosen has to be a member
# of the default group for the project and must already possess a
# minimum level of trust since it would make no sense to make "user" a
# group leader.
#
if (! TBProjAccessCheck($group_leader, $group_pid, 0, $TB_PROJECT_LEADGROUP)) {
    USERERROR("$group_leader is not a trusted (local root or better) member ".
	      "of the project $group_pid!", 1);
}
	       
#
# Make sure the GID is not already there.
#
if (TBValidGroup($group_pid, $group_id)) {
    USERERROR("The group $group_id already exists! Please select another.", 1);
}

#
# The unix group name must be globally unique. Form a name and check it.
#
$unix_gname = substr($group_pid, 0, 3) . "-" . $group_id;
$maxtries   = 99;
$count      = 0;

while ($count < $maxtries) {
    if (strlen($unix_gname) > $TBDB_UNIXGLEN) {
	TBERROR("Unix group name $unix_gname is too long!", 1);
    }
    
    $query_result =
	DBQueryFatal("select gid from groups where unix_name='$unix_gname'");

    if (mysql_num_rows($query_result) == 0) {
	break;
    }
    $count++;

    $unix_gname = substr($group_pid, 0, 3) . "-" .
	substr($group_id,  0, strlen($group_id) - 2) . "$count";
}
if ($count == $maxtries) {
    TBERROR("Could not form a unique Unix group name!", 1);
}

#
# Create the new group and set up the initial membership for the leader
# (and the project leader if not the same).
# 
DBQueryFatal("INSERT INTO groups ".
	     "(pid, gid, leader, created, description, unix_gid, unix_name) ".
	     "VALUES ('$group_pid', '$group_id', '$group_leader', now(), ".
	     "        '$group_description', NULL, '$unix_gname')");

DBQueryFatal("insert into group_membership ".
	     "(uid, pid, gid, trust, date_applied, date_approved) ".
	     "values ('$group_leader','$group_pid','$group_id', ".
	     "        'group_root', now(), now())");

#
# Grab the project head uid for the project. If its different than the
# the leader for the group, insert the project head also. This is polite.
#
$query_result =
    DBQueryFatal("select head_uid from projects where pid='$group_pid'");
if (($row = mysql_fetch_row($query_result)) == 0) {
    DBFatal("Getting head_uid for project $group_pid.");
}
$head_uid = $row[0];

if (strcmp($head_uid, $group_leader)) {
    DBQueryFatal("insert into group_membership ".
		 "(uid, pid, gid, trust, date_applied, date_approved) ".
		 "values ('$head_uid','$group_pid','$group_id', ".
		 "        'group_root', now(), now())");
}

#
# Grab the unix GID for running scripts.
#
TBGroupUnixInfo($group_pid, $group_pid, $unix_gid, $unix_name);

157
158
159
160
161
162
163
echo "<br>
      Group '$group_id' in project '$group_pid' is being created!<br><br>
      This will take a minute or two. <b>Please</b> do not click the Stop
      button during this time. If you do not receive notification within
      a reasonable amount of time, please contact $TBMAILADDR.\n";
flush();

Leigh B. Stoller's avatar
Leigh B. Stoller committed
164
165
166
167
168
#
# Run the script. This will make the group directory, set the perms,
# and do the account stuff for all of the people in the group. This
# is the same script that gets run when the group membership changes.
#
169
170
SUEXEC($uid, $unix_gid, "webmkgroup $group_pid $group_id", 1);
SUEXEC($uid, $unix_gid, "websetgroups $head_uid $group_leader", 1);
171
172
173
174

echo "<br><br>
      <b>Done!</b>
      <br>\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
175

176
177
178
179
#
# Show it!
#
SHOWGROUP($group_pid, $group_id);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
180

181
182
183
184
185
186
#
# Back to ...
# 
echo "<br>
       <A href='showproject.php3?pid=$group_pid'>Back to Project page</a>\n";

Leigh B. Stoller's avatar
Leigh B. Stoller committed
187
188
189
190
191
#
# Standard Testbed Footer
# 
PAGEFOOTER();
?>