This commit was generated by cvs2svn to compensate for changes in r3921,
[freeside.git] / fs_selfadmin / FS-MailAdminServer / fs_mailadmind
1 #!/usr/bin/perl -Tw
2
3 eval 'exec /usr/bin/perl -Tw -S $0 ${1+"$@"}'
4     if 0; # not running under some shell
5 #
6 # fs_mailadmind
7 #
8 # This is run REMOTELY over ssh by fs_mailadmin_server.
9 #
10
11 use strict;
12 use Socket;
13
14 use vars qw( $Debug );
15
16 $Debug = 0;
17
18 my($fs_mailadmind_socket)="/usr/local/freeside/fs_mailadmind_socket";
19
20 $ENV{'PATH'} ='/usr/local/bin:/usr/bin:/usr/ucb:/bin';
21 $ENV{'SHELL'} = '/bin/sh';
22 $ENV{'IFS'} = " \t\n";
23 $ENV{'CDPATH'} = '';
24 $ENV{'ENV'} = '';
25 $ENV{'BASH_ENV'} = '';
26
27 $|=1;
28
29 warn "[fs_mailadmind] Reading locales...\n" if $Debug;
30 chomp( my $n_cust_main_county = <STDIN> );
31 my @cust_main_county = map {
32   chomp( my $taxnum = <STDIN> );
33   chomp( my $state = <STDIN> );
34   chomp( my $county = <STDIN> );
35   chomp( my $country = <STDIN> );
36   {
37     'taxnum'  => $taxnum,
38     'state'   => $state,
39     'county'  => $county,
40     'country' => $country,
41   };
42 } ( 1 .. $n_cust_main_county );
43
44 warn "[fs_mailadmind] Reading package definitions...\n" if $Debug;
45 chomp( my $n_part_pkg = <STDIN> );
46 my @part_pkg = map {
47   chomp( my $pkgpart = <STDIN> );
48   chomp( my $pkg = <STDIN> );
49   {
50     'pkgpart' => $pkgpart,
51     'pkg'     => $pkg,
52   };
53 } ( 1 .. $n_part_pkg );
54
55 warn "[fs_mailadmind] Reading POPs...\n" if $Debug;
56 chomp( my $n_svc_acct_pop = <STDIN> );
57 my @svc_acct_pop = map {
58   chomp( my $popnum = <STDIN> );
59   chomp( my $city = <STDIN> );
60   chomp( my $state = <STDIN> );
61   chomp( my $ac = <STDIN> );
62   chomp( my $exch = <STDIN> );
63   chomp( my $loc = <STDIN> );
64   {
65     'popnum' => $popnum,
66     'city'   => $city,
67     'state'  => $state,
68     'ac'     => $ac,
69     'exch'   => $exch,
70     'loc'    => $loc,
71   };
72 } ( 1 .. $n_svc_acct_pop );
73
74 warn "[fs_mailadmind] Creating $fs_mailadmind_socket\n" if $Debug;
75 my $uaddr = sockaddr_un($fs_mailadmind_socket);
76 my $proto = getprotobyname('tcp');
77 socket(Server,PF_UNIX,SOCK_STREAM,0) or die "socket: $!";
78 unlink($fs_mailadmind_socket);
79 bind(Server, $uaddr) or die "bind: $!";
80 listen(Server,SOMAXCONN) or die "listen: $!";
81
82 warn "[fs_mailadmind] Entering main loop...\n" if $Debug;
83 my $paddr;
84 for ( ; $paddr = accept(Client,Server); close Client) {
85
86   chop( my $command = <Client> );
87
88   if ( $command eq "signup_info" ) {
89     warn "[fs_mailadmind] sending signup info...\n" if $Debug; 
90     print Client join("\n", $n_cust_main_county,
91       map {
92         $_->{taxnum},
93         $_->{state},
94         $_->{county},
95         $_->{country},
96       } @cust_main_county
97     ), "\n";
98
99     print Client join("\n", $n_part_pkg,
100       map {
101         $_->{pkgpart},
102         $_->{pkg},
103       } @part_pkg
104     ), "\n";
105
106     print Client join("\n", $n_svc_acct_pop,
107       map {
108         $_->{popnum},
109         $_->{city},
110         $_->{state},
111         $_->{ac},
112         $_->{exch},
113         $_->{loc},
114       } @svc_acct_pop
115     ), "\n";
116
117   } elsif ( $command eq "new_customer" ) {
118     warn "[fs_mailadmind] reading customer signup...\n" if $Debug;
119     my(
120       $first, $last, $ss, $company, $address1, $address2, $city, $county,
121       $state, $zip, $country, $daytime, $night, $fax, $payby, $payinfo,
122       $paydate, $payname, $invoicing_list, $pkgpart, $username, $password,
123       $popnum,
124     ) = map { scalar(<Client>) } ( 1 .. 23 );
125
126     warn "[fs_mailadmind] sending customer data to remote server...\n" if $Debug;
127     print 
128       $first, $last, $ss, $company, $address1, $address2, $city, $county,
129       $state, $zip, $country, $daytime, $night, $fax, $payby, $payinfo,
130       $paydate, $payname, $invoicing_list, $pkgpart, $username, $password,
131       $popnum,
132     ;
133
134     warn "[fs_mailadmind] reading error from remote server...\n" if $Debug;
135     my $error = <STDIN>;
136
137     warn "[fs_mailadmind] sending error to local client...\n" if $Debug;
138     print Client $error;
139
140   } elsif ( $command eq "authenticate" ) {
141     warn "[fs_mailadmind] reading user information to auth...\n" if $Debug;
142     chop( my $user = <Client> );
143     warn "[fs_mailadmind] reading authentication material...\n" if $Debug;
144     chop( my $password = <Client> );
145     warn "[fs_mailadmind] sending information to remote server...\n" if $Debug;
146     print "authenticate\n", $user, "\n", $password, "\n";
147
148     warn "[fs_mailadmind] reading error from remote server...\n" if $Debug;
149     my $error = <STDIN>;
150
151     warn "[fs_mailadmind] sending error to local client...\n" if $Debug;
152     print Client $error;
153     
154   } elsif ( $command eq "list_packages" ) {
155     warn "[fs_mailadmind] reading user information to list_packages...\n" if $Debug;
156     chop( my $user = <Client> );
157     warn "[fs_mailadmind] sending user information to remote server...\n" if $Debug;
158     print "list_packages\n", $user, "\n";
159
160     warn "[fs_mailadmind] reading data from remote server...\n" if $Debug;
161     chomp( my $n_packages = <STDIN> );
162     my @packages = map {
163       chomp( my $pkgnum  = <STDIN> );
164       chomp( my $domain  = <STDIN> );
165       chomp( my $account = <STDIN> );
166       {
167         'pkgnum'  => $pkgnum,
168         'domain'  => $domain,
169         'account' => $account,
170       };
171     } ( 1 .. $n_packages );
172
173     warn "[fs_mailadmind] sending data to local client...\n" if $Debug;
174
175     print Client join("\n", $n_packages,
176       map {
177         $_->{pkgnum},
178         $_->{domain},
179         $_->{account},
180       } @packages
181     ), "\n";
182
183   } elsif ( $command eq "list_mailboxes" ) {
184     warn "[fs_mailadmind] reading user information to list_mailboxes...\n" if $Debug;
185     chop( my $user = <Client> );
186     warn "[fs_mailadmind] reading package number to list_mailboxes...\n" if $Debug;
187     chop( my $package = <Client> );
188     warn "[fs_mailadmind] sending user information to remote server...\n" if $Debug;
189     print "list_mailboxes\n", $user, "\n", $package, "\n";
190
191     warn "[fs_mailadmind] reading data from remote server...\n" if $Debug;
192     chomp( my $n_svc_acct = <STDIN> );
193     my @svc_acct = map {
194       chomp( my $svcnum = <STDIN> );
195       chomp( my $username = <STDIN> );
196       chomp( my $_password = <STDIN> );
197       {
198         'svcnum' => $svcnum,
199         'username' => $username,
200         '_password'     => $_password,
201       };
202     } ( 1 .. $n_svc_acct );
203
204     warn "[fs_mailadmind] sending data to local client...\n" if $Debug;
205
206     print Client join("\n", $n_svc_acct,
207       map {
208         $_->{svcnum},
209         $_->{username},
210         $_->{_password},
211       } @svc_acct
212     ), "\n";
213
214   } elsif ( $command eq "delete_mailbox" ) {
215     warn "[fs_mailadmind] reading user information to auth...\n" if $Debug;
216     chop( my $user = <Client> );
217     warn "[fs_mailadmind] reading account information to delete...\n" if $Debug;
218     chop( my $account = <Client> );
219     warn "[fs_mailadmind] sending information to remote server...\n" if $Debug;
220     print "delete_mailbox\n", $user, "\n", $account, "\n";
221
222     warn "[fs_mailadmind] reading error from remote server...\n" if $Debug;
223     my $error = <STDIN>;
224
225     warn "[fs_mailadmind] sending error to local client...\n" if $Debug;
226     print Client $error;
227
228   } elsif ( $command eq "password_mailbox" ) {
229     warn "[fs_mailadmind] reading user information to auth...\n" if $Debug;
230     chop( my $user = <Client> );
231     warn "[fs_mailadmind] reading account information to password...\n" if $Debug;
232     my(
233       $account, $_password,
234     ) = map { scalar(<Client>) } ( 1 .. 2 );
235
236     warn "[fs_mailadmind] sending password data to remote server...\n" if $Debug;
237     print "password_mailbox", "\n";
238     print 
239       $user, "\n", $account, $_password,
240     ;
241
242     warn "[fs_mailadmind] reading error from remote server...\n" if $Debug;
243     my $error = <STDIN>;
244
245     warn "[fs_mailadmind] sending error to local client...\n" if $Debug;
246     print Client $error;
247
248   } elsif ( $command eq "add_mailbox" ) {
249     warn "[fs_mailadmind] reading user information to auth...\n" if $Debug;
250     chop( my $user = <Client> );
251     warn "[fs_mailadmind] reading account information to create...\n" if $Debug;
252     my(
253       $package, $account, $_password,
254     ) = map { scalar(<Client>) } ( 1 .. 3 );
255
256     warn "[fs_mailadmind] sending service data to remote server...\n" if $Debug;
257     print "add_mailbox", "\n";
258     print 
259       $user, "\n", $package, $account, $_password,
260     ;
261
262     warn "[fs_mailadmind] reading error from remote server...\n" if $Debug;
263     my $error = <STDIN>;
264
265     warn "[fs_mailadmind] sending error to local client...\n" if $Debug;
266     print Client $error;
267
268   } elsif ( $command eq "add_forward" ) {
269     warn "[fs_mailadmind] reading user information to auth...\n" if $Debug;
270     chop( my $user = <Client> );
271     warn "[fs_mailadmind] reading forward information to create...\n" if $Debug;
272     my(
273       $package, $source, $dest,
274     ) = map { scalar(<Client>) } ( 1 .. 3 );
275
276     warn "[fs_mailadmind] sending service data to remote server...\n" if $Debug;
277     print "add_forward", "\n";
278     print 
279       $user, "\n", $package, $source, $dest,
280     ;
281
282     warn "[fs_mailadmind] reading error from remote server...\n" if $Debug;
283     my $error = <STDIN>;
284
285     warn "[fs_mailadmind] sending error to local client...\n" if $Debug;
286     print Client $error;
287
288   } elsif ( $command eq "delete_forward" ) {
289     warn "[fs_mailadmind] reading user information to auth...\n" if $Debug;
290     chop( my $user = <Client> );
291     warn "[fs_mailadmind] reading forward information to delete...\n" if $Debug;
292     chop( my $service = <Client> );
293     warn "[fs_mailadmind] sending information to remote server...\n" if $Debug;
294     print "delete_forward\n", $user, "\n", $service, "\n";
295
296     warn "[fs_mailadmind] reading error from remote server...\n" if $Debug;
297     my $error = <STDIN>;
298
299     warn "[fs_mailadmind] sending error to local client...\n" if $Debug;
300     print Client $error;
301
302   } elsif ( $command eq "list_forwards" ) {
303     warn "[fs_mailadmind] reading user information to list_forwards...\n" if $Debug;
304     chop( my $user = <Client> );
305     warn "[fs_mailadmind] reading service number to list_forwards...\n" if $Debug;
306     chop( my $service = <Client> );
307     warn "[fs_mailadmind] sending user information to remote server...\n" if $Debug;
308     print "list_forwards\n", $user, "\n", $service, "\n";
309
310     warn "[fs_mailadmind] reading data from remote server...\n" if $Debug;
311     chomp( my $n_svc_forward = <STDIN> );
312     my @svc_forward = map {
313       chomp( my $svcnum = <STDIN> );
314       chomp( my $dest = <STDIN> );
315       {
316         'svcnum' => $svcnum,
317         'dest' => $dest,
318       };
319     } ( 1 .. $n_svc_forward );
320
321     warn "[fs_mailadmind] sending data to local client...\n" if $Debug;
322
323     print Client join("\n", $n_svc_forward,
324       map {
325         $_->{svcnum},
326         $_->{dest},
327       } @svc_forward
328     ), "\n";
329
330   } elsif ( $command eq "list_pkg_forwards" ) {
331     warn "[fs_mailadmind] reading user information to list_pkg_forwards...\n" if $Debug;
332     chop( my $user = <Client> );
333     warn "[fs_mailadmind] reading service number to list_forwards...\n" if $Debug;
334     chop( my $package = <Client> );
335     warn "[fs_mailadmind] sending user information to remote server...\n" if $Debug;
336     print "list_pkg_forwards\n", $user, "\n", $package, "\n";
337
338     warn "[fs_mailadmind] reading data from remote server...\n" if $Debug;
339     chomp( my $n_svc_forward = <STDIN> );
340     my @svc_forward = map {
341       chomp( my $svcnum = <STDIN> );
342       chomp( my $srcsvc = <STDIN> );
343       chomp( my $dest = <STDIN> );
344       {
345         'svcnum' => $svcnum,
346         'srcsvc' => $srcsvc,
347         'dest' => $dest,
348       };
349     } ( 1 .. $n_svc_forward );
350
351     warn "[fs_mailadmind] sending data to local client...\n" if $Debug;
352
353     print Client join("\n", $n_svc_forward,
354       map {
355         $_->{svcnum},
356         $_->{srcsvc},
357         $_->{dest},
358       } @svc_forward
359     ), "\n";
360
361   } else {
362     die "unexpected command from client: $command";
363   }
364
365 }
366