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

Leigh Stoller's avatar
Leigh Stoller committed
3 4
#
# EMULAB-COPYRIGHT
5
# Copyright (c) 2000-2009 University of Utah and the Flux Group.
Leigh Stoller's avatar
Leigh 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
# 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 Stoller's avatar
Leigh Stoller committed
31
	 PROJMEMBERTRUST_ROOT PROJMEMBERTRUST_GROUPROOT
32
	 PROJMEMBERTRUST_PROJROOT PROJMEMBERTRUST_LOCALROOT
33
	 NODEILL_PID NODEILL_EID
Leigh Stoller's avatar
Leigh Stoller committed
34

35
	 TBOPSPID EXPTLOGNAME
36
	 PLABMOND_PID PLABMOND_EID PLABHOLDING_PID PLABHOLDING_EID
37
         PLABTESTING_PID PLABTESTING_EID PLABDOWN_PID PLABDOWN_EID
38

39
	 TBTrustConvert TBMinTrust TBGrpTrust TBProjTrust MapNumericUID
Leigh Stoller's avatar
Leigh 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 Stoller's avatar
Leigh Stoller committed
45

46 47
	 NODEFAILMODE_FATAL NODEFAILMODE_NONFATAL NODEFAILMODE_IGNORE

Leigh Stoller's avatar
Leigh 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 Stoller's avatar
Leigh Stoller committed
55 56 57 58
	 TB_EXPT_MIN TB_EXPT_MAX

	 TB_PROJECT_READINFO TB_PROJECT_MAKEGROUP
	 TB_PROJECT_EDITGROUP TB_PROJECT_DELGROUP
59
	 TB_PROJECT_GROUPGRABUSERS TB_PROJECT_BESTOWGROUPROOT
Leigh Stoller's avatar
Leigh 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 Stoller's avatar
Leigh 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 Stoller's avatar
Leigh Stoller committed
74
	 DBLIMIT_NSFILESIZE NODERELOADPENDING_EID
75

76 77
	 NODEREPOSITIONING_PID NODEREPOSITIONING_EID NODEREPOSPENDING_EID

78
	 EXPTSTATE_NEW EXPTSTATE_PRERUN EXPTSTATE_SWAPPED EXPTSTATE_SWAPPING
Leigh Stoller's avatar
Leigh 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 Stoller's avatar
Leigh 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
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
106
	 TBDB_NODESTATE_PXEBOOTING TBDB_NODESTATE_ALWAYSUP
107
	 TBDB_NODESTATE_MFSSETUP TBDB_NODESTATE_TBFAILED
108
	 TBDB_NODESTATE_POWEROFF
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
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

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
130
	 TBDB_ALLOCSTATE_RES_READY TBDB_ALLOCSTATE_UNKNOWN
131
	 TBDB_ALLOCSTATE_RES_TEARDOWN TBDB_ALLOCSTATE_DEAD
132
	 TBDB_ALLOCSTATE_RES_RECONFIG
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
147
	 TBNodeEventStateUpdated
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 Stoller's avatar
Leigh 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 Stoller's avatar
Leigh Stoller committed
181
	 TBPlabNodeUsername MarkPhysNodeDown
182 183 184
	 TBExptIsElabInElab TBExptIsPlabInElab
	 TBExptPlabInElabPLC TBExptPlabInElabNodes
	 TBBatchUnLockExp TBExptIsBatchExp
185
	 
186 187
	 TBExptFirewall TBNodeFirewall TBExptFirewallAndPort
	 TBSetExptFirewallVlan TBClearExptFirewallVlan
188
	 TBNodeConsoleTail TBExptGetSwapoutAction TBExptGetSwapState
189

190
	 TBNodeSubNodes
191
	 TBNodeAdminOSID TBNodeDiskloadOSID
192
	 TBNodeType TBNodeTypeProcInfo TBNodeTypeBiosWaittime
193

194 195
	 TBExptPortRange

196
	 TBDB_WIDEAREA_LOCALNODE
197
	 TBWideareaNodeID TBTipServers
