dbdefs.php3.in 50.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

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

#
# 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;

31
32
$TBDB_OSID_OSIDLEN         = 35;
$TBDB_OSID_OSNAMELEN       = 20;
33
$TBDB_OSID_VERSLEN         = 12;
34
35
$TBDB_IMAGEID_IMAGEIDLEN   = 45;
$TBDB_IMAGEID_IMAGENAMELEN = 30;
36

37
38
39
#
# User status field.
#
40
41
42
43
44
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");
45

Leigh B. Stoller's avatar
Leigh B. Stoller committed
46
47
48
49
50
51
52
53
54
55
56
#
# 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;

57
58
59
60
61
62
63
64
65
#
# 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#
# 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;
89
$TB_EXPT_UPDATE			= 4;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
90
$TB_EXPT_MIN			= $TB_EXPT_READINFO;
91
$TB_EXPT_MAX			= $TB_EXPT_UPDATE;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
92
93
94
95
96

# Projects.
$TB_PROJECT_READINFO		= 1;
$TB_PROJECT_MAKEGROUP		= 2;
$TB_PROJECT_EDITGROUP		= 3;
Chad Barb's avatar
   
Chad Barb committed
97
$TB_PROJECT_GROUPGRABUSERS      = 4;
Chad Barb's avatar
   
Chad Barb committed
98
99
100
101
102
103
104
105
106
107
$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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
$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;

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

Leigh B. Stoller's avatar
Leigh B. Stoller committed
136
137
138
139
140
# Interfaces roles.
define("TBDB_IFACEROLE_CONTROL",	"ctrl");
define("TBDB_IFACEROLE_EXPERIMENT",	"expt");
define("TBDB_IFACEROLE_JAIL",		"jail");
define("TBDB_IFACEROLE_FAKE",		"fake");
141
define("TBDB_IFACEROLE_GW",		"gw");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
142
143
define("TBDB_IFACEROLE_OTHER",		"other");

144
145
146
147
# Node states that the web page cares about.
define("TBDB_NODESTATE_ISUP",		"ISUP");
define("TBDB_NODESTATE_PXEWAIT",	"PXEWAIT");

148
149
150
151
152
153
# 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);

154
155
156
157
# 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
158
159
160
$linktest_levels[2]	= "Plus Static Routing";
$linktest_levels[3]	= "Plus Loss";
$linktest_levels[4]	= "Plus Bandwidth";
161
162
define("TBDB_LINKTEST_MAX", 4);

Leigh B. Stoller's avatar
Leigh B. Stoller committed
163
164
165
166
167
168
169
170
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
#
# 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
282
    global $TB_PROJECT_GROUPGRABUSERS;
Chad Barb's avatar
   
Chad Barb committed
283
    global $TB_PROJECT_BESTOWGROUPROOT;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
284
285
286
287
288
289
290
291
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
    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) {
325
	$mintrust = $TBDB_TRUST_GROUPROOT;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
326
327
328
    }
    elseif ($access_type == $TB_PROJECT_LEADGROUP) {
	#
329
	# Allow mere user (in default group) to lead a subgroup.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
330
	# 
331
	$mintrust = $TBDB_TRUST_USER;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
332
333
334
335
336
337
    }
    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
338
339
    elseif ($access_type == $TB_PROJECT_ADDUSER ||
	    $access_type == $TB_PROJECT_EDITGROUP) {
340
	#
Chad Barb's avatar
   
Chad Barb committed
341
342
	# If user is project_root or group_root in default group, 
	# allow them to add/edit/remove users in any group.
343
	#
Chad Barb's avatar
   
Chad Barb committed
344
345
346
347
348
349
350
	if (TBMinTrust(TBGrpTrust($uid, $pid, $pid), $TBDB_TRUST_GROUPROOT)) {
	    return 1;
	}
	#
	# Otherwise, editing a group requires group_root 
	# in that group.
	#	
351
352
	$mintrust = $TBDB_TRUST_GROUPROOT;
    }
Chad Barb's avatar
   
Chad Barb committed
353
354
355
356
357
358
359
    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
360
	}
Chad Barb's avatar
   
Chad Barb committed
361
362
363
364

	if (strcmp($gid, $pid) == 0)  {
	    #
	    # Only project_root can bestow group_root in default group, and
365
	    # we already established that they are not project_root, so fail.
Chad Barb's avatar
   
Chad Barb committed
366
367
	    #
	    return 0;
368
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
369
	else {
Chad Barb's avatar
   
Chad Barb committed
370
371
372
373
	    #
	    # Non-default group.
	    # group_root in default group may bestow group_root.
	    #
374
375
	    if (TBMinTrust(TBGrpTrust($uid, $pid, $pid),
			   $TBDB_TRUST_GROUPROOT)) {
Chad Barb's avatar
   
Chad Barb committed
376
377
378
379
380
381
		return 1;
	    }

	    #
	    # group_root in the group in question may also bestow group_root.
	    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
382
383
384
	    $mintrust = $TBDB_TRUST_GROUPROOT;
	}
    }
