libdb.pm.in 85.9 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-2010 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
#
Kevin Atkinson's avatar
 
Kevin Atkinson committed
13
14
# NOTE: Overrides perl's builtin fork()
#
15
16
17
18
# 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.
#
19

20
package libdb;
21
use strict;
22
use Exporter;
Mac Newbold's avatar
Mac Newbold committed
23
use vars qw(@ISA @EXPORT);
24
@ISA = "Exporter";
25

26
@EXPORT =
27
    qw ( NODERELOADING_PID NODERELOADING_EID NODEDEAD_PID NODEDEAD_EID
28
	 OLDRESERVED_PID OLDRESERVED_EID NFREELOCKED_PID NFREELOCKED_EID 
29
30
	 NODEBOOTSTATUS_OKAY NODEBOOTSTATUS_FAILED NODEBOOTSTATUS_UNKNOWN
	 NODESTARTSTATUS_NOSTATUS PROJMEMBERTRUST_NONE PROJMEMBERTRUST_USER
Leigh B. Stoller's avatar
Leigh B. Stoller committed
31
	 PROJMEMBERTRUST_ROOT PROJMEMBERTRUST_GROUPROOT
32
	 PROJMEMBERTRUST_PROJROOT PROJMEMBERTRUST_LOCALROOT
33
	 NODEILL_PID NODEILL_EID
Leigh B. Stoller's avatar
Leigh B. Stoller committed
34

35
	 TBOPSPID EXPTLOGNAME
36
	 PLABMOND_PID PLABMOND_EID PLABHOLDING_PID PLABHOLDING_EID
Kirk Webb's avatar
 
Kirk Webb committed
37
         PLABTESTING_PID PLABTESTING_EID PLABDOWN_PID PLABDOWN_EID
38

39
	 TBTrustConvert TBMinTrust TBGrpTrust TBProjTrust MapNumericUID
Leigh B. Stoller's avatar
Leigh B. Stoller committed
40
41
42

	 TB_NODEACCESS_READINFO TB_NODEACCESS_MODIFYINFO
	 TB_NODEACCESS_LOADIMAGE TB_NODEACCESS_REBOOT
43
44
	 TB_NODEACCESS_POWERCYCLE TB_NODEACCESS_MODIFYVLANS
	 TB_NODEACCESS_MIN TB_NODEACCESS_MAX
Leigh B. Stoller's avatar
Leigh B. Stoller committed
45

46
47
	 NODEFAILMODE_FATAL NODEFAILMODE_NONFATAL NODEFAILMODE_IGNORE

Leigh B. Stoller's avatar
Leigh B. Stoller committed
48
49
50
	 TB_USERINFO_READINFO TB_USERINFO_MODIFYINFO
	 TB_USERINFO_MIN TB_USERINFO_MAX

51
52
	 USERSTATUS_ACTIVE USERSTATUS_FROZEN
	 USERSTATUS_UNAPPROVED USERSTATUS_UNVERIFIED USERSTATUS_NEWUSER
53

54
	 TB_EXPT_READINFO TB_EXPT_MODIFY TB_EXPT_DESTROY TB_EXPT_UPDATE
Leigh B. Stoller's avatar
Leigh B. Stoller committed
55
56
57
58
	 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
59
	 TB_PROJECT_GROUPGRABUSERS TB_PROJECT_BESTOWGROUPROOT
Leigh B. Stoller's avatar
Leigh B. Stoller committed
60
61
62
63
64
65
66
	 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
67
	 TB_OSID_OSIDLEN TB_OSID_OSNAMELEN TB_OSID_VERSLEN
Leigh B. Stoller's avatar
Leigh B. Stoller committed
68
69
70
71

	 TB_IMAGEID_READINFO TB_IMAGEID_MODIFYINFO
	 TB_IMAGEID_CREATE TB_IMAGEID_DESTROY
	 TB_IMAGEID_ACCESS TB_IMAGEID_MIN TB_IMAGEID_MAX
72
	 TB_IMAGEID_IMAGEIDLEN TB_IMAGEID_IMAGENAMELEN
73

Leigh B. Stoller's avatar
Leigh B. Stoller committed
74
	 DBLIMIT_NSFILESIZE NODERELOADPENDING_EID
75

Timothy Stack's avatar
 
Timothy Stack committed
76
77
	 NODEREPOSITIONING_PID NODEREPOSITIONING_EID NODEREPOSPENDING_EID

78
	 EXPTSTATE_NEW EXPTSTATE_PRERUN EXPTSTATE_SWAPPED EXPTSTATE_SWAPPING
Leigh B. Stoller's avatar
Leigh B. Stoller committed
79
	 EXPTSTATE_ACTIVATING EXPTSTATE_ACTIVE EXPTSTATE_PANICED
80
81
82
83
	 EXPTSTATE_TERMINATING EXPTSTATE_TERMINATED EXPTSTATE_QUEUED
	 EXPTSTATE_MODIFY_PARSE EXPTSTATE_MODIFY_REPARSE EXPTSTATE_MODIFY_RESWAP
	 EXPTSTATE_RESTARTING
	 BATCHSTATE_LOCKED BATCHSTATE_UNLOCKED
84
	 EXPTCANCEL_CLEAR EXPTCANCEL_TERM EXPTCANCEL_SWAP EXPTCANCEL_DEQUEUE
85

86
	 TBSetCancelFlag TBGetCancelFlag
Leigh B. Stoller's avatar
Leigh B. Stoller committed
87

Mac Newbold's avatar
Mac Newbold committed
88
	 TB_NODELOGTYPE_MISC TB_NODELOGTYPES TB_DEFAULT_NODELOGTYPE
89
90

	 TB_DEFAULT_RELOADTYPE TB_RELOADTYPE_FRISBEE TB_RELOADTYPE_NETDISK
91

92
93
	 TB_EXPTPRIORITY_LOW TB_EXPTPRIORITY_HIGH

94
	 TB_ASSIGN_TOOFEWNODES TB_OPSPID
95

96
	 TBDB_TBEVENT_NODESTATE TBDB_TBEVENT_NODEOPMODE TBDB_TBEVENT_CONTROL
97
	 TBDB_TBEVENT_COMMAND
Chad Barb's avatar
 
Chad Barb committed
98

99
	 TBDB_NODESTATE_ISUP TBDB_NODESTATE_REBOOTING TBDB_NODESTATE_REBOOTED
100
	 TBDB_NODESTATE_SHUTDOWN TBDB_NODESTATE_BOOTING TBDB_NODESTATE_TBSETUP
101
	 TBDB_NODESTATE_RELOADSETUP TBDB_NODESTATE_RELOADING
102
103
	 TBDB_NODESTATE_RELOADDONE TBDB_NODESTATE_RELOADDONE_V2
	 TBDB_NODESTATE_UNKNOWN
104
	 TBDB_NODESTATE_PXEWAIT TBDB_NODESTATE_PXEWAKEUP
105
	 TBDB_NODESTATE_PXEFAILED TBDB_NODESTATE_PXELIMBO
106
	 TBDB_NODESTATE_PXEBOOTING TBDB_NODESTATE_ALWAYSUP
107
	 TBDB_NODESTATE_MFSSETUP TBDB_NODESTATE_TBFAILED
108
	 TBDB_NODESTATE_POWEROFF
Chad Barb's avatar
 
Chad Barb committed
109

110
111
	 TBDB_NODEOPMODE_NORMAL TBDB_NODEOPMODE_DELAYING
	 TBDB_NODEOPMODE_UNKNOWNOS TBDB_NODEOPMODE_RELOADING
112
113
	 TBDB_NODEOPMODE_NORMALv1 TBDB_NODEOPMODE_MINIMAL TBDB_NODEOPMODE_PCVM
	 TBDB_NODEOPMODE_RELOAD TBDB_NODEOPMODE_RELOADMOTE TBDB_NODEOPMODE_RELOADPCVM
114
	 TBDB_NODEOPMODE_DELAY
115
	 TBDB_NODEOPMODE_BOOTWHAT
116
	 TBDB_NODEOPMODE_ANY
117
	 TBDB_NODEOPMODE_UNKNOWN
Chad Barb's avatar
 
Chad Barb committed
118

119
	 TBDB_COMMAND_REBOOT
120
121
	 TBDB_COMMAND_POWEROFF TBDB_COMMAND_POWERON TBDB_COMMAND_POWERCYCLE

122
123
124
	 TBDB_STATED_TIMEOUT_REBOOT TBDB_STATED_TIMEOUT_NOTIFY
	 TBDB_STATED_TIMEOUT_CMDRETRY

Chad Barb's avatar
 
Chad Barb committed
125
126
127
	 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
128
129
	 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
130
	 TBDB_ALLOCSTATE_RES_READY TBDB_ALLOCSTATE_UNKNOWN
131
	 TBDB_ALLOCSTATE_RES_TEARDOWN TBDB_ALLOCSTATE_DEAD
132
	 TBDB_ALLOCSTATE_RES_RECONFIG
Chad Barb's avatar
 
Chad Barb committed
133

134
135
	 TBDB_STATS_PRELOAD TBDB_STATS_START TBDB_STATS_TERMINATE
	 TBDB_STATS_SWAPIN TBDB_STATS_SWAPOUT TBDB_STATS_SWAPMODIFY
136
	 TBDB_STATS_FLAGS_IDLESWAP TBDB_STATS_FLAGS_PREMODIFY
137
	 TBDB_STATS_FLAGS_START TBDB_STATS_FLAGS_PRESWAPIN
138
	 TBDB_STATS_FLAGS_MODHOSED TBDB_STATS_SWAPUPDATE
139
	 TBDB_STATS_FLAGS_MODSWAPOUT
140

141
142
	 TBDB_JAILIPBASE TBDB_JAILIPMASK

143
	 TBDB_RSRVROLE_NODE TBDB_RSRVROLE_VIRTHOST TBDB_RSRVROLE_DELAYNODE
144
	 TBDB_RSRVROLE_SIMHOST
145

146
	 TBDB_EXPT_WORKDIR
147
	 TBSetNodeEventState TBGetNodeEventState
Timothy Stack's avatar
 
Timothy Stack committed
148
	 TBNodeEventStateUpdated
Chad Barb's avatar
 
Chad Barb committed
149
	 TBSetNodeAllocState TBGetNodeAllocState
150
	 TBSetNodeOpMode TBGetNodeOpMode TBSetNodeNextOpMode
151
	 TB_OSID_MBKERNEL 
Mac Newbold's avatar
Mac Newbold committed
152
	 TB_OSID_FREEBSD_MFS TB_OSID_FRISBEE_MFS
153
	 TBBootWhat TBNodeStateTimeout
Mac Newbold's avatar
Mac Newbold committed
154
	 TBDB_TBCONTROL_RESET TBDB_TBCONTROL_RELOADDONE
155
	 TBDB_TBCONTROL_RELOADDONE_V2
156
	 TBDB_TBCONTROL_TIMEOUT TBDB_NO_STATE_TIMEOUT
Mac Newbold's avatar
Mac Newbold committed
157
158
	 TBDB_TBCONTROL_PXEBOOT TBDB_TBCONTROL_BOOTING
	 TBDB_TBCONTROL_CHECKGENISUP
159

160
161
	 TBDB_LOWVPORT TBDB_MAXVPORT TBDB_PORTRANGE

162
163
	 TBDB_PHYSICAL_NODE_TABLES

164
	 TBAdmin TBOpsGuy TBProjAccessCheck TBNodeAccessCheck 
165
	 TBExptAccessCheck NodeidToExp 
166
	 ExpState
167
	 ExpNodes ExpNodeVnames ExpNodesOldReserved
Leigh B. Stoller's avatar
Leigh B. Stoller committed
168
169
170
171
	 DBDateTime DefaultImageID 
	 TBSetNodeLogEntry
	 MapNodeOSID 
	 TBOSID TBOSMaxConcurrent TBOSCountInstances
172
	 TBResolveNextOSID TBOsidToPid TBOSIDRebootWaittime
173
	 TBOSLoadMaxOkay TBImageLoadMaxOkay TBImageID 
174
	 TBdbfork VnameToNodeid 
175
	 TBIsNodeRemote 
Leigh B Stoller's avatar
Leigh B Stoller committed
176
	 TBIsNodeVirtual TBControlNetIP TBPhysNodeID
177
	 TBNodeUpdateAccountsByPid TBNodeUpdateAccountsByType
178
	 TBNodeUpdateAccountsByUID
179
	 TBExptWorkDir TBExptUserDir
180
181
	 TBIPtoNodeID TBNodeBootReset TBNodeStateWait
	 TBExptSetSwapUID TBExptSetThumbNail
Leigh B. Stoller's avatar
Leigh B. Stoller committed
182
	 TBPlabNodeUsername MarkPhysNodeDown
183
184
185
	 TBExptIsElabInElab TBExptIsPlabInElab
	 TBExptPlabInElabPLC TBExptPlabInElabNodes
	 TBBatchUnLockExp TBExptIsBatchExp
186
	 
Mike Hibler's avatar
Mike Hibler committed
187
188
	 TBExptFirewall TBNodeFirewall TBExptFirewallAndPort
	 TBSetExptFirewallVlan TBClearExptFirewallVlan
189
	 TBNodeConsoleTail TBExptGetSwapoutAction TBExptGetSwapState
190

Timothy Stack's avatar
 
Timothy Stack committed
191
	 TBNodeSubNodes
192
	 TBNodeAdminOSID TBNodeDiskloadOSID
193
	 TBNodeType TBNodeTypeProcInfo TBNodeTypeBiosWaittime
194

195
196
	 TBExptPortRange

197
	 TBDB_WIDEAREA_LOCALNODE
Leigh B. Stoller's avatar
Leigh B. Stoller committed
198
	 TBWideareaNodeID TBTipServers
Mac Newbold's avatar
Mac Newbold committed
199

200
	 TBSiteVarExists TBGetSiteVar TBSetSiteVar
Chad Barb's avatar
 
Chad Barb committed
201

202
	 TBActivityReport GatherAssignStats
203
	 TBAvailablePCs
204

205
206
	 TBDB_IFACEROLE_CONTROL TBDB_IFACEROLE_EXPERIMENT
	 TBDB_IFACEROLE_JAIL TBDB_IFACEROLE_FAKE TBDB_IFACEROLE_OTHER
207
	 TBDB_IFACEROLE_GW TBDB_IFACEROLE_OUTER_CONTROL
208

209
210
	 TBDB_ROUTERTYPE_NONE	TBDB_ROUTERTYPE_OSPF
	 TBDB_ROUTERTYPE_STATIC TBDB_ROUTERTYPE_MANUAL
211
	 TBDB_USER_INTERFACE_EMULAB TBDB_USER_INTERFACE_PLAB
212
	 TBDB_EVENTKEY TBDB_WEBKEY
213
         max min 
Mac Newbold's avatar
Mac Newbold committed
214
	 hash_recurse array_recurse hash_recurse2 array_recurse2
215
216

	 TBExptMinMaxNodes TBExptSecurityLevel TBExptIDX
217
218
	 TBDB_SECLEVEL_GREEN TBDB_SECLEVEL_BLUE TBDB_SECLEVEL_YELLOW
	 TBDB_SECLEVEL_ORANGE TBDB_SECLEVEL_RED TBDB_SECLEVEL_ZAPDISK
Leigh B. Stoller's avatar
Leigh B. Stoller committed
219
220

	 TBExptSetPanicBit TBExptGetPanicBit TBExptClearPanicBit
221
222
223
224

	 TB_NODEHISTORY_OP_FREE TB_NODEHISTORY_OP_ALLOC TB_NODEHISTORY_OP_MOVE
	 TBSetNodeHistory

Timothy Stack's avatar
 
Timothy Stack committed
225
226
	 TBGetOSBootCmd

227
228
	 TBRobotLabExpt

229
	 TBExptContainsNodeCT
230
	 );