Mac Newbold's avatar
Mac Newbold committed
198

199
	 TBSiteVarExists TBGetSiteVar TBSetSiteVar
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 Stoller's avatar
Leigh Stoller committed
218 219

	 TBExptSetPanicBit TBExptGetPanicBit TBExptClearPanicBit
220 221 222 223

	 TB_NODEHISTORY_OP_FREE TB_NODEHISTORY_OP_ALLOC TB_NODEHISTORY_OP_MOVE
	 TBSetNodeHistory

224 225
	 TBGetOSBootCmd

226 227
	 TBRobotLabExpt

228
	 TBExptContainsNodeCT
229
	 );
230

231
use emdb;
232
use emutil;
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"; }
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"; }
292 293
sub PLABTESTING_PID()		{ $TBOPSPID; }
sub PLABTESTING_EID()		{ "plab-testing"; }
294
sub PLABHOLDING_PID()		{ $TBOPSPID; }
295
sub PLABHOLDING_EID()		{ "plabnodes"; }
296
sub PLABDOWN_PID()		{ $TBOPSPID; }
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 Stoller's avatar
Leigh 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 Stoller's avatar
Leigh 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 Stoller's avatar
Leigh 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 Stoller's avatar
Leigh 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 Stoller's avatar
Leigh Stoller committed
368
sub TB_NODEACCESS_MIN()		{ TB_NODEACCESS_READINFO; }
369
sub TB_NODEACCESS_MAX()		{ TB_NODEACCESS_MODIFYVLANS; }
Leigh Stoller's avatar
Leigh 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 Stoller's avatar
Leigh 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 Stoller's avatar
Leigh Stoller committed
382
sub TB_EXPT_MIN()		{ TB_EXPT_READINFO; }
383
sub TB_EXPT_MAX()		{ TB_EXPT_UPDATE; }
Leigh Stoller's avatar
Leigh Stoller committed
384 385 386 387 388

# Projects.
sub TB_PROJECT_READINFO()	{ 1; }
sub TB_PROJECT_MAKEGROUP()	{ 2; }
sub TB_PROJECT_EDITGROUP()	{ 3; }
389
sub TB_PROJECT_GROUPGRABUSERS() { 4; }
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 Stoller's avatar
Leigh 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 Stoller's avatar
Leigh 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 Stoller's avatar
Leigh 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 Stoller's avatar
Leigh 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 Stoller's avatar
Leigh 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 Stoller's avatar
Leigh 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_PXEWAKEUP()	{ "PXEWAKEUP"; }
489
sub TBDB_NODESTATE_PXEFAILED()	{ "PXEFAILED"; }
490
sub TBDB_NODESTATE_PXEBOOTING()	{ "PXEBOOTING"; }
491
sub TBDB_NODESTATE_POWEROFF()	{ "POWEROFF"; }
492

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

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

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

517 518 519
sub TBDB_ALLOCSTATE_FREE_CLEAN()       { "FREE_CLEAN"; }
sub TBDB_ALLOCSTATE_FREE_DIRTY()       { "FREE_DIRTY"; }
sub TBDB_ALLOCSTATE_DOWN()             { "DOWN"; }
520
sub TBDB_ALLOCSTATE_DEAD()             { "DEAD"; }
521 522 523
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
524 525
sub TBDB_ALLOCSTATE_RES_REBOOT_DIRTY() { "RES_REBOOT_DIRTY"; }
sub TBDB_ALLOCSTATE_RES_REBOOT_CLEAN() { "RES_REBOOT_CLEAN"; }
526 527
sub TBDB_ALLOCSTATE_RES_INIT_DIRTY()   { "RES_INIT_DIRTY"; }
sub TBDB_ALLOCSTATE_RES_INIT_CLEAN()   { "RES_INIT_CLEAN"; }
528
sub TBDB_ALLOCSTATE_RES_READY()        { "RES_READY"; }
529
sub TBDB_ALLOCSTATE_RES_RECONFIG()     { "RES_RECONFIG"; }
530
sub TBDB_ALLOCSTATE_RES_TEARDOWN()     { "RES_TEARDOWN"; }
531 532
sub TBDB_ALLOCSTATE_UNKNOWN()          { "UNKNOWN"; };

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

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

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

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

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

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

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

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

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

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

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

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

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

