geni_defs.php 10.9 KB
Newer Older
1 2
<?php
#
3
# Copyright (c) 2006-2013 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
# 
# {{{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/>.
# 
# }}}
23 24 25 26 27 28 29
#
#
function GetDBLink($authority)
{
    if ($authority == "cm")
	$authority = "geni-cm";
    
30
    if ($authority == "sa" || $authority == "geni-sa")
31 32 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 77 78 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
	$authority = "geni";
    
    if ($authority == "ch")
	$authority = "geni-ch";
    
    return DBConnect($authority);
}

class GeniSlice
{
    var	$slice;
    var $dblink;

    #
    # Constructor lookup.
    #
    function GeniSlice($authority, $token) {
	$safe_token = addslashes($token);
	$dblink     = GetDBLink($authority);
	$idx        = null;

	if (! $dblink) {
	    $this->slice = null;
	    return;
	}

	if (preg_match("/^\d+$/", $token)) {
	    $idx = $token;
	}
	elseif (preg_match("/^\w+\-\w+\-\w+\-\w+\-\w+$/", $token)) {
	    $query_result =
		DBQueryWarn("select idx from geni_slices ".
			    "where uuid='$safe_token'",
			    $dblink);

	    if ($query_result && mysql_num_rows($query_result)) {
		$row = mysql_fetch_row($query_result);
		$idx = $row[0];
	    }
	}
	elseif (preg_match("/^[-\w\.]*$/", $token)) {
	    $query_result =
		DBQueryWarn("select idx from geni_slices ".
			    "where hrn='$safe_token'",
			    $dblink);

	    if ($query_result && mysql_num_rows($query_result)) {
		$row = mysql_fetch_row($query_result);
		$idx = $row[0];
	    }
	}
	if (is_null($idx)) {
	    $this->slice = null;
	    return;
	}

	$query_result =
	    DBQueryWarn("select * from geni_slices where idx='$idx'",
			$dblink);

	if (!$query_result || !mysql_num_rows($query_result)) {
	    $this->slice = null;
	    return;
	}
	$this->dblink = $dblink;
	$this->slice  = mysql_fetch_array($query_result);
    }

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

    # Lookup.
    function Lookup($authority, $token) {
	$foo = new GeniSlice($authority, $token);

	if ($foo->IsValid()) {
	    return $foo;
	}
	return null;
    }
    # accessors
    function field($name) {
	return (is_null($this->slice) ? -1 : $this->slice[$name]);
    }
    function idx()	    { return $this->field('idx'); }
    function hrn()	    { return $this->field('hrn'); }
    function uuid()	    { return $this->field('uuid'); }
120
    function publicid()	    { return $this->field('publicid'); }
121 122 123 124 125 126 127 128 129
    function exptidx()	    { return $this->field('exptidx'); }
    function created()	    { return $this->field('created'); }
    function expires()	    { return $this->field('expires'); }
    function shutdown()	    { return $this->field('shutdown'); }
    function locked()	    { return $this->field('locked'); }
    function creator_uuid() { return $this->field('creator_uuid'); }
    function name()	    { return $this->field('name'); }
    function sa_uuid()	    { return $this->field('sa_uuid'); }
    function needsfirewall(){ return $this->field('needsfirewall'); }
130
    function monitor_pid()  { return $this->field('monitor_pid'); }
131 132
    function expiration_max()  { return $this->field('expiration_max'); }
    function renew_limit()  { return $this->field('renew_limit'); }
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162

