home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / share / system-tools-backends-2.0 / scripts / Users / Groups.pm next >
Encoding:
Perl POD Document  |  2006-08-14  |  6.1 KB  |  257 lines

  1. #-*- Mode: perl; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2. #
  3. # Copyright (C) 2000-2001 Ximian, Inc.
  4. #
  5. # Authors: Hans Petter Jansson <hpj@ximian.com>,
  6. #          Arturo Espinosa <arturo@ximian.com>,
  7. #          Tambet Ingo <tambet@ximian.com>.
  8. #          Grzegorz Golawski <grzegol@pld-linux.org> (PLD Support)
  9. #
  10. #
  11. # This program is free software; you can redistribute it and/or modify
  12. # it under the terms of the GNU Library General Public License as published
  13. # by the Free Software Foundation; either version 2 of the License, or
  14. # (at your option) any later version.
  15. #
  16. # This program is distributed in the hope that it will be useful,
  17. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. # GNU Library General Public License for more details.
  20. #
  21. # You should have received a copy of the GNU Library General Public License
  22. # along with this program; if not, write to the Free Software
  23. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  24.  
  25. package Users::Groups;
  26.  
  27. # enum like for verbose group array positions
  28. my $LOGIN  = 0;
  29. my $PASSWD = 1;
  30. my $GID    = 2;
  31. my $USERS  = 3;
  32.  
  33. # quite generic data
  34. $group_names = "/etc/group";
  35.  
  36. # Where are the tools?
  37. $cmd_groupdel = &Utils::File::locate_tool ("groupdel");
  38. $cmd_groupadd = &Utils::File::locate_tool ("groupadd");
  39. $cmd_groupmod = &Utils::File::locate_tool ("groupmod");
  40.  
  41. $cmd_delgroup = &Utils::File::locate_tool ("delgroup");
  42. $cmd_addgroup = &Utils::File::locate_tool ("addgroup");
  43.  
  44. $cmd_gpasswd  = &Utils::File::locate_tool ("gpasswd");    
  45. $cmd_pw       = &Utils::File::locate_tool ("pw");
  46.  
  47. sub del_group
  48. {
  49.   my ($group) = @_;
  50.  
  51.   if ($Utils::Backend::tool{"system"} eq "FreeBSD")
  52.   {
  53.     $command = "$cmd_pw groupdel -n \'" . $$group[$LOGIN] . "\'";
  54.   }
  55.   else
  56.   {
  57.     $command  = ($cmd_delgroup) ? $cmd_delgroup : $cmd_groupdel;
  58.     $command .= " \'" . $$group[$LOGIN] . "\'";
  59.   }
  60.  
  61.   &Utils::File::run ($command);
  62. }
  63.  
  64. sub add_group
  65. {
  66.   my ($group) = @_;
  67.   my ($u, $user, $users);
  68.  
  69.   $u = $$group[$USERS];
  70.  
  71.   if ($Utils::Backend::tool{"system"} eq "FreeBSD")
  72.   {
  73.     $users = join (",", sort @$u);
  74.       
  75.     $command = "$cmd_pw groupadd -n \'" . $$group[$LOGIN] .
  76.       "\' -g \'" . $$group[$GID] .
  77.       "\' -M \'" . $users . "\'";
  78.  
  79.     &Utils::File::run ($command);
  80.   }
  81.   else
  82.   {
  83.     if ($cmd_addgroup)
  84.     {
  85.       $command = "$cmd_addgroup " .
  86.           "--gid \'" . $$group[$GID] . "\' " . $$group[$LOGIN];
  87.     }
  88.     else
  89.     {
  90.       $command = "$cmd_groupadd -g \'" . $$group[$GID] .
  91.           "\' " . $$group[$LOGIN];
  92.     }
  93.  
  94.     &Utils::File::run ($command);
  95.  
  96.     foreach $user (sort @$u)
  97.     {
  98.       $command = "$cmd_gpasswd -a \'" . $user .
  99.           "\' " . $$group[$LOGIN];
  100.  
  101.       &Utils::File::run ($command);
  102.     }
  103.   }
  104. }
  105.  
  106. sub change_group
  107. {
  108.     my ($old_group, $new_group) = @_;
  109.   my (%users, %user, $users_arr, $str);
  110.  
  111.     my ($n, $o, $users, $i, $j, $max_n, $max_o, $r, @tmp); # for iterations
  112.  
  113.   if ($Utils::Backend::tool{"system"} eq "FreeBSD")
  114.   {
  115.     $users_arr = $$new_group[$USERS];
  116.     $str = join (",", sort @$users_arr);
  117.  
  118.     $command = "$cmd_pw groupmod -n \'" . $$old_group[$LOGIN] .
  119.         "\' -g \'" . $$new_group[$GID] .
  120.         "\' -l \'" . $$new_group[$LOGIN] .
  121.         "\' -M \'" . $str . "\'";
  122.  
  123.     &Utils::File::run ($command);
  124.   }
  125.   else
  126.   {
  127.     $command = "$cmd_groupmod -g \'" . $$new_group[$GID] .
  128.         "\' -n \'" . $$new_group[$LOGIN] . "\' " .
  129.         "\'" . $$old_group[$LOGIN] . "\'";
  130.   
  131.     &Utils::File::run ($command);
  132.  
  133.     # Let's see if the users that compose the group have changed.
  134.     if (!Utils::Util::struct_eq ($$new_group[$USERS], $$old_group[$USERS]))
  135.     {
  136.       $users{$_} |= 1 foreach (@{$$new_group[$USERS]});
  137.       $users{$_} |= 2 foreach (@{$$old_group[$USERS]});
  138.  
  139.       foreach $user (keys %users)
  140.       {
  141.         $state = $users{$user};
  142.  
  143.         if ($state == 2)
  144.         {
  145.           # users with state 2 are those that only appeared
  146.           # in the old group configuration, so we must delete them
  147.           $command = "$cmd_gpasswd -d \'" . $user . "\' \'" . 
  148.               $$new_group[$LOGIN] . "\'";
  149.  
  150.           &Utils::File::run ($command);
  151.         }
  152.         else
  153.         {
  154.           # users with state 1 are those who were added
  155.           # to the new group configuration
  156.           $command = "$cmd_gpasswd -a \'" . $user . "\' \'" . 
  157.               $$new_group[$LOGIN] . "\'";
  158.  
  159.           &Utils::File::run ($command);
  160.         }
  161.       }
  162.     }
  163.   }
  164. }
  165.  
  166. sub get
  167. {
  168.   my ($ifh, @groups, $group_last_modified);
  169.   my (@line, $copy, @a);
  170.  
  171.   # Find the file.
  172.  
  173.   $ifh = &Utils::File::open_read_from_names($group_names);
  174.   return unless ($ifh);
  175.  
  176.   # Parse the file.
  177.   @groups = ();
  178.  
  179.   while (<$ifh>)
  180.   {
  181.     chomp;
  182.  
  183.     # FreeBSD allows comments in the group file. */
  184.     next if &Utils::Util::ignore_line ($_);
  185.     $_ = &Utils::XML::unquote ($_);
  186.  
  187.     @line = split ':', $_, -1;
  188.     @a = split ',', pop @line;
  189.     push @line, [@a];
  190.     $copy = [@line];
  191.     push (@groups, $copy);
  192.   }
  193.  
  194.   &Utils::File::close_file ($ifh);
  195.  
  196.   return \@groups;
  197. }
  198.  
  199. sub get_files
  200. {
  201.   my @arr;
  202.  
  203.   push @arr, $group_names;
  204.   return \@arr;
  205. }
  206.  
  207. sub set
  208. {
  209.   my ($config) = @_;
  210.   my ($old_config, %groups);
  211.   my (%config_hash, %old_config_hash);
  212.  
  213.   if ($config)
  214.   {
  215.     # Make backup manually, otherwise they don't get backed up.
  216.     &Utils::File::do_backup ($group_names);
  217.  
  218.     $old_config = &get ();
  219.  
  220.     foreach $i (@$config) 
  221.     {
  222.       $groups{$$i[$LOGIN]} |= 1;
  223.       $config_hash{$$i[$LOGIN]} = $i;
  224.       }    
  225.     
  226.     foreach $i (@$old_config)
  227.     {
  228.         $groups{$$i[$LOGIN]} |= 2;
  229.       $old_config_hash{$$i[$LOGIN]} = $i;
  230.     }
  231.  
  232.     # Delete all groups that only appeared in the old configuration
  233.     foreach $i (sort (keys (%groups)))
  234.     {
  235.       $state = $groups{$i};
  236.  
  237.       if ($state == 1)
  238.       {
  239.         # Groups with state 1 have been added to the config
  240.         &add_group ($config_hash{$i});
  241.       }
  242.       elsif ($state == 2)
  243.       {
  244.         # Groups with state 2 have been deleted from the config
  245.         &del_group ($old_config_hash{$i});
  246.       }
  247.       elsif (($state == 3) &&
  248.              (!Utils::Util::struct_eq ($config_hash{$i}, $old_config_hash{$i})))
  249.       {
  250.         &change_group ($old_config_hash{$i}, $config_hash{$i});
  251.       }
  252.     }
  253.   }
  254. }
  255.  
  256. 1;
  257.