summaryrefslogtreecommitdiffstats
path: root/modules-available/serversetup-bwlp/api.inc.php
blob: 36f9063c24ef74c1844027f7ee299a3f73b5cb87 (plain) (blame)
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php

$BOOT_METHODS = [
	'EXIT' => 'exit 1',
	'COMBOOT' => 'chain /tftp/chain.c32 hd0',
	'SANBOOT' => 'sanboot --no-describe',
];

$serverIp = Property::getServerIp();

$ip = $_SERVER['REMOTE_ADDR'];
if (substr($ip, 0, 7) === '::ffff:') {
	$ip = substr($ip, 7);
}
$uuid = Request::any('uuid', false, 'string');
$menu = IPxeMenu::forClient($ip, $uuid);

// Get platform - EFI or PCBIOS
$platform = strtoupper(Request::any('platform', 'PCBIOS', 'string'));

// Get preferred localboot method, depending on system model
$localboot = false;
$model = false;
if ($uuid !== false && Module::get('statistics') !== false) {
	$row = Database::queryFirst('SELECT systemmodel FROM machine WHERE machineuuid = :uuid', ['uuid' => $uuid]);
	if ($row !== false && !empty($row['systemmodel'])) {
		$model = $row['systemmodel'];
	}
}
if ($model === false) {
	function modfilt($str)
	{
		if (empty($str) || preg_match('/product\s+name|be\s+filled|unknown|default\s+string/i', $str))
			return false;
		return trim(preg_replace('/\s+/', ' ', $str));
	}
	$manuf = modfilt(Request::any('manuf', false, 'string'));
	$product = modfilt(Request::any('product', false, 'string'));
	if (!empty($product)) {
		$model = $product;
		if (!empty($manuf)) {
			$model .= " ($manuf)";
		}
	}
}
// Query
if ($model !== false) {
	$row = Database::queryFirst("SELECT bootmethod FROM serversetup_localboot WHERE systemmodel = :model LIMIT 1",
		['model' => $model]);
	if ($row !== false) {
		$localboot = $row['bootmethod'];
	}
}
if ($localboot === false || !isset($BOOT_METHODS[$localboot])) {
	$localboot = Property::get('serversetup.localboot', false);
	if ($localboot === false) {
		if ($platform === 'EFI') {
			// It seems most (all) EFI platforms won't enumerate any drives in ipxe.
			// No idea if this can be fixed in ipxe code in the future.
			$localboot = 'EXIT';
		} else {
			$localboot = 'SANBOOT';
		}
	}
}
if (isset($BOOT_METHODS[$localboot])) {
	// Move preferred method first
	$BOOT_METHODS[] = $BOOT_METHODS[$localboot];
	unset($BOOT_METHODS[$localboot]);
	$BOOT_METHODS = array_reverse($BOOT_METHODS);
}

$output = <<<HERE
#!ipxe

goto init || goto fail ||

# functions

# password check with gotos
# set slx_hash to the expected hash
#     slx_salt to the salt to use
#     slx_pw_ok to the label to jump on success
#     slx_pw_fail to label for wrong pw
:slx_pass_check
login ||
set slxtmp_pw \${password:md5}-\${slx_salt} || goto fail
set slxtmp_pw \${slxtmp_pw:md5} || goto fail
clear password ||
iseq \${slxtmp_pw} \${slx_hash} || prompt Wrong password. Press a key. ||
iseq \${slxtmp_pw} \${slx_hash} || goto \${slx_pw_fail} ||
iseq \${slxtmp_pw} \${slx_hash} && goto \${slx_pw_ok} ||
goto fail

# local boot with either exit 1 or sanboot
:slx_localboot
console ||

HERE;

foreach ($BOOT_METHODS as $line) {
	$output .= "$line || goto fail\n";
}

$output .= <<<HERE
goto fail

# start
:init

iseq \${nic} \${} && set nic 0 ||

set ipappend1 ip=\${net\${nic}/ip}:{$serverIp}:\${net\${nic}/gateway}:\${net\${nic}/netmask}
set ipappend2 BOOTIF=01-\${net\${nic}/mac:hexhyp}
set serverip $serverIp ||

# Clean up in case we've been chained to
imgfree ||

ifopen ||

imgfetch --name bg-load /tftp/openslx.png ||

imgfetch --name bg-menu /tftp/pxe-menu.png ||

:start

console --left 55 --top 88 --right 63 --bottom 64 --keep --picture bg-menu ||

colour --rgb 0xffffff 7
colour --rgb 0xcccccc 5
cpair --foreground 0 --background 4 1
cpair --foreground 7 --background 5 2
cpair --foreground 7 --background 9 0

:slx_menu

console --left 55 --top 88 --right 63 --bottom 64 --quick --keep --picture bg-menu ||

HERE;

$output .= $menu->getMenuDefinition('target');

$output .= <<<HERE

console --left 60 --top 130 --right 67 --bottom 86 --quick ||
goto \${target} ||
echo Could not find menu entry in script.
prompt Press any key to continue.
goto start

HERE;

$output .= $menu->getItemsCode();

/*
:i1
#console ||
echo Welcome to Shell ||
shell
goto slx_menu

:i2
imgfree ||
kernel /boot/default/kernel slxbase=boot/default slxsrv=$serverIp splash BOOTIF=01-\${net\${nic}/mac:hexhyp} || echo Could not download kernel
initrd /boot/default/initramfs-stage31 || echo Could not download initrd
boot -ar || goto fail

:i3
chain -ar \${self} ||
chain -ar /tftp/undionly.kpxe || goto fail

:i4
imgfree ||
sanboot --no-describe --drive 0x80 || goto fail

:i5
chain -a /tftp/memtest.0 passes=1 onepass || goto membad
prompt Memory OK. Press a key.
goto init

:i6
console --left 60 --top 130 --right 67 --bottom 96 --quick --picture bg-load --keep ||
echo Welcome to Shell ||
shell
goto slx_menu

:i7
chain -ar tftp://132.230.4.6/ipxelinux.0 || prompt FAILED PRESS A KEY
goto slx_menu

:i8
set x:int32 0
:again
console --left 60 --top 130 --right 67 --bottom 96 --picture bg-load --keep --quick ||
console --left 55 --top 88 --right 63 --bottom 64 --picture bg-menu --keep --quick ||
inc x
iseq \${x} 20 || goto again
prompt DONE. Press dein Knie.
goto slx_menu

:i9
reboot ||
prompt Reboot failed. Press a key.
goto slx_menu

:i10
poweroff ||
prompt Poweroff failed. Press a key.
goto slx_menu

:membad
iseq \${errno} 0x1 || goto memaborted
params
param scrot \${vram}
imgfetch -a http://132.230.8.113/screen.php##params ||
prompt Memory is bad. Press a key.
goto init

:memaborted
params
param scrot \${vram}
imgfetch -a http://132.230.8.113/screen.php##params ||
prompt Memory test aborted. Press a key.
goto init

*/

$output .= <<<HERE
:fail
prompt Boot failed. Press any key to start.
goto init
HERE;

if ($platform === 'EFI') {
	$cs = 'ASCII';
} else {
	$cs = 'IBM437';
}
Header('Content-Type: text/plain; charset=' . $cs);

echo iconv('UTF-8', $cs . '//TRANSLIT//IGNORE', $output);