adding xisbn, amazon similar items to OPAC
[koha.git] / C4 / SIP / SIPServer.pm
1 package SIPServer;
2
3 use strict;
4 use warnings;
5 use Exporter;
6 use Sys::Syslog qw(syslog);
7 use Net::Server::PreFork;
8 use IO::Socket::INET;
9 use Socket;
10 use Data::Dumper;               # For debugging
11 require UNIVERSAL::require;
12
13 #use Sip qw(readline);
14 use Sip::Constants qw(:all);
15 use Sip::Configuration;
16 use Sip::Checksum qw(checksum verify_cksum);
17 use Sip::MsgType;
18
19 use constant LOG_SIP => "local6"; # Local alias for the logging facility
20
21 our @ISA = qw(Net::Server::PreFork);
22 #
23 # Main
24 #
25
26 my %transports = (
27     RAW    => \&raw_transport,
28     telnet => \&telnet_transport,
29     http   => \&http_transport,
30 );
31
32 # Read configuration
33
34 my $config = new Sip::Configuration $ARGV[0];
35
36 my @parms;
37
38 #
39 # Ports to bind
40 #
41 foreach my $svc (keys %{$config->{listeners}}) {
42     push @parms, "port=" . $svc;
43 }
44
45 #
46 # Logging
47 #
48 push @parms, "log_file=Sys::Syslog", "syslog_ident=acs-server",
49   "syslog_facility=" . LOG_SIP;
50
51 #
52 # Server Management: set parameters for the Net::Server::PreFork
53 # module.  The module silently ignores parameters that it doesn't
54 # recognize, and complains about invalid values for parameters
55 # that it does.
56 #
57 if (defined($config->{'server-params'})) {
58     while (my ($key, $val) = each %{$config->{'server-params'}}) {
59         push @parms, $key . '=' . $val;
60     }
61 }
62
63 print Dumper(@parms);
64
65 #
66 # This is the main event.
67 SIPServer->run(@parms);
68
69 #
70 # Child
71 #
72
73 # process_request is the callback used by Net::Server to handle
74 # an incoming connection request.
75
76 sub process_request {
77     my $self = shift;
78     my $service;
79     my $sockname;
80     my ($sockaddr, $port, $proto);
81     my $transport;
82
83     $self->{config} = $config;
84
85     $sockname = getsockname(STDIN);
86     ($port, $sockaddr) = sockaddr_in($sockname);
87     $sockaddr = inet_ntoa($sockaddr);
88     $proto = $self->{server}->{client}->NS_proto();
89
90     $self->{service} = $config->find_service($sockaddr, $port, $proto);
91
92     if (!defined($self->{service})) {
93         syslog("LOG_ERR", "process_request: Unknown recognized server connection: %s:%s/%s", $sockaddr, $port, $proto);
94         die "process_request: Bad server connection";
95     }
96
97     $transport = $transports{$self->{service}->{transport}};
98
99     if (!defined($transport)) {
100         syslog("LOG_WARN", "Unknown transport '%s', dropping", $service->{transport});
101         return;
102     } else {
103         &$transport($self);
104     }
105 }
106
107 #
108 # Transports
109 #
110
111 sub raw_transport {
112     my $self = shift;
113     my ($uid, $pwd);
114     my $input;
115     my $service = $self->{service};
116     my $strikes = 3;
117     my $expect;
118     my $inst;
119
120     eval {
121         local $SIG{ALRM} = sub { die "alarm\n"; };
122         syslog("LOG_DEBUG", "raw_transport: timeout is %d",
123                $service->{timeout});
124         while ($strikes--) {
125             alarm $service->{timeout};
126             $input = Sip::read_SIP_packet(*STDIN);
127             alarm 0;
128         if (!$input) {
129                 # EOF on the socket
130                 syslog("LOG_INFO", "raw_transport: shutting down: EOF during login");
131                 return;
132             }
133
134             $input =~ s/[\r\n]+$//sm;   # Strip off trailing line terminator
135
136             last if Sip::MsgType::handle($input, $self, LOGIN);
137         }
138     };
139
140     if ($@) {
141         syslog("LOG_ERR", "raw_transport: LOGIN ERROR: '$@'");
142         die "raw_transport: login error, exiting";
143     } elsif (!$self->{account}) {
144         syslog("LOG_ERR", "raw_transport: LOGIN FAILED");
145         die "raw_transport: Login failed, exiting";
146     }
147
148     syslog("LOG_DEBUG", "raw_transport: uname/inst: '%s/%s'",
149            $self->{account}->{id},
150            $self->{account}->{institution});
151
152     $self->sip_protocol_loop();
153
154     syslog("LOG_INFO", "raw_transport: shutting down");
155 }
156
157 sub telnet_transport {
158     my $self = shift;
159     my ($uid, $pwd);
160     my $strikes = 3;
161     my $account = undef;
162     my $input;
163     my $config = $self->{config};
164
165     # Until the terminal has logged in, we don't trust it
166     # so use a timeout to protect ourselves from hanging.
167     eval {
168         local $SIG{ALRM} = sub { die "alarm\n"; };
169         local $|;
170         my $timeout = 0;
171         $| = 1;                 # Unbuffered output
172         $timeout = $config->{timeout} if (exists($config->{timeout}));
173
174         while ($strikes--) {
175             print "login: ";
176             alarm $timeout;
177             $uid = <STDIN>;
178             alarm 0;
179
180             print "password: ";
181             alarm $timeout;
182             $pwd = <STDIN>;
183             alarm 0;
184
185             $uid =~ s/[\r\n]+$//;
186             $pwd =~ s/[\r\n]+$//;
187
188             if (exists($config->{accounts}->{$uid})
189                 && ($pwd eq $config->{accounts}->{$uid}->password())) {
190                 $account = $config->{accounts}->{$uid};
191                 last;
192             } else {
193                 syslog("LOG_WARNING", "Invalid login attempt: '%s'", $uid);
194                 print("Invalid login\n");
195             }
196         }
197     }; # End of eval
198
199     if ($@) {
200         syslog("LOG_ERR", "telnet_transport: Login timed out");
201         die "Telnet Login Timed out";
202     } elsif (!defined($account)) {
203         syslog("LOG_ERR", "telnet_transport: Login Failed");
204         die "Login Failure";
205     } else {
206         print "Login OK.  Initiating SIP\n";
207     }
208
209     $self->{account} = $account;
210
211     $self->sip_protocol_loop();
212     syslog("LOG_INFO", "telnet_transport: shutting down");
213 }
214
215
216 sub http_transport {
217 }
218
219 #
220 # The terminal has logged in, using either the SIP login process
221 # over a raw socket, or via the pseudo-unix login provided by the
222 # telnet transport.  From that point on, both the raw and the telnet
223 # processes are the same:
224 sub sip_protocol_loop {
225     my $self = shift;
226     my $expect;
227     my $service = $self->{service};
228     my $config = $self->{config};
229     my $input;
230     # Now that the terminal has logged in, the first message
231     # we recieve must be an SC_STATUS message.  But it might be
232     # an SC_REQUEST_RESEND.  So, as long as we keep receiving
233     # SC_REQUEST_RESEND, we keep waiting for an SC_STATUS
234
235     # Comprise reports that no other ILS actually enforces this
236     # constraint, so we'll relax about it too.  As long as everybody
237     # uses the SIP "raw" login process, rather than telnet, this
238     # will be fine, becaues the LOGIN protocol exchange will force
239     # us into SIP 2.00 anyway.  Machines that want to log in using
240     # telnet MUST send an SC Status message first, even though we're
241     # not enforcing it.
242     # 
243     #$expect = SC_STATUS;
244     $expect = '';
245
246     while ($input = Sip::read_SIP_packet(*STDIN)) {
247         my $status;
248
249         $input =~ s/[\r\n]+$//sm;       # Strip off any trailing line ends
250
251         $status = Sip::MsgType::handle($input, $self, $expect);
252         next if $status eq REQUEST_ACS_RESEND;
253 #### stopped here rch
254         if (!$status) {
255             syslog("LOG_ERR", "raw_transport: failed to handle %s",
256                    substr($input, 0, 2));
257             die "raw_transport: dying";
258         } elsif ($expect && ($status ne $expect)) {
259             # We received a non-"RESEND" that wasn't what we were
260             # expecting.
261             syslog("LOG_ERR",
262                    "raw_transport: expected %s, received %s, exiting",
263                    $expect, $input);
264             die "raw_transport: exiting: expected '$expect', received '$status'";
265         }
266         # We successfully received and processed what we were expecting
267         # to receive
268         $expect = '';
269     }
270 }