chiark / gitweb /
Support defaultgid.
[chiark-utils.git] / sync-accounts / sync-accounts
index 8c47b001a0e02b8867c28aff9bab0626d237eedc..ea4e79cc089d8e341503061f1c710f6a02c659eb 100755 (executable)
@@ -1,5 +1,5 @@
 #!/usr/bin/perl
-# $Id: sync-accounts,v 1.7 1999-01-03 01:20:37 ian Exp $
+# $Id: sync-accounts,v 1.8 1999-01-03 01:30:46 ian Exp $
 # usage: sync-accounts [-n] [-C<config-file>] [<host> ...]
 # options:
 #   -n     do not really do anything
 #
 #  usergroups
 #  nousergroups
+#  defaultgid <gid>
 #      Specifies whether local accounts are supposed to have
-#      corresponding groups.  If this is set then when a new account
-#      is created, the corresponding per-user group will be created as
-#      well, and per-user groups are created for existing accounts if
-#      necessary (if account creation is enabled).  If the gid or
-#      group name for a per-user group is already taken for a
-#      different group name or gid this will be logged, and processing
-#      of that account will be inhibited, but it is not a fatal
-#      error.  The default is `usergroups'.
+#      corresponding groups, or all be part of a particular group.  If
+#      usergroups is set, when a new account is created, the
+#      corresponding per-user group will be created as well, and
+#      per-user groups are created for existing accounts if necessary
+#      (if account creation is enabled).  If the gid or group name for
+#      a per-user group is already taken for a different group name or
+#      gid this will be logged, and processing of that account will be
+#      inhibited, but it is not a fatal error.  If defaultgid is used,
+#      then newly-created accounts will be made a part of that group,
+#      and the groups of existing accounts will be left alone.  If
+#      nousergroups is specified then no new accounts can be created,
+#      and existing accounts' groups will be left alone.  The default
+#      is `usergroups'.
 #
 #  createuser
 #  createuser <commandname>
@@ -139,7 +145,7 @@ use POSIX;
 $configfile= '/etc/sync-accounts';
 $def_createuser= 'sync-accounts-createuser';
 $ch_homebase= '/home';
-$opt_usergroups= 1;
+$defaultgid= -1; # -1 => usergroups; -2 => nousergroups
 @groupglobs= [ '.*', 0 ];
 regroupglobs();
 
@@ -292,7 +298,7 @@ sub fetchgroup () {
 sub syncusergroup ($$) {
     my ($lu,$luid) = @_;
 
-    return 1 if !$opt_usergroups;
+    return 1 if $defaultgid != -1;
 #print STDERR "syncusergroup($lu,$luid)\n";
     $ugfound=0;
     
@@ -367,23 +373,26 @@ sub syncuser ($$) {
 
        if ($opt_sameuid) {
            $useuid= $rempasswd{$ru}->[2];
+           $usegid= $rempasswd{$ru}->[3];
        } else {
-           length $ch_uidmin or die "no uidmin specified, cannot create users";
-           length $ch_uidmax or die "no uidmax specified, cannot create users";
-           $ch_uidmin<$ch_uidmax or die "uidmin>=uidmax specified, cannot create users";
+           die "nousergroups specified, cannot create users\n" if $defaultgid==-2;
+           length $ch_uidmin or die "no uidmin specified, cannot create users\n";
+           length $ch_uidmax or die "no uidmax specified, cannot create users\n";
+           $ch_uidmin<$ch_uidmax or die "uidmin>=uidmax, cannot create users\n";
        
            $useuid= $ch_uidmin;
-           for $e (@ownpasswd, @owngroup) {
+           for $e ($defaultgid==-1 ? (@ownpasswd, @owngroup) : (@ownpasswd)) {
                $tuid= $e->[2]; next if $tuid<$useuid || $tuid>$ch_uidmax;
                if ($tuid==$ch_uidmax) {
-                   diag("uid/gid $ch_uidmax used, cannot create users");
+                   diag("uid (or gid?) $ch_uidmax used, cannot create users");
                    return;
                }
                $useuid= $tuid+1;
            }
+           $usegid= $defaultgid==-1 ? $useuid : $defaultgid;
        }
        
-       @newpwent= ($lu,'x',$useuid,$useuid,$rempasswd{$ru}->[4],
+       @newpwent= ($lu,'x',$useuid,$usegid,$rempasswd{$ru}->[4],
                    "$ch_homebase/$lu",$rempasswd{$ru}->[6]);
        
        defined($c= open CU,"-|") or die $!;
@@ -572,8 +581,14 @@ while (<CF>) {
        } elsif (!$display) {
            print "would log to $1\n" or die $!;
        }
-    } elsif (m/^(no|)(sameuid|usergroups)$/) {
+    } elsif (m/^(no|)(sameuid)$/) {
        eval "\$opt_$2= ".($1 eq 'no' ? 0 : 1)."; 1;" or die $@;
+    } elsif (m/^usergroups$/) {
+       $defaultgid= -1;
+    } elsif (m/^nousergroups$/) {
+       $defaultgid= -2;
+    } elsif (m/^defaultgid\s+(\d+)$/) {
+       $defaultgid= $1;
     } elsif (m/^(no|)group\s+([-+.0-9a-zA-Z*?]+)$/) {
        $yes= $1 eq 'no' ? 0 : 1;
        $_= $2;