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