chiark / gitweb /
New -q mode.
[chiark-utils.git] / sync-accounts / sync-accounts
index d83f6f7002050213b834ca6a7c36856177c5a9ab..5bd7a39d43386c4e89946e0df187d1ce37f439f8 100755 (executable)
@@ -1,8 +1,10 @@
 #!/usr/bin/perl
+# $Id: sync-accounts,v 1.5 1999-01-03 00:53:07 ian Exp $
 # usage: sync-accounts [-n] [-C<config-file>] [<host> ...]
 # options:
 #   -n     do not really do anything
 #   -C     alternative config file (default is /etc/sync-accounts)
+#   -q     display accounts synched, not synched, etc.
 # if no host(s) specified, does all
 #
 # The config file consists of directives, one per line.  Leading and
 #      This directive has no effect on `sync-accounts'.  However, it
 #      is used as a placeholder by `grab-account': new accounts for
 #      creation are inserted just before `addhere'.
+#
+# Finally, the config file must finish with:
+# 
+#  end
 
 use POSIX;
 
@@ -155,6 +161,10 @@ while ($ARGV[0] =~ m/^-/) {
        $configfile= $';
     } elsif (m/^-n$/) {
        $no_act= 1;
+       $display= 0;
+    } elsif (m/^-q$/) {
+       $no_act= 1;
+       $display= 1;
     } else {
        die "unknown option $_\n";
     }
