6 use Sys
::Syslog
qw(syslog);
7 use Net
::Server
::PreFork
;
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);
19 use constant LOG_SIP
=> "local6"; # Local alias for the logging facility
21 use vars
qw(@ISA $VERSION);
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?
36 RAW
=> \
&raw_transport
,
37 telnet
=> \
&telnet_transport
,
38 # http => \&http_transport, # for http just use the OPAC
44 my $config = new Sip
::Configuration
$ARGV[0];
45 print STDERR
"SIPServer config: \n" . Dumper
($config) . "\nEND SIPServer config.\n";
51 foreach my $svc (keys %{$config->{listeners
}}) {
52 push @parms, "port=" . $svc;
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
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);
88 # process_request is the callback used by Net::Server to handle
89 # an incoming connection request.
94 my ($sockaddr, $port, $proto);
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
});
128 my $service = $self->{service
};
132 local $SIG{ALRM
} = sub { die "raw_transport Timed Out!\n"; };
133 syslog
("LOG_DEBUG", "raw_transport: timeout is %d", $service->{timeout
});
135 alarm $service->{timeout
};
136 $input = Sip
::read_SIP_packet
(*STDIN
);
140 syslog
("LOG_INFO", "raw_transport: shutting down: EOF during login");
143 $input =~ s/[\r\n]+$//sm; # Strip off trailing line terminator(s)
144 last if Sip
::MsgType
::handle
($input, $self, LOGIN
);
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
($) {
166 if (defined $string) {
167 syslog
("LOG_DEBUG", "get_clean_string pre-clean(length %s): %s", length($string), $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);
173 syslog
("LOG_INFO", "get_clean_string called on undefined");
178 sub get_clean_input
{
181 $in = get_clean_string
($in);
182 while (my $extra = <STDIN
>){
183 syslog
("LOG_ERR", "get_clean_input got extra lines: %s", $extra);
188 sub telnet_transport
{
194 my $config = $self->{config
};
195 my $timeout = $self->{service
}->{timeout
} || $config->{timeout
} || 30;
196 syslog
("LOG_DEBUG", "telnet_transport: timeout is %s", $timeout);
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.
208 # $uid = &get_clean_input;
211 # $pwd = &get_clean_input || '';
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");
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");
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
{
253 my $service = $self->{service
};
254 my $config = $self->{config
};
257 # The spec says the first message will be:
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;
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";}
280 syslog
("LOG_ERR", "sip_protocol_loop: empty input skipped");
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);
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