wr72054 fixing the z3950 search for acquisitions
[koha.git] / C4 / SIP / SIPServer.pm
blobc6de11e46442778b105e58a1be631bbb48e5736d
1 package SIPServer;
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 qw(:DEFAULT :crlf);
10 use Data::Dumper; # For debugging
11 require UNIVERSAL::require;
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;
19 use constant LOG_SIP => "local6"; # Local alias for the logging facility
21 use vars qw(@ISA $VERSION);
23 BEGIN {
24 $VERSION = 1.02;
25 @ISA = qw(Net::Server::PreFork);
29 # Main # not really, since package SIPServer
31 # FIXME: Is this a module or a script?
32 # A script with no MAIN namespace?
33 # A module that takes command line args?
35 my %transports = (
36 RAW => \&raw_transport,
37 telnet => \&telnet_transport,
38 # http => \&http_transport, # for http just use the OPAC
42 # Read configuration
44 my $config = new Sip::Configuration $ARGV[0];
45 print STDERR "SIPServer config: \n" . Dumper($config) . "\nEND SIPServer config.\n";
46 my @parms;
49 # Ports to bind
51 foreach my $svc (keys %{$config->{listeners}}) {
52 push @parms, "port=" . $svc;
56 # Logging
58 # Log lines look like this:
59 # Jun 16 21:21:31 server08 steve_sip[19305]: ILS::Transaction::Checkout performing checkout...
60 # [ TIMESTAMP ] [ HOST ] [ IDENT ] PID : Message...
62 # The IDENT is determined by config file 'server-params' arguments
66 # Server Management: set parameters for the Net::Server::PreFork
67 # module. The module silently ignores parameters that it doesn't
68 # recognize, and complains about invalid values for parameters
69 # that it does.
71 if (defined($config->{'server-params'})) {
72 while (my ($key, $val) = each %{$config->{'server-params'}}) {
73 push @parms, $key . '=' . $val;
77 print scalar(localtime), " -- startup -- procid:$$\n";
78 print "Params for Net::Server::PreFork : \n" . Dumper(\@parms);
81 # This is the main event.
82 __PACKAGE__ ->run(@parms);
85 # Child
88 # process_request is the callback used by Net::Server to handle
89 # an incoming connection request.
91 sub process_request {
92 my $self = shift;
93 my $service;
94 my ($sockaddr, $port, $proto);
95 my $transport;
97 $self->{config} = $config;
99 my $sockname = getsockname(STDIN);
100 ($port, $sockaddr) = sockaddr_in($sockname);
101 $sockaddr = inet_ntoa($sockaddr);
102 $proto = $self->{server}->{client}->NS_proto();
104 $self->{service} = $config->find_service($sockaddr, $port, $proto);
106 if (!defined($self->{service})) {
107 syslog("LOG_ERR", "process_request: Unknown recognized server connection: %s:%s/%s", $sockaddr, $port, $proto);
108 die "process_request: Bad server connection";
111 $transport = $transports{$self->{service}->{transport}};
113 if (!defined($transport)) {
114 syslog("LOG_WARNING", "Unknown transport '%s', dropping", $service->{transport});
115 return;
116 } else {
117 &$transport($self);
122 # Transports
125 sub raw_transport {
126 my $self = shift;
127 my ($input);
128 my $service = $self->{service};
129 my $strikes = 3;
131 eval {
132 local $SIG{ALRM} = sub { die "raw_transport Timed Out!\n"; };
133 syslog("LOG_DEBUG", "raw_transport: timeout is %d", $service->{timeout});
134 while ($strikes--) {
135 alarm $service->{timeout};
136 $input = Sip::read_SIP_packet(*STDIN);
137 alarm 0;
138 if (!$input) {
139 # EOF on the socket
140 syslog("LOG_INFO", "raw_transport: shutting down: EOF during login");
141 return;
143 $input =~ s/[\r\n]+$//sm; # Strip off trailing line terminator(s)
144 last if Sip::MsgType::handle($input, $self, LOGIN);
148 if (length $@) {
149 syslog("LOG_ERR", "raw_transport: LOGIN ERROR: '$@'");
150 die "raw_transport: login error (timeout? $@), exiting";
151 } elsif (!$self->{account}) {
152 syslog("LOG_ERR", "raw_transport: LOGIN FAILED");
153 die "raw_transport: Login failed (no account), exiting";
156 syslog("LOG_DEBUG", "raw_transport: uname/inst: '%s/%s'",
157 $self->{account}->{id},
158 $self->{account}->{institution});
160 $self->sip_protocol_loop();
161 syslog("LOG_INFO", "raw_transport: shutting down");
164 sub get_clean_string ($) {
165 my $string = shift;
166 if (defined $string) {
167 syslog("LOG_DEBUG", "get_clean_string pre-clean(length %s): %s", length($string), $string);
168 chomp($string);
169 $string =~ s/^[^A-z0-9]+//;
170 $string =~ s/[^A-z0-9]+$//;
171 syslog("LOG_DEBUG", "get_clean_string post-clean(length %s): %s", length($string), $string);
172 } else {
173 syslog("LOG_INFO", "get_clean_string called on undefined");
175 return $string;
178 sub get_clean_input {
179 local $/ = "\012";
180 my $in = <STDIN>;
181 $in = get_clean_string($in);
182 while (my $extra = <STDIN>){
183 syslog("LOG_ERR", "get_clean_input got extra lines: %s", $extra);
185 return $in;
188 sub telnet_transport {
189 my $self = shift;
190 my ($uid, $pwd);
191 my $strikes = 3;
192 my $account = undef;
193 my $input;
194 my $config = $self->{config};
195 my $timeout = $self->{service}->{timeout} || $config->{timeout} || 30;
196 syslog("LOG_DEBUG", "telnet_transport: timeout is %s", $timeout);
198 eval {
199 local $SIG{ALRM} = sub { die "telnet_transport: Timed Out ($timeout seconds)!\n"; };
200 local $| = 1; # Unbuffered output
201 $/ = "\015"; # Internet Record Separator (lax version)
202 # Until the terminal has logged in, we don't trust it
203 # so use a timeout to protect ourselves from hanging.
205 while ($strikes--) {
206 print "login: ";
207 alarm $timeout;
208 # $uid = &get_clean_input;
209 $uid = <STDIN>;
210 print "password: ";
211 # $pwd = &get_clean_input || '';
212 $pwd = <STDIN>;
213 alarm 0;
215 syslog("LOG_DEBUG", "telnet_transport 1: uid length %s, pwd length %s", length($uid), length($pwd));
216 $uid = get_clean_string ($uid);
217 $pwd = get_clean_string ($pwd);
218 syslog("LOG_DEBUG", "telnet_transport 2: uid length %s, pwd length %s", length($uid), length($pwd));
220 if (exists ($config->{accounts}->{$uid})
221 && ($pwd eq $config->{accounts}->{$uid}->password())) {
222 $account = $config->{accounts}->{$uid};
223 Sip::MsgType::login_core($self,$uid,$pwd) and last;
225 syslog("LOG_WARNING", "Invalid login attempt: '%s'", ($uid||''));
226 print("Invalid login$CRLF");
228 }; # End of eval
230 if ($@) {
231 syslog("LOG_ERR", "telnet_transport: Login timed out");
232 die "Telnet Login Timed out";
233 } elsif (!defined($account)) {
234 syslog("LOG_ERR", "telnet_transport: Login Failed");
235 die "Login Failure";
236 } else {
237 print "Login OK. Initiating SIP$CRLF";
240 $self->{account} = $account;
241 syslog("LOG_DEBUG", "telnet_transport: uname/inst: '%s/%s'", $account->{id}, $account->{institution});
242 $self->sip_protocol_loop();
243 syslog("LOG_INFO", "telnet_transport: shutting down");
247 # The terminal has logged in, using either the SIP login process
248 # over a raw socket, or via the pseudo-unix login provided by the
249 # telnet transport. From that point on, both the raw and the telnet
250 # processes are the same:
251 sub sip_protocol_loop {
252 my $self = shift;
253 my $service = $self->{service};
254 my $config = $self->{config};
255 my $input;
257 # The spec says the first message will be:
258 # SIP v1: SC_STATUS
259 # SIP v2: LOGIN (or SC_STATUS via telnet?)
260 # But it might be SC_REQUEST_RESEND. As long as we get
261 # SC_REQUEST_RESEND, we keep waiting.
263 # Comprise reports that no other ILS actually enforces this
264 # constraint, so we'll relax about it too.
265 # Using the SIP "raw" login process, rather than telnet,
266 # requires the LOGIN message and forces SIP 2.00. In that
267 # case, the LOGIN message has already been processed (above).
269 # In short, we'll take any valid message here.
270 #my $expect = SC_STATUS;
271 my $expect = '';
272 my $strikes = 3;
273 while ($input = Sip::read_SIP_packet(*STDIN)) {
274 # begin input hacks ... a cheap stand in for better Telnet layer
275 $input =~ s/^[^A-z0-9]+//s; # Kill leading bad characters... like Telnet handshakers
276 $input =~ s/[^A-z0-9]+$//s; # Same on the end, should get DOSsy ^M line-endings too.
277 while (chomp($input)) {warn "Extra line ending on input";}
278 unless ($input) {
279 if ($strikes--) {
280 syslog("LOG_ERR", "sip_protocol_loop: empty input skipped");
281 next;
282 } else {
283 syslog("LOG_ERR", "sip_protocol_loop: quitting after too many errors");
284 die "sip_protocol_loop: quitting after too many errors";
287 # end cheap input hacks
288 my $status = Sip::MsgType::handle($input, $self, $expect);
289 if (!$status) {
290 syslog("LOG_ERR", "sip_protocol_loop: failed to handle %s",substr($input,0,2));
291 die "sip_protocol_loop: failed Sip::MsgType::handle('$input', $self, '$expect')";
293 next if $status eq REQUEST_ACS_RESEND;
294 if ($expect && ($status ne $expect)) {
295 # We received a non-"RESEND" that wasn't what we were expecting.
296 syslog("LOG_ERR", "sip_protocol_loop: expected %s, received %s, exiting", $expect, $input);
297 die "sip_protocol_loop: exiting: expected '$expect', received '$status'";
299 # We successfully received and processed what we were expecting
300 $expect = '';
305 __END__