libdb.pm.in 119 KB
Newer Older
1
2
#!/usr/bin/perl -w

Leigh B. Stoller's avatar
Leigh B. Stoller committed
3
4
#
# EMULAB-COPYRIGHT
5
# Copyright (c) 2000-2004 University of Utah and the Flux Group.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
6
7
8
# All rights reserved.
#

9
#
10
11
# A library of useful DB stuff. Mostly things that get done a lot.
# Saves typing.
12
13
14
15
16
#
# XXX: The notion of "uid" is a tad confused. A unix uid is a number,
#      while in the DB a user uid is a string (equiv to unix login).
#      Needs to be cleaned up.
#
17

18
package libdb;
19
use strict;
20
use Exporter;
Mac Newbold's avatar
Mac Newbold committed
21
use vars qw(@ISA @EXPORT);
22
23
@ISA = "Exporter";
@EXPORT =
24
    qw ( NODERELOADING_PID NODERELOADING_EID NODEDEAD_PID NODEDEAD_EID
25
	 OLDRESERVED_PID OLDRESERVED_EID NFREELOCKED_PID NFREELOCKED_EID 
26
27
	 NODEBOOTSTATUS_OKAY NODEBOOTSTATUS_FAILED NODEBOOTSTATUS_UNKNOWN
	 NODESTARTSTATUS_NOSTATUS PROJMEMBERTRUST_NONE PROJMEMBERTRUST_USER
Leigh B. Stoller's avatar
Leigh B. Stoller committed
28
29
30
	 PROJMEMBERTRUST_ROOT PROJMEMBERTRUST_GROUPROOT
	 PROJMEMBERTRUST_PROJROOT

31
	 PROJROOT GROUPROOT USERROOT TBOPSPID 
32
33
	 PLABMOND_PID PLABMOND_EID PLABHOLDING_PID PLABHOLDING_EID

Leigh B. Stoller's avatar
Leigh B. Stoller committed
34
35
36
37
	 TBTrustConvert TBMinTrust TBGrpTrust TBProjTrust

	 TB_NODEACCESS_READINFO TB_NODEACCESS_MODIFYINFO
	 TB_NODEACCESS_LOADIMAGE TB_NODEACCESS_REBOOT
38
39
	 TB_NODEACCESS_POWERCYCLE TB_NODEACCESS_MODIFYVLANS
	 TB_NODEACCESS_MIN TB_NODEACCESS_MAX
Leigh B. Stoller's avatar
Leigh B. Stoller committed
40

41
42
	 NODEFAILMODE_FATAL NODEFAILMODE_NONFATAL NODEFAILMODE_IGNORE

Leigh B. Stoller's avatar
Leigh B. Stoller committed
43
44
45
	 TB_USERINFO_READINFO TB_USERINFO_MODIFYINFO
	 TB_USERINFO_MIN TB_USERINFO_MAX

46
47
	 USERSTATUS_ACTIVE USERSTATUS_FROZEN
	 USERSTATUS_UNAPPROVED USERSTATUS_UNVERIFIED USERSTATUS_NEWUSER
48

49
	 TB_EXPT_READINFO TB_EXPT_MODIFY TB_EXPT_DESTROY TB_EXPT_UPDATE
Leigh B. Stoller's avatar
Leigh B. Stoller committed
50
51
52
53
	 TB_EXPT_MIN TB_EXPT_MAX

	 TB_PROJECT_READINFO TB_PROJECT_MAKEGROUP
	 TB_PROJECT_EDITGROUP TB_PROJECT_DELGROUP
Chad Barb's avatar
   
Chad Barb committed
54
	 TB_PROJECT_GROUPGRABUSERS TB_PROJECT_BESTOWGROUPROOT
Leigh B. Stoller's avatar
Leigh B. Stoller committed
55
56
57
58
59
60
61
62
63
64
65
	 TB_PROJECT_LEADGROUP TB_PROJECT_ADDUSER
	 TB_PROJECT_DELUSER TB_PROJECT_MAKEOSID
	 TB_PROJECT_DELOSID TB_PROJECT_MAKEIMAGEID TB_PROJECT_DELIMAGEID
	 TB_PROJECT_CREATEEXPT TB_PROJECT_MIN TB_PROJECT_MAX

	 TB_OSID_READINFO TB_OSID_CREATE
	 TB_OSID_DESTROY TB_OSID_MIN TB_OSID_MAX

	 TB_IMAGEID_READINFO TB_IMAGEID_MODIFYINFO
	 TB_IMAGEID_CREATE TB_IMAGEID_DESTROY
	 TB_IMAGEID_ACCESS TB_IMAGEID_MIN TB_IMAGEID_MAX
66

Leigh B. Stoller's avatar
Leigh B. Stoller committed
67
	 DBLIMIT_NSFILESIZE NODERELOADPENDING_EID
68

69
	 EXPTSTATE_NEW EXPTSTATE_PRERUN EXPTSTATE_SWAPPED EXPTSTATE_SWAPPING
70
71
72
73
74
75
	 EXPTSTATE_ACTIVATING EXPTSTATE_ACTIVE
	 EXPTSTATE_TERMINATING EXPTSTATE_TERMINATED EXPTSTATE_QUEUED
	 EXPTSTATE_MODIFY_PARSE EXPTSTATE_MODIFY_REPARSE EXPTSTATE_MODIFY_RESWAP
	 EXPTSTATE_RESTARTING
	 BATCHSTATE_LOCKED BATCHSTATE_UNLOCKED
	 EXPTCANCEL_CLEAR EXPTCANCEL_TERM EXPTCANCEL_SWAP
76

77
	 TBSetCancelFlag TBGetCancelFlag
Leigh B. Stoller's avatar
Leigh B. Stoller committed
78

Mac Newbold's avatar
Mac Newbold committed
79
	 TB_NODELOGTYPE_MISC TB_NODELOGTYPES TB_DEFAULT_NODELOGTYPE
80
81

	 TB_DEFAULT_RELOADTYPE TB_RELOADTYPE_FRISBEE TB_RELOADTYPE_NETDISK
82

83
84
	 TB_EXPTPRIORITY_LOW TB_EXPTPRIORITY_HIGH

85
	 TB_ASSIGN_TOOFEWNODES TB_OPSPID
86

87
	 TBDB_TBEVENT_NODESTATE TBDB_TBEVENT_NODEOPMODE TBDB_TBEVENT_CONTROL
88
	 TBDB_TBEVENT_COMMAND
Chad Barb's avatar
   
Chad Barb committed
89

90
	 TBDB_NODESTATE_ISUP TBDB_NODESTATE_REBOOTING TBDB_NODESTATE_REBOOTED
91
	 TBDB_NODESTATE_SHUTDOWN TBDB_NODESTATE_BOOTING TBDB_NODESTATE_TBSETUP
92
	 TBDB_NODESTATE_RELOADSETUP TBDB_NODESTATE_RELOADING
93
94
	 TBDB_NODESTATE_RELOADDONE TBDB_NODESTATE_UNKNOWN
	 TBDB_NODESTATE_PXEWAIT TBDB_NODESTATE_PXEWAKEUP
95
	 TBDB_NODESTATE_PXEBOOTING TBDB_NODESTATE_ALWAYSUP
Chad Barb's avatar
   
Chad Barb committed
96

97
98
	 TBDB_NODEOPMODE_NORMAL TBDB_NODEOPMODE_DELAYING
	 TBDB_NODEOPMODE_UNKNOWNOS TBDB_NODEOPMODE_RELOADING
99
	 TBDB_NODEOPMODE_NORMALv1 TBDB_NODEOPMODE_MINIMAL
100
101
	 TBDB_NODEOPMODE_RELOAD TBDB_NODEOPMODE_RELOADMOTE
	 TBDB_NODEOPMODE_DELAY
102
	 TBDB_NODEOPMODE_BOOTWHAT
103
	 TBDB_NODEOPMODE_ANY
104
	 TBDB_NODEOPMODE_UNKNOWN
Chad Barb's avatar
   
Chad Barb committed
105

106
	 TBDB_COMMAND_REBOOT
107
108
	 TBDB_COMMAND_POWEROFF TBDB_COMMAND_POWERON TBDB_COMMAND_POWERCYCLE

109
110
111
	 TBDB_STATED_TIMEOUT_REBOOT TBDB_STATED_TIMEOUT_NOTIFY
	 TBDB_STATED_TIMEOUT_CMDRETRY

Chad Barb's avatar
   
Chad Barb committed
112
113
114
	 TBDB_ALLOCSTATE_FREE_CLEAN TBDB_ALLOCSTATE_FREE_DIRTY
	 TBDB_ALLOCSTATE_DOWN TBDB_ALLOCSTATE_RELOAD_TO_FREE
	 TBDB_ALLOCSTATE_RELOAD_PENDING TBDB_ALLOCSTATE_RES_RELOAD
Mac Newbold's avatar
Mac Newbold committed
115
116
	 TBDB_ALLOCSTATE_RES_INIT_DIRTY TBDB_ALLOCSTATE_RES_INIT_CLEAN
	 TBDB_ALLOCSTATE_RES_REBOOT_DIRTY TBDB_ALLOCSTATE_RES_REBOOT_CLEAN
Chad Barb's avatar
   
Chad Barb committed
117
	 TBDB_ALLOCSTATE_RES_READY TBDB_ALLOCSTATE_UNKNOWN
118
	 TBDB_ALLOCSTATE_RES_TEARDOWN TBDB_ALLOCSTATE_DEAD
119
	 TBDB_ALLOCSTATE_RES_RECONFIG
Chad Barb's avatar
   
Chad Barb committed
120

121
122
	 TBDB_STATS_PRELOAD TBDB_STATS_START TBDB_STATS_TERMINATE
	 TBDB_STATS_SWAPIN TBDB_STATS_SWAPOUT TBDB_STATS_SWAPMODIFY
123
	 TBDB_STATS_FLAGS_IDLESWAP TBDB_STATS_FLAGS_PREMODIFY
124
	 TBDB_STATS_FLAGS_START
125

126
127
	 TBDB_JAILIPBASE TBDB_JAILIPMASK

128
	 TBDB_RSRVROLE_NODE TBDB_RSRVROLE_VIRTHOST TBDB_RSRVROLE_DELAYNODE
129
	 TBDB_RSRVROLE_SIMHOST
130

131
	 TBDB_EXPT_WORKDIR
132
	 TBSetNodeEventState TBGetNodeEventState
Chad Barb's avatar
   
Chad Barb committed
133
	 TBSetNodeAllocState TBGetNodeAllocState
134
	 TBSetNodeOpMode TBGetNodeOpMode TBSetNodeNextOpMode
135
	 TB_OSID_MBKERNEL TB_OSID_PXEBOOT TB_OSID_FRISBEE
Mac Newbold's avatar
Mac Newbold committed
136
	 TB_OSID_FREEBSD_MFS TB_OSID_FRISBEE_MFS
137
	 TBBootWhat TBNodeStateTimeout
Mac Newbold's avatar
Mac Newbold committed
138
	 TBDB_TBCONTROL_RESET TBDB_TBCONTROL_RELOADDONE
139
	 TBDB_TBCONTROL_TIMEOUT TBDB_NO_STATE_TIMEOUT
Mac Newbold's avatar
Mac Newbold committed
140
141
	 TBDB_TBCONTROL_PXEBOOT TBDB_TBCONTROL_BOOTING
	 TBDB_TBCONTROL_CHECKGENISUP
142

143
144
	 TBDB_LOWVPORT TBDB_MAXVPORT TBDB_PORTRANGE

145
146
	 TBDB_PHYSICAL_NODE_TABLES

Leigh B. Stoller's avatar
Leigh B. Stoller committed
147
148
	 TBAdmin TBProjAccessCheck TBNodeAccessCheck TBOSIDAccessCheck
	 TBImageIDAccessCheck TBExptAccessCheck ExpLeader MarkNodeDown
149
	 SetNodeBootStatus OSFeatureSupported IsShelved NodeidToExp NodeidToExpOldReserved
150
	 UserDBInfo DBQuery DBQueryFatal DBQueryWarn DBWarn DBFatal
151
	 DBQuoteSpecial UNIX2DBUID ExpState SetExpState ProjLeader
152
	 ExpNodes ExpNodesOldReserved DBDateTime DefaultImageID GroupLeader TBGroupUnixInfo
153
	 TBValidNodeLogType TBValidNodeName TBSetNodeLogEntry
154
	 TBSetSchedReload MapNodeOSID TBLockExp TBUnLockExp TBSetExpSwapTime
155
	 TBUnixGroupList TBOSID TBOSMaxConcurrent TBOSCountInstances
156
	 TBResolveNextOSID TBOsidToPid
157
	 TBOSLoadMaxOkay TBImageLoadMaxOkay TBImageID ExpSwapper
158
	 TBdbfork VnameToNodeid TBExpLocked
159
	 TBIsNodeRemote TBExptSetLogFile TBExptClearLogFile TBExptGetLogFile
160
	 TBIsNodeVirtual TBControlNetIP TBPhysNodeID
161
	 TBExptOpenLogFile TBExptCloseLogFile TBExptCreateLogFile
162
	 TBNodeUpdateAccountsByPid TBNodeUpdateAccountsByType
163
	 TBNodeUpdateAccountsByUID
164
	 TBSaveExpLogFiles TBExptWorkDir TBExptUserDir TBExptLogDir
165
	 TBExptDestroy TBIPtoNodeID TBNodeBootReset TBNodeStateWait
166
	 TBLeaderMailList ExpGroup TBExptSetSwapUID TBExptSetThumbNail
167
	 TBNodeAllocCheck TBPlabNodeUsername MarkPhysNodeDown TBExptIsElabInElab
168
	 TBExptFirewall TBNodeFirewall TBSetExptFirewallVlan
169

170
171
	 TBNodeType TBNodeTypeProcInfo

Mac Newbold's avatar
Mac Newbold committed
172
	 TBExptRemoveVirtualState TBExptBackupVirtualState
Chad Barb's avatar
   
Chad Barb committed
173
174
	 TBExptRestoreVirtualState

Mac Newbold's avatar
Mac Newbold committed
175
	 TBExptRemovePhysicalState TBExptBackupPhysicalState
176
	 TBExptRestorePhysicalState TBExptClearBackupState
Chad Barb's avatar
   
Chad Barb committed
177

178
179
	 TBExptPortRange

180
	 TBDB_WIDEAREA_LOCALNODE
Leigh B. Stoller's avatar
Leigh B. Stoller committed
181
	 TBWideareaNodeID TBTipServers
Mac Newbold's avatar
Mac Newbold committed
182

Chad Barb's avatar
   
Chad Barb committed
183
184
	 TBSiteVarExists TBGetSiteVar

185
	 TBActivityReport GatherSwapStats GatherAssignStats
186
	 TBAvailablePCs
187

188
189
	 TBDB_IFACEROLE_CONTROL TBDB_IFACEROLE_EXPERIMENT
	 TBDB_IFACEROLE_JAIL TBDB_IFACEROLE_FAKE TBDB_IFACEROLE_OTHER
190
	 TBDB_IFACEROLE_GW TBDB_IFACEROLE_OUTER_CONTROL
191

192
193
	 TBDB_ROUTERTYPE_NONE	TBDB_ROUTERTYPE_OSPF
	 TBDB_ROUTERTYPE_STATIC TBDB_ROUTERTYPE_MANUAL
194
	 TBDB_EVENTKEY TBDB_WEBKEY
195
196
	 TBDB_CHECKDBSLOT_NOFLAGS TBDB_CHECKDBSLOT_WARN TBDB_CHECKDBSLOT_ERROR
         max min TBcheck_dbslot
Mac Newbold's avatar
Mac Newbold committed
197
	 hash_recurse array_recurse hash_recurse2 array_recurse2
198
	 TBGetUniqueIndex
199
200
201
202

	 TBExptMinMaxNodes TBExptSecurityLevel TBExptIDX
	 TBDB_SECLEVEL_GREEN  TBDB_SECLEVEL_YELLOW
	 TBDB_SECLEVEL_ORANGE TBDB_SECLEVEL_RED
203
	 );