629 630 631 632 633 634 635 636 637 638 639
#
# 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' ],
640
	'interface_state'	=> [ 'node_id' ],
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 668
	'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' ],
	'uidnodelastlogin'	=> [ 'node_id' ],
	'v2pmap'		=> [ 'node_id' ],
669
	'vinterfaces'		=> [ 'node_id' ],
670
	'widearea_nodeinfo'	=> [ 'node_id' ],
671 672 673 674
	'widearea_accounts'	=> [ 'node_id' ],
	'widearea_delays'	=> [ 'node_id1', 'node_id2' ],
	'widearea_recent'	=> [ 'node_id1', 'node_id2' ],
	'wires'			=> [ 'node_id1', 'node_id2' ],
675 676 677
	'node_startloc'		=> [ 'node_id' ],
	'node_history'		=> [ 'node_id' ],
	'node_bootlogs'		=> [ 'node_id' ],
678
        'plab_mapping'          => [ 'node_id' ],
679
        'node_utilization'      => [ 'node_id' ],
680 681 682
    );
}

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

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

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

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

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

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

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

Leigh Stoller's avatar
Leigh Stoller committed
793 794 795
    return TBGrpTrust($uid, $pid, $pid);
}

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

810
    return $this_user->IsAdmin();
811 812
}

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

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

832 833 834 835
    # Ditto the group
    my $target_group = Group->Lookup($pid, $gid);
    return 0
	if (! defined($target_group));
Leigh Stoller's avatar
Leigh Stoller committed
836

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

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

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

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

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

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

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

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

892 893
	return 0
	    if (!$node->AccessCheck($target_user, $access_type));
894 895 896 897
    }
    return 1;
}

898 899 900 901 902 903
#
# 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.
Mac Newbold's avatar
Mac Newbold committed
904
#
905 906
sub ExpState($$)
{
907
    my ($pid, $eid) = @_;
908

909
    my $experiment = Experiment->Lookup($pid, $eid);
910
    return 0
911 912
	if (!defined($experiment));
    return $experiment->state();
913 914
}

915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943
#
# Helper function for batch system.
#
sub TBBatchUnLockExp($$;$)
{
    my($pid, $eid, $newstate) = @_;
    my $BSTATE_UNLOCKED       = BATCHSTATE_UNLOCKED;
    
    my $query_result =
	DBQueryWarn("update experiments set expt_locked=NULL, ".
		    "       batchstate='$BSTATE_UNLOCKED' ".
		    (defined($newstate) ? ",state='$newstate' " : "") .
		    "where eid='$eid' and pid='$pid'");
    
    if (! $query_result ||
	$query_result->numrows == 0) {
	return 0;
    }
    
    if ($EVENTSYS && defined($newstate)) {
	EventSendWarn(objtype   => TBDB_TBEVENT_EXPTSTATE,
		      objname   => "$pid/$eid",
		      eventtype => $newstate,
		      expt      => "$pid/$eid",
		      host      => $BOSSNODE);
    }
    return 1;
}

Leigh Stoller's avatar
Leigh Stoller committed
944
#
945
# Set cancel flag,
Leigh Stoller's avatar
Leigh Stoller committed
946
#
947
# usage: SetCancelFlag(char *pid, char *eid, char *flag)
948 949
#        returns 1 if okay.
#        returns 0 if an invalid pid/eid or if an error.
Mac Newbold's avatar
Mac Newbold committed
950
#
951
sub TBSetCancelFlag($$$)
952 953 954 955 956 957 958 959 960 961 962 963 964 965
{
    my($pid, $eid, $flag) = @_;

    my $query_result =
	DBQueryWarn("update experiments set canceled='$flag' ".
		    "where eid='$eid' and pid='$pid'");

    if (! $query_result ||
	$query_result->numrows == 0) {
	return 0;
    }
    return 1;
}

