2 package C4
::SIP
::SIPServer
;
8 use Sys::Syslog qw(syslog);
9 use Net
::Server
::PreFork
;
11 use Socket
qw(:DEFAULT :crlf);
12 require UNIVERSAL
::require;
14 use C4
::SIP
::Sip
::Constants
qw(:all);
15 use C4
::SIP
::Sip
::Configuration
;
16 use C4
::SIP
::Sip
::Checksum
qw(checksum verify_cksum);
17 use C4
::SIP
::Sip
::MsgType
qw( handle login_core );
18 use C4
::SIP
::Sip
qw( read_SIP_packet );
20 use base
qw(Net::Server::PreFork);
22 use constant LOG_SIP
=> "local6"; # Local alias for the logging facility
25 # Main # not really, since package SIPServer
27 # FIXME: Is this a module or a script?
28 # A script with no MAIN namespace?
29 # A module that takes command line args?
32 RAW
=> \
&raw_transport
,
33 telnet
=> \
&telnet_transport
,
39 my $config = C4
::SIP
::Sip
::Configuration
->new( $ARGV[0] );
45 foreach my $svc (keys %{$config->{listeners
}}) {
46 push @parms, "port=" . $svc;
52 # Log lines look like this:
53 # Jun 16 21:21:31 server08 steve_sip[19305]: ILS::Transaction::Checkout performing checkout...
54 # [ TIMESTAMP ] [ HOST ] [ IDENT ] PID : Message...
56 # The IDENT is determined by config file 'server-params' arguments
60 # Server Management: set parameters for the Net::Server::PreFork
61 # module. The module silently ignores parameters that it doesn't
62 # recognize, and complains about invalid values for parameters
65 if (defined($config->{'server-params'})) {
66 while (my ($key, $val) = each %{$config->{'server-params'}}) {
67 push @parms, $key . '=' . $val;
73 # This is the main event.
74 __PACKAGE__
->run(@parms);
80 # process_request is the callback used by Net::Server to handle
81 # an incoming connection request.
86 my ($sockaddr, $port, $proto);
89 $self->{config
} = $config;
91 my $sockname = getsockname(STDIN
);
93 # Check if socket connection is IPv6 before resolving address
94 my $family = Socket
::sockaddr_family
($sockname);
95 if ($family == AF_INET6
) {
96 ($port, $sockaddr) = sockaddr_in6
($sockname);
97 $sockaddr = Socket
::inet_ntop
(AF_INET6
, $sockaddr);
99 ($port, $sockaddr) = sockaddr_in
($sockname);
100 $sockaddr = inet_ntoa
($sockaddr);
102 $proto = $self->{server
}->{client
}->NS_proto();
104 $self->{service
} = $config->find_service($sockaddr, $port, $proto);
106 if (!defined($self->{service
})) {
107 syslog
("LOG_ERR", "process_request: Unknown recognized server connection: %s:%s/%s", $sockaddr, $port, $proto);
108 die "process_request: Bad server connection";
111 $transport = $transports{$self->{service
}->{transport
}};
113 if (!defined($transport)) {
114 syslog
("LOG_WARNING", "Unknown transport '%s', dropping", $service->{transport
});
128 my $service = $self->{service
};
130 while (!$self->{account
}) {
131 local $SIG{ALRM
} = sub { die "raw_transport Timed Out!\n"; };
132 syslog
("LOG_DEBUG", "raw_transport: timeout is %d", $service->{timeout
});
133 $input = read_SIP_packet
(*STDIN
);
136 syslog
("LOG_INFO", "raw_transport: shutting down: EOF during login");
139 $input =~ s/[\r\n]+$//sm; # Strip off trailing line terminator(s)
140 last if C4
::SIP
::Sip
::MsgType
::handle
($input, $self, LOGIN
);
143 syslog
("LOG_DEBUG", "raw_transport: uname/inst: '%s/%s'",
144 $self->{account
}->{id
},
145 $self->{account
}->{institution
});
147 $self->sip_protocol_loop();
148 syslog
("LOG_INFO", "raw_transport: shutting down");
151 sub get_clean_string
{
153 if (defined $string) {
154 syslog
("LOG_DEBUG", "get_clean_string pre-clean(length %s): %s", length($string), $string);
156 $string =~ s/^[^A-z0-9]+//;
157 $string =~ s/[^A-z0-9]+$//;
158 syslog
("LOG_DEBUG", "get_clean_string post-clean(length %s): %s", length($string), $string);
160 syslog
("LOG_INFO", "get_clean_string called on undefined");
165 sub get_clean_input
{
168 $in = get_clean_string
($in);
169 while (my $extra = <STDIN
>){
170 syslog
("LOG_ERR", "get_clean_input got extra lines: %s", $extra);
175 sub telnet_transport
{
181 my $config = $self->{config
};
182 my $timeout = $self->{service
}->{timeout
} || $config->{timeout
} || 30;
183 syslog
("LOG_DEBUG", "telnet_transport: timeout is %s", $timeout);
186 local $SIG{ALRM
} = sub { die "telnet_transport: Timed Out ($timeout seconds)!\n"; };
187 local $| = 1; # Unbuffered output
188 $/ = "\015"; # Internet Record Separator (lax version)
189 # Until the terminal has logged in, we don't trust it
190 # so use a timeout to protect ourselves from hanging.
195 # $uid = &get_clean_input;
198 # $pwd = &get_clean_input || '';
202 syslog
("LOG_DEBUG", "telnet_transport 1: uid length %s, pwd length %s", length($uid), length($pwd));
203 $uid = get_clean_string
($uid);
204 $pwd = get_clean_string
($pwd);
205 syslog
("LOG_DEBUG", "telnet_transport 2: uid length %s, pwd length %s", length($uid), length($pwd));
207 if (exists ($config->{accounts
}->{$uid})
208 && ($pwd eq $config->{accounts
}->{$uid}->{password
})) {
209 $account = $config->{accounts
}->{$uid};
210 if ( C4
::SIP
::Sip
::MsgType
::login_core
($self,$uid,$pwd) ) {
214 syslog
("LOG_WARNING", "Invalid login attempt: '%s'", ($uid||''));
215 print("Invalid login$CRLF");
220 syslog
("LOG_ERR", "telnet_transport: Login timed out");
221 die "Telnet Login Timed out";
222 } elsif (!defined($account)) {
223 syslog
("LOG_ERR", "telnet_transport: Login Failed");
226 print "Login OK. Initiating SIP$CRLF";
229 $self->{account
} = $account;
230 syslog
("LOG_DEBUG", "telnet_transport: uname/inst: '%s/%s'", $account->{id
}, $account->{institution
});
231 $self->sip_protocol_loop();
232 syslog
("LOG_INFO", "telnet_transport: shutting down");
236 # The terminal has logged in, using either the SIP login process
237 # over a raw socket, or via the pseudo-unix login provided by the
238 # telnet transport. From that point on, both the raw and the telnet
239 # processes are the same:
240 sub sip_protocol_loop
{
242 my $service = $self->{service
};
243 my $config = $self->{config
};
244 my $timeout = $self->{service
}->{timeout
} || $config->{timeout
} || 30;
247 # The spec says the first message will be:
249 # SIP v2: LOGIN (or SC_STATUS via telnet?)
250 # But it might be SC_REQUEST_RESEND. As long as we get
251 # SC_REQUEST_RESEND, we keep waiting.
253 # Comprise reports that no other ILS actually enforces this
254 # constraint, so we'll relax about it too.
255 # Using the SIP "raw" login process, rather than telnet,
256 # requires the LOGIN message and forces SIP 2.00. In that
257 # case, the LOGIN message has already been processed (above).
259 # In short, we'll take any valid message here.
260 #my $expect = SC_STATUS;
261 local $SIG{ALRM
} = sub { die "SIP Timed Out!\n"; };
265 $input = read_SIP_packet
(*STDIN
);
269 # begin input hacks ... a cheap stand in for better Telnet layer
270 $input =~ s/^[^A-z0-9]+//s; # Kill leading bad characters... like Telnet handshakers
271 $input =~ s/[^A-z0-9]+$//s; # Same on the end, should get DOSsy ^M line-endings too.
272 while (chomp($input)) {warn "Extra line ending on input";}
274 syslog
("LOG_ERR", "sip_protocol_loop: empty input skipped");
278 # end cheap input hacks
279 my $status = handle
($input, $self, $expect);
281 syslog
("LOG_ERR", "sip_protocol_loop: failed to handle %s",substr($input,0,2));
283 next if $status eq REQUEST_ACS_RESEND
;
284 if ($expect && ($status ne $expect)) {
285 # We received a non-"RESEND" that wasn't what we were expecting.
286 syslog
("LOG_ERR", "sip_protocol_loop: expected %s, received %s, exiting", $expect, $input);
288 # We successfully received and processed what we were expecting