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 push @parms, "log_file=Sys::Syslog", "syslog_ident=acs-server",
59 "syslog_facility=" . LOG_SIP;
62 # Server Management: set parameters for the Net::Server::PreFork
63 # module. The module silently ignores parameters that it doesn't
64 # recognize, and complains about invalid values for parameters
67 if (defined($config->{'server-params'})) {
68 while (my ($key, $val) = each %{$config->{'server-params'}}) {
69 push @parms, $key . '=' . $val;
73 print "Params for Net::Server::PreFork : \n" . Dumper(\@parms);
76 # This is the main event.
77 __PACKAGE__ ->run(@parms);
83 # process_request is the callback used by Net::Server to handle
84 # an incoming connection request.
89 my ($sockaddr, $port, $proto);
92 $self->{config} = $config;
94 my $sockname = getsockname(STDIN);
95 ($port, $sockaddr) = sockaddr_in($sockname);
96 $sockaddr = inet_ntoa($sockaddr);
97 $proto = $self->{server}->{client}->NS_proto();
99 $self->{service} = $config->find_service($sockaddr, $port, $proto);
101 if (!defined($self->{service})) {
102 syslog("LOG_ERR", "process_request: Unknown recognized server connection: %s:%s/%s", $sockaddr, $port, $proto);
103 die "process_request: Bad server connection";
106 $transport = $transports{$self->{service}->{transport}};
108 if (!defined($transport)) {
109 syslog("LOG_WARNING", "Unknown transport '%s', dropping", $service->{transport});
123 my $service = $self->{service};
127 local $SIG{ALRM} = sub { die "raw_transport Timed Out!\n"; };
128 syslog("LOG_DEBUG", "raw_transport: timeout is %d", $service->{timeout});
130 alarm $service->{timeout};
131 $input = Sip::read_SIP_packet(*STDIN);
135 syslog("LOG_INFO", "raw_transport: shutting down: EOF during login");
138 $input =~ s/[\r\n]+$//sm; # Strip off trailing line terminator(s)
139 last if Sip::MsgType::handle($input, $self, LOGIN);
144 syslog("LOG_ERR", "raw_transport: LOGIN ERROR: '$@'");
145 die "raw_transport: login error (timeout? $@), exiting";
146 } elsif (!$self->{account}) {
147 syslog("LOG_ERR", "raw_transport: LOGIN FAILED");
148 die "raw_transport: Login failed (no account), exiting";
151 syslog("LOG_DEBUG", "raw_transport: uname/inst: '%s/%s'",
152 $self->{account}->{id},
153 $self->{account}->{institution});
155 $self->sip_protocol_loop();
156 syslog("LOG_INFO", "raw_transport: shutting down");
159 sub get_clean_string ($) {
161 if (defined $string) {
162 syslog("LOG_DEBUG", "get_clean_string pre-clean(length %s): %s", length($string), $string);
164 $string =~ s/^[^A-z0-9]+//;
165 $string =~ s/[^A-z0-9]+$//;
166 syslog("LOG_DEBUG", "get_clean_string post-clean(length %s): %s", length($string), $string);
168 syslog("LOG_INFO", "get_clean_string called on undefined");
173 sub get_clean_input {
176 $in = get_clean_string($in);
177 while (my $extra = <STDIN>){
178 syslog("LOG_ERR", "get_clean_input got extra lines: %s", $extra);
183 sub telnet_transport {
189 my $config = $self->{config};
190 my $timeout = $self->{service}->{timeout} || $config->{timeout} || 30;
191 syslog("LOG_DEBUG", "telnet_transport: timeout is %s", $timeout);
194 local $SIG{ALRM} = sub { die "telnet_transport: Timed Out ($timeout seconds)!\n"; };
195 local $| = 1; # Unbuffered output
196 $/ = "\015"; # Internet Record Separator (lax version)
197 # Until the terminal has logged in, we don't trust it
198 # so use a timeout to protect ourselves from hanging.
203 # $uid = &get_clean_input;
206 # $pwd = &get_clean_input || '';
210 syslog("LOG_DEBUG", "telnet_transport 1: uid length %s, pwd length %s", length($uid), length($pwd));
211 $uid = get_clean_string ($uid);
212 $pwd = get_clean_string ($pwd);
213 syslog("LOG_DEBUG", "telnet_transport 2: uid length %s, pwd length %s", length($uid), length($pwd));
214 # $uid =~ s/^\s+//; #
215 # $pwd =~ s/^\s+//; #
216 # $uid =~ s/[\r\n]+$//gms; #
217 # $pwd =~ s/[\r\n]+$//gms; #
218 # $uid =~ s/[[:cntrl:]]//g; #
219 # $pwd =~ s/[[:cntrl:]]//g; #
220 # syslog("LOG_DEBUG", "telnet_transport 3: uid length %s, pwd length %s", length($uid), length($pwd));
222 if (exists ($config->{accounts}->{$uid})
223 && ($pwd eq $config->{accounts}->{$uid}->password())) {
224 $account = $config->{accounts}->{$uid};
225 Sip::MsgType::login_core($self,$uid,$pwd) and last;
227 syslog("LOG_WARNING", "Invalid login attempt: '%s'", ($uid||''));
228 print("Invalid login$CRLF");
233 syslog("LOG_ERR", "telnet_transport: Login timed out");
234 die "Telnet Login Timed out";
235 } elsif (!defined($account)) {
236 syslog("LOG_ERR", "telnet_transport: Login Failed");
239 print "Login OK. Initiating SIP$CRLF";
242 $self->{account} = $account;
243 syslog("LOG_DEBUG", "telnet_transport: uname/inst: '%s/%s'", $account->{id}, $account->{institution});
244 $self->sip_protocol_loop();
245 syslog("LOG_INFO", "telnet_transport: shutting down");
249 # The terminal has logged in, using either the SIP login process
250 # over a raw socket, or via the pseudo-unix login provided by the
251 # telnet transport. From that point on, both the raw and the telnet
252 # processes are the same:
253 sub sip_protocol_loop {
255 my $service = $self->{service};
256 my $config = $self->{config};
259 # The spec says the first message will be:
261 # SIP v2: LOGIN (or SC_STATUS via telnet?)
262 # But it might be SC_REQUEST_RESEND. As long as we get
263 # SC_REQUEST_RESEND, we keep waiting.
265 # Comprise reports that no other ILS actually enforces this
266 # constraint, so we'll relax about it too.
267 # Using the SIP "raw" login process, rather than telnet,
268 # requires the LOGIN message and forces SIP 2.00. In that
269 # case, the LOGIN message has already been processed (above).
271 # In short, we'll take any valid message here.
272 #my $expect = SC_STATUS;
275 while ($input = Sip::read_SIP_packet(*STDIN)) {
276 # begin input hacks ... a cheap stand in for better Telnet layer
277 $input =~ s/^[^A-z0-9]+//s; # Kill leading bad characters... like Telnet handshakers
278 $input =~ s/[^A-z0-9]+$//s; # Same on the end, should get DOSsy ^M line-endings too.
279 while (chomp($input)) {warn "Extra line ending on input";}
282 syslog("LOG_ERR", "sip_protocol_loop: empty input skipped");
285 syslog("LOG_ERR", "sip_protocol_loop: quitting after too many errors");
286 die "sip_protocol_loop: quitting after too many errors";
289 # end cheap input hacks
290 my $status = Sip::MsgType::handle($input, $self, $expect);
292 syslog("LOG_ERR", "sip_protocol_loop: failed to handle %s",substr($input,0,2));
293 die "sip_protocol_loop: failed Sip::MsgType::handle('$input', $self, '$expect')";
295 next if $status eq REQUEST_ACS_RESEND;
296 if ($expect && ($status ne $expect)) {
297 # We received a non-"RESEND" that wasn't what we were expecting.
298 syslog("LOG_ERR", "sip_protocol_loop: expected %s, received %s, exiting", $expect, $input);
299 die "sip_protocol_loop: exiting: expected '$expect', received '$status'";
301 # We successfully received and processed what we were expecting