966
#
967
# Get cancel flag,
968
#
969
# usage: TBGetCancelFlag(char *pid, char *eid, char **flag)
970 971
#        returns 1 if okay.
#        returns 0 if an invalid pid/eid or if an error.
Mac Newbold's avatar
Mac Newbold committed
972
#
973
sub TBGetCancelFlag($$$)
974 975 976 977 978 979 980 981 982 983 984 985 986 987 988
{
    my($pid, $eid, $flag) = @_;

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

    if (! $query_result ||
	$query_result->numrows == 0) {
	return 0;
    }
    ($$flag) = $query_result->fetchrow_array();
    return 1;
}

989 990 991
#
# Return a list of all the nodes in an experiment.
#
992
# usage: ExpNodes(char *pid, char *eid, [bool islocal])
993
#        returns the list if a valid pid/eid.
994 995
#	 If the optional flag is set, returns only local nodes.
#        Returns 0 if an invalid pid/eid or if an error.
Mac Newbold's avatar
Mac Newbold committed
996
#
997
sub ExpNodes($$;$$)
998
{
999
    my($pid, $eid, $localonly, $physonly) = @_;
1000 1001
    my(@row);
    my(@nodes);
1002
    my $clause = "";
1003

Leigh Stoller's avatar
Leigh Stoller committed
1004
    if (defined($localonly) && $localonly) {
1005 1006
	$clause .= " and nt.isremotenode=0";
    }
Leigh Stoller's avatar
Leigh Stoller committed
1007
    if (defined($physonly) && $physonly) {
1008
	$clause .= " and nt.isvirtnode=0";
1009
    }
1010

1011
    my $query_result =
1012 1013 1014 1015
	DBQueryWarn("select r.node_id from reserved as r ".
		    "left join nodes as n on n.node_id=r.node_id ".
		    "left join node_types as nt on nt.type=n.type ".
		    "where r.pid='$pid' and r.eid='$eid' $clause");
1016 1017 1018 1019 1020 1021

    if (! $query_result or
	$query_result->numrows == 0) {
	return ();
    }
    while (@row = $query_result->fetchrow_array()) {
Mac Newbold's avatar
Mac Newbold committed
1022
	my $node = $row[0];
1023 1024 1025 1026

	#
	# Taint check. I do not understand this sillyness, but if I
	# taint check these node names, I avoid warnings throughout.
Mac Newbold's avatar
Mac Newbold committed
1027
	#
1028
	if ($node =~ /^([-\w]+)$/) {
1029
	    $node = $1;
Mac Newbold's avatar
Mac Newbold committed
1030

1031 1032 1033 1034 1035
	    push(@nodes, $node);
	}
	else {
	    print "*** $0: WARNING: Bad node name: $node.\n";
	}
1036 1037 1038 1039
    }
    return @nodes;
}

1040 1041 1042 1043
#
# Return a hash of all the nodes in an experiment.  The hash maps pnames
# to vnames.
#
1044
# usage: ExpNodeVnames(char *pid, char *eid, [bool islocal], [bool isphys])
1045
#        returns the hash if a valid pid/eid.
1046 1047
#	 If the optional islocal is set, returns only local nodes.
#	 If the optional isphys is set, returns only physical nodes.
1048 1049
#        Returns 0 if an invalid pid/eid or if an error.
#
1050
sub ExpNodeVnames($$;$$)
1051
{
1052
    my($pid, $eid, $localonly, $physonly) = @_;
1053 1054 1055 1056
    my(@row);
    my(%nodes);
    my $clause = "";

1057
    if (defined($localonly)) {
1058 1059
	$clause = "and nt.isremotenode=0";
    }
1060 1061 1062
    if (defined($physonly)) {
	$clause = "and nt.isvirtnode=0";
    }
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
    my $query_result =
	DBQueryWarn("select r.node_id,r.vname from reserved as r ".
		    "left join nodes as n on n.node_id=r.node_id ".
		    "left join node_types as nt on nt.type=n.type ".
		    "where r.pid='$pid' and r.eid='$eid' $clause");

    if (!$query_result || $query_result->numrows == 0) {
	return ();
    }
    while (@row = $query_result->fetchrow_array()) {
	my $node = $row[0];
	my $vname = $row[1];

	#
	# Taint check. I do not understand this sillyness, but if I
	# taint check these node names, I avoid warnings throughout.
	#
	if ($node =~ /^([-\w]+)$/) {
	    $node = $1;
	    if ($vname =~ /^([-\w]+)$/) {
		$vname = $1;
	    } else {
		$vname = $node;
	    }
	    $nodes{$node} = $vname;
	} else {
	    print "*** $0: WARNING: Bad node name: $node.\n";
	}
    }
    return %nodes;
}

