This repository was archived by the owner on Feb 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboot.php
More file actions
executable file
·150 lines (140 loc) · 4.11 KB
/
boot.php
File metadata and controls
executable file
·150 lines (140 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
function check_mac($item) {
global $cfg;
$mac_arr = $item["mac"];
$mac_pattern = '/(' . $cfg->mac . ')(\s*m?)$/i';
if ($mac_arr) {
foreach ($mac_arr as $mac) {
if (preg_match($mac_pattern,$mac,$matches)) {
return array("item" => $item, "show_menu" => (preg_match('/m/i',$matches[2])));
break;
}
}
}
return false;
}
function check_ip($item) {
global $cfg;
$ip_arr = $item["ip"];
$range_pattern = '/^\s*(.*?)\.(\d+)\s*\-\s*(.*?)\.(\d+)(\s*m?)\s*$/i';
$ip_pattern = '/^\s*(.*?)\.(\d+)\s*$/';
if ($ip_arr) {
foreach ($ip_arr as $ip) {
// check ip
$curr_ip_pattern = '/^\s*(' . $cfg->ip . ')(\s*m?)$/';
if (preg_match($curr_ip_pattern,$ip,$matches)) {
return array("item" => $item, "show_menu" => (preg_match('/m/i',$matches[2])));
}
// check ip range
if (preg_match($range_pattern,$ip,$matches)) {
// valid subnet range?
$subnet = $matches[1];
if ($matches[1] != $matches[3]) {
// ToDo: log?
continue;
}
$min = (int)$matches[2];
$max = (int)$matches[4];
$show_menu = $matches[5];
// valid subnet?
if (preg_match($ip_pattern,$cfg->ip,$matches)) {
if ($matches[1] == $subnet) {
$val = (int)$matches[2];
if ($val >= $min && $val <= $max) {
return array("item" => $item, "show_menu" => (preg_match('/m/i',$show_menu)));
}
}
}
}
}
}
return false;
}
function check_host($item) {
global $cfg;
$host_arr = $item["host"];
$host_pattern = '/^\s*(' . $cfg->host . ')(\s*m?)\s*$/';
foreach ($host_arr as $host) {
if (preg_match($host_pattern,$host,$matches)) {
return array("item" => $item, "show_menu" => (preg_match('/m/i',$matches[2])));
break;
}
}
}
/**
* check if ip exists in netmask p.e. 192.168.18.0/24
* example:
* foreach ( $n as $k=>$v ) {
* list($net,$mask)=split("/",$k);
* if (ipInMask($myip,$net,$mask)) {
* echo $n[$k]."<br />\n"; }
* }
*/
function ipInMask($ip,$net,$mask) {
$lnet=ip2long($net);
$lip=ip2long($ip);
$binnet=str_pad( decbin($lnet),32,"0","STR_PAD_LEFT" );
$firstpart=substr($binnet,0,$mask);
$binip=str_pad( decbin($lip),32,"0","STR_PAD_LEFT" );
$firstip=substr($binip,0,$mask);
return(strcmp($firstpart,$firstip)==0);
}
function boot() {
global $items, $cfg;
if ($cfg->mac == '' || $cfg->ip == '' || $cfg->host == '') {
out_txt("wrong parameter");
}
foreach ($items as $k => $item) {
$ret = check_mac($item);
if ($ret) {
out($ret["item"],$ret["show_menu"]);
break;
return;
}
$ret = check_ip($item);
if ($ret) {
out($ret["item"],$ret["show_menu"]);
break;
return;
}
$ret = check_host($item);
if ($ret) {
out($ret["item"],$ret["show_menu"]);
break;
return;
}
}
// ToDo: ipxe message or default image....
out_txt("no mac/ip/hostname assigned");
}
function out($item,$show_menu) {
global $menu;
$txt = "";
if ($show_menu) {
$txt = <<<EOF
#!ipxe
$menu
EOF;
}
else {
$kernel = $item["kernel"];
$initrd = $item["initrd"];
$imgargs = $item["imgargs"];
$txt = <<<EOF
#!ipxe
kernel $kernel
initrd $initrd
imgargs $imgargs
boot
exit 0
EOF;
}
out_txt($txt);
}
function out_txt($txt) {
ob_end_clean();
header("Content-Type: text/plain");
echo $txt;
exit;
}
?>