231

232
use emdb;
233
use emutil;
Kevin Atkinson's avatar
 
Kevin Atkinson committed
234
use libtblog_simple;
235
use English;
236
use File::Basename;
237
use POSIX qw(strftime);
238
239
240
241

# This line has to come before the requires.
@EXPORT = (@emutil::EXPORT, @emdb::EXPORT, @EXPORT);
# These are requires because of cross dependency hell.
242
243
244
require User;
require Project;
require Group;
245
246
require Node;
require NodeType;
247
require Lan;
248

249
250
251
# Configure variables
my $TB		= "@prefix@";
my $DBNAME	= "@TBDBNAME@";
252
my $TBOPS       = "@TBOPSEMAIL@";
253
254
my $EVENTSYS    = "@EVENTSYS@";
my $BOSSNODE    = "@BOSSNODE@";
255
my $TESTMODE    = @TESTMODE@;
256
my $TBOPSPID	= "emulab-ops";
257
my $EXPTLOGNAME = "activity.log";
258
my $PROJROOT    = "@PROJROOT_DIR@";
259

260
261
262
263
if ($EVENTSYS) {
    require event;
    import event;
}
264
265
sub TBdbfork()
{
266
267
268
    if ($EVENTSYS) {
	EventFork();
    }
269
270
}

