2 # usage: sync-accounts [-n] [-C<config-file>] [<host> ...]
4 # -n do not really do anything
5 # -C alternative config file (default is /etc/sync-accounts)
6 # if no host(s) specified, does all
8 # The config file consists of directives, one per line. Leading and
9 # trailing whitespace, blank lines and lines starting # are ignored.
11 # Some config file directives apply globally and should appear first:
13 # lockpasswd <suffix/filename>
14 # lockgroup <suffix/filename>
15 # Specifies the lockfile suffix or pathname to use when editing
16 # the passwd and group files. The value is a suffix if it does
20 # Append log messages to <filename> instead of stdout.
21 # Errors still go to stderr.
23 # Some config file directives set options which may be different at
24 # different points in the file. The most-recently-seen value is used
30 # When an account is to be created, a uid/gid will be chosen
31 # which is one higher than the highest currently in use (except
32 # that ids outside the range <min>-<max> are ignored and will
33 # never be used). The default home directory location is
34 # <pathname>/<username>.
38 # Specifies whether uids are supposed to match. The default is
39 # nosameuid. When sameuid is on, it is an error for the uid or
40 # gid of a local account not to match the corresponding remote
41 # account, and new local accounts will get the remote accounts'
46 # Specifies whether local accounts are supposed to have
47 # corresponding groups. If this is set then when a new account
48 # is created, the corresponding per-user group will be created as
49 # well, and per-user groups are created for existing accounts if
50 # necessary (if account creation is enabled). If the gid or
51 # group name for a per-user group is already taken for a
52 # different group name or gid this will be logged, and processing
53 # of that account will be inhibited, but it is not a fatal
54 # error. The default is `usergroups'.
57 # createuser <commandname>
59 # Specifies whether accounts found on the remote host should be
60 # created if necessary, and what command to run to do the
61 # creation (eg, setup of home directory). The default is
62 # nocreateuser. If createuser is specified without a commandname
63 # then sync-accounts-createuser is used. The command is found on
64 # the PATH if necessary. Either sameuid, or both uidmin and
65 # uidmax, must be specified, if accounts are to be created.
67 # group <glob-pattern>
68 # nogroup <glob-pattern>
69 # Specifies that the membership of the local groups specified
70 # should be adjusted or not adjusted whenever account data for a
71 # particular user is copied, so that the account will be a member
72 # of the affected group locally iff it is a member of the same
73 # group on the remote host. The most recently-encountered
74 # glob-pattern for a particular group takes effect. The default
77 # Some config file directives are per-host, and should appear before
78 # any directives which actually modify accounts:
80 # host <shorthostname>
81 # Starts a host's section. This resets the per-host parameters
82 # to the defaults. The shorthostname need not be the host's
83 # official name in any sense. If sync-accounts is invoked with
84 # host names on the command line they are compared with the
89 # Commands to run on the local host to get the passwd, shadow and
90 # group data for the host in question. getpasswd must be
91 # specified if user data is to be transferred; getgroup must be
92 # specified if group data is to be transferred.
95 # Specifies that shadow file data is to be used (by default,
96 # password information is found from the output of getpasswd).
97 # The command should emit shadow data in the format specified by
98 # shadow(5) on Linux. getshadow should not be specified without
101 # Some configuration file directives specify that account data is to
102 # transferred from the current host. They should appear as the last
103 # thing(s) in a host section:
105 # user <username> [remote=<remoteusername>]
106 # Specifies that account data should be copied for local user
107 # <username> from the remote account <remoteusername> (assumed to
108 # be the same as <username> if not specified). The account
109 # password, comment field, and shell will be copied
110 # unconditionally. If sameuid is specified the uid will be
113 # users <ruidmin>-<ruidmax>
114 # Specifies that all remote users whose uid is in the given range
115 # are to be copied to corresponding local user accounts.
118 # Specifies that data for <username> is _not_ to be copied, even
119 # if subsequent user or users directives suggest that it should
122 # (A note is made when a `user', `users' or `nouser' directive is
123 # encountered for a particular account, and no subsequent directives
124 # for that account will take effect.)
127 # This directive has no effect on `sync-accounts'. However, it
128 # is used as a placeholder by `grab-account': new accounts for
129 # creation are inserted just before `addhere'.
131 # Finally, the config file must finish with:
137 $configfile= '/etc/sync-accounts';
138 $def_createuser= 'sync-accounts-createuser';
139 $ch_homebase= '/home';
141 @groupglobs= [ '.*', 0 ];
146 unlink $x or warn "unable to unlock by removing $x: $!\n";
151 $cdays= int(time/86400);
153 @envs_passwd= qw(USER x UID GID COMMENT HOME SHELL);
155 while ($ARGV[0] =~ m/^-/) {
163 die "unknown option $_\n";
167 for $h (@ARGV) { $wanthost{$h}= 1; }
169 open CF,"< $configfile" or die "$configfile: $!";
171 sub fetchfile (\%$) {
172 my ($ary_ref,$get_str) = @_;
175 open G,"$get_str" or die "$get_str: $!";
178 m/^([^:]+)\:/ or die "$ch_name: $get_str:$.: $_ ?\n";
179 $ary_ref->{$1}= [ split(/\:/,$_,-1) ];
181 close G; $? and die "$ch_name: $get_str: exit code $?\n";
184 sub fetchownfile (\@$$) {
185 my ($ary_ref,$fn_str,$lock_str) = @_;
186 die "$configfile:$.: lockfile name for $fn_str not".
187 " defined (use lockpasswd/lockgroup)\n" unless length $lock_str;
188 if ($lock_str ne '/dev/null' && !$no_act) {
189 $lock_str= "$fn_str$lock_str" unless $lock_str =~ m,^/,;
190 link $fn_str,$lock_str or die "cannot lock $fn_str by creating $lock_str: $!\n";
191 push @unlocks,$lock_str;
193 open O,"$fn_str" or die "$fn_str: $!";
196 push @$ary_ref, [ split(/\:/,$_,-1) ];
198 close O or die "$fn_str: $!";
202 print "$diagstr: $_[0]\n" or die $!;
205 sub regroupglobs () {
206 $nogroups= (@groupglobs == 1 &&
207 $groupglobs[0]->[0] eq '.*' &&
208 !$groupglobs[0]->[1]);
209 $ggfunc= "sub wantsyncgroup {\n \$_= \$_[0];\n return\n";
210 for $e (@groupglobs) { $ggfunc.= " m/^$e->[0]\$/ ? $e->[1] :\n"; }
211 $ggfunc.= " die;\n};\n1;\n";
212 #print STDERR "$ggfunc\n";
213 eval $ggfunc or die "$ggfunc // $@";
217 if (!$own_fetchedpasswd) {
218 fetchownfile(@ownpasswd,'/etc/passwd',$ch_lockpasswd);
219 if (defined stat('/etc/shadow')) {
221 $own_fetchedshadow= 1;
222 fetchownfile(@ownshadow,'/etc/shadow','/dev/null');
223 } elsif ($! == &ENOENT) {
226 die "unable to check for /etc/shadow: $!\n";
228 $own_fetchedpasswd= 1;
230 if (!$own_fetchedgroup) {
231 fetchownfile(@owngroup,'/etc/group',$ch_lockgroup);
232 $own_fetchedgroup= 1;
234 #print STDERR "fetchown() -> $#ownpasswd $#owngroup\n";
238 my ($useuid,$foruser) = @_;
239 for $e (@ownpasswd) {
240 if ($e->[0] ne $foruser && $e->[2] == $useuid) {
241 diag("uid clash with $e->[0] (uid $e->[2])");
248 sub copyfield ($$$$) {
249 my ($file,$entry,$field,$value) = @_;
250 eval "\$ary_ref= \\\@own$file; 1;" or die $@;
251 #print STDERR "copyfield($file,$entry,$field,$value)\n";
253 #print STDERR "copyfield($file,$entry,$field,$value) $e->[0] $e->[field] ".join(':',@$e)."\n";
254 next unless $e->[0] eq $entry;
255 next if $e->[$field] eq $value;
256 $e->[$field]= $value;
257 eval "\$modified$file= 1; 1;" or die $@;
262 return if $ch_fetchedpasswd;
263 die "$configfile:$.: getpasswd not specified for host $ch_name\n"
264 unless length $ch_getpasswd;
266 fetchfile(%rempasswd,"$ch_getpasswd |");
267 if (length $ch_getshadow) {
268 fetchfile(%remshadow,"$ch_getshadow |");
269 for $k (keys %rempasswd) {
270 $rempasswd{$k}->[1]= 'xx' unless length $rempasswd{$k}->[1];
272 for $k (keys %remshadow) {
273 next unless exists $rempasswd{$k};
274 $rempasswd{$k}->[1]= $remshadow{$k}->[1];
280 return if $ch_fetchedgroup;
281 die "$configfile:$.: getgroup not specified for host $ch_name\n"
282 unless length $ch_getgroup;
283 fetchfile(%remgroup,"$ch_getgroup |");
286 sub syncusergroup ($$) {
289 return 1 if !$opt_usergroups;
290 #print STDERR "syncusergroup($lu,$luid)\n";
294 $samename= $e->[0] eq $lu;
295 $sameid= $e->[2] eq $luid;
296 next unless $samename || $sameid;
297 if (!$samename || !$sameid) {
298 diag("local group $e->[0] ($e->[2]) mismatch vs. local user $lu ($luid)");
302 diag("per-user group $lu ($luid) duplicated");
308 return 1 if $ugfound;
310 if (!length $opt_createuser) {
311 diag("account creation not enabled, not creating per-user group");
314 push @owngroup, [ $lu,'x',$luid,'' ];
322 #print STDERR "syncuser($lu,$ru)\n";
323 next unless $ch_doinghost;
324 return if $doneuser{$lu}++;
325 return if !length $ru;
326 $diagstr= "user $lu from $ch_name!$ru";
329 #print STDERR "syncuser($lu,$ru) doing\n";
332 if (!$rempasswd{$ru}) { diag("no remote entry"); return; }
333 if (length $ch_getshadow && exists $remshadow{$ru} && length $remshadow{$ru}->[7]) {
334 diag("remote account disabled in shadow");
338 if (!grep($_->[0] eq $lu, @ownpasswd)) {
339 if (!length $opt_createuser) { diag("account creation not enabled"); return; }
340 if ($no_act) { diag("-n specified; not creating account"); return; }
343 $useuid= $rempasswd{$ru}->[2];
345 length $ch_uidmin or die "no uidmin specified, cannot create users";
346 length $ch_uidmax or die "no uidmax specified, cannot create users";
347 $ch_uidmin<$ch_uidmax or die "uidmin>=uidmax specified, cannot create users";
350 for $e (@ownpasswd, @owngroup) {
351 $tuid= $e->[2]; next if $tuid<$useuid || $tuid>$ch_uidmax;
352 if ($tuid==$ch_uidmax) {
353 diag("uid/gid $ch_uidmax used, cannot create users");
360 @newpwent= ($lu,'x',$useuid,$useuid,$rempasswd{$ru}->[4],
361 "$ch_homebase/$lu",$rempasswd{$ru}->[6]);
363 defined($c= open CU,"-|") or die $!;
366 defined($c2= open STDIN,"-|") or die $!;
368 print STDOUT join(':',@newpwent),"\n" or die $!;
371 for ($i=0; $i<@envs_passwd; $i++) {
372 next if $envs_passwd[$i] eq 'x';
373 $ENV{"SYNCUSER_CREATE_$envs_passwd[$i]"}= $newpwent[$i];
375 exec $opt_createuser; die "$configfile:$.: ($lu): $opt_createuser: $!\n";
378 close CU; $? and die "$configfile:$.: ($lu): $opt_createuser: code $?\n";
380 if (length $newpwent) {
381 if ($newpwent !~ m/\:/) { diag("creation script demurred"); return; }
382 @newpwent= split(/\:/,$newpwent,-1);
384 die "$opt_createuser: bad output: $_\n" if @newpwent!=7 or $newpwent[0] ne $lu;
385 checkuid($newpwent[2],$lu) or return;
386 if ($own_haveshadow) {
387 push(@ownshadow,[ $lu, $newpwent[1], $cdays, 0,99999,7,'','','' ]);
391 syncusergroup($lu,$newpwent[2]) or return;
392 push @ownpasswd,[ @newpwent ];
396 for $e (@ownpasswd) {
397 next unless $e->[0] eq $lu;
398 syncusergroup($lu,$e->[2]) or return;
401 $ruid= $rempasswd{$ru}->[2];
402 $rgid= $rempasswd{$ru}->[3];
403 if ($opt_sameuid && checkuid($ruid,$lu)) {
404 for $e (@ownpasswd) {
405 next unless $e->[0] eq $lu;
406 $luid= $e->[2]; $lgid= $e->[3];
407 die "$diagstr: local uid $luid, remote uid $ruid\n" if $luid ne $ruid;
408 die "$diagstr: local gid $lgid, remote gid $rgid\n" if $lgid ne $rgid;
412 #print STDERR "syncuser($lu,$ru) exists $own_haveshadow\n";
413 if ($own_haveshadow && grep($_->[0] eq $lu, @ownshadow)) {
414 #print STDERR "syncuser($lu,$ru) shadow $rempasswd{$ru}->[1]\n";
415 copyfield('shadow',$lu,1, $rempasswd{$ru}->[1]);
417 #print STDERR "syncuser($lu,$ru) passwd $rempasswd{$ru}->[1]\n";
418 copyfield('passwd',$lu,1, $rempasswd{$ru}->[1]);
420 copyfield('passwd',$lu,4, $rempasswd{$ru}->[4]); # comment
421 copyfield('passwd',$lu,6, $rempasswd{$ru}->[6]); # shell
426 #print STDERR "syncuser($lu,$ru) group $tgroup\n";
427 next unless &wantsyncgroup($tgroup);
428 #print STDERR "syncuser($lu,$ru) group $tgroup yes\n";
430 if (!exists $remgroup{$tgroup}) {
431 diag("group $tgroup: not on remote host");
434 $inremote= grep($_ eq $ru, split(/\,/,$remgroup{$tgroup}->[3]));
435 $cusers= $e->[3]; $inlocal= grep($_ eq $lu, split(/\,/,$cusers));
436 if ($inremote && !$inlocal) {
437 $cusers.= ',' if length $cusers;
439 } elsif ($inlocal && !$inremote) {
440 $cusers= join(',', grep($_ ne $lu, split(/\,/, $cusers)));
451 return if $bannerdone;
452 print "\n" or die $!; system 'date'; $? and die $?;
457 for $h (keys %wanthost) {
458 die "host $h not in config file\n" if $wanthost{$h};
462 for $file (qw(passwd shadow group)) {
463 eval "\$modified= \$modified$file; \$data_ref= \\\@own$file;".
464 " \$fetched= \$own_fetched$file; 1;" or die $@;
466 die $file unless $fetched;
468 $newfile= $no_act ? "$file.new" : "/etc/$file.new";
469 open NF,"> $newfile";
470 for $e (@$data_ref) {
471 print NF join(':',@$e),"\n" or die $!;
474 system "diff -U0 /etc/$file $newfile"; $?==256 or die $?;
476 if ($file eq 'shadow') {
477 system "chown root.shadow $newfile"; $? and die $?;
478 chmod 0640, $newfile or die $!;
480 chown 0,0, $newfile or die $!;
481 chmod 0644, $newfile or die $!;
483 rename $newfile, "/etc/$file" or die $!;
491 next if m/^\#/ || !m/\S/;
493 finish() if m/^end$/;
494 if (m/^host\s+(\S+)$/) {
496 $ch_getpasswd= $ch_getgroup= $ch_getshadow= '';
497 $ch_fetchedpasswd= $ch_fetchedgroup;
500 } elsif (exists $wanthost{$ch_name}) {
501 $wanthost{$ch_name}= 0;
506 } elsif (m/^(getpasswd|getshadow|getgroup)\s+(.*\S)$/) {
507 eval "\$ch_$1= \$2; 1;" or die $@;
508 } elsif (m/^(lockpasswd|lockgroup)\s+(\S+)$/) {
509 eval "\$ch_$1= \$2; 1;" or die $@;
510 } elsif (m,^(homebase)\s+(/\S+)$,) {
511 eval "\$ch_$1= \$2; 1;" or die $@;
512 } elsif (m/^(uidmin|uidmax)\s+(\d+)$/ && $2>0) {
513 eval "\$ch_$1= \$2; 1;" or die $@;
514 } elsif (m/^createuser$/) {
515 $opt_createuser= $def_createuser;
516 } elsif (m/^nocreateuser$/) {
518 } elsif (m/^createuser\s+(\S+)$/) {
520 } elsif (m/^logfile\s+(.*\S)$/) {
522 print "would log to $1\n" or die $!;
524 open STDOUT,">> $1" or die "$1: $!"; $|=1;
526 } elsif (m/^(no|)(sameuid|usergroups)$/) {
527 eval "\$opt_$2= ".($1 eq 'no' ? 0 : 1)."; 1;" or die $@;
528 } elsif (m/^(no|)group\s+([-+.0-9a-zA-Z*?]+)$/) {
529 $yes= $1 eq 'no' ? 0 : 1;
531 @groupglobs=() if $_ eq '*';
535 unshift @groupglobs, [ $_, $yes ];
537 } elsif (m/^user\s+(\S+)$/) {
539 } elsif (m/^user\s+(\S+)\s+remote\=(\S+)$/) {
541 } elsif (m/^nouser\s+(\S+)$/) {
543 } elsif (m/^users\s+(\d+)\-(\d+)$/) {
544 $tmin= $1; $tmax= $2; $except= $3;
546 for $k (keys %rempasswd) {
547 $tuid= $rempasswd{$k}->[2];
548 next if $tuid<$1 or $tuid>$2;
551 } elsif (m/^addhere$/) {
553 die "$configfile:$.: unknown directive\n";
557 die "$configfile:$.: missing \`end', or read error\n";