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