271
272
273
274
#
# Needs to be config'ed.
#
sub TBDB_EXPT_WORKDIR()		{ "/usr/testbed/expwork"; }
Mac Newbold's avatar
Mac Newbold committed
275

276
277
278
279
280
#
# 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.
#
281
sub NODERELOADING_PID()		{ $TBOPSPID; }
282
sub NODERELOADING_EID()		{ "reloading"; }
283
sub NODERELOADPENDING_EID()	{ "reloadpending"; }
Timothy Stack's avatar
 
Timothy Stack committed
284
285
286
sub NODEREPOSITIONING_PID()	{ $TBOPSPID; }
sub NODEREPOSITIONING_EID()	{ "repositioning"; }
sub NODEREPOSPENDING_EID()	{ "repositionpending"; }
287
sub NODEDEAD_PID()		{ $TBOPSPID; }
288
sub NODEDEAD_EID()		{ "hwdown"; }
289
290
sub NODEILL_PID()		{ $TBOPSPID; }
sub NODEILL_EID()		{ "hwcheckup"; }
291
292
sub PLABMOND_PID()		{ $TBOPSPID; }
sub PLABMOND_EID()		{ "plab-monitor"; }
Kirk Webb's avatar
 
Kirk Webb committed
293
294
sub PLABTESTING_PID()		{ $TBOPSPID; }
sub PLABTESTING_EID()		{ "plab-testing"; }
295
sub PLABHOLDING_PID()		{ $TBOPSPID; }
Kirk Webb's avatar
 
