Assigns password from the shadow file for RADIUS password "UNIX"
[freeside.git] / bin / svc_acct.import
1 #!/usr/bin/perl -Tw
2 #
3 # $Id: svc_acct.import,v 1.2 1998-10-13 12:07:51 ivan Exp $
4 #
5 # ivan@sisd.com 98-mar-9
6 #
7 # changed 'password' field to '_password' because PgSQL 6.3 reserves this word
8 #       bmccane@maxbaud.net  98-Apr-3
9 #
10 # generalized svcparts (still needs radius import) ivan@sisd.com 98-mar-23
11 #
12 # radius import, now an interactive script.  still needs erpcd import?
13 # ivan@sisd.com 98-jun-24
14 #
15 # arbitrary radius attributes ivan@sisd.com 98-aug-9
16 #
17 # don't import /var/spool/freeside/conf/shells!  ivan@sisd.com 98-aug-13
18 #
19 # $Log: svc_acct.import,v $
20 # Revision 1.2  1998-10-13 12:07:51  ivan
21 # Assigns password from the shadow file for RADIUS password "UNIX"
22 #
23
24 use strict;
25 use vars qw(%part_svc);
26 use Date::Parse;
27 use FS::SSH qw(iscp);
28 use FS::UID qw(adminsuidsetup);
29 use FS::Record qw(qsearch);
30 use FS::svc_acct;
31
32 adminsuidsetup;
33
34 #my($spooldir)="/var/spool/freeside/export";
35 my($spooldir)="unix/";
36
37 $FS::svc_acct::nossh_hack = 1;
38
39 ###
40
41 %part_svc=map { $_->svcpart, $_ } qsearch('part_svc',{'svcdb'=>'svc_acct'});
42
43 print "\n\n", &menu_svc, "\n", <<END;
44 Most accounts probably have entries in passwd and users (with Port-Limit
45 nonexistant or 1).
46 END
47 my($ppp_svcpart)=&getpart;
48
49 print "\n\n", &menu_svc, "\n", <<END;
50 Some accounts have entries in passwd and users, but with Port-Limit 2 (or
51 more).
52 END
53 my($isdn_svcpart)=&getpart;
54
55 print "\n\n", &menu_svc, "\n", <<END;
56 Some accounts might have entries in users only (Port-Limit 1)
57 END
58 my($oppp_svcpart)=&getpart;
59
60 print "\n\n", &menu_svc, "\n", <<END;
61 Some accounts might have entries in users only (Port-Limit >= 2)
62 END
63 my($oisdn_svcpart)=&getpart;
64
65 print "\n\n", &menu_svc, "\n", <<END;
66 POP mail accounts have entries in passwd only, and have a particular shell.
67 END
68 print "Enter that shell: ";
69 my($pop_shell)=&getvalue;
70 my($popmail_svcpart)=&getpart;
71
72 print "\n\n", &menu_svc, "\n", <<END;
73 Everything else in passwd is a shell account.
74 END
75 my($shell_svcpart)=&getpart;
76
77 print "\n\n", <<END;
78 Enter the location and name of your _user_ passwd file, for example
79 "mail.isp.com:/etc/passwd" or "nis.isp.com:/etc/global/passwd"
80 END
81 print ":";
82 my($loc_passwd)=&getvalue;
83 iscp("root\@$loc_passwd", "$spooldir/passwd.import");
84
85 print "\n\n", <<END;
86 Enter the location and name of your _user_ shadow file, for example
87 "mail.isp.com:/etc/shadow" or "bsd.isp.com:/etc/master.passwd"
88 END
89 print ":";
90 my($loc_shadow)=&getvalue;
91 iscp("root\@$loc_shadow", "$spooldir/shadow.import");
92
93 print "\n\n", <<END;
94 Enter the location and name of your radius "users" file, for example
95 "radius.isp.com:/etc/raddb/users"
96 END
97 print ":";
98 my($loc_users)=&getvalue;
99 iscp("root\@$loc_users", "$spooldir/users.import");
100
101 sub menu_svc {
102   ( join "\n", map "$_: ".$part_svc{$_}->svc, sort keys %part_svc ). "\n";
103 }
104 sub getpart {
105   print "Enter part number, or 0 for none: ";
106   &getvalue;
107 }
108 sub getvalue {
109   my($x)=scalar(<STDIN>);
110   chop $x;
111   $x;
112 }
113
114 print "\n\n";
115
116 ###
117
118 open(PASSWD,"<$spooldir/passwd.import");
119 open(SHADOW,"<$spooldir/shadow.import");
120 open(USERS,"<$spooldir/users.import");
121
122 my(%upassword,%ip,%allparam);
123 my(%param,$username);
124 while (<USERS>) {
125   chop;
126   next if /^$/;
127   if ( /^\S/ ) {
128     /^(\w+)\s+Password\s+=\s+"([^"]+)"(,\s+Expiration\s+=\s+"([^"]*")\s*)?$/
129       or die "1Unexpected line in users.import: $_";
130     my($password,$expiration);
131     ($username,$password,$expiration)=(lc($1),$2,$4);
132     $password = '' if $password eq 'UNIX';
133     $upassword{$username}=$password;
134     undef %param;
135   } else {
136     die "2Unexpected line in users.import: $_";
137   }
138   while (<USERS>) {
139     chop;
140     if ( /^\s*$/ ) {
141       $ip{$username}=$param{'radius_Framed_IP_Address'}||'0e0';
142       delete $param{'radius_Framed_IP_Address'};
143       $allparam{$username}={ %param };
144       last;
145     } elsif ( /^\s+([\w\-]+)\s=\s"?([\w\.\-\s]+)"?,?\s*$/ ) {
146       my($attribute,$value)=($1,$2);
147       $attribute =~ s/\-/_/g;
148       $param{'radius_'.$attribute}=$value;
149     } else {
150       die "3Unexpected line in users.import: $_";
151     }
152   }
153 }
154 #? incase there isn't a terminating blank line ?
155 $ip{$username}=$param{'radius_Framed_IP_Address'}||'0e0';
156 delete $param{'radius_Framed_IP_Address'};
157 $allparam{$username}={ %param };
158
159 my(%password);
160 while (<SHADOW>) {
161   chop;
162   my($username,$password)=split(/:/);
163   $password{$username}=$password;
164 }
165
166 while (<PASSWD>) {
167   chop;
168   my($username,$x,$uid,$gid,$finger,$dir,$shell)=split(/:/);
169   my($password)=$upassword{$username} || $password{$username};
170
171   my($maxb)=${$allparam{$username}}{'radius_Port_Limit'};
172   my($svcpart);
173   if ( exists $upassword{$username} ) {
174     if ( $maxb >= 2 ) {
175       $svcpart = $isdn_svcpart
176     } elsif ( ! $maxb || $maxb == 1 ) {
177       $svcpart = $ppp_svcpart
178     } else {
179       die "Illegal Port-Limit in users ($username)!\n";
180     }
181   } elsif ( $shell eq $pop_shell ) {
182     $svcpart = $popmail_svcpart;
183   } else {
184     $svcpart = $shell_svcpart;
185   }
186
187   my($svc_acct) = create FS::svc_acct ({
188     'svcpart'  => $svcpart,
189     'username' => $username,
190     'password' => $password,
191     'uid'      => $uid,
192     'gid'      => $gid,
193     'finger'   => $finger,
194     'dir'      => $dir,
195     'shell'    => $shell,
196     'slipip'   => $ip{$username},
197     %{$allparam{$username}},
198   });
199   my($error);
200   $error=$svc_acct->insert;
201   die $error if $error;
202
203   delete $allparam{$username};
204   delete $upassword{$username};
205 }
206
207 #my($username);
208 foreach $username ( keys %upassword ) {
209   my($password)=$upassword{$username};
210
211   my($maxb)=${$allparam{$username}}{'radius_Port_Limit'} || 0;
212   my($svcpart);
213   if ( $maxb == 2 ) {
214     $svcpart = $oisdn_svcpart
215   } elsif ( ! $maxb || $maxb == 1 ) {
216     $svcpart = $oppp_svcpart
217   } else {
218     die "Illegal Port-Limit in users!\n";
219   }
220
221   my($svc_acct) = create FS::svc_acct ({
222     'svcpart'  => $svcpart,
223     'username' => $username,
224     'password' => $password,
225     'slipip'   => $ip{$username},
226     %{$allparam{$username}},
227   });
228   my($error);
229   $error=$svc_acct->insert;
230   die $error, if $error;
231
232   delete $allparam{$username};
233   delete $upassword{$username};
234 }
235