dbdefs.php3.in 53.7 KB
Newer Older
1
2
3
4
<?php
#
# Database Constants
#
5
6
require("dbcheck.php3");

7
$TBDBNAME       = "@TBDBNAME@";
8
$TBOPSPID	= "emulab-ops";
9
$NODEDEAD_PID   = $TBOPSPID;
10
$NODEDEAD_EID   = "hwdown";
11
$FIRSTUSER      = "elabman";
12
13
14
$NODERELOADING_PID	= $TBOPSPID;
$NODERELOADING_EID	= "reloading";
$NODERELOADPENDING_EID	= "reloadpending";
15

16
17
# All these constants need to go at some point, replaced by data from
# the regex table. 
18
19
$TBDB_UIDLEN    = 8;
$TBDB_PIDLEN    = 12;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
20
21
$TBDB_GIDLEN	= 12;
$TBDB_UNIXGLEN	= 16;
22
$TBDB_NODEIDLEN = 10;
23
24
$TBDB_PHONELEN  = 32;
$TBDB_USRNAMELEN= 64;
25
$TBDB_EMAILLEN  = 64;
26
$TBDB_MMLENGTH  = 64;
27
28
29
30
31
32
33
34
35

#
# Current policy is to prefix the EID with the PID. Make sure it is not
# too long for the database. PID is 12, and the max is 32, so the user
# cannot have provided an EID more than 19, since other parts of the system
# may concatenate them together with a hyphen.
#
$TBDB_EIDLEN    = 19;

36
37
$TBDB_OSID_OSIDLEN         = 35;
$TBDB_OSID_OSNAMELEN       = 20;
38
$TBDB_OSID_VERSLEN         = 12;
39
40
$TBDB_IMAGEID_IMAGEIDLEN   = 45;
$TBDB_IMAGEID_IMAGENAMELEN = 30;
41

42
43
44
#
# User status field.
#
45
46
47
48
49
define("TBDB_USERSTATUS_ACTIVE",	"active");
define("TBDB_USERSTATUS_NEWUSER",	"newuser");
define("TBDB_USERSTATUS_UNAPPROVED",	"unapproved");
define("TBDB_USERSTATUS_UNVERIFIED",	"unverified");
define("TBDB_USERSTATUS_FROZEN",	"frozen");
50

Leigh B. Stoller's avatar
Leigh B. Stoller committed
51
52
53
54
55
56
57
58
59
60
61
#
# Trust. Define the trust level as an increasing value. Then define a
# function to return whether the given trust is high enough.
#
$TBDB_TRUST_NONE		= 0;
$TBDB_TRUST_USER		= 1;
$TBDB_TRUST_LOCALROOT		= 2;
$TBDB_TRUST_GROUPROOT		= 3;
$TBDB_TRUST_PROJROOT		= 4;
$TBDB_TRUST_ADMIN		= 5;

62
63
64
65
66
67
68
69
70
#
# Text strings in the DB for above.
# 
define("TBDB_TRUSTSTRING_NONE",		"none");
define("TBDB_TRUSTSTRING_USER",		"user");
define("TBDB_TRUSTSTRING_LOCALROOT",	"local_root");
define("TBDB_TRUSTSTRING_GROUPROOT",	"group_root");
define("TBDB_TRUSTSTRING_PROJROOT",	"project_root");

Leigh B. Stoller's avatar
Leigh B. Stoller committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#
# These are the permission types. Different operations for the varying
# types of things we need to control access to.
#
# Things you can do to a node.
$TB_NODEACCESS_READINFO		= 1;
$TB_NODEACCESS_MODIFYINFO	= 2;
$TB_NODEACCESS_LOADIMAGE	= 3;
$TB_NODEACCESS_REBOOT		= 4;
$TB_NODEACCESS_POWERCYCLE	= 5;
$TB_NODEACCESS_MIN		= $TB_NODEACCESS_READINFO;
$TB_NODEACCESS_MAX		= $TB_NODEACCESS_POWERCYCLE;

# User Info (modinfo web page, etc).
$TB_USERINFO_READINFO		= 1;
$TB_USERINFO_MODIFYINFO		= 2;
$TB_USERINFO_MIN		= $TB_USERINFO_READINFO;
$TB_USERINFO_MAX		= $TB_USERINFO_MODIFYINFO;

# Experiments (also batch experiments).
$TB_EXPT_READINFO		= 1;
$TB_EXPT_MODIFY			= 2;	# Allocate/dealloc nodes
$TB_EXPT_DESTROY		= 3;
94
$TB_EXPT_UPDATE			= 4;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
95
$TB_EXPT_MIN			= $TB_EXPT_READINFO;
96
$TB_EXPT_MAX			= $TB_EXPT_UPDATE;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
97
98
99
100
101