Kirk Webb committed
296
sub PLABHOLDING_EID()		{ "plabnodes"; }
Kirk Webb's avatar
 
Kirk Webb committed
297
sub PLABDOWN_PID()		{ $TBOPSPID; }
Kirk Webb's avatar
 
Kirk Webb committed
298
sub PLABDOWN_EID()		{ "hwdown"; }
299
300
sub OLDRESERVED_PID()		{ $TBOPSPID; }
sub OLDRESERVED_EID()		{ "oldreserved"; }
301
302
sub NFREELOCKED_PID()		{ $TBOPSPID; }
sub NFREELOCKED_EID()		{ "nfree-locked"; }
Robert Ricci's avatar
Robert Ricci committed
303
sub TBOPSPID()			{ $TBOPSPID; }
304
sub EXPTLOGNAME()		{ $EXPTLOGNAME; }
305
306
307
308
309
310

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

311
312
313
314
sub NODEFAILMODE_FATAL()	{ "fatal"; }
sub NODEFAILMODE_NONFATAL()	{ "nonfatal"; }
sub NODEFAILMODE_IGNORE()	{ "ignore"; }

315
# Experiment states
316
317
318
sub EXPTSTATE_NEW()		{ "new"; }
sub EXPTSTATE_PRERUN()		{ "prerunning"; }
sub EXPTSTATE_SWAPPED()		{ "swapped"; }
319
sub EXPTSTATE_QUEUED()		{ "queued"; }
320
321
322
sub EXPTSTATE_SWAPPING()	{ "swapping"; }
sub EXPTSTATE_ACTIVATING()	{ "activating"; }
sub EXPTSTATE_ACTIVE()		{ "active"; }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
323
sub EXPTSTATE_PANICED()		{ "paniced"; }
324
325
sub EXPTSTATE_TERMINATING()	{ "terminating"; }
sub EXPTSTATE_TERMINATED()	{ "ended"; }
326
327
328
329
330
331
332
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";}
333

