Handle hostnames with upper-case letters
[webmin.git] / acl / convert.cgi
1 #!/usr/local/bin/perl
2 # convert.cgi
3 # Convert unix to webmin users
4
5 require './acl-lib.pl';
6 &ReadParse();
7 &error_setup($text{'convert_err'});
8 $access{'sync'} && $access{'create'} || &error($text{'convert_ecannot'});
9 &foreign_require("useradmin", "user-lib.pl");
10
11 # Validate inputs
12 if ($access{'gassign'} ne '*') {
13         @gcan = split(/\s+/, $access{'gassign'});
14         &indexof($in{'wgroup'}, @gcan) >= 0 ||
15                 &error($text{'convert_ewgroup2'});
16         }
17 if ($in{'conv'} == 1) {
18         $in{'users'} =~ /\S/ || &error($text{'convert_eusers'});
19         map { $users{$_}++ } split(/\s+/, $in{'users'});
20         }
21 elsif ($in{'conv'} == 2) {
22         map { $nusers{$_}++ } split(/\s+/, $in{'nusers'});
23         }
24 elsif ($in{'conv'} == 3) {
25         $gid = getgrnam($in{'group'});
26         defined($gid) || &error($text{'convert_egroup'});
27         }
28 elsif ($in{'conv'} == 4) {
29         $in{'min'} =~ /^\d+$/ || &error($text{'convert_emin'});
30         $in{'max'} =~ /^\d+$/ || &error($text{'convert_emax'});
31         }
32
33 # Get the group to add to
34 foreach $g (&list_groups()) {
35         $group = $g if ($g->{'name'} eq $in{'wgroup'});
36         $exists{$g->{'name'}}++;
37         }
38 $group || &error($text{'convert_ewgroup'});
39
40 if ($in{'conv'} == 3) {
41         # Find secondary members of group
42         @ginfo = getgrnam($in{'group'});
43         @members = split(/\s+/, $ginfo[3]);
44         }
45
46 # Convert matching users
47 &ui_print_header(undef, $text{'convert_title'}, "");
48 print &ui_subheading($text{'convert_msg'});
49 print "<table border width=100%><tr><td bgcolor=#c0c0c0><pre>\n";
50 map { $exists{$_->{'name'}}++ } &list_users();
51 foreach $u (&foreign_call("useradmin", "list_users")) {
52         local $ok;
53         if ($in{'conv'} == 0) {
54                 $ok = 1;
55                 }
56         elsif ($in{'conv'} == 1) {
57                 $ok = $users{$u->{'user'}};
58                 }
59         elsif ($in{'conv'} == 2) {
60                 $ok = !$nusers{$u->{'user'}};
61                 }
62         elsif ($in{'conv'} == 3) {
63                 $ok = $u->{'gid'} == $gid ||
64                       &indexof($u->{'user'}, @members) >= 0;
65                 }
66         elsif ($in{'conv'} == 4) {
67                 $ok = $u->{'uid'} >= $in{'min'} &&
68                       $u->{'uid'} <= $in{'max'};
69                 }
70         if (!$ok) {
71                 print &text('convert_skip', $u->{'user'}),"\n";
72                 }
73         elsif ($exists{$u->{'user'}}) {
74                 print "<i>",&text('convert_exists', $u->{'user'}),"</i>\n";
75                 }
76         elsif ($u->{'user'} !~ /^[A-z0-9\-\_\.]+$/) {
77                 print "<i>",&text('convert_invalid', $u->{'user'}),"</i>\n";
78                 }
79         else {
80                 # Actually add the user
81                 print "<b>",&text('convert_added', $u->{'user'}),"</b>\n";
82                 local $user = { 'name' => $u->{'user'},
83                                 'pass' => $in{'sync'} ? 'x' : $u->{'pass'},
84                                 'modules' => $group->{'modules'} };
85                 &create_user($user);
86                 foreach $m (@{$group->{'modules'}}, "") {
87                         local %groupacl;
88                         if (&read_file("$config_directory/$m/$in{'wgroup'}.gacl",
89                                        \%groupacl)) {
90                                 &write_file(
91                                         "$config_directory/$m/$u->{'user'}.acl",
92                                         \%groupacl);
93                                 }
94                         }
95
96                 push(@{$group->{'members'}}, $u->{'user'});
97                 $exists{$u->{'user'}}++;
98                 }
99         }
100 endpwent() if ($gconfig{'os_type'} ne 'hpux');
101
102 # Finish off
103 &modify_group($group->{'name'}, $group);
104 &restart_miniserv();
105
106 print "</pre></td></tr></table><br>\n";
107 &ui_print_footer("", $text{'index_return'});
108