Chad Barb's avatar
   
Chad Barb committed
385
    elseif ($access_type == $TB_PROJECT_GROUPGRABUSERS) {
Chad Barb's avatar
   
Chad Barb committed
386
387
388
389
390
391
	#
	# 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
392
    }
393
394
395
    elseif ($access_type == $TB_PROJECT_DELUSER) {
	$mintrust = $TBDB_TRUST_PROJROOT;
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
396
397
398
399
400
401
402
    else {
	TBERROR("Unexpected access type: $access_type!", 1);
    }

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

Chad Barb's avatar
   
Chad Barb committed
403
404
405
406
407
408
409
410
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500

#
# 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.
	# Don't verify anything (yet.)
	#
	$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];
	
	# 
	# Get what the user's trust level is in the 
	# current subgroup we're looking at.
	#
	$grptrustisroot = 
	    TBTrustConvert( $grptrust ) > $TBDB_TRUST_USER ? 1 : 0;

	#
	# If user's trust level is higher in the default group than in the
	# 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)) {
            #
	    # Iff we're modifying a subgroup, 
	    # Make sure that the trust we're setting is as
	    # 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;
}


501
# Usage: TBExptGroup($pid, $eid, &$gid)
502
#	 returns 0 if expt does not exist.
503
504
505
#        returns 1 if expt exists.
function TBExptGroup($pid, $eid, &$gid)
{
506
507
508
509
510
511
512
513
514
    $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;
515
516
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
517
518
519
520
521
522
523
524
525
526
527
528
#
# 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;
529
    global $TB_EXPT_UPDATE;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
    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 =
551
	DBQueryFatal("SELECT gid,expt_head_uid FROM experiments WHERE ".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
552
553
554
555
556
		     "eid='$eid' and pid='$pid'");
    
    if (mysql_num_rows($query_result) == 0) {
	return 0;
    }
557
558
559
    $row  = mysql_fetch_array($query_result);
    $gid  = $row[gid];
    $head = $row[expt_head_uid];
Leigh B. Stoller's avatar
Leigh B. Stoller committed
560
561
562
563
564
565
566
567

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

568
569
570
571
572
573
574
575
    #
    # 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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
}

#
# 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;
595
    global $TBDB_TRUST_GROUPROOT;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
596
597
598
599
600
601
602
603
604
    global $TBDB_TRUST_LOCALROOT;
    $mintrust;

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

    $query_result =
605
	DBQueryFatal("select e.pid,e.gid from reserved as r ".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
606
		     "left join experiments as e on ".
607
608
		     "     e.pid=r.pid and e.eid=r.eid ".
		     "where r.node_id='$node_id'");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
609
610
611
612

    if (mysql_num_rows($query_result) == 0) {
	return 0;
    }
613
614
615
    $row = mysql_fetch_array($query_result);
    $pid = $row[pid];
    $gid = $row[gid];
Leigh B. Stoller's avatar
Leigh B. Stoller committed
616
617
618
619
620
621
622

    if ($access_type == $TB_NODEACCESS_READINFO) {
	$mintrust = $TBDB_TRUST_USER;
    }
    else {
	$mintrust = $TBDB_TRUST_LOCALROOT;
    }
623
624
    return TBMinTrust(TBGrpTrust($uid, $pid, $gid), $mintrust) ||
	TBMinTrust(TBGrpTrust($uid, $pid, $pid), $TBDB_TRUST_GROUPROOT);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
}

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

649
650
651
652
653
654
655
    #
    # Admins do whatever they want!
    # 
    if (ISADMIN($uid)) {
	return 1;
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
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
    #
    # 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 =
708
	DBQueryFatal("SELECT pid,shared FROM os_info WHERE osid='$osid'");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
709
710
711
712
713
    
    if (mysql_num_rows($query_result) == 0) {
	return 0;
    }
    $row = mysql_fetch_array($query_result);
714
715
    $pid    = $row[pid];
    $shared = $row[shared];
Leigh B. Stoller's avatar
Leigh B. Stoller committed
716
717
718
719

    #
    # Global OSIDs can be read by anyone.
    # 
720
    if ($shared) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
	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;
756
    global $TBDB_TRUST_GROUPROOT;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
    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 =
772
773
	DBQueryFatal("SELECT pid,gid,shared,global FROM images ".
		     "WHERE imageid='$imageid'");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
774
775
776
777
    
    if (mysql_num_rows($query_result) == 0) {
	return 0;
    }
778
779
    $row    = mysql_fetch_array($query_result);
    $shared = $row[shared];
780
    $global = $row["global"];
781
    $pid    = $row[pid];
782
    $gid    = $row[gid];
Leigh B. Stoller's avatar
Leigh B. Stoller committed
783
784

    #
785
    # Global ImageIDs can be read by anyone but written by Admins only.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
786
    # 
787
    if ($global) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
788
789
790
791
792
793
794
795
796
797
798
	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;
799
800
801
802
803
        #
        # Shared imageids are readable by anyone in the project.
        #
	if ($shared)
	    $gid = $pid;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
804
805
806
807
808
    }
    else {
	$mintrust = $TBDB_TRUST_LOCALROOT;
    }

