This commit was generated by cvs2svn to compensate for changes in r3241,
[freeside.git] / fs_signup / FS-SignupClient / fs_signupd
1 #!/usr/bin/perl -Tw
2 #
3 # fs_signupd
4 #
5 # This is run REMOTELY over ssh by fs_signup_server.
6
7 use strict;
8 use Socket;
9 use Storable qw(nstore_fd fd_retrieve);
10 use IO::Handle;
11
12 use vars qw( $Debug );
13
14 $Debug = 1;
15
16 my $fs_signupd_socket = "/usr/local/freeside/fs_signupd_socket";
17 my $pid_file = "$fs_signupd_socket.pid";
18
19 $ENV{'PATH'} ='/usr/local/bin:/usr/bin:/usr/ucb:/bin';
20 $ENV{'SHELL'} = '/bin/sh';
21 $ENV{'IFS'} = " \t\n";
22 $ENV{'CDPATH'} = '';
23 $ENV{'ENV'} = '';
24 $ENV{'BASH_ENV'} = '';
25
26 $|=1;
27
28 warn "[fs_signupd] Reading init data...\n" if $Debug;
29 my $init_data = fd_retrieve(\*STDIN);
30
31 warn "[fs_signupd] Creating $fs_signupd_socket\n" if $Debug;
32 my $uaddr = sockaddr_un($fs_signupd_socket);
33 my $proto = getprotobyname('tcp');
34 socket(Server,PF_UNIX,SOCK_STREAM,0) or die "socket: $!";
35 unlink($fs_signupd_socket);
36 bind(Server, $uaddr) or die "bind: $!";
37 listen(Server,SOMAXCONN) or die "listen: $!";
38
39 if ( -e $pid_file ) {
40   open(PIDFILE,"<$pid_file");
41   #chomp( my $old_pid = <PIDFILE> );
42   my $old_pid = <PIDFILE>;
43   close PIDFILE;
44   $old_pid =~ /^(\d+)$/;
45   kill 'TERM', $1;
46 }
47 open(PIDFILE,">$pid_file");
48 print PIDFILE "$$\n";
49 close PIDFILE;
50
51 warn "[fs_signupd] Entering main loop...\n" if $Debug;
52 my $paddr;
53 for ( ; $paddr = accept(Client,Server); close Client) {
54
55   chop( my $command = <Client> );
56
57   if ( $command eq "signup_info" ) {
58
59     warn "[fs_signupd] sending signup info...\n" if $Debug; 
60     nstore_fd($init_data, \*Client) or die "can't send init data: $!";
61     Client->flush;
62
63   } elsif ( $command eq "new_customer" ) {
64
65     #inefficient...
66
67     warn "[fs_signupd] reading customer signup...\n" if $Debug;
68     my $signup_data = fd_retrieve(\*Client);
69
70     warn "[fs_signupd] sending customer data to remote server...\n" if $Debug;
71     nstore_fd($signup_data, \*STDOUT) or die "can't send signup data: $!";
72     STDOUT->flush;
73
74     warn "[fs_signupd] reading error from remote server...\n" if $Debug;
75     my $error = <STDIN>;
76
77     warn "[fs_signupd] sending error to local client...\n" if $Debug;
78     print Client $error;
79     Client->flush;
80
81   } else {
82     die "unexpected command from client: $command";
83   }
84
85 }
86