Imported Upstream version 0.66.1
[hcoop/debian/courier-authlib.git] / userdb / pw2userdb.in
1 #! @PERL@
2 #
3 # Convert /etc/passwd and /etc/shadow to userdb format.
4 #
5 #
6 # Copyright 1998 - 1999 Double Precision, Inc. See COPYING for
7 # distribution information.
8
9 use Getopt::Long;
10
11 #
12 # Some undocumented options here (for vchkpw2userdb)
13 #
14
15 die "Invalid options.\n" unless
16 GetOptions("passwd=s" => \$passwd, "shadow=s" => \$shadow,
17 "noshadow" => \$noshadow, "nouid" => \$nouid,
18 "domain=s" => \$domain, "vpopuid" => \$vpopuid );
19
20 ($dummy, $dummy, $fixed_uid, $fixed_gid)=getpwnam("vpopmail")
21 if $vpopuid;
22
23 $passwd="/etc/passwd" unless $passwd =~ /./;
24 $shadow="/etc/shadow" unless $shadow =~ /./;
25
26 $domain="" unless $domain =~ /./;
27 $domain="\@$domain" if $domain =~ /./;
28
29 open(PASSWD, $passwd) || die "$!\n";
30
31 while (<PASSWD>)
32 {
33 chop if /\n$/;
34 next if /^#/;
35 ($acct,$passwd,$uid,$gid,$name,$home,$shell)=split( /:/ );
36
37 ($uid,$gid)=($fixed_uid,$fixed_gid) if $vpopuid;
38
39 $PASSWORD{$acct}=$passwd if $passwd ne "x";
40 $UID{$acct}=$uid;
41 $GID{$acct}=$gid;
42 $HOME{$acct}=$home;
43 $SHELL{$acct}=$shell;
44
45 $name =~ s/\|/./g; # Just in case
46 $GECOS{$acct}=$name;
47 }
48 close (PASSWD);
49
50 if ( -f $shadow && ! $noshadow)
51 {
52 open (SHADOW, $shadow) || die "$!\n";
53 while (<SHADOW>)
54 {
55 next if /^#/;
56 ($acct,$passwd,$dummy)=split(/:/);
57 $PASSWORD{$acct}=$passwd;
58 }
59 close (SHADOW);
60 }
61
62 while ( defined ($key=each %UID))
63 {
64 print "$key$domain\tuid=$UID{$key}|gid=$GID{$key}|home=$HOME{$key}" .
65 ( $SHELL{$key} =~ /./ ? "|shell=$SHELL{$key}":"") .
66 ( $PASSWORD{$key} =~ /./ ? "|systempw=$PASSWORD{$key}":"") .
67 ( $GECOS{$key} =~ /./ ? "|gecos=$GECOS{$key}":"") .
68 "\n";
69 print "$UID{$key}=\t$key\n" unless $nouid;
70 }