334
# Cancel flags
335
336
337
sub EXPTCANCEL_CLEAR()		{ 0 ;}
sub EXPTCANCEL_TERM()		{ 1 ;}
sub EXPTCANCEL_SWAP()		{ 2 ;}
338
sub EXPTCANCEL_DEQUEUE()	{ 3 ;}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
339

340
341
sub USERSTATUS_ACTIVE()		{ "active"; }
sub USERSTATUS_FROZEN()		{ "frozen"; }
342
343
344
sub USERSTATUS_UNAPPROVED()	{ "unapproved"; }
sub USERSTATUS_UNVERIFIED()	{ "unverified"; }
sub USERSTATUS_NEWUSER()	{ "newuser"; }
345
sub USERSTATUS_ARCHIVED()	{ "archived"; }
346

347
348
349
#
# 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
350
#
351
352
sub PROJMEMBERTRUST_NONE()	{ 0; }
sub PROJMEMBERTRUST_USER()	{ 1; }
353
sub PROJMEMBERTRUST_ROOT()	{ 2; }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
354
355
356
357
358
359
360
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
361
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
362
363
364
365
366
367
# 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; }
368
sub TB_NODEACCESS_MODIFYVLANS()	{ 6; }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
369
sub TB_NODEACCESS_MIN()		{ TB_NODEACCESS_READINFO; }
370
sub TB_NODEACCESS_MAX()		{ TB_NODEACCESS_MODIFYVLANS; }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
371
372
373
374
375
376
377

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

378
# Experiments.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
379
380
381
sub TB_EXPT_READINFO()		{ 1; }
sub TB_EXPT_MODIFY()		{ 2; }
sub TB_EXPT_DESTROY()		{ 3; }
382
sub TB_EXPT_UPDATE()		{ 4; }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
383
sub TB_EXPT_MIN()		{ TB_EXPT_READINFO; }
384
sub TB_EXPT_MAX()		{ TB_EXPT_UPDATE; }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
385
386
387
388
389

# Projects.
sub TB_PROJECT_READINFO()	{ 1; }
sub TB_PROJECT_MAKEGROUP()	{ 2; }
sub TB_PROJECT_EDITGROUP()	{ 3; }
Chad Barb's avatar
 
Chad Barb committed
390
sub TB_PROJECT_GROUPGRABUSERS() { 4; }
Chad Barb's avatar
 
Chad Barb committed
391
392
393
394
395
396
397
398
399
400
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
401
402
403
sub TB_PROJECT_MIN()		{ TB_PROJECT_READINFO; }
sub TB_PROJECT_MAX()		{ TB_PROJECT_CREATEEXPT; }

Mac Newbold's avatar
Mac Newbold committed
404
# OSIDs
Leigh B. Stoller's avatar
Leigh B. Stoller committed
405
406
407
408
409
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; }
410
411
412
sub TB_OSID_OSIDLEN()		{ 35; }
sub TB_OSID_OSNAMELEN()		{ 20; }
sub TB_OSID_VERSLEN()		{ 12; }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
413

414
415
# Magic OSID constants
sub TB_OSID_MBKERNEL()          { "_KERNEL_"; } # multiboot kernel OSID
416
417
418
419

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

Leigh B. Stoller's avatar
Leigh B. Stoller committed
421
# ImageIDs
422
423
424
425
426
#
# 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
427
428
429
430
431
432
433
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; }
434
435
sub TB_IMAGEID_IMAGEIDLEN()	{ 45; }
sub TB_IMAGEID_IMAGENAMELEN()	{ 30; }
436

437
# Node Log Types
438
439
440
441
sub TB_NODELOGTYPE_MISC		{ "misc"; }
sub TB_NODELOGTYPES()		{ ( TB_NODELOGTYPE_MISC ) ; }
sub TB_DEFAULT_NODELOGTYPE()	{ TB_NODELOGTYPE_MISC; }

442
443
444
445
446
# Node History Stuff.
sub TB_NODEHISTORY_OP_FREE	{ "free"; }
sub TB_NODEHISTORY_OP_ALLOC	{ "alloc"; }
sub TB_NODEHISTORY_OP_MOVE	{ "move"; }

