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
);
92 # Check if socket connection is IPv6 before resolving address
93 my $family = Socket
::sockaddr_family
($sockname);
94 if ($family == AF_INET6
) {
95 ($port, $sockaddr) = sockaddr_in6
($sockname);
96 $sockaddr = Socket
::inet_ntop
(AF_INET6
, $sockaddr);
98 ($port, $sockaddr) = sockaddr_in
($sockname);
99 $sockaddr = inet_ntoa
($sockaddr);
101 $proto = $self->{server
}->{client
}->NS_proto();
103 $self->{service
} = $config->find_service($sockaddr, $port, $proto);
105 if (!defined($self->{service
})) {
106 syslog
("LOG_ERR", "process_request: Unknown recognized server connection: %s:%s/%s", $sockaddr, $port, $proto);
107 die "process_request: Bad server connection";
110 $transport = $transports{$self->{service
}->{transport
}};
112 if (!defined($transport)) {
113 syslog
("LOG_WARNING", "Unknown transport '%s', dropping", $service->{transport
});
127 my $service = $self->{service
};
129 while (!$self->{account
}) {
130 local $SIG{ALRM
} = sub { die "raw_transport Timed Out!\n"; };
131 syslog
("LOG_DEBUG", "raw_transport: timeout is %d", $service->{timeout
});
132 $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
);
142 syslog
("LOG_DEBUG", "raw_transport: uname/inst: '%s/%s'",
143 $self->{account
}->{id
},
144 $self->{account
}->{institution
});
146 $self->sip_protocol_loop();
147 syslog
("LOG_INFO", "raw_transport: shutting down");
150 sub get_clean_string
{
152 if (defined $string) {
153 syslog
("LOG_DEBUG", "get_clean_string pre-clean(length %s): %s", length($string), $string);
155 $string =~ s/^[^A-z0-9]+//;
156 $string =~ s/[^A-z0-9]+$//;
157 syslog
("LOG_DEBUG", "get_clean_string post-clean(length %s): %s", length($string), $string);
159 syslog
("LOG_INFO", "get_clean_string called on undefined");
164 sub get_clean_input
{
167 $in = get_clean_string
($in);
168 while (my $extra = <STDIN
>){
169 syslog
("LOG_ERR", "get_clean_input got extra lines: %s", $extra);
174 sub telnet_transport
{
180 my $config = $self->{config
};
181 my $timeout = $self->{service
}->{timeout
} || $config->{timeout
} || 30;
182 syslog
("LOG_DEBUG", "telnet_transport: timeout is %s", $timeout);
185 local $SIG{ALRM
} = sub { die "telnet_transport: Timed Out ($timeout seconds)!\n"; };
186 local $| = 1; # Unbuffered output
187 $/ = "\015"; # Internet Record Separator (lax version)
188 # Until the terminal has logged in, we don't trust it
189 # so use a timeout to protect ourselves from hanging.
194 # $uid = &get_clean_input;
197 # $pwd = &get_clean_input || '';
201 syslog
("LOG_DEBUG", "telnet_transport 1: uid length %s, pwd length %s", length($uid), length($pwd));
202 $uid = get_clean_string
($uid);
203 $pwd = get_clean_string
($pwd);
204 syslog
("LOG_DEBUG", "telnet_transport 2: uid length %s, pwd length %s", length($uid), length($pwd));
206 if (exists ($config->{accounts
}->{$uid})
207 && ($pwd eq $config->{accounts
}->{$uid}->password())) {
208 $account = $config->{accounts
}->{$uid};
209 Sip
::MsgType
::login_core
($self,$uid,$pwd) and last;
211 syslog
("LOG_WARNING", "Invalid login attempt: '%s'", ($uid||''));
212 print("Invalid login$CRLF");
217 syslog
("LOG_ERR", "telnet_transport: Login timed out");
218 die "Telnet Login Timed out";
219 } elsif (!defined($account)) {
220 syslog
("LOG_ERR", "telnet_transport: Login Failed");
223 print "Login OK. Initiating SIP$CRLF";
226 $self->{account
} = $account;
227 syslog
("LOG_DEBUG", "telnet_transport: uname/inst: '%s/%s'", $account->{id
}, $account->{institution
});
228 $self->sip_protocol_loop();
229 syslog
("LOG_INFO", "telnet_transport: shutting down");
233 # The terminal has logged in, using either the SIP login process
234 # over a raw socket, or via the pseudo-unix login provided by the
235 # telnet transport. From that point on, both the raw and the telnet
236 # processes are the same:
237 sub sip_protocol_loop
{
239 my $service = $self->{service
};
240 my $config = $self->{config
};
241 my $timeout = $self->{service
}->{timeout
} || $config->{timeout
} || 30;
244 # The spec says the first message will be:
246 # SIP v2: LOGIN (or SC_STATUS via telnet?)
247 # But it might be SC_REQUEST_RESEND. As long as we get
248 # SC_REQUEST_RESEND, we keep waiting.
250 # Comprise reports that no other ILS actually enforces this
251 # constraint, so we'll relax about it too.
252 # Using the SIP "raw" login process, rather than telnet,
253 # requires the LOGIN message and forces SIP 2.00. In that
254 # case, the LOGIN message has already been processed (above).
256 # In short, we'll take any valid message here.
257 #my $expect = SC_STATUS;
258 local $SIG{ALRM
} = sub { die "SIP Timed Out!\n"; };
262 $input = Sip
::read_SIP_packet
(*STDIN
);
266 # begin input hacks ... a cheap stand in for better Telnet layer
267 $input =~ s/^[^A-z0-9]+//s; # Kill leading bad characters... like Telnet handshakers
268 $input =~ s/[^A-z0-9]+$//s; # Same on the end, should get DOSsy ^M line-endings too.
269 while (chomp($input)) {warn "Extra line ending on input";}
271 syslog
("LOG_ERR", "sip_protocol_loop: empty input skipped");
275 # end cheap input hacks
276 my $status = Sip
::MsgType
::handle
($input, $self, $expect);
278 syslog
("LOG_ERR", "sip_protocol_loop: failed to handle %s",substr($input,0,2));
280 next if $status eq REQUEST_ACS_RESEND
;
281 if ($expect && ($status ne $expect)) {
282 # We received a non-"RESEND" that wasn't what we were expecting.
283 syslog
("LOG_ERR", "sip_protocol_loop: expected %s, received %s, exiting", $expect, $input);
285 # We successfully received and processed what we were expecting