|
Lines 5-11
use strict;
Link Here
|
| 5 |
use warnings; |
5 |
use warnings; |
| 6 |
use FindBin qw($Bin); |
6 |
use FindBin qw($Bin); |
| 7 |
use lib "$Bin"; |
7 |
use lib "$Bin"; |
| 8 |
use Sys::Syslog qw(syslog); |
|
|
| 9 |
use Net::Server::PreFork; |
8 |
use Net::Server::PreFork; |
| 10 |
use IO::Socket::INET; |
9 |
use IO::Socket::INET; |
| 11 |
use Socket qw(:DEFAULT :crlf); |
10 |
use Socket qw(:DEFAULT :crlf); |
|
Lines 16-21
use C4::SIP::Sip::Configuration;
Link Here
|
| 16 |
use C4::SIP::Sip::Checksum qw(checksum verify_cksum); |
15 |
use C4::SIP::Sip::Checksum qw(checksum verify_cksum); |
| 17 |
use C4::SIP::Sip::MsgType qw( handle login_core ); |
16 |
use C4::SIP::Sip::MsgType qw( handle login_core ); |
| 18 |
use C4::SIP::Sip qw( read_SIP_packet ); |
17 |
use C4::SIP::Sip qw( read_SIP_packet ); |
|
|
18 |
use Koha::Log qw(:log set_default_logger); |
| 19 |
|
19 |
|
| 20 |
use base qw(Net::Server::PreFork); |
20 |
use base qw(Net::Server::PreFork); |
| 21 |
|
21 |
|
|
Lines 28-33
use constant LOG_SIP => "local6"; # Local alias for the logging facility
Link Here
|
| 28 |
# A script with no MAIN namespace? |
28 |
# A script with no MAIN namespace? |
| 29 |
# A module that takes command line args? |
29 |
# A module that takes command line args? |
| 30 |
|
30 |
|
|
|
31 |
set_default_logger( |
| 32 |
Syslog => {min_level => 'warn', facility => LOG_SIP}, |
| 33 |
Stdout => {min_level => 'fatal'}, |
| 34 |
); |
| 35 |
|
| 31 |
my %transports = ( |
36 |
my %transports = ( |
| 32 |
RAW => \&raw_transport, |
37 |
RAW => \&raw_transport, |
| 33 |
telnet => \&telnet_transport, |
38 |
telnet => \&telnet_transport, |
|
Lines 104-117
sub process_request {
Link Here
|
| 104 |
$self->{service} = $config->find_service($sockaddr, $port, $proto); |
109 |
$self->{service} = $config->find_service($sockaddr, $port, $proto); |
| 105 |
|
110 |
|
| 106 |
if (!defined($self->{service})) { |
111 |
if (!defined($self->{service})) { |
| 107 |
syslog("LOG_ERR", "process_request: Unknown recognized server connection: %s:%s/%s", $sockaddr, $port, $proto); |
112 |
log_fatal { "process_request: Unknown recognized server connection: %s:%s/%s", $sockaddr, $port, $proto }; |
| 108 |
die "process_request: Bad server connection"; |
|
|
| 109 |
} |
113 |
} |
| 110 |
|
114 |
|
| 111 |
$transport = $transports{$self->{service}->{transport}}; |
115 |
$transport = $transports{$self->{service}->{transport}}; |
| 112 |
|
116 |
|
| 113 |
if (!defined($transport)) { |
117 |
if (!defined($transport)) { |
| 114 |
syslog("LOG_WARNING", "Unknown transport '%s', dropping", $service->{transport}); |
118 |
log_warn { "Unknown transport '%s', dropping", $service->{transport} }; |
| 115 |
return; |
119 |
return; |
| 116 |
} else { |
120 |
} else { |
| 117 |
&$transport($self); |
121 |
&$transport($self); |
|
Lines 129-163
sub raw_transport {
Link Here
|
| 129 |
|
133 |
|
| 130 |
while (!$self->{account}) { |
134 |
while (!$self->{account}) { |
| 131 |
local $SIG{ALRM} = sub { die "raw_transport Timed Out!\n"; }; |
135 |
local $SIG{ALRM} = sub { die "raw_transport Timed Out!\n"; }; |
| 132 |
syslog("LOG_DEBUG", "raw_transport: timeout is %d", $service->{timeout}); |
136 |
log_debug { "raw_transport: timeout is %d", $service->{timeout} }; |
| 133 |
$input = read_SIP_packet(*STDIN); |
137 |
$input = read_SIP_packet(*STDIN); |
| 134 |
if (!$input) { |
138 |
if (!$input) { |
| 135 |
# EOF on the socket |
139 |
# EOF on the socket |
| 136 |
syslog("LOG_INFO", "raw_transport: shutting down: EOF during login"); |
140 |
log_info { "raw_transport: shutting down: EOF during login" }; |
| 137 |
return; |
141 |
return; |
| 138 |
} |
142 |
} |
| 139 |
$input =~ s/[\r\n]+$//sm; # Strip off trailing line terminator(s) |
143 |
$input =~ s/[\r\n]+$//sm; # Strip off trailing line terminator(s) |
| 140 |
last if C4::SIP::Sip::MsgType::handle($input, $self, LOGIN); |
144 |
last if C4::SIP::Sip::MsgType::handle($input, $self, LOGIN); |
| 141 |
} |
145 |
} |
| 142 |
|
146 |
|
| 143 |
syslog("LOG_DEBUG", "raw_transport: uname/inst: '%s/%s'", |
147 |
log_debug { "raw_transport: uname/inst: '%s/%s'", |
| 144 |
$self->{account}->{id}, |
148 |
$self->{account}->{id}, |
| 145 |
$self->{account}->{institution}); |
149 |
$self->{account}->{institution} }; |
| 146 |
|
150 |
|
| 147 |
$self->sip_protocol_loop(); |
151 |
$self->sip_protocol_loop(); |
| 148 |
syslog("LOG_INFO", "raw_transport: shutting down"); |
152 |
log_info { "raw_transport: shutting down" }; |
| 149 |
} |
153 |
} |
| 150 |
|
154 |
|
| 151 |
sub get_clean_string { |
155 |
sub get_clean_string { |
| 152 |
my $string = shift; |
156 |
my $string = shift; |
| 153 |
if (defined $string) { |
157 |
if (defined $string) { |
| 154 |
syslog("LOG_DEBUG", "get_clean_string pre-clean(length %s): %s", length($string), $string); |
158 |
log_debug { "get_clean_string pre-clean(length %s): %s", length($string), $string }; |
| 155 |
chomp($string); |
159 |
chomp($string); |
| 156 |
$string =~ s/^[^A-z0-9]+//; |
160 |
$string =~ s/^[^A-z0-9]+//; |
| 157 |
$string =~ s/[^A-z0-9]+$//; |
161 |
$string =~ s/[^A-z0-9]+$//; |
| 158 |
syslog("LOG_DEBUG", "get_clean_string post-clean(length %s): %s", length($string), $string); |
162 |
log_debug { "get_clean_string post-clean(length %s): %s", length($string), $string }; |
| 159 |
} else { |
163 |
} else { |
| 160 |
syslog("LOG_INFO", "get_clean_string called on undefined"); |
164 |
log_info { "get_clean_string called on undefined" }; |
| 161 |
} |
165 |
} |
| 162 |
return $string; |
166 |
return $string; |
| 163 |
} |
167 |
} |
|
Lines 167-173
sub get_clean_input {
Link Here
|
| 167 |
my $in = <STDIN>; |
171 |
my $in = <STDIN>; |
| 168 |
$in = get_clean_string($in); |
172 |
$in = get_clean_string($in); |
| 169 |
while (my $extra = <STDIN>){ |
173 |
while (my $extra = <STDIN>){ |
| 170 |
syslog("LOG_ERR", "get_clean_input got extra lines: %s", $extra); |
174 |
log_error { "get_clean_input got extra lines: %s", $extra }; |
| 171 |
} |
175 |
} |
| 172 |
return $in; |
176 |
return $in; |
| 173 |
} |
177 |
} |
|
Lines 180-186
sub telnet_transport {
Link Here
|
| 180 |
my $input; |
184 |
my $input; |
| 181 |
my $config = $self->{config}; |
185 |
my $config = $self->{config}; |
| 182 |
my $timeout = $self->{service}->{timeout} || $config->{timeout} || 30; |
186 |
my $timeout = $self->{service}->{timeout} || $config->{timeout} || 30; |
| 183 |
syslog("LOG_DEBUG", "telnet_transport: timeout is %s", $timeout); |
187 |
log_debug { "telnet_transport: timeout is %s", $timeout }; |
| 184 |
|
188 |
|
| 185 |
eval { |
189 |
eval { |
| 186 |
local $SIG{ALRM} = sub { die "telnet_transport: Timed Out ($timeout seconds)!\n"; }; |
190 |
local $SIG{ALRM} = sub { die "telnet_transport: Timed Out ($timeout seconds)!\n"; }; |
|
Lines 199-208
sub telnet_transport {
Link Here
|
| 199 |
$pwd = <STDIN>; |
203 |
$pwd = <STDIN>; |
| 200 |
alarm 0; |
204 |
alarm 0; |
| 201 |
|
205 |
|
| 202 |
syslog("LOG_DEBUG", "telnet_transport 1: uid length %s, pwd length %s", length($uid), length($pwd)); |
206 |
log_debug { "telnet_transport 1: uid length %s, pwd length %s", length($uid), length($pwd) }; |
| 203 |
$uid = get_clean_string ($uid); |
207 |
$uid = get_clean_string ($uid); |
| 204 |
$pwd = get_clean_string ($pwd); |
208 |
$pwd = get_clean_string ($pwd); |
| 205 |
syslog("LOG_DEBUG", "telnet_transport 2: uid length %s, pwd length %s", length($uid), length($pwd)); |
209 |
log_debug { "telnet_transport 2: uid length %s, pwd length %s", length($uid), length($pwd) }; |
| 206 |
|
210 |
|
| 207 |
if (exists ($config->{accounts}->{$uid}) |
211 |
if (exists ($config->{accounts}->{$uid}) |
| 208 |
&& ($pwd eq $config->{accounts}->{$uid}->password())) { |
212 |
&& ($pwd eq $config->{accounts}->{$uid}->password())) { |
|
Lines 211-235
sub telnet_transport {
Link Here
|
| 211 |
last; |
215 |
last; |
| 212 |
} |
216 |
} |
| 213 |
} |
217 |
} |
| 214 |
syslog("LOG_WARNING", "Invalid login attempt: '%s'", ($uid||'')); |
218 |
log_warn { "Invalid login attempt: '%s'", ($uid||'') }; |
| 215 |
print("Invalid login$CRLF"); |
219 |
print("Invalid login$CRLF"); |
| 216 |
} |
220 |
} |
| 217 |
}; # End of eval |
221 |
}; # End of eval |
| 218 |
|
222 |
|
| 219 |
if ($@) { |
223 |
if ($@) { |
| 220 |
syslog("LOG_ERR", "telnet_transport: Login timed out"); |
224 |
log_fatal { "telnet_transport: Login timed out" }; |
| 221 |
die "Telnet Login Timed out"; |
|
|
| 222 |
} elsif (!defined($account)) { |
225 |
} elsif (!defined($account)) { |
| 223 |
syslog("LOG_ERR", "telnet_transport: Login Failed"); |
226 |
log_fatal { "telnet_transport: Login Failed" }; |
| 224 |
die "Login Failure"; |
|
|
| 225 |
} else { |
227 |
} else { |
| 226 |
print "Login OK. Initiating SIP$CRLF"; |
228 |
print "Login OK. Initiating SIP$CRLF"; |
| 227 |
} |
229 |
} |
| 228 |
|
230 |
|
| 229 |
$self->{account} = $account; |
231 |
$self->{account} = $account; |
| 230 |
syslog("LOG_DEBUG", "telnet_transport: uname/inst: '%s/%s'", $account->{id}, $account->{institution}); |
232 |
log_debug { "telnet_transport: uname/inst: '%s/%s'", $account->{id}, $account->{institution} }; |
| 231 |
$self->sip_protocol_loop(); |
233 |
$self->sip_protocol_loop(); |
| 232 |
syslog("LOG_INFO", "telnet_transport: shutting down"); |
234 |
log_info { "telnet_transport: shutting down" }; |
| 233 |
} |
235 |
} |
| 234 |
|
236 |
|
| 235 |
# |
237 |
# |
|
Lines 271-289
sub sip_protocol_loop {
Link Here
|
| 271 |
$input =~ s/[^A-z0-9]+$//s; # Same on the end, should get DOSsy ^M line-endings too. |
273 |
$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 |
while (chomp($input)) {warn "Extra line ending on input";} |
| 273 |
unless ($input) { |
275 |
unless ($input) { |
| 274 |
syslog("LOG_ERR", "sip_protocol_loop: empty input skipped"); |
276 |
log_error { "sip_protocol_loop: empty input skipped" }; |
| 275 |
print("96$CR"); |
277 |
print("96$CR"); |
| 276 |
next; |
278 |
next; |
| 277 |
} |
279 |
} |
| 278 |
# end cheap input hacks |
280 |
# end cheap input hacks |
| 279 |
my $status = handle($input, $self, $expect); |
281 |
my $status = handle($input, $self, $expect); |
| 280 |
if (!$status) { |
282 |
if (!$status) { |
| 281 |
syslog("LOG_ERR", "sip_protocol_loop: failed to handle %s",substr($input,0,2)); |
283 |
log_error { "sip_protocol_loop: failed to handle %s",substr($input,0,2) }; |
| 282 |
} |
284 |
} |
| 283 |
next if $status eq REQUEST_ACS_RESEND; |
285 |
next if $status eq REQUEST_ACS_RESEND; |
| 284 |
if ($expect && ($status ne $expect)) { |
286 |
if ($expect && ($status ne $expect)) { |
| 285 |
# We received a non-"RESEND" that wasn't what we were expecting. |
287 |
# 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 |
log_error { "sip_protocol_loop: expected %s, received %s, exiting", $expect, $input }; |
| 287 |
} |
289 |
} |
| 288 |
# We successfully received and processed what we were expecting |
290 |
# We successfully received and processed what we were expecting |
| 289 |
$expect = ''; |
291 |
$expect = ''; |
| 290 |
- |
|
|