2 # $Id: sync-accounts,v 1.8 1999-01-03 01:30:46 ian Exp $
3 # usage: sync-accounts [-n] [-C<config-file>] [<host> ...]
5 # -n do not really do anything
6 # -C alternative config file (default is /etc/sync-accounts)
7 # -q display accounts synched, not synched, etc.
8 # if no host(s) specified, does all
10 # The config file consists of directives, one per line. Leading and
11 # trailing whitespace, blank lines and lines starting # are ignored.
13 # Some config file directives apply globally and should appear first:
15 # lockpasswd <suffix/filename>
16 # lockgroup <suffix/filename>
17 # Specifies the lockfile suffix or pathname to use when editing
18 # the passwd and group files. The value is a suffix if it does
19 # not start with `/'. If set to /dev/null no locking is done.
22 # Append log messages to <filename> instead of stdout.
23 # Errors still go to stderr.
25 # Some config file directives set options which may be different at
26 # different points in the file. The most-recently-seen value is used
32 # When an account is to be created, a uid/gid will be chosen
33 # which is one higher than the highest currently in use (except
34 # that ids outside the range <min>-<max> are ignored and will
35 # never be used). The default home directory location is
36 # <pathname>/<username>.
40 # Specifies whether uids are supposed to match. The default is
41 # nosameuid. When sameuid is on, it is an error for the uid or
42 # gid of a local account not to match the corresponding remote
43 # account, and new local accounts will get the remote accounts'
49 # Specifies whether local accounts are supposed to have
50 # corresponding groups, or all be part of a particular group. If
51 # usergroups is set, when a new account is created, the
52 # corresponding per-user group will be created as well, and
53 # per-user groups are created for existing accounts if necessary
54 # (if account creation is enabled). If the gid or group name for
55 # a per-user group is already taken for a different group name or
56 # gid this will be logged, and processing of that account will be
57 # inhibited, but it is not a fatal error. If defaultgid is used,
58 # then newly-created accounts will be made a part of that group,
59 # and the groups of existing accounts will be left alone. If
60 # nousergroups is specified then no new accounts can be created,
61 # and existing accounts' groups will be left alone. The default
65 # createuser <commandname>
67 # Specifies whether accounts found on the remote host should be
68 # created if necessary, and what command to run to do the
69 # creation (eg, setup of home directory). The default is
70 # nocreateuser. If createuser is specified without a commandname
71 # then sync-accounts-createuser is used. The command is found on
72 # the PATH if necessary. Either sameuid, or both uidmin and
73 # uidmax, must be specified, if accounts are to be created.
75 # group <glob-pattern>
76 # nogroup <glob-pattern>
77 # Specifies that the membership of the local groups specified
78 # should be adjusted or not adjusted whenever account data for a
79 # particular user is copied, so that the account will be a member
80 # of the affected group locally iff it is a member of the same
81 # group on the remote host. The most recently-encountered
82 # glob-pattern for a particular group takes effect. The default
85 # Some config file directives are per-host, and should appear before
86 # any directives which actually modify accounts:
88 # host <shorthostname>
89 # Starts a host's section. This resets the per-host parameters
90 # to the defaults. The shorthostname need not be the host's
91 # official name in any sense. If sync-accounts is invoked with
92 # host names on the command line they are compared with the
97 # Commands to run on the local host to get the passwd, shadow and
98 # group data for the host in question. getpasswd must be
99 # specified if user data is to be transferred; getgroup must be
100 # specified if group data is to be transferred.
102 # getshadow <command>
103 # Specifies that shadow file data is to be used (by default,
104 # password information is found from the output of getpasswd).
105 # The command should emit shadow data in the format specified by
106 # shadow(5) on Linux. getshadow should not be specified without
109 # Some configuration file directives specify that account data is to
110 # transferred from the current host. They should appear as the last
111 # thing(s) in a host section:
113 # user <username> [remote=<remoteusername>]
114 # Specifies that account data should be copied for local user
115 # <username> from the remote account <remoteusername> (assumed to
116 # be the same as <username> if not specified). The account
117 # password, comment field, and shell will be copied
118 # unconditionally. If sameuid is specified the uid will be
121 # users <ruidmin>-<ruidmax>
122 # Specifies that all remote users whose uid is in the given range
123 # are to be copied to corresponding local user accounts.
126 # Specifies that data for <username> is _not_ to be copied, even
127 # if subsequent user or users directives suggest that it should
130 # (A note is made when a `user', `users' or `nouser' directive is
131 # encountered for a particular account, and no subsequent directives
132 # for that account will take effect.)
135 # This directive has no effect on `sync-accounts'. However, it
136 # is used as a placeholder by `grab-account': new accounts for
137 # creation are inserted just before `addhere'.
139 # Finally, the config file must finish with:
145 $configfile= '/etc/sync-accounts';
146 $def_createuser= 'sync-accounts-createuser';
147 $ch_homebase= '/home';
148 $defaultgid= -1; # -1 => usergroups; -2 => nousergroups
149 @groupglobs= [ '.*', 0 ];
154 unlink $x or warn "unable to unlock by removing $x: $!\n";
159 $cdays= int(time/86400);
161 @envs_passwd= qw(USER x UID GID COMMENT HOME SHELL);
163 while ($ARGV[0] =~ m/^-/) {
175 die "unknown option $_\n";
179 for $h (@ARGV) { $wanthost{$h}= 1; }
181 open CF,"< $configfile" or die "$configfile: $!";
183 sub fetchfile (\%$) {
184 my ($ary_ref,$get_str) = @_;
187 open G,"$get_str" or die "$get_str: $!";
190 m/^([^:]+)\:/ or die "$ch_name: $get_str:$.: $_ ?\n";
191 $ary_ref->{$1}= [ split(/\:/,$_,-1) ];
193 close G; $? and die "$ch_name: $get_str: exit code $?\n";
196 sub fetchownfile (\@$$) {
197 my ($ary_ref,$fn_str,$lock_str) = @_;
198 die "$configfile:$.: lockfile name for $fn_str not".
199 " defined (use lockpasswd/lockgroup)\n" unless length $lock_str;
200 if ($lock_str ne '/dev/null' && !$no_act) {
201 $lock_str= "$fn_str$lock_str" unless $lock_str =~ m,^/,;
202 link $fn_str,$lock_str or die "cannot lock $fn_str by creating $lock_str: $!\n";
203 push @unlocks,$lock_str;
205 open O,"$fn_str" or die "$fn_str: $!";
208 push @$ary_ref, [ split(/\:/,$_,-1) ];
210 close O or die "$fn_str: $!";
214 print "$diagstr: $_[0]\n" or die $!;
217 sub regroupglobs () {
218 $nogroups= (@groupglobs == 1 &&
219 $groupglobs[0]->[0] eq '.*' &&
220 !$groupglobs[0]->[1]);
221 $ggfunc= "sub wantsyncgroup {\n \$_= \$_[0];\n return\n";
222 for $e (@groupglobs) { $ggfunc.= " m/^$e->[0]\$/ ? $e->[1] :\n"; }
223 $ggfunc.= " die;\n};\n1;\n";
224 #print STDERR "$ggfunc\n";
225 eval $ggfunc or die "$ggfunc // $@";
229 if (!$own_fetchedpasswd) {
230 fetchownfile(@ownpasswd,'/etc/passwd',$ch_lockpasswd);
231 if (defined stat('/etc/shadow')) {
233 $own_fetchedshadow= 1;
234 fetchownfile(@ownshadow,'/etc/shadow','/dev/null');
235 } elsif ($! == &ENOENT) {
238 die "unable to check for /etc/shadow: $!\n";
240 $own_fetchedpasswd= 1;
242 if (!$own_fetchedgroup) {
243 fetchownfile(@owngroup,'/etc/group',$ch_lockgroup);
244 $own_fetchedgroup= 1;
246 #print STDERR "fetchown() -> $#ownpasswd $#owngroup\n";
250 my ($useuid,$foruser) = @_;
251 for $e (@ownpasswd) {
252 if ($e->[0] ne $foruser && $e->[2] == $useuid) {
253 diag("uid clash with $e->[0] (uid $e->[2])");
260 sub copyfield ($$$$) {
261 my ($file,$entry,$field,$value) = @_;
262 eval "\$ary_ref= \\\@own$file; 1;" or die $@;
263 #print STDERR "copyfield($file,$entry,$field,$value)\n";
265 #print STDERR "copyfield($file,$entry,$field,$value) $e->[0] $e->[field] ".join(':',@$e)."\n";
266 next unless $e->[0] eq $entry;
267 next if $e->[$field] eq $value;
268 $e->[$field]= $value;
269 eval "\$modified$file= 1; 1;" or die $@;
274 return if $ch_fetchedpasswd;
275 die "$configfile:$.: getpasswd not specified for host $ch_name\n"
276 unless length $ch_getpasswd;
278 fetchfile(%rempasswd,"$ch_getpasswd |");
279 if (length $ch_getshadow) {
280 fetchfile(%remshadow,"$ch_getshadow |");
281 for $k (keys %rempasswd) {
282 $rempasswd{$k}->[1]= 'xx' unless length $rempasswd{$k}->[1];
284 for $k (keys %remshadow) {
285 next unless exists $rempasswd{$k};
286 $rempasswd{$k}->[1]= $remshadow{$k}->[1];
292 return if $ch_fetchedgroup;
293 die "$configfile:$.: getgroup not specified for host $ch_name\n"
294 unless length $ch_getgroup;
295 fetchfile(%remgroup,"$ch_getgroup |");
298 sub syncusergroup ($$) {
301 return 1 if $defaultgid != -1;
302 #print STDERR "syncusergroup($lu,$luid)\n";
306 $samename= $e->[0] eq $lu;
307 $sameid= $e->[2] eq $luid;
308 next unless $samename || $sameid;
309 if (!$samename || !$sameid) {
310 diag("local group $e->[0] ($e->[2]) mismatch vs. local user $lu ($luid)");
314 diag("per-user group $lu ($luid) duplicated");
320 return 1 if $ugfound;
322 if (!length $opt_createuser) {
323 diag("account creation not enabled, not creating per-user group");
326 push @owngroup, [ $lu,'x',$luid,'' ];
333 return if $hostheaddone eq $th;
334 print "\n\n" or die $! if length $hostheaddone;
335 print "==== $th ====\n" or die $!;
342 #print STDERR "syncuser($lu,$ru)\n";
343 next unless $ch_doinghost;
344 return if $doneuser{$lu}++;
345 return if !length $ru;
350 for $e (@ownpasswd) {
351 next unless $e->[0] eq $lu;
352 hosthead("from $ch_name");
353 print ($lu eq $ru ? " $lu" : " $lu($ru)") or die $!;
354 print "<DUPLICATE>" if $displaydone{$lu}++;
359 $diagstr= "user $lu from $ch_name!$ru";
361 #print STDERR "syncuser($lu,$ru) doing\n";
364 if (!$rempasswd{$ru}) { diag("no remote entry"); return; }
365 if (length $ch_getshadow && exists $remshadow{$ru} && length $remshadow{$ru}->[7]) {
366 diag("remote account disabled in shadow");
370 if (!grep($_->[0] eq $lu, @ownpasswd)) {
371 if (!length $opt_createuser) { diag("account creation not enabled"); return; }
372 if ($no_act) { diag("-n specified; not creating account"); return; }
375 $useuid= $rempasswd{$ru}->[2];
376 $usegid= $rempasswd{$ru}->[3];
378 die "nousergroups specified, cannot create users\n" if $defaultgid==-2;
379 length $ch_uidmin or die "no uidmin specified, cannot create users\n";
380 length $ch_uidmax or die "no uidmax specified, cannot create users\n";
381 $ch_uidmin<$ch_uidmax or die "uidmin>=uidmax, cannot create users\n";
384 for $e ($defaultgid==-1 ? (@ownpasswd, @owngroup) : (@ownpasswd)) {
385 $tuid= $e->[2]; next if $tuid<$useuid || $tuid>$ch_uidmax;
386 if ($tuid==$ch_uidmax) {
387 diag("uid (or gid?) $ch_uidmax used, cannot create users");
392 $usegid= $defaultgid==-1 ? $useuid : $defaultgid;
395 @newpwent= ($lu,'x',$useuid,$usegid,$rempasswd{$ru}->[4],
396 "$ch_homebase/$lu",$rempasswd{$ru}->[6]);
398 defined($c= open CU,"-|") or die $!;
401 defined($c2= open STDIN,"-|") or die $!;
403 print STDOUT join(':',@newpwent),"\n" or die $!;
406 for ($i=0; $i<@envs_passwd; $i++) {
407 next if $envs_passwd[$i] eq 'x';
408 $ENV{"SYNCUSER_CREATE_$envs_passwd[$i]"}= $newpwent[$i];
410 exec $opt_createuser; die "$configfile:$.: ($lu): $opt_createuser: $!\n";
413 close CU; $? and die "$configfile:$.: ($lu): $opt_createuser: code $?\n";
415 if (length $newpwent) {
416 if ($newpwent !~ m/\:/) { diag("creation script demurred"); return; }
417 @newpwent= split(/\:/,$newpwent,-1);
419 die "$opt_createuser: bad output: $_\n" if @newpwent!=7 or $newpwent[0] ne $lu;
420 checkuid($newpwent[2],$lu) or return;
421 if ($own_haveshadow) {
422 push(@ownshadow,[ $lu, $newpwent[1], $cdays, 0,99999,7,'','','' ]);
426 syncusergroup($lu,$newpwent[2]) or return;
427 push @ownpasswd,[ @newpwent ];
431 for $e (@ownpasswd) {
432 next unless $e->[0] eq $lu;
433 syncusergroup($lu,$e->[2]) or return;
436 $ruid= $rempasswd{$ru}->[2];
437 $rgid= $rempasswd{$ru}->[3];
438 if ($opt_sameuid && checkuid($ruid,$lu)) {
439 for $e (@ownpasswd) {
440 next unless $e->[0] eq $lu;
441 $luid= $e->[2]; $lgid= $e->[3];
442 die "$diagstr: local uid $luid, remote uid $ruid\n" if $luid ne $ruid;
443 die "$diagstr: local gid $lgid, remote gid $rgid\n" if $lgid ne $rgid;
447 #print STDERR "syncuser($lu,$ru) exists $own_haveshadow\n";
448 if ($own_haveshadow && grep($_->[0] eq $lu, @ownshadow)) {
449 #print STDERR "syncuser($lu,$ru) shadow $rempasswd{$ru}->[1]\n";
450 copyfield('shadow',$lu,1, $rempasswd{$ru}->[1]);
452 #print STDERR "syncuser($lu,$ru) passwd $rempasswd{$ru}->[1]\n";
453 copyfield('passwd',$lu,1, $rempasswd{$ru}->[1]);
455 copyfield('passwd',$lu,4, $rempasswd{$ru}->[4]); # comment
456 copyfield('passwd',$lu,6, $rempasswd{$ru}->[6]); # shell
461 #print STDERR "syncuser($lu,$ru) group $tgroup\n";
462 next unless &wantsyncgroup($tgroup);
463 #print STDERR "syncuser($lu,$ru) group $tgroup yes\n";
465 if (!exists $remgroup{$tgroup}) {
466 diag("group $tgroup: not on remote host");
469 $inremote= grep($_ eq $ru, split(/\,/,$remgroup{$tgroup}->[3]));
470 $cusers= $e->[3]; $inlocal= grep($_ eq $lu, split(/\,/,$cusers));
471 if ($inremote && !$inlocal) {
472 $cusers.= ',' if length $cusers;
474 } elsif ($inlocal && !$inremote) {
475 $cusers= join(',', grep($_ ne $lu, split(/\,/, $cusers)));
486 return if $bannerdone;
487 print "\n" or die $!; system 'date'; $? and die $?;
492 for $h (keys %wanthost) {
493 die "host $h not in config file\n" if $wanthost{$h};
498 #print STDERR "\n\nfinish display=$display pw=$pw\n\n";
499 for $e (@ownpasswd) {
501 next if $displaydone{$tu};
503 for $e2 (@ownshadow) {
504 next unless $e2->[0] eq $tu;
505 $tpw= $e2->[1]; last;
507 $tpw= length($tpw)==13 ? 1 : length($tpw) ? -1 : 0;
508 next unless $pw == $tpw;
509 hosthead($pw == 1 ? "unsynched normal account" :
510 $pw == 0 ? "unsynched, passwordless" :
511 "unsynched, no logins");
512 print " $e->[0]" or die $!;
515 print "\n\n" or die $! if $hostheaddone;
520 for $file (qw(passwd shadow group)) {
521 eval "\$modified= \$modified$file; \$data_ref= \\\@own$file;".
522 " \$fetched= \$own_fetched$file; 1;" or die $@;
524 die $file unless $fetched;
526 $newfile= $no_act ? "$file.new" : "/etc/$file.new";
527 open NF,"> $newfile";
528 for $e (@$data_ref) {
529 print NF join(':',@$e),"\n" or die $!;
532 system "diff -U0 /etc/$file $newfile"; $?==256 or die $?;
534 if ($file eq 'shadow') {
535 system "chown root.shadow $newfile"; $? and die $?;
536 chmod 0640, $newfile or die $!;
538 chown 0,0, $newfile or die $!;
539 chmod 0644, $newfile or die $!;
541 rename $newfile, "/etc/$file" or die $!;
549 next if m/^\#/ || !m/\S/;
551 finish() if m/^end$/;
552 if (m/^host\s+(\S+)$/) {
554 $ch_getpasswd= $ch_getgroup= $ch_getshadow= '';
555 $ch_fetchedpasswd= $ch_fetchedgroup;
558 } elsif (exists $wanthost{$ch_name}) {
559 $wanthost{$ch_name}= 0;
564 } elsif (m/^(getpasswd|getshadow|getgroup)\s+(.*\S)$/) {
565 eval "\$ch_$1= \$2; 1;" or die $@;
566 } elsif (m/^(lockpasswd|lockgroup)\s+(\S+)$/) {
567 eval "\$ch_$1= \$2; 1;" or die $@;
568 } elsif (m,^(homebase)\s+(/\S+)$,) {
569 eval "\$ch_$1= \$2; 1;" or die $@;
570 } elsif (m/^(uidmin|uidmax)\s+(\d+)$/ && $2>0) {
571 eval "\$ch_$1= \$2; 1;" or die $@;
572 } elsif (m/^createuser$/) {
573 $opt_createuser= $def_createuser;
574 } elsif (m/^nocreateuser$/) {
576 } elsif (m/^createuser\s+(\S+)$/) {
578 } elsif (m/^logfile\s+(.*\S)$/) {
580 open STDOUT,">> $1" or die "$1: $!"; $|=1;
581 } elsif (!$display) {
582 print "would log to $1\n" or die $!;
584 } elsif (m/^(no|)(sameuid)$/) {
585 eval "\$opt_$2= ".($1 eq 'no' ? 0 : 1)."; 1;" or die $@;
586 } elsif (m/^usergroups$/) {
588 } elsif (m/^nousergroups$/) {
590 } elsif (m/^defaultgid\s+(\d+)$/) {
592 } elsif (m/^(no|)group\s+([-+.0-9a-zA-Z*?]+)$/) {
593 $yes= $1 eq 'no' ? 0 : 1;
595 @groupglobs=() if $_ eq '*';
599 unshift @groupglobs, [ $_, $yes ];
601 } elsif (m/^user\s+(\S+)$/) {
603 } elsif (m/^user\s+(\S+)\s+remote\=(\S+)$/) {
605 } elsif (m/^nouser\s+(\S+)$/) {
607 } elsif (m/^users\s+(\d+)\-(\d+)$/) {
608 $tmin= $1; $tmax= $2; $except= $3;
610 for $k (keys %rempasswd) {
611 $tuid= $rempasswd{$k}->[2];
612 next if $tuid<$1 or $tuid>$2;
615 } elsif (m/^addhere$/) {
617 die "$configfile:$.: unknown directive\n";
621 die "$configfile:$.: missing \`end', or read error\n";