Handle hostnames with upper-case letters
[webmin.git] / ldap-useradmin / index.cgi
1 #!/usr/local/bin/perl
2 # index.cgi
3 # List all LDAP users for editing
4
5 require './ldap-useradmin-lib.pl';
6 &ui_print_header(undef, $module_info{'desc'}, "", "intro", 1, 1);
7 &useradmin::load_theme_library();       # So that ui functions work
8 &ReadParse();
9
10 # Make sure the LDAP Perl module is installed, and if not offer to install
11 if (!$got_net_ldap) {
12         local @needs;
13         foreach $m ("Convert::ASN1", "Net::LDAP") {
14                 eval "use $m";
15                 push(@needs, $m) if ($@);
16                 }
17         $missing = &urlize(join(" ", @needs));
18         print &text('index_eperl', "<tt>$missing</tt>",
19                     "/cpan/download.cgi?source=3&cpan=$missing&mode=2&".
20                     "return=/$module_name/&returndesc=".
21                     &urlize($text{'index_return'})),"<p>\n";
22         print "$text{'index_eperl2'}\n";
23         print "<pre>$net_ldap_error</pre>\n";
24         &ui_print_footer("/", $text{'index'});
25         exit;
26         }
27
28 # Try to login .. may fail
29 $ldap = &ldap_connect(1);
30 if (!ref($ldap)) {
31         print &text('index_eldap', $ldap,
32                     "$gconfig{'webprefix'}/config.cgi?$module_name"),"<p>\n";
33         &ui_print_footer("/", $text{'index'});
34         exit;
35         }
36
37 # Make sure we can get the schema
38 $schema = $ldap->schema();
39 if (!$schema) {
40         print &text('index_eschema', '../ldap-server/'),"<p>\n";
41         &ui_print_footer("/", $text{'index'});
42         exit;
43         }
44
45 # Make sure the LDAP bases are set or available
46 if (!&get_user_base() || !&get_group_base()) {
47         print &text('index_ebase',
48                     "$gconfig{'webprefix'}/config.cgi?$module_name"),"<p>\n";
49         &ui_print_footer("/", $text{'index'});
50         exit;
51         }
52
53 if ($config{'imap_host'}) {
54         # Make sure the IMAP Perl module is installed, and if not offer
55         # to install
56         if (!$got_net_imap) {
57                 print &text('index_eperl', "<tt>Net::IMAP</tt>",
58                           "/cpan/download.cgi?source=3&cpan=Net::IMAP&mode=2&".
59                           "return=/$module_name/&returndesc=".
60                           &urlize($text{'index_return'})),"<p>\n";
61                 print "$text{'index_eperl2'}\n";
62                 print "<pre>$net_imap_error</pre>\n";
63                 &ui_print_footer("/", $text{'index'});
64                 exit;
65                 }
66
67         # Try to connect to the IMAP server
68         $imap = &imap_connect(1);
69         if (!ref($imap)) {
70                 print &text('index_eimap', $imap,
71                     "$gconfig{'webprefix'}/config.cgi?$module_name"),"<p>\n";
72                 &ui_print_footer("/", $text{'index'});
73                 exit;
74                 }
75         }
76
77 # If using MD5, make sure needed perl modules or programs are installed
78 if ($config{'md5'} == 1) {
79         # Check if MD5 perl module is installed, and offer to install
80         &foreign_require("useradmin", "user-lib.pl");
81         if ($err = &useradmin::check_md5()) {
82                 print &text('index_emd5',
83                             "../config.cgi?$module_name",
84                             "<tt>$err</tt>",
85                             "../cpan/download.cgi?source=3&cpan=Digest::MD5&mode=2&return=/$module_name/&returndesc=".&urlize($text{'index_return'})),"<p>\n";
86                 &ui_print_footer("/", $text{'index'});
87                 exit;
88                 }
89         }
90 elsif ($config{'md5'} == 3 || $config{'md5'} == 4) {
91         # Check if slappasswd is installed
92         if (!&has_command($config{'slappasswd'})) {
93                 print &text('index_eslappasswd',
94                             "../config.cgi?$module_name",
95                             "<tt>$config{'slappasswd'}</tt>"),"<p>\n";
96                 &ui_print_footer("/", $text{'index'});
97                 exit;
98                 }
99         }
100
101 # Count the number of users and groups
102 $base = &get_user_base();
103 $rv = $ldap->search(base => $base,
104                     filter => &user_filter(),
105                     sizelimit => $mconfig{'display_max'}+1);
106 $ucount = $rv->count;
107 $base = &get_group_base();
108 $rv = $ldap->search(base => $base,
109                     filter => &group_filter(),
110                     sizelimit => $mconfig{'display_max'}+1);
111 $gcount = $rv->count;
112
113 # Get the list of users and groups
114 if ($ucount <= $mconfig{'display_max'}) {
115         @allulist = &list_users();
116         @ulist = &useradmin::list_allowed_users(\%access, \@allulist);
117         }
118 if ($gcount <= $mconfig{'display_max'}) {
119         @allglist = &list_groups();
120         @glist = &useradmin::list_allowed_groups(\%access, \@allglist);
121         }
122
123 # Start of tabs, based on what can be edited
124 @tabs = ( );
125 if ($ucount || $access{'ucreate'}) {
126         push(@tabs, [ "users", $text{'index_users'},
127                       "index.cgi?mode=users" ]);
128         $can_users = 1;
129         }
130 if ($gcount || $access{'gcreate'}) {
131         push(@tabs, [ "groups", $text{'index_groups'},
132                       "index.cgi?mode=groups" ]);
133         $can_groups = 1;
134         }
135 print &ui_tabs_start(\@tabs, "mode", $in{'mode'} || $tabs[0]->[0], 1);
136
137 # Start of users tab
138 if ($can_users) {
139         print &ui_tabs_start_tab("mode", "users");
140         }
141
142 # Build links for adding users
143 @links = ( );
144 if ($access{'ucreate'}) {
145         push(@links,
146              "<a href='edit_user.cgi?new=1'>$text{'index_uadd'}</a>");
147         }
148 if ($access{'batch'}) {
149         push(@links, 
150              "<a href=\"batch_form.cgi\">$text{'index_batch'}</a>");
151         }
152
153 $form = 0;
154 if ($ucount > $mconfig{'display_max'}) {
155         # Show user search form
156         print "<b>$text{'index_toomany'}</b><p>\n";
157         print &ui_form_start("search_user.cgi");
158         print &ui_table_start($text{'index_usheader'}, undef, 2);
159
160         # Field to search
161         print &ui_table_row($text{'index_find'},
162                 &ui_select("field", "uid",
163                            [ [ "uid", $text{'user'} ],
164                              [ "cn", $text{'real'} ],
165                              [ "loginShell", $text{'shell'} ],
166                              [ "homeDirectory", $text{'home'} ],
167                              [ "uidNumber", $text{'uid'} ],
168                              [ "gidNumber", $text{'gid'} ] ])." ".
169                 &ui_select("match", 1, $match_modes));
170
171         # Text
172         print &ui_table_row($text{'index_ftext'},
173                 &ui_textbox("what", undef, 50));
174
175         print &ui_table_end();
176         print &ui_form_end([ [ undef, $text{'find'} ] ]);
177         $formno++;
178         print &ui_links_row(\@links);
179         }
180 elsif (@ulist) {
181         # Show table of all users
182         @ulist = &useradmin::sort_users(\@ulist, $mconfig{'sort_mode'});
183         @left = grep { !/batch_form|export_form/ } @links;
184         @right = grep { /batch_form|export_form/ } @links;
185         &useradmin::users_table(\@ulist, $form++, 1, 0, \@left, \@right);
186         }
187 elsif ($access{'ucreate'}) {
188         # No users
189         $base = &get_user_base();
190         print "<b>",&text('index_unone', "<tt>$base</tt>"),"</b><p>\n";
191         print &ui_links_row(\@links);
192         }
193
194 # End of users tab
195 if ($can_users) {
196         print &ui_tabs_end_tab("mode", "users");
197         }
198
199 # Start of groups tab
200 if ($can_groups) {
201         print &ui_tabs_start_tab("mode", "groups");
202         }
203
204 # Create group links
205 @links = ( );
206 if ($access{'gcreate'}) {
207         push(@links, "<a href='edit_group.cgi?new=1'>$text{'index_gadd'}</a>");
208         }
209
210 if ($gcount > $mconfig{'display_max'}) {
211         # Show group search form
212         print "<b>$text{'index_gtoomany'}</b><p>\n";
213         print &ui_form_start("search_group.cgi");
214         print &ui_table_start($text{'index_gsheader'}, undef, 2);
215
216         # Field to search
217         print &ui_table_row($text{'index_gfind'},
218                 &ui_select("field", "cn",
219                            [ [ "cn", $text{'gedit_group'} ],
220                              [ "memberUid", $text{'gedit_members'} ],
221                              [ "gidNumber", $text{'gedit_gid'} ] ])." ".
222                 &ui_select("match", 1, $match_modes));
223
224         # Text
225         print &ui_table_row($text{'index_ftext'},
226                 &ui_textbox("what", undef, 50));
227
228         print &ui_table_end();
229         print &ui_form_end([ [ undef, $text{'find'} ] ]);
230         print &ui_links_row(\@links);
231         }
232 elsif (@glist) {
233         # Show table of all groups
234         @glist = &useradmin::sort_groups(\@glist, $mconfig{'sort_mode'});
235         &useradmin::groups_table(\@glist, $form++, 0, \@links);
236         }
237 elsif ($access{'gcreate'} || !@allglist) {
238         # Show none message
239         $base = &get_group_base();
240         print "<b>",&text('index_gnone', "<tt>$base</tt>"),"</b><p>\n";
241         print &ui_links_row(\@links);
242         }
243
244 # End of groups tab
245 if ($can_groups) {
246         print &ui_tabs_end_tab("mode", "groups");
247         }
248 print &ui_tabs_end(1);
249
250 &ui_print_footer("/", $text{'index'});
251