chiark / gitweb /
Manpages for all of sync-accounts.
[chiark-utils.git] / sync-accounts / sync-accounts
1 #!/usr/bin/perl
2 # $Id: sync-accounts,v 1.20 2002-07-14 19:29:37 ianmdlvl Exp $
3 #
4 # Copyright 1999-2000,2002 Ian Jackson <ian@davenant.greenend.org.uk>
5 # Copyright 2000-2001 nCipher Corporation Ltd
6 #
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
10 #  version.
11 #
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
15 #  for more details.
16 #
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.
20
21 use POSIX;
22
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 ];
29 regroupglobs();
30
31 $file{'passwd','std'}= 'passwd';
32 $file{'shadow','std'}= 'shadow';
33 $file{'group','std'}= 'group';
34
35 $file{'passwd','bsd'}= 'master.passwd';
36 $file{'shadow','bsd'}= 'shadow-non-existent';
37 $file{'group','bsd'}= 'group';
38
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 !
45
46 END {
47     foreach $x (@unlocks) {
48         ($fn, $style, $arg) = @$x;
49         &{ "unlock_$style" } ( $fn,$arg );
50     }
51 }
52
53 sub fields {
54     my ($pfx,@l) = @_;
55     my ($i, $v, $vn);
56     foreach $v (@l) { $vn= "${pfx}_$v"; $$vn = $i++; }
57     $vn= "${pfx}_fields"; $$vn= $i;
58 }
59
60 sub fields_fmt ($$) {
61     my ($pfx,$fmt) = @_;
62     my ($vn);
63     $vn= "fields_pw_$fmt";
64     die "unknown format $fmt\n" unless defined @$vn;
65     fields($pfx,@$vn);
66     $vn= "${pfx}_format";
67     $$vn= $fmt;
68 }
69
70 sub newentry {
71     my ($pfx,$name,@field_val_list) = @_;
72     my (@rv, $vn, $fn, $v, $i);
73     @rv= ();
74     $vn= "${pfx}_fields";
75     for ($i=0; $i<$$vn; $i++) { $rv[$i]= ''; }
76     die "@field_val_list ?" if @field_val_list % 2;
77     $rv[0] = $name;
78     while (@field_val_list) {
79         ($fn,$v,@field_val_list) = @field_val_list;
80         $vn= "${pfx}_$fn";
81 #print STDERR ">$fn|$v|$vn|$$vn<\n";
82         $rv[$$vn]= $v;
83     }
84     return @rv;
85 }
86
87 $|=1;
88 $cdays= int(time/86400);
89
90 @envs_createuser= qw(USER UID GID COMMENT HOME SHELL);
91
92 if (@ARGV == 1 && length $ENV{'SYNC_ACCOUNTS_RUNVIA_INFO'}) {
93     @na= map {
94         s/\%([0-9a-f][0-9a-f])/ pack("C", hex $1) /ge;
95         $_;
96     } split(/\:/, $ENV{'SYNC_ACCOUNTS_RUNVIA_INFO'});
97     delete $ENV{'SYNC_ACCOUNTS_RUNVIA_INFO'};
98     $ta= shift @na;
99     $ENV{"SYNC_ACCOUNTS_RUNVIA_LOCKEDGOT_$ta"} = $ARGV[0];
100     @ARGV= @na;
101 }
102
103 @orgargv= @ARGV;
104
105 while ($ARGV[0] =~ m/^-/) {
106     $_= shift @ARGV;
107     last if m/^--$/;
108     if (m/^-C/) {
109         $configfile= $';
110     } elsif (m/^-n$/) {
111         $no_act= 1;
112         $display= 0;
113     } elsif (m/^-q$/) {
114         $no_act= 1;
115         $display= 1;
116     } else {
117         die "unknown option $_\n";
118     }
119 }
120
121 die "hosts must not be specified with -q\n" if @ARGV && $display;
122
123 for $h (@ARGV) { $wanthost{$h}= 1; }    
124
125 open CF,"< $configfile" or die "$configfile: $!";
126
127 sub fetchfile (\%$) {
128     my ($ary_ref,$get_str) = @_;
129
130     undef %$ary_ref;
131     open G,"$get_str" or die "$get_str: $!";
132     while (<G>) {
133         chomp;
134         m/^([^:]+)\:/ or die "$ch_name: $get_str:$.: $_ ?\n";
135         $ary_ref->{$1}= [ split(/\:/,$_,-1) ];
136     }
137     close G; $? and die "$ch_name: $get_str: exit code $?\n";
138 }
139
140 sub lockstyle_ ($$) {
141     my ($fn,$lock) = @_;
142
143     die "$configfile:$.: locking mechanism for $fn not".
144         " defined (use lockpasswd/lockgroup)\n";
145 }
146
147 sub abslock (@) {
148     my ($fn,$lock) = @_;
149
150     $fn= "/etc/$fn";
151     $lock= "$fn$lock" unless $lock =~ m,^/,;
152     return ($fn,$lock);
153 }
154
155 sub lock_none ($$) { return (abslock(@_))[0]; }
156 sub unlock_none ($$) { }
157
158 sub lock_link ($$) {
159     my ($fn,$lock) = abslock(@_);
160     link $fn,$lock or die "cannot lock $fn by creating $lock: $!\n";
161     return $fn;
162 }
163 sub unlock_link ($$) {
164     my ($fn,$lock) = abslock(@_);
165     unlink $lock or warn "unable to unlock by removing $lock: $!\n";
166 }
167
168 sub lock_runvia ($$) {
169     my ($fn,$lock) = @_;
170     my ($evn);
171     $evn= "SYNC_ACCOUNTS_RUNVIA_LOCKEDGOT_$fn";
172     return $ENV{$evn} if exists $ENV{$evn};
173
174     @na= map {
175         s/\W/ sprintf("%%%02x", unpack("C", $&)) /ge;
176         $_;
177     } ($fn,@orgargv);
178     $ENV{'SYNC_ACCOUNTS_RUNVIA_INFO'}= join(":", @na);
179     $ENV{'EDITOR'}= $0;
180     delete $ENV{'VISUAL'};
181     exec $lock; die "cannot lock $fn by executing $lock: $!\n";
182 }
183 sub unlock_runvia ($$) { }
184
185 sub fetchownfile (\@$$$$) {
186     my ($ary_ref,$fn_str,$nfields,$style,$lock_arg) = @_;
187     my ($fn_use, $record, $fn_emsg);
188     $fn_emsg= $fn_str;
189     if (!$no_act) {
190         $fn_use= &{ "lock_$style" } ($fn_str, $lock_arg);
191         push @unlocks, [ $fn_str, $style, $lock_arg ];
192         $savebackto{$fn_str}= $fn_use;
193     } else {
194         $fn_use= $fn_emsg= "/etc/".$file{$fn_str,$PW_format};
195     }
196     open O,"$fn_use" or die "$fn_use ($fn_str): $!";
197     while (<O>) {
198         chomp;
199         $record= [ split(/\:/,$_,-1) ];
200         die "$fn_emsg:$.:wrong number of fields:\`$_'\n"
201             unless @$record == $nfields;
202         push @$ary_ref, $record;
203     }
204     close O or die "$fn_use ($fn_str): $!";
205 }
206
207 sub diag ($) {
208     print "$diagstr: $_[0]\n" or die $!;
209 }
210
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 // $@";
220 }
221
222 sub fetchown () {
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")) {
229             $own_haveshadow= 1;
230             $own_fetchedshadow= 1;
231             fetchownfile(@ownshadow,'shadow',$SP_fields,'none','');
232         } elsif ($! == &ENOENT) {
233             $own_haveshadow= 0;
234         } else {
235             die "unable to check for /etc/$shadowfile: $!\n";
236         }
237         $own_fetchedpasswd= 1;
238     }
239     if (!$own_fetchedgroup) {
240         fetchownfile(@owngroup,'group',$GR_fields,
241                      $ch_lockstyle_group, $ch_lock_group);
242         $own_fetchedgroup= 1;
243     }   
244 #print STDERR "fetchown() -> $#ownpasswd $#owngroup\n";
245 }
246
247 sub checkuid ($$) {
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])");
252             return 0;
253         }
254     }
255     return 1;
256 }
257
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";
262     for $e (@$ary_ref) {
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 $@;
268     }
269 }
270
271 sub fetchpasswd () {
272     return if $ch_fetchedpasswd;
273     die "$configfile:$.: getpasswd not specified for host $ch_name\n"
274         unless length $ch_getpasswd;
275     undef %remshadow;
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];
281         }
282         for $k (keys %remshadow) {
283             next unless exists $rempasswd{$k};
284             $rempasswd{$k}->[$REM_PW]= $remshadow{$k}->[$SP_PW];
285         }
286     }
287     $ch_fetchedpasswd= 1;
288 }
289
290 sub fetchgroup () {
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 |");
295     $ch_fetchedgroup= 1;
296 }
297
298 sub syncusergroup ($$) {
299     my ($lu,$luid) = @_;
300
301     return 1 if $defaultgid != -1;
302 #print STDERR "syncusergroup($lu,$luid)\n";
303     $ugfound=0;
304     
305     for $e (@owngroup) {
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)");
312             return 0;
313         }
314         if ($ugfound) {
315             diag("per-user group $lu ($luid) duplicated");
316             return 0;
317         }
318         $ugfound=1;
319     }
320
321     return 1 if $ugfound;
322
323     if (!length $opt_createuser) {
324         diag("account creation not enabled, not creating per-user group");
325         return 0;
326     }
327     push @owngroup, [ newentry('GR', $lu,
328                                'PW', 'x',
329                                'GID', $luid) ];
330     $modifiedgroup= 1;
331     return 1;
332 }
333
334 sub hosthead ($) {
335     my ($th) = @_;
336     return if $hostheaddone eq $th;
337     print "\n\n" or die $! if length $hostheaddone;
338     print "==== $th ====\n" or die $!;
339     $hostheaddone= $th;
340 }
341
342 sub syncuser ($$) {
343     my ($lu,$ru) = @_;
344     my ($vn);
345
346 #print STDERR "syncuser($lu,$ru)\n";
347     return if $doneuser{$lu}++;
348     next unless $ch_doinghost;
349     return if !length $ru;
350
351     fetchown();
352
353     if ($display) {
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}++;
359         }
360         return;
361     }
362     
363     $diagstr= "user $lu from $ch_name!$ru";
364
365 #print STDERR "syncuser($lu,$ru) doing\n";
366     fetchpasswd();
367
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");
372         return;
373     }
374
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; }
378
379         if ($opt_sameuid) {
380             $useuid= $rempasswd{$ru}->[$REM_UID];
381             $usegid= $rempasswd{$ru}->[$REM_GID];
382         } else {
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";
387         
388             $useuid= $ch_uidmin;
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");
393                     return;
394                 }
395                 $useuid= $tuid+1;
396             }
397             $usegid= $defaultgid==-1 ? $useuid : $defaultgid;
398         }
399         
400         @newpwent= newentry('PW', $lu,
401                             'PW', 'x',
402                             'UID', $useuid,
403                             'GID', $usegid,
404                             'COMMENT', $rempasswd{$ru}->[$REM_COMMENT],
405                             'HOME', "$ch_homebase/$lu",
406                             'SHELL', $ch_defaultshell);
407         
408         defined($c= open CU,"-|") or die $!;
409         if (!$c) {
410             @unlocks= ();
411             defined($c2= open STDIN,"-|") or die $!;
412             if (!$c2) {
413                 print STDOUT join(':',@newpwent),"\n" or die $!;
414                 exit 0;
415             }
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];
420             }
421             exec $opt_createuser; die "$configfile:$.: ($lu): $opt_createuser: $!\n";
422         }
423         $newpwent= <CU>;
424         close CU; $? and die "$configfile:$.: ($lu): $opt_createuser: code $?\n";
425         chomp $newpwent;
426         if (length $newpwent) {
427             if ($newpwent !~ m/\:/) { diag("creation script demurred"); return; }
428             @newpwent= split(/\:/,$newpwent,-1);
429         }
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,
435                                         'PW', 'x',
436                                         'DAYSCHGD', $cdays,
437                                         'DAYSFIX', 0,
438                                         'DAYSEXP', 99999,
439                                         'DAYSEXPDIS', 7) ];
440             $modifiedshadow= 1;
441         }
442         syncusergroup($lu,$newpwent[$PW_UID]) or return;
443         push @ownpasswd,[ @newpwent ];
444         $modifiedpasswd= 1;
445     }
446
447     for $e (@ownpasswd) {
448         next unless $e->[$PW_USER] eq $lu;
449         syncusergroup($lu,$e->[$PW_UID]) or return;
450     }
451
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;
460         }
461     }
462
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]);
467     } else {
468 #print STDERR "syncuser($lu,$ru) passwd $rempasswd{$ru}->[$REM_PW]\n";
469         copyfield('passwd',$lu,$PW_PW, $rempasswd{$ru}->[$REM_PW]);
470     }
471     copyfield('passwd',$lu,$PW_COMMENT, $rempasswd{$ru}->[$REM_COMMENT]);
472
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;
477
478     if (!$nogroups) {
479         for $e (@owngroup) {
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";
484             fetchgroup();
485             if (!exists $remgroup{$tgroup}) {
486                 diag("group $tgroup: not on remote host");
487                 next;
488             }
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;
493                 $cusers.= $lu;
494             } elsif ($inlocal && !$inremote) {
495                 $cusers= join(',', grep($_ ne $lu, split(/\,/, $cusers)));
496             } else {
497                 next;
498             }
499             $e->[$GR_USERS]= $cusers;
500             $modifiedgroup= 1;
501         }
502     }
503 }
504
505 sub banner () {
506     return if $bannerdone;
507     print "\n" or die $!; system 'date'; $? and die $?;
508     $bannerdone= 1;
509 }
510
511 sub finish () {
512     for $h (keys %wanthost) {
513         die "host $h not in config file\n" if $wanthost{$h};
514     }
515
516     if ($display) {
517 #print STDERR "\n\nfinish display=$display pw=$pw\n\n";
518         for $e (@ownpasswd) {
519             $tu= $e->[$PW_USER];
520             $tuid= $e->[$PW_UID];
521             next if $displaydone{$tu};
522             $tpw= $e->[$PW_PW];
523 #print STDERR ">$tu|$tpw<\n";
524             for $e2 (@ownshadow) {
525                 next unless $e2->[$SP_USER] eq $tu;
526                 $tpw= $e2->[$SP_PW]; last;
527             }
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]";
534         }
535         foreach $head (sort keys %unsynch_type) {
536             hosthead($head);
537             print $unsynch_type{$head} or die $!;
538         }
539         print "\n\n" or die $!;
540         exit 0;
541     }
542     
543     umask 077;
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 $@;
548         next if !$modified;
549         die $file unless $fetched;
550         banner();
551         if ($no_act) {
552             $newfileinst= "/etc/$realfile";
553             $newfile= "$realfile.new";
554         } else {
555             $newfileinst= $savebackto{$file};
556             $newfile= "$newfileinst.new";
557         }
558         open NF,"> $newfile" or die "$newfile: $!";
559         for $e (@$data_ref) {
560             print NF join(':',@$e),"\n" or die $!;
561         }
562         close NF or die $!;
563         system 'diff','-U0','--label',$realfile,$newfileinst,
564                             '--label',"$realfile.new",$newfile;
565         $?==256 or die $?;
566         if (!$no_act) {
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 $!;
571         }
572     }
573     exit 0;
574 }
575
576 while (<CF>) {
577     chomp;
578     s/^\s*//; s/\s*$//;
579     next if m/^\#/ || !m/\S/;
580     finish() if m/^end$/;
581     if (m/^host\s+(\S+)$/) {
582         $ch_name= $1;
583         $ch_getpasswd= $ch_getgroup= $ch_getshadow= '';
584         $ch_fetchedpasswd= $ch_fetchedgroup;
585         if (!@ARGV) {
586             $ch_doinghost= 1;
587         } elsif (exists $wanthost{$ch_name}) {
588             $wanthost{$ch_name}= 0;
589             $ch_doinghost= 1;
590         } else {
591             $ch_doinghost= 0;
592         }
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,
599                    $2);
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$/) {
611         $opt_createuser= '';
612     } elsif (m/^createuser\s+(\S+)$/) {
613         $opt_createuser= $1;
614     } elsif (m/^logfile\s+(.*\S)$/) {
615         if (!$no_act) {
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 $!;
620         }
621     } elsif (m/^(no|)(sameuid)$/) {
622         eval "\$opt_$2= ".($1 eq 'no' ? 0 : 1)."; 1;" or die $@;
623     } elsif (m/^usergroups$/) {
624         $defaultgid= -1;
625     } elsif (m/^nousergroups$/) {
626         $defaultgid= -2;
627     } elsif (m/^defaultgid\s+(\d+)$/) {
628         $defaultgid= $1;
629     } elsif (m/^(no|)group\s+([-+.0-9a-zA-Z*?]+)$/) {
630         $yes= $1 eq 'no' ? 0 : 1;
631         $_= $2;
632         @groupglobs=() if $_ eq '*';
633         s/[-+._]/\\$1/g;
634         s/\*/\.\*/g;
635         s/\?/\./g;
636         unshift @groupglobs, [ $_, $yes ];
637         regroupglobs();
638     } elsif (m/^user\s+(\S+)$/) {
639         syncuser($1,$1);
640     } elsif (m/^user\s+(\S+)\s+remote\=(\S+)$/) {
641         syncuser($1,$2);
642     } elsif (m/^nouser\s+(\S+)$/) {
643         syncuser($1,'');
644     } elsif (m/^users\s+(\d+)\-(\d+)$/) {
645         $tmin= $1; $tmax= $2; $except= $3;
646         fetchpasswd();
647         for $k (keys %rempasswd) {
648             $tuid= $rempasswd{$k}->[2];
649             next if $tuid<$1 or $tuid>$2;
650             syncuser($k,$k);
651         }
652     } elsif (m/^addhere$/) {
653     } else {
654         die "$configfile:$.: unknown directive\n";
655     }
656 }
657
658 die "$configfile:$.: missing \`end', or read error\n";