2 # Copyright (C) 2006-2008 Georgia Public Library Service
4 # Author: David J. Fiander
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of version 2 of the GNU General Public
8 # License as published by the Free Software Foundation.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public
16 # License along with this program; if not, write to the Free
17 # Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25 use Sys::Syslog qw(syslog);
26 use Net::Server::PreFork;
27 use Net::Server::Proto;
30 use Data::Dumper; # For debugging
31 require UNIVERSAL::require;
33 #use Sip qw(readline);
34 use Sip::Constants qw(:all);
35 use Sip::Configuration;
36 use Sip::Checksum qw(checksum verify_cksum);
39 use constant LOG_SIP => "local6"; # Local alias for the logging facility
41 our @ISA = qw(Net::Server::PreFork);
47 RAW => \&raw_transport,
48 telnet => \&telnet_transport,
49 http => \&http_transport,
54 my $config = new Sip::Configuration $ARGV[0];
61 foreach my $svc (keys %{$config->{listeners}}) {
62 push @parms, "port=" . $svc;
68 # Log lines look like this:
69 # Jun 16 21:21:31 server08 steve_sip: Sip::MsgType::_initialize('Login', ...)
70 # [ TIMESTAMP ] [ HOST ] [ IDENT ]: Message...
72 # The IDENT is determined by $ENV{SIP_LOG_IDENT}, if present.
73 # Otherwise it is "_sip" appended to $USER, if present, or "acs-server" as a fallback.
76 my $syslog_ident = $ENV{SIP_LOG_IDENT} || ($ENV{USER} ? $ENV{USER} . "_sip" : 'acs-server');
79 "log_file=Sys::Syslog",
80 "syslog_ident=$syslog_ident",
81 "syslog_facility=" . LOG_SIP;
84 # Server Management: set parameters for the Net::Server::PreFork
85 # module. The module silently ignores parameters that it doesn't
86 # recognize, and complains about invalid values for parameters
89 if (defined($config->{'server-params'})) {
90 while (my ($key, $val) = each %{$config->{'server-params'}}) {
91 push @parms, $key . '=' . $val;
98 # This is the main event.
99 SIPServer->run(@parms);
105 # process_request is the callback used by Net::Server to handle
106 # an incoming connection request.
108 sub process_request {
112 my ($sockaddr, $port, $proto);
115 $self->{config} = $config;
117 $sockaddr = $self->{server}->{sockaddr};
118 $port = $self->{server}->{sockport};
119 $proto = $self->{server}->{client}->NS_proto();
120 syslog('LOG_INFO', "Inbound connection from $sockaddr on port $port and proto $proto");
122 $self->{service} = $config->find_service( $sockaddr, $port, $proto );
124 if (! defined($self->{service})) {
125 syslog( "LOG_ERR", "process_request: Unknown recognized server connection: %s:%s/%s",
126 $sockaddr, $port, $proto );
127 die "process_request: Bad server connection";
130 $transport = $transports{ $self->{service}->{transport} };
132 if ( !defined($transport) ) {
133 syslog("LOG_WARNING", "Unknown transport '%s', dropping", $service->{transport});
148 my $service = $self->{service};
154 local $SIG{ALRM} = sub { die "raw_transport Timed Out!\n"; };
155 syslog("LOG_DEBUG", "raw_transport: timeout is %d", $service->{timeout});
158 alarm $service->{timeout};
159 $input = Sip::read_SIP_packet(*STDIN);
164 syslog("LOG_INFO", "raw_transport: shutting down: EOF during login");
167 $input =~ s/[\r\n]+$//sm; # Strip off trailing line terminator
168 last if Sip::MsgType::handle($input, $self, LOGIN);
173 syslog("LOG_ERR", "raw_transport: LOGIN ERROR: '$@'");
174 die "raw_transport: login error (timeout? $@), exiting";
175 } elsif (!$self->{account}) {
176 syslog("LOG_ERR", "raw_transport: LOGIN FAILED");
177 die "raw_transport: Login failed (no account), exiting";
180 syslog("LOG_DEBUG", "raw_transport: uname/inst: '%s/%s'",
181 $self->{account}->{id},
182 $self->{account}->{institution});
184 $self->sip_protocol_loop();
186 syslog("LOG_INFO", "raw_transport: shutting down");
189 sub telnet_transport {
195 my $config = $self->{config};
196 my $timeout = $self->{service}->{timeout} || $config->{timeout} || 0;
197 syslog("LOG_DEBUG", "telnet_transport: timeout is %s", $timeout);
199 # Until the terminal has logged in, we don't trust it
200 # so use a timeout to protect ourselves from hanging.
202 local $SIG{ALRM} = sub { die "telnet_transport: Timed Out ($timeout seconds)!\n";; };
203 local $| = 1; # Unbuffered output
216 $uid =~ s/[\r\n]+$//;
217 $pwd =~ s/[\r\n]+$//;
219 if (exists($config->{accounts}->{$uid})
220 && ($pwd eq $config->{accounts}->{$uid}->password())) {
221 $account = $config->{accounts}->{$uid};
224 syslog("LOG_WARNING", "Invalid login attempt: '%s'", $uid);
225 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");
251 # The terminal has logged in, using either the SIP login process
252 # over a raw socket, or via the pseudo-unix login provided by the
253 # telnet transport. From that point on, both the raw and the telnet
254 # processes are the same:
255 sub sip_protocol_loop {
258 my $service = $self->{service};
259 my $config = $self->{config};
262 # Now that the terminal has logged in, the first message
263 # we recieve must be an SC_STATUS message. But it might be
264 # an SC_REQUEST_RESEND. So, as long as we keep receiving
265 # SC_REQUEST_RESEND, we keep waiting for an SC_STATUS
267 # Comprise reports that no other ILS actually enforces this
268 # constraint, so we'll relax about it too. As long as everybody
269 # uses the SIP "raw" login process, rather than telnet, this
270 # will be fine, becaues the LOGIN protocol exchange will force
271 # us into SIP 2.00 anyway. Machines that want to log in using
272 # telnet MUST send an SC Status message first, even though we're
275 #$expect = SC_STATUS;
278 while ( $input = Sip::read_SIP_packet(*STDIN) ) {
279 $input =~ s/[\r\n]+$//sm; # Strip off any trailing line ends
281 my $status = Sip::MsgType::handle($input, $self, $expect);
282 next if $status eq REQUEST_ACS_RESEND;
285 syslog("LOG_ERR", "raw_transport: failed to handle %s", substr($input,0,2));
286 die "sip_protocol_loop: failed Sip::MsgType::handle('$input', $self, '$expect')";
288 elsif ($expect && ($status ne $expect)) {
289 # We received a non-"RESEND" that wasn't what we were expecting.
290 syslog("LOG_ERR", "raw_transport: expected %s, received %s, exiting", $expect, $input);
291 die "sip_protocol_loop: exiting: expected '$expect', received '$status'";
294 # We successfully received and processed what we were expecting