Handle hostnames with upper-case letters
[webmin.git] / pap / pap-lib.pl
1 # pap-lib.pl
2 # Functions for managing the mgetty configuration files
3
4 BEGIN { push(@INC, ".."); };
5 use WebminCore;
6 &init_config();
7 %access = &get_module_acl();
8
9 do 'secrets-lib.pl';
10
11 # mgetty_inittabs()
12 # Returns a list of inittab entries for mgetty, with options parsed
13 sub mgetty_inittabs
14 {
15 local @rv;
16 foreach $i (&inittab::parse_inittab()) {
17         if ($i->{'process'} =~ /^(\S*mgetty)\s*(.*)\s+((\/.*)?(tty|term|cua)\S+)(\s+(\S+))?$/) {
18                 $i->{'mgetty'} = $1;
19                 $i->{'args'} = $2;
20                 $i->{'tty'} = $3;
21                 $i->{'ttydefs'} = $7;
22                 if ($i->{'args'} =~ s/\s*-s\s+(\d+)//) {
23                         $i->{'speed'} = $1;
24                         }
25                 if ($i->{'args'} =~ s/\s*-r//) {
26                         $i->{'direct'} = 1;
27                         }
28                 if ($i->{'args'} =~ s/\s*-n\s+(\d+)//) {
29                         $i->{'rings'} = $1;
30                         }
31                 if ($i->{'args'} =~ s/\s*-D//) {
32                         $i->{'data'} = 1;
33                         }
34                 if ($i->{'args'} =~ s/\s*-F//) {
35                         $i->{'fax'} = 1;
36                         }
37                 if ($i->{'args'} =~ s/\s*-R\s+(\d+)//) {
38                         $i->{'back'} = $1;
39                         }
40                 if ($i->{'args'} =~ s/\s*-p\s+'([^']+)'// ||
41                     $i->{'args'} =~ s/\s*-p\s+"([^"]+)"// ||
42                     $i->{'args'} =~ s/\s*-p\s+(\S+)//) {
43                         $i->{'prompt'} = $1;
44                         }
45                 push(@rv, $i);
46                 }
47         elsif ($i->{'process'} =~ /^(\S*vgetty)\s*(.*)\s+((\/.*)?tty\S+)/) {
48                 $i->{'vgetty'} = $1;
49                 $i->{'args'} = $2;
50                 $i->{'tty'} = $3;
51                 push(@rv, $i);
52                 }
53         }
54 return @rv;
55 }
56
57 # parse_ppp_options(file)
58 sub parse_ppp_options
59 {
60 local @rv;
61 local $lnum = 0;
62 open(OPTS, $_[0]);
63 while(<OPTS>) {
64         s/\r|\n//g;
65         s/#.*$//g;
66         if (/^([0-9\.]+):([0-9\.]+)/) {
67                 push(@rv, { 'local' => $1,
68                             'remote' => $2,
69                             'file' => $_[0],
70                             'line' => $lnum,
71                             'index' => scalar(@rv) });
72                 }
73         elsif (/^(\S+)\s*(.*)/) {
74                 push(@rv, { 'name' => $1,
75                             'value' => $2,
76                             'file' => $_[0],
77                             'line' => $lnum,
78                             'index' => scalar(@rv) });
79                 }
80         $lnum++;
81         }
82 close(OPTS);
83 return @rv;
84 }
85
86 # find(name, &config)
87 sub find
88 {
89 local @rv = grep { lc($_->{'name'}) eq lc($_[0]) } @{$_[1]};
90 return wantarray ? @rv : $rv[0];
91 }
92
93 # save_ppp_option(&config, file, &old|name, &new)
94 sub save_ppp_option
95 {
96 local $ol = ref($_[2]) || !defined($_[2]) ? $_[2] : &find($_[2], $_[0]);
97 local $nw = $_[3];
98 local $lref = &read_file_lines($_[1]);
99 local $line;
100 if ($nw) {
101         if ($nw->{'local'}) {
102                 $line = $nw->{'local'}.":".$nw->{'remote'};
103                 }
104         else {
105                 $line = $nw->{'name'};
106                 $line .= " $nw->{'value'}" if ($nw->{'value'} ne "");
107                 }
108         }
109 if ($ol && $nw) {
110         $lref->[$ol->{'line'}] = $line;
111         }
112 elsif ($ol) {
113         splice(@$lref, $ol->{'line'}, 1);
114         local $c;
115         foreach $c (@{$_[0]}) {
116                 $c->{'line'}-- if ($c->{'line'} > $ol->{'line'});
117                 }
118         }
119 elsif ($nw) {
120         push(@$lref, $line);
121         }
122 }
123
124 # parse_login_config()
125 # Parses the mgetty login options file into a list of users
126 sub parse_login_config
127 {
128 local @rv;
129 local $lnum = 0;
130 open(LOGIN, $config{'login_config'});
131 while(<LOGIN>) {
132         s/\r|\n//g;
133         s/#.*$//g;
134         if (/^(\S+)\s+(\S+)\s+(\S+)\s+(.*)/) {
135                 push(@rv, { 'user' => $1,
136                             'userid' => $2,
137                             'utmp' => $3,
138                             'program' => $4,
139                             'line' => $lnum });
140                 }
141         $lnum++;
142         }
143 close(LOGIN);
144 return @rv;
145 }
146
147 # delete_login_config(&config, &login)
148 sub delete_login_config
149 {
150 local $lref = &read_file_lines($config{'login_config'});
151 splice(@$lref, $_[1]->{'line'}, 1);
152 }
153
154 # create_login_config(&config, &login)
155 sub create_login_config
156 {
157 local ($star) = grep { $_->{'user'} eq '*' } @{$_[0]};
158 local $line = join("\t", $_[1]->{'user'}, $_[1]->{'userid'},
159                          $_[1]->{'utmp'}, $_[1]->{'program'});
160 local $lref = &read_file_lines($config{'login_config'});
161 if ($star) {
162         splice(@$lref, $star->{'line'}, 0, $line);
163         }
164 else {
165         push(@$lref, $line);
166         }
167 }
168
169 # parse_dialin_config()
170 # Parses the mgetty dialin file
171 sub parse_dialin_config
172 {
173 local @rv;
174 local $lnum = 0;
175 open(DIALIN, $config{'dialin_config'});
176 while(<DIALIN>) {
177         s/\r|\n//g;
178         s/#.*$//g;
179         s/^\s+//;
180         local $t;
181         foreach $t (split(/[ \t,]+/, $_)) {
182                 local ($not) = ($t =~ s/^\!//);
183                 push(@rv, { 'number' => $t,
184                             'not' => $not,
185                             'index' => scalar(@rv),
186                             'line' => $lnum });
187                 }
188         $lnum++;
189         }
190 close(DIALIN);
191 return @rv;
192 }
193
194 # create_dialin(&dialin)
195 sub create_dialin
196 {
197 &open_tempfile(DIALIN, ">>$config{'dialin_config'}");
198 &print_tempfile(DIALIN, &dialin_line($_[0])."\n");
199 &close_tempfile(DIALIN);
200 }
201
202 # delete_dialin(&dialin, &config)
203 sub delete_dialin
204 {
205 local @same = grep { $_->{'line'} == $_[0]->{'line'} && $_ ne $_[0] }
206                    @{$_[1]};
207 if (@same) {
208         &replace_file_line($config{'dialin_config'}, $_[0]->{'line'},
209                            join(" ", map { &dialin_line($_) } @same)."\n");
210         }
211 else {
212         &replace_file_line($config{'dialin_config'}, $_[0]->{'line'});
213         }
214 }
215
216 # modify_dialin(&dialin, &config)
217 sub modify_dialin
218 {
219 local @same = grep { $_->{'line'} == $_[0]->{'line'} } @{$_[1]};
220 &replace_file_line($config{'dialin_config'}, $_[0]->{'line'},
221                    join(" ", map { &dialin_line($_) } @same)."\n");
222 }
223
224 # swap_dialins(&dialin1, &dialin2, &config)
225 sub swap_dialins
226 {
227 local $lref = &read_file_lines($config{'dialin_config'});
228 local @same1 = grep { $_->{'line'} == $_[0]->{'line'} } @{$_[2]};
229 local @same2 = grep { $_->{'line'} == $_[1]->{'line'} } @{$_[2]};
230 local $idx1 = &indexof($_[0], @same1);
231 local $idx2 = &indexof($_[1], @same2);
232 if ($_[0]->{'line'} == $_[1]->{'line'}) {
233         ($same1[$idx1], $same1[$idx2]) = ($same1[$idx2], $same1[$idx1]);
234         &replace_file_line($config{'dialin_config'}, $_[0]->{'line'},
235                            join(" ", map { &dialin_line($_) } @same1)."\n");
236         }
237 else {
238         ($same1[$idx1], $same2[$idx2]) = ($same2[$idx2], $same1[$idx1]);
239         &replace_file_line($config{'dialin_config'}, $_[0]->{'line'},
240                            join(" ", map { &dialin_line($_) } @same1)."\n");
241         &replace_file_line($config{'dialin_config'}, $_[1]->{'line'},
242                            join(" ", map { &dialin_line($_) } @same2)."\n");
243         }
244 }
245
246 # dialin_line(&dialin)
247 sub dialin_line
248 {
249 return ($_[0]->{'not'} ? "!" : "").$_[0]->{'number'};
250 }
251
252 # apply_mgetty()
253 # Apply the current serial port and mgetty configuration, or return an
254 # error message
255 sub apply_mgetty
256 {
257 local %iconfig = &foreign_config("inittab");
258 local $out = &backquote_logged("$iconfig{'telinit'} q 2>&1 </dev/null");
259 if ($?) {
260         return "<tt>$out</tt>";
261         }
262 &kill_byname_logged("mgetty", 'TERM');
263 return undef;
264 }
265
266 1;
267