# Projects.
$TB_PROJECT_READINFO		= 1;
$TB_PROJECT_MAKEGROUP		= 2;
$TB_PROJECT_EDITGROUP		= 3;
Chad Barb's avatar
   
Chad Barb committed
102
$TB_PROJECT_GROUPGRABUSERS      = 4;
Chad Barb's avatar
   
Chad Barb committed
103
104
105
106
107
108
109
110
111
112
$TB_PROJECT_BESTOWGROUPROOT     = 5;
$TB_PROJECT_DELGROUP		= 6;
$TB_PROJECT_LEADGROUP		= 7;
$TB_PROJECT_ADDUSER		= 8;
$TB_PROJECT_DELUSER             = 9;
$TB_PROJECT_MAKEOSID		= 10;
$TB_PROJECT_DELOSID		= 11;
$TB_PROJECT_MAKEIMAGEID		= 12;
$TB_PROJECT_DELIMAGEID		= 13;
$TB_PROJECT_CREATEEXPT		= 14;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
$TB_PROJECT_MIN			= $TB_PROJECT_READINFO;
$TB_PROJECT_MAX			= $TB_PROJECT_CREATEEXPT;

# OSIDs 
$TB_OSID_READINFO		= 1;
$TB_OSID_MODIFYINFO		= 2;
$TB_OSID_DESTROY		= 3;
$TB_OSID_MIN			= $TB_OSID_READINFO;
$TB_OSID_MAX			= $TB_OSID_DESTROY;

# ImageIDs
$TB_IMAGEID_READINFO		= 1;
$TB_IMAGEID_MODIFYINFO		= 2;
$TB_IMAGEID_ACCESS		= 3;
$TB_IMAGEID_DESTROY		= 4;
$TB_IMAGEID_MIN			= $TB_IMAGEID_READINFO;
$TB_IMAGEID_MAX			= $TB_IMAGEID_DESTROY;

131
# Experiment states (that matter to us).
132
133
$TB_EXPTSTATE_NEW		= "new"; 
$TB_EXPTSTATE_PRERUN		= "prerunning"; 
134
$TB_EXPTSTATE_SWAPPING		= "swapping";
135
$TB_EXPTSTATE_SWAPPED		= "swapped";
136
$TB_EXPTSTATE_ACTIVATING	= "activating";
137
$TB_EXPTSTATE_ACTIVE		= "active";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
138
$TB_EXPTSTATE_PANICED		= "paniced";
139
$TB_EXPTSTATE_QUEUED		= "queued";
140

Leigh B. Stoller's avatar
Leigh B. Stoller committed
141
142
143
144
145
# Interfaces roles.
define("TBDB_IFACEROLE_CONTROL",	"ctrl");
define("TBDB_IFACEROLE_EXPERIMENT",	"expt");
define("TBDB_IFACEROLE_JAIL",		"jail");
define("TBDB_IFACEROLE_FAKE",		"fake");
146
define("TBDB_IFACEROLE_GW",		"gw");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
147
define("TBDB_IFACEROLE_OTHER",		"other");
148
define("TBDB_IFACEROLE_OUTER_CONTROL",  "outer_ctrl");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
149

150
151
152
# Node states that the web page cares about.
define("TBDB_NODESTATE_ISUP",		"ISUP");
define("TBDB_NODESTATE_PXEWAIT",	"PXEWAIT");
Timothy Stack's avatar
   
Timothy Stack committed
153
154
define("TBDB_NODESTATE_POWEROFF",	"POWEROFF");
define("TBDB_NODESTATE_ALWAYSUP",	"ALWAYSUP");
155

156
157
158
159
160
161
# User Interface types
define("TBDB_USER_INTERFACE_EMULAB",	"emulab");
define("TBDB_USER_INTERFACE_PLAB",	"plab");
$TBDB_USER_INTERFACE_LIST = array(TBDB_USER_INTERFACE_EMULAB,
				  TBDB_USER_INTERFACE_PLAB);

162
163
164
165
# Lintest levels.
$linktest_levels	= array();
$linktest_levels[0]	= "Skip Linktest";
$linktest_levels[1]	= "Connectivity and Latency";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
166
167
168
$linktest_levels[2]	= "Plus Static Routing";
$linktest_levels[3]	= "Plus Loss";
$linktest_levels[4]	= "Plus Bandwidth";
169
170
define("TBDB_LINKTEST_MAX", 4);