@@ -199,6 +209,9 @@ sub diag ($) {
 }
 
 sub regroupglobs () {
+    $nogroups= (@groupglobs == 1 &&
+               $groupglobs[0]->[0] eq '.*' &&
+               !$groupglobs[0]->[1]);
     $ggfunc= "sub wantsyncgroup {\n  \$_= \$_[0];\n  return\n";
     for $e (@groupglobs) { $ggfunc.= "    m/^$e->[0]\$/ ? $e->[1] :\n"; }
     $ggfunc.= "    die;\n};\n1;\n";
@@ -308,7 +321,15 @@ sub syncusergroup ($$) {
     $modifiedgroup= 1;
     return 1;
 }
-    
+
+sub hosthead ($) {
+    my ($th) = @_;
+    return if $hostheaddone eq $th;
+    print "\n\n" or die $! if length $hostheaddone;
+    print "==== $th ====\n" or die $!;
+    $hostheaddone= $th;
+}
+
 sub syncuser ($$) {
     my ($lu,$ru) = @_;
 
@@ -316,9 +337,21 @@ sub syncuser ($$) {
     next unless $ch_doinghost;
     return if $doneuser{$lu}++;
     return if !length $ru;
-    $diagstr= "user $lu from $ch_name!$ru";
 
     fetchown();
+
+    if ($display) {
+       for $e (@ownpasswd) {
+           next unless $e->[0] eq $lu;
+           hosthead("from $ch_name");
+           print ($lu eq $ru ? " $lu" : " $lu($ru)") or die $!;
+           print "<DUPLICATE>" if $displaydone{$lu}++;
+       }
+       return;
+    }
+    
+    $diagstr= "user $lu from $ch_name!$ru";
+
 #print STDERR "syncuser($lu,$ru) doing\n";
     fetchpasswd();
 
@@ -413,28 +446,30 @@ sub syncuser ($$) {
     copyfield('passwd',$lu,4, $rempasswd{$ru}->[4]); # comment
     copyfield('passwd',$lu,6, $rempasswd{$ru}->[6]); # shell
 
-    for $e (@owngroup) {
-       $tgroup= $e->[0];
+    if (!$nogroups) {
+       for $e (@owngroup) {
+           $tgroup= $e->[0];
 #print STDERR "syncuser($lu,$ru) group $tgroup\n";
-       next unless &wantsyncgroup($tgroup);
+           next unless &wantsyncgroup($tgroup);
 #print STDERR "syncuser($lu,$ru) group $tgroup yes\n";
-       fetchgroup();
-       if (!exists $remgroup{$tgroup}) {
-           diag("group $tgroup: not on remote host");
-           next;
-       }
-       $inremote= grep($_ eq $ru, split(/\,/,$remgroup{$tgroup}->[3]));
-       $cusers= $e->[3]; $inlocal= grep($_ eq $lu, split(/\,/,$cusers));
-       if ($inremote && !$inlocal) {
-           $cusers.= ',' if length $cusers;
-           $cusers.= $lu;
-       } elsif ($inlocal && !$inremote) {
-           $cusers= join(',', grep($_ ne $lu, split(/\,/, $cusers)));
-       } else {
-           next;
+           fetchgroup();
+           if (!exists $remgroup{$tgroup}) {
+               diag("group $tgroup: not on remote host");
+               next;
+           }
+           $inremote= grep($_ eq $ru, split(/\,/,$remgroup{$tgroup}->[3]));
+           $cusers= $e->[3]; $inlocal= grep($_ eq $lu, split(/\,/,$cusers));
+           if ($inremote && !$inlocal) {
+               $cusers.= ',' if length $cusers;
+               $cusers.= $lu;
+           } elsif ($inlocal && !$inremote) {
+               $cusers= join(',', grep($_ ne $lu, split(/\,/, $cusers)));
+           } else {
+               next;
+           }
+           $e->[3]= $cusers;
+           $modifiedgroup= 1;
        }
-       $e->[3]= $cusers;
-       $modifiedgroup= 1;
     }
 }
 
@@ -448,6 +483,29 @@ sub finish () {
     for $h (keys %wanthost) {
        die "host $h not in config file\n" if $wanthost{$h};
     }
+
+    if ($display) {
+       for $pw (1,-1,0) {
+#print STDERR "\n\nfinish display=$display pw=$pw\n\n";
+           for $e (@ownpasswd) {
+               $tu= $e->[0];
+               next if $displaydone{$tu};
+               $tpw= $e->[1];
+               for $e2 (@ownshadow) {
+                   next unless $e2->[0] eq $tu;
+                   $tpw= $e2->[1]; last;
+               }
+               $tpw= length($tpw)==13 ? 1 : length($tpw) ? -1 : 0;
+               next unless $pw == $tpw;
+               hosthead($pw == 1 ? "unsynched normal account" :
+                        $pw == 0 ? "unsynched, passwordless" :
+                        "unsynched, no logins");
+               print " $e->[0]" or die $!;
+           }
+       }
+       print "\n\n" or die $! if $hostheaddone;
+       exit 0;
+    }
     
     umask 077;
     for $file (qw(passwd shadow group)) {
@@ -509,15 +567,15 @@ while (<CF>) {
     } elsif (m/^createuser\s+(\S+)$/) {
        $opt_createuser= $1;
     } elsif (m/^logfile\s+(.*\S)$/) {
-       if ($no_act) {
-           print "would log to $1\n" or die $!;
-       } else {
+       if (!$no_act) {
            open STDOUT,">> $1" or die "$1: $!"; $|=1;
+       } elsif (!$display) {
+           print "would log to $1\n" or die $!;
        }
     } elsif (m/^(no|)(sameuid|usergroups)$/) {
        eval "\$opt_$2= ".($1 eq 'no' ? 0 : 1)."; 1;" or die $@;
     } elsif (m/^(no|)group\s+([-+.0-9a-zA-Z*?]+)$/) {
-       $yes= $1 ne 'no';
+       $yes= $1 eq 'no' ? 0 : 1;
        $_= $2;
        @groupglobs=() if $_ eq '*';
        s/[-+._]/\\$1/g;
@@ -539,6 +597,7 @@ while (<CF>) {
            next if $tuid<$1 or $tuid>$2;
            syncuser($k,$k);
        }
+    } elsif (m/^addhere$/) {
     } else {
        die "$configfile:$.: unknown directive\n";
     }