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
42 our @ISA = qw(Net::Server::PreFork);
48 RAW => \&raw_transport,
49 telnet => \&telnet_transport,
50 http => \&http_transport,
55 my $config = Sip::Configuration->new($ARGV[0]);
62 foreach my $svc (keys %{$config->{listeners}}) {
63 push @parms, "port=" . $svc;
69 # Log lines look like this:
70 # Jun 16 21:21:31 server08 steve_sip: Sip::MsgType::_initialize('Login', ...)
71 # [ TIMESTAMP ] [ HOST ] [ IDENT ]: Message...
73 # The IDENT is determined by $ENV{SIP_LOG_IDENT}, if present.
74 # Otherwise it is "_sip" appended to $USER, if present, or "acs-server" as a fallback.
77 my $syslog_ident = $ENV{SIP_LOG_IDENT} || ($ENV{USER} ? $ENV{USER} . "_sip" : 'acs-server');
80 "log_file=Sys::Syslog",
81 "syslog_ident=$syslog_ident",
82 "syslog_facility=" . LOG_SIP;
85 # Server Management: set parameters for the Net::Server::PreFork
86 # module. The module silently ignores parameters that it doesn't
87 # recognize, and complains about invalid values for parameters
90 if (defined($config->{'server-params'})) {
91 while (my ($key, $val) = each %{$config->{'server-params'}}) {
92 push @parms, $key . '=' . $val;
99 # This is the main event.
100 SIPServer->run(@parms);
106 # process_request is the callback used by Net::Server to handle
107 # an incoming connection request.
109 sub process_request {
113 my ($sockaddr, $port, $proto);
116 $self->{account} = undef; # New connection, no need to keep login info
117 $self->{config} = $config;
119 $sockaddr = $self->{server}->{sockaddr};
120 $port = $self->{server}->{sockport};
121 $proto = $self->{server}->{client}->NS_proto();
122 syslog('LOG_INFO', "Inbound connection from $sockaddr on port $port and proto $proto");
124 $self->{service} = $config->find_service( $sockaddr, $port, $proto );
126 if (! defined($self->{service})) {
127 syslog( "LOG_ERR", "process_request: Unrecognized server connection: %s:%s/%s",
128 $sockaddr, $port, $proto );
129 die "process_request: Bad server connection";
132 $transport = $transports{ $self->{service}->{transport} };
134 if ( !defined($transport) ) {
135 syslog("LOG_WARNING", "Unknown transport '%s', dropping", $service->{transport});
139 # Transport has shut down, remove any lingering login info
140 $self->{account} = undef;
152 my $service = $self->{service};
155 my $timeout = $self->{service}->{timeout} || $self->{config}->{timeout} || 0;
158 local $SIG{ALRM} = sub { die "raw_transport Timed Out!\n"; };
159 syslog("LOG_DEBUG", "raw_transport: timeout is $timeout");
163 $input = Sip::read_SIP_packet(*STDIN);
168 syslog("LOG_INFO", "raw_transport: shutting down: EOF during login");
170 } elsif ($input !~ /\S/) {
171 syslog("LOG_INFO", "raw_transport: received whitespace line (length %s) during login, skipping", length($input));
174 $input =~ s/[\r\n]+$//sm; # Strip off trailing line terminator
175 last if Sip::MsgType::handle($input, $self, LOGIN);
180 syslog("LOG_ERR", "raw_transport: LOGIN ERROR: '$@'");
181 die "raw_transport: login error (timeout? $@), exiting";
182 } elsif (!$self->{account}) {
183 syslog("LOG_ERR", "raw_transport: LOGIN FAILED");
184 die "raw_transport: Login failed (no account), exiting";
187 syslog("LOG_DEBUG", "raw_transport: uname/inst: '%s/%s'",
188 $self->{account}->{id},
189 $self->{account}->{institution});
191 $self->sip_protocol_loop();
193 syslog("LOG_INFO", "raw_transport: shutting down");
196 sub telnet_transport {
202 my $config = $self->{config};
203 my $timeout = $self->{service}->{timeout} || $config->{timeout} || 0;
204 syslog("LOG_DEBUG", "telnet_transport: timeout is %s", $timeout);
206 # Until the terminal has logged in, we don't trust it
207 # so use a timeout to protect ourselves from hanging.
209 local $SIG{ALRM} = sub { die "telnet_transport: Timed Out ($timeout seconds)!\n";; };
210 local $| = 1; # Unbuffered output
223 $uid =~ s/[\r\n]+$//;
224 $pwd =~ s/[\r\n]+$//;
226 if (exists($config->{accounts}->{$uid})
227 && ($pwd eq $config->{accounts}->{$uid}->password())) {
228 $account = $config->{accounts}->{$uid};
231 syslog("LOG_WARNING", "Invalid login attempt: '%s'", $uid);
232 print("Invalid login$CRLF");
238 syslog("LOG_ERR", "telnet_transport: Login timed out");
239 die "Telnet Login Timed out";
240 } elsif (!defined($account)) {
241 syslog("LOG_ERR", "telnet_transport: Login Failed");
244 print "Login OK. Initiating SIP$CRLF";
247 $self->{account} = $account;
248 syslog("LOG_DEBUG", "telnet_transport: uname/inst: '%s/%s'", $account->{id}, $account->{institution});
249 $self->sip_protocol_loop();
250 syslog("LOG_INFO", "telnet_transport: shutting down");
258 # The terminal has logged in, using either the SIP login process
259 # over a raw socket, or via the pseudo-unix login provided by the
260 # telnet transport. From that point on, both the raw and the telnet
261 # processes are the same:
262 sub sip_protocol_loop {
265 my $service = $self->{service};
266 my $config = $self->{config};
269 # Now that the terminal has logged in, the first message
270 # we recieve must be an SC_STATUS message. But it might be
271 # an SC_REQUEST_RESEND. So, as long as we keep receiving
272 # SC_REQUEST_RESEND, we keep waiting for an SC_STATUS
274 # Comprise reports that no other ILS actually enforces this
275 # constraint, so we'll relax about it too. As long as everybody
276 # uses the SIP "raw" login process, rather than telnet, this
277 # will be fine, becaues the LOGIN protocol exchange will force
278 # us into SIP 2.00 anyway. Machines that want to log in using
279 # telnet MUST send an SC Status message first, even though we're
282 #$expect = SC_STATUS;
285 while ( $input = Sip::read_SIP_packet(*STDIN) ) {
286 $input =~ s/[\r\n]+$//sm; # Strip off any trailing line ends
288 my $status = Sip::MsgType::handle($input, $self, $expect);
289 next if $status eq REQUEST_ACS_RESEND;
292 syslog("LOG_ERR", "raw_transport: failed to handle %s", substr($input,0,2));
293 die "sip_protocol_loop: failed Sip::MsgType::handle('$input', $self, '$expect')";
295 elsif ($expect && ($status ne $expect)) {
296 # We received a non-"RESEND" that wasn't what we were expecting.
297 syslog("LOG_ERR", "raw_transport: expected %s, received %s, exiting", $expect, $input);
298 die "sip_protocol_loop: exiting: expected '$expect', received '$status'";
301 # We successfully received and processed what we were expecting