Setting tag nodeconfig-4.3-7
[nodeconfig.git] / 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 // Default bootmanager
16 $bootmanager = "bootmanager.sh.sgn";
17
18 // Look up the node
19 $interfaces = $adm->GetInterfaces(array('ip' => $_SERVER['REMOTE_ADDR']));
20 if (!empty($interfaces)) {
21   $nodes = $adm->GetNodes(array($interfaces[0]['node_id']));
22   if (!empty($nodes)) {
23     $node = $nodes[0];
24   }
25 }
26
27 if (isset($node)) {
28   // Allow very old nodes that do not have a node key in their
29   // configuration files to use their "boot nonce" instead. The boot
30   // nonce is a random value generated by the node itself and POSTed
31   // by the Boot CD when it requests the Boot Manager. This is
32   // obviously not very secure, so we only allow it to be used if the
33   // requestor IP is the same as the IP address we have on record for
34   // the node.
35
36   // 3.x CDs post 'version', 2.x CDs post 'id'.
37   if (!empty($_REQUEST['version'])) {
38     $version = trim($_REQUEST['version']);
39   } elseif (!empty($_REQUEST['id'])) {
40     $version = trim($_REQUEST['id']);
41   } else {
42     $version = "2.0";
43   }
44
45   if (empty($node['key']) && !empty($_REQUEST['nonce'])) {
46     // 3.x CDs post the boot nonce in ASCII hex. 2.x CDs post it in binary.
47     if (strstr($version, "2.0") === FALSE) {
48       // 3.x CDs post a trailing newline...sigh
49       $nonce = trim($_REQUEST['nonce']);
50     } else {
51       $nonce = bin2hex($_REQUEST['nonce']);
52     }
53     $adm->UpdateNode($node['node_id'], array('boot_nonce' => $nonce));
54   }
55
56   // Custom bootmanager for the node, e.g.
57   // planetlab-1.cs.princeton.edu_bootmanager.sh.sgn
58   $bootmanagers = array(strtolower($node['hostname']) . "_" . $bootmanager);
59
60   // Custom bootmanager for the node group, e.g.
61   // alpha_bootmanager.sh.sgn
62   if (!empty($node['nodegroup_ids'])) {
63     $nodegroups = $adm->GetNodeGroups($node['nodegroup_ids']);
64     foreach ($nodegroups as $nodegroup) {
65       $bootmanagers[] = strtolower($nodegroup['groupname']) . "_" . $bootmanager;
66     }
67   }
68 }
69
70 // Default bootmanager
71 $bootmanagers[] = $bootmanager;
72
73 foreach ($bootmanagers as $bootmanager) {
74   if (file_exists($bootmanager)) {
75     readfile($bootmanager);
76     exit();
77   }
78 }
79
80 ?>