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