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