Leigh B. Stoller's avatar
Leigh B. Stoller committed
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#
# Convert a trust string to the above numeric values.
#
function TBTrustConvert($trust_string)
{
    global $TBDB_TRUST_NONE;
    global $TBDB_TRUST_USER;
    global $TBDB_TRUST_LOCALROOT;
    global $TBDB_TRUST_GROUPROOT;
    global $TBDB_TRUST_PROJROOT;
    global $TBDB_TRUST_ADMIN;
    $trust_value = 0;

    #
    # Convert string to value. Perhaps the DB should have done it this way?
    # 
    if (strcmp($trust_string, "none") == 0) {
	    $trust_value = $TBDB_TRUST_NONE;
    }
    elseif (strcmp($trust_string, "user") == 0) {
	    $trust_value = $TBDB_TRUST_USER;
    }
    elseif (strcmp($trust_string, "local_root") == 0) {
	    $trust_value = $TBDB_TRUST_LOCALROOT;
    }
    elseif (strcmp($trust_string, "group_root") == 0) {
	    $trust_value = $TBDB_TRUST_GROUPROOT;
    }
    elseif (strcmp($trust_string, "project_root") == 0) {
	    $trust_value = $TBDB_TRUST_PROJROOT;
    }
    elseif (strcmp($trust_string, "admin") == 0) {
	    $trust_value = $TBDB_TRUST_ADMIN;
    }
    else {
	    TBERROR("Invalid trust value $trust_string!", 1);
    }

    return $trust_value;
}

#
# Return true if the given trust string is >= to the minimum required.
# The trust value can be either numeric or a string; if a string its
# first converted to the numeric equiv.
#
function TBMinTrust($trust_value, $minimum)
{
    global $TBDB_TRUST_NONE;
    global $TBDB_TRUST_ADMIN;

    if ($minimum < $TBDB_TRUST_NONE || $minimum > $TBDB_TRUST_ADMIN) {
	    TBERROR("Invalid minimum trust $minimum!", 1);
    }

    #
    # Sleazy?
    #
    if (gettype($trust_value) == "string") {
	$trust_value = TBTrustConvert($trust_value);
    }
    
    return $trust_value >= $minimum;
}

#
# Determine the trust level for a uid/pid/gid. That is, each uid will have
# a different trust level depending on the project/group in question.
# Return that trust level as one of the numeric values above. 
# 
function TBGrpTrust($uid, $pid, $gid)
{
    global $TBDB_TRUST_NONE;

    #
    # No group, then use the default group.
    #
    if (! $gid) {
	$gid = $pid;
    }

    $query_result =
	DBQueryFatal("select trust from group_membership ".
		     "where uid='$uid' and pid='$pid' and gid='$gid'");

    #
    # No membership is the same as no trust. True? Maybe an error instead?
    # 
    if (mysql_num_rows($query_result) == 0) {
	return $TBDB_TRUST_NONE;
    }
    $row = mysql_fetch_array($query_result);
    $trust_string = $row[trust];

    return TBTrustConvert($trust_string);
}

#
# Determine the project trust level for a uid/pid. This is the trust level
# for the default group in the project.
# 
function TBProjTrust($uid, $pid)
{
    return TBGrpTrust($uid, $pid, $pid);
}

