chiark / gitweb /
Portability fix: Fix on libcs where st_mtime is not a macro (ie, ones lacking st_mtim.)
[chiark-utils.git] / sync-accounts / sync-accounts
1 #!/usr/bin/perl
2 # This is part of sync-accounts, a tool for synchronising UN*X password data.
3 #
4 # sync-accounts is
5 #  Copyright 1999-2000,2002 Ian Jackson <ian@davenant.greenend.org.uk>
6 #  Copyright 2000-2001 nCipher Corporation Ltd
7 #
8 #  sync-accounts is free software; you can redistribute it and/or
9 #  modify it under the terms of the GNU General Public License as
10 #  published by the Free Software Foundation; either version 3, or (at
11 #  your option) any later version.
12 #
13 #  sync-accounts is distributed in the hope that it will be useful,
14 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 #  General Public License for more details.
17 #
18 #  You should already have a copy of the GNU General Public License.
19 #  If not, consult the Free Software Foundation's website at
20 #  www.fsf.org, or the GNU Project website at www.gnu.org.
21 #
22 # $Id: sync-accounts,v 1.25 2007-09-21 21:21:15 ianmdlvl Exp $
23
24 use POSIX;
25
26 $configfile= '/etc/sync-accounts';
27 $def_createuser= 'sync-accounts-createuser';
28 $ch_homebase= '/home';
29 $ch_defaultshell= '/bin/sh';
30 $defaultgid= -1; # -1 => usergroups; -2 => nousergroups
31 @groupglobs= [ '.*', 0 ];
32 regroupglobs();
33
34 $file{'passwd','std'}= 'passwd';
35 $file{'shadow','std'}= 'shadow';
36 $file{'group','std'}= 'group';
37
38 $file{'passwd','bsd'}= 'master.passwd';
39 $file{'shadow','bsd'}= 'shadow-non-existent';
40 $file{'group','bsd'}= 'group';
41
42 @fields_pw_std= qw(USER PW UID GID COMMENT HOME SHELL);
43 @fields_pw_bsd= qw(USER PW UID GID CLASS CHANGE EXPIRE COMMENT HOME SHELL);
44 fields_fmt('PW','std');
45 fields('GR',qw(GROUP PW GID USERS));
46 fields('SP',qw(USER PW DAYSCHGD DAYSFIX DAYSEXP DAYSWARN DAYSEXPDIS DAYSDISD RESERVED));
47 # The name field had better always be field 0 !
48
49 END {
50     foreach $x (@unlocks) {
51         ($fn, $style, $arg) = @$x;
52         &{ "unlock_$style" } ( $fn,$arg );
53     }
54 }
55
56 sub fields {
57     my ($pfx,@l) = @_;
58     my ($i, $v, $vn);
59     foreach $v (@l) { $vn= "${pfx}_$v"; $$vn = $i++; }
60     $vn= "${pfx}_fields"; $$vn= $i;
61 }
62
63 sub fields_fmt ($$) {
64     my ($pfx,$fmt) = @_;
65     my ($vn);
66     $vn= "fields_pw_$fmt";
67     die "unknown format $fmt\n" unless defined @$vn;
68     fields($pfx,@$vn);
69     $vn= "${pfx}_format";
70     $$vn= $fmt;
71 }
72
73 sub newentry {
74     my ($pfx,$name,@field_val_list) = @_;
75     my (@rv, $vn, $fn, $v, $i);
76     @rv= ();
77     $vn= "${pfx}_fields";
78     for ($i=0; $i<$$vn; $i++) { $rv[$i]= ''; }
79     die "@field_val_list ?" if @field_val_list % 2;
80     $rv[0] = $name;
81     while (@field_val_list) {
82         ($fn,$v,@field_val_list) = @field_val_list;
83         $vn= "${pfx}_$fn";
84 #print STDERR ">$fn|$v|$vn|$$vn<\n";
85         $rv[$$vn]= $v;
86     }
87     return @rv;
88 }
89
90 $|=1;
91 $cdays= int(time/86400);
92
93 @envs_createuser= qw(USER UID GID COMMENT HOME SHELL);
94
95 if (@ARGV == 1 && length $ENV{'SYNC_ACCOUNTS_RUNVIA_INFO'}) {
96     @na= map {
97         s/\%([0-9a-f][0-9a-f])/ pack("C", hex $1) /ge;
98         $_;
99     } split(/\:/, $ENV{'SYNC_ACCOUNTS_RUNVIA_INFO'});
100     delete $ENV{'SYNC_ACCOUNTS_RUNVIA_INFO'};
101     $ta= shift @na;
102     $ENV{"SYNC_ACCOUNTS_RUNVIA_LOCKEDGOT_$ta"} = $ARGV[0];
103     @ARGV= @na;
104 }
105
106 @orgargv= @ARGV;
107
108 while ($ARGV[0] =~ m/^-/) {
109     $_= shift @ARGV;
110     last if m/^--$/;
111     if (m/^-C/) {
112         $configfile= $';
113     } elsif (m/^-n$/) {
114         $no_act= 1;
115         $display= 0;
116     } elsif (m/^-q$/) {
117         $no_act= 1;
118         $display= 1;
119     } else {
120         die "unknown option $_\n";
121     }
122 }
123
124 die "hosts must not be specified with -q\n" if @ARGV && $display;
125
126 for $h (@ARGV) { $wanthost{$h}= 1; }    
127
128 open CF,"< $configfile" or die "$configfile: $!";
129
130 sub fetchfile (\%$) {
131     my ($ary_ref,$get_str) = @_;
132
133     undef %$ary_ref;
134     open G,"$get_str" or die "$get_str: $!";
135     while (<G>) {
136         chomp;
137         m/^([^:]+)\:/ or die "$ch_name: $get_str:$.: $_ ?\n";
138         $ary_ref->{$1}= [ split(/\:/,$_,-1) ];
139     }
140     close G; $? and die "$ch_name: $get_str: exit code $?\n";
141 }
142
143 sub lockstyle_ ($$) {
144     my ($fn,$lock) = @_;
145
146     die "$configfile:$.: locking mechanism for $fn not".
147         " defined (use lockpasswd/lockgroup)\n";
148 }
149
150 sub abslock (@) {
151     my ($fn,$lock) = @_;
152
153     $fn= "/etc/$fn";
154     $lock= "$fn$lock" unless $lock =~ m,^/,;
155     return ($fn,$lock);
156 }
157
158 sub lock_none ($$) { return (abslock(@_))[0]; }
159 sub unlock_none ($$) { }
160
161 sub lock_link ($$) {
162     my ($fn,$lock) = abslock(@_);
163     link $fn,$lock or die "cannot lock $fn by creating $lock: $!\n";
164     return $fn;
165 }
166 sub unlock_link ($$) {
167     my ($fn,$lock) = abslock(@_);
168     unlink $lock or warn "unable to unlock by removing $lock: $!\n";
169 }
170
171 sub lock_runvia ($$) {
172     my ($fn,$lock) = @_;
173     my ($evn);
174     $evn= "SYNC_ACCOUNTS_RUNVIA_LOCKEDGOT_$fn";
175     return $ENV{$evn} if exists $ENV{$evn};
176
177     @na= map {
178         s/\W/ sprintf("%%%02x", unpack("C", $&)) /ge;
179         $_;
180     } ($fn,@orgargv);
181     $ENV{'SYNC_ACCOUNTS_RUNVIA_INFO'}= join(":", @na);
182     $ENV{'EDITOR'}= $0;
183     delete $ENV{'VISUAL'};
184     exec $lock; die "cannot lock $fn by executing $lock: $!\n";
185 }
186 sub unlock_runvia ($$) { }
187
188 sub fetchownfile (\@$$$$) {
189     my ($ary_ref,$fn_str,$nfields,$style,$lock_arg) = @_;
190     my ($fn_use, $record, $fn_emsg);
191     $fn_emsg= $fn_str;
192     if (!$no_act) {
193         $fn_use= &{ "lock_$style" } ($fn_str, $lock_arg);
194         push @unlocks, [ $fn_str, $style, $lock_arg ];
195         $savebackto{$fn_str}= $fn_use;
196     } else {
197         $fn_use= $fn_emsg= "/etc/".$file{$fn_str,$PW_format};
198     }
199     open O,"$fn_use" or die "$fn_use ($fn_str): $!";
200     while (<O>) {
201         chomp;
202         if (m/^\#/ || !m/\S/) {
203             $record= $_;
204         } else {
205             $record= [ split(/\:/,$_,-1) ];
206             die "$fn_emsg:$.:wrong number of fields:\`$_'\n"
207                 unless @$record == $nfields;
208         }
209         push @$ary_ref, $record;
210     }
211     close O or die "$fn_use ($fn_str): $!";
212 }
213
214 sub diag ($) {
215     print "$diagstr: $_[0]\n" or die $!;
216 }
217
218 sub regroupglobs () {
219     $nogroups= (@groupglobs == 1 &&
220                 $groupglobs[0]->[0] eq '.*' &&
221                 !$groupglobs[0]->[1]);
222     $ggfunc= "sub wantsyncgroup {\n  \$_= \$_[0];\n  return\n";
223     for $g (@groupglobs) { $ggfunc.= "    m/^$g->[0]\$/ ? $g->[1] :\n"; }
224     $ggfunc.= "    die;\n};\n1;\n";
225 #print STDERR "$ggfunc\n";
226     must_eval($ggfunc);
227 }
228
229 sub fetchown () {
230     if (!$own_fetchedpasswd) {
231 #print STDERR ">$PW_fields<\n";
232         fetchownfile(@ownpasswd,'passwd',
233                      $PW_fields, $ch_lockstyle_passwd, $ch_lock_passwd);
234         $shadowfile= $file{'shadow',$PW_format};
235         if (stat("/etc/$shadowfile")) {
236             $own_haveshadow= 1;
237             $own_fetchedshadow= 1;
238             fetchownfile(@ownshadow,'shadow',$SP_fields,'none','');
239         } elsif ($! == &ENOENT) {
240             $own_haveshadow= 0;
241         } else {
242             die "unable to check for /etc/$shadowfile: $!\n";
243         }
244         $own_fetchedpasswd= 1;
245     }
246     if (!$own_fetchedgroup) {
247         fetchownfile(@owngroup,'group',$GR_fields,
248                      $ch_lockstyle_group, $ch_lock_group);
249         $own_fetchedgroup= 1;
250     }   
251 #print STDERR "fetchown() -> $#ownpasswd $#owngroup\n";
252 }
253
254 sub checkuid ($$) {
255     my ($useuid,$foruser) = @_;
256     for $e (@ownpasswd) {
257         next unless ref $e;
258         if ($e->[$PW_USER] ne $foruser && $e->[$PW_UID] == $useuid) {
259             diag("uid clash with $e->[$PW_USER] (uid $e->[$PW_UID])");
260             return 0;
261         }
262     }
263     return 1;
264 }
265
266 sub must_eval ($) {
267     eval $_[0] or die "$_[0] // $@";
268 }
269
270 sub copyfield ($$$$) {
271     my ($file,$entry,$field,$value) = @_;
272     must_eval("\$ary_ref= \\\@own$file; 1;");
273 #print STDERR "copyfield($file,$entry,$field,$value)\n";
274     for $e (@$ary_ref) {
275 #print STDERR "copyfield($file,$entry,$field,$value) $e->[0] $e->[field] ".join(':',@$e)."\n";
276         next unless $e->[0] eq $entry;
277         next if $e->[$field] eq $value;
278         $e->[$field]= $value;
279         must_eval("\$modified$file= 1; 1;");
280     }
281 }
282
283 sub fetchpasswd () {
284     return if $ch_fetchedpasswd;
285     die "$configfile:$.: getpasswd not specified for host $ch_name\n"
286         unless length $ch_getpasswd;
287     undef %remshadow;
288     fetchfile(%rempasswd,"$ch_getpasswd |");
289     if (length $ch_getshadow) {
290         fetchfile(%remshadow,"$ch_getshadow |");
291         for $k (keys %rempasswd) {
292             $rempasswd{$k}->[$REM_PW]= 'xx' unless length $rempasswd{$k}->[$REM_PW];
293         }
294         for $k (keys %remshadow) {
295             next unless exists $rempasswd{$k};
296             $rempasswd{$k}->[$REM_PW]= $remshadow{$k}->[$SP_PW];
297         }
298     }
299     $ch_fetchedpasswd= 1;
300 }
301
302 sub fetchgroup () {
303     return if $ch_fetchedgroup;
304     die "$configfile:$.: getgroup not specified for host $ch_name\n"
305         unless length $ch_getgroup;
306     fetchfile(%remgroup,"$ch_getgroup |");
307     $ch_fetchedgroup= 1;
308 }
309
310 sub syncusergroup ($$) {
311     my ($lu,$luid) = @_;
312
313     return 1 if $defaultgid != -1;
314 #print STDERR "syncusergroup($lu,$luid)\n";
315     $ugfound=0;
316     
317     for $e (@owngroup) {
318         next unless ref $e;
319         $samename= $e->[$GR_GROUP] eq $lu;
320         $sameid= $e->[$GR_GID] eq $luid;
321         next unless $samename || $sameid;
322         if (!$samename || !$sameid) {
323             diag("local group $e->[$GR_GROUP] ($e->[$GR_GID]) mismatch vs.".
324                  " local user $lu ($luid)");
325             return 0;
326         }
327         if ($ugfound) {
328             diag("per-user group $lu ($luid) duplicated");
329             return 0;
330         }
331         $ugfound=1;
332     }
333
334     return 1 if $ugfound;
335
336     if (!length $opt_createuser) {
337         diag("account creation not enabled, not creating per-user group");
338         return 0;
339     }
340     push @owngroup, [ newentry('GR', $lu,
341                                'PW', 'x',
342                                'GID', $luid) ];
343     $modifiedgroup= 1;
344     return 1;
345 }
346
347 sub hosthead ($) {
348     my ($th) = @_;
349     return if $hostheaddone eq $th;
350     print "\n\n" or die $! if length $hostheaddone;
351     print "==== $th ====\n" or die $!;
352     $hostheaddone= $th;
353 }
354
355 sub syncuser ($$) {
356     my ($lu,$ru) = @_;
357     my ($vn);
358
359 #print STDERR "syncuser($lu,$ru)\n";
360     return if $doneuser{$lu}++;
361     next unless $ch_doinghost;
362     return if !length $ru;
363
364     fetchown();
365
366     if ($display) {
367         for $e (@ownpasswd) {
368             next unless ref $e && $e->[$PW_USER] eq $lu;
369             hosthead("from $ch_name");
370             print ($lu eq $ru ? " $lu" : " $lu($ru)") or die $!;
371             print "<DUPLICATE>" if $displaydone{$lu}++;
372         }
373         return;
374     }
375     
376     $diagstr= "user $lu from $ch_name!$ru";
377
378 #print STDERR "syncuser($lu,$ru) doing\n";
379     fetchpasswd();
380
381     if (!$rempasswd{$ru}) { diag("no remote entry"); return; }
382     if (length $ch_getshadow && exists $remshadow{$ru} &&
383         length $remshadow{$ru}->[$SP_DAYSDISD]) {
384         diag("remote account disabled in shadow");
385         return;
386     }
387
388     if (!grep(ref $_ && $_->[$PW_USER] eq $lu, @ownpasswd)) {
389         if (!length $opt_createuser) { diag("account creation not enabled"); return; }
390         if ($no_act) { diag("-n specified; not creating account"); return; }
391
392         if ($opt_sameuid) {
393             $useuid= $rempasswd{$ru}->[$REM_UID];
394             $usegid= $rempasswd{$ru}->[$REM_GID];
395         } else {
396             die "nousergroups specified, cannot create users\n" if $defaultgid==-2;
397             length $ch_uidmin or die "no uidmin specified, cannot create users\n";
398             length $ch_uidmax or die "no uidmax specified, cannot create users\n";
399             $ch_uidmin<$ch_uidmax or die "uidmin>=uidmax, cannot create users\n";
400         
401             $useuid= $ch_uidmin;
402             for $e ($defaultgid==-1 ? (@ownpasswd, @owngroup) : (@ownpasswd)) {
403                 next unless ref $e;
404                 $tuid= $e->[$PW_UID]; next if $tuid<$useuid || $tuid>$ch_uidmax;
405                 if ($tuid==$ch_uidmax) {
406                     diag("uid (or gid?) $ch_uidmax used, cannot create users");
407                     return;
408                 }
409                 $useuid= $tuid+1;
410             }
411             $usegid= $defaultgid==-1 ? $useuid : $defaultgid;
412         }
413         
414         @newpwent= newentry('PW', $lu,
415                             'PW', 'x',
416                             'UID', $useuid,
417                             'GID', $usegid,
418                             'COMMENT', $rempasswd{$ru}->[$REM_COMMENT],
419                             'HOME', "$ch_homebase/$lu",
420                             'SHELL', $ch_defaultshell);
421         
422         defined($c= open CU,"-|") or die $!;
423         if (!$c) {
424             @unlocks= ();
425             defined($c2= open STDIN,"-|") or die $!;
426             if (!$c2) {
427                 print STDOUT join(':',@newpwent),"\n" or die $!;
428                 exit 0;
429             }
430             for ($i=0; $i<@envs_createuser; $i++) {
431                 $vn= "PW_$envs_createuser[$i]";
432 #print STDERR ">$i|$vn|$$vn|$newpwent[$$vn]<\n";
433                 $ENV{"SYNCUSER_CREATE_$envs_createuser[$i]"}= $newpwent[$$vn];
434             }
435             exec $opt_createuser; die "$configfile:$.: ($lu): $opt_createuser: $!\n";
436         }
437         $newpwent= <CU>;
438         close CU; $? and die "$configfile:$.: ($lu): $opt_createuser: code $?\n";
439         chomp $newpwent;
440         if (length $newpwent) {
441             if ($newpwent !~ m/\:/) { diag("creation script demurred"); return; }
442             @newpwent= split(/\:/,$newpwent,-1);
443         }
444         die "$opt_createuser: bad result: \`".join(':',@newpwent)."\'\n"
445             if @newpwent != $PW_fields or $newpwent[$PW_USER] ne $lu;
446         checkuid($newpwent[$PW_UID],$lu) or return;
447         if ($own_haveshadow) {
448             push @ownshadow, [ newentry('SP', $lu,
449                                         'PW', 'x',
450                                         'DAYSCHGD', $cdays,
451                                         'DAYSFIX', 0,
452                                         'DAYSEXP', 99999,
453                                         'DAYSEXPDIS', 7) ];
454             $modifiedshadow= 1;
455         }
456         syncusergroup($lu,$newpwent[$PW_UID]) or return;
457         push @ownpasswd,[ @newpwent ];
458         $modifiedpasswd= 1;
459     }
460
461     for $e (@ownpasswd) {
462         next unless ref $e && $e->[$PW_USER] eq $lu;
463         syncusergroup($lu,$e->[$PW_UID]) or return;
464     }
465
466     $ruid= $rempasswd{$ru}->[$REM_UID];
467     $rgid= $rempasswd{$ru}->[$REM_GID];
468     if ($opt_sameuid && checkuid($ruid,$lu)) {
469         for $e (@ownpasswd) {
470             next unless ref $e && $e->[$PW_USER] eq $lu;
471             $luid= $e->[$PW_UID]; $lgid= $e->[$PW_GID];
472             die "$diagstr: local uid $luid, remote uid $ruid\n" if $luid ne $ruid;
473             die "$diagstr: local gid $lgid, remote gid $rgid\n" if $lgid ne $rgid;
474         }
475     }
476
477 #print STDERR "syncuser($lu,$ru) exists $own_haveshadow\n";
478     if ($own_haveshadow && grep(ref $_ && $_->[$PW_USER] eq $lu, @ownshadow)) {
479 #print STDERR "syncuser($lu,$ru) shadow $rempasswd{$ru}->[$REM_PW]\n";
480         copyfield('shadow',$lu,$SP_PW, $rempasswd{$ru}->[$REM_PW]);
481     } else {
482 #print STDERR "syncuser($lu,$ru) passwd $rempasswd{$ru}->[$REM_PW]\n";
483         copyfield('passwd',$lu,$PW_PW, $rempasswd{$ru}->[$REM_PW]);
484     }
485     copyfield('passwd',$lu,$PW_COMMENT, $rempasswd{$ru}->[$REM_COMMENT]);
486
487     $newsh= $rempasswd{$ru}->[$REM_SHELL];
488     $oksh= $checkedshell{$newsh};
489     if (!length $oksh) { $checkedshell{$newsh}= $oksh= (-x $newsh) ? 1 : 0; }
490     copyfield('passwd',$lu,$PW_SHELL, $newsh) if $oksh;
491
492     if (!$nogroups) {
493         for $e (@owngroup) {
494             next unless ref $e;
495             $tgroup= $e->[$GR_GROUP];
496 #print STDERR "syncuser($lu,$ru) group $tgroup\n";
497             next unless &wantsyncgroup($tgroup);
498 #print STDERR "syncuser($lu,$ru) group $tgroup yes\n";
499             fetchgroup();
500             if (!exists $remgroup{$tgroup}) {
501                 diag("group $tgroup: not on remote host");
502                 next;
503             }
504             $inremote= grep($_ eq $ru, split(/\,/,$remgroup{$tgroup}->[$GR_USERS]));
505             $cusers= $e->[$GR_USERS]; $inlocal= grep($_ eq $lu, split(/\,/,$cusers));
506             if ($inremote && !$inlocal) {
507                 $cusers.= ',' if length $cusers;
508                 $cusers.= $lu;
509             } elsif ($inlocal && !$inremote) {
510                 $cusers= join(',', grep($_ ne $lu, split(/\,/, $cusers)));
511             } else {
512                 next;
513             }
514             $e->[$GR_USERS]= $cusers;
515             $modifiedgroup= 1;
516         }
517     }
518 }
519
520 sub banner () {
521     return if $bannerdone;
522     print "\n" or die $!; system 'date'; $? and die $?;
523     $bannerdone= 1;
524 }
525
526 sub finish () {
527     my ($record);
528
529     for $h (keys %wanthost) {
530         die "host $h not in config file\n" if $wanthost{$h};
531     }
532
533     if ($display) {
534 #print STDERR "\n\nfinish display=$display pw=$pw\n\n";
535         for $e (@ownpasswd) {
536             next unless ref $e;
537             $tu= $e->[$PW_USER];
538             $tuid= $e->[$PW_UID];
539             next if $displaydone{$tu};
540             $tpw= $e->[$PW_PW];
541 #print STDERR ">$tu|$tpw<\n";
542             for $e2 (@ownshadow) {
543                 next unless ref $e2 && $e2->[$SP_USER] eq $tu;
544                 $tpw= $e2->[$SP_PW]; last;
545             }
546             $tpw= length($tpw)>=13 ? 1 : length($tpw) ? -1 : 0;
547             $head= ($tpw == 1 ? "unsynched" :
548                     $tpw == 0 ? "unsynched, passwordless" :
549                     "unsynched, no-logins").
550                     ($tuid < 100 ? " system account" : " normal user");
551             $unsynch_type{$head} .= " $e->[$PW_USER]";
552         }
553         foreach $head (sort keys %unsynch_type) {
554             hosthead($head);
555             print $unsynch_type{$head} or die $!;
556         }
557         print "\n\n" or die $!;
558         exit 0;
559     }
560     
561     umask 077;
562     for $file (qw(passwd shadow group)) {
563         $realfile= $file{$file,$PW_format};
564         must_eval("\$modified= \$modified$file; \$data_ref= \\\@own$file;".
565                   " \$fetched= \$own_fetched$file; 1;");
566         next if !$modified;
567         die $file unless $fetched;
568         banner();
569         if ($no_act) {
570             $newfileinst= "/etc/$realfile";
571             $newfile= "$realfile.new";
572         } else {
573             $newfileinst= $savebackto{$file};
574             $newfile= "$newfileinst.new";
575         }
576         open NF,"> $newfile" or die "$newfile: $!";
577         for $e (@$data_ref) {
578             $record= ref $e ? join(':',@$e) : $e;
579             print NF $record,"\n" or die $!;
580         }
581         close NF or die $!;
582         system 'diff','-U0','--label',$realfile,$newfileinst,
583                             '--label',"$realfile.new",$newfile;
584         $?==256 or die $?;
585         if (!$no_act) {
586             (@stats= stat $newfileinst) or die "$file: $!";
587             chown $stats[4],$stats[5], $newfile or die $!;
588             chmod $stats[2] & 07777, $newfile or die $!;
589             rename $newfile, $newfileinst or die $!;
590         }
591     }
592     exit 0;
593 }
594
595 while (<CF>) {
596     chomp;
597     s/^\s*//; s/\s*$//;
598     next if m/^\#/ || !m/\S/;
599     finish() if m/^end$/;
600     if (m/^host\s+(\S+)$/) {
601         $ch_name= $1;
602         $ch_getpasswd= $ch_getgroup= $ch_getshadow= '';
603         $ch_fetchedpasswd= $ch_fetchedgroup;
604         if (!@ARGV) {
605             $ch_doinghost= 1;
606         } elsif (exists $wanthost{$ch_name}) {
607             $wanthost{$ch_name}= 0;
608             $ch_doinghost= 1;
609         } else {
610             $ch_doinghost= 0;
611         }
612         fields_fmt('REM','std');
613     } elsif (m/^(getpasswd|getshadow|getgroup)\s+(.*\S)$/) {
614         must_eval("\$ch_$1= \$2; 1;");
615     } elsif (m/^(local|remote)format\s+(\w+)$/) {
616         fields_fmt($1 eq 'local' ? 'PW' :
617                    $1 eq 'remote' ? 'REM' : die,
618                    $2);
619     } elsif (m/^lock(passwd|group)\s+(runvia|link)\s+(\S+)$/) {
620         must_eval("\$ch_lock_$1= \$3; \$ch_lockstyle_$1= \$2; 1;");
621     } elsif (m/^lock(passwd|group)\s+(none)$/) {
622         must_eval("\$ch_lockstyle_$1= \$2; 1;");
623     } elsif (m,^(homebase|defaultshell)\s+(/\S+)$,) {
624         must_eval("\$ch_$1= \$2; 1;");
625     } elsif (m/^(uidmin|uidmax)\s+(\d+)$/ && $2>0) {
626         must_eval("\$ch_$1= \$2; 1;");
627     } elsif (m/^createuser$/) {
628         $opt_createuser= $def_createuser;
629     } elsif (m/^nocreateuser$/) {
630         $opt_createuser= '';
631     } elsif (m/^createuser\s+(\S+)$/) {
632         $opt_createuser= $1;
633     } elsif (m/^logfile\s+(.*\S)$/) {
634         if (!$no_act) {
635             open STDOUT,">> $1" or die "$1: $!"; $|=1;
636             $!=0; system 'date'; $? and die "date: $! $?\n";
637         } elsif (!$display) {
638             print "would log to $1\n" or die $!;
639         }
640     } elsif (m/^(no|)(sameuid)$/) {
641         must_eval("\$opt_$2= ".($1 eq 'no' ? 0 : 1)."; 1;");
642     } elsif (m/^usergroups$/) {
643         $defaultgid= -1;
644     } elsif (m/^nousergroups$/) {
645         $defaultgid= -2;
646     } elsif (m/^defaultgid\s+(\d+)$/) {
647         $defaultgid= $1;
648     } elsif (m/^(no|)group\s+([-+.0-9a-zA-Z*?]+)$/) {
649         $yes= $1 eq 'no' ? 0 : 1;
650         $_= $2;
651         @groupglobs=() if $_ eq '*';
652         s/[-+._]/\\$&/g;
653         s/\*/\.\*/g;
654         s/\?/\./g;
655         unshift @groupglobs, [ $_, $yes ];
656         regroupglobs();
657     } elsif (m/^user\s+(\S+)$/) {
658         syncuser($1,$1);
659     } elsif (m/^user\s+(\S+)\s+remote\=(\S+)$/) {
660         syncuser($1,$2);
661     } elsif (m/^nouser\s+(\S+)$/) {
662         syncuser($1,'');
663     } elsif (m/^users\s+(\d+)\-(\d+)$/) {
664         $tmin= $1; $tmax= $2; $except= $3;
665         fetchpasswd();
666         for $k (keys %rempasswd) {
667             $tuid= $rempasswd{$k}->[2];
668             next if $tuid<$1 or $tuid>$2;
669             syncuser($k,$k);
670         }
671     } elsif (m/^addhere$/) {
672     } else {
673         die "$configfile:$.: unknown directive\n";
674     }
675 }
676
677 die "$configfile:$.: missing \`end', or read error\n";