8 use Sys::Syslog qw(syslog);
9 use Net::Server::PreFork;
11 use Socket qw(:DEFAULT :crlf);
12 use Data::Dumper; # For debugging
13 require UNIVERSAL::require;
15 #use Sip qw(readline);
16 use Sip::Constants qw(:all);
17 use Sip::Configuration;
18 use Sip::Checksum qw(checksum verify_cksum);
21 use constant LOG_SIP => "local6"; # Local alias for the logging facility
23 use vars qw(@ISA $VERSION);
26 $VERSION = 3.07.00.049;
27 @ISA = qw(Net::Server::PreFork);
31 # Main # not really, since package SIPServer
33 # FIXME: Is this a module or a script?
34 # A script with no MAIN namespace?
35 # A module that takes command line args?
38 RAW => \&raw_transport,
39 telnet => \&telnet_transport,
45 my $config = new Sip::Configuration $ARGV[0];
46 print STDERR "SIPServer config: \n" . Dumper($config) . "\nEND SIPServer config.\n";
52 foreach my $svc (keys %{$config->{listeners}}) {
53 push @parms, "port=" . $svc;
59 # Log lines look like this:
60 # Jun 16 21:21:31 server08 steve_sip[19305]: ILS::Transaction::Checkout performing checkout...
61 # [ TIMESTAMP ] [ HOST ] [ IDENT ] PID : Message...
63 # The IDENT is determined by config file 'server-params' arguments
67 # Server Management: set parameters for the Net::Server::PreFork
68 # module. The module silently ignores parameters that it doesn't
69 # recognize, and complains about invalid values for parameters
72 if (defined($config->{'server-params'})) {
73 while (my ($key, $val) = each %{$config->{'server-params'}}) {
74 push @parms, $key . '=' . $val;
78 print scalar(localtime), " -- startup -- procid:$$\n";
79 print "Params for Net::Server::PreFork : \n" . Dumper(\@parms);
82 # This is the main event.
83 __PACKAGE__ ->run(@parms);
89 # process_request is the callback used by Net::Server to handle
90 # an incoming connection request.
95 my ($sockaddr, $port, $proto);
98 $self->{config} = $config;
100 my $sockname = getsockname(STDIN);
101 ($port, $sockaddr) = sockaddr_in($sockname);
102 $sockaddr = inet_ntoa($sockaddr);
103 $proto = $self->{server}->{client}->NS_proto();
105 $self->{service} = $config->find_service($sockaddr, $port, $proto);
107 if (!defined($self->{service})) {
108 syslog("LOG_ERR", "process_request: Unknown recognized server connection: %s:%s/%s", $sockaddr, $port, $proto);
109 die "process_request: Bad server connection";
112 $transport = $transports{$self->{service}->{transport}};
114 if (!defined($transport)) {
115 syslog("LOG_WARNING", "Unknown transport '%s', dropping", $service->{transport});
129 my $service = $self->{service};
131 while (!$self->{account}) {
132 local $SIG{ALRM} = sub { die "raw_transport Timed Out!\n"; };
133 syslog("LOG_DEBUG", "raw_transport: timeout is %d", $service->{timeout});
134 $input = Sip::read_SIP_packet(*STDIN);
137 syslog("LOG_INFO", "raw_transport: shutting down: EOF during login");
140 $input =~ s/[\r\n]+$//sm; # Strip off trailing line terminator(s)
141 last if Sip::MsgType::handle($input, $self, LOGIN);
144 syslog("LOG_DEBUG", "raw_transport: uname/inst: '%s/%s'",
145 $self->{account}->{id},
146 $self->{account}->{institution});
148 $self->sip_protocol_loop();
149 syslog("LOG_INFO", "raw_transport: shutting down");
152 sub get_clean_string {
154 if (defined $string) {
155 syslog("LOG_DEBUG", "get_clean_string pre-clean(length %s): %s", length($string), $string);
157 $string =~ s/^[^A-z0-9]+//;
158 $string =~ s/[^A-z0-9]+$//;
159 syslog("LOG_DEBUG", "get_clean_string post-clean(length %s): %s", length($string), $string);
161 syslog("LOG_INFO", "get_clean_string called on undefined");
166 sub get_clean_input {
169 $in = get_clean_string($in);
170 while (my $extra = <STDIN>){
171 syslog("LOG_ERR", "get_clean_input got extra lines: %s", $extra);
176 sub telnet_transport {
182 my $config = $self->{config};
183 my $timeout = $self->{service}->{timeout} || $config->{timeout} || 30;
184 syslog("LOG_DEBUG", "telnet_transport: timeout is %s", $timeout);
187 local $SIG{ALRM} = sub { die "telnet_transport: Timed Out ($timeout seconds)!\n"; };
188 local $| = 1; # Unbuffered output
189 $/ = "\015"; # Internet Record Separator (lax version)
190 # Until the terminal has logged in, we don't trust it
191 # so use a timeout to protect ourselves from hanging.
196 # $uid = &get_clean_input;
199 # $pwd = &get_clean_input || '';
203 syslog("LOG_DEBUG", "telnet_transport 1: uid length %s, pwd length %s", length($uid), length($pwd));
204 $uid = get_clean_string ($uid);
205 $pwd = get_clean_string ($pwd);
206 syslog("LOG_DEBUG", "telnet_transport 2: uid length %s, pwd length %s", length($uid), length($pwd));
208 if (exists ($config->{accounts}->{$uid})
209 && ($pwd eq $config->{accounts}->{$uid}->password())) {
210 $account = $config->{accounts}->{$uid};
211 Sip::MsgType::login_core($self,$uid,$pwd) and last;
213 syslog("LOG_WARNING", "Invalid login attempt: '%s'", ($uid||''));
214 print("Invalid login$CRLF");
219 syslog("LOG_ERR", "telnet_transport: Login timed out");
220 die "Telnet Login Timed out";
221 } elsif (!defined($account)) {
222 syslog("LOG_ERR", "telnet_transport: Login Failed");
225 print "Login OK. Initiating SIP$CRLF";
228 $self->{account} = $account;
229 syslog("LOG_DEBUG", "telnet_transport: uname/inst: '%s/%s'", $account->{id}, $account->{institution});
230 $self->sip_protocol_loop();
231 syslog("LOG_INFO", "telnet_transport: shutting down");
235 # The terminal has logged in, using either the SIP login process
236 # over a raw socket, or via the pseudo-unix login provided by the
237 # telnet transport. From that point on, both the raw and the telnet
238 # processes are the same:
239 sub sip_protocol_loop {
241 my $service = $self->{service};
242 my $config = $self->{config};
245 # The spec says the first message will be:
247 # SIP v2: LOGIN (or SC_STATUS via telnet?)
248 # But it might be SC_REQUEST_RESEND. As long as we get
249 # SC_REQUEST_RESEND, we keep waiting.
251 # Comprise reports that no other ILS actually enforces this
252 # constraint, so we'll relax about it too.
253 # Using the SIP "raw" login process, rather than telnet,
254 # requires the LOGIN message and forces SIP 2.00. In that
255 # case, the LOGIN message has already been processed (above).
257 # In short, we'll take any valid message here.
258 #my $expect = SC_STATUS;
261 $input = Sip::read_SIP_packet(*STDIN);
265 # begin input hacks ... a cheap stand in for better Telnet layer
266 $input =~ s/^[^A-z0-9]+//s; # Kill leading bad characters... like Telnet handshakers
267 $input =~ s/[^A-z0-9]+$//s; # Same on the end, should get DOSsy ^M line-endings too.
268 while (chomp($input)) {warn "Extra line ending on input";}
270 syslog("LOG_ERR", "sip_protocol_loop: empty input skipped");
274 # end cheap input hacks
275 my $status = Sip::MsgType::handle($input, $self, $expect);
277 syslog("LOG_ERR", "sip_protocol_loop: failed to handle %s",substr($input,0,2));
279 next if $status eq REQUEST_ACS_RESEND;
280 if ($expect && ($status ne $expect)) {
281 # We received a non-"RESEND" that wasn't what we were expecting.
282 syslog("LOG_ERR", "sip_protocol_loop: expected %s, received %s, exiting", $expect, $input);
284 # We successfully received and processed what we were expecting