Completed batch group modification:
authorJamie Cameron <jcameron@webmin.com>
Sun, 14 Dec 2008 06:38:12 +0000 (06:38 +0000)
committerJamie Cameron <jcameron@webmin.com>
Sun, 14 Dec 2008 06:38:12 +0000 (06:38 +0000)
useradmin/gbatch_exec.cgi
useradmin/gbatch_form.cgi
useradmin/lang/en
useradmin/save_group.cgi
useradmin/user-lib.pl

index 76ea4bc..4716b53 100755 (executable)
@@ -206,180 +206,96 @@ foreach $line (split(/[\r\n]+/, $data)) {
                $deleted++;
                }
        elsif ($line[0] eq 'modify') {
-               # Modifying an existing user
-               local $wlen = $pft == 5 ? 11 :
-                             $pft == 4 ? 13 :
-                             $pft == 2 ? 14 :
-                             $pft == 1 || $pft == 6 ? 12 : 9;
-               if (@line != $wlen) {
-                       print &text('batch_elen', $lnum, $wlen),"\n";
+               # Modifying an existing group
+               if (@line != 6) {
+                       print &text('batch_elen', $lnum, 6),"\n";
                        next;
                        }
-               local @ulist = &list_users();
-               local ($user) = grep { $_->{'user'} eq $line[1] } @ulist;
-               if (!$user) {
-                       print &text('batch_enouser', $lnum, $line[1]),"\n";
+               local @glist = &list_groups();
+               local ($group) = grep { $_->{'group'} eq $line[1] } @glist;
+               if (!$group) {
+                       print &text('gbatch_enogroup', $lnum, $line[1]),"\n";
                        next;
                        }
-               %olduser = %user = %$user;
+               %oldgroup = %group = %$group;
                $user{'olduser'} = $user->{'user'};
-               if (!&can_edit_user(\%access, \%user)) {
-                       print &text('batch_emaccess', $lnum,
-                                   $text{'usave_eedit'}),"\n";
+               if (!&can_edit_group(\%access, \%group)) {
+                       print &text('gbatch_emaccess', $lnum,
+                                   $text{'gsave_eedit'}),"\n";
                        next;
                        }
 
                # Update supplied fields
                if ($line[2] ne '') {
-                       if (!$access{'urename'}) {
-                               print &text('batch_erename', $lnum, $line[1]),"\n";
+                       if (!$access{'grename'}) {
+                               print &text('gbatch_erename',
+                                           $lnum, $line[1]),"\n";
                                }
-                       $user{'user'} = $line[2];
-                       }
-               if ($in{'crypt'} && $line[3] ne '') {
-                       # Changing to pre-encrypted password
-                       $user{'pass'} = $line[3];
-                       $user{'passmode'} = 2;
-                       }
-               elsif ($line[3] eq 'x') {
-                       # No login allowed
-                       $user{'pass'} = $config{'lock_string'};
-                       $user{'passmode'} = 1;
+                       $group{'group'} = $line[2];
                        }
-               elsif ($line[3] ne '') {
-                       # Normal password
-                       $user{'pass'} = &encrypt_password($line[3]);
-                       $user{'passmode'} = 3;
-                       $user{'plainpass'} = $line[3];
+               if ($line[3] ne '') {
+                       # New normal password
+                       $group{'pass'} = &encrypt_password($line[3]);
+                       $group{'passmode'} = 3;
+                       $group{'plainpass'} = $line[3];
                        }
                else {
                        # No change
-                       $user{'passmode'} = 4;
+                       $group{'passmode'} = 4;
                        }
-               $user{'uid'} = $line[4] if ($line[4] ne '');
-               $user{'gid'} = $line[5] if ($line[5] ne '');
-               $user{'real'} = $line[6] if ($line[6] ne '');
-               $user{'home'} = $line[7] if ($line[7] ne '');
-               $user{'shell'} = $line[8] if ($line[8] ne '');
-               if ($access{'peopt'}) {
-                       if ($pft == 5) {
-                               # Openserver password and short shadow
-                               $user{'min'}=$line[9] if ($line[9] ne '');
-                               $user{'max'}=$line[10] if ($line[10] ne '');
-                               $user{'change'}=int(time() / (60*60*24))
-                                       if ($line[3] ne '');
-                               }
-                       elsif ($pft == 4) {
-                               # AIX password and security information
-                               $user{'min'}=$line[9] if ($line[9] ne '');
-                               $user{'max'}=$line[10] if ($line[10] ne '');
-                               $user{'expire'}=$line[11] if ($line[11] ne '');
-                               if ($line[12] ne '') {
-                                       delete($user{'admin'});
-                                       delete($user{'admchg'});
-                                       delete($user{'nocheck'});
-                                       map { $user{$_}++ }
-                                           split(/\s+/, $line[12]);
-                                       }
-                               $user{'change'}=time() if ($line[3] ne '');
-                               }
-                       elsif ($pft == 2) {
-                               # SYSV-style passwd and shadow information
-                               $user{'min'}=$line[9] if ($line[9] ne '');
-                               $user{'max'}=$line[10] if ($line[10] ne '');
-                               $user{'warn'}=$line[11] if ($line[11] ne '');
-                               $user{'inactive'}=$line[12]
-                                       if ($line[12] ne '');
-                               $user{'expire'}=$line[13] if ($line[13] ne '');
-                               $user{'change'}=int(time() / (60*60*24))
-                                       if ($line[3] ne '');
-                               }
-                       elsif ($pft == 1 || $pft == 6) {
-                               # BSD master.passwd information
-                               $user{'class'}=$line[9] if ($line[9] ne '');
-                               $user{'change'}=$line[10] if ($line[10] ne '');
-                               $user{'expire'}=$line[11] if ($line[11] ne '');
-                               }
+               $group{'gid'} = $line[4] if ($line[4] ne '');
+               if ($line[5] =~ /^\s+$/ || $line[5] eq 'NONE') {
+                       # No members
+                       $group{'members'} = '';
+                       }
+               elsif ($line[5]) {
+                       $group{'members'} = $line[5];
                        }
 
                # Check access control restrictions
-               local $ch = &check_user(\%user, \%olduser);
+               local $ch = &check_group(\%group, \%oldgroup);
                if ($ch) {
-                       print &text('batch_emaccess', $lnum, $ch),"\n";
+                       print &text('gbatch_emaccess', $lnum, $ch),"\n";
                        next;
                        }
 
                # Run the before command
-               &set_user_envs(\%user, 'MODIFY_USER', $user{'plainpass'},
-                              [ &secondary_groups($user{'user'}) ]);
+               &set_user_envs(\%group, 'MODIFY_GROUP');
                $merr = &making_changes();
                &error(&text('usave_emaking', "<tt>$merr</tt>"))
                        if (defined($merr));
 
-               # Move home directory if needed
-               if ($olduser{'home'} ne $user{'home'} && $in{'movehome'} &&
-                   $user{'home'} ne '/' && $olduser{'home'} ne '/') {
-                       if (-d $olduser{'home'} && !-e $user{'home'}) {
-                               local $out = &backquote_logged(
-                                       "mv \"$olduser{'home'}\" ".
-                                       "\"$user{'home'}\" 2>&1");
-                               if ($?) { &error(&text('batch_emove',
-                                                $lnum, $out)); }
-                               }
-                       }
-
-               # Change UIDs and GIDs
-               if ($olduser{'gid'} != $user{'gid'} && $in{'chgid'}) {
+               # Change GIDs
+               if ($oldgroup{'gid'} != $group{'gid'} && $in{'chgid'}) {
                        if ($in{'chgid'} == 1) {
-                               &recursive_change($user{'home'},$olduser{'uid'},
-                                         $olduser{'gid'}, -1, $user{'gid'});
-                               }
-                       else {
-                               &recursive_change("/", $olduser{'uid'},
-                                         $olduser{'gid'}, -1, $user{'gid'});
-                               }
-                       }
-               if ($olduser{'uid'} != $user{'uid'} && $in{'chuid'}) {
-                       if ($in{'chuid'} == 1) {
-                               &recursive_change($user{'home'},$olduser{'uid'},
-                                                 -1, $user{'uid'}, -1);
+                               # Do all the home directories of members
+                               &change_all_home_groups(
+                                       $oldgroup{'gid'}, $group{'gid'},
+                                       [ split(/,/, $group{'members'}) ]);
                                }
                        else {
-                               &recursive_change("/", $olduser{'uid'},
-                                                 -1, $user{'uid'}, -1);
-                               }
-                       }
-
-               # Actually modify the user
-               &modify_user(\%olduser, \%user);
-
-               # If the user has been renamed, update any secondary groups
-               if ($olduser{'user'} ne $user{'user'}) {
-                       foreach $group (@glist) {
-                               local @mems = split(/,/, $group->{'members'});
-                               local $idx = &indexof($olduser{'user'}, @mems);
-                               if ($idx >= 0) {
-                                       $mems[$idx] = $user{'user'};
-                                       $group->{'members'} = join(",", @mems);
-                                       &modify_group($group, $group);
-                                       }
+                               # Do all files in this group from the root dir
+                               &recursive_change("/", -1, $oldgroup{'gid'},
+                                                      -1, $group{'gid'});
                                }
                        }
 
+               # Actually modify the group
+               &modify_group(\%oldgroup, \%group);
                &made_changes();
 
                # Modify in other modules, ignoring errors
                $error_must_die = 1;
                eval {
-                       &other_modules("useradmin_modify_user",
-                                      \%user, \%olduser)
+                       &other_modules("groupadmin_modify_group",
+                                      \%group, \%oldgroup)
                                if ($access{'mothers'} == 1 && $in{'others'} ||
                                    $access{'mothers'} == 0);
                        };
                $error_must_die = 0;
                $other_err = $@;
 
-               print "<b>",&text('batch_modified',$olduser{'user'}),"</b>\n";
+               print "<b>",&text('batch_modified',$oldgroup{'group'}),"</b>\n";
                print "<b><i>",&text('batch_eother', $other_err),"</i></b>\n"
                        if ($other_err);
                $modified++;
index 572d9bd..7ff4aed 100755 (executable)
@@ -39,7 +39,7 @@ print &ui_table_row($text{'gbatch_batch'},
 if ($access{'chgid'}) {
        # Update GIDs on files
        print &ui_table_row($text{'gbatch_chgid'},
-               &ui_radio("chgid", 1, [ [ 0, $text{'no'} ],
+               &ui_radio("chgid", 0, [ [ 0, $text{'no'} ],
                                        [ 1, $text{'home'} ],
                                        [ 2, $text{'uedit_allfiles'} ] ]));
        }
index deb1869..50236c9 100644 (file)
@@ -519,3 +519,4 @@ gbatch_enogroup=Group does not exist at line $1 : $2
 gbatch_eprimary=Group at line $1 cannot be deleted, as it is the primary group of user $2.
 gbatch_egroup=Duplicate group name at line $1 : $2
 gbatch_egroupname=Invalid group name at line $1
+gbatch_erename=You are not allowed to rename groups at line $1 : $2
index 4fa9360..e27dc8c 100755 (executable)
@@ -117,16 +117,8 @@ if (%ogroup) {
                # Change GID on files if needed
                if ($in{'chgid'} == 1) {
                        # Do all the home directories of users in this group
-                       &my_setpwent();
-                       while(@tmp = &my_getpwent()) {
-                               if ($tmp[3] == $ogroup{'gid'} ||
-                                   &indexof($tmp[0], @mems) >= 0) {
-                                       &recursive_change(
-                                               $tmp[7], -1, $ogroup{'gid'},
-                                                        -1, $group{'gid'});
-                                       }
-                               }
-                       &my_endpwent();
+                       &change_all_home_groups($ogroup{'gid'}, $group{'gid'},
+                                               \@mems);
                        }
                else {
                        # Do all files in this group from the root dir
index 0817930..6caab9a 100644 (file)
@@ -2210,4 +2210,19 @@ sub supports_temporary_disable
 return &passfiles_type() != 7;    # Not on OSX, which has a fixed-size hash
 }
 
+# change_all_home_groups(old-gid, new-gid, &members)
+# Change the GID on all files in the home directories of users whose GID is the
+# old GID.
+sub change_all_home_groups
+{
+local ($oldgid, $gid, $mems) = @_;
+&my_setpwent();
+while(my @uinfo = &my_getpwent()) {
+       if ($uinfo[3] == $oldgid || &indexof($uinfo[0], @$mems) >= 0) {
+               &recursive_change($uinfo[7], -1, $oldgid, -1, $gid);
+               }
+       }
+&my_endpwent();
+}
+
 1;