2 # $Id: sync-accounts,v 1.20 2002-07-14 19:29:37 ianmdlvl Exp $
4 # Copyright 1999-2000,2002 Ian Jackson <ian@davenant.greenend.org.uk>
5 # Copyright 2000-2001 nCipher Corporation Ltd
7 # This is free software; you can redistribute it and/or modify it under
8 # the terms of the GNU General Public License as published by the Free
9 # Software Foundation; either version 2, or (at your option) any later
12 # This is distributed in the hope that it will be useful, but WITHOUT
13 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 # You should already have a copy of the GNU General Public License.
18 # If not, write to the Free Software Foundation, Inc., 59 Temple
19 # Place - Suite 330, Boston, MA 02111-1307, USA.
23 $configfile= '/etc/sync-accounts';
24 $def_createuser= 'sync-accounts-createuser';
25 $ch_homebase= '/home';
26 $ch_defaultshell= '/bin/sh';
27 $defaultgid= -1; # -1 => usergroups; -2 => nousergroups
28 @groupglobs= [ '.*', 0 ];
31 $file{'passwd','std'}= 'passwd';
32 $file{'shadow','std'}= 'shadow';
33 $file{'group','std'}= 'group';
35 $file{'passwd','bsd'}= 'master.passwd';
36 $file{'shadow','bsd'}= 'shadow-non-existent';
37 $file{'group','bsd'}= 'group';
39 @fields_pw_std= qw(USER PW UID GID COMMENT HOME SHELL);
40 @fields_pw_bsd= qw(USER PW UID GID CLASS CHANGE EXPIRE COMMENT HOME SHELL);
41 fields_fmt('PW','std');
42 fields('GR',qw(GROUP PW GID USERS));
43 fields('SP',qw(USER PW DAYSCHGD DAYSFIX DAYSEXP DAYSWARN DAYSEXPDIS DAYSDISD RESERVED));
44 # The name field had better always be field 0 !
47 foreach $x (@unlocks) {
48 ($fn, $style, $arg) = @$x;
49 &{ "unlock_$style" } ( $fn,$arg );
56 foreach $v (@l) { $vn= "${pfx}_$v"; $$vn = $i++; }
57 $vn= "${pfx}_fields"; $$vn= $i;
63 $vn= "fields_pw_$fmt";
64 die "unknown format $fmt\n" unless defined @$vn;
71 my ($pfx,$name,@field_val_list) = @_;
72 my (@rv, $vn, $fn, $v, $i);
75 for ($i=0; $i<$$vn; $i++) { $rv[$i]= ''; }
76 die "@field_val_list ?" if @field_val_list % 2;
78 while (@field_val_list) {
79 ($fn,$v,@field_val_list) = @field_val_list;
81 #print STDERR ">$fn|$v|$vn|$$vn<\n";
88 $cdays= int(time/86400);
90 @envs_createuser= qw(USER UID GID COMMENT HOME SHELL);
92 if (@ARGV == 1 && length $ENV{'SYNC_ACCOUNTS_RUNVIA_INFO'}) {
94 s/\%([0-9a-f][0-9a-f])/ pack("C", hex $1) /ge;
96 } split(/\:/, $ENV{'SYNC_ACCOUNTS_RUNVIA_INFO'});
97 delete $ENV{'SYNC_ACCOUNTS_RUNVIA_INFO'};
99 $ENV{"SYNC_ACCOUNTS_RUNVIA_LOCKEDGOT_$ta"} = $ARGV[0];
105 while ($ARGV[0] =~ m/^-/) {
117 die "unknown option $_\n";
121 die "hosts must not be specified with -q\n" if @ARGV && $display;
123 for $h (@ARGV) { $wanthost{$h}= 1; }
125 open CF,"< $configfile" or die "$configfile: $!";
127 sub fetchfile (\%$) {
128 my ($ary_ref,$get_str) = @_;
131 open G,"$get_str" or die "$get_str: $!";
134 m/^([^:]+)\:/ or die "$ch_name: $get_str:$.: $_ ?\n";
135 $ary_ref->{$1}= [ split(/\:/,$_,-1) ];
137 close G; $? and die "$ch_name: $get_str: exit code $?\n";
140 sub lockstyle_ ($$) {
143 die "$configfile:$.: locking mechanism for $fn not".
144 " defined (use lockpasswd/lockgroup)\n";
151 $lock= "$fn$lock" unless $lock =~ m,^/,;
155 sub lock_none ($$) { return (abslock(@_))[0]; }
156 sub unlock_none ($$) { }
159 my ($fn,$lock) = abslock(@_);
160 link $fn,$lock or die "cannot lock $fn by creating $lock: $!\n";
163 sub unlock_link ($$) {
164 my ($fn,$lock) = abslock(@_);
165 unlink $lock or warn "unable to unlock by removing $lock: $!\n";
168 sub lock_runvia ($$) {
171 $evn= "SYNC_ACCOUNTS_RUNVIA_LOCKEDGOT_$fn";
172 return $ENV{$evn} if exists $ENV{$evn};
175 s/\W/ sprintf("%%%02x", unpack("C", $&)) /ge;
178 $ENV{'SYNC_ACCOUNTS_RUNVIA_INFO'}= join(":", @na);
180 delete $ENV{'VISUAL'};
181 exec $lock; die "cannot lock $fn by executing $lock: $!\n";
183 sub unlock_runvia ($$) { }
185 sub fetchownfile (\@$$$$) {
186 my ($ary_ref,$fn_str,$nfields,$style,$lock_arg) = @_;
187 my ($fn_use, $record, $fn_emsg);
190 $fn_use= &{ "lock_$style" } ($fn_str, $lock_arg);
191 push @unlocks, [ $fn_str, $style, $lock_arg ];
192 $savebackto{$fn_str}= $fn_use;
194 $fn_use= $fn_emsg= "/etc/".$file{$fn_str,$PW_format};
196 open O,"$fn_use" or die "$fn_use ($fn_str): $!";
199 $record= [ split(/\:/,$_,-1) ];
200 die "$fn_emsg:$.:wrong number of fields:\`$_'\n"
201 unless @$record == $nfields;
202 push @$ary_ref, $record;
204 close O or die "$fn_use ($fn_str): $!";
208 print "$diagstr: $_[0]\n" or die $!;
211 sub regroupglobs () {
212 $nogroups= (@groupglobs == 1 &&
213 $groupglobs[0]->[0] eq '.*' &&
214 !$groupglobs[0]->[1]);
215 $ggfunc= "sub wantsyncgroup {\n \$_= \$_[0];\n return\n";
216 for $g (@groupglobs) { $ggfunc.= " m/^$g->[0]\$/ ? $g->[1] :\n"; }
217 $ggfunc.= " die;\n};\n1;\n";
218 #print STDERR "$ggfunc\n";
219 eval $ggfunc or die "$ggfunc // $@";
223 if (!$own_fetchedpasswd) {
224 #print STDERR ">$PW_fields<\n";
225 fetchownfile(@ownpasswd,'passwd',
226 $PW_fields, $ch_lockstyle_passwd, $ch_lock_passwd);
227 $shadowfile= $file{'shadow',$PW_format};
228 if (stat("/etc/$shadowfile")) {
230 $own_fetchedshadow= 1;
231 fetchownfile(@ownshadow,'shadow',$SP_fields,'none','');
232 } elsif ($! == &ENOENT) {
235 die "unable to check for /etc/$shadowfile: $!\n";
237 $own_fetchedpasswd= 1;
239 if (!$own_fetchedgroup) {
240 fetchownfile(@owngroup,'group',$GR_fields,
241 $ch_lockstyle_group, $ch_lock_group);
242 $own_fetchedgroup= 1;
244 #print STDERR "fetchown() -> $#ownpasswd $#owngroup\n";
248 my ($useuid,$foruser) = @_;
249 for $e (@ownpasswd) {
250 if ($e->[$PW_USER] ne $foruser && $e->[$PW_UID] == $useuid) {
251 diag("uid clash with $e->[$PW_USER] (uid $e->[$PW_UID])");
258 sub copyfield ($$$$) {
259 my ($file,$entry,$field,$value) = @_;
260 eval "\$ary_ref= \\\@own$file; 1;" or die $@;
261 #print STDERR "copyfield($file,$entry,$field,$value)\n";
263 #print STDERR "copyfield($file,$entry,$field,$value) $e->[0] $e->[field] ".join(':',@$e)."\n";
264 next unless $e->[0] eq $entry;
265 next if $e->[$field] eq $value;
266 $e->[$field]= $value;
267 eval "\$modified$file= 1; 1;" or die $@;
272 return if $ch_fetchedpasswd;
273 die "$configfile:$.: getpasswd not specified for host $ch_name\n"
274 unless length $ch_getpasswd;
276 fetchfile(%rempasswd,"$ch_getpasswd |");
277 if (length $ch_getshadow) {
278 fetchfile(%remshadow,"$ch_getshadow |");
279 for $k (keys %rempasswd) {
280 $rempasswd{$k}->[$REM_PW]= 'xx' unless length $rempasswd{$k}->[$REM_PW];
282 for $k (keys %remshadow) {
283 next unless exists $rempasswd{$k};
284 $rempasswd{$k}->[$REM_PW]= $remshadow{$k}->[$SP_PW];
287 $ch_fetchedpasswd= 1;
291 return if $ch_fetchedgroup;
292 die "$configfile:$.: getgroup not specified for host $ch_name\n"
293 unless length $ch_getgroup;
294 fetchfile(%remgroup,"$ch_getgroup |");
298 sub syncusergroup ($$) {
301 return 1 if $defaultgid != -1;
302 #print STDERR "syncusergroup($lu,$luid)\n";
306 $samename= $e->[$GR_GROUP] eq $lu;
307 $sameid= $e->[$GR_GID] eq $luid;
308 next unless $samename || $sameid;
309 if (!$samename || !$sameid) {
310 diag("local group $e->[$GR_GROUP] ($e->[$GR_GID]) mismatch vs.".
311 " local user $lu ($luid)");
315 diag("per-user group $lu ($luid) duplicated");
321 return 1 if $ugfound;
323 if (!length $opt_createuser) {
324 diag("account creation not enabled, not creating per-user group");
327 push @owngroup, [ newentry('GR', $lu,
336 return if $hostheaddone eq $th;
337 print "\n\n" or die $! if length $hostheaddone;
338 print "==== $th ====\n" or die $!;
346 #print STDERR "syncuser($lu,$ru)\n";
347 return if $doneuser{$lu}++;
348 next unless $ch_doinghost;
349 return if !length $ru;
354 for $e (@ownpasswd) {
355 next unless $e->[$PW_USER] eq $lu;
356 hosthead("from $ch_name");
357 print ($lu eq $ru ? " $lu" : " $lu($ru)") or die $!;
358 print "<DUPLICATE>" if $displaydone{$lu}++;
363 $diagstr= "user $lu from $ch_name!$ru";
365 #print STDERR "syncuser($lu,$ru) doing\n";
368 if (!$rempasswd{$ru}) { diag("no remote entry"); return; }
369 if (length $ch_getshadow && exists $remshadow{$ru} &&
370 length $remshadow{$ru}->[$SP_DAYSDISD]) {
371 diag("remote account disabled in shadow");
375 if (!grep($_->[$PW_USER] eq $lu, @ownpasswd)) {
376 if (!length $opt_createuser) { diag("account creation not enabled"); return; }
377 if ($no_act) { diag("-n specified; not creating account"); return; }
380 $useuid= $rempasswd{$ru}->[$REM_UID];
381 $usegid= $rempasswd{$ru}->[$REM_GID];
383 die "nousergroups specified, cannot create users\n" if $defaultgid==-2;
384 length $ch_uidmin or die "no uidmin specified, cannot create users\n";
385 length $ch_uidmax or die "no uidmax specified, cannot create users\n";
386 $ch_uidmin<$ch_uidmax or die "uidmin>=uidmax, cannot create users\n";
389 for $e ($defaultgid==-1 ? (@ownpasswd, @owngroup) : (@ownpasswd)) {
390 $tuid= $e->[$PW_UID]; next if $tuid<$useuid || $tuid>$ch_uidmax;
391 if ($tuid==$ch_uidmax) {
392 diag("uid (or gid?) $ch_uidmax used, cannot create users");
397 $usegid= $defaultgid==-1 ? $useuid : $defaultgid;
400 @newpwent= newentry('PW', $lu,
404 'COMMENT', $rempasswd{$ru}->[$REM_COMMENT],
405 'HOME', "$ch_homebase/$lu",
406 'SHELL', $ch_defaultshell);
408 defined($c= open CU,"-|") or die $!;
411 defined($c2= open STDIN,"-|") or die $!;
413 print STDOUT join(':',@newpwent),"\n" or die $!;
416 for ($i=0; $i<@envs_createuser; $i++) {
417 $vn= "PW_$envs_createuser[$i]";
418 #print STDERR ">$i|$vn|$$vn|$newpwent[$$vn]<\n";
419 $ENV{"SYNCUSER_CREATE_$envs_createuser[$i]"}= $newpwent[$$vn];
421 exec $opt_createuser; die "$configfile:$.: ($lu): $opt_createuser: $!\n";
424 close CU; $? and die "$configfile:$.: ($lu): $opt_createuser: code $?\n";
426 if (length $newpwent) {
427 if ($newpwent !~ m/\:/) { diag("creation script demurred"); return; }
428 @newpwent= split(/\:/,$newpwent,-1);
430 die "$opt_createuser: bad result: \`".join(':',@newpwent)."\'\n"
431 if @newpwent != $PW_fields or $newpwent[$PW_USER] ne $lu;
432 checkuid($newpwent[$PW_UID],$lu) or return;
433 if ($own_haveshadow) {
434 push @ownshadow, [ newentry('SP', $lu,
442 syncusergroup($lu,$newpwent[$PW_UID]) or return;
443 push @ownpasswd,[ @newpwent ];
447 for $e (@ownpasswd) {
448 next unless $e->[$PW_USER] eq $lu;
449 syncusergroup($lu,$e->[$PW_UID]) or return;
452 $ruid= $rempasswd{$ru}->[$REM_UID];
453 $rgid= $rempasswd{$ru}->[$REM_GID];
454 if ($opt_sameuid && checkuid($ruid,$lu)) {
455 for $e (@ownpasswd) {
456 next unless $e->[$PW_USER] eq $lu;
457 $luid= $e->[$PW_UID]; $lgid= $e->[$PW_GID];
458 die "$diagstr: local uid $luid, remote uid $ruid\n" if $luid ne $ruid;
459 die "$diagstr: local gid $lgid, remote gid $rgid\n" if $lgid ne $rgid;
463 #print STDERR "syncuser($lu,$ru) exists $own_haveshadow\n";
464 if ($own_haveshadow && grep($_->[$PW_USER] eq $lu, @ownshadow)) {
465 #print STDERR "syncuser($lu,$ru) shadow $rempasswd{$ru}->[$REM_PW]\n";
466 copyfield('shadow',$lu,$SP_PW, $rempasswd{$ru}->[$REM_PW]);
468 #print STDERR "syncuser($lu,$ru) passwd $rempasswd{$ru}->[$REM_PW]\n";
469 copyfield('passwd',$lu,$PW_PW, $rempasswd{$ru}->[$REM_PW]);
471 copyfield('passwd',$lu,$PW_COMMENT, $rempasswd{$ru}->[$REM_COMMENT]);
473 $newsh= $rempasswd{$ru}->[$REM_SHELL];
474 $oksh= $checkedshell{$newsh};
475 if (!length $oksh) { $checkedshell{$newsh}= $oksh= (-x $newsh) ? 1 : 0; }
476 copyfield('passwd',$lu,$PW_SHELL, $newsh) if $oksh;
480 $tgroup= $e->[$GR_GROUP];
481 #print STDERR "syncuser($lu,$ru) group $tgroup\n";
482 next unless &wantsyncgroup($tgroup);
483 #print STDERR "syncuser($lu,$ru) group $tgroup yes\n";
485 if (!exists $remgroup{$tgroup}) {
486 diag("group $tgroup: not on remote host");
489 $inremote= grep($_ eq $ru, split(/\,/,$remgroup{$tgroup}->[$GR_USERS]));
490 $cusers= $e->[$GR_USERS]; $inlocal= grep($_ eq $lu, split(/\,/,$cusers));
491 if ($inremote && !$inlocal) {
492 $cusers.= ',' if length $cusers;
494 } elsif ($inlocal && !$inremote) {
495 $cusers= join(',', grep($_ ne $lu, split(/\,/, $cusers)));
499 $e->[$GR_USERS]= $cusers;
506 return if $bannerdone;
507 print "\n" or die $!; system 'date'; $? and die $?;
512 for $h (keys %wanthost) {
513 die "host $h not in config file\n" if $wanthost{$h};
517 #print STDERR "\n\nfinish display=$display pw=$pw\n\n";
518 for $e (@ownpasswd) {
520 $tuid= $e->[$PW_UID];
521 next if $displaydone{$tu};
523 #print STDERR ">$tu|$tpw<\n";
524 for $e2 (@ownshadow) {
525 next unless $e2->[$SP_USER] eq $tu;
526 $tpw= $e2->[$SP_PW]; last;
528 $tpw= length($tpw)>=13 ? 1 : length($tpw) ? -1 : 0;
529 $head= ($tpw == 1 ? "unsynched" :
530 $tpw == 0 ? "unsynched, passwordless" :
531 "unsynched, no-logins").
532 ($tuid < 100 ? " system account" : " normal user");
533 $unsynch_type{$head} .= " $e->[$PW_USER]";
535 foreach $head (sort keys %unsynch_type) {
537 print $unsynch_type{$head} or die $!;
539 print "\n\n" or die $!;
544 for $file (qw(passwd shadow group)) {
545 $realfile= $file{$file,$PW_format};
546 eval "\$modified= \$modified$file; \$data_ref= \\\@own$file;".
547 " \$fetched= \$own_fetched$file; 1;" or die $@;
549 die $file unless $fetched;
552 $newfileinst= "/etc/$realfile";
553 $newfile= "$realfile.new";
555 $newfileinst= $savebackto{$file};
556 $newfile= "$newfileinst.new";
558 open NF,"> $newfile" or die "$newfile: $!";
559 for $e (@$data_ref) {
560 print NF join(':',@$e),"\n" or die $!;
563 system 'diff','-U0','--label',$realfile,$newfileinst,
564 '--label',"$realfile.new",$newfile;
567 (@stats= stat $newfileinst) or die "$file: $!";
568 chown $stats[4],$stats[5], $newfile or die $!;
569 chmod $stats[2] & 07777, $newfile or die $!;
570 rename $newfile, $newfileinst or die $!;
579 next if m/^\#/ || !m/\S/;
580 finish() if m/^end$/;
581 if (m/^host\s+(\S+)$/) {
583 $ch_getpasswd= $ch_getgroup= $ch_getshadow= '';
584 $ch_fetchedpasswd= $ch_fetchedgroup;
587 } elsif (exists $wanthost{$ch_name}) {
588 $wanthost{$ch_name}= 0;
593 fields_fmt('REM','std');
594 } elsif (m/^(getpasswd|getshadow|getgroup)\s+(.*\S)$/) {
595 eval "\$ch_$1= \$2; 1;" or die $@;
596 } elsif (m/^(local|remote)format\s+(\w+)$/) {
597 fields_fmt($1 eq 'local' ? 'PW' :
598 $1 eq 'remote' ? 'REM' : die,
600 } elsif (m/^lock(passwd|group)\s+(runvia|link)\s+(\S+)$/) {
601 eval "\$ch_lock_$1= \$3; \$ch_lockstyle_$1= \$2; 1;" or die $@;
602 } elsif (m/^lock(passwd|group)\s+(none)$/) {
603 eval "\$ch_lockstyle_$1= \$2; 1;" or die $@;
604 } elsif (m,^(homebase|defaultshell)\s+(/\S+)$,) {
605 eval "\$ch_$1= \$2; 1;" or die $@;
606 } elsif (m/^(uidmin|uidmax)\s+(\d+)$/ && $2>0) {
607 eval "\$ch_$1= \$2; 1;" or die $@;
608 } elsif (m/^createuser$/) {
609 $opt_createuser= $def_createuser;
610 } elsif (m/^nocreateuser$/) {
612 } elsif (m/^createuser\s+(\S+)$/) {
614 } elsif (m/^logfile\s+(.*\S)$/) {
616 open STDOUT,">> $1" or die "$1: $!"; $|=1;
617 $!=0; system 'date'; $? and die "date: $! $?\n";
618 } elsif (!$display) {
619 print "would log to $1\n" or die $!;
621 } elsif (m/^(no|)(sameuid)$/) {
622 eval "\$opt_$2= ".($1 eq 'no' ? 0 : 1)."; 1;" or die $@;
623 } elsif (m/^usergroups$/) {
625 } elsif (m/^nousergroups$/) {
627 } elsif (m/^defaultgid\s+(\d+)$/) {
629 } elsif (m/^(no|)group\s+([-+.0-9a-zA-Z*?]+)$/) {
630 $yes= $1 eq 'no' ? 0 : 1;
632 @groupglobs=() if $_ eq '*';
636 unshift @groupglobs, [ $_, $yes ];
638 } elsif (m/^user\s+(\S+)$/) {
640 } elsif (m/^user\s+(\S+)\s+remote\=(\S+)$/) {
642 } elsif (m/^nouser\s+(\S+)$/) {
644 } elsif (m/^users\s+(\d+)\-(\d+)$/) {
645 $tmin= $1; $tmax= $2; $except= $3;
647 for $k (keys %rempasswd) {
648 $tuid= $rempasswd{$k}->[2];
649 next if $tuid<$1 or $tuid>$2;
652 } elsif (m/^addhere$/) {
654 die "$configfile:$.: unknown directive\n";
658 die "$configfile:$.: missing \`end', or read error\n";