447
448
449
# Reload Types.
sub TB_RELOADTYPE_NETDISK()	{ "netdisk"; }
sub TB_RELOADTYPE_FRISBEE()	{ "frisbee"; }
450
sub TB_DEFAULT_RELOADTYPE()	{ TB_RELOADTYPE_FRISBEE; }
451

452
453
454
455
456
457
458
# Experiment priorities.
sub TB_EXPTPRIORITY_LOW()	{ 0; }
sub TB_EXPTPRIORITY_HIGH()	{ 20; }

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

459
460
461
# System PID.
sub TB_OPSPID()			{ $TBOPSPID; }

462
#
463
464
465
466
# Events we may want to send
#
sub TBDB_TBEVENT_NODESTATE	{ "TBNODESTATE"; }
sub TBDB_TBEVENT_NODEOPMODE	{ "TBNODEOPMODE"; }
467
sub TBDB_TBEVENT_CONTROL	{ "TBCONTROL"; }
468
sub TBDB_TBEVENT_COMMAND	{ "TBCOMMAND"; }
469
sub TBDB_TBEVENT_EXPTSTATE	{ "TBEXPTSTATE"; }
470
471
472
473

#
# For nodes, we use this set of events.
#
474
sub TBDB_NODESTATE_ISUP()	{ "ISUP"; }
475
sub TBDB_NODESTATE_ALWAYSUP()	{ "ALWAYSUP"; }
476
477
478
479
480
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"; }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
481
482
sub TBDB_NODESTATE_RELOADSETUP(){ "RELOADSETUP"; }
sub TBDB_NODESTATE_MFSSETUP()   { "MFSSETUP"; }
483
sub TBDB_NODESTATE_TBFAILED()	{ "TBFAILED"; }
484
485
sub TBDB_NODESTATE_RELOADING()	{ "RELOADING"; }
sub TBDB_NODESTATE_RELOADDONE()	{ "RELOADDONE"; }
486
sub TBDB_NODESTATE_RELOADDONE_V2(){ "RELOADDONEV2"; }
487
sub TBDB_NODESTATE_UNKNOWN()	{ "UNKNOWN"; };
488
sub TBDB_NODESTATE_PXEWAIT()	{ "PXEWAIT"; }
489
sub TBDB_NODESTATE_PXELIMBO()	{ "PXELIMBO"; }
490
sub TBDB_NODESTATE_PXEWAKEUP()	{ "PXEWAKEUP"; }
491
sub TBDB_NODESTATE_PXEFAILED()	{ "PXEFAILED"; }
492
sub TBDB_NODESTATE_PXEBOOTING()	{ "PXEBOOTING"; }
493
sub TBDB_NODESTATE_POWEROFF()	{ "POWEROFF"; }
494

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

510
511
512
513
514
sub TBDB_COMMAND_REBOOT         { "REBOOT"; }
sub TBDB_COMMAND_POWEROFF       { "POWEROFF"; }
sub TBDB_COMMAND_POWERON        { "POWERON"; }
sub TBDB_COMMAND_POWERCYCLE     { "POWERCYCLE"; }

515
516
517
518
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
519
520
521
sub TBDB_ALLOCSTATE_FREE_CLEAN()       { "FREE_CLEAN"; }
sub TBDB_ALLOCSTATE_FREE_DIRTY()       { "FREE_DIRTY"; }
sub TBDB_ALLOCSTATE_DOWN()             { "DOWN"; }
522
sub TBDB_ALLOCSTATE_DEAD()             { "DEAD"; }
Chad Barb's avatar
 
Chad Barb committed
523
524
525
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
526
527
sub TBDB_ALLOCSTATE_RES_REBOOT_DIRTY() { "RES_REBOOT_DIRTY"; }
sub TBDB_ALLOCSTATE_RES_REBOOT_CLEAN() { "RES_REBOOT_CLEAN"; }
528
529
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
530
sub TBDB_ALLOCSTATE_RES_READY()        { "RES_READY"; }
531
sub TBDB_ALLOCSTATE_RES_RECONFIG()     { "RES_RECONFIG"; }
532
sub TBDB_ALLOCSTATE_RES_TEARDOWN()     { "RES_TEARDOWN"; }
Chad Barb's avatar
 
Chad Barb committed
533
534
sub TBDB_ALLOCSTATE_UNKNOWN()          { "UNKNOWN"; };

535
536
sub TBDB_TBCONTROL_RESET	{ "RESET"; }
sub TBDB_TBCONTROL_RELOADDONE	{ "RELOADDONE"; }
537
sub TBDB_TBCONTROL_RELOADDONE_V2{ "RELOADDONEV2"; }
538
sub TBDB_TBCONTROL_TIMEOUT	{ "TIMEOUT"; }
Mac Newbold's avatar
Mac Newbold committed
539
540
541
sub TBDB_TBCONTROL_PXEBOOT	{ "PXEBOOT"; }
sub TBDB_TBCONTROL_BOOTING	{ "BOOTING"; }
sub TBDB_TBCONTROL_CHECKGENISUP	{ "CHECKGENISUP"; }
542
543
544
545

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

546
547
548
549
550
551
#
# 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"; }