1095
#
Mac Newbold's avatar
Mac Newbold committed
1096
# Find out what osid a node will boot next time it comes up,
1097 1098
# Usually (but not always) the currently running OS as well.
#
1099 1100 1101 1102 1103 1104 1105
sub TBBootWhat($;$)
{
    my ($node, $debug) = @_;
    $debug = 0
	if (!defined($debug));

    #
1106 1107 1108
    # WARNING!!!
    #
    # DO NOT change this function without making corresponding changes to
1109 1110
    # pxe/bootinfo_mysql.c.
    #
1111 1112
    # ALWAYS find exactly the same resulting OSID given the same inputs.
    #
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
    my $query_result =
	DBQueryWarn("select def_boot_osid, odef.op_mode, ".
		    "       temp_boot_osid, otemp.op_mode, ".
		    "       next_boot_osid, onext.op_mode ".
		    "from nodes as n ".
		    "left join os_info as odef  on odef.osid=def_boot_osid ".
		    "left join os_info as otemp on otemp.osid=temp_boot_osid ".
		    "left join os_info as onext on onext.osid=next_boot_osid ".
		    "where node_id='$node'");

    if (!$query_result || !$query_result->numrows) {
	print("*** Warning: No bootwhat info for $node\n");
1125 1126
	return 0;
    }
1127 1128 1129 1130 1131
    my ($def_boot_osid, $def_boot_opmode,
	$temp_boot_osid, $temp_boot_opmode,
	$next_boot_osid, $next_boot_opmode) = $query_result->fetchrow_array();

    #
Mac Newbold's avatar
Mac Newbold committed
1132
    # The priority would seem pretty clear.
1133 1134
    #
    return ($next_boot_osid, $next_boot_opmode)
1135
	if (defined($next_boot_osid) && $next_boot_osid ne 0);
1136
    return ($temp_boot_osid, $temp_boot_opmode)
1137
	if (defined($temp_boot_osid) && $temp_boot_osid ne 0);
1138
    return ($def_boot_osid, $def_boot_opmode)
1139
	if (defined($def_boot_osid) && $def_boot_osid ne 0);
Mac Newbold's avatar
Mac Newbold committed
1140

1141 1142
    print("*** Warning: node '$node': All boot info was null!\n");
    return undef;
1143 1144
}

1145
#
1146
# Map nodeid to its pid/eid/vname. vname is optional.
1147
#
1148
# usage: NodeidToExp(char *nodeid, \$pid, \$eid, \$vname)
1149 1150 1151
#        returns 1 if the node is reserved.
#        returns 0 if the node is not reserved.
#
1152 1153
sub NodeidToExp ($$$;$) {
    my($nodeid, $pid, $eid, $vname) = @_;
1154 1155

    my $query_result =
1156 1157
	DBQueryWarn("select pid,eid,vname from reserved ".
		    "where node_id='$nodeid'");
1158

1159 1160
    if (! $query_result ||
	! $query_result->num_rows) {
1161 1162
	return 0;
    }
Mac Newbold's avatar
Mac Newbold committed
1163

1164 1165 1166
    my @row = $query_result->fetchrow_array();
    $$pid = $row[0];
    $$eid = $row[1];
1167 1168 1169 1170 1171 1172 1173 1174
    if (defined($vname)) {
	if (defined($row[2])) {
	    $$vname = $row[2];
	}
	else {
	    $$vname = undef;
	}
    }
1175 1176 1177
    return 1;
}

