osinfo_defs.php 19 KB
Newer Older
1 2
<?php
#
3
# Copyright (c) 2006-2014 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 30 31
#

class OSinfo
{
    var	$osinfo;

    #
    # Constructor by lookup on unique ID
    #
32 33 34 35
    function OSinfo($id, $version = NULL) {
	if (is_null($version)) {
	    list($id,$version) = preg_split('/:/', $id);
	}
36 37
	$safe_id = addslashes($id);

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
	if (is_null($version)) {
	    $query_result =
		DBQueryWarn("select o.*,v.* from os_info as o ".
			    "left join os_info_versions as v on ".
			    "     v.osid=o.osid and v.vers=o.version ".
			    "where o.osid='$safe_id'");
	}
	else {
	    # This will get deleted images, but that is okay.
	    $safe_version = addslashes($version);
	    $query_result =
	        DBQueryWarn("select v.* from os_info_versions as v ".
			    "where v.osid='$safe_id' and ".
			    "      v.vers='$safe_version'");
	}
53 54 55 56 57 58 59 60 61 62 63 64 65 66

	if (!$query_result || !mysql_num_rows($query_result)) {
	    $this->osinfo = NULL;
	    return;
	}
	$this->osinfo = mysql_fetch_array($query_result);
    }

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

    # Lookup by osid
67 68
    function Lookup($id, $version = NULL) {
	$foo = new OSinfo($id, $version);
69 70 71 72 73 74 75

	if (! $foo->IsValid())
	    return null;

	return $foo;
    }

76
    # Lookup by osname in a project. This returns the newest version.
77
    function LookupByName($project, $name) {
Leigh Stoller's avatar
Leigh Stoller committed
78 79 80 81 82 83
	if (is_a($project, "Project")) {
	    $pid = $project->pid();
	}
	else {
	    $pid = addslashes($project);
	}
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
	$safe_name = addslashes($name);
	
	$query_result =
	    DBQueryFatal("select osid from os_info ".
			 "where pid='$pid' and osname='$safe_name'");

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

    #
    # Refresh an instance by reloading from the DB.
    #
    function Refresh() {
	if (! $this->IsValid())
	    return -1;

	$osid = $this->osid();
105
	$vers = $this->vers();
106 107

	$query_result =
108 109
	    DBQueryWarn("select * from os_info_versions ".
			"where osid='$osid' and vers='$vers'");
110 111 112 113 114 115 116 117 118
    
	if (!$query_result || !mysql_num_rows($query_result)) {
	    $this->osinfo = NULL;
	    return -1;
	}
	$this->osinfo = mysql_fetch_array($query_result);
	return 0;
    }

119 120 121 122 123 124 125 126 127 128 129 130
    #
    # Class function to create new osid and return object.
    #
    function NewOSID($user, $project, $osname, $args, &$errors) {
	global $suexec_output, $suexec_output_array;

        #
        # Generate a temporary file and write in the XML goo.
        #
	$xmlname = tempnam("/tmp", "newosid");
	if (! $xmlname) {
	    TBERROR("Could not create temporary filename", 0);
131
	    $errors[] = "Transient error(1); please try again later.";
132 133 134 135
	    return null;
	}
	if (! ($fp = fopen($xmlname, "w"))) {
	    TBERROR("Could not open temp file $xmlname", 0);
136
	    $errors[] = "Transient error(2); please try again later.";
137 138 139 140 141
	    return null;
	}

	# Add these. Maybe caller should do this?
	$args["osname"]  = $osname;
142
	$args["pid"]     = $project->pid();
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158

	fwrite($fp, "<osid>\n");
	foreach ($args as $name => $value) {
	    fwrite($fp, "<attribute name=\"$name\">");
	    fwrite($fp, "  <value>" . htmlspecialchars($value) . "</value>");
	    fwrite($fp, "</attribute>\n");
	}
	fwrite($fp, "</osid>\n");
	fclose($fp);
	chmod($xmlname, 0666);

	$retval = SUEXEC("nobody", "nobody", "webnewosid $xmlname",
			 SUEXEC_ACTION_IGNORE);

	if ($retval) {
	    if ($retval < 0) {
159
		$errors[] = "Transient error(3, $retval); please try again later.";
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
		SUEXECERROR(SUEXEC_ACTION_CONTINUE);
	    }
	    else {
		# unlink($xmlname);
		if (count($suexec_output_array)) {
		    for ($i = 0; $i < count($suexec_output_array); $i++) {
			$line = $suexec_output_array[$i];
			if (preg_match("/^([-\w]+):\s*(.*)$/",
				       $line, $matches)) {
			    $errors[$matches[1]] = $matches[2];
			}
			else
			    $errors[] = $line;
		    }
		}
		else
176
		    $errors[] = "Transient error(4, $retval); please try again later.";
177 178 179 180 181 182 183 184 185 186 187 188
	    }
	    return null;
	}

        #
        # Parse the last line of output. Ick.
        #
	unset($matches);
	
	if (!preg_match("/^OSID\s+([^\/]+)\/(\d+)\s+/",
			$suexec_output_array[count($suexec_output_array)-1],
			$matches)) {
189
	    $errors[] = "Transient error(5); please try again later.";
190 191 192 193 194 195
	    SUEXECERROR(SUEXEC_ACTION_CONTINUE);
	    return null;
	}
	$osid = $matches[2];
	$newosid = OSinfo::Lookup($osid);
	if (! $newosid) {
196
	    $errors[] = "Transient error(6); please try again later.";
197 198 199 200 201 202 203 204 205
	    TBERROR("Could not lookup new osid $osid", 0);
	    return null;
	}
	# Unlink this here, so that the file is left behind in case of error.
	# We can then create the osid by hand from the xmlfile, if desired.
	unlink($xmlname);
	return $newosid; 
    }

206 207 208 209 210 211 212 213 214 215 216 217 218 219
    #
    # Equality test.
    #
    function SameOS($osinfo) {
	return $this->osid() == $osinfo->osid();
    }

    # accessors
    function field($name) {
	return (is_null($this->osinfo) ? -1 : $this->osinfo[$name]);
    }
    function osname()		{ return $this->field("osname"); }
    function pid()		{ return $this->field("pid"); }
    function osid()		{ return $this->field("osid"); }
220 221 222
    function vers()		{ return $this->field("vers"); }
    function parent_osid()	{ return $this->field("parent_osid"); }
    function parent_vers()	{ return $this->field("parent_vers"); }
223
    function uuid()		{ return $this->field("uuid"); }
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
    function creator()		{ return $this->field("creator"); }
    function creator_idx()	{ return $this->field("creator_idx"); }
    function created()		{ return $this->field("created"); }
    function description()	{ return $this->field("description"); }
    function OS()		{ return $this->field("OS"); }
    function version()		{ return $this->field("version"); }
    function path()		{ return $this->field("path"); }
    function magic()		{ return $this->field("magic"); }
    function machinetype()	{ return $this->field("machinetype"); }
    function osfeatures()	{ return $this->field("osfeatures"); }
    function ezid()		{ return $this->field("ezid"); }
    function shared()		{ return $this->field("shared"); }
    function mustclean()	{ return $this->field("mustclean"); }
    function op_mode()		{ return $this->field("op_mode"); }
    function nextosid()		{ return $this->field("nextosid"); }
    function max_concurrent()	{ return $this->field("max_concurrent"); }
    function mfs()		{ return $this->field("mfs"); }
    function reboot_waittime()  { return $this->field("reboot_waittime"); }
242
    function def_parentosid()   { return $this->field("def_parentosid"); }
243

244 245
    function SetParent($parent_osid) {
	$osid = $this->osid();
246
	$vers = $this->vers();
247 248
	$safe_osid = addslashes($parent_osid);

249 250
	DBQueryFatal("update os_info_versions set def_parentosid='$safe_osid' ".
		     "where osid='$osid' and vers='$vers'");
251 252 253 254 255 256 257

	DBQueryFatal("replace into os_submap set ".
		     " parent_osid='$safe_osid', osid='$osid'");

	return 0;
    }

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 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
    #
    # Access Check, determines if $user can access $this record.
    # 
    function AccessCheck($user, $access_type) {
	global $TB_OSID_READINFO;
	global $TB_OSID_MODIFYINFO;
	global $TB_OSID_DESTROY;
	global $TB_OSID_MIN;
	global $TB_OSID_MAX;
	global $TBDB_TRUST_USER;
	global $TBDB_TRUST_LOCALROOT;
	$mintrust = $TB_OSID_READINFO;

	if ($access_type < $TB_OSID_MIN || $access_type > $TB_OSID_MAX) {
	    TBERROR("Invalid access type $access_type!", 1);
	}

        #
        # Admins do whatever they want!
        # 
	if (ISADMIN()) {
	    return 1;
	}

        #
        # No GIDs yet.
        #
	$pid    = $this->pid();
	$shared = $this->shared();
	$uid    = $user->uid();

        #
        # Global OSIDs can be read by anyone.
        # 
	if ($shared) {
	    if ($access_type == $TB_OSID_READINFO) {
		return 1;
	    }
	    return 0;
	}
    
        #
        # Otherwise must have proper trust in the project.
        # 
	if ($access_type == $TB_OSID_READINFO) {
	    $mintrust = $TBDB_TRUST_USER;
	}
	else {
	    $mintrust = $TBDB_TRUST_LOCALROOT;
	}

	#
	# Need the project object to complete this test.
	#
	if (! ($project = Project::Lookup($pid))) {
	    TBERROR("Could not map project $pid to its object", 1);
	}
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 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
	if (TBMinTrust($project->UserTrust($user), $mintrust)) {
	    return 1;
	}
	elseif (!$this->ezid()) {
	    return 0;
	}
	#
	# If this is an ez image, look in the image permissions.
        # First look for a user permission, then look for a group permission. 
	#
	$osid = $this->osid();
	$uid_idx = $user->uid_idx();
	$trust_none = TBDB_TRUSTSTRING_NONE;
	 
	$query_result = 
	    DBQueryFatal("select allow_write from image_permissions ".
			 "where imageid='$osid' and ".
			 "      permission_type='user' and ".
			 "      permission_idx='$uid_idx'");
	
	if (mysql_num_rows($query_result)) {
	    $row  = mysql_fetch_array($query_result);

            # Only allowed to read.
	    if ($access_type == $TB_OSID_READINFO) 
		return 1;
	}
	$trust_none = TBDB_TRUSTSTRING_NONE;
	$query_result = 
	    DBQueryFatal("select allow_write from group_membership as g ".
			 "left join image_permissions as p on ".
			 "     p.permission_type='group' and ".
			 "     p.permission_idx=g.gid_idx ".
			 "where g.uid_idx='$uid_idx' and ".
			 "      p.imageid='$osid' and ".
			 "      trust!='$trust_none'");

	if (mysql_num_rows($query_result)) {
            # Only allowed to read.
	    if ($access_type == $TB_OSID_READINFO)
		return 1;
	}
	return 0;
	
359 360
    }

361 362 363 364 365 366 367 368 369 370 371 372 373 374
    #
    # See if a feature is supported
    #
    function FeatureSupported($feature) {
	if ($this->osfeatures()) {
	    foreach (preg_split("/,/", $this->osfeatures()) as $f) {
		if ($feature == $f) {
		    return 1;
		}
	    }
	}
	return 0;
    }

375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
    #
    # Spit out an OSID link in user format.
    #
    function SpitLink() {
	$osname = $this->osname();
	$url    = CreateURL("showosinfo", $this);
	
	echo "<a href='$url'>$osname</a>\n";
    }

    #
    # Show OS INFO record.
    #
    function Show() {
	$osid           = $this->osid();
390
	$vers           = $this->vers();
391 392 393 394 395 396 397 398 399 400 401 402 403 404
	$os_description = $this->description();
	$os_OS          = $this->OS();
	$os_version     = $this->version();
	$os_path        = $this->path();
	$os_magic       = $this->magic();
	$os_osfeatures  = $this->osfeatures();
	$os_op_mode     = $this->op_mode();
	$os_pid         = $this->pid();
	$os_shared      = $this->shared();
	$os_osname      = $this->osname();
	$creator        = $this->creator();
	$created        = $this->created();
	$mustclean      = $this->mustclean();
	$nextosid       = $this->nextosid();
405
	$def_parentosid = $this->def_parentosid();
406 407
	$max_concurrent = $this->max_concurrent();
	$reboot_waittime= $this->reboot_waittime();
408
	$uuid           = $this->uuid();
409
	$ezid           = $this->ezid();
410
	$mfs            = $this->mfs();
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 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518

	if (! ($creator_user = User::Lookup($creator))) {
	    TBERROR("Error getting object for user $creator", 1);
	}
	$showuser_url = CreateURL("showuser", $creator_user);

	if (!$os_description)
	    $os_description = "&nbsp;";
	if (!$os_version)
	    $os_version = "&nbsp;";
	if (!$os_path)
	    $os_path = "&nbsp;";
	if (!$os_magic)
	    $os_magic = "&nbsp;";
	if (!$os_osfeatures)
	    $os_osfeatures = "&nbsp;";
	if (!$os_op_mode)
	    $os_op_mode = "&nbsp;";
	if (!$created)
	    $created = "N/A";
	if (!$reboot_waittime)
	    $reboot_waittime = "&nbsp;";

        #
        # Generate the table.
        #
	echo "<table align=center border=1>\n";

	echo "<tr>
                <td>Name: </td>
                <td class=\"left\">$os_osname</td>
              </tr>\n";

	echo "<tr>
                <td>Project: </td>
                <td class=\"left\">
                  <a href='showproject.php3?pid=$os_pid'>$os_pid</a></td>
              </tr>\n";

	echo "<tr>
                <td>Creator: </td>
                <td class=left>
                  <a href='$showuser_url'>$creator</a></td>
 	      </tr>\n";

	echo "<tr>
                <td>Created: </td>
                <td class=left>$created</td>
    	      </tr>\n";

	echo "<tr>
                <td>Description: </td>
                <td class=\"left\">$os_description</td>
              </tr>\n";

        echo "<tr>
                <td>Operating System: </td>
                <td class=\"left\">$os_OS</td>
              </tr>\n";

	echo "<tr>
                <td>Version: </td>
                <td class=\"left\">$os_version</td>
             </tr>\n";

	echo "<tr>
                <td>Path: </td>
                <td class=\"left\">$os_path</td>
              </tr>\n";

	echo "<tr>
                <td>Magic (uname -r -s): </td>
                <td class=\"left\">$os_magic</td>
              </tr>\n";

	echo "<tr>
                <td>Features: </td>
                <td class=\"left\">$os_osfeatures</td>
              </tr>\n";

	echo "<tr>
                <td>Operational Mode: </td>
                <td class=\"left\">$os_op_mode</td>
              </tr>\n";

	if (isset($max_concurrent) and $max_concurrent > 0) {
	    echo "<tr>
                    <td>Max Concurrent Usage: </td>
                    <td class=\"left\">$max_concurrent</td>
                  </tr>\n";
	}

	echo "<tr>
                <td>Reboot Waittime: </td>
                <td class=\"left\">$reboot_waittime</td>
              </tr>\n";

	echo "<tr>
                <td>Shared?: </td>
                <td class=left>" . YesNo($os_shared) . "</td>
              </tr>\n";

	echo "<tr>
                <td>Must Clean?: </td>
                <td class=left>" . YesNo($mustclean) . "</td>
              </tr>\n";

	if ($nextosid) {
519
	    if ($nextosid == 0) {
520 521 522
		echo "<tr>
		        <td>Next Osid: </td>
		        <td class=left>
523 524
			    Mapped via DB table: osid_map</td></tr>\n";
	    }
525 526 527
	    else {
		$nextosinfo = OSinfo::Lookup($nextosid);
	        $nextosname = $nextosinfo->osname();
528 529 530 531
		echo "<tr>
                        <td>Next Osid: </td>
                        <td class=left>
                            <a href='showosinfo.php3?osid=$nextosid'>
532
                                            $nextosname</a></td>
533
                      </tr>\n";
534
	    }
535
	}
536
	if ($def_parentosid) {
537 538
	    $nextosinfo = OSinfo::Lookup($def_parentosid);
	    $nextosname = $nextosinfo->osname();
539 540 541 542
	    echo "<tr>
                      <td>Parent Osid: </td>
                      <td class=left>
                          <a href='showosinfo.php3?osid=$def_parentosid'>
543
                                           $nextosname</a></td>
544 545
                  </tr>\n";
	}
546 547 548 549
	if ($ezid) {
		echo "<tr>
                        <td>Image Link: </td>
                        <td class=left>
Leigh Stoller's avatar
Leigh Stoller committed
550
                            <a href='showimageid.php3?imageid=$osid&version=$vers'>
551 552 553
                                            $os_osname</a></td>
                      </tr>\n";
	}
554 555 556 557 558 559
	if ($mfs) {
		echo "<tr>
                        <td>MFS: </td>
                        <td class=left>Yes</td>
                      </tr>\n";
	}
560 561

	echo "<tr>
562 563
                <td>Internal ID (Vers): </td>
                <td class=\"left\">$osid ($vers)</td>
564 565
              </tr>\n";

566 567 568 569 570
	echo "<tr>
                <td>UUID: </td>
                <td class=left>$uuid</td>
              </tr>\n";

571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
	if ($def_parentosid) {
	    $parent_result =
		DBQueryFatal("select m.parent_osid,o.osname,o.pid ".
			     "   from os_submap as m ".
			     "left join os_info as o on o.osid=m.parent_osid ".
			     "where m.osid='$osid'");
	    
	    if (mysql_num_rows($parent_result)) {
		while ($prow = mysql_fetch_array($parent_result)) {
		    $posid   = $prow["parent_osid"];
		    $posname = $prow["osname"];

		    echo "<tr>";
		    echo "  <td>Parent $posid:</td>";
		    echo "  <td class=left>";
		    echo "   <a href='showosinfo.php3?osid=$posid'>";
		    echo "$posname</a></td>\n";
		    echo "</tr>\n";
		}
	    }
	}
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
	echo "</table>\n";
    }

    #
    # Display list of experiments using this osid.
    #
    function ShowExperiments($user) {
	global $TBOPSPID;
	global $TB_EXPT_READINFO;

	$uid = $user->uid();
	$pid = $this->pid();
	$osname = $this->osname();

        #
        # Due to the funny way we handle 'global' images in the emulab-ops 
        # project, we have to treat its images specially - namely, we 
        # have to make sure there is not an osname in that project, which 
        # takes priority over the global ones.
        #
	if ($pid == $TBOPSPID) {
	    $query_result =
614 615 616
		DBQueryFatal("select distinct v.pid,v.eid,e.state,".
			     "      e.expt_swapped " .
			     "   from virt_nodes as v ".
617 618 619 620 621 622 623 624 625
			     "left join os_info as o on " .
			     "     v.osname=o.osname and v.pid=o.pid ".
			     "left join experiments as e on v.pid=e.pid and ".
			     "     v.eid=e.eid " .
			     "where v.osname='$osname' and o.osname is NULL " .
			     "order by v.pid, v.eid, e.state");
	}
	else {
	    $query_result =
626 627
		DBQueryFatal("select distinct v.pid,v.eid,e.state,".
			     "    e.expt_swapped " .
628 629 630 631 632 633 634 635 636 637 638 639
			     "  from virt_nodes as v ".
			     "left join experiments as e " .
			     "     on v.pid=e.pid and v.eid=e.eid " .
			     "where v.pid='$pid' and v.osname='$osname' " .
			     "order by v.pid, v.eid, e.state");
	}

	if (mysql_num_rows($query_result) == 0) {
	    echo "<h4 align='center'>No experiments are using this OS</h3>";
	}
	else {
	    $other_exps = 0;
640 641

	    echo "<h3 align='center'>Experiments using this OS</h3>\n";
642 643 644 645 646
	    echo "<table align=center border=1>\n";
	    echo "  <tr> 
		        <th>PID</th>
   		        <th>EID</th>
		        <th>State</th>
647
		        <th>Last Swap</th>
648 649 650 651 652
		    </tr>\n";
	    while($row = mysql_fetch_array($query_result)) {
		$pid   = $row[0];
		$eid   = $row[1];
		$state = $row[2];
653 654 655 656
		$lswap = $row[3];
		if (!$lswap) {
		    $lswap = "Never";
		}
657 658 659 660 661 662 663 664 665

                #
	        # Gotta make sure that the user actually has the right to 
	        # see this experiment - summarize all the experiments that
	        # he/she cannot see at the bottom
	        #
		if (! ($experiment = Experiment::LookupByPidEid($pid, $eid))) {
		    continue;
		}
666
		if (! $experiment->AccessCheck($user, $TB_EXPT_READINFO)) {
667 668 669 670 671 672 673
		    $other_exps++;
		    continue;
		}
		$showexp_url = CreateURL("showexp", $experiment);

		echo "<tr>\n";
		echo "  <td>$pid</td>\n";
674
		echo "  <td><a href='$showexp_url'>$eid</td>\n";
675
		echo "  <td>$state</td>\n";
676
		echo "  <td>$lswap</td>\n";
677 678 679 680 681 682 683 684 685 686 687 688 689 690
		echo "</tr>\n";
	    }
	    if ($other_exps) {
		echo "<tr><td colspan=3>
                        $other_exps experiments in other projects</td></tr>\n";
	    }
	    echo "</table>\n";
	}
    }
}

#
# Spit out an OSID link in user format.
#
691
function SpitOSIDLink($osid, $vers)
692
{
693
    $osinfo = OSInfo::Lookup($osid, $vers);
694 695 696 697 698 699 700 701

    if ($osinfo) {
	$osname = $osinfo->osname();
	$url    = CreateURL("showosinfo", $osinfo);
	
	echo "<a href='$url'>$osname</a>\n";
    }
}
702
?>