home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / share / perl5 / Debian / AdduserCommon.pm next >
Encoding:
Text File  |  2006-07-10  |  5.7 KB  |  221 lines

  1. use vars qw(@EXPORT $VAR1);
  2.  
  3.  
  4. # Common functions that are used in adduser and deluser
  5. # Copyright (C) 2000 Roland Bauerschmidt <rb@debian.org>
  6.  
  7. # Most of the functions are adopted from the original adduser
  8. # Copyright (C) 1997, 1998, 1999 Guy Maor <maor@debian.org>
  9. # Copyright (C) 1995 Ted Hajek <tedhajek@boombox.micro.umn.edu>
  10. #                     Ian A. Murdock <imurdock@gnu.ai.mit.edu>
  11. #
  12.  
  13. @EXPORT = qw(invalidate_nscd gtx dief warnf read_config get_users_groups get_group_members s_print s_printf systemcall);
  14.  
  15. sub invalidate_nscd {
  16.     # Check if we need to do make -C /var/yp for NIS
  17.     my $nisconfig;
  18.     if(-f "/etc/default/nis") {
  19.         $nisconfig = "/etc/default/nis";
  20.     } elsif(-f "/etc/init.d/nis") {
  21.         $nisconfig = "/etc/init.d/nis";
  22.     }
  23.     if(defined($nisconfig) && -f "/var/yp/Makefile" &&
  24.         -x "/usr/bin/rpcinfo" && grep(/ypserv/, qx{/usr/bin/rpcinfo -p})) {
  25.     open(NISCONFIG, "<$nisconfig");
  26.     if(grep(/^NISSERVER=master/, <NISCONFIG>)) {
  27.             system("make", "-C", "/var/yp");
  28.     }
  29.     close(NISCONFIG);
  30.     }
  31.  
  32.     # Check if we need to invalidate the NSCD cache
  33.     my $nscd = &which('nscd',1);
  34.     # this function replaces startnscd and stopnscd (closes: #54726)
  35.     # We are ignoring any error messages given by nscd here since we
  36.     # cannot expect the nscd maintainer and upstream to document their
  37.     # interfaces. See #330929.
  38.     if(defined($nscd) && -x $nscd)
  39.       {
  40.         my $table = shift;
  41.         if ($table)
  42.           {
  43.             system ($nscd, "-i", $table);
  44.           }
  45.         else
  46.           {
  47.             # otherwise we invalidate passwd and group table
  48.             system ($nscd, "-i", "passwd");
  49.             system ($nscd, "-i", "group");
  50.           }
  51.       }
  52. }
  53.  
  54. sub gtx {
  55.     return gettext( shift );
  56. }
  57.  
  58. sub dief {
  59.     my ($form,@argu)=@_;
  60.     printf STDERR "$0: $form",@argu;
  61.     exit 1;
  62. }
  63.  
  64. sub warnf {
  65.     my ($form,@argu)=@_;
  66.     printf STDERR "$0: $form",@argu;
  67. }
  68.  
  69. # parse the configuration file
  70. # parameters:
  71. #  -- filename of the configuration file
  72. #  -- a hash for the configuration data
  73. sub read_config {
  74.     my ($conf_file, $configref) = @_;
  75.     my ($var, $lcvar, $val);
  76.  
  77.     if (! -f $conf_file) {
  78.     printf gtx("%s: `%s' doesn't exist.  Using defaults.\n"),$0,$conf_file if $verbose;
  79.     return;
  80.     }
  81.  
  82.     open (CONF, $conf_file) || dief ("%s: `%s'\n",$conf_file,$!);
  83.     while (<CONF>) {
  84.     chomp;
  85.     next if /^#/ || /^\s*$/;
  86.  
  87.     if ((($var, $val) = /^\s*([_a-zA-Z0-9]+)\s*=\s*(.*)/) != 2) {
  88.         warnf gtx("Couldn't parse `%s':%s.\n"),$conf_file,$.;
  89.         next;
  90.     }
  91.     $lcvar = lc $var;
  92.     if (!defined($configref->{$lcvar})) {
  93.         warnf gtx("Unknown variable `%s' at `%s':%s.\n"),$var,$conf_file,$.;
  94.         next;
  95.     }
  96.  
  97.     $val =~ s/^"(.*)"$/$1/;
  98.     $val =~ s/^'(.*)'$/$1/;
  99.  
  100.     $configref->{$lcvar} = $val;
  101.     }
  102.  
  103.     close CONF || die "$!";
  104. }
  105.  
  106. # return a user's groups
  107. sub get_users_groups {
  108.     my($user) = @_;
  109.     my($name,$members,@groups);
  110.     setgrent;
  111.     while (($name,$members) = (getgrent)[0,3]) {
  112.     for (split(/ /, $members)) {
  113.         if ($user eq $_) {
  114.         push @groups, $name;
  115.         last;
  116.         }
  117.     }
  118.     }
  119.     endgrent;
  120.     @groups;
  121. }
  122.  
  123. # return a group's members
  124. sub get_group_members
  125.   {
  126.       my $group = shift;
  127.       my @members;
  128.       foreach (split(/ /, (getgrnam($group))[3])) {
  129.       if( getpwnam($_) ) {
  130.           push @members, $_;
  131.       }
  132.       }
  133.       return @members;
  134.   }
  135.  
  136. sub s_print
  137. {
  138.     print join(" ",@_)
  139.     if($verbose);
  140. }
  141.  
  142. sub s_printf
  143. {
  144.     printf @_
  145.     if($verbose);
  146. }
  147.  
  148. sub d_printf
  149. {
  150.     printf @_
  151.         if((defined($verbose) && $verbose > 1) || (defined($debugging) && $debugging == 1));
  152. }
  153.  
  154. sub systemcall {
  155.     my $c = join(' ', @_);
  156.     print ("$c\n") if $verbose==2;
  157.     if (system(@_)) {
  158.         die ("$0: `$c' returned error code " . ($?>>8) . ".  Aborting.\n")
  159.           if ($?>>8);
  160.         die ("$0: `$c' exited from signal " . ($?&255) . ".  Aborting.\n");
  161.     }
  162. }
  163.  
  164. sub which {
  165.     my ($progname, $nonfatal) = @_ ;
  166.     for my $dir (split /:/, $ENV{"PATH"}) {
  167.         if (-x "$dir/$progname" ) {
  168.             return "$dir/$progname";
  169.         }
  170.     }
  171.     dief(gtx("No program named %s in \$PATH\n"), $progname) unless ($nonfatal);
  172. }
  173.  
  174.  
  175. # preseed the configuration variables 
  176. # then read the config file /etc/adduser and overwrite the data hardcoded here
  177. sub preseed_config {
  178.   my ($conflistref, $configref) = @_;
  179.   $configref->{"system"} = 0;
  180.   $configref->{"only_if_empty"} = 0;
  181.   $configref->{"remove_home"} = 0;
  182.   $configref->{"home"} = "";
  183.   $configref->{"remove_all_files"} = 0;
  184.   $configref->{"backup"} = 0;
  185.   $configref->{"backup_to"} = ".";
  186.   $configref->{"dshell"} = "/bin/bash";
  187.   $configref->{"first_system_uid"} = 100;
  188.   $configref->{"last_system_uid"} = 999;
  189.   $configref->{"first_uid"} = 1000;
  190.   $configref->{"last_uid"} = 29999;
  191.   $configref->{"first_system_gid"} = 100;
  192.   $configref->{"last_system_gid"} = 999;
  193.   $configref->{"first_gid"} = 1000;
  194.   $configref->{"last_gid"} = 29999;
  195.   $configref->{"dhome"} = "/home";
  196.   $configref->{"skel"} = "/etc/skel";
  197.   $configref->{"usergroups"} = "yes";
  198.   $configref->{"users_gid"} = "100";
  199.   $configref->{"grouphomes"} = "no";
  200.   $configref->{"letterhomes"} = "no";
  201.   $configref->{"quotauser"} = "";
  202.   $configref->{"dir_mode"} = "0755";
  203.   $configref->{"setgid_home"} = "no";
  204.   $configref->{"no_del_paths"} = "^/$ ^/lost+found/.* ^/media/.* ^/mnt/.* ^/etc/.* ^/bin/.* ^/boot/.* ^/dev/.* ^/lib/.* ^/proc/.* ^/root/.* ^/sbin/.* ^/tmp/.* ^/sys/.* ^/srv/.* ^/opt/.* ^/initrd/.* ^/usr/.* ^/var/.*";
  205.   $configref->{"name_regex"} = "^[a-z][-a-z0-9]*\$";
  206.   $configref->{"exclude_fstypes"} = "(proc|sysfs|usbfs|devpts|tmpfs)";
  207.   $configref->{"skel_ignore_regex"} = "dpkg-(old|new|dist)\$";
  208.   $configref->{"extra_groups"} = "dialout cdrom floppy audio src video lp users";
  209.   $configref->{"add_extra_groups"} = 0;
  210.  
  211.   foreach( @$conflistref ) {
  212.       read_config($_,$configref);
  213.   }
  214. }
  215.  
  216. # Local Variables:
  217. # mode:cperl
  218. # End:
  219.  
  220. #vim:set ai et sts=4 sw=4 tw=0:
  221.