Handle hostnames with upper-case letters
[webmin.git] / pap / save_mgetty.cgi
1 #!/usr/local/bin/perl
2 # save_mgetty.cgi
3 # Save, create or delete a serial port configuration
4
5 require './pap-lib.pl';
6 $access{'mgetty'} || &error($text{'mgetty_ecannot'});
7 &foreign_require("inittab", "inittab-lib.pl");
8 &ReadParse();
9 @inittab = &inittab::parse_inittab();
10 @mgt = &mgetty_inittabs();
11 if (!$in{'new'}) {
12         ($init) = grep { $_->{'id'} eq $in{'id'} } @mgt;
13         $oldtty = $init->{'tty'};
14         $oldtty = "/dev/$oldtty" if ($oldtty !~ /^\//);
15         }
16
17 &lock_file($inittab::config{'inittab_file'});
18 if ($in{'delete'}) {
19         # Just deleting an inittab entry
20         &inittab::delete_inittab($init);
21         }
22 else {
23         # Validate and store inputs
24         &error_setup($text{'mgetty_err'});
25         $cmd = $in{'new'} ? &has_command($config{'mgetty'}) : $init->{'mgetty'};
26         $cmd .= " -r" if ($in{'direct'});
27         if (!$in{'speed_def'}) {
28                 $in{'speed'} =~ /^\d+$/ || &error($text{'mgetty_espeed'});
29                 $cmd .= " -s $in{'speed'}";
30                 }
31         $in{'rings'} =~ /^\d+$/ || &error($text{'mgetty_eanswer'});
32         $cmd .= " -n $in{'rings'}" if ($in{'rings'} != 1);
33         if ($in{'mode'} == 1) {
34                 $cmd .= " -D";
35                 }
36         elsif ($in{'mode'} == 2) {
37                 $cmd .= " -F";
38                 }
39         if (!$in{'back_def'}) {
40                 $in{'back'} =~ /^\d+$/ || &error($text{'mgetty_eback'});
41                 $cmd .= " -R $in{'back'}";
42                 }
43         if (!$in{'prompt_def'}) {
44                 $cmd .= $in{'prompt'} =~ /"/ ? " -p '$in{'prompt'}'"
45                                              : " -p \"$in{'prompt'}\"";
46                 }
47         if ($init->{'args'}) {
48                 $init->{'args'} =~ s/^\s+//;
49                 $cmd .= " $init->{'args'}";
50                 }
51         if ($in{'tty'}) {
52                 $cmd .= " $in{'tty'}";
53                 $init->{'tty'} = $in{'tty'};
54                 }
55         else {
56                 -r $in{'other'} || &error($text{'mgetty_etty'});
57                 $cmd .= " $in{'other'}";
58                 $init->{'tty'} = $in{'other'};
59                 }
60         $newtty = $init->{'tty'};
61         $newtty = "/dev/$newtty" if ($newtty !~ /^\//);
62         if ($in{'new'} || $newtty ne $oldtty) {
63                 # Check for tty clash
64                 foreach $m (&mgetty_inittabs()) {
65                         local $mtty = $m->{'tty'};
66                         $mtty = "/dev/$mtty" if ($mtty !~ /^\//);
67                         &error(&text('mgetty_eclash', "<tt>$mtty</tt>"))
68                                 if ($mtty eq $newtty);
69                         }
70                 }
71         $cmd .= " $init->{'ttydefs'}" if ($init->{'ttydefs'});
72         $init->{'process'} = $cmd;
73         
74         if ($in{'new'}) {
75                 $maxid = 1;
76                 foreach $i (@inittab) {
77                         $maxid = $i->{'id'} if ($i->{'id'} =~ /^\d+$/ &&
78                                                 $i->{'id'} > $maxid);
79                         }
80                 $init->{'id'} = $maxid + 1;
81                 $init->{'levels'} = [ 2, 3, 4, 5 ];
82                 $init->{'action'} = "respawn";
83                 &inittab::create_inittab($init);
84                 }
85         else {
86                 &inittab::modify_inittab($init);
87                 }
88         }
89 &unlock_file($inittab::config{'inittab_file'});
90 &webmin_log($in{'delete'} ? "delete" : $in{'new'} ? "create" : "modify",
91             "mgetty", $init->{'tty'}, $init);
92
93 &redirect("list_mgetty.cgi");
94