lease_defs.php 13.3 KB
Newer Older
1 2
<?php
#
3
# Copyright (c) 2006-2018 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
#

25 26 27
define("GLOBAL_PERM_ANON_RO_IDX",  1);
define("GLOBAL_PERM_USER_RO_IDX",  2);

28 29 30 31
class Lease
{
    var	$lease;
    var $attributes;
32
    var $project;
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

    #
    # Constructor by lookup on unique index.
    #
    function Lease($token) {
	$safe_token = addslashes($token);
	$query_result = null;

	if (preg_match("/^\d+$/", $token)) {
	    $query_result =
		DBQueryWarn("select * from project_leases ".
			    "where lease_idx='$safe_token'");
	}
	elseif (preg_match("/^\w+\-\w+\-\w+\-\w+\-\w+$/", $token)) {
	    $query_result =
		DBQueryWarn("select * from project_leases ".
			    "where uuid='$safe_token'");
	}
	if (!$query_result || !mysql_num_rows($query_result)) {
	    $this->lease = NULL;
	    return;
	}
	$this->lease = mysql_fetch_array($query_result);
	$lease_idx   = $this->lease_idx();

	#
	# Load the attributes.
	#
	$query_result =
	    DBQueryWarn("select attrkey,attrval ".
			"  from lease_attributes ".
			"  where lease_idx='$lease_idx'");
	if (!$query_result) {
	    $this->lease = NULL;
	    return;
	}
	$attrs = array();

	while ($row = mysql_fetch_array($query_result)) {
	    $key = $row["attrkey"];
	    $val = $row["attrval"];
	    $attrs[$key] = $val;
	}
	$this->attributes = $attrs;
77 78
	# Load lazily;
	$this->project    = null;
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
    }

    # Hmm, how does one cause an error in a php constructor?
    function IsValid() {
	return !is_null($this->lease);
    }

    # Lookup.
    function Lookup($token) {
	$foo = new Lease($token);

	if (! $foo->IsValid()) {
	    return null;
	}
	return $foo;
    }
    # Lookup by name in a project
    function LookupByName($project, $name) {
	$pid       = $project->pid();
	$safe_name = addslashes($name);
	
	$query_result =
	    DBQueryFatal("select lease_idx from project_leases ".
			 "where pid='$pid' and lease_id='$safe_name'");

	if (mysql_num_rows($query_result) == 0) {
	    return null;
	}
	$row = mysql_fetch_array($query_result);
	return Lease::Lookup($row["lease_idx"]);
    }

    # accessors
    function field($name) {
	return (is_null($this->lease) ? -1 : $this->lease[$name]);
    } 
    function lease_idx()     { return $this->field("lease_idx"); }
    function idx()           { return $this->field("lease_idx"); }
    function lease_id()      { return $this->field("lease_id"); }
    function id()            { return $this->field("lease_id"); }
    function owner_uid()     { return $this->field("owner_uid"); }
    function uuid()          { return $this->field("uuid"); }
    function pid()           { return $this->field("pid"); }
122
    function gid()           { return $this->field("gid"); }
123 124
    function lease_type()    { return $this->field("type"); }
    function type()          { return $this->field("type"); }
125 126 127 128 129
    function inception()     { return NullDate($this->field("inception")); }
    function created()       { return $this->inception(); }
    function lease_end()     { return NullDate($this->field("lease_end")); }
    function expires()       { return $this->lease_end(); }
    function last_used()     { return NullDate($this->field("last_used")); }
130 131 132
    function state()	     { return $this->field("state"); }
    function locked()	     { return $this->field("locked"); }
    function locker_pid()    { return $this->field("locker_pid"); }
Leigh Stoller's avatar
Leigh Stoller committed
133
    function updated()       { return null; }
134 135 136 137 138 139 140 141 142

    function attribute($key) {
	if (array_key_exists($key, $this->attributes)) {
	    return $this->attributes[$key];
	}
	return null;
    }
    function size()	{ return $this->attribute("size"); }
    function fstype()	{ return $this->attribute("fstype"); }
143
    function islocal()  { return 1; }
144 145 146 147 148

