Bug 15092: item type descriptions not showing in OPAC advsearch
[koha.git] / C4 / SIP / SIPServer.pm
blob8722ab6b3d98ddc64e267d49166c60a2c5f13563
1 #!/usr/bin/perl
2 package C4::SIP::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 C4::SIP::Sip::Constants qw(:all);
15 use C4::SIP::Sip::Configuration;
16 use C4::SIP::Sip::Checksum qw(checksum verify_cksum);
17 use C4::SIP::Sip::MsgType qw( handle login_core );
18 use C4::SIP::Sip qw( read_SIP_packet );
20 use base qw(Net::Server::PreFork);
22 use constant LOG_SIP => "local6"; # Local alias for the logging facility
25 # Main # not really, since package SIPServer
27 # FIXME: Is this a module or a script?
28 # A script with no MAIN namespace?
29 # A module that takes command line args?
31 my %transports = (
32 RAW => \&raw_transport,
33 telnet => \&telnet_transport,
37 # Read configuration
39 my $config = C4::SIP::Sip::Configuration->new( $ARGV[0] );
40 my @parms;
43 # Ports to bind
45 foreach my $svc (keys %{$config->{listeners}}) {
46 push @parms, "port=" . $svc;
50 # Logging
52 # Log lines look like this:
53 # Jun 16 21:21:31 server08 steve_sip[19305]: ILS::Transaction::Checkout performing checkout...
54 # [ TIMESTAMP ] [ HOST ] [ IDENT ] PID : Message...
56 # The IDENT is determined by config file 'server-params' arguments
60 # Server Management: set parameters for the Net::Server::PreFork
61 # module. The module silently ignores parameters that it doesn't
62 # recognize, and complains about invalid values for parameters
63 # that it does.
65 if (defined($config->{'server-params'})) {
66 while (my ($key, $val) = each %{$config->{'server-params'}}) {
67 push @parms, $key . '=' . $val;
73 # This is the main event.
74 __PACKAGE__ ->run(@parms);
77 # Child
80 # process_request is the callback used by Net::Server to handle
81 # an incoming connection request.
83 sub process_request {
84 my $self = shift;
85 my $service;
86 my ($sockaddr, $port, $proto);
87 my $transport;
89 $self->{config} = $config;
91 my $sockname = getsockname(STDIN);
93 # Check if socket connection is IPv6 before resolving address
94 my $family = Socket::sockaddr_family($sockname);
95 if ($family == AF_INET6) {
96 ($port, $sockaddr) = sockaddr_in6($sockname);
97 $sockaddr = Socket::inet_ntop(AF_INET6, $sockaddr);
98 } else {
99 ($port, $sockaddr) = sockaddr_in($sockname);
100 $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};
130 while (!$self->{account}) {
131 local $SIG{ALRM} = sub { die "raw_transport Timed Out!\n"; };
132 syslog("LOG_DEBUG", "raw_transport: timeout is %d", $service->{timeout});
133 $input = read_SIP_packet(*STDIN);
134 if (!$input) {
135 # EOF on the socket
136 syslog("LOG_INFO", "raw_transport: shutting down: EOF during login");
137 return;
139 $input =~ s/[\r\n]+$//sm; # Strip off trailing line terminator(s)
140 last if C4::SIP::Sip::MsgType::handle($input, $self, LOGIN);
143 syslog("LOG_DEBUG", "raw_transport: uname/inst: '%s/%s'",
144 $self->{account}->{id},
145 $self->{account}->{institution});
147 $self->sip_protocol_loop();
148 syslog("LOG_INFO", "raw_transport: shutting down");
151 sub get_clean_string {
152 my $string = shift;
153 if (defined $string) {
154 syslog("LOG_DEBUG", "get_clean_string pre-clean(length %s): %s", length($string), $string);
155 chomp($string);
156 $string =~ s/^[^A-z0-9]+//;
157 $string =~ s/[^A-z0-9]+$//;
158 syslog("LOG_DEBUG", "get_clean_string post-clean(length %s): %s", length($string), $string);
159 } else {
160 syslog("LOG_INFO", "get_clean_string called on undefined");
162 return $string;
165 sub get_clean_input {
166 local $/ = "\012";
167 my $in = <STDIN>;
168 $in = get_clean_string($in);
169 while (my $extra = <STDIN>){
170 syslog("LOG_ERR", "get_clean_input got extra lines: %s", $extra);
172 return $in;
175 sub telnet_transport {
176 my $self = shift;
177 my ($uid, $pwd);
178 my $strikes = 3;
179 my $account = undef;
180 my $input;
181 my $config = $self->{config};
182 my $timeout = $self->{service}->{timeout} || $config->{timeout} || 30;
183 syslog("LOG_DEBUG", "telnet_transport: timeout is %s", $timeout);
185 eval {
186 local $SIG{ALRM} = sub { die "telnet_transport: Timed Out ($timeout seconds)!\n"; };
187 local $| = 1; # Unbuffered output
188 $/ = "\015"; # Internet Record Separator (lax version)
189 # Until the terminal has logged in, we don't trust it
190 # so use a timeout to protect ourselves from hanging.
192 while ($strikes--) {
193 print "login: ";
194 alarm $timeout;
195 # $uid = &get_clean_input;
196 $uid = <STDIN>;
197 print "password: ";
198 # $pwd = &get_clean_input || '';
199 $pwd = <STDIN>;
200 alarm 0;
202 syslog("LOG_DEBUG", "telnet_transport 1: uid length %s, pwd length %s", length($uid), length($pwd));
203 $uid = get_clean_string ($uid);
204 $pwd = get_clean_string ($pwd);
205 syslog("LOG_DEBUG", "telnet_transport 2: uid length %s, pwd length %s", length($uid), length($pwd));
207 if (exists ($config->{accounts}->{$uid})
208 && ($pwd eq $config->{accounts}->{$uid}->{password})) {
209 $account = $config->{accounts}->{$uid};
210 if ( C4::SIP::Sip::MsgType::login_core($self,$uid,$pwd) ) {
211 last;
214 syslog("LOG_WARNING", "Invalid login attempt: '%s'", ($uid||''));
215 print("Invalid login$CRLF");
217 }; # End of eval
219 if ($@) {
220 syslog("LOG_ERR", "telnet_transport: Login timed out");
221 die "Telnet Login Timed out";
222 } elsif (!defined($account)) {
223 syslog("LOG_ERR", "telnet_transport: Login Failed");
224 die "Login Failure";
225 } else {
226 print "Login OK. Initiating SIP$CRLF";
229 $self->{account} = $account;
230 syslog("LOG_DEBUG", "telnet_transport: uname/inst: '%s/%s'", $account->{id}, $account->{institution});
231 $self->sip_protocol_loop();
232 syslog("LOG_INFO", "telnet_transport: shutting down");
236 # The terminal has logged in, using either the SIP login process
237 # over a raw socket, or via the pseudo-unix login provided by the
238 # telnet transport. From that point on, both the raw and the telnet
239 # processes are the same:
240 sub sip_protocol_loop {
241 my $self = shift;
242 my $service = $self->{service};
243 my $config = $self->{config};
244 my $timeout = $self->{service}->{timeout} || $config->{timeout} || 30;
245 my $input;
247 # The spec says the first message will be:
248 # SIP v1: SC_STATUS
249 # SIP v2: LOGIN (or SC_STATUS via telnet?)
250 # But it might be SC_REQUEST_RESEND. As long as we get
251 # SC_REQUEST_RESEND, we keep waiting.
253 # Comprise reports that no other ILS actually enforces this
254 # constraint, so we'll relax about it too.
255 # Using the SIP "raw" login process, rather than telnet,
256 # requires the LOGIN message and forces SIP 2.00. In that
257 # case, the LOGIN message has already been processed (above).
259 # In short, we'll take any valid message here.
260 #my $expect = SC_STATUS;
261 local $SIG{ALRM} = sub { die "SIP Timed Out!\n"; };
262 my $expect = '';
263 while (1) {
264 alarm $timeout;
265 $input = read_SIP_packet(*STDIN);
266 unless ($input) {
267 return; # EOF
269 # begin input hacks ... a cheap stand in for better Telnet layer
270 $input =~ s/^[^A-z0-9]+//s; # Kill leading bad characters... like Telnet handshakers
271 $input =~ s/[^A-z0-9]+$//s; # Same on the end, should get DOSsy ^M line-endings too.
272 while (chomp($input)) {warn "Extra line ending on input";}
273 unless ($input) {
274 syslog("LOG_ERR", "sip_protocol_loop: empty input skipped");
275 print("96$CR");
276 next;
278 # end cheap input hacks
279 my $status = handle($input, $self, $expect);
280 if (!$status) {
281 syslog("LOG_ERR", "sip_protocol_loop: failed to handle %s",substr($input,0,2));
283 next if $status eq REQUEST_ACS_RESEND;
284 if ($expect && ($status ne $expect)) {
285 # We received a non-"RESEND" that wasn't what we were expecting.
286 syslog("LOG_ERR", "sip_protocol_loop: expected %s, received %s, exiting", $expect, $input);
288 # We successfully received and processed what we were expecting
289 $expect = '';
290 alarm 0;
295 __END__