    #
    # Class function to return a list of all slices.
    #
    function AllSlices($authority) {
	$result     = array();
	$dblink     = GetDBLink($authority);

	if (! $dblink) {
	    return null;
	}
	$query_result =
	    DBQueryFatal("select idx from geni_slices", $dblink);

	if (! ($query_result && mysql_num_rows($query_result))) {
	    return null;
	}
    
	while ($row = mysql_fetch_array($query_result)) {
	    $idx = $row["idx"];

	    if (! ($slice = GeniSlice::Lookup($authority, $idx))) {
		TBERROR("GeniSlice::AllSlices: ".
			"Could not load slice $idx!", 1);
	    }
	    $result[] = $slice;
	}
	return $result;
    }

163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
    function LookupByExperiment($authority, $experiment) {
	$dblink     = GetDBLink($authority);
	$exptidx    = $experiment->idx();

	if (! $dblink) {
	    return null;
	}
	$query_result =
	    DBQueryFatal("select idx from geni_slices ".
			 "where exptidx=$exptidx",
			 $dblink);

	if (! ($query_result && mysql_num_rows($query_result))) {
	    return null;
	}
	$row = mysql_fetch_row($query_result);
	$idx = $row[0];
 	return GeniSlice::Lookup($authority, $idx);
    }

183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
    function LookupByPublicID($authority, $publicid) {
	$dblink     = GetDBLink($authority);
	$safeid     = addslashes($publicid);

	if (! $dblink) {
	    return null;
	}
	$query_result =
	    DBQueryFatal("select idx from geni_slices ".
			 "where publicid='$safeid'",
			 $dblink);

	if (! ($query_result && mysql_num_rows($query_result))) {
	    return null;
	}
	$row = mysql_fetch_row($query_result);
	$idx = $row[0];
 	return GeniSlice::Lookup($authority, $idx);
    }

203 204 205 206 207 208 209 210 211 212 213 214 215 216
    function GetManifest() {
	$slice_uuid = $this->uuid();
	
	$query_result =
	    DBQueryFatal("select manifest from geni_manifests ".
			 "where slice_uuid='$slice_uuid'",
			 $this->dblink);

	if ($query_result && mysql_num_rows($query_result)) {
	    $row = mysql_fetch_row($query_result);
	    return $row[0];
	}
	return null;
    }
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
    #
    # The urn is stored in the certificate.
    #
    function urn() {
	$slice_uuid = $this->uuid();
	
	$query_result =
	    DBQueryFatal("select urn from geni_certificates ".
			 "where uuid='$slice_uuid'",
			 $this->dblink);

	if ($query_result && mysql_num_rows($query_result)) {
	    $row = mysql_fetch_row($query_result);
	    return $row[0];
	}
	return null;
    }
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
}

class GeniUser
{
    var	$user;
    var $dblink;

    #
    # Constructor lookup.
    #
    function GeniUser($authority, $token) {
	$safe_token = addslashes($token);
	$dblink     = GetDBLink($authority);
	$idx        = null;

	if (! $dblink) {
	    $this->user = null;
	    return;
	}

	if (preg_match("/^\d+$/", $token)) {
	    $idx = $token;
	}
	elseif (preg_match("/^\w+\-\w+\-\w+\-\w+\-\w+$/", $token)) {
	    $query_result =
		DBQueryWarn("select idx from geni_users ".
			    "where uuid='$safe_token'",
			    $dblink);

	    if ($query_result && mysql_num_rows($query_result)) {
		$row = mysql_fetch_row($query_result);
		$idx = $row[0];
	    }
	}
	elseif (preg_match("/^[-\w\.]*$/", $token)) {
	    $query_result =
		DBQueryWarn("select idx from geni_users ".
			    "where hrn='$safe_token'",
			    $dblink);

	    if ($query_result && mysql_num_rows($query_result)) {
		$row = mysql_fetch_row($query_result);
		$idx = $row[0];
	    }
	}
	if (is_null($idx)) {
	    $this->user = null;
	    return;
	}

	$query_result =
	    DBQueryWarn("select * from geni_users where idx='$idx'",
			$dblink);

	if (!$query_result || !mysql_num_rows($query_result)) {
	    $this->user = null;
	    return;
	}
	$this->dblink = $dblink;
	$this->user  = mysql_fetch_array($query_result);
294 295 296 297 298 299 300 301 302 303 304 305 306

	#
	# And get the urn.
	#
	$uuid = $this->user['uuid'];
	$query_result =
	    DBQueryWarn("select urn from geni_certificates where uuid='$uuid'",
			$dblink);
	if ($query_result && mysql_num_rows($query_result)) {
	    $foo = mysql_fetch_array($query_result);
	    $this->user['urn'] = $foo['urn'];
	    return;
	}
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
    }

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

