preliminary self administration
[freeside.git] / fs_selfadmin / FS-MailAdminServer / fs_mailadmind
diff --git a/fs_selfadmin/FS-MailAdminServer/fs_mailadmind b/fs_selfadmin/FS-MailAdminServer/fs_mailadmind
new file mode 100755 (executable)
index 0000000..746d782
--- /dev/null
@@ -0,0 +1,366 @@
+#!/usr/bin/perl -Tw
+
+eval 'exec /usr/bin/perl -Tw -S $0 ${1+"$@"}'
+    if 0; # not running under some shell
+#
+# fs_mailadmind
+#
+# This is run REMOTELY over ssh by fs_mailadmin_server.
+#
+
+use strict;
+use Socket;
+
+use vars qw( $Debug );
+
+$Debug = 0;
+
+my($fs_mailadmind_socket)="/usr/local/freeside/fs_mailadmind_socket";
+
+$ENV{'PATH'} ='/usr/local/bin:/usr/bin:/usr/ucb:/bin';
+$ENV{'SHELL'} = '/bin/sh';
+$ENV{'IFS'} = " \t\n";
+$ENV{'CDPATH'} = '';
+$ENV{'ENV'} = '';
+$ENV{'BASH_ENV'} = '';
+
+$|=1;
+
+warn "[fs_mailadmind] Reading locales...\n" if $Debug;
+chomp( my $n_cust_main_county = <STDIN> );
+my @cust_main_county = map {
+  chomp( my $taxnum = <STDIN> );
+  chomp( my $state = <STDIN> );
+  chomp( my $county = <STDIN> );
+  chomp( my $country = <STDIN> );
+  {
+    'taxnum'  => $taxnum,
+    'state'   => $state,
+    'county'  => $county,
+    'country' => $country,
+  };
+} ( 1 .. $n_cust_main_county );
+
+warn "[fs_mailadmind] Reading package definitions...\n" if $Debug;
+chomp( my $n_part_pkg = <STDIN> );
+my @part_pkg = map {
+  chomp( my $pkgpart = <STDIN> );
+  chomp( my $pkg = <STDIN> );
+  {
+    'pkgpart' => $pkgpart,
+    'pkg'     => $pkg,
+  };
+} ( 1 .. $n_part_pkg );
+
+warn "[fs_mailadmind] Reading POPs...\n" if $Debug;
+chomp( my $n_svc_acct_pop = <STDIN> );
+my @svc_acct_pop = map {
+  chomp( my $popnum = <STDIN> );
+  chomp( my $city = <STDIN> );
+  chomp( my $state = <STDIN> );
+  chomp( my $ac = <STDIN> );
+  chomp( my $exch = <STDIN> );
+  chomp( my $loc = <STDIN> );
+  {
+    'popnum' => $popnum,
+    'city'   => $city,
+    'state'  => $state,
+    'ac'     => $ac,
+    'exch'   => $exch,
+    'loc'    => $loc,
+  };
+} ( 1 .. $n_svc_acct_pop );
+
+warn "[fs_mailadmind] Creating $fs_mailadmind_socket\n" if $Debug;
+my $uaddr = sockaddr_un($fs_mailadmind_socket);
+my $proto = getprotobyname('tcp');
+socket(Server,PF_UNIX,SOCK_STREAM,0) or die "socket: $!";
+unlink($fs_mailadmind_socket);
+bind(Server, $uaddr) or die "bind: $!";
+listen(Server,SOMAXCONN) or die "listen: $!";
+
+warn "[fs_mailadmind] Entering main loop...\n" if $Debug;
+my $paddr;
+for ( ; $paddr = accept(Client,Server); close Client) {
+
+  chop( my $command = <Client> );
+
+  if ( $command eq "signup_info" ) {
+    warn "[fs_mailadmind] sending signup info...\n" if $Debug; 
+    print Client join("\n", $n_cust_main_county,
+      map {
+        $_->{taxnum},
+        $_->{state},
+        $_->{county},
+        $_->{country},
+      } @cust_main_county
+    ), "\n";
+
+    print Client join("\n", $n_part_pkg,
+      map {
+        $_->{pkgpart},
+        $_->{pkg},
+      } @part_pkg
+    ), "\n";
+
+    print Client join("\n", $n_svc_acct_pop,
+      map {
+        $_->{popnum},
+        $_->{city},
+        $_->{state},
+        $_->{ac},
+        $_->{exch},
+        $_->{loc},
+      } @svc_acct_pop
+    ), "\n";
+
+  } elsif ( $command eq "new_customer" ) {
+    warn "[fs_mailadmind] reading customer signup...\n" if $Debug;
+    my(
+      $first, $last, $ss, $company, $address1, $address2, $city, $county,
+      $state, $zip, $country, $daytime, $night, $fax, $payby, $payinfo,
+      $paydate, $payname, $invoicing_list, $pkgpart, $username, $password,
+      $popnum,
+    ) = map { scalar(<Client>) } ( 1 .. 23 );
+
+    warn "[fs_mailadmind] sending customer data to remote server...\n" if $Debug;
+    print 
+      $first, $last, $ss, $company, $address1, $address2, $city, $county,
+      $state, $zip, $country, $daytime, $night, $fax, $payby, $payinfo,
+      $paydate, $payname, $invoicing_list, $pkgpart, $username, $password,
+      $popnum,
+    ;
+
+    warn "[fs_mailadmind] reading error from remote server...\n" if $Debug;
+    my $error = <STDIN>;
+
+    warn "[fs_mailadmind] sending error to local client...\n" if $Debug;
+    print Client $error;
+
+  } elsif ( $command eq "authenticate" ) {
+    warn "[fs_mailadmind] reading user information to auth...\n" if $Debug;
+    chop( my $user = <Client> );
+    warn "[fs_mailadmind] reading authentication material...\n" if $Debug;
+    chop( my $password = <Client> );
+    warn "[fs_mailadmind] sending information to remote server...\n" if $Debug;
+    print "authenticate\n", $user, "\n", $password, "\n";
+
+    warn "[fs_mailadmind] reading error from remote server...\n" if $Debug;
+    my $error = <STDIN>;
+
+    warn "[fs_mailadmind] sending error to local client...\n" if $Debug;
+    print Client $error;
+    
+  } elsif ( $command eq "list_packages" ) {
+    warn "[fs_mailadmind] reading user information to list_packages...\n" if $Debug;
+    chop( my $user = <Client> );
+    warn "[fs_mailadmind] sending user information to remote server...\n" if $Debug;
+    print "list_packages\n", $user, "\n";
+
+    warn "[fs_mailadmind] reading data from remote server...\n" if $Debug;
+    chomp( my $n_packages = <STDIN> );
+    my @packages = map {
+      chomp( my $pkgnum  = <STDIN> );
+      chomp( my $domain  = <STDIN> );
+      chomp( my $account = <STDIN> );
+      {
+        'pkgnum'  => $pkgnum,
+        'domain'  => $domain,
+        'account' => $account,
+      };
+    } ( 1 .. $n_packages );
+
+    warn "[fs_mailadmind] sending data to local client...\n" if $Debug;
+
+    print Client join("\n", $n_packages,
+      map {
+        $_->{pkgnum},
+        $_->{domain},
+        $_->{account},
+      } @packages
+    ), "\n";
+
+  } elsif ( $command eq "list_mailboxes" ) {
+    warn "[fs_mailadmind] reading user information to list_mailboxes...\n" if $Debug;
+    chop( my $user = <Client> );
+    warn "[fs_mailadmind] reading package number to list_mailboxes...\n" if $Debug;
+    chop( my $package = <Client> );
+    warn "[fs_mailadmind] sending user information to remote server...\n" if $Debug;
+    print "list_mailboxes\n", $user, "\n", $package, "\n";
+
+    warn "[fs_mailadmind] reading data from remote server...\n" if $Debug;
+    chomp( my $n_svc_acct = <STDIN> );
+    my @svc_acct = map {
+      chomp( my $svcnum = <STDIN> );
+      chomp( my $username = <STDIN> );
+      chomp( my $_password = <STDIN> );
+      {
+        'svcnum' => $svcnum,
+        'username' => $username,
+        '_password'     => $_password,
+      };
+    } ( 1 .. $n_svc_acct );
+
+    warn "[fs_mailadmind] sending data to local client...\n" if $Debug;
+
+    print Client join("\n", $n_svc_acct,
+      map {
+        $_->{svcnum},
+        $_->{username},
+        $_->{_password},
+      } @svc_acct
+    ), "\n";
+
+  } elsif ( $command eq "delete_mailbox" ) {
+    warn "[fs_mailadmind] reading user information to auth...\n" if $Debug;
+    chop( my $user = <Client> );
+    warn "[fs_mailadmind] reading account information to delete...\n" if $Debug;
+    chop( my $account = <Client> );
+    warn "[fs_mailadmind] sending information to remote server...\n" if $Debug;
+    print "delete_mailbox\n", $user, "\n", $account, "\n";
+
+    warn "[fs_mailadmind] reading error from remote server...\n" if $Debug;
+    my $error = <STDIN>;
+
+    warn "[fs_mailadmind] sending error to local client...\n" if $Debug;
+    print Client $error;
+
+  } elsif ( $command eq "password_mailbox" ) {
+    warn "[fs_mailadmind] reading user information to auth...\n" if $Debug;
+    chop( my $user = <Client> );
+    warn "[fs_mailadmind] reading account information to password...\n" if $Debug;
+    my(
+      $account, $_password,
+    ) = map { scalar(<Client>) } ( 1 .. 2 );
+
+    warn "[fs_mailadmind] sending password data to remote server...\n" if $Debug;
+    print "password_mailbox", "\n";
+    print 
+      $user, "\n", $account, $_password,
+    ;
+
+    warn "[fs_mailadmind] reading error from remote server...\n" if $Debug;
+    my $error = <STDIN>;
+
+    warn "[fs_mailadmind] sending error to local client...\n" if $Debug;
+    print Client $error;
+
+  } elsif ( $command eq "add_mailbox" ) {
+    warn "[fs_mailadmind] reading user information to auth...\n" if $Debug;
+    chop( my $user = <Client> );
+    warn "[fs_mailadmind] reading account information to create...\n" if $Debug;
+    my(
+      $package, $account, $_password,
+    ) = map { scalar(<Client>) } ( 1 .. 3 );
+
+    warn "[fs_mailadmind] sending service data to remote server...\n" if $Debug;
+    print "add_mailbox", "\n";
+    print 
+      $user, "\n", $package, $account, $_password,
+    ;
+
+    warn "[fs_mailadmind] reading error from remote server...\n" if $Debug;
+    my $error = <STDIN>;
+
+    warn "[fs_mailadmind] sending error to local client...\n" if $Debug;
+    print Client $error;
+
+  } elsif ( $command eq "add_forward" ) {
+    warn "[fs_mailadmind] reading user information to auth...\n" if $Debug;
+    chop( my $user = <Client> );
+    warn "[fs_mailadmind] reading forward information to create...\n" if $Debug;
+    my(
+      $package, $source, $dest,
+    ) = map { scalar(<Client>) } ( 1 .. 3 );
+
+    warn "[fs_mailadmind] sending service data to remote server...\n" if $Debug;
+    print "add_forward", "\n";
+    print 
+      $user, "\n", $package, $source, $dest,
+    ;
+
+    warn "[fs_mailadmind] reading error from remote server...\n" if $Debug;
+    my $error = <STDIN>;
+
+    warn "[fs_mailadmind] sending error to local client...\n" if $Debug;
+    print Client $error;
+
+  } elsif ( $command eq "delete_forward" ) {
+    warn "[fs_mailadmind] reading user information to auth...\n" if $Debug;
+    chop( my $user = <Client> );
+    warn "[fs_mailadmind] reading forward information to delete...\n" if $Debug;
+    chop( my $service = <Client> );
+    warn "[fs_mailadmind] sending information to remote server...\n" if $Debug;
+    print "delete_forward\n", $user, "\n", $service, "\n";
+
+    warn "[fs_mailadmind] reading error from remote server...\n" if $Debug;
+    my $error = <STDIN>;
+
+    warn "[fs_mailadmind] sending error to local client...\n" if $Debug;
+    print Client $error;
+
+  } elsif ( $command eq "list_forwards" ) {
+    warn "[fs_mailadmind] reading user information to list_forwards...\n" if $Debug;
+    chop( my $user = <Client> );
+    warn "[fs_mailadmind] reading service number to list_forwards...\n" if $Debug;
+    chop( my $service = <Client> );
+    warn "[fs_mailadmind] sending user information to remote server...\n" if $Debug;
+    print "list_forwards\n", $user, "\n", $service, "\n";
+
+    warn "[fs_mailadmind] reading data from remote server...\n" if $Debug;
+    chomp( my $n_svc_forward = <STDIN> );
+    my @svc_forward = map {
+      chomp( my $svcnum = <STDIN> );
+      chomp( my $dest = <STDIN> );
+      {
+        'svcnum' => $svcnum,
+        'dest' => $dest,
+      };
+    } ( 1 .. $n_svc_forward );
+
+    warn "[fs_mailadmind] sending data to local client...\n" if $Debug;
+
+    print Client join("\n", $n_svc_forward,
+      map {
+        $_->{svcnum},
+        $_->{dest},
+      } @svc_forward
+    ), "\n";
+
+  } elsif ( $command eq "list_pkg_forwards" ) {
+    warn "[fs_mailadmind] reading user information to list_pkg_forwards...\n" if $Debug;
+    chop( my $user = <Client> );
+    warn "[fs_mailadmind] reading service number to list_forwards...\n" if $Debug;
+    chop( my $package = <Client> );
+    warn "[fs_mailadmind] sending user information to remote server...\n" if $Debug;
+    print "list_pkg_forwards\n", $user, "\n", $package, "\n";
+
+    warn "[fs_mailadmind] reading data from remote server...\n" if $Debug;
+    chomp( my $n_svc_forward = <STDIN> );
+    my @svc_forward = map {
+      chomp( my $svcnum = <STDIN> );
+      chomp( my $srcsvc = <STDIN> );
+      chomp( my $dest = <STDIN> );
+      {
+        'svcnum' => $svcnum,
+        'srcsvc' => $srcsvc,
+        'dest' => $dest,
+      };
+    } ( 1 .. $n_svc_forward );
+
+    warn "[fs_mailadmind] sending data to local client...\n" if $Debug;
+
+    print Client join("\n", $n_svc_forward,
+      map {
+        $_->{svcnum},
+        $_->{srcsvc},
+        $_->{dest},
+      } @svc_forward
+    ), "\n";
+
+  } else {
+    die "unexpected command from client: $command";
+  }
+
+}
+