Handle hostnames with upper-case letters
[webmin.git] / vgetty / save_vgetty.cgi
1 #!/usr/local/bin/perl
2 # save_vgetty.cgi
3 # Save, create or delete a serial port configuration
4
5 require './vgetty-lib.pl';
6 &foreign_require("inittab", "inittab-lib.pl");
7 &ReadParse();
8 @inittab = &inittab::parse_inittab();
9 @vgt = &vgetty_inittabs();
10 if (!$in{'new'}) {
11         ($init) = grep { $_->{'id'} eq $in{'id'} } @vgt;
12         $oldtty = $init->{'tty'};
13         $oldtty = "/dev/$oldtty" if ($oldtty !~ /^\//);
14         }
15 @conf = &get_config();
16 $rings = &find_value("rings", \@conf);
17 $ans = &find_value("answer_mode", \@conf);
18
19 &lock_file($inittab::config{'inittab_file'});
20 if ($in{'delete'}) {
21         # Just deleting an inittab entry
22         &inittab::delete_inittab($init);
23         if (defined($in{'rings_def'})) {
24                 local $tf = &tty_opt_file($ans, $oldtty);
25                 &lock_file($tf);
26                 unlink($tf);
27                 &unlock_file($tf);
28                 }
29         if (defined($in{'ans_def'})) {
30                 $tf = &tty_opt_file($rings, $oldtty);
31                 &lock_file($tf);
32                 unlink($tf);
33                 &unlock_file($tf);
34                 }
35         }
36 else {
37         # Validate and store inputs
38         &error_setup($text{'vgetty_err'});
39         $cmd = $in{'new'} ? &has_command("vgetty") : $init->{'vgetty'};
40         if ($init->{'args'}) {
41                 $init->{'args'} =~ s/^\s+//;
42                 $cmd .= " $init->{'args'}";
43                 }
44         if ($in{'tty'}) {
45                 $cmd .= " $in{'tty'}";
46                 $init->{'tty'} = $in{'tty'};
47                 }
48         else {
49                 -r $in{'other'} || &error($text{'vgetty_etty'});
50                 $cmd .= " $in{'other'}";
51                 $init->{'tty'} = $in{'other'};
52                 }
53         $newtty = $init->{'tty'};
54         $newtty = "/dev/$newtty" if ($newtty !~ /^\//);
55         if ($in{'new'} || $newtty ne $oldtty) {
56                 # Check for tty clash
57                 foreach $v (&vgetty_inittabs()) {
58                         local $vtty = $v->{'tty'};
59                         $vtty = "/dev/$vtty" if ($vtty !~ /^\//);
60                         &error(&text('vgetty_eclash', "<tt>$vtty</tt>"))
61                                 if ($vtty eq $newtty);
62                         }
63                 }
64         $init->{'process'} = $cmd;
65
66         if (defined($in{'rings_def'})) {
67                 $tf = &tty_opt_file($rings, $init->{'tty'});
68                 &lock_file($tf);
69                 if (!$in{'new'} && $oldtty ne $newtty) {
70                         unlink(&tty_opt_file($rings, $oldtty));
71                         }
72                 if ($in{'rings_def'}) {
73                         unlink($tf);
74                         }
75                 else {
76                         $in{'rings'} =~ /^\d+$/ ||
77                                 &error($text{'vgetty_erings'});
78                         $in{'rings'} >= 2 || &error($text{'vgetty_erings2'});
79                         &open_tempfile(TF, ">$tf");
80                         &print_tempfile(TF, $in{'rings'},"\n");
81                         &close_tempfile(TF);
82                         }
83                 &unlock_file($tf);
84                 }
85
86         if (defined($in{'ans_def'})) {
87                 $tf = &tty_opt_file($ans, $init->{'tty'});
88                 &lock_file($tf);
89                 if (!$in{'new'} && $oldtty ne $newtty) {
90                         unlink(&tty_opt_file($ans, $oldtty));
91                         }
92                 if ($in{'ans_def'}) {
93                         unlink($tf);
94                         }
95                 else {
96                         $mode = &parse_answer_mode("ans");
97                         $mode || &error($text{'vgetty_eans'});
98                         &open_tempfile(TF, ">$tf");
99                         &print_tempfile(TF, $mode,"\n");
100                         &close_tempfile(TF);
101                         }
102                 &unlock_file($tf);
103                 }
104         
105         if ($in{'new'}) {
106                 $maxid = 1;
107                 foreach $i (@inittab) {
108                         $maxid = $i->{'id'} if ($i->{'id'} =~ /^\d+$/ &&
109                                                 $i->{'id'} > $maxid);
110                         }
111                 $init->{'id'} = $maxid + 1;
112                 $init->{'levels'} = [ 2, 3, 4, 5 ];
113                 $init->{'action'} = "respawn";
114                 &inittab::create_inittab($init);
115                 }
116         else {
117                 &inittab::modify_inittab($init);
118                 }
119         }
120 &unlock_file($inittab::config{'inittab_file'});
121 &webmin_log($in{'delete'} ? "delete" : $in{'new'} ? "create" : "modify",
122             "vgetty", $init->{'tty'}, $init);
123
124 &redirect("list_vgetty.cgi");
125