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