552
553
554
#
# We should list all of the DB limits.
#
555
sub DBLIMIT_NSFILESIZE()	{ (2**24 - 1); }
556

557
558
559
560
561
562
563
564
565
566
567
#
# 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;   }

568
569
570
571
572
573
574
575
#
# 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"; }
576
sub TBDB_STATS_SWAPMODIFY()	{ "swapmod"; }
577
sub TBDB_STATS_SWAPUPDATE()	{ "swapupdate"; }
578
sub TBDB_STATS_FLAGS_IDLESWAP()	{ 0x01; }
579
sub TBDB_STATS_FLAGS_PREMODIFY(){ 0x02; }
580
sub TBDB_STATS_FLAGS_START()    { 0x04; }
581
sub TBDB_STATS_FLAGS_PRESWAPIN(){ 0x08; }
582
sub TBDB_STATS_FLAGS_BATCHCTRL(){ 0x10; }
583
sub TBDB_STATS_FLAGS_MODHOSED() { 0x20; }
584
sub TBDB_STATS_FLAGS_MODSWAPOUT() { 0x40; }
585
# Do not export these variables!
586
my $TBDB_STATS_STARTCLOCK;
587
my $TBDB_STATS_SAVEDSWAPUID;
588

589
590
591
592
# Jail.
sub TBDB_JAILIPBASE()		{ "@JAILIPBASE@"; }
sub TBDB_JAILIPMASK()		{ "@JAILIPMASK@"; }

593
594
595
596
# Reserved node "roles"
sub TBDB_RSRVROLE_NODE()	{ "node"; }
sub TBDB_RSRVROLE_VIRTHOST()	{ "virthost"; }
sub TBDB_RSRVROLE_DELAYNODE()	{ "delaynode"; }
597
sub TBDB_RSRVROLE_SIMHOST()	{ "simhost"; }
598

599
600
601
602
603
# Interfaces roles.
sub TBDB_IFACEROLE_CONTROL()	{ "ctrl"; }
sub TBDB_IFACEROLE_EXPERIMENT()	{ "expt"; }
sub TBDB_IFACEROLE_JAIL()	{ "jail"; }
sub TBDB_IFACEROLE_FAKE()	{ "fake"; }
604
sub TBDB_IFACEROLE_GW()		{ "gw"; }
605
sub TBDB_IFACEROLE_OTHER()	{ "other"; }
606
sub TBDB_IFACEROLE_OUTER_CONTROL(){ "outer_ctrl"; }
607

608
609
610
611
612
613
# Routertypes.
sub TBDB_ROUTERTYPE_NONE()	{ "none"; }
sub TBDB_ROUTERTYPE_OSPF()	{ "ospf"; }
sub TBDB_ROUTERTYPE_STATIC()	{ "static"; }
sub TBDB_ROUTERTYPE_MANUAL()	{ "manual"; }

614
615
616
617
# User Interface types.
sub TBDB_USER_INTERFACE_EMULAB(){ "emulab"; }
sub TBDB_USER_INTERFACE_PLAB()	{ "plab"; }

618
# Key Stuff
619
sub TBDB_EVENTKEY($$)	{ TBExptUserDir($_[0],$_[1]) . "/tbdata/eventkey"; }
620
sub TBDB_WEBKEY($$)	{ TBExptUserDir($_[0],$_[1]) . "/tbdata/webkey"; }
621

622
623
# Security Levels.
sub TBDB_SECLEVEL_GREEN()	{ 0; }
624
625
626
627
628
629
630
sub TBDB_SECLEVEL_BLUE()	{ 1; }
sub TBDB_SECLEVEL_YELLOW()	{ 2; }
sub TBDB_SECLEVEL_ORANGE()	{ 3; }
sub TBDB_SECLEVEL_RED()		{ 4; }

# This is the level at which we get extremely cautious when swapping out
sub TBDB_SECLEVEL_ZAPDISK()	{ TBDB_SECLEVEL_YELLOW; }
631

632
633
634
635
636
637
638
639
640
641
642
#
# 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' ],
643
	'interface_state'	=> [ 'node_id' ],
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
	'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' ],
661
	'outlets_remoteauth'	=> [ 'node_id' ],
662
663
664
665
666
667
668
669
670
671
672
	'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' ],
	'uidnodelastlogin'	=> [ 'node_id' ],
	'v2pmap'		=> [ 'node_id' ],
673
	'vinterfaces'		=> [ 'node_id' ],
674
	'widearea_nodeinfo'	=> [ 'node_id' ],
675
676
677
678
	'widearea_accounts'	=> [ 'node_id' ],
	'widearea_delays'	=> [ 'node_id1', 'node_id2' ],
	'widearea_recent'	=> [ 'node_id1', 'node_id2' ],
	'wires'			=> [ 'node_id1', 'node_id2' ],
679
680
681
	'node_startloc'		=> [ 'node_id' ],
	'node_history'		=> [ 'node_id' ],
	'node_bootlogs'		=> [ 'node_id' ],
Kirk Webb's avatar
 
Kirk Webb committed
682
        'plab_mapping'          => [ 'node_id' ],
