8 use Sys::Syslog qw(syslog);
9 use Net::Server::PreFork;
11 use Socket qw(:DEFAULT :crlf);
12 require UNIVERSAL::require;
14 use Sip::Constants qw(:all);
15 use Sip::Configuration;
16 use Sip::Checksum qw(checksum verify_cksum);
19 use base qw(Net::Server::PreFork);
21 use constant LOG_SIP => "local6"; # Local alias for the logging facility
24 # Main # not really, since package SIPServer
26 # FIXME: Is this a module or a script?
27 # A script with no MAIN namespace?
28 # A module that takes command line args?
31 RAW => \&raw_transport,
32 telnet => \&telnet_transport,
38 my $config = new Sip::Configuration $ARGV[0];
44 foreach my $svc (keys %{$config->{listeners}}) {
45 push @parms, "port=" . $svc;
51 # Log lines look like this:
52 # Jun 16 21:21:31 server08 steve_sip[19305]: ILS::Transaction::Checkout performing checkout...
53 # [ TIMESTAMP ] [ HOST ] [ IDENT ] PID : Message...
55 # The IDENT is determined by config file 'server-params' arguments
59 # Server Management: set parameters for the Net::Server::PreFork
60 # module. The module silently ignores parameters that it doesn't
61 # recognize, and complains about invalid values for parameters
64 if (defined($config->{'server-params'})) {
65 while (my ($key, $val) = each %{$config->{'server-params'}}) {
66 push @parms, $key . '=' . $val;
72 # This is the main event.
73 __PACKAGE__ ->run(@parms);
79 # process_request is the callback used by Net::Server to handle
80 # an incoming connection request.
85 my ($sockaddr, $port, $proto);
88 $self->{config} = $config;
90 my $sockname = getsockname(STDIN);
91 ($port, $sockaddr) = sockaddr_in($sockname);
92 $sockaddr = inet_ntoa($sockaddr);
93 $proto = $self->{server}->{client}->NS_proto();
95 $self->{service} = $config->find_service($sockaddr, $port, $proto);
97 if (!defined($self->{service})) {
98 syslog("LOG_ERR", "process_request: Unknown recognized server connection: %s:%s/%s", $sockaddr, $port, $proto);
99 die "process_request: Bad server connection";
102 $transport = $transports{$self->{service}->{transport}};
104 if (!defined($transport)) {
105 syslog("LOG_WARNING", "Unknown transport '%s', dropping", $service->{transport});
119 my $service = $self->{service};
121 while (!$self->{account}) {
122 local $SIG{ALRM} = sub { die "raw_transport Timed Out!\n"; };
123 syslog("LOG_DEBUG", "raw_transport: timeout is %d", $service->{timeout});
124 $input = Sip::read_SIP_packet(*STDIN);
127 syslog("LOG_INFO", "raw_transport: shutting down: EOF during login");
130 $input =~ s/[\r\n]+$//sm; # Strip off trailing line terminator(s)
131 last if Sip::MsgType::handle($input, $self, LOGIN);
134 syslog("LOG_DEBUG", "raw_transport: uname/inst: '%s/%s'",
135 $self->{account}->{id},
136 $self->{account}->{institution});
138 $self->sip_protocol_loop();
139 syslog("LOG_INFO", "raw_transport: shutting down");
142 sub get_clean_string {
144 if (defined $string) {
145 syslog("LOG_DEBUG", "get_clean_string pre-clean(length %s): %s", length($string), $string);
147 $string =~ s/^[^A-z0-9]+//;
148 $string =~ s/[^A-z0-9]+$//;
149 syslog("LOG_DEBUG", "get_clean_string post-clean(length %s): %s", length($string), $string);
151 syslog("LOG_INFO", "get_clean_string called on undefined");
156 sub get_clean_input {
159 $in = get_clean_string($in);
160 while (my $extra = <STDIN>){
161 syslog("LOG_ERR", "get_clean_input got extra lines: %s", $extra);
166 sub telnet_transport {
172 my $config = $self->{config};
173 my $timeout = $self->{service}->{timeout} || $config->{timeout} || 30;
174 syslog("LOG_DEBUG", "telnet_transport: timeout is %s", $timeout);
177 local $SIG{ALRM} = sub { die "telnet_transport: Timed Out ($timeout seconds)!\n"; };
178 local $| = 1; # Unbuffered output
179 $/ = "\015"; # Internet Record Separator (lax version)
180 # Until the terminal has logged in, we don't trust it
181 # so use a timeout to protect ourselves from hanging.
186 # $uid = &get_clean_input;
189 # $pwd = &get_clean_input || '';
193 syslog("LOG_DEBUG", "telnet_transport 1: uid length %s, pwd length %s", length($uid), length($pwd));
194 $uid = get_clean_string ($uid);
195 $pwd = get_clean_string ($pwd);
196 syslog("LOG_DEBUG", "telnet_transport 2: uid length %s, pwd length %s", length($uid), length($pwd));
198 if (exists ($config->{accounts}->{$uid})
199 && ($pwd eq $config->{accounts}->{$uid}->password())) {
200 $account = $config->{accounts}->{$uid};
201 Sip::MsgType::login_core($self,$uid,$pwd) and last;
203 syslog("LOG_WARNING", "Invalid login attempt: '%s'", ($uid||''));
204 print("Invalid login$CRLF");
209 syslog("LOG_ERR", "telnet_transport: Login timed out");
210 die "Telnet Login Timed out";
211 } elsif (!defined($account)) {
212 syslog("LOG_ERR", "telnet_transport: Login Failed");
215 print "Login OK. Initiating SIP$CRLF";
218 $self->{account} = $account;
219 syslog("LOG_DEBUG", "telnet_transport: uname/inst: '%s/%s'", $account->{id}, $account->{institution});
220 $self->sip_protocol_loop();
221 syslog("LOG_INFO", "telnet_transport: shutting down");
225 # The terminal has logged in, using either the SIP login process
226 # over a raw socket, or via the pseudo-unix login provided by the
227 # telnet transport. From that point on, both the raw and the telnet
228 # processes are the same:
229 sub sip_protocol_loop {
231 my $service = $self->{service};
232 my $config = $self->{config};
235 # The spec says the first message will be:
237 # SIP v2: LOGIN (or SC_STATUS via telnet?)
238 # But it might be SC_REQUEST_RESEND. As long as we get
239 # SC_REQUEST_RESEND, we keep waiting.
241 # Comprise reports that no other ILS actually enforces this
242 # constraint, so we'll relax about it too.
243 # Using the SIP "raw" login process, rather than telnet,
244 # requires the LOGIN message and forces SIP 2.00. In that
245 # case, the LOGIN message has already been processed (above).
247 # In short, we'll take any valid message here.
248 #my $expect = SC_STATUS;
251 $input = Sip::read_SIP_packet(*STDIN);
255 # begin input hacks ... a cheap stand in for better Telnet layer
256 $input =~ s/^[^A-z0-9]+//s; # Kill leading bad characters... like Telnet handshakers
257 $input =~ s/[^A-z0-9]+$//s; # Same on the end, should get DOSsy ^M line-endings too.
258 while (chomp($input)) {warn "Extra line ending on input";}
260 syslog("LOG_ERR", "sip_protocol_loop: empty input skipped");
264 # end cheap input hacks
265 my $status = Sip::MsgType::handle($input, $self, $expect);
267 syslog("LOG_ERR", "sip_protocol_loop: failed to handle %s",substr($input,0,2));
269 next if $status eq REQUEST_ACS_RESEND;
270 if ($expect && ($status ne $expect)) {
271 # We received a non-"RESEND" that wasn't what we were expecting.
272 syslog("LOG_ERR", "sip_protocol_loop: expected %s, received %s, exiting", $expect, $input);
274 # We successfully received and processed what we were expecting