Handle hostnames with upper-case letters
[webmin.git] / nis / edit_tables.cgi
1 #!/usr/local/bin/perl
2 # edit_tables.cgi
3 # Allow the selection of and display a NIS table for editing
4
5 require './nis-lib.pl';
6 &ui_print_header(undef, $text{'tables_title'}, "");
7 &ReadParse();
8
9 $mode = &get_server_mode();
10 if ($mode == 0 || !(&get_nis_support() & 2)) {
11         print "<p>$text{'tables_emaster'}<p>\n";
12         &ui_print_footer("", $text{'index'});
13         exit;
14         }
15 elsif ($mode == 2) {
16         print "<p>$text{'tables_eslave'}<p>\n";
17         &ui_print_footer("", $text{'index'});
18         exit;
19         }
20
21 @tables = &list_nis_tables();
22 @domains = &unique(map { $_->{'domain'} } @tables);
23 $in{'table'} = int($in{'table'});
24
25 print "<form action=edit_tables.cgi method=post>\n";
26 print "<input type=submit value='$text{'tables_switch'}'>\n";
27 print "<select name=table>\n";
28 foreach $t (@tables) {
29         $t->{'desc'} = $text{"desc_".$t->{'table'}};
30         $t->{'desc'} = $t->{'table'} if (!$t->{'desc'});
31         $t->{'desc'} .= " ($t->{'domain'})" if (@domains > 1);
32         printf "<option value=%d %s>%s\n",
33                 $t->{'index'}, $in{'table'} eq $t->{'index'} ? 'selected' : '',
34                 $t->{'desc'};
35         }
36 print "</select></form>\n";
37
38 $t = $tables[$in{'table'}];
39 $type = $in{'text'} ? undef : $t->{'type'};
40 print "<b>",&text('tables_header', $t->{'desc'},
41                    "<tt>".join(" ", @{$t->{'files'}})."</tt>"),"</b><p>\n";
42 if ($type eq 'hosts') {
43         &show_nis_table([ $text{'hosts_ip'},
44                           $text{'hosts_name'} ],
45                         $t, '\s+', [ 0, 1 ]);
46         }
47 elsif ($type eq 'networks') {
48         &show_nis_table([ $text{'networks_name'},
49                           $text{'networks_ip'} ],
50                         $t, '\s+', [ 0, 1 ]);
51         }
52 elsif ($type eq 'group' || $type eq 'group_shadow') {
53         &show_nis_table([ $text{'group_name'},
54                           $text{'group_gid'},
55                           $text{'group_members'} ],
56                         $t, ':', [ 0, 2, 3 ], "width=100%");
57         }
58 elsif ($type eq 'passwd_shadow' || $type eq 'passwd_shadow_full' ||
59        $type eq 'passwd') {
60         &show_nis_table([ $text{'passwd_name'},
61                           $text{'passwd_uid'},
62                           $text{'passwd_real'},
63                           $text{'passwd_home'},
64                           $text{'passwd_shell'} ],
65                         $t, ':', [ 0, 2, 4, 5, 6 ], "width=100%");
66         }
67 elsif ($type eq 'services') {
68         &show_nis_table([ $text{'services_name'},
69                           $text{'services_proto'},
70                           $text{'services_port'} ],
71                         $t, '[\s/]+', [ 0, 2, 1 ]);
72         }
73 elsif ($type eq 'services2') {
74         &show_nis_table([ $text{'services_name'},
75                           $text{'services_proto'},
76                           $text{'services_port'} ],
77                         $t, '[\s/]+', [ 0, 1, 2 ]);
78         }
79 elsif ($type eq 'protocols') {
80         &show_nis_table([ $text{'protocols_name'},
81                           $text{'protocols_number'},
82                           $text{'protocols_aliases'} ],
83                         $t, '\s+', [ 0, 1, -2 ]);
84         }
85 elsif ($type eq 'netgroup') {
86         &show_nis_table([ $text{'netgroup_name'},
87                           $text{'netgroup_members'} ],
88                         $t, '\s+', [ 0, -1 ]);
89         }
90 elsif ($type eq 'ethers') {
91         &show_nis_table([ $text{'ethers_mac'},
92                           $text{'ethers_ip'} ],
93                         $t, '\s+', [ 0, 1 ]);
94         }
95 elsif ($type eq 'rpc') {
96         &show_nis_table([ $text{'rpc_name'},
97                           $text{'rpc_number'},
98                           $text{'rpc_aliases'} ],
99                         $t, '\s+', [ 0, 1, -2 ]);
100         }
101 elsif ($type eq 'netmasks') {
102         &show_nis_table([ $text{'netmasks_net'},
103                           $text{'netmasks_mask'} ],
104                         $t, '\s+', [ 0, 1 ]);
105         }
106 elsif ($type eq 'aliases') {
107         &show_nis_table([ $text{'aliases_from'},
108                           $text{'aliases_to'} ],
109                         $t, '[\s:]+', [ 0, 1 ]);
110         }
111 else {
112         # Allow editing of file directly
113         print "<form method=post action=save_file.cgi enctype=multipart/form-data>\n";
114         print "<input type=hidden name=table value='$in{'table'}'>\n";
115         $fnum = 0;
116         foreach $f (@{$t->{'files'}}) {
117                 print "<table border>\n";
118                 print "<tr $tb> <td><b>",&text('tables_file', "<tt>$f</tt>"),
119                       "</b></td> </tr>\n";
120                 print "<tr $cb> <td><textarea name=data_$fnum rows=20 cols=80>";
121                 open(FILE, $f);
122                 print <FILE>;
123                 close(FILE);
124                 print "</textarea></td></tr></table><br>\n";
125                 $fnum++;
126                 }
127         print "<input type=submit value='$text{'tables_ok'}'></form>\n";
128         }
129
130 if ($config{'manual_build'}) {
131         print &ui_hr();
132         print "<table width=100%><tr>\n";
133         print "<form action=build.cgi>\n";
134         print "<input type=hidden name=table value='$in{'table'}'>\n";
135         print "<td><input type=submit value='$text{'tables_build'}'></td>\n";
136         print "<td>$text{'tables_buildmsg'}</td>\n";
137         print "</form></tr></table>\n";
138         }
139
140 &ui_print_footer("", $text{'index_return'});
141
142 # show_nis_table(&headers, &table, splitter, &columns, params)
143 sub show_nis_table
144 {
145 local @f = @{$_[1]->{'files'}};
146 local $lines = 0;
147 open(FILE, $f[0]);
148 while(<FILE>) {
149         s/\r|\n//g;
150         s/^\s*#.*$//;
151         push(@lines, $_);
152         $lines++ if (/\S/);
153         $empty = 0 if (/\S/);
154         }
155 close(FILE);
156 if ($config{'max_size'} && $lines > $config{'max_size'}) {
157         # Display a search form
158         print "<form action=edit_tables.cgi>\n";
159         print "<input type=hidden name=table value='$in{'table'}'>\n";
160         local $sel = "<select name=field>\n";
161         local $n = 0;
162         foreach $f (@{$_[0]}) {
163                 $sel .= sprintf "<option value=%s %s>%s\n",
164                         $n, $in{'field'} == $n ? 'selected' : '', $f;
165                 $n++;
166                 }
167         $sel .= "</select>\n";
168         print &text('tables_find', $t->{'desc'}, $sel,
169                     "<input name=what size=15 value='$in{'what'}'>"),
170                     "&nbsp;&nbsp;&nbsp;\n";
171         print "<input type=submit value='$text{'tables_search'}'></form>\n";
172         }
173 if ($lines && (defined($in{'field'}) || !$config{'max_size'} ||
174                $lines <= $config{'max_size'})) {
175         # Show table records
176         print "<a href='edit_$t->{'type'}.cgi?table=$in{'table'}'>",
177               "$text{'tables_add'}</a><br>\n";
178         print "<table border $_[4]>\n";
179         print "<tr $tb> ",(map { "<td><b>$_</b></td>" } @{$_[0]}),"</tr>\n";
180         local ($c, @c) = @{$_[3]};
181         local $lnum = 0;
182         local $matches = 0;
183         foreach $l (@lines) {
184                 local @r = split($_[2], $l);
185                 if ($l =~ /\S/ && (!defined($in{'field'}) ||
186                     $r[$_[3]->[$in{'field'}]] =~ /$in{'what'}/i)) {
187                         print "<tr $cb><td><a href='edit_$t->{'type'}.cgi?",
188                               "line=$lnum&table=$in{'table'}'>",
189                               &html_escape($r[$c]),"</a></td>\n";
190                         foreach $i (@c) {
191                                 if ($i < 0) {
192                                         print "<td>",&html_escape(join(" ", @r[-$i .. $#r])),"<br></td>\n";
193                                         }
194                                 else {
195                                         print "<td>",&html_escape($r[$i]),"<br></td>\n";
196                                         }
197                                 }
198                         $matches++;
199                         }
200                 $lnum++;
201                 }
202         if (!$matches) {
203                 print "<tr $cb> <td colspan=",scalar(@{$_[3]}),">",
204                       "$text{'tables_nomatch'}</td> </tr>\n";
205                 }
206         print "</table>\n";
207         }
208 else {
209         print "<b>",&text('tables_none', $t->{'desc'}),"</b><p>\n";
210         }
211 print "<a href='edit_$t->{'type'}.cgi?table=$in{'table'}'>",
212       "$text{'tables_add'}</a>&nbsp;&nbsp;\n";
213 print "<a href='edit_tables.cgi?table=$in{'table'}&text=1'>",
214       "$text{'tables_text'}</a><p>\n";
215 }
216