683
        'node_utilization'      => [ 'node_id' ],
684
685
686
    );
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
687
688
689
690
691
692
693
694
695
696
697
698
699
700
#
# 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
701
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
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
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
    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
747

Leigh B. Stoller's avatar
Leigh B. Stoller committed
748
749
750
751
752
753
    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
754
755
# Return that trust level as one of the numeric values above.
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
756
757
758
# 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
759
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
760
761
762
763
764
765
766
sub TBGrpTrust($$$)
{
    my ($uid, $pid, $gid) = @_;

    #
    # No group, then use the default group.
    #
767
768
    $gid = $pid
	if (! $gid);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
769

770
    #
771
    # Must map to an existing user to be trusted, obviously.
772
773
774
775
776
    #
    my $target_user = User->Lookup($uid);
    return PROJMEMBERTRUST_NONE
	if (! defined($target_user));

777
778
    # Ditto the group.
    my $target_group = Group->Lookup($pid, $gid);
779
    return PROJMEMBERTRUST_NONE
780
	if (! defined($target_group));
Leigh B. Stoller's avatar
Leigh B. Stoller committed
781

782
    return $target_group->Trust($target_user)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
783
784
785
786
787
788
789
790
791
}

#
# 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
792
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
793
794
795
sub TBProjTrust($$)
{
    my ($uid, $pid) = @_;
Mac Newbold's avatar
Mac Newbold committed
796

Leigh B. Stoller's avatar
Leigh B. Stoller committed
797
798
799
    return TBGrpTrust($uid, $pid, $pid);
}

800
#
801
802
# Test admin status. Ignore argument; we only care if the current user
# has admin privs turned on.
803
#
804
# usage: TBAdmin();
805
806
#        returns 1 if an admin type.
#        returns 0 if a mere user.
Mac Newbold's avatar
Mac Newbold committed
807
#
808
809
sub TBAdmin(;$)
{
810
811
812
    my $this_user = User->ThisUser();
    return 0
	if (! defined($this_user));
813

814
    return $this_user->IsAdmin();
815
816
}

817
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
818
819
# Project permission checks. The group id (gid) can be undef, in which case
# the pid is used (ie: a default group check is made).
820
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
821
822
823
# Usage: TBProjAccessCheck($uid, $pid, $gid, $access_type)
#	 returns 0 if not allowed.
#        returns 1 if allowed.
Mac Newbold's avatar
Mac Newbold committed
824
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
825
sub TBProjAccessCheck($$$$)
826
{
Leigh B. Stoller's avatar
Leigh B. Stoller committed
827
828
829
    my ($uid, $pid, $gid, $access_type) = @_;

    #
830
    # Must map to an existing user to be trusted, obviously.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
831
    #
832
    my $target_user = User->LookupByUnixId($uid);
833
834
    return 0
	if (! defined($target_user));
835

836
837
838
839
    # Ditto the group
    my $target_group = Group->Lookup($pid, $gid);
    return 0
	if (! defined($target_group));
Leigh B. Stoller's avatar
Leigh B. Stoller committed
840

841
    return $target_group->AccessCheck($target_user, $access_type);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
842
843
844
845
846
847
848
849
}

#
# 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
850
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
851
852
853
sub TBExptAccessCheck($$$$)
{
    my ($uid, $pid, $eid, $access_type) = @_;
854
855

    #
856
    # Must map to an existing user to be trusted, obviously.
Mac Newbold's avatar
Mac Newbold committed
857
    #
858
    my $target_user = User->LookupByUnixId($uid);
859
860
    return 0
	if (! defined($target_user));
Leigh B. Stoller's avatar
Leigh B. Stoller committed
861

862
863
864
865
    # Ditto the group
    my $experiment = Experiment->Lookup($pid, $eid);
    return 0
	if (! defined($experiment));
Leigh B. Stoller's avatar
Leigh B. Stoller committed
866

867
    return $experiment->AccessCheck($target_user, $access_type);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
868
869
870
871
872
873
874
875
}

#
# 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
876
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
877
878
879
880
sub TBNodeAccessCheck($$@)
{
    my ($uid, $access_type) = (shift, shift);
    my @nodelist = @_;
881
882

    #
883
    # Must map to an existing user to be trusted, obviously.
Mac Newbold's avatar
Mac Newbold committed
884
    #
885
    my $target_user = User->LookupByUnixId($uid);
886
887
888
889
    return 0
	if (! defined($target_user));
    return 1
	if ($target_user->IsAdmin());
890

891
892
893
894
    foreach my $nodeid (@nodelist) {
	my $node = Node->Lookup($nodeid);
	return 0
	    if (!defined($node));
Leigh B. Stoller's avatar
Leigh B. Stoller committed
895

896
897
	return 0
	    if (!$node->AccessCheck($target_user, $access_type));
898
899
900
901
    }
    return 1;
}

902
903
904
905
906
907
#
# Return Experiment state.
#
# usage: ExpState(char *pid, char *eid)
#        returns state if a valid pid/eid.
#        returns 0 if an invalid pid/eid or if an error.