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