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