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