    # Lookup.
    function Lookup($authority, $token) {
	$foo = new GeniUser($authority, $token);

	if ($foo->IsValid()) {
	    return $foo;
	}
	return null;
    }
    # accessors
    function field($name) {
	return (is_null($this->user) ? -1 : $this->user[$name]);
    }
    function idx()	    { return $this->field('idx'); }
    function hrn()	    { return $this->field('hrn'); }
329
    function urn()	    { return $this->field('urn'); }
330 331 332 333 334 335 336 337 338 339 340 341 342 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
    function uuid()	    { return $this->field('uuid'); }
    function exptidx()	    { return $this->field('exptidx'); }
    function created()	    { return $this->field('created'); }
    function expires()	    { return $this->field('expires'); }
    function locked()	    { return $this->field('locked'); }
    function status()	    { return $this->field('staus'); }
    function name()	    { return $this->field('name'); }
    function email()	    { return $this->field('email'); }
    function sa_uuid()	    { return $this->field('sa_uuid'); }
}

class ClientSliver
{
    var	$sliver;
    var $dblink;

    #
    # Constructor lookup.
    #
    function ClientSliver($token) {
	$safe_token = addslashes($token);
	$dblink     = GetDBLink("sa");
	$idx        = null;

	if (! $dblink) {
	    $this->sliver = null;
	    return;
	}

	if (preg_match("/^\d+$/", $token)) {
	    $idx = $token;
	}
	if (is_null($idx)) {
	    $this->sliver = null;
	    return;
	}

	$query_result =
	    DBQueryWarn("select * from client_slivers where idx='$idx'",
			$dblink);

	if (!$query_result || !mysql_num_rows($query_result)) {
	    $this->sliver = null;
	    return;
	}
	$this->dblink = $dblink;
	$this->sliver  = mysql_fetch_array($query_result);
    }

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

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

	if ($foo->IsValid()) {
	    return $foo;
	}
	return null;
    }
    # accessors
    function field($name) {
	return (is_null($this->sliver) ? -1 : $this->sliver[$name]);
    }
    function idx()	    { return $this->field('idx'); }
    function urn()	    { return $this->field('urn'); }
    function slice_idx()    { return $this->field('slice_idx'); }
    function manager_urn()  { return $this->field('manager_urn'); }
    function creator_idx()  { return $this->field('creator_idx'); }
    function created()	    { return $this->field('created'); }
    function expires()	    { return $this->field('expires'); }
    function locked()	    { return $this->field('locked'); }
    function manifest()	    { return $this->field('manifest'); }

    #
    # Class function to return a list of all slivers for a slice
    #
    function SliverList($slice) {
	$result     = array();
	$dblink     = GetDBLink("sa");
	$slice_idx  = $slice->idx();

	if (! $dblink) {
	    return null;
	}
	$query_result =
	    DBQueryFatal("select idx from client_slivers ".
			 "where slice_idx='$slice_idx'",
			 $dblink);

	if (! ($query_result && mysql_num_rows($query_result))) {
	    return null;
	}
    
	while ($row = mysql_fetch_array($query_result)) {
	    $idx = $row["idx"];

	    if (! ($sliver = ClientSliver::Lookup($idx))) {
		TBERROR("ClientSliver::SliverList: ".
			"Could not load client sliver $idx!", 1);
	    }
	    $result[] = $sliver;
	}
	return $result;
    }
}
439
?>