809
810
    return TBMinTrust(TBGrpTrust($uid, $pid, $gid), $mintrust) ||
	TBMinTrust(TBGrpTrust($uid, $pid, $pid), $TBDB_TRUST_GROUPROOT);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
811
812
813
814
815
816
}

#
# 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
817
# usage: TBGroupUnixInfo($pid, $gid, &$unix_gid, &$unix_name)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
818
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
819
function TBGroupUnixInfo($pid, $gid, &$unix_gid, &$unix_name)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
{
    $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];
}

851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
#
# 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);
}

866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
#
# 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);
}

881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
#
# 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
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
#
# 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;
}

928
929
930
931
932
933
934
935
936
937
938
939
#
# 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
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
#
# 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;
}

967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
#
# Confirm a valid OS Descriptor in the specified project.
#
# usage TBValidOS($pid, $imagename)
#       returns 1 if valid
#       returns 0 if not valid
#
function TBValidOS($pid, $osname)
{
    $query_result =
	DBQueryFatal("select osid from os_info where ".
		     "pid='$pid' and osname='$osname'");

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

Leigh B. Stoller's avatar
Leigh B. Stoller committed
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
#
# Confirm a valid osid.
#
# usage TBValidOSID($osid)
#       returns 1 if valid
#       returns 0 if not valid
#
function TBValidOSID($osid)
{
    $query_result =
	DBQueryFatal("select osid from os_info where osid='$osid'");

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

#
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
# Confirm a valid image in the specified project.
#
# usage TBValidImage($pid, $imagename)
#       returns 1 if valid
#       returns 0 if not valid
#
function TBValidImage($pid, $imagename)
{
    $query_result =
	DBQueryFatal("select imageid from images where ".
		     "pid='$pid' and imagename='$imagename'");

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

#
# Confirm a valid imageid (imageids are globally unique).
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
#
# usage TBValidImageID($imageid)
#       returns 1 if valid
#       returns 0 if not valid
#
function TBValidImageID($imageid)
{
    $query_result =
	DBQueryFatal("select imageid from images where imageid='$imageid'");

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

1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
#
# Return info for an imageid.
# 
# usage TBImageInfo($imageid, &$imagename, &$pid)
#       returns 1 if valid
#       returns 0 if not valid
#
function TBImageInfo($imageid, &$imagename, &$pid)
{
    $query_result =
	DBQueryFatal("select * from images where imageid='$imageid'");

    if (mysql_num_rows($query_result) == 0) {
	return 0;
    }
    $row = mysql_fetch_array($query_result);
    $imagename = $row[imagename];
    $pid       = $row[pid];
    return 1;
}

#
# Return info for an osid.
# 
# usage TBOSInfo($osid, &$osname, &$pid)
#       returns 1 if valid
#       returns 0 if not valid
#
function TBOSInfo($osid, &$osname, &$pid)
{
    $query_result =
	DBQueryFatal("select * from os_info where osid='$osid'");

    if (mysql_num_rows($query_result) == 0) {
	return 0;
    }
    $row = mysql_fetch_array($query_result);
    $osname = $row[osname];
    $pid    = $row[pid];
    return 1;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
#
# Confirm a valid experiment.
#
# usage TBValidExperiment($pid, $eid)
#       returns 1 if valid
#       returns 0 if not valid
#
function TBValidExperiment($pid, $eid)
{
    $query_result =
	DBQueryFatal("select eid from experiments ".
		     "where eid='$eid' and pid='$pid'");

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

#
1103
# Confirm a valid node name.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1104
#
1105
# usage TBValidNodeName($node)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1106
1107
1108
#       returns 1 if valid
#       returns 0 if not valid
#
1109
function TBValidNodeName($node)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1110
1111
{
    $query_result =
1112
	DBQueryFatal("select node_id from nodes where node_id='$node'");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1113
1114
1115
1116
1117
1118
1119

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

1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
#
# Confirm a valid node type
#
# usage TBValidNodeType($type)
#       returns 1 if valid
#       returns 0 if not valid
#
function TBValidNodeType($type)
{
    $query_result =
	DBQueryFatal("select type from node_types where type='$type'");

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

1138
1139
1140
1141
1142
1143
#
# Return mail addresses for project_root and group_root people.
#
# usage TBLeaderMailList($pid, $gid)
#       Uses default group (pid=gid) if gid not given.
#       returns string like "Leader Name (uid) <email>[, ...]"
1144
#       returns the empty string if not valid
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
function TBLeaderMailList($pid, $gid)
{
  # No group, then use the default group.
  if (! $gid) { $gid = $pid; }
  
  $query_result =
    DBQueryFatal("select distinct usr_name,u.uid,usr_email from users as u ".
		 "left join group_membership as gm on gm.uid=u.uid ".
		 "where (trust='". TBDB_TRUSTSTRING_PROJROOT .
		 "' and pid='$pid') or (trust='". TBDB_TRUSTSTRING_GROUPROOT .
1155
1156
		 "' and pid='$pid' and gid='$gid') ".
		 "order by trust DESC, usr_name");
1157
1158
  
  if (mysql_num_rows($query_result) == 0) {
1159
    return "";
1160
1161
1162
1163
  }
  $mailstr="";
  while ($row = mysql_fetch_array($query_result)) {
    if ($mailstr != "") { $mailstr .=", "; }
1164
    $mailstr .= '"'.$row[usr_name]." (".$row[uid].")\" <".$row[usr_email].">";
1165
1166
1167
1168
  }
  return $mailstr;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
#
# Return Project Leader.
#
# usage TBProjLeader($pid, &$projleader)
#       returns 1 if valid
#       returns 0 if not valid
#
function TBProjLeader($pid, &$projleader)
{
    $query_result =
	DBQueryFatal("select head_uid from projects where pid='$pid'");

    if (mysql_num_rows($query_result) == 0) {
	return 0;
    }
    $row = mysql_fetch_array($query_result);
    $projleader  = $row[head_uid];
    
    return 1;
}

1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
#
# Return Group Leader.
#
# usage TBGroupLeader($pid, $gid, &$groupleader)
#       returns 1 if valid
#       returns 0 if not valid
#
function TBGroupLeader($pid, $gid, &$groupleader)
{
    $query_result =
	DBQueryFatal("select leader from groups ".
		     "where pid='$pid' and gid='$gid'");

    if (mysql_num_rows($query_result) == 0) {
	return 0;
    }
    $row = mysql_fetch_array($query_result);
    $groupleader  = $row[leader];
    
    return 1;
}

1212
1213
1214
#
# Return Experiment Leader.
#
1215
# usage TBExpLeader($pid, $eid, &$expleader)
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
#       returns 1 if valid
#       returns 0 if not valid
#
function TBExpLeader($pid, $eid, &$expleader)
{
    $query_result =
	DBQueryFatal("select expt_head_uid from experiments ".
		     "where pid='$pid' and eid='$eid'");

    if (mysql_num_rows($query_result) == 0) {
	return 0;
    }
    $row = mysql_fetch_array($query_result);
    $expleader  = $row[expt_head_uid];
    
    return 1;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
1234
1235
1236
1237
1238
1239
#
# Return a list of projects for which the uid is allowed to perform the
# requested operation. Only a couple of access options are supported since
# to be totally general would require too many DB operations. Not worth
# the effort or the expense.
#
1240
1241
# Returns an indexed array (by pid) of lists (of gids).
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1242
1243
1244
1245
1246
function TBProjList($uid, $access_type)
{
    global $TB_PROJECT_CREATEEXPT;
    global $TB_PROJECT_MAKEOSID;
    global $TB_PROJECT_MAKEIMAGEID;
1247
    global $TB_PROJECT_MAKEGROUP;
1248
    global $TB_PROJECT_READINFO;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1249
    $result = array();
1250
    $user_clause = "where uid='$uid' and";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1251
    
1252
1253
1254
1255
    if ($access_type == $TB_PROJECT_READINFO) {
	$trust_clause = "trust!='none'";
    }
    elseif ($access_type == $TB_PROJECT_MAKEGROUP) {
1256
1257
	$trust_clause = "trust='project_root'";
    }
1258
    elseif ($access_type == $TB_PROJECT_CREATEEXPT) {
1259
1260
1261
	$trust_clause = "(trust='project_root' or trust='group_root' or ".
	                " trust='local_root')";
    }
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
    elseif ($access_type == $TB_PROJECT_MAKEOSID ||
	    $access_type == $TB_PROJECT_MAKEIMAGEID) {
        if (ISADMIN($uid)) {
            $user_clause = "";
	}
        else {
  	    $trust_clause = "(trust='project_root' or trust='group_root' or ".
	                    " trust='local_root')";
        }
    }
1272
    else {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1273
1274
1275
1276
	TBERROR("Invalid access type $access_type!", 1);
    }
    
    $query_result =
1277
	DBQueryFatal("SELECT distinct pid,gid FROM group_membership ".
1278
		     "$user_clause $trust_clause order by pid");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1279
1280
1281
1282
1283
1284
1285

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

    $i = 0;
    while ($row = mysql_fetch_array($query_result)) {
1286
1287
1288
1289
	$pid = $row['pid'];
	$gid = $row['gid'];
	
	$result[$pid][] = $gid;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1290
1291
1292
1293
1294
1295

	$i++;
    }
    return $result;
}

1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
function TBExptState($pid, $eid)
{
    $query_result =
	DBQueryFatal("select state from experiments ".
		     "where eid='$eid' and pid='$pid'");

    if (mysql_num_rows($query_result) == 0) {
	return 0;
    }
    $row   = mysql_fetch_array($query_result);
    $state = $row[state];
    return $state;
}

1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
function TBExptIndex($pid, $eid)
{
    $query_result =
	DBQueryFatal("select idx from experiments ".
		     "where eid='$eid' and pid='$pid'");

    if (mysql_num_rows($query_result) == 0) {
	return -1;
    }
    $row   = mysql_fetch_array($query_result);
    $idx   = $row[idx];
    return $idx;
}

1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
function TBrsrcIndex($pid, $eid)
{
    $query_result =
	DBQueryFatal("select s.rsrcidx from experiments as e ".
		     "left join experiment_stats as s on s.exptidx=e.idx ".
		     "where e.pid='$pid' and e.eid='$eid'");

    if (mysql_num_rows($query_result) == 0) {
	return -1;
    }
    $row   = mysql_fetch_array($query_result);
    $idx   = $row[rsrcidx];
    return $idx;
}

1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
function TBUidNodeLastLogin($uid)
{
    $query_result =
	DBQueryFatal("select * from uidnodelastlogin where uid='$uid'");

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

#
# Return the last login for the node, but only after the experiment was
# created (swapped in).
# 
function TBNodeUidLastLogin($node_id)
{
    $query_result =
	DBQueryFatal("select n.* from nodeuidlastlogin as n ".
		     "left join reserved as r on n.node_id=r.node_id ".
		     "left join experiments as e ".
		     " on r.eid=e.eid and r.pid=e.pid ".
		     "where r.node_id='$node_id' and ".
		     "  DATE_ADD(n.date, INTERVAL n.time HOUR_SECOND) >= ".
		     "           e.expt_swapped");

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

1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
#
# Return the last login for the users node.
# 
function TBUsersLastLogin($uid)
{
    $query_result =
	DBQueryFatal("select * from userslastlogin where uid='$uid'");

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

1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
#
# Return number of events in an experiment.
#
function TBEventCount($pid, $eid)
{
    $query_result =
	DBQueryFatal("select count(*) from eventlist ".
		     "where pid='$pid' and eid='$eid'");

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

1404
1405
1406
1407
1408
1409
1410
#
# Return the last login for all of the nodes in an experiment, but only
# after the experiment was created (swapped in).
# 
function TBExpUidLastLogins($pid, $eid)
{
    $query_result =
1411
1412
	DBQueryFatal("select n.*, date_format(n.date,'%c/%e') as shortdate, ".
		     "to_days(now())-to_days(n.date) as daysidle ".
1413
	             "from nodeuidlastlogin as n ".
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
		     "left join reserved as r on n.node_id=r.node_id ".
		     "left join experiments as e ".
		     " on r.eid=e.eid and r.pid=e.pid ".
		     "where r.pid='$pid' and r.eid='$eid' and ".
		     "  DATE_ADD(n.date, INTERVAL n.time HOUR_SECOND) >= ".
		     "           e.expt_swapped ".
		     "      order by n.date DESC");

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

1429
#
1430
1431
1432
# Number of globally free PCs. See ptopgen and libdb for corresponding
# usage of the eventstate. A node is not really considered free for use
# unless it is also in the ISUP/PXEWAIT state.
1433
1434
1435
1436
#
function TBFreePCs()
{
    $query_result =
1437
	DBQueryFatal("select count(a.node_id) from nodes as a ".
1438
1439
		     "left join reserved as b on a.node_id=b.node_id ".
		     "left join node_types as nt on a.type=nt.type ".
1440
1441
		     "left join nodetypeXpid_permissions as p ".
		     "  on a.type=p.type ".
1442
		     "where b.node_id is null and a.role='testnode' ".
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
		     "  and nt.class = 'pc' and p.pid is null and ".
                     "      (a.eventstate='" . TBDB_NODESTATE_ISUP . "' or ".
                     "       a.eventstate='" . TBDB_NODESTATE_PXEWAIT . "') and".
		     "      (p.pid is null)");
    
    if (mysql_num_rows($query_result) == 0) {
	return 0;
    }
    $row = mysql_fetch_row($query_result);
    return $row[0];
1453
1454
}

1455
1456
1457
#
# Number of PCs a user has reserved to his experiments.
#
1458
1459
1460
1461
1462
1463
1464
1465
1466
function TBUserPCs($uid)
{
    $query_result =
	DBQueryFatal("select a.node_id from nodes as a ".
		     "left join reserved as b on a.node_id=b.node_id ".
		     "left join node_types as nt on a.type=nt.type ".
                     "left join experiments as e on b.pid=e.pid and ".
                     " b.eid=e.eid ".
		     "where e.expt_head_uid='$uid' and e.pid!='emulab-ops' ".
1467
		     "  and a.role='testnode' and nt.class = 'pc'");
1468
1469
1470
1471

    return mysql_num_rows($query_result);
}

1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
function TBExptLogFile($pid, $eid)
{
    $query_result =
	DBQueryFatal("select logfile from experiments ".
		     "where pid='$pid' and eid='$eid' and ".
		     " logfile is not null");

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

1486
1487
1488
1489
1490
1491
1492
1493
function TBWebdbAllowed($uid) {
    $query_result =
        DBQueryFatal("select dbedit from users ".
		     "WHERE uid='$uid' and dbedit=1");

    return mysql_num_rows($query_result);
}

1494
1495
1496
1497
1498
1499
1500
1501
function TBCvswebAllowed($uid) {
    $query_result =
        DBQueryFatal("select cvsweb from users ".
		     "WHERE uid='$uid' and cvsweb=1");

    return mysql_num_rows($query_result);
}

1502
1503
1504
1505
1506
1507
1508
1509
1510
#
# Returns > 0 if a node has a serial console, 0 if it does not
#
function TBHasSerialConsole($node_id) {
    $query_result =
	DBQueryFatal("select tipname from tiplines WHERE node_id='$node_id'");
    return mysql_num_rows($query_result);
}

1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
#
# Generate a verification key, and stash it in the database
#
function TBGenVerificationKey($name) {
    $key = md5(uniqid(rand(),1));
    DBQueryFatal("update users set verify_key='$key' where uid='$name'");
    return $key;
}

#
# Get a verification key from the database
#
function TBGetVerificationKey($name) {
    $query_result =
	DBQueryFatal("select verify_key from users where uid='$name'");

    if (mysql_num_rows($query_result) == 0) {
	# Can we signal error somehow?
	return "";
    }
    $row   = mysql_fetch_array($query_result);
    return $row[verify_key];
}

1535
1536
1537
1538
1539
1540
1541
1542
#
# Map IP to nodeid.
#
function TBIPtoNodeID($ip) {
    $query_result =
	DBQueryFatal("select n.node_id from interfaces as i ".
		     "left join nodes as n on n.node_id=i.node_id ".
		     "left join node_types as t ".
1543
		     "   on t.type=n.type and i.iface=t.control_iface ".
1544
1545
1546
1547
1548
1549
1550
1551
1552
		     "where i.IP='$ip'");

    if (mysql_num_rows($query_result) == 0) {
	return 0;
    }
    $row   = mysql_fetch_array($query_result);
    return $row[node_id];
}

1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
#
# Map nodeid to pid,eid,gid. Return 0 if not reserved. 
#
function TBNodeIDtoExpt($nodeid, &$pid, &$eid, &$gid) {
    $query_result =
	DBQueryFatal("select r.pid,r.eid,e.gid from reserved as r ".
		     "left join experiments as e on r.pid=e.pid and ".
		     "     r.eid=e.eid where node_id='$nodeid'");

    if (mysql_num_rows($query_result) == 0) {
	return 0;
    }
    $row   = mysql_fetch_array($query_result);
    $pid   = $row["pid"];
    $eid   = $row["eid"];
    $gid   = $row["gid"];
    return 1;
}

Timothy Stack's avatar
   
Timothy Stack committed
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
#
# Get the class of a node.
#
function TBNodeClass($nodeid) {
    $query_result =
	DBQueryFatal("select nt.class from nodes as n ".
		     "left join node_types as nt on nt.type=n.type ".
		     "  where n.node_id='$nodeid'");

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

    $row = mysql_fetch_array($query_result);
    return $row["class"];
}

#
# Get the status of a node.
#
function TBNodeStatus($nodeid) {
    $query_result =
	DBQueryFatal("select status from nodes as n where node_id='$nodeid'");

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

    $row = mysql_fetch_array($query_result);
    return $row["status"];
}

1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
#
# Functions related to idle times
#

$lastact_query = "greatest(last_tty_act,last_net_act,last_cpu_act,".
                 "last_ext_act)";

#
# Gets the time of last activity for a node
#
function TBGetNodeLastAct($pc) {
    global $lastact_query;
    $query_result =
Mac Newbold's avatar
Mac Newbold committed
1617
	DBQueryWarn("select $lastact_query as last_act from node_activity ".
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
		    "where node_id='$pc'");

    if (mysql_num_rows($query_result) == 0) {
	return -1;
    }
    $row   = mysql_fetch_array($query_result);
    return $row[last_act];
}

#
1628
# Gets the time of idleness for a node, in hours by default (ie '2.75')
1629
#
1630
function TBGetNodeIdleTime($pc, $format = 0) {
1631
1632
1633
    global $lastact_query;
    $query_result =
	DBQueryWarn("select (unix_timestamp(now()) - unix_timestamp( ".
1634
		    "$lastact_query)) as idle_time from node_activity ".
1635
1636
1637
1638
1639
1640
1641
1642
		    "where node_id='$pc'");

    if (mysql_num_rows($query_result) == 0) {
	return -1;
    }
    $row   = mysql_fetch_array($query_result);
    $t = $row[idle_time];
    # if it is less than 5 minutes, it is not idle at all...
1643
1644
1645
1646
1647
1648
1649
    $t = ($t < 300 ? 0 : $t);
    if (!$format) {
	$t = round($t/3600,2);
    } else {
	$t = date($format,mktime(0,0,$t));
    }
    return $t;
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
}

#
# Find out if a node's idle reports are stale
#
function TBGetNodeIdleStale($pc) {
    global $lastact_query;

    # We currently have a 5 minute interval for slothd between reports
    # So give some slack in case a node reboots without reporting for a while
    $staletime = 10; # in minutes
    $stalesec = 60 * $staletime;
    $query_result =
	DBQueryWarn("select (unix_timestamp(now()) - ".
		    "unix_timestamp(last_report )) as t ".
		    "from node_activity where node_id='$pc'");

    if (mysql_num_rows($query_result) == 0) {
	return -1;
    }
    $row   = mysql_fetch_array($query_result);
    return ($row[t]>$stalesec);
}

#
# Gets the time of last activity for an expt
#
1677
function TBGetExptLastAct($pid, $eid) {
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
    global $lastact_query;
    $query_result =
	DBQueryWarn("select max($lastact_query) as last_act ".
		    "from node_activity as na ".
		    "left join reserved as r on na.node_id=r.node_id ".
		    "where pid='$pid' and eid='$eid' ".
		    "group by pid,eid");

    if (mysql_num_rows($query_result) == 0) {
	return -1;
    }
    $row   = mysql_fetch_array($query_result);
    return $row[last_act];
}

#
# Gets the time of idleness for an expt, in hours
#
1696
function TBGetExptIdleTime($pid, $eid, $format = 0) {
1697
1698
1699
    global $lastact_query;
    $query_result =
	DBQueryWarn("select (unix_timestamp(now()) - unix_timestamp( ".
1700
		    "max($lastact_query))) as idle_time ".
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
		    "from node_activity as na ".
		    "left join reserved as r on na.node_id=r.node_id ".
		    "where pid='$pid' and eid='$eid' ".
		    "group by pid,eid");

    if (mysql_num_rows($query_result) == 0) {
	return -1;
    }
    $row   = mysql_fetch_array($query_result);
    $t = $row[idle_time];
    # if it is less than 5 minutes, it is not idle at all...
1712
1713
1714
1715
    $t = ($t < 300 ? 0 : $t);
    if (!$format) {
	$t = round($t/3600,2);
    } else {
1716
	$t = date($format,mktime(0,0,$t,1,1));
1717
1718
    }
    return $t;
1719
1720
1721
1722
1723
}

#
# Find out if an expt's idle report is stale
#
1724
function TBGetExptIdleStale($pid, $eid) {
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
    global $lastact_query;

    # We currently have a 5 minute interval for slothd between reports
    # So give some slack in case a node reboots without reporting for a while
    $staletime = 10; # in minutes
    $stalesec = 60 * $staletime;
    $query_result =
	DBQueryWarn("select (unix_timestamp(now()) - ".
		    "unix_timestamp(min(last_report) )) as t ".
		    "from node_activity as na ".
		    "left join reserved as r on na.node_id=r.node_id ".
		    "where pid='$pid' and eid='$eid' ".
		    "group by pid,eid");

    if (mysql_num_rows($query_result) == 0) {
	return -1;
    }
    $row   = mysql_fetch_array($query_result);
    return ($row[t]>$stalesec);
}

#
# Find out if an expt's idleness should be ignored
#
function TBGetExptIdleIgnore($pid,$eid) {
    global $lastact_query;

    $query_result =
	DBQueryWarn("select idle_ignore from experiments ".
		    "where pid='$pid' and eid='$eid'");

    if (mysql_num_rows($query_result) == 0) {
	return 0;
    }
    $row   = mysql_fetch_array($query_result);
    return $row[idle_ignore];
}

1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
#
# Get the default user interface that has been selected for the project.
#
function TBGetDefaultProjectUserInterface($pid) {
    $query_result =
	DBQueryWarn("select default_user_interface from projects ".
		    "where pid='$pid'");

    if (mysql_num_rows($query_result) == 0) {
	return 0;
    }
    $row   = mysql_fetch_array($query_result);
    return $row[0];
}
Chad Barb's avatar
   
Chad Barb committed
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829

#
# Check if a site-specific variable exists. 
#
# usage: TBSiteVarExists($name)
#        returns 1 if variable exists;
#        returns 0 otherwise.
#
function TBSiteVarExists($name)
{
    global $lastact_query;

    $name  = addslashes( $name );

    $query_result = 
	DBQueryFatal("select name from sitevariables ".
		     "where name='$name'");

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

#
# Get site-specific variable.
# Get the value of the variable, or the default value if
# the value is undefined (NULL).
#
# usage: TBGetSiteVar($name)
#        returns value if variable is defined;
#        dies otherwise.
#
function TBGetSiteVar($name)
{
    global $lastact_query;

    $name  = addslashes( $name );

    $query_result = 
	DBQueryFatal("select value, defaultvalue from sitevariables ".
		     "where name='$name'");

    if (mysql_num_rows($query_result) > 0) {    
	$row = mysql_fetch_array($query_result);

	$value = $row[value];
	$defaultvalue = $row[defaultvalue];
	if (isset($value)) { return $value; }
	if (isset($defaultvalue)) { return $defaultvalue; }
    }
    
1830
    TBERROR("Attempted to fetch unknown site variable '$name'!", 1);
Chad Barb's avatar
   
Chad Barb committed
1831
1832
}

1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
#
# Count available planetlab nodes.
#
# usage: TBPlabAvail()
#        returns the number of free PlanetLab nodes of each type
#        returns an empty array on error
#
function TBPlabAvail() {
    $types = array();
    #
    # We have to do this in two queries, due to the fact that we do pcplabtypes
    # different from the way we do other types (it's on the vnodes, not in the
    # node_autypes for the pnode.)
    #
    # XXX - hardcodes hwdown and emulab-ops
    #
    $tables = "nodes AS n " .
	      "LEFT JOIN widearea_nodeinfo AS w ON n.phys_nodeid = w.node_id " .
              "LEFT JOIN node_auxtypes AS na ON n.node_id = na.node_id " .
	      "LEFT JOIN reserved AS r ON n.phys_nodeid = r.node_id " .
	      "LEFT JOIN node_status AS ns ON n.phys_nodeid = ns.node_id " .
	      "LEFT JOIN node_features AS nf ON n.phys_nodeid = nf.node_id " .
	      "LEFT JOIN node_features AS nf2 ON n.phys_nodeid = nf2.node_id";
    $available = "ns.status='up' AND (nf.feature='+load' AND nf.weight < 1.0) " .
                 "AND (nf2.feature='+disk' and nf2.weight < 1.0) " .
		 "AND !(r.pid = 'emulab-ops' and r.eid = 'hwdown')";

    #
    # Grab pcplab nodes
    #
    $query_result = DBQueryFatal("SELECT count(*), count(distinct w.site) " .
                                 "FROM $tables " .
				 "WHERE (n.type='pcplabphys') ".
				 "    AND($available)");
    if (mysql_num_rows($query_result)) {
	$row = mysql_fetch_row($query_result);
	$types['pcplab'] = array($row[0],$row[1]);
    }

    #
    # Grab the more specific types
    #
    $query_result = DBQueryFatal("SELECT na.type, count(*), " .
				 "count(distinct w.site) FROM $tables " .
				 "WHERE (n.type='pcplabphys') " .
				 "    AND ($available) " .
				 "GROUP BY na.type");
    while ($row = mysql_fetch_row($query_result)) {
	$types[$row[0]] = array($row[1],$row[2]);
    }

    return $types;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
#
# Is an experiment firewalled.
# 
function TBExptFirewall($pid, $eid) {
    #
    # Short form: is there a firewall?
    # Only check the firewalls table so that we can be called for a swapped
    # experiment (swapped experiments don't have reserved table info).
    #
    $query_result =
	DBQueryWarn("SELECT eid FROM firewalls ".
		    "WHERE pid='$pid' and eid='$eid' ".
		    "AND type LIKE '%-vlan'");
    if (!$query_result || !mysql_num_rows($query_result))
	return 0;
   
    return 1;
}

1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
#
# See if user has enough permission to view the webcams. If not an admin
# person, then must be a project with permission to use the robots.
# Eventually this needs to be a much more restrictive test.
#
function TBWebCamAllowed($uid) {
    $query_result =
	DBQueryFatal("select distinct class from group_membership as g ".
		     "left join nodetypeXpid_permissions as p on g.pid=p.pid ".
		     "left join node_types as nt on nt.type=p.type ".
		     "where uid='$uid' and class='robot'");
    return mysql_num_rows($query_result);
}

1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
#
# Return lockeddown bit
#
function TBExptLockedDown($pid, $eid)
{
    $query_result =
	DBQueryFatal("select lockdown from experiments ".
		     "where pid='$pid' and eid='$eid'");

    if (!$query_result || !mysql_num_rows($query_result))
	return 0;

    $row = mysql_fetch_array($query_result);
    return $row[0];
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
1936
1937
1938
#
# DB Interface.
#
1939
1940
$maxtries = 3;
$DBlinkid = 0;
1941
while ($maxtries) {
1942
    $DBlinkid = mysql_connect("localhost", basename($SCRIPT_NAME));
1943
1944
1945
    if ($DBlinkid) {
	break;
    }
1946
1947
1948
1949
1950
1951
1952
1953
1954
    $maxtries--;
    sleep(1);
}
if (! $DBlinkid) {
    TBERROR("Could not connect to mysqld after several attempts!", 1);
}
if (!mysql_select_db($TBDBNAME)) {
    TBERROR("Could not select DB after connecting!", 1);
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981

#
# Record last DB error string.
#
$DBErrorString = "";

#
# This mirrors the routine in the PERL code. The point is to avoid
# writing the same thing repeatedly, get consistent error handling,
# and make sure that mail is sent to the testbed list when things go
# wrong!
#
# Argument is a string. Returns the actual query object, so it is up to
# the caller to test it. I would not for one moment view this as
# encapsulation of the DB interface. 
# 
# usage: DBQuery(char *str)
#        returns the query object result.
#
# Sets $DBErrorString is case of error; saving the original query string and
# the error string from the DB module. Use DBFatal (below) to print/email
# that string, and then exit.
#
function DBQuery($query)
{
    global	$TBDBNAME;
    global	$DBErrorString;
1982
    global      $DBlinkid;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1983
    
1984
    $result = mysql_query($query, $DBlinkid);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009