iterate
[bootmanager.git] / nodeconfig / boot / index.php
1 <?php
2 //
3 // Returns node boot script
4 //
5 // Mark Huang <mlhuang@cs.princeton.edu>
6 // Copyright (C) 2006 The Trustees of Princeton University
7 //
8 // $Id$ $
9 //
10
11 // Get admin API handle
12 require_once 'plc_api.php';
13 global $adm;
14
15 // location for signed scripts
16 $bmdir="/var/www/html/boot";
17 $bmext=".sh.sgn";
18
19 $candidates = array();
20
21 // Look up the node
22 $interfaces = $adm->GetInterfaces(array('ip' => $_SERVER['REMOTE_ADDR']));
23 if (!empty($interfaces)) {
24   $nodes = $adm->GetNodes(array($interfaces[0]['node_id']));
25   if (!empty($nodes)) {
26     $node = $nodes[0];
27   }
28 }
29
30 if (isset($node)) {
31   // Allow very old nodes that do not have a node key in their
32   // configuration files to use their "boot nonce" instead. The boot
33   // nonce is a random value generated by the node itself and POSTed
34   // by the Boot CD when it requests the Boot Manager. This is
35   // obviously not very secure, so we only allow it to be used if the
36   // requestor IP is the same as the IP address we have on record for
37   // the node.
38
39   // 3.x CDs post 'version', 2.x CDs post 'id'.
40   if (!empty($_REQUEST['version'])) {
41     $version = trim($_REQUEST['version']);
42   } elseif (!empty($_REQUEST['id'])) {
43     $version = trim($_REQUEST['id']);
44   } else {
45     $version = "2.0";
46   }
47
48   if (empty($node['key']) && !empty($_REQUEST['nonce'])) {
49     // 3.x CDs post the boot nonce in ASCII hex. 2.x CDs post it in binary.
50     if (strstr($version, "2.0") === FALSE) {
51       // 3.x CDs post a trailing newline...sigh
52       $nonce = trim($_REQUEST['nonce']);
53     } else {
54       $nonce = bin2hex($_REQUEST['nonce']);
55     }
56     $adm->UpdateNode($node['node_id'], array('boot_nonce' => $nonce));
57   }
58
59   // Custom bootmanager for the node, e.g.
60   // planetlab-1.cs.princeton.edu_bootmanager.sh.sgn
61   $candidates [] = "bootmanager" . "_" . strtolower($node['hostname']);
62
63   // Custom bootmanager for the deployment tag, e.g.
64   $deployment = $adm->GetNodeDeployment($node['node_id']);
65   if ( ! empty ($deployment) ) {
66     $candidates[] = "bootmanager" . "_" . $deployment;
67   }
68 }
69
70 // Default bootmanager
71 $candidates[] = "bootmanager_regular";
72
73 foreach ($candidates as $bootmanager) {
74   $candidate=$bmdir . "/" . $bootmanager . $bmext ;
75   if (file_exists($candidate)) {
76     readfile($candidate);
77     exit();
78   }
79 }
80
81 ?>