    #
    # This is incomplete.
    #
    function AccessCheck($user, $access_type) {
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
        global $LEASE_ACCESS_READINFO;
        global $LEASE_ACCESS_MODIFYINFO;
        global $LEASE_ACCESS_READ;
        global $LEASE_ACCESS_MODIFY;
        global $LEASE_ACCESS_DESTROY;
        global $LEASE_ACCESS_MIN;
        global $LEASE_ACCESS_MAX;
	global $TBDB_TRUST_USER;
	global $TBDB_TRUST_GROUPROOT;
	global $TBDB_TRUST_LOCALROOT;

	$mintrust = $LEASE_ACCESS_READINFO;
        $read_access  = $this->read_access();
        $write_access = $this->write_access();
        
164 165 166 167 168 169 170 171 172
        #
        # Admins do whatever they want.
        # 
	if (ISADMIN()) {
	    return 1;
	}
	if ($this->owner_uid() == $user->uid()) {
	    return 1;
	}
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
        if ($read_access == "global") {
            if ($access_type == $LEASE_ACCESS_READINFO) {
                return 1;
            }
        }
        if ($write_access == "creator") {
            if ($access_type > $LEASE_ACCESS_READINFO) {
                return 0;
            }
        }
	$pid    = $this->pid();
	$gid    = $this->gid();
	$uid    = $user->uid();
	$uid_idx= $user->uid_idx();
	$pid_idx= $user->uid_idx();
	$gid_idx= $user->uid_idx();
        
        #
        # Otherwise must have proper trust in the project.
        # 
	if ($access_type == $LEASE_ACCESS_READINFO) {
	    $mintrust = $TBDB_TRUST_USER;
	}
	else {
	    $mintrust = $TBDB_TRUST_LOCALROOT;
	}
	if (TBMinTrust(TBGrpTrust($uid, $pid, $gid), $mintrust) ||
	    TBMinTrust(TBGrpTrust($uid, $pid, $pid), $TBDB_TRUST_GROUPROOT)) {
	    return 1;
	}
203 204
	return 0;
    }
Leigh Stoller's avatar
Leigh Stoller committed
205

206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
    function read_access() {
        $lease_idx = $this->lease_idx();
        $perm_idx  = GLOBAL_PERM_ANON_RO_IDX;

        $query_result =
            DBQueryFatal("select * from lease_permissions ".
                         "where lease_idx='$lease_idx' and ".
                         "      permission_idx='$perm_idx'");
        
	if (mysql_num_rows($query_result) == 0) {
	    return "project";
	}
        else {
            return "global";
        }
    }
    function write_access() {
        $lease_idx = $this->lease_idx();
        $pid       = $this->pid();

        $query_result =
            DBQueryFatal("select * from lease_permissions ".
                         "where lease_idx='$lease_idx' and ".
                         "      permission_id='$pid' and allow_modify=1");
        
	if (mysql_num_rows($query_result)) {
	    return "project";
	}
        else {
            return "creator";
        }
Leigh Stoller's avatar
Leigh Stoller committed
237 238
    }

239
    #
240
    # Form a URN for the dataset. No subgroup support yet.
241 242 243
    #
    function URN() {
	global $OURDOMAIN;
244 245 246 247 248 249 250 251
        $pid    = $this->pid();
        $gid    = $this->gid();
        $id     = $this->id();
        $type   = $this->type();
        $domain = $OURDOMAIN;
        $domain .= ":${pid}";
        if ($pid != $gid)
            $domain .= ":${gid}";
252
	
253
	return "urn:publicid:IDN+${domain}+${type}+${id}";
254
    }
255

256 257
    # We ignore webtasks for classic UI
    function deleteCommand($webtask) {
258 259
	return  "webdeletelease -f -b " . $this->pid() . "/" . $this->id();
    }
260
    function grantCommand($webtask) {
261 262
	return  "webgrantlease ";
    }
263 264 265 266 267 268 269 270 271 272 273 274 275

    function Project() {
	$pid = $this->pid();

	if ($this->project)
	    return $this->project;

	$this->project = Project::Lookup($pid);
	if (! $this->project) {
	    TBERROR("Could not lookup project $pid!", 1);
	}
	return $this->project;
    }
276 277 278 279 280 281 282 283
}

#
# This is basically a wrapper around an image acting as a dataset.
#
class ImageDataset
{
    var	$image;
284
    var $project;
285 286 287 288 289 290 291 292 293 294 295

    #
    # Constructor by lookup on unique index.
    #
    function ImageDataset($token) {
        $image = Image::LookupByUUID($token);
        if (!$image || !$image->isdataset()) {
	    $this->image = NULL;
	    return;
        }
        $this->image = $image;
296 297
	# Load lazily;
	$this->project = null;
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
    }

    # Hmm, how does one cause an error in a php constructor?
    function IsValid() {
	return !is_null($this->image);
    }

    # Lookup by uuid.
    function Lookup($token) {
        $image = Image::LookupByUUID($token);
        if (!$image || !$image->isdataset()) {
            return null;
        }
	return new ImageDataset($token);
    }
    # Lookup by name in a project
    function LookupByName($project, $name) {
        $image = Image::LookupByName($project, $name);
316
        If (!$image || !$image->isdataset()) {
317 318 319 320 321 322 323 324 325 326 327
            return null;
        }
	return ImageDataset::Lookup($image->image_uuid());
    }

