Handle hostnames with upper-case letters
[webmin.git] / acl / index.cgi
1 #!/usr/local/bin/perl
2 # index.cgi
3 # List all webmin users
4
5 require './acl-lib.pl';
6 &ui_print_header(undef, $text{'index_title'}, "", undef, 1, 1);
7
8 # Fetch user and group lists, if possible
9 eval {
10         $main::error_must_die = 1;
11         @glist = &list_groups();
12         foreach $g (@glist) {
13                 foreach $gm (@{$g->{'members'}}) {
14                         $ingroup{$gm} = $g;
15                         }
16                 }
17         @ulist = &list_users();
18         };
19 if ($@) {
20         print "<b>",&text('index_eulist', "$@"),"</b><p>\n";
21         }
22
23 foreach $u (@ulist) {
24         $me = $u if ($u->{'name'} eq $base_remote_user);
25         }
26 @mcan = $access{'mode'} == 1 ? @{$me->{'modules'}} :
27         $access{'mode'} == 2 ? split(/\s+/, $access{'mods'}) :
28                                &list_modules();
29 map { $mcan{$_}++ } @mcan;
30
31 if ($config{'order'}) {
32         @ulist = sort { $a->{'name'} cmp $b->{'name'} } @ulist;
33         @glist = sort { $a->{'name'} cmp $b->{'name'} } @glist;
34         }
35
36 foreach $m (&list_module_infos()) {
37         $modname{$m->{'dir'}} = $m->{'desc'};
38         }
39 @canulist = grep { &can_edit_user($_->{'name'}, \@glist) } @ulist;
40 if (!@canulist) {
41         # If no users, only show section heading if can create
42         if ($access{'create'}) {
43                 print &ui_subheading($text{'index_users'})
44                         if (!$config{'display'});
45                 print "<b>$text{'index_nousers'}</b><p>\n";
46                 print "<a href=edit_user.cgi>$text{'index_create'}</a>\n";
47                 $shown_users = 1;
48                 }
49         }
50 elsif ($config{'display'}) {
51         # Show as table of names
52         print &ui_subheading($text{'index_users'});
53         print &ui_form_start("delete_users.cgi", "post");
54         &show_name_table(\@canulist, "edit_user.cgi",
55                          $access{'create'} ? $text{'index_create'} : undef,
56                          $text{'index_users'}, "user");
57         print &ui_form_end([ [ "delete", $text{'index_delete'} ] ]);
58         $shown_users = 1;
59         $form++;
60         }
61 else {
62         # Show usernames and modules
63         print &ui_subheading($text{'index_users'});
64         @rowlinks = ( );
65         if (!$config{'select'}) {
66                 print &ui_form_start("delete_users.cgi", "post");
67                 push(@rowlinks, &select_all_link("d", $form),
68                              &select_invert_link("d", $form));
69                 }
70         push(@rowlinks, "<a href=edit_user.cgi>$text{'index_create'}</a>")
71                 if ($access{'create'});
72         print &ui_links_row(\@rowlinks);
73
74         print &ui_columns_start([ $text{'index_user'},
75                                   $text{'index_modules'} ], 100);
76         foreach $u (@canulist) {
77                 if ($ingroup{$u->{'name'}}) {
78                         # Is a member of a group
79                         $smods = &show_modules(
80                               "user", $u->{'name'}, $u->{'ownmods'}, 0,
81                               &text('index_modgroups',
82                                   "<tt>$ingroup{$u->{'name'}}->{'name'}</tt>"));
83                                       
84                         }
85                 else {
86                         # Is a stand-alone user
87                         $smods = &show_modules(
88                               "user", $u->{'name'}, $u->{'modules'}, 1);
89                         }
90                 print &ui_columns_row([ &user_link($u, "edit_user.cgi", "user"),
91                                         $smods ], [ "valign=top" ]);
92                 }
93         print &ui_columns_end();
94         print &ui_links_row(\@rowlinks);
95         if (!$config{'select'}) {
96                 print &ui_form_end([ [ "delete", $text{'index_delete'} ] ]);
97                 }
98         $shown_users = 1;
99         $form++;
100         }
101 print "<p>\n";
102
103 if ($shown_users && $access{'groups'}) {
104         print &ui_hr();
105         }
106
107 if ($access{'groups'}) {
108         print &ui_subheading($text{'index_groups'});
109         if (!@glist) {
110                 # No groups, so just show create link
111                 print "<b>$text{'index_nogroups'}</b><p>\n";
112                 print "<a href=edit_group.cgi>$text{'index_gcreate'}</a><p>\n";
113                 }
114         elsif ($config{'display'}) {
115                 # Show just group names
116                 print &ui_form_start("delete_groups.cgi", "post");
117                 &show_name_table(\@glist, "edit_group.cgi",
118                                  $text{'index_gcreate'},
119                                  $text{'index_groups'}, "group");
120                 print &ui_form_end([ [ "delete", $text{'index_delete'} ] ]);
121                 $form++;
122                 }
123         else {
124                 # Show table of groups
125                 @rowlinks = ( );
126                 if (!$config{'select'}) {
127                         print &ui_form_start("delete_groups.cgi", "post");
128                         push(@rowlinks, &select_all_link("d", $form),
129                                      &select_invert_link("d", $form));
130                         }
131                 push(@rowlinks,
132                      "<a href=edit_group.cgi>$text{'index_gcreate'}</a>");
133                 print &ui_links_row(\@rowlinks);
134
135                 print &ui_columns_start([ $text{'index_group'},
136                                           $text{'index_members'},
137                                           $text{'index_modules'} ], 100);
138                 foreach $g (@glist) {
139                         local @cols;
140                         push(@cols, &user_link($g,"edit_group.cgi","group"));
141                         push(@cols, join(" ", @{$g->{'members'}}));
142                         if ($ingroup{'@'.$g->{'name'}}) {
143                                 # Is a member of some other group
144                                 push(@cols, &show_modules("group", $g->{'name'},
145                                     $g->{'ownmods'},0,
146                                     &text('index_modgroups',
147                                           "<tt>$ingroup{$g->{'name'}}->{'name'}</tt>")));
148                                 }
149                         else {
150                                 # Is a top-level group
151                                 push(@cols, &show_modules("group", $g->{'name'},
152                                               $g->{'modules'}, 1));
153                                 }
154                         print &ui_columns_row(\@cols);
155                         }
156                 print &ui_columns_end();
157                 print &ui_links_row(\@rowlinks);
158                 if (!$config{'select'}) {
159                         print &ui_form_end([ [ "delete", $text{'index_delete'} ] ]);
160                         }
161                 $form++;
162                 }
163         }
164
165 &get_miniserv_config(\%miniserv);
166 if ($access{'sync'} && &foreign_check("useradmin")) {
167         push(@icons, "images/convert.gif");
168         push(@links, "convert_form.cgi");
169         push(@titles, $text{'index_convert'});
170         }
171 if ($access{'sync'} && $access{'create'} && $access{'delete'}) {
172         push(@icons, "images/sync.gif");
173         push(@links, "edit_sync.cgi");
174         push(@titles, $text{'index_sync'});
175         }
176 if ($access{'unix'} && $access{'create'} && $access{'delete'}) {
177         push(@icons, "images/unix.gif");
178         push(@links, "edit_unix.cgi");
179         push(@titles, $text{'index_unix'});
180         }
181 if ($access{'sessions'} && $miniserv{'session'}) {
182         push(@icons, "images/sessions.gif");
183         push(@links, "list_sessions.cgi");
184         push(@titles, $text{'index_sessions'});
185         }
186 if (uc($ENV{'HTTPS'}) eq "ON" && $miniserv{'ca'}) {
187         push(@icons, "images/cert.gif");
188         push(@links, "cert_form.cgi");
189         push(@titles, $text{'index_cert'});
190         }
191 if ($access{'rbacenable'}) {
192         push(@icons, "images/rbac.gif");
193         push(@links, "edit_rbac.cgi");
194         push(@titles, $text{'index_rbac'});
195         }
196 if ($access{'pass'}) {
197         push(@icons, "images/pass.gif");
198         push(@links, "edit_pass.cgi");
199         push(@titles, $text{'pass_title'});
200         }
201 if ($access{'sql'}) {
202         push(@icons, "images/sql.gif");
203         push(@links, "edit_sql.cgi");
204         push(@titles, $text{'sql_title'});
205         }
206
207 if (@icons) {
208         print &ui_hr();
209         &icons_table(\@links, \@titles, \@icons);
210         }
211
212 &ui_print_footer("/", $text{'index'});
213
214 # show_modules(type, who, &mods, show-global, prefix)
215 sub show_modules
216 {
217 local ($type, $who, $mods, $global, $prefix) = @_;
218 local $rv;
219 if ($config{'select'}) {
220         # Show as drop-down menu
221         $rv .= &ui_form_start("edit_acl.cgi");
222         $rv .= $prefix."<br>\n" if ($prefix);
223         if (@$mods) {
224                 $rv .= &ui_hidden($type, $who);
225                 if ($access{'acl'}) {
226                         $rv .= &ui_submit($text{'index_edit'});
227                         }
228                 local @opts;
229                 foreach my $m (sort { $modname{$a} cmp $modname{$b} } @$mods) {
230                         if ($modname{$m}) {
231                                 push(@opts, [ $m, $modname{$m} ]);
232                                 }
233                         }
234                 $rv .= &ui_select("mod", undef, \@opts);
235                 }
236         $rv .= &ui_form_end();
237         }
238 else {
239         # Show as table
240         $rv .= $prefix."<br>\n" if ($prefix);
241         local @grid;
242         foreach my $m (sort { $modname{$a} cmp $modname{$b} } @$mods) {
243                 if ($modname{$m}) {
244                         if ($mcan{$m} && $access{'acl'}) {
245                                 push(@grid, "<a href='edit_acl.cgi?mod=".
246                                       &urlize($m)."&$type=".&urlize($who).
247                                       "'>$modname{$m}</a>");
248                                 }
249                         else {
250                                 push(@grid, $modname{$m});
251                                 }
252                         }
253                 }
254         $rv .= &ui_grid_table(\@grid, 3, 100,
255                 [ "width=33%", "width=33%", "width=33%" ]);
256         }
257 return $rv;
258 }
259
260 # show_name_table(&users|&groups, cgi, create-text, header-text, param)
261 sub show_name_table
262 {
263 # Show table of users, and maybe create links
264 local @rowlinks = ( &select_all_link("d", $form),
265                     &select_invert_link("d", $form) );
266 push(@rowlinks, "<a href=$_[1]>$_[2]</a>") if ($_[2]);
267 print &ui_links_row(\@rowlinks);
268 local @links;
269 for(my $i=0; $i<@{$_[0]}; $i++) {
270         push(@links, &user_link($_[0]->[$i], $_[1], $_[4]));
271         }
272 print &ui_grid_table(\@links, 4, 100,
273         [ "width=25%", "width=25%", "width=25%", "width=25%" ], undef, $_[3]);
274 print &ui_links_row(\@rowlinks);
275 }
276
277 # user_link(user, cgi, param)
278 sub user_link
279 {
280 local $lck = $_[0]->{'pass'} =~ /^\!/ ? 1 : 0;
281 local $ro = $_[0]->{'readonly'};
282 return ($config{'select'} ? "" : &ui_checkbox("d", $_[0]->{'name'}, "", 0)).
283        ($lck ? "<i>" : "").
284        ($ro ? "<b>" : "").
285        "<a href='$_[1]?$_[2]=".&urlize($_[0]->{'name'})."'>".
286         $_[0]->{'name'}."</a>".
287        ($ro ? "</b>" : "").
288        ($lck ? "</i>" : "");
289 }
290