#
# Project permission checks. The group id (gid) can be zero, in which case
# the pid is used (ie: a default group check is made).
#
# Usage: TBProjAccessCheck($uid, $pid, $gid, $access_type)
#	 returns 0 if not allowed.
#        returns 1 if allowed.
# 
function TBProjAccessCheck($uid, $pid, $gid, $access_type)
{
    global $TB_PROJECT_READINFO;
    global $TB_PROJECT_MAKEGROUP;
    global $TB_PROJECT_EDITGROUP;
Chad Barb's avatar
   
Chad Barb committed
290
    global $TB_PROJECT_GROUPGRABUSERS;
Chad Barb's avatar
   
Chad Barb committed
291
    global $TB_PROJECT_BESTOWGROUPROOT;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
    global $TB_PROJECT_DELGROUP;
    global $TB_PROJECT_LEADGROUP;
    global $TB_PROJECT_ADDUSER;
    global $TB_PROJECT_DELUSER;
    global $TB_PROJECT_MAKEOSID;
    global $TB_PROJECT_DELOSID;
    global $TB_PROJECT_MAKEIMAGEID;
    global $TB_PROJECT_DELIMAGEID;
    global $TB_PROJECT_CREATEEXPT;
    global $TB_PROJECT_MIN;
    global $TB_PROJECT_MAX;
    global $TBDB_TRUST_USER;
    global $TBDB_TRUST_LOCALROOT;
    global $TBDB_TRUST_GROUPROOT;
    global $TBDB_TRUST_PROJROOT;
    $mintrust;

    if ($access_type < $TB_PROJECT_MIN ||
	$access_type > $TB_PROJECT_MAX) {
	TBERROR("Invalid access type: $access_type!", 1);
    }

    #
    # Admins do whatever they want!
    # 
    if (ISADMIN($uid)) {
	return 1;
    }

    #
    # No group, then use the default group.
    #
    if (! $gid) {
	$gid = $pid;
    }

    if ($access_type == $TB_PROJECT_READINFO) {
	$mintrust = $TBDB_TRUST_USER;
    }
    elseif ($access_type == $TB_PROJECT_MAKEGROUP ||
	    $access_type == $TB_PROJECT_DELGROUP) {
333
	$mintrust = $TBDB_TRUST_GROUPROOT;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
334
335
336
    }
    elseif ($access_type == $TB_PROJECT_LEADGROUP) {
	#
337
	# Allow mere user (in default group) to lead a subgroup.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
338
	# 
339
	$mintrust = $TBDB_TRUST_USER;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
340
341
342
343
344
345
    }
    elseif ($access_type == $TB_PROJECT_MAKEOSID ||
	    $access_type == $TB_PROJECT_MAKEIMAGEID ||
	    $access_type == $TB_PROJECT_CREATEEXPT) {
	$mintrust = $TBDB_TRUST_LOCALROOT;
    }
Chad Barb's avatar
   
Chad Barb committed
346
347
    elseif ($access_type == $TB_PROJECT_ADDUSER ||
	    $access_type == $TB_PROJECT_EDITGROUP) {
348
	#
Chad Barb's avatar
   
Chad Barb committed
349
350
	# If user is project_root or group_root in default group, 
	# allow them to add/edit/remove users in any group.
351
	#
Chad Barb's avatar
   
Chad Barb committed
352
353
354
355
356
357
358
	if (TBMinTrust(TBGrpTrust($uid, $pid, $pid), $TBDB_TRUST_GROUPROOT)) {
	    return 1;
	}
	#
	# Otherwise, editing a group requires group_root 
	# in that group.
	#	
359
360
	$mintrust = $TBDB_TRUST_GROUPROOT;
    }
Chad Barb's avatar
   
Chad Barb committed
361
362
363
364
365
366
367
    elseif ($access_type == $TB_PROJECT_BESTOWGROUPROOT) {
	#
	# If user is project_root, 
	# allow them to bestow group_root in any group.
	#
	if (TBMinTrust(TBGrpTrust($uid, $pid, $pid), $TBDB_TRUST_PROJROOT)) {
	    return 1;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
368
	}
Chad Barb's avatar
   
Chad Barb committed
369
370
371
372

	if (strcmp($gid, $pid) == 0)  {
	    #
	    # Only project_root can bestow group_root in default group, and
373
	    # we already established that they are not project_root, so fail.
Chad Barb's avatar
   
Chad Barb committed
374
375
	    #
	    return 0;
376
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
377
	else {
Chad Barb's avatar
   
Chad Barb committed
378
379
380
381
	    #
	    # Non-default group.
	    # group_root in default group may bestow group_root.
	    #
382
383
	    if (TBMinTrust(TBGrpTrust($uid, $pid, $pid),
			   $TBDB_TRUST_GROUPROOT)) {
Chad Barb's avatar
   
Chad Barb committed
384
385
386
387
388
389
		return 1;
	    }

	    #
	    # group_root in the group in question may also bestow group_root.
	    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
390
391
392
	    $mintrust = $TBDB_TRUST_GROUPROOT;
	}
    }
Chad Barb's avatar
   
Chad Barb committed
393
    elseif ($access_type == $TB_PROJECT_GROUPGRABUSERS) {
Chad Barb's avatar
   
Chad Barb committed
394
395
396
397
398
399
	#
	# Only project_root or group_root in default group
	# may grab (involuntarily add) users into groups.
	#
	$gid = $pid;
	$mintrust = $TBDB_TRUST_GROUPROOT;
Chad Barb's avatar
   
Chad Barb committed
400
    }
401
402
403
    elseif ($access_type == $TB_PROJECT_DELUSER) {
	$mintrust = $TBDB_TRUST_PROJROOT;
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
404
405
406
407
408
409
410
    else {
	TBERROR("Unexpected access type: $access_type!", 1);
    }

    return TBMinTrust(TBGrpTrust($uid, $pid, $gid), $mintrust);
}

Chad Barb's avatar
   
Chad Barb committed
411
412
413
414
415
416
417
418
419
420
421
422
423
424
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

#
# Checks proposed Group trust change for consistency.
#
# Usage: TBCheckGroupTrustConsistency($user, $pid, $gid, $newtrust, $fail) 
#        returns 1 if proposed change is valid
#        returns 0 if proposed change is invalid and $fail == 0
#        does not return if proposed change is invalid and $fail == 1.
#

function TBCheckGroupTrustConsistency($user, $pid, $gid, $newtrust, $fail)
{
    global $TBDB_TRUST_USER;

    # 
    # set $newtrustisroot to 1 if attempting to set a rootful trust,
    # 0 otherwise.
    #
    $newtrustisroot = TBTrustConvert($newtrust) > $TBDB_TRUST_USER ? 1 : 0;

    #
    # If changing subgroup trust level, then compare levels.
    # A user may not have root privs in the project and user privs
    # in the subgroup; it makes no sense to do that and can violate trust.
    #
    if (strcmp($pid, $gid)) {
	#
	# Setting non-default "sub"group.
	# Verify that if user has root in project,
	# we are setting a rootful trust for him in 
	# the subgroup as well.
	#
	$projtrustisroot = TBProjTrust($user, $pid) > $TBDB_TRUST_USER ? 1 : 0;

	if ($projtrustisroot > $newtrustisroot) {
	    if (!$fail) { return 0; }
	    TBERROR("User $user may not have a root trust level in ".
		      "the default group of $pid, ".
		      "yet be non-root in subgroup $gid!", 1);
	}
    }
    else {
	#
	# Setting default group.
455
	# Do not verify anything (yet.)
Chad Barb's avatar
   
Chad Barb committed
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
	#
	$projtrustisroot = $newtrustisroot;
    }

    #
    # Get all the subgroups not equal to the subgroup being changed.
    # 
    $query_result =
	DBQueryFatal("select trust,gid from group_membership ".
		     "where uid='$user' and pid='$pid' and trust!='none' ".
		     " and gid!=pid and gid!='$gid'");

    while ($row = mysql_fetch_array($query_result)) {
	$grptrust = $row[0];
	$ogid     = $row[1];
	
	# 
473
	# Get what the users trust level is in the 
Chad Barb's avatar
   
Chad Barb committed
474
475
476
477
478
479
	# current subgroup we're looking at.
	#
	$grptrustisroot = 
	    TBTrustConvert( $grptrust ) > $TBDB_TRUST_USER ? 1 : 0;

	#
480
	# If users trust level is higher in the default group than in the
Chad Barb's avatar
   
Chad Barb committed
481
482
483
484
485
486
487
488
489
490
491
	# subgroup we are looking at, this is wrong.
	#
	if ($projtrustisroot > $grptrustisroot) {
	    if (!$fail) { return 0; }
	    TBERROR("User $user may not have a root trust level in ".
		      "the default group of $pid, ".
		      "yet be non-root in subgroup $ogid!", 1);
	}

	if (strcmp($pid, $gid)) {
            #
492
493
	    # Iff we are modifying a subgroup, 
	    # Make sure that the trust we are setting is as
Chad Barb's avatar
   
Chad Barb committed
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
	    # rootful as the trust we already have set in
	    # every other subgroup.
	    # 
	    if ($newtrustisroot != $grptrustisroot) { 
		if (!$fail) { return 0; }
		TBERROR("User $user may not mix root and ".
			  "non-root trust levels in ".
			  "different subgroups of $pid!", 1);
	    }
	}
    }
    return 1;
}


509
# Usage: TBExptGroup($pid, $eid, &$gid)
510
#	 returns 0 if expt does not exist.
511
512
513
#        returns 1 if expt exists.
function TBExptGroup($pid, $eid, &$gid)
{
514
515
516
517
518
519
520
521
522
    $query_result =
	DBQueryFatal("SELECT gid FROM experiments WHERE ".
		     "eid='$eid' and pid='$pid'");
    if (mysql_num_rows($query_result) == 0) {
	return 0;
    }
    $row  = mysql_fetch_array($query_result);
    $gid  = $row[gid];
    return 1;
523
524
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
525
526
527
528
529
530
531
532
533
534
535
536
#
# Experiment permission checks.
#
# Usage: TBExptAccessCheck($uid, $pid, $eid, $access_type)
#	 returns 0 if not allowed.
#        returns 1 if allowed.
# 
function TBExptAccessCheck($uid, $pid, $eid, $access_type)
{
    global $TB_EXPT_READINFO;
    global $TB_EXPT_MODIFY;
    global $TB_EXPT_DESTROY;
537
    global $TB_EXPT_UPDATE;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
    global $TB_EXPT_MIN;
    global $TB_EXPT_MAX;
    global $TBDB_TRUST_USER;
    global $TBDB_TRUST_LOCALROOT;
    global $TBDB_TRUST_GROUPROOT;
    global $TBDB_TRUST_PROJROOT;
    $mintrust;

    if ($access_type < $TB_EXPT_MIN ||
	$access_type > $TB_EXPT_MAX) {
	TBERROR("Invalid access type: $access_type!", 1);
    }

    #
    # Admins do whatever they want!
    # 
    if (ISADMIN($uid)) {
	return 1;
    }

    $query_result =
559
	DBQueryFatal("SELECT gid,expt_head_uid FROM experiments WHERE ".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
560
561
562
563
564
		     "eid='$eid' and pid='$pid'");
    
    if (mysql_num_rows($query_result) == 0) {
	return 0;
    }
565
566
567
    $row  = mysql_fetch_array($query_result);
    $gid  = $row[gid];
    $head = $row[expt_head_uid];
Leigh B. Stoller's avatar
Leigh B. Stoller committed
568
569
570
571
572
573
574
575

    if ($access_type == $TB_EXPT_READINFO) {
	$mintrust = $TBDB_TRUST_USER;
    }
    else {
	$mintrust = $TBDB_TRUST_LOCALROOT;
    }

576
577
578
579
580
581
582
583
    #
    # Either proper permission in the group, or group_root in the project.
    # This lets group_roots muck with other peoples experiments, including
    # those in groups they do not belong to.
    #

   return TBMinTrust(TBGrpTrust($uid, $pid, $gid), $mintrust) ||
	TBMinTrust(TBGrpTrust($uid, $pid, $pid), $TBDB_TRUST_GROUPROOT);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
}

#
# Determine if uid can access a node (read or modify).
#
# Usage: TBNodeAccessCheck($uid, $node_id, $access_type)
#	 returns 0 if not allowed.
#        returns 1 if allowed.
# 
function TBNodeAccessCheck($uid, $node_id, $access_type)
{
    global $TB_NODEACCESS_READINFO;
    global $TB_NODEACCESS_MODIFYINFO;
    global $TB_NODEACCESS_LOADIMAGE;
    global $TB_NODEACCESS_REBOOT;
    global $TB_NODEACCESS_POWERCYCLE;
    global $TB_NODEACCESS_MIN;
    global $TB_NODEACCESS_MAX;
    global $TBDB_TRUST_USER;
603
    global $TBDB_TRUST_GROUPROOT;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
604
    global $TBDB_TRUST_LOCALROOT;
605
    global $TBOPSPID;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
606
607
608
609
610
611
612
613
    $mintrust;

    if ($access_type < $TB_NODEACCESS_MIN ||
	$access_type > $TB_NODEACCESS_MAX) {
	TBERROR("Invalid access type: $access_type!", 1);
    }

    $query_result =
614
	DBQueryFatal("select e.pid,e.gid from reserved as r ".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
615
		     "left join experiments as e on ".
616
617
		     "     e.pid=r.pid and e.eid=r.eid ".
		     "where r.node_id='$node_id'");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
618
619

    if (mysql_num_rows($query_result) == 0) {
620
621
622
623
624
625
626
627
	#
	# If the current user is in the emulab-ops project and has sufficient
	# privs, then he can muck with free nodes as if he were an admin type.
	#
	if ($uid == GETUID() && OPSGUY()) {
	    return(TBMinTrust(TBGrpTrust($uid, $TBOPSPID, $TBOPSPID),
			      $TBDB_TRUST_LOCALROOT));
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
628
629
	return 0;
    }
630
631
632
    $row = mysql_fetch_array($query_result);
    $pid = $row[pid];
    $gid = $row[gid];
Leigh B. Stoller's avatar
Leigh B. Stoller committed
633
634
635
636
637
638
639

    if ($access_type == $TB_NODEACCESS_READINFO) {
	$mintrust = $TBDB_TRUST_USER;
    }
    else {
	$mintrust = $TBDB_TRUST_LOCALROOT;
    }
640
641
    return TBMinTrust(TBGrpTrust($uid, $pid, $gid), $mintrust) ||
	TBMinTrust(TBGrpTrust($uid, $pid, $pid), $TBDB_TRUST_GROUPROOT);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
}

#
# Determine if uid can access a another user record (read or modify).
#
# Usage: TBUserInfoAccessCheck($uid, $target_uid, $access_type)
#	 returns 0 if not allowed.
#        returns 1 if allowed.
# 
function TBUserInfoAccessCheck($uid, $target_uid, $access_type)
{
    global $TB_USERINFO_READINFO;
    global $TB_USERINFO_MODIFYINFO;
    global $TB_USERINFO_MIN;
    global $TB_USERINFO_MAX;

    if ($access_type < $TB_USERINFO_MIN || $access_type > $TB_USERINFO_MAX) {
	TBERROR("Invalid access type $access_type!", 1);
    }

    if (strcmp($uid, $target_uid) == 0) {
	return 1;
    }

666
667
668
669
670
671
672
    #
    # Admins do whatever they want!
    # 
    if (ISADMIN($uid)) {
	return 1;
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
    #
    # This join will allow the operation if the current user is in the same
    # group (any group) as the target user, but with root permissions.
    # 
    $query_result =
	DBQueryFatal("select g.trust from group_membership as g ".
		     "left join group_membership as authed on ".
		     "     g.pid=authed.pid and g.gid=authed.gid and ".
		     "     g.uid='$target_uid' ".
		     "where authed.uid='$uid' and ".
		     "      (authed.trust='group_root' or ".
		     "       authed.trust='project_root')");

    if (mysql_num_rows($query_result) == 0) {
	return 0;
    }
    return 1;
}

#
# Access checks for an OSID. Tests for tbadmin.
#
# Usage: TBOSIDAccessCheck($uid, $osid, $access_type)
#	 returns 0 if not allowed.
#        returns 1 if allowed.
# 
function TBOSIDAccessCheck($uid, $osid, $access_type)
{
    global $TB_OSID_READINFO;
    global $TB_OSID_MODIFYINFO;
    global $TB_OSID_DESTROY;
    global $TB_OSID_MIN;
    global $TB_OSID_MAX;
    global $TBDB_TRUST_USER;
    global $TBDB_TRUST_LOCALROOT;
    $mintrust;

    if ($access_type < $TB_OSID_MIN || $access_type > $TB_OSID_MAX) {
	TBERROR("Invalid access type $access_type!", 1);
    }

    #
    # Admins do whatever they want!
    # 
    if (ISADMIN($uid)) {
	return 1;
    }

    #
    # No GIDs yet.
    #
    $query_result =
725
	DBQueryFatal("SELECT pid,shared FROM os_info WHERE osid='$osid'");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
726
727
728
729
730
    
    if (mysql_num_rows($query_result) == 0) {
	return 0;
    }
    $row = mysql_fetch_array($query_result);
731
732
    $pid    = $row[pid];
    $shared = $row[shared];
Leigh B. Stoller's avatar
Leigh B. Stoller committed
733
734
735
736

    #
    # Global OSIDs can be read by anyone.
    # 
737
    if ($shared) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
	if ($access_type == $TB_OSID_READINFO) {
	    return 1;
	}
	return 0;
    }
    
    #
    # Otherwise must have proper trust in the project.
    # 
    if ($access_type == $TB_OSID_READINFO) {
	$mintrust = $TBDB_TRUST_USER;
    }
    else {
	$mintrust = $TBDB_TRUST_LOCALROOT;
    }

    return TBMinTrust(TBProjTrust($uid, $pid), $mintrust);
}

#
# Access checks for an ImageID
#
# Usage: TBImageIDAccessCheck($uid, $imageid, $access_type)
#	 returns 0 if not allowed.
#        returns 1 if allowed.
# 
function TBImageIDAccessCheck($uid, $imageid, $access_type)
{
    global $TB_IMAGEID_READINFO;
    global $TB_IMAGEID_MODIFYINFO;
    global $TB_IMAGEID_DESTROY;
    global $TB_IMAGEID_ACCESS;
    global $TB_IMAGEID_MIN;
    global $TB_IMAGEID_MAX;
    global $TBDB_TRUST_USER;
773
    global $TBDB_TRUST_GROUPROOT;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
    global $TBDB_TRUST_LOCALROOT;
    $mintrust;

    if ($access_type < $TB_IMAGEID_MIN || $access_type > $TB_IMAGEID_MAX) {
	TBERROR("Invalid access type $access_type!", 1);
    }

    #
    # Admins do whatever they want!
    # 
    if (ISADMIN($uid)) {
	return 1;
    }

    $query_result =
789
790
	DBQueryFatal("SELECT pid,gid,shared,global FROM images ".
		     "WHERE imageid='$imageid'");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
791
792
793
794
    
    if (mysql_num_rows($query_result) == 0) {
	return 0;
    }
795
796
    $row    = mysql_fetch_array($query_result);
    $shared = $row[shared];
797
    $global = $row["global"];
798
    $pid    = $row[pid];
799
    $gid    = $row[gid];
Leigh B. Stoller's avatar
Leigh B. Stoller committed
800
801

    #
802
    # Global ImageIDs can be read by anyone but written by Admins only.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
803
    # 
804
    if ($global) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
805
806
807
808
809
810
811
812
813
814
815
	if ($access_type == $TB_IMAGEID_READINFO) {
	    return 1;
	}
	return 0;
    }

    #
    # Otherwise must have proper trust in the project.
    # 
    if ($access_type == $TB_IMAGEID_READINFO) {
	$mintrust = $TBDB_TRUST_USER;
816
817
818
819
820
        #
        # Shared imageids are readable by anyone in the project.
        #
	if ($shared)
	    $gid = $pid;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
821
822
823
824
825
    }
    else {
	$mintrust = $TBDB_TRUST_LOCALROOT;
    }

826
827
    return TBMinTrust(TBGrpTrust($uid, $pid, $gid), $mintrust) ||
	TBMinTrust(TBGrpTrust($uid, $pid, $pid), $TBDB_TRUST_GROUPROOT);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
828
829
830
831
832
833
}

#
# Return the Unix GID Name and Number for a group. This is distinct from
# the name we use within the Testbed system.
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
834
# usage: TBGroupUnixInfo($pid, $gid, &$unix_gid, &$unix_name)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
835
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
836
function TBGroupUnixInfo($pid, $gid, &$unix_gid, &$unix_name)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
{
    $query_result =
	DBQueryFatal("select unix_name,unix_gid from groups ".
		     "where pid='$pid' and gid='$gid'");

    if (mysql_num_rows($query_result) == 0) {
	TBERROR("No such group $gid in project $pid!", 1);
    }
    $row = mysql_fetch_array($query_result);
    $unix_gid  = $row[unix_gid];
    $unix_name = $row[unix_name];
}

#
# Get name and email info for a uid. Args are pass by reference.
#
# usage: TBUserInfo($uid, $user_name, $user_email)
#
function TBUserInfo($uid, &$user_name, &$user_email)
{
    $query_result =
	DBQueryFatal("select usr_name,usr_email from users where uid='$uid'");

    if (mysql_num_rows($query_result) == 0) {
	TBERROR("No such user $uid!", 1);
    }
    $row = mysql_fetch_array($query_result);
    $user_name  = $row[usr_name];
    $user_email = $row[usr_email];
}

868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
#
# Confirm a current user or not.
#
# usage TBCurrentUser($uid)
#       returns 1 if a valid user.
#       returns 0 if not a valid user.
#
function TBCurrentUser($uid)
{
    $query_result =
	DBQueryFatal("SELECT usr_pswd FROM users WHERE uid='$uid'");

    return mysql_num_rows($query_result);
}

883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
#
# Confirm a current WikiName or not.
#
# usage TBCurrentWikiName($uid)
#       returns 1 if a current wikiname.
#       returns 0 if not a current wikiname
#
function TBCurrentWikiName($wikiname)
{
    $query_result =
	DBQueryFatal("SELECT usr_pswd FROM users WHERE wikiname='$wikiname'");

    return mysql_num_rows($query_result);
}

898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
#
# Check to see if an email is being used twice.
#
# usage TBCurrentEmail($email)
#       returns 1 if a valid email for someone.
#       returns 0 if no such email address used by anyone.
#
function TBCurrentEmail($email)
{
    $query_result =
	DBQueryFatal("SELECT uid FROM users WHERE usr_email='$email'");

    return mysql_num_rows($query_result);
}

913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
#
# Get user status,
#
# usage TBUserStatus($uid)
#       returns status if a valid user.
#       returns 0 if not a valid user.
#
function TBUserStatus($uid)
{
    $query_result =
	DBQueryFatal("SELECT status FROM users WHERE uid='$uid'");

    if (! mysql_num_rows($query_result)) {
	return 0;
    }
    $row = mysql_fetch_row($query_result);
    return $row[0];
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
#
# Confirm a valid pid/gid
#
# usage TBValidGroup($pid, $gid)
#       returns 1 if a valid group.
#       returns 0 if not a valid group
#
function TBValidGroup($pid, $gid)
{
    $query_result =
	DBQueryFatal("select pid,gid from groups as g ".
		     "where pid='$pid' and gid='$gid'");

    if (mysql_num_rows($query_result) == 0) {
	return 0;
    }

    #
    # XXX String compare to ensure case match. 
    #
    $row = mysql_fetch_row($query_result);
    if (strcmp($row[0], $pid) ||
	strcmp($row[1], $gid)) {
	return 0;
    }
    return 1;
}

960
961
962
963
964
965
966
967
968
969
970
971
#
# Confirm a valid project.
#
# usage TBValidProject($pid)
#       returns 1 if a valid project.
#       returns 0 if not a valid project.
#
function TBValidProject($pid)
{
    return TBValidGroup($pid, $pid);
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
#
# Confirm a group member.
#
# usage TBGroupMember($uid, $pid, $gid, &$approved)
#       returns 1 if a member.
#       returns 0 if not a member.
#
function TBGroupMember($uid, $pid, $gid, &$approved)
{
    global $TBDB_TRUST_USER;
	
    $approved = 0;
    
    $query_result =
	DBQueryFatal("select trust from group_membership ".
		     "where uid='$uid' and pid='$pid' and gid='$gid'");

    if (mysql_num_rows($query_result) == 0) {
	return 0;
    }
    $row = mysql_fetch_row($query_result);
    $trust = $row[0];

    $approved = TBMinTrust($trust, $TBDB_TRUST_USER);
    return 1;
}

999
1000
#
# Confirm a valid OS Descriptor in the specified project.
For faster browsing, not all history is shown. View entire blame