204

205
# Must come after package declaration!
206
use lib '@prefix@/lib';
207
use English;
208
use File::Basename;
209
use POSIX qw(strftime);
210
require Mysql;
Mac Newbold's avatar
Mac Newbold committed
211
212
use vars qw($DBQUERY_MAXTRIES $DBCONN_MAXTRIES @EXPORT_OK @virtualTables
	    @physicalTables);
213

214
215
216
# Configure variables
my $TB		= "@prefix@";
my $DBNAME	= "@TBDBNAME@";
217
my $TBOPS       = "@TBOPSEMAIL@";
218
219
my $EVENTSYS    = "@EVENTSYS@";
my $BOSSNODE    = "@BOSSNODE@";
220
my $TESTMODE    = @TESTMODE@;
221
my $TBOPSPID	= "emulab-ops";
222
223
224
225
my $SCRIPTNAME  = "Unknown";
my $PROJROOT    = "/proj";
my $GROUPROOT   = "/groups";
my $USERROOT    = "/users";
226

227
228
229
230
231
if ($EVENTSYS) {
    require event;
    import event;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
232
233
234
# Untainted scriptname for email below.
if ($PROGRAM_NAME =~ /^([-\w\.\/]+)$/) {
    $SCRIPTNAME = basename($1);
235
236
}
else {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
237
    $SCRIPTNAME = "Tainted";
238
239
}

240
#
241
# Set up for querying the database. Note that fork causes a reconnect
Mac Newbold's avatar
Mac Newbold committed
242
# to the DB in the child.
243
244
#
my $DB;
245
246
247
$DBQUERY_MAXTRIES = 1;
$DBCONN_MAXTRIES  = 5;
@EXPORT_OK        = qw($DBQUERY_MAXTRIES $DBCONN_MAXTRIES);
248
249
250

sub TBDBConnect()
{
251
    my $maxtries = $DBCONN_MAXTRIES;
252
253
254
255
256
257
258
259
260

    #
    # Construct a 'username' from the name of this script and the user who
    # ran it. This is for accounting purposes.
    #
    my $name = getpwuid($UID);
    if (!$name) {
	$name = "uid$UID";
    }
261
    my $dbuser = "$SCRIPTNAME:$name:$PID";
262

263
    while ($maxtries) {
264
	$DB = Mysql->connect("localhost", $DBNAME, $dbuser, "none");
265
266
267
	if (defined($DB)) {
	    last;
	}
268
269
270
271
272
273
	$maxtries--;
	sleep(1);
    }
    if (!defined($DB)) {
	die("Cannot connect to DB after several attempts!\n");
    }
274
275
    $DB->{'dbh'}->{'PrintError'} = 0;
    $Mysql::QUIET = 1;
276
277
}
TBDBConnect();
278

279
280
sub TBdbfork()
{
281
    select(undef, undef, undef, 0.3);
282
    undef($DB);
283
    TBDBConnect();
284
285
286
    if ($EVENTSYS) {
	EventFork();
    }
287
288
}

289
290
291
292
293
#
# Record last DB error string.
#
my $DBErrorString = "";

294
295
296
297
#
# Needs to be config'ed.
#
sub TBDB_EXPT_WORKDIR()		{ "/usr/testbed/expwork"; }
Mac Newbold's avatar
Mac Newbold committed
298

299
300
301
302
303
#
# Define exported "constants". Basically, these are just perl subroutines
# that look like constants cause you do not need to call a perl subroutine
# with parens. That is, FOO and FOO() are the same thing.
#
304
sub NODERELOADING_PID()		{ $TBOPSPID; }
305
sub NODERELOADING_EID()		{ "reloading"; }
306
sub NODERELOADPENDING_EID()	{ "reloadpending"; }
307
sub NODEDEAD_PID()		{ $TBOPSPID; }
308
sub NODEDEAD_EID()		{ "hwdown"; }
309
310
311
312
sub PLABMOND_PID()		{ $TBOPSPID; }
sub PLABMOND_EID()		{ "plab-monitor"; }
sub PLABHOLDING_PID()		{ $TBOPSPID; }
sub PLABHOLDING_EID()		{ "plabnodes"; }
313
314
sub OLDRESERVED_PID()		{ $TBOPSPID; }
sub OLDRESERVED_EID()		{ "oldreserved"; }
315
316
sub NFREELOCKED_PID()		{ $TBOPSPID; }
sub NFREELOCKED_EID()		{ "nfree-locked"; }
317
318
319
sub PROJROOT()			{ $PROJROOT; }
sub GROUPROOT()			{ $GROUPROOT; }
sub USERROOT()			{ $USERROOT; }
Robert Ricci's avatar
Robert Ricci committed
320
sub TBOPSPID()			{ $TBOPSPID; }
321
322
323
324
325
326

sub NODEBOOTSTATUS_OKAY()	{ "okay" ; }
sub NODEBOOTSTATUS_FAILED()	{ "failed"; }
sub NODEBOOTSTATUS_UNKNOWN()	{ "unknown"; }
sub NODESTARTSTATUS_NOSTATUS()	{ "none"; }

327
328
329
330
sub NODEFAILMODE_FATAL()	{ "fatal"; }
sub NODEFAILMODE_NONFATAL()	{ "nonfatal"; }
sub NODEFAILMODE_IGNORE()	{ "ignore"; }

331
# Experiment states
332
333
334
sub EXPTSTATE_NEW()		{ "new"; }
sub EXPTSTATE_PRERUN()		{ "prerunning"; }
sub EXPTSTATE_SWAPPED()		{ "swapped"; }
335
sub EXPTSTATE_QUEUED()		{ "queued"; }
336
337
338
339
340
sub EXPTSTATE_SWAPPING()	{ "swapping"; }
sub EXPTSTATE_ACTIVATING()	{ "activating"; }
sub EXPTSTATE_ACTIVE()		{ "active"; }
sub EXPTSTATE_TERMINATING()	{ "terminating"; }
sub EXPTSTATE_TERMINATED()	{ "ended"; }
341
342
343
344
345
346
347
sub EXPTSTATE_MODIFY_PARSE()	{ "modify_parse"; }
sub EXPTSTATE_MODIFY_REPARSE()	{ "modify_reparse"; }
sub EXPTSTATE_MODIFY_RESWAP()	{ "modify_reswap"; }
sub EXPTSTATE_RESTARTING()	{ "restarting"; }
# For the batch_daemon.
sub BATCHSTATE_LOCKED()		{ "locked";}
sub BATCHSTATE_UNLOCKED()	{ "unlocked";}
348

349
# Cancel flags
350
351
352
sub EXPTCANCEL_CLEAR()		{ 0 ;}
sub EXPTCANCEL_TERM()		{ 1 ;}
sub EXPTCANCEL_SWAP()		{ 2 ;}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
353

354
355
sub USERSTATUS_ACTIVE()		{ "active"; }
sub USERSTATUS_FROZEN()		{ "frozen"; }
356
357
358
sub USERSTATUS_UNAPPROVED()	{ "unapproved"; }
sub USERSTATUS_UNVERIFIED()	{ "unverified"; }
sub USERSTATUS_NEWUSER()	{ "newuser"; }
359

360
361
362
#
# We want valid project membership to be non-zero for easy membership
# testing. Specific trust levels are encoded thusly.
Mac Newbold's avatar
Mac Newbold committed
363
#
364
365
sub PROJMEMBERTRUST_NONE()	{ 0; }
sub PROJMEMBERTRUST_USER()	{ 1; }
366
sub PROJMEMBERTRUST_ROOT()	{ 2; }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
367
368
369
370
371
372
373
sub PROJMEMBERTRUST_LOCALROOT()	{ 2; }
sub PROJMEMBERTRUST_GROUPROOT()	{ 3; }
sub PROJMEMBERTRUST_PROJROOT()	{ 4; }
sub PROJMEMBERTRUST_ADMIN()	{ 5; }

#
# Access types. Duplicated in the web interface. Make changes there too!
Mac Newbold's avatar
Mac Newbold committed
374
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
375
376
377
378
379
380
# Things you can do to a node.
sub TB_NODEACCESS_READINFO()	{ 1; }
sub TB_NODEACCESS_MODIFYINFO()	{ 2; }
sub TB_NODEACCESS_LOADIMAGE()	{ 3; }
sub TB_NODEACCESS_REBOOT()	{ 4; }
sub TB_NODEACCESS_POWERCYCLE()	{ 5; }
381
sub TB_NODEACCESS_MODIFYVLANS()	{ 6; }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
382
sub TB_NODEACCESS_MIN()		{ TB_NODEACCESS_READINFO; }
383
sub TB_NODEACCESS_MAX()		{ TB_NODEACCESS_MODIFYVLANS; }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
384
385
386
387
388
389
390

# User Info (modinfo web page, etc).
sub TB_USERINFO_READINFO()	{ 1; }
sub TB_USERINFO_MODIFYINFO()	{ 2; }
sub TB_USERINFO_MIN()		{ TB_USERINFO_READINFO; }
sub TB_USERINFO_MAX()		{ TB_USERINFO_MODIFYINFO; }

391
# Experiments.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
392
393
394
sub TB_EXPT_READINFO()		{ 1; }
sub TB_EXPT_MODIFY()		{ 2; }
sub TB_EXPT_DESTROY()		{ 3; }
395
sub TB_EXPT_UPDATE()		{ 4; }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
396
sub TB_EXPT_MIN()		{ TB_EXPT_READINFO; }
397
sub TB_EXPT_MAX()		{ TB_EXPT_UPDATE; }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
398
399
400
401
402

# Projects.
sub TB_PROJECT_READINFO()	{ 1; }
sub TB_PROJECT_MAKEGROUP()	{ 2; }
sub TB_PROJECT_EDITGROUP()	{ 3; }
Chad Barb's avatar
   
Chad Barb committed
403
sub TB_PROJECT_GROUPGRABUSERS() { 4; }
Chad Barb's avatar
   
Chad Barb committed
404
405
406
407
408
409
410
411
412
413
sub TB_PROJECT_BESTOWGROUPROOT(){ 5; }
sub TB_PROJECT_DELGROUP()	{ 6; }
sub TB_PROJECT_LEADGROUP()	{ 7; }
sub TB_PROJECT_ADDUSER()	{ 8; }
sub TB_PROJECT_DELUSER()	{ 9; }
sub TB_PROJECT_MAKEOSID()	{ 10; }
sub TB_PROJECT_DELOSID()	{ 11; }
sub TB_PROJECT_MAKEIMAGEID()	{ 12; }
sub TB_PROJECT_DELIMAGEID()	{ 13; }
sub TB_PROJECT_CREATEEXPT()	{ 14; }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
414
415
416
sub TB_PROJECT_MIN()		{ TB_PROJECT_READINFO; }
sub TB_PROJECT_MAX()		{ TB_PROJECT_CREATEEXPT; }

Mac Newbold's avatar
Mac Newbold committed
417
# OSIDs
Leigh B. Stoller's avatar
Leigh B. Stoller committed
418
419
420
421
422
423
sub TB_OSID_READINFO()		{ 1; }
sub TB_OSID_CREATE()		{ 2; }
sub TB_OSID_DESTROY()		{ 3; }
sub TB_OSID_MIN()		{ TB_OSID_READINFO; }
sub TB_OSID_MAX()		{ TB_OSID_DESTROY; }

424
425
426
# Magic OSID constants
sub TB_OSID_MBKERNEL()          { "_KERNEL_"; } # multiboot kernel OSID
sub TB_OSID_PXEBOOT()           { "PXEBOOT"; } # osid for def pxe_boot_path
427
428
429
430
431
sub TB_OSID_FRISBEE()           { "PXEFRISBEE"; }

# Magic MFS constants
sub TB_OSID_FREEBSD_MFS()	{ "FREEBSD-MFS" };
sub TB_OSID_FRISBEE_MFS()	{ "FRISBEE-MFS" };
432

Leigh B. Stoller's avatar
Leigh B. Stoller committed
433
# ImageIDs
434
435
436
437
438
#
# Clarification:
# READINFO is read-only access to the image and its contents
# (This is what people get for shared images)
# ACCESS means complete power over the image and its [meta]data
Leigh B. Stoller's avatar
Leigh B. Stoller committed
439
440
441
442
443
444
445
sub TB_IMAGEID_READINFO()	{ 1; }
sub TB_IMAGEID_MODIFYINFO()	{ 2; }
sub TB_IMAGEID_CREATE()		{ 3; }
sub TB_IMAGEID_DESTROY()	{ 4; }
sub TB_IMAGEID_ACCESS()		{ 5; }
sub TB_IMAGEID_MIN()		{ TB_IMAGEID_READINFO; }
sub TB_IMAGEID_MAX()		{ TB_IMAGEID_ACCESS; }
446

447
# Node Log Types
448
449
450
451
452
453
454
sub TB_NODELOGTYPE_MISC		{ "misc"; }
sub TB_NODELOGTYPES()		{ ( TB_NODELOGTYPE_MISC ) ; }
sub TB_DEFAULT_NODELOGTYPE()	{ TB_NODELOGTYPE_MISC; }

# Reload Types.
sub TB_RELOADTYPE_NETDISK()	{ "netdisk"; }
sub TB_RELOADTYPE_FRISBEE()	{ "frisbee"; }
455
sub TB_DEFAULT_RELOADTYPE()	{ TB_RELOADTYPE_FRISBEE; }
456

457
458
459
460
461
462
463
# Experiment priorities.
sub TB_EXPTPRIORITY_LOW()	{ 0; }
sub TB_EXPTPRIORITY_HIGH()	{ 20; }

# Assign exit status for too few nodes.
sub TB_ASSIGN_TOOFEWNODES()	{ 2; }

464
465
466
# System PID.
sub TB_OPSPID()			{ $TBOPSPID; }

467
#
468
469
470
471
# Events we may want to send
#
sub TBDB_TBEVENT_NODESTATE	{ "TBNODESTATE"; }
sub TBDB_TBEVENT_NODEOPMODE	{ "TBNODEOPMODE"; }
472
sub TBDB_TBEVENT_CONTROL	{ "TBCONTROL"; }
473
sub TBDB_TBEVENT_COMMAND	{ "TBCOMMAND"; }
474
sub TBDB_TBEVENT_EXPTSTATE	{ "TBEXPTSTATE"; }
475
476
477
478

#
# For nodes, we use this set of events.
#
479
sub TBDB_NODESTATE_ISUP()	{ "ISUP"; }
480
sub TBDB_NODESTATE_ALWAYSUP()	{ "ALWAYSUP"; }
481
482
483
484
485
486
487
488
489
sub TBDB_NODESTATE_REBOOTED()	{ "REBOOTED"; }
sub TBDB_NODESTATE_REBOOTING()	{ "REBOOTING"; }
sub TBDB_NODESTATE_SHUTDOWN()	{ "SHUTDOWN"; }
sub TBDB_NODESTATE_BOOTING()	{ "BOOTING"; }
sub TBDB_NODESTATE_TBSETUP()	{ "TBSETUP"; }
sub TBDB_NODESTATE_RELOADSETUP() { "RELOADSETUP"; }
sub TBDB_NODESTATE_RELOADING()	{ "RELOADING"; }
sub TBDB_NODESTATE_RELOADDONE()	{ "RELOADDONE"; }
sub TBDB_NODESTATE_UNKNOWN()	{ "UNKNOWN"; };
490
sub TBDB_NODESTATE_PXEWAIT()	{ "PXEWAIT"; }
491
492
sub TBDB_NODESTATE_PXEWAKEUP()	{ "PXEWAKEUP"; }
sub TBDB_NODESTATE_PXEBOOTING()	{ "PXEBOOTING"; }
493

494
sub TBDB_NODEOPMODE_ANY		{ "*"; } # A wildcard opmode
495
496
497
498
sub TBDB_NODEOPMODE_NORMAL	{ "NORMAL"; }
sub TBDB_NODEOPMODE_DELAYING	{ "DELAYING"; }
sub TBDB_NODEOPMODE_UNKNOWNOS	{ "UNKNOWNOS"; }
sub TBDB_NODEOPMODE_RELOADING	{ "RELOADING"; }
499
500
501
sub TBDB_NODEOPMODE_NORMALv1	{ "NORMALv1"; }
sub TBDB_NODEOPMODE_MINIMAL	{ "MINIMAL"; }
sub TBDB_NODEOPMODE_RELOAD	{ "RELOAD"; }
502
sub TBDB_NODEOPMODE_RELOADMOTE	{ "RELOAD-MOTE"; }
503
sub TBDB_NODEOPMODE_DELAY	{ "DELAY"; }
504
sub TBDB_NODEOPMODE_BOOTWHAT	{ "_BOOTWHAT_"; } # A redirection opmode
505
506
sub TBDB_NODEOPMODE_UNKNOWN	{ "UNKNOWN"; }

507
508
509
510
511
sub TBDB_COMMAND_REBOOT         { "REBOOT"; }
sub TBDB_COMMAND_POWEROFF       { "POWEROFF"; }
sub TBDB_COMMAND_POWERON        { "POWERON"; }
sub TBDB_COMMAND_POWERCYCLE     { "POWERCYCLE"; }

512
513
514
515
sub TBDB_STATED_TIMEOUT_REBOOT  { "REBOOT"; }
sub TBDB_STATED_TIMEOUT_NOTIFY  { "NOTIFY"; }
sub TBDB_STATED_TIMEOUT_CMDRETRY{ "CMDRETRY"; }

Chad Barb's avatar
   
Chad Barb committed
516
517
518
sub TBDB_ALLOCSTATE_FREE_CLEAN()       { "FREE_CLEAN"; }
sub TBDB_ALLOCSTATE_FREE_DIRTY()       { "FREE_DIRTY"; }
sub TBDB_ALLOCSTATE_DOWN()             { "DOWN"; }
519
sub TBDB_ALLOCSTATE_DEAD()             { "DEAD"; }
Chad Barb's avatar
   
Chad Barb committed
520
521
522
sub TBDB_ALLOCSTATE_RELOAD_TO_FREE()   { "RELOAD_TO_FREE"; }
sub TBDB_ALLOCSTATE_RELOAD_PENDING()   { "RELOAD_PENDING"; }
sub TBDB_ALLOCSTATE_RES_RELOAD()       { "RES_RELOAD"; }
Chad Barb's avatar
   
Chad Barb committed
523
524
sub TBDB_ALLOCSTATE_RES_REBOOT_DIRTY() { "RES_REBOOT_DIRTY"; }
sub TBDB_ALLOCSTATE_RES_REBOOT_CLEAN() { "RES_REBOOT_CLEAN"; }
525
526
sub TBDB_ALLOCSTATE_RES_INIT_DIRTY()   { "RES_INIT_DIRTY"; }
sub TBDB_ALLOCSTATE_RES_INIT_CLEAN()   { "RES_INIT_CLEAN"; }
Chad Barb's avatar
   
Chad Barb committed
527
sub TBDB_ALLOCSTATE_RES_READY()        { "RES_READY"; }
528
sub TBDB_ALLOCSTATE_RES_RECONFIG()     { "RES_RECONFIG"; }
529
sub TBDB_ALLOCSTATE_RES_TEARDOWN()     { "RES_TEARDOWN"; }
Chad Barb's avatar
   
Chad Barb committed
530
531
sub TBDB_ALLOCSTATE_UNKNOWN()          { "UNKNOWN"; };

532
533
534
sub TBDB_TBCONTROL_RESET	{ "RESET"; }
sub TBDB_TBCONTROL_RELOADDONE	{ "RELOADDONE"; }
sub TBDB_TBCONTROL_TIMEOUT	{ "TIMEOUT"; }
Mac Newbold's avatar
Mac Newbold committed
535
536
537
sub TBDB_TBCONTROL_PXEBOOT	{ "PXEBOOT"; }
sub TBDB_TBCONTROL_BOOTING	{ "BOOTING"; }
sub TBDB_TBCONTROL_CHECKGENISUP	{ "CHECKGENISUP"; }
538
539
540
541

# Constant we use for the timeout field when there is no timeout for a state
sub TBDB_NO_STATE_TIMEOUT	{ 0; }

542
543
544
545
546
547
#
# Node name we use in the widearea_* tables to represent a generic local node.
# All local nodes are considered to have the same network characteristcs.
#
sub TBDB_WIDEAREA_LOCALNODE     { "boss"; }

548
549
550
#
# We should list all of the DB limits.
#
551
sub DBLIMIT_NSFILESIZE()	{ (2**24 - 1); }
552

553
554
555
556
557
558
559
560
561
562
563
#
# Virtual nodes must operate within a restricted port range. The range
# is effective across all virtual nodes in the experiment. When an
# experiment is swapped in, allocate a subrange from this and setup
# all the vnodes to allocate from that range. We tell the user this
# range so this they can set up their programs to operate in that range.
#
sub TBDB_LOWVPORT()		{ 30000; }
sub TBDB_MAXVPORT()		{ 60000; }
sub TBDB_PORTRANGE()		{ 256;   }

564
565
566
567
568
569
570
571
#
# STATS constants.
#
sub TBDB_STATS_PRELOAD()	{ "preload"; }
sub TBDB_STATS_START()		{ "start"; }
sub TBDB_STATS_TERMINATE()	{ "destroy"; }
sub TBDB_STATS_SWAPIN()		{ "swapin"; }
sub TBDB_STATS_SWAPOUT()	{ "swapout"; }
572
sub TBDB_STATS_SWAPMODIFY()	{ "swapmod"; }
573
sub TBDB_STATS_FLAGS_IDLESWAP()	{ 0x01; }
574
sub TBDB_STATS_FLAGS_PREMODIFY(){ 0x02; }
575
576
577
sub TBDB_STATS_FLAGS_START()    { 0x04; }
# Do not export this variable!
my $TBDB_STATS_STARTCLOCK;
578

579
580
581
582
# Jail.
sub TBDB_JAILIPBASE()		{ "@JAILIPBASE@"; }
sub TBDB_JAILIPMASK()		{ "@JAILIPMASK@"; }

583
584
585
586
# Reserved node "roles"
sub TBDB_RSRVROLE_NODE()	{ "node"; }
sub TBDB_RSRVROLE_VIRTHOST()	{ "virthost"; }
sub TBDB_RSRVROLE_DELAYNODE()	{ "delaynode"; }
587
sub TBDB_RSRVROLE_SIMHOST()	{ "simhost"; }
588

589
590
591
592
593
# Interfaces roles.
sub TBDB_IFACEROLE_CONTROL()	{ "ctrl"; }
sub TBDB_IFACEROLE_EXPERIMENT()	{ "expt"; }
sub TBDB_IFACEROLE_JAIL()	{ "jail"; }
sub TBDB_IFACEROLE_FAKE()	{ "fake"; }
594
sub TBDB_IFACEROLE_GW()		{ "gw"; }
595
sub TBDB_IFACEROLE_OTHER()	{ "other"; }
596
sub TBDB_IFACEROLE_OUTER_CONTROL(){ "outer_ctrl"; }
597

598
599
600
601
602
603
# Routertypes.
sub TBDB_ROUTERTYPE_NONE()	{ "none"; }
sub TBDB_ROUTERTYPE_OSPF()	{ "ospf"; }
sub TBDB_ROUTERTYPE_STATIC()	{ "static"; }
sub TBDB_ROUTERTYPE_MANUAL()	{ "manual"; }

604
# Key Stuff
605
sub TBDB_EVENTKEY($$)	{ TBExptUserDir($_[0],$_[1]) . "/tbdata/eventkey"; }
606
sub TBDB_WEBKEY($$)	{ TBExptUserDir($_[0],$_[1]) . "/tbdata/webkey"; }
607

608
609
610
611
612
# Regex stuff
sub TBDB_CHECKDBSLOT_NOFLAGS()	{ 0x0; }
sub TBDB_CHECKDBSLOT_WARN()	{ 0x1; }
sub TBDB_CHECKDBSLOT_ERROR()	{ 0x2; }

613
614
615
616
617
618
# Security Levels.
sub TBDB_SECLEVEL_GREEN()	{ 0; }
sub TBDB_SECLEVEL_YELLOW()	{ 1; }
sub TBDB_SECLEVEL_ORANGE()	{ 2; }
sub TBDB_SECLEVEL_RED()		{ 3; }

619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
#
# A hash of all tables that contain information about physical nodes - the
# value for each key is the list of columns that could contain the node's ID.
#
sub TBDB_PHYSICAL_NODE_TABLES() {
    return (
	'current_reloads'	=> [ 'node_id' ],
	'delays'		=> [ 'node_id' ],
	'iface_counters'	=> [ 'node_id' ],
	'interfaces'		=> [ 'node_id' ],
	'interface_settings'	=> [ 'node_id' ],
	'last_reservation'	=> [ 'node_id' ],
	'linkdelays'		=> [ 'node_id' ],
	'location_info'		=> [ 'node_id' ],
	'next_reserve'		=> [ 'node_id' ],
	'node_activity'		=> [ 'node_id' ],
	'node_auxtypes'		=> [ 'node_id' ],
	'node_features'		=> [ 'node_id' ],
	'node_hostkeys'		=> [ 'node_id' ],
	'node_idlestats'	=> [ 'node_id' ],
	'node_status'   	=> [ 'node_id' ],
	'node_rusage'		=> [ 'node_id' ],
	'nodeipportnum'		=> [ 'node_id' ],
	'nodelog'		=> [ 'node_id' ],
	'nodes'			=> [ 'node_id', 'phys_nodeid' ],
	'nodeuidlastlogin'	=> [ 'node_id' ],
	'ntpinfo'		=> [ 'node_id' ],
	'outlets'		=> [ 'node_id' ],
	'partitions'		=> [ 'node_id' ],
	'plab_slice_nodes'	=> [ 'node_id' ],
	'port_counters'		=> [ 'node_id' ],
	'reserved'		=> [ 'node_id' ],
	'scheduled_reloads'	=> [ 'node_id' ],
	'state_triggers'	=> [ 'node_id' ],
	'switch_stacks'		=> [ 'node_id' ],
	'tiplines'		=> [ 'node_id' ],
	'tmcd_redirect'		=> [ 'node_id' ],
	'tunnels'		=> [ 'node_id' ],
	'uidnodelastlogin'	=> [ 'node_id' ],
	'v2pmap'		=> [ 'node_id' ],
	'veth_interfaces'	=> [ 'node_id' ],
	'widearea_accounts'	=> [ 'node_id' ],
	'widearea_delays'	=> [ 'node_id1', 'node_id2' ],
	'widearea_nodeinfo'	=> [ 'node_id' ],
	'widearea_recent'	=> [ 'node_id1', 'node_id2' ],
	'wires'			=> [ 'node_id1', 'node_id2' ],
    );
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
668
669
670
671
672
673
674
675
676
677
678
679
680
681
#
# Auth stuff.
#

#
# Convert a trust string to the above numeric values.
#
sub TBTrustConvert($)
{
    my($trust_string) = @_;
    my $trust_value = 0;

    #
    # Convert string to value. Perhaps the DB should have done it this way?
Mac Newbold's avatar
Mac Newbold committed
682
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
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
725
726
727
    if ($trust_string eq "none") {
	$trust_value = PROJMEMBERTRUST_NONE;
    }
    elsif ($trust_string eq "user") {
	$trust_value = PROJMEMBERTRUST_USER;
    }
    elsif ($trust_string eq "local_root") {
	$trust_value = PROJMEMBERTRUST_LOCALROOT;
    }
    elsif ($trust_string eq "group_root") {
	$trust_value = PROJMEMBERTRUST_GROUPROOT;
    }
    elsif ($trust_string eq "project_root") {
	$trust_value = PROJMEMBERTRUST_PROJROOT;
    }
    elsif ($trust_string eq "admin") {
	$trust_value = PROJMEMBERTRUST_ADMIN;
    }
    else {
	    die("*** Invalid trust value $trust_string!");
    }

    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.
#
sub TBMinTrust($$)
{
    my ($trust_value, $minimum) = @_;

    if ($minimum < PROJMEMBERTRUST_NONE ||
	$minimum > PROJMEMBERTRUST_ADMIN) {
	    die("*** Invalid minimum trust $minimum!");
    }

    #
    # Sleazy? How do you do a typeof in perl?
    #
    if (length($trust_value) != 1) {
	$trust_value = TBTrustConvert($trust_value);
    }
Mac Newbold's avatar
Mac Newbold committed
728

Leigh B. Stoller's avatar
Leigh B. Stoller committed
729
730
731
732
733
734
    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.
Mac Newbold's avatar
Mac Newbold committed
735
736
# Return that trust level as one of the numeric values above.
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
737
738
739
# usage: TBGrpTrust($dbuid, $pid, $gid)
#        returns numeric trust value if a group member.
#        returns PROJMEMBERTRUST_NONE if not a group member.
Mac Newbold's avatar
Mac Newbold committed
740
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
sub TBGrpTrust($$$)
{
    my ($uid, $pid, $gid) = @_;

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

    my $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 ($query_result->numrows == 0) {
	return PROJMEMBERTRUST_NONE;
    }

    my @row = $query_result->fetchrow_array();
Mac Newbold's avatar
Mac Newbold committed
764
    my $trust_string = $row[0];
Leigh B. Stoller's avatar
Leigh B. Stoller committed
765
766
767
768
769
770
771
772
773
774
775

    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.
#
# usage: TBProjTrust($dbuid, $pid)
#        returns numeric trust value if a project member.
#        returns PROJMEMBERTRUST_NONE if not a project member.
Mac Newbold's avatar
Mac Newbold committed
776
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
777
778
779
sub TBProjTrust($$)
{
    my ($uid, $pid) = @_;
Mac Newbold's avatar
Mac Newbold committed
780

Leigh B. Stoller's avatar
Leigh B. Stoller committed
781
782
783
    return TBGrpTrust($uid, $pid, $pid);
}

784
#
785
786
# Test admin status. Optional argument is the UID or Name to test. If not
# provided, then test the current UID.
787
#
788
789
790
# XXX Argument is *either* a numeric UID, or a string name.
#
# usage: TBAdmin([int or char* uid]);
791
792
#        returns 1 if an admin type.
#        returns 0 if a mere user.
Mac Newbold's avatar
Mac Newbold committed
793
#
794
795
796
sub TBAdmin(;$)
{
    my($uid) = @_;
797
    my($name);
798

799
800
801
802
    #
    # No one is considered an admin unless they have the magic environment
    # variable set (so that you have to be a bit more explict about wanting
    # admin privs.) Use the withadminprivs script to get this variable set.
803
804
    # Also check with HTTP_ at the front of the name, since this is required
    # to get it through suexec from the web scripts.
805
    #
806
    if (!($ENV{WITH_TB_ADMIN_PRIVS} || $ENV{HTTP_WITH_TB_ADMIN_PRIVS})) {
807
808
809
	return 0;
    }

810
811
812
813
    if (!defined($uid)) {
	$uid = $UID;
    }

814
815
    #
    # Test if numeric. Map to name if it is.
Mac Newbold's avatar
Mac Newbold committed
816
    #
817
818
819
820
821
822
823
    if ($uid =~ /^[0-9]+$/) {
	($name) = getpwuid($uid)
	    or die "$uid not in passwd file\n";
    }
    else {
	$name = $uid;
    }
824
825

    my $query_result =
826
	DBQueryFatal("select admin from users where uid='$name'");
827
828
829
830
831
832
833
834
835

    my @row = $query_result->fetchrow_array();
    if ($row[0] == 1) {
	return 1;
    }
    return 0;
}

#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
836
837
# Project permission checks. The group id (gid) can be undef, in which case
# the pid is used (ie: a default group check is made).
838
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
839
840
841
# Usage: TBProjAccessCheck($uid, $pid, $gid, $access_type)
#	 returns 0 if not allowed.
#        returns 1 if allowed.
Mac Newbold's avatar
Mac Newbold committed
842
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
843
sub TBProjAccessCheck($$$$)
844
{
Leigh B. Stoller's avatar
Leigh B. Stoller committed
845
846
    my ($uid, $pid, $gid, $access_type) = @_;
    my $mintrust;
847

Leigh B. Stoller's avatar
Leigh B. Stoller committed
848
849
850
    if ($access_type < TB_PROJECT_MIN ||
	$access_type > TB_PROJECT_MAX) {
	die("*** Invalid access type: $access_type!");
851
852
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
853
854
    #
    # Admins do whatever they want!
Mac Newbold's avatar
Mac Newbold committed
855
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
856
857
    if (TBAdmin($uid)) {
	return 1;
858
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
859
860
861
862
863
864
865
    $uid = MapNumericUID($uid);

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

Leigh B. Stoller's avatar
Leigh B. Stoller committed
868
869
870
871
872
873
    if ($access_type == TB_PROJECT_READINFO) {
	$mintrust = PROJMEMBERTRUST_USER;
    }
    elsif ($access_type == TB_PROJECT_CREATEEXPT) {
	$mintrust = PROJMEMBERTRUST_LOCALROOT;
    }
874
875
876
    elsif ($access_type == TB_PROJECT_DELUSER) {
	$mintrust = PROJMEMBERTRUST_PROJROOT;
    }
877
878
879
880
    elsif ($access_type == TB_PROJECT_MAKEGROUP ||
	   $access_type == TB_PROJECT_DELGROUP) {
	$mintrust = PROJMEMBERTRUST_GROUPROOT;
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
881
882
883
884
885
886
887
888
889
890
891
892
893
    else {
	die("*** Unexpected access type: $access_type!");
    }

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

#
# Experiment permission checks.
#
# Usage: TBExptAccessCheck($uid, $pid, $eid, $access_type)
#	 returns 0 if not allowed.
#        returns 1 if allowed.
Mac Newbold's avatar
Mac Newbold committed
894
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
895
896
897
898
899
900
901
902
sub TBExptAccessCheck($$$$)
{
    my ($uid, $pid, $eid, $access_type) = @_;
    my $mintrust;

    if ($access_type < TB_EXPT_MIN ||
	$access_type > TB_EXPT_MAX) {
	die("*** Invalid access type: $access_type!");
903
904
905
    }

    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
906
    # Admins do whatever they want!
Mac Newbold's avatar
Mac Newbold committed
907
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
908
    if (TBAdmin($uid)) {
909
910
	return 1;
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
911
    $uid = MapNumericUID($uid);
912

Leigh B. Stoller's avatar
Leigh B. Stoller committed
913
    my $query_result =
914
	DBQueryFatal("SELECT gid,expt_head_uid FROM experiments WHERE ".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
915
		     "eid='$eid' and pid='$pid'");
Mac Newbold's avatar
Mac Newbold committed
916

Leigh B. Stoller's avatar
Leigh B. Stoller committed
917
918
919
920
    if ($query_result->numrows == 0) {
	return 0;
    }
    my @row = $query_result->fetchrow_array();
921
922
    my $gid     = $row[0];
    my $creator = $row[1];
Leigh B. Stoller's avatar
Leigh B. Stoller committed
923

924
925
926
    #
    # An experiment may be destroyed by the experiment creator or the
    # project/group leader.
Mac Newbold's avatar
Mac Newbold committed
927
    #
928
    if ($access_type == TB_EXPT_READINFO) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
929
930
931
932
933
934
	$mintrust = PROJMEMBERTRUST_USER;
    }
    else {
	$mintrust = PROJMEMBERTRUST_LOCALROOT;
    }

935
936
937
938
939
940
941
    #
    # Either proper permission in the group, or group_root in the project.
    # This lets group_roots muck with other people's experiments, including
    # those in groups they do not belong to.
    #
    return TBMinTrust(TBGrpTrust($uid, $pid, $gid), $mintrust) ||
	TBMinTrust(TBGrpTrust($uid, $pid, $pid), PROJMEMBERTRUST_GROUPROOT);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
942
943
944
945
946
947
948
949
}

#
# Determine if uid can access a node or list of nodes.
#
# Usage: TBNodeAccessCheck($uid, $access_type, $node_id, ...)
#	 returns 0 if not allowed.
#        returns 1 if allowed.
Mac Newbold's avatar
Mac Newbold committed
950
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
951
952
953
954
955
956
957
958
959
960
sub TBNodeAccessCheck($$@)
{
    my ($uid, $access_type) = (shift, shift);
    my @nodelist = @_;
    my $mintrust;

    if ($access_type < TB_NODEACCESS_MIN ||
	$access_type > TB_NODEACCESS_MAX) {
	die("*** Invalid access type: $access_type!");
    }
961
962

    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
963
    # Admins do whatever they want!
Mac Newbold's avatar
Mac Newbold committed
964
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
965
966
967
968
    if (TBAdmin($uid)) {
	return 1;
    }
    $uid = MapNumericUID($uid);
Mac Newbold's avatar
Mac Newbold committed
969

Leigh B. Stoller's avatar
Leigh B. Stoller committed
970
971
972
973
974
975
976
977
    if ($access_type == TB_NODEACCESS_READINFO) {
	$mintrust = PROJMEMBERTRUST_USER;
    }
    else {
	$mintrust = PROJMEMBERTRUST_LOCALROOT;
    }

    foreach my $node (@nodelist) {
978
	my $query_result =
979
	    DBQueryFatal("select e.pid,e.gid from reserved as r ".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
980
			 "left join experiments as e on ".
981
982
			 "     e.pid=r.pid and e.eid=r.eid ".
			 "where r.node_id='$node'");
983

984
	if ($query_result->numrows == 0) {
985
986
	    return 0;
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
987
	my @row = $query_result->fetchrow_array();
988
989
	my $pid = $row[0];
	my $gid = $row[1];
Leigh B. Stoller's avatar
Leigh B. Stoller committed
990

991
992
993
994
995
996
997
998
	#
	# Either proper permission in the group, or group_root in the
	# project. This lets group_roots muck with other people's
	# nodes, including those in groups they do not belong to.
	#
	if (! TBMinTrust(TBGrpTrust($uid, $pid, $gid), $mintrust) &&
	    ! TBMinTrust(TBGrpTrust($uid, $pid, $pid),
			 PROJMEMBERTRUST_GROUPROOT)) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
999
1000
	    return 0;
	}
1001
1002
1003
1004
1005
    }
    return 1;
}

#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1006
# Access checks for an OSID. Tests for tbadmin.
1007
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1008
1009
1010
# Usage: TBOSIDAccessCheck($uid, $osid, $access_type)
#	 returns 0 if not allowed.
#        returns 1 if allowed.
Mac Newbold's avatar
Mac Newbold committed
1011
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1012
sub TBOSIDAccessCheck($$$)
1013
{
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1014
1015
    my ($uid, $osid, $access_type) = @_;
    my $mintrust;
1016

Leigh B. Stoller's avatar
Leigh B. Stoller committed
1017
1018
    if ($access_type < TB_OSID_MIN || $access_type > TB_OSID_MAX) {
	die("*** Invalid access type $access_type!");
1019
1020
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
1021
1022
    #
    # Admins do whatever they want!
Mac Newbold's avatar
Mac Newbold committed
1023
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1024
1025
1026
1027
    if (TBAdmin($uid)) {
	return 1;
    }
    $uid = MapNumericUID($uid);
1028

Leigh B. Stoller's avatar
Leigh B. Stoller committed
1029
1030
1031
1032
    #
    # No GIDs yet.
    #
    my $query_result =
1033
	DBQueryFatal("SELECT pid,shared FROM os_info WHERE osid='$osid'");
Mac Newbold's avatar
Mac Newbold committed
1034

1035
    if ($query_result->numrows == 0) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1036
	return 0;
1037
    }
1038
    my @row = $query_result->fetchrow_array();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1039
    my $pid = $row[0];
1040
    my $shared = $row[1];
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1041
1042

    #
Mac Newbold's avatar
Mac Newbold committed
1043
1044
    # Global OSIDs can be read by anyone, but must be admin to read.
    #
1045
    if ($shared) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1046
1047
1048
1049
1050
	if ($access_type == TB_OSID_READINFO) {
	    return 1;
	}
	return 0;
    }
Mac Newbold's avatar
Mac Newbold committed
1051

Leigh B. Stoller's avatar
Leigh B. Stoller committed
1052
1053
    #
    # Otherwise must have proper trust in the project.
Mac Newbold's avatar
Mac Newbold committed
1054
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
    if ($access_type == TB_OSID_READINFO) {
	$mintrust = PROJMEMBERTRUST_USER;
    }
    else {
	$mintrust = PROJMEMBERTRUST_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.
Mac Newbold's avatar
Mac Newbold committed
1071
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1072
1073
1074
1075
1076
1077
1078
sub TBImageIDAccessCheck($$$)
{
    my ($uid, $imageid, $access_type) = @_;
    my $mintrust;

    if ($access_type < TB_IMAGEID_MIN || $access_type > TB_IMAGEID_MAX) {
	die("*** Invalid access type $access_type!");
1079
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1080
1081

    #
1082
    # Admins and root do whatever they want!
Mac Newbold's avatar
Mac Newbold committed
1083
    #
1084
    if (TBAdmin($uid) || !$UID || $UID eq "root" || $uid eq "root") {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1085
	return 1;
1086
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1087
1088
1089
1090
1091
1092
    $uid = MapNumericUID($uid);

    #
    # No GIDs yet.
    #
    my $query_result =
1093
1094
	DBQueryFatal("SELECT pid,gid,shared,global FROM images ".
		     "WHERE imageid='$imageid'");
Mac Newbold's avatar
Mac Newbold committed
1095

Leigh B. Stoller's avatar
Leigh B. Stoller committed
1096
1097
    if ($query_result->numrows == 0) {
	return 0;
1098
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1099
1100
    my @row = $query_result->fetchrow_array();
    my $pid = $row[0];
1101
1102
1103
    my $gid = $row[1];
    my $shared = $row[2];
    my $global = $row[3];
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1104

1105
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1106
    # Global ImageIDs can be read by anyone.
Mac Newbold's avatar
Mac Newbold committed
1107
    #
1108
    if ($global) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1109
1110
1111
1112
1113
1114
	if ($access_type == TB_IMAGEID_READINFO) {
	    return 1;
	}
	return 0;
    }

1115
    #
1116
    # Otherwise must have proper trust in the pid/gid
Mac Newbold's avatar
Mac Newbold committed
1117
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1118
1119
    if ($access_type == TB_IMAGEID_READINFO) {
	$mintrust = PROJMEMBERTRUST_USER;
1120
1121
1122
1123
1124
1125
        #
        # Shared imageids are readable by anyone in the project.
        #
	if ($shared) {
	    $gid = $pid;
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1126
1127
1128
1129
1130
    }
    else {
	$mintrust = PROJMEMBERTRUST_LOCALROOT;
    }

1131
1132
1133
1134
1135
1136
1137
    #
    # Either proper permission in the group, or group_root in the project.
    # This lets group_roots muck with other people's experiments, including
    # those in groups they do not belong to.
    #
    return TBMinTrust(TBGrpTrust($uid, $pid, $gid), $mintrust) ||
	TBMinTrust(TBGrpTrust($uid, $pid, $pid), PROJMEMBERTRUST_GROUPROOT);
1138
1139
}

1140
#
Mac Newbold's avatar
Mac Newbold committed
1141
# Determine if a node can be allocated to a project.
1142
1143
1144
1145
#
# Usage: TBNodeAllocCheck($pid, $node_id)
#	 returns 0 if not allowed or error.
#        returns 1 if allowed.
Mac Newbold's avatar
Mac Newbold committed
1146
#
1147
1148
1149
1150
1151
1152
sub TBNodeAllocCheck($$)
{
    my ($pid, $node_id) = @_;

    #
    # Admins do whatever they want!
Mac Newbold's avatar
Mac Newbold committed
1153
    #
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
    if (TBAdmin()) {
	return 1;
    }

    #
    # Hmm. The point of this join is to find rows in the permissions table
    # with the corresponding type of the node. If no rows come back, its
    # a non-existent node! If the values are NULL, then there are no rows
    # with that type/class, and thus the type/class is free to be allocated
    # by anyone. Otherwise we get the list of projects that are allowed,
    # and so we have to look at those.
    #