Release bumped to "gts4".
[tac_plus.git] / convert.pl
1 #! /usr/bin/perl
2
3 # convert a passwd(5) and optional supplementary file into the new
4 # file format
5
6 # Please NOTE:  None of the TACACS code available here comes with any
7 # warranty or support.
8 # Copyright (c) 1995-1998 by Cisco systems, Inc.
9 #
10 # Permission to use, copy, modify, and distribute this software for any
11 # purpose and without fee is hereby granted, provided that this
12 # copyright and permission notice appear on all copies of the software and
13 # supporting documentation, the name of Cisco Systems, Inc. not be used
14 # in advertising or publicity pertaining to distribution of the
15 # program without specific prior permission, and notice be given
16 # in supporting documentation that modification, copying and distribution is by
17 # permission of Cisco Systems, Inc.
18 #
19 # Cisco Systems, Inc. makes no representations about the suitability of this
20 # software for any purpose.  THIS SOFTWARE IS PROVIDED ``AS IS''
21 # AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT
22 # LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 # FOR A PARTICULAR PURPOSE.
24
25 die 'Usage: convert.pl <password file> [-g] [ <supplementary file> ]'
26     if $#ARGV < 0;
27
28 $pwfile = '';
29 $supfile  = '';
30 %sup = ();
31 $acl_valid = 0;  # is acl valid in gid field?
32
33 $pwfile = shift(@ARGV);
34 while ($#ARGV >= 0) {
35     local($arg) = shift(@ARGV);
36     $acl_valid++, next if ($arg eq '-g');
37     $supfile = $arg;
38 }
39
40 if ($supfile) {
41     open(SUP, $supfile) || die "Can't read $supfile -- $!";
42     while(<SUP>) {
43         next if /^#/;
44         chop;
45
46         local($user, $inacl, $outacl, $arap, $chap) = split(/:/);
47
48         if (defined $sup{$user,'user'}) {
49             die "User $user is multiply defined on lines $sup{$user,'user'} and $. of $supfile";
50         }
51         $users{$user} = 1;
52         $sup{$user,'user'} = $.;
53         $sup{$user,'inacl'} = $inacl;
54         $sup{$user,'outacl'} = $outacl;
55         $sup{$user,'arap'} = $arap;
56         $sup{$user,'chap'} = $chap;
57     }
58     close(SUP);
59 }
60
61 open(PASSWD, $pwfile) || die "Can't read $pwfile -- $!";
62
63 while(<PASSWD>) {
64     chop;
65     next if ($_ eq '');
66
67     local($user, $pass, $uid, $gid, $gcos, $home, $exp) = split(/:/);
68
69     $users{$user} = 2;
70
71     print "user = $user {\n";
72     print "    login = des $pass\n";
73     if (!$acl_valid) {
74         print "    member = $gid\n";
75         $groups{$gid}++;
76     }
77     if ($gcos) {
78         if ($gcos =~ /[         ]/) {
79             print "    name = \"$gcos\"\n";
80         } else {
81             print "    name = $gcos\n";
82         }
83     }
84
85     if ($exp =~ /\S+\s+\d+\s+\d+/) {
86         print "    expires = \"$exp\"\n";
87     }
88
89     if ($acl_valid) {
90         print "    service = exec {\n";
91         print "        acl = $gid\n";
92         print "    }\n";
93     }
94
95     local($outacl) = $sup{$user,'outacl'};
96     local($inacl) = $sup{$user,'inacl'};
97     if ($inacl ne '' || $outacl ne '') {
98         print "    service = slip {\n";
99         print " inacl = $inacl\n" if $inacl ne '';
100         print " outacl = $outacl\n" if $outacl ne '';
101         print "    }\n";
102
103         print "    service = ppp protocol = ip {\n";
104         print " inacl = $inacl\n" if $inacl ne '';
105         print " outacl = $outacl\n" if $outacl ne '';
106         print "    }\n";
107     }
108
109     print "    arap = $sup{$user,'arap'}\n" if $sup{$user,'arap'} ne '';
110     print "    chap = $sup{$user,'chap'}\n" if $sup{$user,'chap'} ne '';
111     print "}\n";
112 }
113
114 close(PASSWD);
115
116 foreach $user (keys %users) {
117     next if $users{$user} != 1;
118     # This user only in supfile
119     print "user = $user {\n";
120     local($outacl) = $sup{$user,'outacl'};
121     local($inacl) = $sup{$user,'inacl'};
122     if ($inacl ne '' || $outacl ne '') {
123         print "    service = slip {\n";
124         print " inacl = $inacl\n" if $inacl ne '';
125         print " outacl = $outacl\n" if $outacl ne '';
126         print "    }\n";
127
128         print "    service = ppp protocol = ip {\n";
129         print " inacl = $inacl\n" if $inacl ne '';
130         print " outacl = $outacl\n" if $outacl ne '';
131         print "    }\n";
132     }
133
134     print "    arap = $sup{$user,'arap'}\n" if $sup{$user,'arap'} ne '';
135     print "    chap = $sup{$user,'chap'}\n" if $sup{$user,'chap'} ne '';
136     print "}\n";
137 }
138
139 exit 0 if ($acl_valid);
140
141 foreach $group (keys %groups) {
142     print "group = $group { }\n";
143 }