chiark / gitweb /
949ebec1718e1f5702835ff327bb3152105f23ba
[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 2, 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, write to the Free Software Foundation, Inc., 59 Temple
20 #  Place - Suite 330, Boston, MA 02111-1307, USA.
21 #
22 # $Id: sync-accounts,v 1.21 2002-07-14 19:42:38 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         $record= [ split(/\:/,$_,-1) ];
203         die "$fn_emsg:$.:wrong number of fields:\`$_'\n"
204             unless @$record == $nfields;
205         push @$ary_ref, $record;
206     }
207     close O or die "$fn_use ($fn_str): $!";
208 }
209
210 sub diag ($) {
211     print "$diagstr: $_[0]\n" or die $!;
212 }
213
214 sub regroupglobs () {
215     $nogroups= (@groupglobs == 1 &&
216                 $groupglobs[0]->[0] eq '.*' &&
217                 !$groupglobs[0]->[1]);
218     $ggfunc= "sub wantsyncgroup {\n  \$_= \$_[0];\n  return\n";
219     for $g (@groupglobs) { $ggfunc.= "    m/^$g->[0]\$/ ? $g->[1] :\n"; }
220     $ggfunc.= "    die;\n};\n1;\n";
221 #print STDERR "$ggfunc\n";
222     eval $ggfunc or die "$ggfunc // $@";
223 }
224
225 sub fetchown () {
226     if (!$own_fetchedpasswd) {
227 #print STDERR ">$PW_fields<\n";
228         fetchownfile(@ownpasswd,'passwd',
229                      $PW_fields, $ch_lockstyle_passwd, $ch_lock_passwd);
230         $shadowfile= $file{'shadow',$PW_format};
231         if (stat("/etc/$shadowfile")) {
232             $own_haveshadow= 1;
233             $own_fetchedshadow= 1;
234             fetchownfile(@ownshadow,'shadow',$SP_fields,'none','');
235         } elsif ($! == &ENOENT) {
236             $own_haveshadow= 0;
237         } else {
238             die "unable to check for /etc/$shadowfile: $!\n";
239         }
240         $own_fetchedpasswd= 1;
241     }
242     if (!$own_fetchedgroup) {
243         fetchownfile(@owngroup,'group',$GR_fields,
244                      $ch_lockstyle_group, $ch_lock_group);
245         $own_fetchedgroup= 1;
246     }   
247 #print STDERR "fetchown() -> $#ownpasswd $#owngroup\n";
248 }
249
250 sub checkuid ($$) {
251     my ($useuid,$foruser) = @_;
252     for $e (@ownpasswd) {
253         if ($e->[$PW_USER] ne $foruser && $e->[$PW_UID] == $useuid) {
254             diag("uid clash with $e->[$PW_USER] (uid $e->[$PW_UID])");
255             return 0;
256         }
257     }
258     return 1;
259 }
260
261 sub copyfield ($$$$) {
262     my ($file,$entry,$field,$value) = @_;
263     eval "\$ary_ref= \\\@own$file; 1;" or die $@;
264 #print STDERR "copyfield($file,$entry,$field,$value)\n";
265     for $e (@$ary_ref) {
266 #print STDERR "copyfield($file,$entry,$field,$value) $e->[0] $e->[field] ".join(':',@$e)."\n";
267         next unless $e->[0] eq $entry;
268         next if $e->[$field] eq $value;
269         $e->[$field]= $value;
270         eval "\$modified$file= 1; 1;" or die $@;
271     }
272 }
273
274 sub fetchpasswd () {
275     return if $ch_fetchedpasswd;
276     die "$configfile:$.: getpasswd not specified for host $ch_name\n"
277         unless length $ch_getpasswd;
278     undef %remshadow;
279     fetchfile(%rempasswd,"$ch_getpasswd |");
280     if (length $ch_getshadow) {
281         fetchfile(%remshadow,"$ch_getshadow |");
282         for $k (keys %rempasswd) {
283             $rempasswd{$k}->[$REM_PW]= 'xx' unless length $rempasswd{$k}->[$REM_PW];
284         }
285         for $k (keys %remshadow) {
286             next unless exists $rempasswd{$k};
287             $rempasswd{$k}->[$REM_PW]= $remshadow{$k}->[$SP_PW];
288         }
289     }
290     $ch_fetchedpasswd= 1;
291 }
292
293 sub fetchgroup () {
294     return if $ch_fetchedgroup;
295     die "$configfile:$.: getgroup not specified for host $ch_name\n"
296         unless length $ch_getgroup;
297     fetchfile(%remgroup,"$ch_getgroup |");
298     $ch_fetchedgroup= 1;
299 }
300
301 sub syncusergroup ($$) {
302     my ($lu,$luid) = @_;
303
304     return 1 if $defaultgid != -1;
305 #print STDERR "syncusergroup($lu,$luid)\n";
306     $ugfound=0;
307     
308     for $e (@owngroup) {
309         $samename= $e->[$GR_GROUP] eq $lu;
310         $sameid= $e->[$GR_GID] eq $luid;
311         next unless $samename || $sameid;
312         if (!$samename || !$sameid) {
313             diag("local group $e->[$GR_GROUP] ($e->[$GR_GID]) mismatch vs.".
314                  " local user $lu ($luid)");
315             return 0;
316         }
317         if ($ugfound) {
318             diag("per-user group $lu ($luid) duplicated");
319             return 0;
320         }
321         $ugfound=1;
322     }
323
324     return 1 if $ugfound;
325
326     if (!length $opt_createuser) {
327         diag("account creation not enabled, not creating per-user group");
328         return 0;
329     }
330     push @owngroup, [ newentry('GR', $lu,
331                                'PW', 'x',
332                                'GID', $luid) ];
333     $modifiedgroup= 1;
334     return 1;
335 }
336
337 sub hosthead ($) {
338     my ($th) = @_;
339     return if $hostheaddone eq $th;
340     print "\n\n" or die $! if length $hostheaddone;
341     print "==== $th ====\n" or die $!;
342     $hostheaddone= $th;
343 }
344
345 sub syncuser ($$) {
346     my ($lu,$ru) = @_;
347     my ($vn);
348
349 #print STDERR "syncuser($lu,$ru)\n";
350     return if $doneuser{$lu}++;
351     next unless $ch_doinghost;
352     return if !length $ru;
353
354     fetchown();
355
356     if ($display) {
357         for $e (@ownpasswd) {
358             next unless $e->[$PW_USER] eq $lu;
359             hosthead("from $ch_name");
360             print ($lu eq $ru ? " $lu" : " $lu($ru)") or die $!;
361             print "<DUPLICATE>" if $displaydone{$lu}++;
362         }
363         return;
364     }
365     
366     $diagstr= "user $lu from $ch_name!$ru";
367
368 #print STDERR "syncuser($lu,$ru) doing\n";
369     fetchpasswd();
370
371     if (!$rempasswd{$ru}) { diag("no remote entry"); return; }
372     if (length $ch_getshadow && exists $remshadow{$ru} &&
373         length $remshadow{$ru}->[$SP_DAYSDISD]) {
374         diag("remote account disabled in shadow");
375         return;
376     }
377
378     if (!grep($_->[$PW_USER] eq $lu, @ownpasswd)) {
379         if (!length $opt_createuser) { diag("account creation not enabled"); return; }
380         if ($no_act) { diag("-n specified; not creating account"); return; }
381
382         if ($opt_sameuid) {
383             $useuid= $rempasswd{$ru}->[$REM_UID];
384             $usegid= $rempasswd{$ru}->[$REM_GID];
385         } else {
386             die "nousergroups specified, cannot create users\n" if $defaultgid==-2;
387             length $ch_uidmin or die "no uidmin specified, cannot create users\n";
388             length $ch_uidmax or die "no uidmax specified, cannot create users\n";
389             $ch_uidmin<$ch_uidmax or die "uidmin>=uidmax, cannot create users\n";
390         
391             $useuid= $ch_uidmin;
392             for $e ($defaultgid==-1 ? (@ownpasswd, @owngroup) : (@ownpasswd)) {
393                 $tuid= $e->[$PW_UID]; next if $tuid<$useuid || $tuid>$ch_uidmax;
394                 if ($tuid==$ch_uidmax) {
395                     diag("uid (or gid?) $ch_uidmax used, cannot create users");
396                     return;
397                 }
398                 $useuid= $tuid+1;
399             }
400             $usegid= $defaultgid==-1 ? $useuid : $defaultgid;
401         }
402         
403         @newpwent= newentry('PW', $lu,
404                             'PW', 'x',
405                             'UID', $useuid,
406                             'GID', $usegid,
407                             'COMMENT', $rempasswd{$ru}->[$REM_COMMENT],
408                             'HOME', "$ch_homebase/$lu",
409                             'SHELL', $ch_defaultshell);
410         
411         defined($c= open CU,"-|") or die $!;
412         if (!$c) {
413             @unlocks= ();
414             defined($c2= open STDIN,"-|") or die $!;
415             if (!$c2) {
416                 print STDOUT join(':',@newpwent),"\n" or die $!;
417                 exit 0;
418             }
419             for ($i=0; $i<@envs_createuser; $i++) {
420                 $vn= "PW_$envs_createuser[$i]";
421 #print STDERR ">$i|$vn|$$vn|$newpwent[$$vn]<\n";
422                 $ENV{"SYNCUSER_CREATE_$envs_createuser[$i]"}= $newpwent[$$vn];
423             }
424             exec $opt_createuser; die "$configfile:$.: ($lu): $opt_createuser: $!\n";
425         }
426         $newpwent= <CU>;
427         close CU; $? and die "$configfile:$.: ($lu): $opt_createuser: code $?\n";
428         chomp $newpwent;
429         if (length $newpwent) {
430             if ($newpwent !~ m/\:/) { diag("creation script demurred"); return; }
431             @newpwent= split(/\:/,$newpwent,-1);
432         }
433         die "$opt_createuser: bad result: \`".join(':',@newpwent)."\'\n"
434             if @newpwent != $PW_fields or $newpwent[$PW_USER] ne $lu;
435         checkuid($newpwent[$PW_UID],$lu) or return;
436         if ($own_haveshadow) {
437             push @ownshadow, [ newentry('SP', $lu,
438                                         'PW', 'x',
439                                         'DAYSCHGD', $cdays,
440                                         'DAYSFIX', 0,
441                                         'DAYSEXP', 99999,
442                                         'DAYSEXPDIS', 7) ];
443             $modifiedshadow= 1;
444         }
445         syncusergroup($lu,$newpwent[$PW_UID]) or return;
446         push @ownpasswd,[ @newpwent ];
447         $modifiedpasswd= 1;
448     }
449
450     for $e (@ownpasswd) {
451         next unless $e->[$PW_USER] eq $lu;
452         syncusergroup($lu,$e->[$PW_UID]) or return;
453     }
454
455     $ruid= $rempasswd{$ru}->[$REM_UID];
456     $rgid= $rempasswd{$ru}->[$REM_GID];
457     if ($opt_sameuid && checkuid($ruid,$lu)) {
458         for $e (@ownpasswd) {
459             next unless $e->[$PW_USER] eq $lu;
460             $luid= $e->[$PW_UID]; $lgid= $e->[$PW_GID];
461             die "$diagstr: local uid $luid, remote uid $ruid\n" if $luid ne $ruid;
462             die "$diagstr: local gid $lgid, remote gid $rgid\n" if $lgid ne $rgid;
463         }
464     }
465
466 #print STDERR "syncuser($lu,$ru) exists $own_haveshadow\n";
467     if ($own_haveshadow && grep($_->[$PW_USER] eq $lu, @ownshadow)) {
468 #print STDERR "syncuser($lu,$ru) shadow $rempasswd{$ru}->[$REM_PW]\n";
469         copyfield('shadow',$lu,$SP_PW, $rempasswd{$ru}->[$REM_PW]);
470     } else {
471 #print STDERR "syncuser($lu,$ru) passwd $rempasswd{$ru}->[$REM_PW]\n";
472         copyfield('passwd',$lu,$PW_PW, $rempasswd{$ru}->[$REM_PW]);
473     }
474     copyfield('passwd',$lu,$PW_COMMENT, $rempasswd{$ru}->[$REM_COMMENT]);
475
476     $newsh= $rempasswd{$ru}->[$REM_SHELL];
477     $oksh= $checkedshell{$newsh};
478     if (!length $oksh) { $checkedshell{$newsh}= $oksh= (-x $newsh) ? 1 : 0; }
479     copyfield('passwd',$lu,$PW_SHELL, $newsh) if $oksh;
480
481     if (!$nogroups) {
482         for $e (@owngroup) {
483             $tgroup= $e->[$GR_GROUP];
484 #print STDERR "syncuser($lu,$ru) group $tgroup\n";
485             next unless &wantsyncgroup($tgroup);
486 #print STDERR "syncuser($lu,$ru) group $tgroup yes\n";
487             fetchgroup();
488             if (!exists $remgroup{$tgroup}) {
489                 diag("group $tgroup: not on remote host");
490                 next;
491             }
492             $inremote= grep($_ eq $ru, split(/\,/,$remgroup{$tgroup}->[$GR_USERS]));
493             $cusers= $e->[$GR_USERS]; $inlocal= grep($_ eq $lu, split(/\,/,$cusers));
494             if ($inremote && !$inlocal) {
495                 $cusers.= ',' if length $cusers;
496                 $cusers.= $lu;
497             } elsif ($inlocal && !$inremote) {
498                 $cusers= join(',', grep($_ ne $lu, split(/\,/, $cusers)));
499             } else {
500                 next;
501             }
502             $e->[$GR_USERS]= $cusers;
503             $modifiedgroup= 1;
504         }
505     }
506 }
507
508 sub banner () {
509     return if $bannerdone;
510     print "\n" or die $!; system 'date'; $? and die $?;
511     $bannerdone= 1;
512 }
513
514 sub finish () {
515     for $h (keys %wanthost) {
516         die "host $h not in config file\n" if $wanthost{$h};
517     }
518
519     if ($display) {
520 #print STDERR "\n\nfinish display=$display pw=$pw\n\n";
521         for $e (@ownpasswd) {
522             $tu= $e->[$PW_USER];
523             $tuid= $e->[$PW_UID];
524             next if $displaydone{$tu};
525             $tpw= $e->[$PW_PW];
526 #print STDERR ">$tu|$tpw<\n";
527             for $e2 (@ownshadow) {
528                 next unless $e2->[$SP_USER] eq $tu;
529                 $tpw= $e2->[$SP_PW]; last;
530             }
531             $tpw= length($tpw)>=13 ? 1 : length($tpw) ? -1 : 0;
532             $head= ($tpw == 1 ? "unsynched" :
533                     $tpw == 0 ? "unsynched, passwordless" :
534                     "unsynched, no-logins").
535                     ($tuid < 100 ? " system account" : " normal user");
536             $unsynch_type{$head} .= " $e->[$PW_USER]";
537         }
538         foreach $head (sort keys %unsynch_type) {
539             hosthead($head);
540             print $unsynch_type{$head} or die $!;
541         }
542         print "\n\n" or die $!;
543         exit 0;
544     }
545     
546     umask 077;
547     for $file (qw(passwd shadow group)) {
548         $realfile= $file{$file,$PW_format};
549         eval "\$modified= \$modified$file; \$data_ref= \\\@own$file;".
550             " \$fetched= \$own_fetched$file; 1;" or die $@;
551         next if !$modified;
552         die $file unless $fetched;
553         banner();
554         if ($no_act) {
555             $newfileinst= "/etc/$realfile";
556             $newfile= "$realfile.new";
557         } else {
558             $newfileinst= $savebackto{$file};
559             $newfile= "$newfileinst.new";
560         }
561         open NF,"> $newfile" or die "$newfile: $!";
562         for $e (@$data_ref) {
563             print NF join(':',@$e),"\n" or die $!;
564         }
565         close NF or die $!;
566         system 'diff','-U0','--label',$realfile,$newfileinst,
567                             '--label',"$realfile.new",$newfile;
568         $?==256 or die $?;
569         if (!$no_act) {
570             (@stats= stat $newfileinst) or die "$file: $!";
571             chown $stats[4],$stats[5], $newfile or die $!;
572             chmod $stats[2] & 07777, $newfile or die $!;
573             rename $newfile, $newfileinst or die $!;
574         }
575     }
576     exit 0;
577 }
578
579 while (<CF>) {
580     chomp;
581     s/^\s*//; s/\s*$//;
582     next if m/^\#/ || !m/\S/;
583     finish() if m/^end$/;
584     if (m/^host\s+(\S+)$/) {
585         $ch_name= $1;
586         $ch_getpasswd= $ch_getgroup= $ch_getshadow= '';
587         $ch_fetchedpasswd= $ch_fetchedgroup;
588         if (!@ARGV) {
589             $ch_doinghost= 1;
590         } elsif (exists $wanthost{$ch_name}) {
591             $wanthost{$ch_name}= 0;
592             $ch_doinghost= 1;
593         } else {
594             $ch_doinghost= 0;
595         }
596         fields_fmt('REM','std');
597     } elsif (m/^(getpasswd|getshadow|getgroup)\s+(.*\S)$/) {
598         eval "\$ch_$1= \$2; 1;" or die $@;
599     } elsif (m/^(local|remote)format\s+(\w+)$/) {
600         fields_fmt($1 eq 'local' ? 'PW' :
601                    $1 eq 'remote' ? 'REM' : die,
602                    $2);
603     } elsif (m/^lock(passwd|group)\s+(runvia|link)\s+(\S+)$/) {
604         eval "\$ch_lock_$1= \$3; \$ch_lockstyle_$1= \$2; 1;" or die $@;
605     } elsif (m/^lock(passwd|group)\s+(none)$/) {
606         eval "\$ch_lockstyle_$1= \$2; 1;" or die $@;
607     } elsif (m,^(homebase|defaultshell)\s+(/\S+)$,) {
608         eval "\$ch_$1= \$2; 1;" or die $@;
609     } elsif (m/^(uidmin|uidmax)\s+(\d+)$/ && $2>0) {
610         eval "\$ch_$1= \$2; 1;" or die $@;
611     } elsif (m/^createuser$/) {
612         $opt_createuser= $def_createuser;
613     } elsif (m/^nocreateuser$/) {
614         $opt_createuser= '';
615     } elsif (m/^createuser\s+(\S+)$/) {
616         $opt_createuser= $1;
617     } elsif (m/^logfile\s+(.*\S)$/) {
618         if (!$no_act) {
619             open STDOUT,">> $1" or die "$1: $!"; $|=1;
620             $!=0; system 'date'; $? and die "date: $! $?\n";
621         } elsif (!$display) {
622             print "would log to $1\n" or die $!;
623         }
624     } elsif (m/^(no|)(sameuid)$/) {
625         eval "\$opt_$2= ".($1 eq 'no' ? 0 : 1)."; 1;" or die $@;
626     } elsif (m/^usergroups$/) {
627         $defaultgid= -1;
628     } elsif (m/^nousergroups$/) {
629         $defaultgid= -2;
630     } elsif (m/^defaultgid\s+(\d+)$/) {
631         $defaultgid= $1;
632     } elsif (m/^(no|)group\s+([-+.0-9a-zA-Z*?]+)$/) {
633         $yes= $1 eq 'no' ? 0 : 1;
634         $_= $2;
635         @groupglobs=() if $_ eq '*';
636         s/[-+._]/\\$1/g;
637         s/\*/\.\*/g;
638         s/\?/\./g;
639         unshift @groupglobs, [ $_, $yes ];
640         regroupglobs();
641     } elsif (m/^user\s+(\S+)$/) {
642         syncuser($1,$1);
643     } elsif (m/^user\s+(\S+)\s+remote\=(\S+)$/) {
644         syncuser($1,$2);
645     } elsif (m/^nouser\s+(\S+)$/) {
646         syncuser($1,'');
647     } elsif (m/^users\s+(\d+)\-(\d+)$/) {
648         $tmin= $1; $tmax= $2; $except= $3;
649         fetchpasswd();
650         for $k (keys %rempasswd) {
651             $tuid= $rempasswd{$k}->[2];
652             next if $tuid<$1 or $tuid>$2;
653             syncuser($k,$k);
654         }
655     } elsif (m/^addhere$/) {
656     } else {
657         die "$configfile:$.: unknown directive\n";
658     }
659 }
660
661 die "$configfile:$.: missing \`end', or read error\n";