forked from evergreen-library-system/SIPServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SIPServer.pm
301 lines (250 loc) · 8.69 KB
/
SIPServer.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#
# Copyright (C) 2006-2008 Georgia Public Library Service
#
# Author: David J. Fiander
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of version 2 of the GNU General Public
# License as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the Free
# Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307 USA
package SIPServer;
use strict;
use warnings;
use Exporter;
use Sys::Syslog qw(syslog);
use Net::Server::PreFork;
use Net::Server::Proto;
use IO::Socket::INET;
use Socket qw(:crlf);
use Data::Dumper; # For debugging
require UNIVERSAL::require;
#use Sip qw(readline);
use Sip::Constants qw(:all);
use Sip::Configuration;
use Sip::Checksum qw(checksum verify_cksum);
use Sip::MsgType;
use constant LOG_SIP => "local6"; # Local alias for the logging facility
our $VERSION = 0.02;
our @ISA = qw(Net::Server::PreFork);
#
# Main
#
my %transports = (
RAW => \&raw_transport,
telnet => \&telnet_transport,
http => \&http_transport,
);
# Read configuration
my $config = Sip::Configuration->new($ARGV[0]);
my @parms;
#
# Ports to bind
#
foreach my $svc (keys %{$config->{listeners}}) {
push @parms, "port=" . $svc;
}
#
# Logging
#
# Log lines look like this:
# Jun 16 21:21:31 server08 steve_sip: Sip::MsgType::_initialize('Login', ...)
# [ TIMESTAMP ] [ HOST ] [ IDENT ]: Message...
#
# The IDENT is determined by $ENV{SIP_LOG_IDENT}, if present.
# Otherwise it is "_sip" appended to $USER, if present, or "acs-server" as a fallback.
#
my $syslog_ident = $ENV{SIP_LOG_IDENT} || ($ENV{USER} ? $ENV{USER} . "_sip" : 'acs-server');
push @parms,
"log_file=Sys::Syslog",
"syslog_ident=$syslog_ident",
"syslog_facility=" . LOG_SIP;
#
# Server Management: set parameters for the Net::Server::PreFork
# module. The module silently ignores parameters that it doesn't
# recognize, and complains about invalid values for parameters
# that it does.
#
if (defined($config->{'server-params'})) {
while (my ($key, $val) = each %{$config->{'server-params'}}) {
push @parms, $key . '=' . $val;
}
}
print Dumper(@parms);
#
# This is the main event.
SIPServer->run(@parms);
#
# Child
#
# process_request is the callback used by Net::Server to handle
# an incoming connection request.
sub process_request {
my $self = shift;
my $service;
my $sockname;
my ($sockaddr, $port, $proto);
my $transport;
$self->{config} = $config;
$sockaddr = $self->{server}->{sockaddr};
$port = $self->{server}->{sockport};
$proto = $self->{server}->{client}->NS_proto();
syslog('LOG_INFO', "Inbound connection from $sockaddr on port $port and proto $proto");
$self->{service} = $config->find_service( $sockaddr, $port, $proto );
if (! defined($self->{service})) {
syslog( "LOG_ERR", "process_request: Unrecognized server connection: %s:%s/%s",
$sockaddr, $port, $proto );
die "process_request: Bad server connection";
}
$transport = $transports{ $self->{service}->{transport} };
if ( !defined($transport) ) {
syslog("LOG_WARNING", "Unknown transport '%s', dropping", $service->{transport});
return;
} else {
&$transport($self);
}
}
#
# Transports
#
sub raw_transport {
my $self = shift;
my ($uid, $pwd);
my $input;
my $service = $self->{service};
my $strikes = 3;
my $inst;
my $timeout = $self->{service}->{timeout} || $self->{config}->{timeout} || 0;
eval {
local $SIG{ALRM} = sub { die "raw_transport Timed Out!\n"; };
syslog("LOG_DEBUG", "raw_transport: timeout is $timeout");
while ($strikes--) {
alarm $timeout;
$input = Sip::read_SIP_packet(*STDIN);
alarm 0;
if (!$input) {
# EOF on the socket
syslog("LOG_INFO", "raw_transport: shutting down: EOF during login");
return;
} elsif ($input !~ /\S/) {
syslog("LOG_INFO", "raw_transport: received whitespace line (length %s) during login, skipping", length($input));
next;
}
$input =~ s/[\r\n]+$//sm; # Strip off trailing line terminator
last if Sip::MsgType::handle($input, $self, LOGIN);
}
};
if ($@) {
syslog("LOG_ERR", "raw_transport: LOGIN ERROR: '$@'");
die "raw_transport: login error (timeout? $@), exiting";
} elsif (!$self->{account}) {
syslog("LOG_ERR", "raw_transport: LOGIN FAILED");
die "raw_transport: Login failed (no account), exiting";
}
syslog("LOG_DEBUG", "raw_transport: uname/inst: '%s/%s'",
$self->{account}->{id},
$self->{account}->{institution});
$self->sip_protocol_loop();
syslog("LOG_INFO", "raw_transport: shutting down");
}
sub telnet_transport {
my $self = shift;
my ($uid, $pwd);
my $strikes = 3;
my $account = undef;
my $input;
my $config = $self->{config};
my $timeout = $self->{service}->{timeout} || $config->{timeout} || 0;
syslog("LOG_DEBUG", "telnet_transport: timeout is %s", $timeout);
# Until the terminal has logged in, we don't trust it
# so use a timeout to protect ourselves from hanging.
eval {
local $SIG{ALRM} = sub { die "telnet_transport: Timed Out ($timeout seconds)!\n";; };
local $| = 1; # Unbuffered output
while ($strikes--) {
print "login: ";
alarm $timeout;
$uid = <STDIN>;
alarm 0;
print "password: ";
alarm $timeout;
$pwd = <STDIN>;
alarm 0;
$uid =~ s/[\r\n]+$//;
$pwd =~ s/[\r\n]+$//;
if (exists($config->{accounts}->{$uid})
&& ($pwd eq $config->{accounts}->{$uid}->password())) {
$account = $config->{accounts}->{$uid};
last;
} else {
syslog("LOG_WARNING", "Invalid login attempt: '%s'", $uid);
print("Invalid login$CRLF");
}
}
}; # End of eval
if ($@) {
syslog("LOG_ERR", "telnet_transport: Login timed out");
die "Telnet Login Timed out";
} elsif (!defined($account)) {
syslog("LOG_ERR", "telnet_transport: Login Failed");
die "Login Failure";
} else {
print "Login OK. Initiating SIP$CRLF";
}
$self->{account} = $account;
syslog("LOG_DEBUG", "telnet_transport: uname/inst: '%s/%s'", $account->{id}, $account->{institution});
$self->sip_protocol_loop();
syslog("LOG_INFO", "telnet_transport: shutting down");
}
sub http_transport {
}
#
# The terminal has logged in, using either the SIP login process
# over a raw socket, or via the pseudo-unix login provided by the
# telnet transport. From that point on, both the raw and the telnet
# processes are the same:
sub sip_protocol_loop {
my $self = shift;
my $expect;
my $service = $self->{service};
my $config = $self->{config};
my $input;
# Now that the terminal has logged in, the first message
# we recieve must be an SC_STATUS message. But it might be
# an SC_REQUEST_RESEND. So, as long as we keep receiving
# SC_REQUEST_RESEND, we keep waiting for an SC_STATUS
# Comprise reports that no other ILS actually enforces this
# constraint, so we'll relax about it too. As long as everybody
# uses the SIP "raw" login process, rather than telnet, this
# will be fine, becaues the LOGIN protocol exchange will force
# us into SIP 2.00 anyway. Machines that want to log in using
# telnet MUST send an SC Status message first, even though we're
# not enforcing it.
#
#$expect = SC_STATUS;
$expect = '';
while ( $input = Sip::read_SIP_packet(*STDIN) ) {
$input =~ s/[\r\n]+$//sm; # Strip off any trailing line ends
my $status = Sip::MsgType::handle($input, $self, $expect);
next if $status eq REQUEST_ACS_RESEND;
if (!$status) {
syslog("LOG_ERR", "raw_transport: failed to handle %s", substr($input,0,2));
die "sip_protocol_loop: failed Sip::MsgType::handle('$input', $self, '$expect')";
}
elsif ($expect && ($status ne $expect)) {
# We received a non-"RESEND" that wasn't what we were expecting.
syslog("LOG_ERR", "raw_transport: expected %s, received %s, exiting", $expect, $input);
die "sip_protocol_loop: exiting: expected '$expect', received '$status'";
}
# We successfully received and processed what we were expecting
$expect = '';
}
}