1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
#
# Map a pid/eid/vname to its real nodename
#
# usage: VnameToNodeid(char *pid, char * eid, char *vname, \$nodeid)
#        returns 1 if the specified pid/eid/vname exists
#        returns 0 if it does not
#
sub VnameToNodeid ($$$$) {
    my($pid, $eid, $vname, $nodeid) = @_;

    my $query_result =
	DBQueryWarn("select node_id from reserved ".
		    "where pid='$pid' and eid='$eid' and vname='$vname'");

    if (! $query_result ||
	! $query_result->num_rows) {
	return 0;
    }
Mac Newbold's avatar
Mac Newbold committed
1196

1197 1198 1199 1200 1201 1202
    my @row = $query_result->fetchrow_array();
    $$nodeid = $row[0];
    return 1;
}


Leigh Stoller's avatar
Leigh Stoller committed
1203
#
1204
# Get the default ImageID for a particular node.
Leigh Stoller's avatar
Leigh Stoller committed
1205
#
1206 1207 1208
# usage: DefaultImageID(char *nodeid, \*imageid)
#        returns 1 if the node is valid and has a default imageid.
#                  Imageid is returned in reference param
Leigh Stoller's avatar
Leigh Stoller committed
1209 1210
#        returns 0 if there are problems.
#
1211
sub DefaultImageID ($$) {
1212
    my ($nodeid, $imageid) = @_;
Leigh Stoller's avatar
Leigh Stoller committed
1213

1214
    my $node = Node->Lookup($nodeid);
Leigh Stoller's avatar
Leigh Stoller committed
1215

1216
    if (! $node) {
1217
	$$imageid = undef;
Leigh Stoller's avatar
Leigh Stoller committed
1218 1219
	return 0;
    }
Mac Newbold's avatar
Mac Newbold committed
1220

1221 1222
    if ($node->NodeTypeAttribute("default_imageid", $imageid) ||
	$$imageid eq "") {
1223 1224 1225 1226
	$$imageid = undef;
	return 0;
    }
    return 1;
Leigh Stoller's avatar
Leigh Stoller committed
1227 1228
}

1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
#
# Convert user pid/name to internal imageid.
#
# usage: TBImageID(char *pid, char *imagename)
#        returns imageid if its valid.
#        returns 0 if not valid.
#
sub TBImageID ($$) {
    my($pid, $imagename) = @_;

    my $query_result =
	DBQueryFatal("select imageid from images ".
		     "where pid='$pid' and imagename='$imagename'");

    if (! $query_result->num_rows) {
	return 0;
    }
Mac Newbold's avatar
Mac Newbold committed
1246

1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
    my @row = $query_result->fetchrow_array();
    return $row[0];
}

#
# Convert user pid/name to internal osid.
#
# usage: TBOSID(char *pid, char *isname)
#        returns osid if its valid.
#        returns 0 if not valid.
#
1258 1259
sub TBOSID($$) {
    my ($pid, $osname) = @_;
1260

1261
    require OSinfo;
Mac Newbold's avatar
Mac Newbold committed
1262

1263 1264 1265 1266
    my $osinfo = OSinfo->Lookup($pid, $osname);
    return 0
	if (!defined($osinfo));
    return $osinfo->osid();
1267 1268
}

1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
#
# Return pid of an osid (internal name).
#
# usage: TBOsidToPid(char *osid, \$pid)
#        returns 1 if osid is valid; store pid into return arg.
#        returns 0 if osid is not valid.
#
sub TBOsidToPid ($$) {
    my($osid, $ppid) = @_;

1279
    require OSinfo;
1280

1281 1282 1283 1284
    my $osinfo = OSinfo->Lookup($osid);
    return 0
	if (!defined($osinfo));
    $$ppid = $osinfo->pid();
1285