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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
|
#!/usr/bin/env perl
#-*- Mode: perl; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
# Boot manager configurator: LILO-related routines.
#
# Copyright (C) 2001 Ximian, Inc.
#
# Authors: Arturo Espinosa <arturo@ximian.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Library General Public License as published
# by the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
$SCRIPTSDIR = "@scriptsdir@";
if ($SCRIPTSDIR =~ /^@scriptsdir[@]/)
{
$SCRIPTSDIR = ".";
$DOTIN = ".in";
}
require "$SCRIPTSDIR/general.pl$DOTIN";
require "$SCRIPTSDIR/util.pl$DOTIN";
require "$SCRIPTSDIR/file.pl$DOTIN";
require "$SCRIPTSDIR/xml.pl$DOTIN";
require "$SCRIPTSDIR/parse.pl$DOTIN";
require "$SCRIPTSDIR/replace.pl$DOTIN";
require "$SCRIPTSDIR/partition.pl$DOTIN";
my @lilo_global_vars = qw (default prompt delay timeout append boot root);
my @lilo_common_image_vars = qw (label password);
my @lilo_image_vars = (@lilo_common_image_vars, qw (image append root vga initrd));
my @lilo_other_vars = (@lilo_common_image_vars, qw (other));
sub gst_boot_lilo_verify_entrylabel
{
my ($label) = @_;
return "toolong" if length ($label) > 15;
$label =~ s/[\w!%^&*()_\-:;+\[\]{}?\/<>]//g;
return "badchar" if $label ne "";
return "success";
}
sub gst_boot_lilo_verify
{
my ($key, @values) = @_;
my ($i);
my %keymap =
(
"entrylabel" => \&gst_boot_lilo_verify_entrylabel
);
return "notable" if not exists ($keymap{$key});
return &{$keymap{$key}}(@values);
}
sub gst_boot_lilo_known_var
{
my $key = shift;
my $list = shift;
my $from_xml = shift;
if (ref ($list) ne "ARRAY")
{
# TODO: Give warning;
return 0;
}
$key = lc ($key);
# Hard coded known variables which are not standard lilo.conf variables.
return 0 if ($key eq "key" && $from_xml); # "key" is valid in xml only.
return 0 if ($key eq "type" && $from_xml); # "key" is valid in xml only.
return &gst_item_is_in_list ($key, @$list);
}
sub gst_boot_lilo_parse_global_kw
{
my ($file, $key) = @_;
my $fd;
return undef unless &gst_boot_lilo_known_var ($key, \@lilo_global_vars, 0);
$fd = &gst_file_open_read_from_names ($file);
if (! $fd)
{
&gst_report ("boot_conf_read_failed", $file);
return undef;
}
while (<$fd>)
{
chomp;
s/^[ \t]+//;
next if (/^\#/ || /^$/);
last if (/^(image|other)/);
if ($key eq $_)
{
&gst_file_close ($fd);
return 1;
}
}
&gst_file_close ($fd);
return undef;
}
sub gst_boot_lilo_parse_global
{
my ($file, $key) = @_;
my ($fd, $line, $re);
return undef unless &gst_boot_lilo_known_var ($key, \@lilo_global_vars, 0);
$re = "[ \t]*=[ \t]*";
$fd = &gst_file_open_read_from_names ($file);
if (! $fd)
{
&gst_report ("boot_conf_read_failed", $file);
return undef;
}
while ($line = <$fd>)
{
chomp $line;
$line =~ s/^[ \t]+//;
$line = &gst_parse_process_sh_line ($line);
next if ($line eq "");
last if ($line =~ /^(image|other)/);
my @line = split ($re, $line, 2);
if (shift (@line) eq $key)
{
&gst_file_close ($fd);
return $line[0];
}
}
&gst_file_close ($fd);
return undef;
}
sub gst_boot_lilo_set_default_type
{
my ($entry, $partition) = @_;
my ($dev, $type);
$dev = $$entry{"root"};
$dev = $$entry{"other"} if $dev eq undef;
$dev =~ s/.*\///;
if (exists $$partition{$dev})
{
$type = $ {$$partition{$dev}}{"typestr"};
$$entry{"type"} = $type if $type ne undef;
}
}
sub gst_boot_lilo_parse_entries
{
my ($file, $partition) = @_;
my ($fd, $line, @entries, $entry);
my ($line, $known_vars, $found);
$found = -1;
$fd = &gst_file_open_read_from_names ($file);
if (! $fd)
{
&gst_report ("boot_conf_read_failed", $file);
return undef;
}
while (($line = <$fd>))
{
chomp $line;
$line =~ s/^[ \t]+//;
if ($line =~ /^image/i)
{
$found++;
$known_vars = \@lilo_image_vars;
$entries[$found]{"key"} = $found;
}
elsif ($line =~ /^other/i)
{
$found++;
$known_vars = \@lilo_other_vars;
$entries[$found]{"key"} = $found;
}
next if $found < 0;
next if $line =~ /^\#/;
$line = &gst_parse_process_sh_line ($line);
next if $line eq "";
my @line = split ("[ \t]*=[ \t]*", $line, 2);
my $key = shift @line;
# Only deal with known variables
next unless &gst_boot_lilo_known_var ($key, $known_vars, 0);
my $val = shift @line;
$val =~ s/^[ \t]+//;
$val =~ s/\"//g;
$val =~ s/[ \t]+$//;
#if the variable is "vga", we append it to the "append" var
if ($key eq "vga")
{
$entries[$found]{"append"} .= " vga=" . $val. " ";
}
else
{
$entries[$found]{$key} = $val;
}
}
&gst_file_close ($fd);
foreach $entry (@entries)
{
&gst_boot_lilo_set_default_type ($entry, $partition);
}
return \@entries;
}
sub gst_boot_lilo_replace_global_kw
{
my ($file, $key) = @_;
my ($buff, $i, $found);
my $lineno = 0;
return 0 unless &gst_boot_lilo_known_var ($key, \@lilo_global_vars, 0);
$buff = &gst_file_buffer_load ($file);
$found = 0;
foreach $i (@$buff)
{
if (&gst_ignore_line ($i))
{
$lineno++;
next;
}
if ($i =~ /^[ \t]*$key/)
{
$found++;
last;
}
if ($i =~ /^[ \t]*(image|other)/)
{
$lineno--; # "pop" it back
last;
}
$lineno++;
}
# Not found, let's add
if (!$found)
{
# pop back empty lines
while ($lineno > 0 && $$buff[$lineno] =~ /^\s*$/)
{
$lineno--;
}
$$buff[$lineno] .= $key . "\n";
}
&gst_file_buffer_clean ($buff);
return &gst_file_buffer_save ($buff, $file);
}
sub gst_boot_lilo_replace_global
{
my ($file, $key, $val) = @_;
my ($buff, $i, $found);
my $quote = '"';
my $lineno = 0;
return 0 unless &gst_boot_lilo_known_var ($key, \@lilo_global_vars, 0);
$val = "\"$val\"" if ($val =~ /[ \t]/ && (! ($val =~ /^\".+\"$/)));
$val = "\"$val\"" if ($val =~ /\=/ && (!($val =~ /^\".+\"$/)));
$buff = &gst_file_buffer_load ($file);
$found = 0;
foreach $i (@$buff)
{
if (&gst_ignore_line ($i))
{
$lineno++;
next;
}
if ($i =~ /^[ \t]*(image|other)/)
{
$lineno--;
last;
}
if ($i =~ /^[ \t]*$key([ \t]*=[ \t]*)/)
{
$found++;
my $op = $1;
chomp $i;
my $pre_space = $1 if $i =~ s/^([ \t]+)//;
my $post_comment = $1 if $i =~ s/([ \t]*\#.*)//;
$i = $pre_space . $key . $op . $val . $post_comment . "\n";
last;
}
$lineno++;
}
if (!$found)
{
# pop back empty lines
while ($lineno > 0 && $$buff[$lineno] =~ /^\s*$/)
{
$lineno--;
}
$$buff[$lineno] .= $key . " = " . $val . "\n";
}
&gst_file_buffer_clean ($buff);
return &gst_file_buffer_save ($buff, $file);
}
sub gst_boot_lilo_del_global
{
my ($file, $key) = @_;
my ($buff, $i);
return 0 unless &gst_boot_lilo_known_var ($key, \@lilo_global_vars, 0);
$buff = &gst_file_buffer_load ($file);
foreach $i (@$buff)
{
last if ($i =~ /^[ \t]*(image|other)/);
if ($i =~ /^[ \t]*$key/)
{
$i = "";
last;
}
}
&gst_file_buffer_clean ($buff);
return &gst_file_buffer_save ($buff, $file);
}
# Scans @buff until finds first line which looks like entry.
# Returns line number or -1 if no entry found.
sub gst_boot_lilo_find_entry
{
my ($buff, $lineno) = @_;
my $i;
for (; $lineno <= $#$buff; $lineno++)
{
$i = $$buff[$lineno];
return $lineno if ($i =~ /^[ \t]*(image)|(other)[ \t]*=[ \t]*\S+/);
}
# Not found.
return -1;
}
# Deletes lines from $buff starting from $lineno till the next entry
# or till end of the file if no more entries left.
sub gst_boot_lilo_delete_entry
{
my ($buff, $lineno) = @_;
my ($end, $i);
$end = &gst_boot_lilo_find_entry ($buff, $lineno + 1);
$end = scalar @$buff if ($end < 0);
for ($i = $lineno; $i < $end; $i++)
{
$$buff[$i] = "";
}
return $buff;
}
# Edit entry in $buff which starts at $lineno. Get changes from $entry.
sub gst_boot_lilo_edit_entry
{
my ($entry, $buff, $lineno) = @_;
my $known_vars = \@lilo_image_vars if (exists $entry->{'image'});
$known_vars = \@lilo_other_vars if (!$known_vars && exists $entry->{'other'});
return $buff unless $known_vars;
my $end = &gst_boot_lilo_find_entry ($buff, $lineno + 1);
$end = scalar @$buff if ($end < 0);
# extract the "vga" var from the "append" var, this is done this way for
# compatibility with grub
if ($entry->{"append"} =~ /[ \t]*vga[ \t]*=[ \t]*(((0x)?[0-9][0-9][0-9]|ask))/)
{
$entry->{"vga"} = $1;
$entry->{"append"} =~ s/[ \t]*vga[ \t]*=[ \t]*((0x)?[0-9][0-9][0-9]|ask)//;
}
for ($lineno; $lineno < $end; $lineno++)
{
# Get the variable.
my $key = $1 if ($$buff[$lineno] =~ /^[ \t]*([\w\-]+)/);
next if ($key && (!&gst_boot_lilo_known_var ($key, $known_vars, 0)));
next unless $key;
unless (exists ($entry->{$key}))
{
# Keyword is known, but isn't in entry.
delete $$buff[$lineno];
next;
}
# Read old value
my $old_val = $$buff[$lineno];
$old_val =~ s/^[ \t]*$key[ \t]*=[ \t]*//; #everything till value
$old_val =~ s/[ \t]*\#.*$//; #post comment;
chomp $old_val;
if ($old_val)
{
# String.
my $val = $entry->{$key};
$val = "\"$val\"" if ($val =~ /[ \t]/ && (! ($val =~ /^\".+\"$/)));
$val = "\"$val\"" if ($val =~ /\=/ && (!($val =~ /^\".+\"$/)));
$$buff[$lineno] =~ s/$old_val/$val/;
}
delete $entry->{$key};
}
# Add new fields.
foreach my $key (keys %$entry)
{
next unless &gst_boot_lilo_known_var ($key, $known_vars, 1);
my $val = $entry->{$key};
$val = "\"$val\"" if ($val =~ /[ \t]/ && (! ($val =~ /^\".+\"$/)));
$val = "\"$val\"" if ($val =~ /\=/ && (!($val =~ /^\".+\"$/)));
my $line = "\t$key";;
$line .= " = $val" if $val;
$line .= "\n";
$$buff[$end -1] .= $line;
}
}
# Add $entry to the end of $buff.
sub gst_boot_lilo_add_entry
{
my ($entry, $buff) = @_;
my ($line, $key, $value, $known_vars);
# Entry line
if (exists $entry->{'image'})
{
$known_vars = \@lilo_image_vars;
$value = 'image';
}
elsif (exists $entry->{'other'})
{
$known_vars = \@lilo_other_vars;
$value = 'other';
}
else
{
return;
}
$line = $value . " = " . $$entry{$value} . "\n";
push @$buff, $line;
delete $$entry{$value};
# extract the "vga" var from the "append" var, this is done this way for
# compatibility with grub
if ($entry->{"append"} =~ /[ \t]*vga[ \t]*=[ \t]*(((0x)?[0-9][0-9][0-9]|ask))/)
{
$entry->{"vga"} = $1;
$entry->{"append"} =~ s/[ \t]*vga[ \t]*=[ \t]*((0x)?[0-9][0-9][0-9]|ask)//;
}
# Parameters for entry
foreach $key (keys %$entry)
{
next unless &gst_boot_lilo_known_var ($key, $known_vars, 1);
$value = $$entry{$key};
$value = "\"$value\"" if ($value =~ /[ \t]/ && (!($value =~ /^\".+\"$/)));
$value = "\"$value\"" if ($value =~ /\=/&& (!($value =~ /^\".+\"$/)));
$line = "\t$key";
$line .= " = " . $value if $value;
$line .= "\n";
push @$buff, $line;
}
}
sub gst_boot_lilo_entries_set
{
my ($file, $entries) = @_;
my ($buff, $lineno, $found, $entry);
return if (scalar @$entries <= 0);
$buff = &gst_file_buffer_load ($file);
&gst_file_buffer_join_lines ($buff);
my $entry_nr = -1;
$lineno = &gst_boot_lilo_find_entry ($buff, 0);
while ($lineno > 0)
{
$entry_nr++;
$found = 0;
foreach $entry (@$entries)
{
next unless $entry;
if (exists ($entry->{"key"}))
{
$found++ if $entry->{"key"} == $entry_nr;
if ($found > 0)
{
# Found entry, change it if necessary,
# remove %entry from @entries and find new entry.
&gst_boot_lilo_edit_entry ($entry, $buff, $lineno);
$entry = undef;
last;
}
}
}
# Found entry wasn't in our @entries list: delete.
$buff = &gst_boot_lilo_delete_entry ($buff, $lineno) if ($found <= 0);
# Search new entry line.
$lineno = &gst_boot_lilo_find_entry ($buff, $lineno + 1);
}
# At this point @entries contains only new entries, let's add them.
foreach $entry (@$entries)
{
&gst_boot_lilo_add_entry ($entry, $buff);
}
&gst_file_buffer_clean ($buff);
return &gst_file_buffer_save ($buff, $file);
}
|