s/!/*/
[freeside.git] / bin / svc_acct.import
1 #!/usr/bin/perl -Tw
2 #
3 # $Id: svc_acct.import,v 1.14 2001-05-07 15:24:15 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.14  2001-05-07 15:24:15  ivan
21 # s/!/*/
22 #
23 # Revision 1.13  2001/05/05 08:51:16  ivan
24 # http://www.sisd.com/freeside/list-archive/msg01915.html
25 #
26 # Revision 1.12  2001/04/22 01:56:15  ivan
27 # get rid of FS::SSH.pm (became Net::SSH and Net::SCP on CPAN)
28 #
29 # Revision 1.11  2000/06/29 12:27:01  ivan
30 # s/password/_password/ for PostgreSQL wasn't done in the import.
31 #
32 # Revision 1.10  2000/06/28 12:32:30  ivan
33 # allow RADIUS lines with "Auth-Type = Local" too
34 #
35 # Revision 1.8  2000/02/03 05:16:52  ivan
36 # beginning of DNS and Apache support
37 #
38 # Revision 1.7  1999/07/08 02:32:26  ivan
39 # import fix, noticed by Ben Leibig and Joel Griffiths
40 #
41 # Revision 1.6  1999/07/08 01:49:00  ivan
42 # updates to avoid -w warnings from Joel Griffiths <griff@aver-computer.com>
43 #
44 # Revision 1.5  1999/03/25 08:42:19  ivan
45 # import stuff uses Term::Query and spits out (some kinds of) nonsensical input
46 #
47 # Revision 1.4  1999/03/24 00:43:38  ivan
48 # die if no relevant services
49 #
50 # Revision 1.3  1998/12/10 07:23:16  ivan
51 # use FS::Conf, need user (for datasrc)
52 #
53 # Revision 1.2  1998/10/13 12:07:51  ivan
54 # Assigns password from the shadow file for RADIUS password "UNIX"
55 #
56
57 use strict;
58 use vars qw(%part_svc);
59 use Date::Parse;
60 use Term::Query qw(query);
61 use Net::SCP qw(iscp);
62 use FS::UID qw(adminsuidsetup datasrc);
63 use FS::Record qw(qsearch);
64 use FS::svc_acct;
65 use FS::part_svc;
66
67 my $user = shift or die &usage;
68 adminsuidsetup $user;
69
70 my($spooldir)="/usr/local/etc/freeside/export.". datasrc;
71
72 $FS::svc_acct::nossh_hack = 1;
73
74 ###
75
76 %part_svc=map { $_->svcpart, $_ } qsearch('part_svc',{'svcdb'=>'svc_acct'});
77
78 die "No services with svcdb svc_acct!\n" unless %part_svc;
79
80 print "\n\n", &menu_svc, "\n", <<END;
81 Most accounts probably have entries in passwd and users (with Port-Limit
82 nonexistant or 1).
83 END
84 my($ppp_svcpart)=&getpart;
85
86 print "\n\n", &menu_svc, "\n", <<END;
87 Some accounts have entries in passwd and users, but with Port-Limit 2 (or
88 more).
89 END
90 my($isdn_svcpart)=&getpart;
91
92 print "\n\n", &menu_svc, "\n", <<END;
93 Some accounts might have entries in users only (Port-Limit 1)
94 END
95 my($oppp_svcpart)=&getpart;
96
97 print "\n\n", &menu_svc, "\n", <<END;
98 Some accounts might have entries in users only (Port-Limit >= 2)
99 END
100 my($oisdn_svcpart)=&getpart;
101
102 print "\n\n", &menu_svc, "\n", <<END;
103 POP mail accounts have entries in passwd only, and have a particular shell.
104 END
105 my($pop_shell)=&getvalue("Enter that shell:");
106 my($popmail_svcpart)=&getpart;
107
108 print "\n\n", &menu_svc, "\n", <<END;
109 Everything else in passwd is a shell account.
110 END
111 my($shell_svcpart)=&getpart;
112
113 print "\n\n", <<END;
114 Enter the location and name of your _user_ passwd file, for example
115 "mail.isp.com:/etc/passwd" or "nis.isp.com:/etc/global/passwd"
116 END
117 my($loc_passwd)=&getvalue(":");
118 iscp("root\@$loc_passwd", "$spooldir/passwd.import");
119
120 print "\n\n", <<END;
121 Enter the location and name of your _user_ shadow file, for example
122 "mail.isp.com:/etc/shadow" or "bsd.isp.com:/etc/master.passwd"
123 END
124 my($loc_shadow)=&getvalue(":");
125 iscp("root\@$loc_shadow", "$spooldir/shadow.import");
126
127 print "\n\n", <<END;
128 Enter the location and name of your radius "users" file, for example
129 "radius.isp.com:/etc/raddb/users"
130 END
131 my($loc_users)=&getvalue(":");
132 iscp("root\@$loc_users", "$spooldir/users.import");
133
134 sub menu_svc {
135   ( join "\n", map "$_: ".$part_svc{$_}->svc, sort keys %part_svc ). "\n";
136 }
137 sub getpart {
138   $^W=0; # Term::Query isn't -w-safe
139   my $return = query "Enter part number:", 'irk', [ keys %part_svc ];
140   $^W=1;
141   $return;
142 }
143 sub getvalue {
144   my $prompt = shift;
145   $^W=0; # Term::Query isn't -w-safe
146   my $return = query $prompt, '';
147   $^W=1;
148   $return;
149 }
150
151 print "\n\n";
152
153 ###
154
155 open(PASSWD,"<$spooldir/passwd.import");
156 open(SHADOW,"<$spooldir/shadow.import");
157 open(USERS,"<$spooldir/users.import");
158
159 my(%upassword,%ip,%allparam);
160 my(%param,$username);
161 while (<USERS>) {
162   chop;
163   next if /^\s*$/;
164   next if /^\s*#/;
165   if ( /^\S/ ) {
166     /^(\w+)\s+(Auth-Type\s+=\s+Local,\s+)Password\s+=\s+"([^"]+)"(,\s+Expiration\s+=\s+"([^"]*")\s*)?$/
167       or die "1Unexpected line in users.import: $_";
168     my($password,$expiration);
169     ($username,$password,$expiration)=(lc($1),$3,$5);
170     $password = '' if $password eq 'UNIX';
171     $upassword{$username}=$password;
172     undef %param;
173   } else {
174     die "2Unexpected line in users.import: $_";
175   }
176   while (<USERS>) {
177     chop;
178     if ( /^\s*$/ ) {
179       if ( defined $param{'radius_Framed_IP_Address'} ) {
180         $ip{$username} = $param{'radius_Framed_IP_Address'};
181         delete $param{'radius_Framed_IP_Address'};
182       } else {
183         $ip{$username} = '0e0';
184       }
185       $allparam{$username}={ %param };
186       last;
187     } elsif ( /^\s+([\w\-]+)\s=\s"?([\w\.\-\s]+)"?,?\s*$/ ) {
188       my($attribute,$value)=($1,$2);
189       $attribute =~ s/\-/_/g;
190       $param{'radius_'.$attribute}=$value;
191     } else {
192       die "3Unexpected line in users.import: $_";
193     }
194   }
195 }
196 #? incase there isn't a terminating blank line ?
197 if ( defined $param{'radius_Framed_IP_Address'} ) {
198   $ip{$username} = $param{'radius_Framed_IP_Address'};
199   delete $param{'radius_Framed_IP_Address'};
200 } else {
201   $ip{$username} = '0e0';
202 }
203 $allparam{$username}={ %param };
204
205 my(%password);
206 while (<SHADOW>) {
207   chop;
208   my($username,$password)=split(/:/);
209   $password =~ s/^\!$/\*/;
210   $password =~ s/\!+/\*SUSPENDED\* /;
211   $password{$username}=$password;
212 }
213
214 while (<PASSWD>) {
215   chop;
216   my($username,$x,$uid,$gid,$finger,$dir,$shell)=split(/:/);
217   my($password)=$upassword{$username} || $password{$username};
218
219   my($maxb)=${$allparam{$username}}{'radius_Port_Limit'};
220   my($svcpart);
221   if ( exists $upassword{$username} ) {
222     if ( $maxb >= 2 ) {
223       $svcpart = $isdn_svcpart
224     } elsif ( ! $maxb || $maxb == 1 ) {
225       $svcpart = $ppp_svcpart
226     } else {
227       die "Illegal Port-Limit in users ($username)!\n";
228     }
229   } elsif ( $shell eq $pop_shell ) {
230     $svcpart = $popmail_svcpart;
231   } else {
232     $svcpart = $shell_svcpart;
233   }
234
235   my($svc_acct) = new FS::svc_acct ({
236     'svcpart'   => $svcpart,
237     'username'  => $username,
238     '_password' => $password,
239     'uid'       => $uid,
240     'gid'       => $gid,
241     'finger'    => $finger,
242     'dir'       => $dir,
243     'shell'     => $shell,
244     'slipip'    => $ip{$username},
245     %{$allparam{$username}},
246   });
247   my($error);
248   $error=$svc_acct->insert;
249   die $error if $error;
250
251   delete $allparam{$username};
252   delete $upassword{$username};
253 }
254
255 #my($username);
256 foreach $username ( keys %upassword ) {
257   my($password)=$upassword{$username};
258
259   my($maxb)=${$allparam{$username}}{'radius_Port_Limit'} || 0;
260   my($svcpart);
261   if ( $maxb == 2 ) {
262     $svcpart = $oisdn_svcpart
263   } elsif ( ! $maxb || $maxb == 1 ) {
264     $svcpart = $oppp_svcpart
265   } else {
266     die "Illegal Port-Limit in users!\n";
267   }
268
269   my($svc_acct) = new FS::svc_acct ({
270     'svcpart'   => $svcpart,
271     'username'  => $username,
272     '_password' => $password,
273     'slipip'    => $ip{$username},
274     %{$allparam{$username}},
275   });
276   my($error);
277   $error=$svc_acct->insert;
278   die $error, if $error;
279
280   delete $allparam{$username};
281   delete $upassword{$username};
282 }
283
284 #
285
286 sub usage {
287   die "Usage:\n\n  svc_acct.import user\n";
288 }
289