-
Notifications
You must be signed in to change notification settings - Fork 6
/
menu.cgi
executable file
·423 lines (361 loc) · 11.3 KB
/
menu.cgi
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#!/usr/bin/perl -Ilib/perl
# -*- coding: utf-8-unix; mode: perl -*-
#
# menu.cgi - syslinux menu generator over HTTP.
#
# Usage:
# http://host/path/to/m.cgi?q=<query>;r=<expr>;...
#
# Query Parameters;
# q=<query> : Which folder/file/menu/menuitem to show.
# r=<perlexpr>: Filter file with given substitution.
#
# Usage Example:
# - http://host/boot/m.cgi?q=.
# - http://host/boot/m.cgi?q=nfsroot/sid64a
# - http://host/boot/m.cgi?q=pxelinux.cfg/debian
# - http://host/boot/m.cgi?q=pxelinux.cfg/dynamic.tmpl
# - http://host/boot/m.cgi?q=m,debian,+and+m,sid,
# - http://host/boot/m.cgi?q=freebsd/pxeboot;r=s,/pxeroot,/n/fb70a,
#
# Author: Taisuke Yamada <[email protected]>
#
use DirHandle;
use FileHandle;
use File::Basename;
use File::Find;
use IO::String;
use YAML;
use Safe;
use Cwd qw(realpath);
use CGI::Simple;
use Text::ScriptTemplate;
use strict;
use warnings;
######################################################################
# Default configuration
######################################################################
our $CONFIG = {
LIB_URI => undef, # where syslinux *.c32 and other files reside
CGI_URI => undef, # location of this CGI
};
######################################################################
# Internal functions
######################################################################
=item $uri = self_uri();
Returns URI of this CGI.
=cut
sub self_uri {
my $host = $ENV{HTTP_HOST} || "localhost";
my $path = $ENV{SCRIPT_NAME} || "/";
return "http://${host}${path}";
}
=item $bool = is_menu($path);
Returns true if given file seems to be a syslinux menu file.
=cut
sub is_menu {
my $path = shift;
return if -B $path;
return if $path =~ /\.cgi/;
my $file = new FileHandle($path) || return;
while (defined($_ = $file->getline)) {
return 1 if /^label /io;
}
}
=item loadmenu($cgi, [@list]);
Expands syslinux menu template.
=cut
sub loadmenu {
my($cgi, $list, $file) = @_;
my $arg = {
CONFIG => $CONFIG,
CGI => $cgi,
CGI_URI => $CONFIG->{CGI_URI} || self_uri(),
UPLEVEL => dirname($cgi->param('q') || ""),
LIST => $list,
};
$arg->{LIB_URI} = $CONFIG->{LIB_URI} || dirname($arg->{CGI_URI});
return Text::ScriptTemplate->new->load($file)->setq(%$arg)->fill;
}
=item showmenu($cgi, [@list]);
Expands syslinux menu template.
=cut
sub showmenu {
print "Content-Type: text/plain\n\n";
print loadmenu(@_, \*DATA);
}
=item genfail($cgi, $message);
Generates and returns error screen in a form of syslinux menu to a browser.
=cut
sub genfail {
my($cgi, $err) = @_;
showmenu($cgi, [{ type => ":message", text => $err }]);
exit(0);
}
=item genfile($cgi, $path);
Filters and returns specified file content to a browser.
=cut
sub genfile {
my($cgi, $path) = @_;
my $expr = $cgi->param('r');
my $safe = Safe->new;
# minimal permission to use "eval" for substitution
$safe->permit_only(qw{
null const pushmark uc lc quotemeta match list not and or lineseq
padany
leaveeval
subst
});
$_ = join("", FileHandle->new($path)->getlines);
$safe->reval($expr) if $expr;
genfail($cgi, $@) if $@;
print "Content-Type: application/octet-stream\n\n";
print;
}
=item genmenu($cgi, $path);
Expands and returns specified syslinux menu file to a browser.
=cut
sub genmenu {
my($cgi, $path) = @_;
print "Content-Type: text/plain\n\n";
print loadmenu($cgi, [], $path);
}
=item genlist($cgi, $path, $%hint);
Generates and returns folder index in a form of syslinux menu to a browser.
=cut
sub genlist {
my($cgi, $path, %hint) = @_;
my $mcgi = $CONFIG->{CGI_URI} || self_uri();
my @file = DirHandle->new($path)->read;
my @list;
foreach my $name (@file) {
next if $name =~ /^\./;
my $item = {
hint => { %hint }, # create copy
name => $name,
path => "$path/$name",
base => $path,
base_uri => "$mcgi?q=$path",
};
if (-d $item->{path}) {
$item->{type} = ':dir';
$item->{uri} = "$mcgi?q=$item->{path}";
}
elsif ($name =~ /^pxe/) {
$item->{type} = ':pxe';
$item->{uri} = "$mcgi?q=$item->{path}";
}
elsif ($name =~ /^xen/) {
$item->{type} = ':xen';
$item->{uri} = "$mcgi?q=$item->{path}";
}
elsif ($name =~ /^(linux|vmlinux|vmlinuz|bzimage)(-\S+)?$/) {
$item->{type} = ':linux';
$item->{uri} = "$mcgi?q=$item->{path}";
my @initrd = grep { -f "$path/$_" && /^initrd.*$2\b/ } @file;
if (@initrd) {
$item->{initrd} = "$mcgi?q=$path/$initrd[0]";
}
my @iramfs = grep { -f "$path/$_" && /^initramfs.*$2\b/ } @file;
if (@iramfs) {
$item->{type} = ':linux-dracut';
$item->{initrd} = "$mcgi?q=$path/$iramfs[0]";
}
}
elsif (is_menu($item->{path})) {
$item->{type} = ':menu';
$item->{uri} = "$mcgi?q=$item->{path}";
}
elsif ($name =~ /\.(\w+)$/) {
$item->{type} = $1;
$item->{uri} = "$mcgi?q=$item->{path}";
}
push(@list, $item) if defined($item->{type});
}
@list = sort { $a->{name} cmp $b->{name} } @list;
showmenu($cgi, \@list);
}
=item genpack($cgi, $path, $type);
Generates and returns packed archive of given folder to a browser.
=cut
sub genpack {
my($cgi, $base, $type) = @_;
my $name = basename($base);
print "Content-Disposition: attachment; filename=$name.$type\n";
print "Content-Type: application/octet-stream\n\n";
if ($type eq "cgz") {
chdir($base);
system("find . | cpio --quiet -o -H newc | gzip");
}
elsif ($type eq "tgz") {
chdir($base);
system("tar zcf - .");
}
elsif ($type eq "tar") {
chdir($base);
system("tar cf - .");
}
}
=item genscan($cgi, $expr);
Generates dynamic menu from matching menu entries.
=cut
sub genscan {
my($cgi, $expr) = @_;
my @list;
my $safe = Safe->new;
# minimal permission to use "eval" for query matching
$safe->permit_only(qw{
null const pushmark uc lc quotemeta match list not and or lineseq
padany
leaveeval
});
# scan syslinux menu files
find(sub {
my $path = $_;
$File::Find::prune = 1, return if -d "$_/dev" && -d "$_/etc";
return unless is_menu($path);
# expand menu as it could be a template
my $menu = loadmenu($cgi, [], $path);
my $file = IO::String->new($menu);
my @line;
# lookup each "LABEL" entry
while (defined($_ = pop(@line) || $file->getline)) {
next unless /^label /io;
my $buff = $_;
while (defined($_ = $file->getline)) {
next if /^\s*\#/;
push(@line, $_), last if /^label /io;
$buff .= $_;
}
# pick matching entry
$_ = $buff;
push(@list, { type => ":asis", text=>$_ }) if $safe->reval($expr);
warn $@ if $@;
}
$file->close;
}, '.');
@list = sort { $a->{text} cmp $b->{text} } @list;
showmenu($cgi, \@list);
}
######################################################################
# main
######################################################################
my $cgi = CGI::Simple->new;
eval {
my $path = $cgi->param('q') || ".";
my $name = basename($path);
# reject suspicous-looking query
die "Invalid query: $path"
if $path =~ m|^/| or $path =~ /\.\./ or $path =~ /[[:cntrl:]]/;
foreach (qw(/etc/syslinux/mcgi.config .mcgi.config $path/.mcgi.config)) {
do $_ if -f $_; warn $@ if $@;
}
if (-d $path) {
# hack for nfsroot+aufs
if (-d "$path/rw" && -l "$path/ro" && -d "$path/ro/boot") {
my $here = realpath(".");
my $real = realpath("$path/ro") . "/boot";
$real =~ s|^$here/?||;
genlist($cgi, $real, aufs => $path);
}
# hack for nfsroot
elsif (-d "$path/boot" && -d "$path/sbin") {
my $real = "$path/boot";
genlist($cgi, $real);
}
else {
genlist($cgi, $path);
}
}
elsif (is_menu($path)) {
genmenu($cgi, $path);
}
elsif (-s $path) {
genfile($cgi, $path);
}
elsif ($path =~ m/^(\S*\w)\.(cgz|tgz|tar)$/ && -d $1) {
genpack($cgi, $1, $2);
}
else {
genscan($cgi, $path); # handle as query term, not path
}
};
genfail($cgi, "$@ - $!") if $@;
exit(0);
######################################################################
# Default syslinux menu template
######################################################################
__DATA__
######################################################################
# Standard Preamble
######################################################################
serial 0 115200
prompt 1
######################################################################
# Menu Configuration
######################################################################
menu title DEMO: Dynamic menu over HTTP (<%= $CGI->param('q') %>)
menu margin 1
label self
menu label . - Reload
kernel <%= $LIB_URI %>/menu.c32
append <%= $CGI_URI %>?<%= $ENV{QUERY_STRING} %>
label prev
menu label .. - Go uplevel
kernel <%= $LIB_URI %>/menu.c32
append <%= $CGI_URI %>?q=<%= $UPLEVEL %>
label main
menu label main - Jump to main menu
kernel <%= $LIB_URI %>/menu.c32
menu separator
label > This is a sample dynamic syslinux menu over HTTP.
label > With menu.cgi, you can:
label > - Browse folders and boot from files without writing a menu.
label > - Generate menu dynamically from custom template.
label > - Search across menu entries and show result as a menu.
menu separator
######################################################################
# Menu Items
######################################################################
<% foreach my $item (grep { $_->{type} eq ':message' } @$LIST) { %>
say <%= $item->{text} %>
<% } %>
<% foreach my $item (grep { $_->{type} eq ':asis' } @$LIST) { %>
<%= $item->{text} %>
<% } %>
<% foreach my $item (grep { $_->{type} eq ':menu' } @$LIST) { %>
label <%= $item->{name} %>
kernel <%= $LIB_URI %>/menu.c32
append <%= $item->{uri} %>
<% } %>
<% foreach my $item (grep { $_->{type} eq ':dir' } @$LIST) { %>
label <%= $item->{name} %>
menu label <%= $item->{name} %>/
kernel <%= $LIB_URI %>/menu.c32
append <%= $item->{uri} %>
<% } %>
<% foreach my $item (grep { $_->{type} eq ':pxe' } @$LIST) { %>
label <%= $item->{name} %>
pxe <%= $item->{uri} %>;r=s,/pxeroot,/n/,
<% } %>
<% foreach my $item (grep { $_->{type} eq ':linux' } @$LIST) { %>
label <%= $item->{name} %>
kernel <%= $item->{uri} %>
initrd <%= $item->{initrd} %>
append panic=30 console=tty0 console=ttyS0,115200 root=/dev/nfs rw ip=dhcp nfsroot=<%= $ENV{HTTP_HOST} %>:/n/
<% } %>
<% foreach my $item (grep { $_->{type} eq ':linux-dracut' } @$LIST) { %>
label <%= $item->{name} %>
kernel <%= $item->{uri} %>
initrd <%= $item->{initrd} %>
<% if ($item->{uri} =~ m|\b(n/[^/]+)|) { %>
append console=tty0 console=ttyS0,115200 ip=dhcp root=nfs:<%= $ENV{HTTP_HOST} %>:/<%= $1 %>:rw,tcp,soft,intr,vers=3
<% } else { %>
append console=tty0 console=ttyS0,115200 ro root=/dev/ram
<% } %>
<% } %>
<% foreach my $item (grep { $_->{type} eq 'img' } @$LIST) { %>
label <%= $item->{name} %>*
kernel <%= $LIB_URI %>/memdisk
append initrd=<%= $item->{uri} %>
<% } %>