Bug 13315 - Add feedback for last item checked out to circulation.pl
[koha.git] / C4 / SIP / SIPServer.pm
blobe891a054e28c68293b9707635bb22d4c5dc8ef9c
1 #!/usr/bin/perl
2 package SIPServer;
4 use strict;
5 use warnings;
6 use FindBin qw($Bin);
7 use lib "$Bin";
8 use Sys::Syslog qw(syslog);
9 use Net::Server::PreFork;
10 use IO::Socket::INET;
11 use Socket qw(:DEFAULT :crlf);
12 require UNIVERSAL::require;
14 use Sip::Constants qw(:all);
15 use Sip::Configuration;
16 use Sip::Checksum qw(checksum verify_cksum);
17 use Sip::MsgType;
19 use base qw(Net::Server::PreFork);
21 use constant LOG_SIP => "local6"; # Local alias for the logging facility
24 # Main # not really, since package SIPServer
26 # FIXME: Is this a module or a script?
27 # A script with no MAIN namespace?
28 # A module that takes command line args?
30 my %transports = (
31 RAW => \&raw_transport,
32 telnet => \&telnet_transport,
36 # Read configuration
38 my $config = new Sip::Configuration $ARGV[0];
39 my @parms;
42 # Ports to bind
44 foreach my $svc (keys %{$config->{listeners}}) {
45 push @parms, "port=" . $svc;
49 # Logging
51 # Log lines look like this:
52 # Jun 16 21:21:31 server08 steve_sip[19305]: ILS::Transaction::Checkout performing checkout...
53 # [ TIMESTAMP ] [ HOST ] [ IDENT ] PID : Message...
55 # The IDENT is determined by config file 'server-params' arguments
59 # Server Management: set parameters for the Net::Server::PreFork
60 # module. The module silently ignores parameters that it doesn't
61 # recognize, and complains about invalid values for parameters
62 # that it does.
64 if (defined($config->{'server-params'})) {
65 while (my ($key, $val) = each %{$config->{'server-params'}}) {
66 push @parms, $key . '=' . $val;
72 # This is the main event.
73 __PACKAGE__ ->run(@parms);
76 # Child
79 # process_request is the callback used by Net::Server to handle
80 # an incoming connection request.
82 sub process_request {
83 my $self = shift;
84 my $service;
85 my ($sockaddr, $port, $proto);
86 my $transport;
88 $self->{config} = $config;
90 my $sockname = getsockname(STDIN);
92 # Check if socket connection is IPv6 before resolving address
93 my $family = Socket::sockaddr_family($sockname);
94 if ($family == AF_INET6) {
95 ($port, $sockaddr) = sockaddr_in6($sockname);
96 $sockaddr = Socket::inet_ntop(AF_INET6, $sockaddr);
97 } else {
98 ($port, $sockaddr) = sockaddr_in($sockname);
99 $sockaddr = inet_ntoa($sockaddr);
101 $proto = $self->{server}->{client}->NS_proto();
103 $self->{service} = $config->find_service($sockaddr, $port, $proto);
105 if (!defined($self->{service})) {
106 syslog("LOG_ERR", "process_request: Unknown recognized server connection: %s:%s/%s", $sockaddr, $port, $proto);
107 die "process_request: Bad server connection";
110 $transport = $transports{$self->{service}->{transport}};
112 if (!defined($transport)) {
113 syslog("LOG_WARNING", "Unknown transport '%s', dropping", $service->{transport});
114 return;
115 } else {
116 &$transport($self);
121 # Transports
124 sub raw_transport {
125 my $self = shift;
126 my ($input);
127 my $service = $self->{service};
129 while (!$self->{account}) {
130 local $SIG{ALRM} = sub { die "raw_transport Timed Out!\n"; };
131 syslog("LOG_DEBUG", "raw_transport: timeout is %d", $service->{timeout});
132 $input = Sip::read_SIP_packet(*STDIN);
133 if (!$input) {
134 # EOF on the socket
135 syslog("LOG_INFO", "raw_transport: shutting down: EOF during login");
136 return;
138 $input =~ s/[\r\n]+$//sm; # Strip off trailing line terminator(s)
139 last if Sip::MsgType::handle($input, $self, LOGIN);
142 syslog("LOG_DEBUG", "raw_transport: uname/inst: '%s/%s'",
143 $self->{account}->{id},
144 $self->{account}->{institution});
146 $self->sip_protocol_loop();
147 syslog("LOG_INFO", "raw_transport: shutting down");
150 sub get_clean_string {
151 my $string = shift;
152 if (defined $string) {
153 syslog("LOG_DEBUG", "get_clean_string pre-clean(length %s): %s", length($string), $string);
154 chomp($string);
155 $string =~ s/^[^A-z0-9]+//;
156 $string =~ s/[^A-z0-9]+$//;
157 syslog("LOG_DEBUG", "get_clean_string post-clean(length %s): %s", length($string), $string);
158 } else {
159 syslog("LOG_INFO", "get_clean_string called on undefined");
161 return $string;
164 sub get_clean_input {
165 local $/ = "\012";
166 my $in = <STDIN>;
167 $in = get_clean_string($in);
168 while (my $extra = <STDIN>){
169 syslog("LOG_ERR", "get_clean_input got extra lines: %s", $extra);
171 return $in;
174 sub telnet_transport {
175 my $self = shift;
176 my ($uid, $pwd);
177 my $strikes = 3;
178 my $account = undef;
179 my $input;
180 my $config = $self->{config};
181 my $timeout = $self->{service}->{timeout} || $config->{timeout} || 30;
182 syslog("LOG_DEBUG", "telnet_transport: timeout is %s", $timeout);
184 eval {
185 local $SIG{ALRM} = sub { die "telnet_transport: Timed Out ($timeout seconds)!\n"; };
186 local $| = 1; # Unbuffered output
187 $/ = "\015"; # Internet Record Separator (lax version)
188 # Until the terminal has logged in, we don't trust it
189 # so use a timeout to protect ourselves from hanging.
191 while ($strikes--) {
192 print "login: ";
193 alarm $timeout;
194 # $uid = &get_clean_input;
195 $uid = <STDIN>;
196 print "password: ";
197 # $pwd = &get_clean_input || '';
198 $pwd = <STDIN>;
199 alarm 0;
201 syslog("LOG_DEBUG", "telnet_transport 1: uid length %s, pwd length %s", length($uid), length($pwd));
202 $uid = get_clean_string ($uid);
203 $pwd = get_clean_string ($pwd);
204 syslog("LOG_DEBUG", "telnet_transport 2: uid length %s, pwd length %s", length($uid), length($pwd));
206 if (exists ($config->{accounts}->{$uid})
207 && ($pwd eq $config->{accounts}->{$uid}->password())) {
208 $account = $config->{accounts}->{$uid};
209 Sip::MsgType::login_core($self,$uid,$pwd) and last;
211 syslog("LOG_WARNING", "Invalid login attempt: '%s'", ($uid||''));
212 print("Invalid login$CRLF");
214 }; # End of eval
216 if ($@) {
217 syslog("LOG_ERR", "telnet_transport: Login timed out");
218 die "Telnet Login Timed out";
219 } elsif (!defined($account)) {
220 syslog("LOG_ERR", "telnet_transport: Login Failed");
221 die "Login Failure";
222 } else {
223 print "Login OK. Initiating SIP$CRLF";
226 $self->{account} = $account;
227 syslog("LOG_DEBUG", "telnet_transport: uname/inst: '%s/%s'", $account->{id}, $account->{institution});
228 $self->sip_protocol_loop();
229 syslog("LOG_INFO", "telnet_transport: shutting down");
233 # The terminal has logged in, using either the SIP login process
234 # over a raw socket, or via the pseudo-unix login provided by the
235 # telnet transport. From that point on, both the raw and the telnet
236 # processes are the same:
237 sub sip_protocol_loop {
238 my $self = shift;
239 my $service = $self->{service};
240 my $config = $self->{config};
241 my $timeout = $self->{service}->{timeout} || $config->{timeout} || 30;
242 my $input;
244 # The spec says the first message will be:
245 # SIP v1: SC_STATUS
246 # SIP v2: LOGIN (or SC_STATUS via telnet?)
247 # But it might be SC_REQUEST_RESEND. As long as we get
248 # SC_REQUEST_RESEND, we keep waiting.
250 # Comprise reports that no other ILS actually enforces this
251 # constraint, so we'll relax about it too.
252 # Using the SIP "raw" login process, rather than telnet,
253 # requires the LOGIN message and forces SIP 2.00. In that
254 # case, the LOGIN message has already been processed (above).
256 # In short, we'll take any valid message here.
257 #my $expect = SC_STATUS;
258 local $SIG{ALRM} = sub { die "SIP Timed Out!\n"; };
259 my $expect = '';
260 while (1) {
261 alarm $timeout;
262 $input = Sip::read_SIP_packet(*STDIN);
263 unless ($input) {
264 return; # EOF
266 # begin input hacks ... a cheap stand in for better Telnet layer
267 $input =~ s/^[^A-z0-9]+//s; # Kill leading bad characters... like Telnet handshakers
268 $input =~ s/[^A-z0-9]+$//s; # Same on the end, should get DOSsy ^M line-endings too.
269 while (chomp($input)) {warn "Extra line ending on input";}
270 unless ($input) {
271 syslog("LOG_ERR", "sip_protocol_loop: empty input skipped");
272 print("96$CR");
273 next;
275 # end cheap input hacks
276 my $status = Sip::MsgType::handle($input, $self, $expect);
277 if (!$status) {
278 syslog("LOG_ERR", "sip_protocol_loop: failed to handle %s",substr($input,0,2));
280 next if $status eq REQUEST_ACS_RESEND;
281 if ($expect && ($status ne $expect)) {
282 # We received a non-"RESEND" that wasn't what we were expecting.
283 syslog("LOG_ERR", "sip_protocol_loop: expected %s, received %s, exiting", $expect, $input);
285 # We successfully received and processed what we were expecting
286 $expect = '';
287 alarm 0;
292 __END__