libdb.pm.in 86.6 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

140 141
	 TBDB_JAILIPBASE TBDB_JAILIPMASK

142
	 TBDB_RSRVROLE_NODE TBDB_RSRVROLE_VIRTHOST TBDB_RSRVROLE_DELAYNODE
143
	 TBDB_RSRVROLE_SIMHOST
144

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

159 160
	 TBDB_LOWVPORT TBDB_MAXVPORT TBDB_PORTRANGE

161 162
	 TBDB_PHYSICAL_NODE_TABLES

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

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

194 195
	 TBExptPortRange

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

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

201
	 TBActivityReport GatherAssignStats
202
	 TBAvailablePCs
203

204 205
	 TBDB_IFACEROLE_CONTROL TBDB_IFACEROLE_EXPERIMENT
	 TBDB_IFACEROLE_JAIL TBDB_IFACEROLE_FAKE TBDB_IFACEROLE_OTHER
206
	 TBDB_IFACEROLE_GW TBDB_IFACEROLE_OUTER_CONTROL
207

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

	 TBExptMinMaxNodes TBExptSecurityLevel TBExptIDX
216 217
	 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
218 219

	 TBExptSetPanicBit TBExptGetPanicBit TBExptClearPanicBit
220 221 222 223

	 TB_NODEHISTORY_OP_FREE TB_NODEHISTORY_OP_ALLOC TB_NODEHISTORY_OP_MOVE
	 TBSetNodeHistory

Timothy Stack's avatar
 
Timothy Stack committed
224 225
	 TBGetOSBootCmd

226 227
	 TBRobotLabExpt

228
	 TBExptContainsNodeCT
229
	 );
230

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

#
# For nodes, we use this set of events.
#
473
sub TBDB_NODESTATE_ISUP()	{ "ISUP"; }
474
sub TBDB_NODESTATE_ALWAYSUP()	{ "ALWAYSUP"; }
475 476 477 478 479
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
480 481
sub TBDB_NODESTATE_RELOADSETUP(){ "RELOADSETUP"; }
sub TBDB_NODESTATE_MFSSETUP()   { "MFSSETUP"; }
482
sub TBDB_NODESTATE_TBFAILED()	{ "TBFAILED"; }
483 484
sub TBDB_NODESTATE_RELOADING()	{ "RELOADING"; }
sub TBDB_NODESTATE_RELOADDONE()	{ "RELOADDONE"; }
485
sub TBDB_NODESTATE_RELOADDONE_V2(){ "RELOADDONEV2"; }
486
sub TBDB_NODESTATE_UNKNOWN()	{ "UNKNOWN"; };
487
sub TBDB_NODESTATE_PXEWAIT()	{ "PXEWAIT"; }
488
sub TBDB_NODESTATE_PXELIMBO()	{ "PXELIMBO"; }
489
sub TBDB_NODESTATE_PXEWAKEUP()	{ "PXEWAKEUP"; }
490
sub TBDB_NODESTATE_PXEFAILED()	{ "PXEFAILED"; }
491
sub TBDB_NODESTATE_PXEBOOTING()	{ "PXEBOOTING"; }
492
sub TBDB_NODESTATE_POWEROFF()	{ "POWEROFF"; }
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
sub TBDB_NODEOPMODE_NORMALv1	{ "NORMALv1"; }
sub TBDB_NODEOPMODE_MINIMAL	{ "MINIMAL"; }
501
sub TBDB_NODEOPMODE_PCVM	{ "PCVM"; }
502
sub TBDB_NODEOPMODE_RELOAD	{ "RELOAD"; }
503
sub TBDB_NODEOPMODE_RELOADMOTE	{ "RELOAD-MOTE"; }
504
sub TBDB_NODEOPMODE_RELOADPCVM	{ "RELOAD-PCVM"; }
505
sub TBDB_NODEOPMODE_DELAY	{ "DELAY"; }
506
sub TBDB_NODEOPMODE_BOOTWHAT	{ "_BOOTWHAT_"; } # A redirection opmode
507 508
sub TBDB_NODEOPMODE_UNKNOWN	{ "UNKNOWN"; }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

620 621
# Security Levels.
sub TBDB_SECLEVEL_GREEN()	{ 0; }
622 623 624 625 626 627 628
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; }
629

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

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

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

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

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

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

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

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

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

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

812
    return $this_user->IsAdmin();
813 814
}

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

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

834 835 836 837
    # 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
838

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

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

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

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

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

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

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

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

894 895
	return 0
	    if (!$node->AccessCheck($target_user, $access_type));