    # accessors
    function idx()           { return $this->image->imageid(); }
    function dataset_id()    { return $this->image->imagename(); }
    function id()            { return $this->dataset_id(); }
    function creator_uid()   { return $this->image->creator(); }
    function owner_uid()     { return $this->image->creator(); }
328 329
    # Use the image uuid here.
    function uuid()          { return $this->image->image_uuid(); }
330 331 332 333 334 335 336 337 338 339 340 341
    function pid()           { return $this->image->pid(); }
    function pid_idx()       { return $this->image->pid_idx(); }
    function gid()           { return $this->image->pid(); }
    function aggregate_urn() { return ""; }
    function type()          { return "imdataset"; }
    function fstype()        { return "unknown"; }
    function created()       { return NullDate($this->image->created()); }
    function expires()       { return ""; }
    function last_used()     { return ""; }
    function locked()	     { return $this->image->locked(); }
    function locker_pid()    { return $this->image->locker_pid(); }
    function islocal()       { return 1; }
342
    function updated()       { return $this->image->updated(); }
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447

    #
    # Convert to mebi.
    #
    function size() {
        return intval((($this->image->lba_high() -
                        $this->image->lba_low() + 1) *
                       $this->image->lba_size()) /
                      pow(2, 20));
    }

    function state() {
        return ($this->image->locked() ? "imaging" :
                $this->image->size() ? "valid" : "allocated");
    }

    #
    # This is incomplete.
    #
    function AccessCheck($user, $access_type) {
        global $LEASE_ACCESS_READINFO;
        global $LEASE_ACCESS_MODIFYINFO;
        global $LEASE_ACCESS_READ;
        global $LEASE_ACCESS_MODIFY;
        global $LEASE_ACCESS_DESTROY;
        global $LEASE_ACCESS_MIN;
        global $LEASE_ACCESS_MAX;
	global $TBDB_TRUST_USER;
	global $TBDB_TRUST_GROUPROOT;
	global $TBDB_TRUST_LOCALROOT;

	$mintrust = $LEASE_ACCESS_READINFO;
        $read_access  = $this->read_access();
        $write_access = $this->write_access();
        #
        # Admins do whatever they want.
        # 
	if (ISADMIN()) {
	    return 1;
	}
	if ($this->creator_uid() == $user->uid()) {
	    return 1;
	}
        if ($read_access == "global") {
            if ($access_type == $LEASE_ACCESS_READINFO) {
                return 1;
            }
        }
        if ($write_access == "creator") {
            if ($access_type > $LEASE_ACCESS_READINFO) {
                return 0;
            }
        }
	$pid    = $this->pid();
	$gid    = $this->gid();
	$uid    = $user->uid();
	$uid_idx= $user->uid_idx();
	$pid_idx= $user->uid_idx();
	$gid_idx= $user->uid_idx();
        
        #
        # Otherwise must have proper trust in the project.
        # 
	if ($access_type == $LEASE_ACCESS_READINFO) {
	    $mintrust = $TBDB_TRUST_USER;
	}
	else {
	    $mintrust = $TBDB_TRUST_LOCALROOT;
	}
	if (TBMinTrust(TBGrpTrust($uid, $pid, $gid), $mintrust) ||
	    TBMinTrust(TBGrpTrust($uid, $pid, $pid), $TBDB_TRUST_GROUPROOT)) {
	    return 1;
	}
	return 0;
    }

    function read_access() {
	if ($this->image->isglobal()) {
	    return "global";
	}
        else {
            return "project";
        }
    }
    function write_access() {
        $imageid = $this->image->imageid();
        $pid     = $this->pid();

        $query_result =
            DBQueryFatal("select * from image_permissions ".
                         "where imageid='$imageid' and ".
                         "      permission_id='$pid' and allow_write=1");
        
	if (mysql_num_rows($query_result)) {
	    return "project";
	}
        else {
            return "creator";
        }
    }

    #
    # Form a URN for the dataset.
    #
    function URN() {
448 449 450 451 452 453 454 455 456 457 458
	global $OURDOMAIN;
        $pid    = $this->pid();
        $gid    = $this->gid();
        $id     = $this->id();
        $type   = $this->type();
        $domain = $OURDOMAIN;
        $domain .= ":${pid}";
        if ($pid != $gid)
            $domain .= ":${gid}";
	
	return "urn:publicid:IDN+${domain}+${type}+${id}";
459
    }
Leigh Stoller's avatar
Leigh Stoller committed
460 461 462 463 464 465
    function URL() {
        global $TBBASE;
        $uuid     = $this->uuid();

        return "$TBBASE/image_metadata.php?uuid=$image_uuid";
    }
466
    
467 468
    # We ignore webtasks for classic UI
    function deleteCommand($webtask) {
469 470
	return  "webdelete_image -F -p " . $this->image->imageid();
    }
471
    function grantCommand($webtask) {
472 473
	return  "webgrantimage ";
    }
474 475 476 477 478 479 480 481 482 483 484 485
    function Project() {
	$pid = $this->pid();

	if ($this->project)
	    return $this->project;

	$this->project = Project::Lookup($pid);
	if (! $this->project) {
	    TBERROR("Could not lookup project $pid!", 1);
	}
	return $this->project;
    }
486
}
487

488
?>