Line 0
Link Here
|
|
|
1 |
#!/usr/bin/perl -w |
2 |
|
3 |
# Copyright 2012 CatalystIT |
4 |
# |
5 |
# This file is part of Koha. |
6 |
# |
7 |
# Koha is free software; you can redistribute it and/or modify it under the |
8 |
# terms of the GNU General Public License as published by the Free Software |
9 |
# Foundation; either version 2 of the License, or (at your option) any later |
10 |
# version. |
11 |
# |
12 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
13 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
14 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
15 |
# |
16 |
# You should have received a copy of the GNU General Public License along |
17 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
18 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
19 |
|
20 |
use strict; |
21 |
use warnings; |
22 |
|
23 |
use Getopt::Long; |
24 |
|
25 |
my ($help, $config, $daemon); |
26 |
|
27 |
GetOptions( |
28 |
'config|c=s' => \$config, |
29 |
'daemon|d' => \$daemon, |
30 |
'help|?' => \$help, |
31 |
); |
32 |
|
33 |
if($help || !$config){ |
34 |
print <<EOF |
35 |
$0 --config=my.conf |
36 |
Parameters : |
37 |
--daemon | d - go to background; prints pid to stdout |
38 |
--config | c - config file |
39 |
--help | ? - this message |
40 |
|
41 |
Config file format: |
42 |
Lines of the form: |
43 |
name: value |
44 |
|
45 |
# comments are supported |
46 |
No quotes |
47 |
|
48 |
Parameter Names: |
49 |
host - ip address or hostname to bind to, defaults all available |
50 |
port - port to bind to, mandatory |
51 |
log - log file path, stderr if omitted |
52 |
debug - dumps requests to the log file, passwords inclusive |
53 |
koha - koha intranet base url, eg http://librarian.koha |
54 |
user - koha user, authentication |
55 |
password - koha user password, authentication |
56 |
match - marc_matchers.code: ISBN or ISSN |
57 |
overlay_action - import_batches.overlay_action: replace, create_new or ignore |
58 |
nomatch_action - import_batches.nomatch_action: create_new or ignore |
59 |
item_action - import_batches.item_action: always_add, |
60 |
add_only_for_matches, add_only_for_new or ignore |
61 |
import_mode - stage or direct |
62 |
framework - to be used if import_mode is direct |
63 |
|
64 |
All process related parameters (all but ip and port) have default values as |
65 |
per Koha import process. |
66 |
EOF |
67 |
; |
68 |
exit; |
69 |
} |
70 |
|
71 |
my $server = ImportProxyServer->new($config); |
72 |
|
73 |
if ($daemon) { |
74 |
print $server->background; |
75 |
} else { |
76 |
$server->run; |
77 |
} |
78 |
|
79 |
exit; |
80 |
|
81 |
{ |
82 |
package ImportProxyServer; |
83 |
|
84 |
use Carp; |
85 |
use IO::Socket::INET; |
86 |
# use IO::Socket::IP; |
87 |
use IO::Select; |
88 |
use POSIX; |
89 |
use HTTP::Status qw(:constants); |
90 |
|
91 |
use LWP::UserAgent; |
92 |
use XML::Simple; |
93 |
|
94 |
use constant CLIENT_READ_TIMEOUT => 5; |
95 |
use constant CLIENT_READ_BUFFER_SIZE => 4 * 1024; |
96 |
use constant AUTH_URI => "/cgi-bin/koha/mainpage.pl"; |
97 |
use constant IMPORT_SVC_URI => "/cgi-bin/koha/svc/import_bib"; |
98 |
|
99 |
sub new { |
100 |
my $class = shift; |
101 |
my $config_file = shift or croak "No config file"; |
102 |
|
103 |
my $self = {time_to_die => 0, config_file => $config_file }; |
104 |
bless $self, $class; |
105 |
|
106 |
$self->parse_config; |
107 |
return $self; |
108 |
} |
109 |
|
110 |
sub parse_config { |
111 |
my $self = shift; |
112 |
|
113 |
my $config_file = $self->{config_file}; |
114 |
|
115 |
open CONF, $config_file or die "Cannot open config file $config: $!"; |
116 |
|
117 |
my %param; |
118 |
my $line = 0; |
119 |
while (<CONF>) { |
120 |
$line++; |
121 |
chomp; |
122 |
s/\s*#.*//o; # remove comments |
123 |
s/^\s+//o; # trim leading spaces |
124 |
s/\s+$//o; # trim trailing spaces |
125 |
next unless $_; |
126 |
|
127 |
my ($p, $v) = m/(\S+?):\s*(.*)/o; |
128 |
die "Invalid config line $line: $_" unless defined $v; |
129 |
$param{$p} = $v; |
130 |
} |
131 |
|
132 |
$self->{koha} = delete( $param{koha} ) |
133 |
or die "No koha base url in config file"; |
134 |
$self->{user} = delete( $param{user} ) |
135 |
or die "No koha user in config file"; |
136 |
$self->{password} = delete( $param{password} ) |
137 |
or die "No koha user password in config file"; |
138 |
|
139 |
$self->{host} = delete( $param{host} ); |
140 |
$self->{port} = delete( $param{port} ) |
141 |
or die "Port not specified"; |
142 |
|
143 |
$self->{debug} = delete( $param{debug} ); |
144 |
|
145 |
my $log_fh; |
146 |
close $self->{log_fh} if $self->{log_fh}; |
147 |
if (my $logfile = delete $param{log}) { |
148 |
open $log_fh, ">>$logfile" or die "Cannot open $logfile for write: $!"; |
149 |
} else { |
150 |
$log_fh = \*STDERR; |
151 |
} |
152 |
$self->{log_fh} = $log_fh; |
153 |
|
154 |
my $prefix = ""; |
155 |
while ( my ($p, $v) = each %param ) { |
156 |
$prefix .= "$p: $v\n"; |
157 |
} |
158 |
|
159 |
$self->{prefix} = $prefix; |
160 |
} |
161 |
|
162 |
sub log { |
163 |
my $self = shift; |
164 |
my $log_fh = $self->{log_fh} |
165 |
or warn "No log fh", |
166 |
return; |
167 |
my $t = localtime; |
168 |
print $log_fh map "$t: $_\n", @_; |
169 |
} |
170 |
|
171 |
sub background { |
172 |
my $self = shift; |
173 |
|
174 |
my $pid = fork; |
175 |
return ($pid) if $pid; # parent |
176 |
|
177 |
die "Couldn't fork: $!" unless defined($pid); |
178 |
|
179 |
POSIX::setsid() or die "Can't start a new session: $!"; |
180 |
|
181 |
$SIG{INT} = $SIG{TERM} = $SIG{HUP} = sub { $self->{time_to_die} = 1 }; |
182 |
# trap or ignore $SIG{PIPE} |
183 |
$SIG{USR1} = sub { $self->parse_config }; |
184 |
|
185 |
$self->run; |
186 |
} |
187 |
|
188 |
sub run { |
189 |
my $self = shift; |
190 |
|
191 |
my $server_port = $self->{port}; |
192 |
my $server_host = $self->{host}; |
193 |
|
194 |
my $server = IO::Socket::INET->new( |
195 |
LocalHost => $server_host, |
196 |
LocalPort => $server_port, |
197 |
Type => SOCK_STREAM, |
198 |
Proto => "tcp", |
199 |
Listen => 12, |
200 |
Blocking => 1, |
201 |
ReuseAddr => 1, |
202 |
) or die "Couldn't be a tcp server on port $server_port: $! $@"; |
203 |
|
204 |
$self->log("Started tcp listener on $server_host:$server_port"); |
205 |
|
206 |
$self->{ua} = _ua(); |
207 |
|
208 |
while ("FOREVER") { |
209 |
my $client = $server->accept() |
210 |
or die "Cannot accept: $!"; |
211 |
my $oldfh = select($client); |
212 |
$self->handle_request($client); |
213 |
select($oldfh); |
214 |
last if $self->{time_to_die}; |
215 |
} |
216 |
|
217 |
close($server); |
218 |
} |
219 |
|
220 |
sub _ua { |
221 |
my $ua = LWP::UserAgent->new; |
222 |
$ua->timeout(10); |
223 |
$ua->cookie_jar({}); |
224 |
return $ua; |
225 |
} |
226 |
|
227 |
sub read_request { |
228 |
my ( $self, $io ) = @_; |
229 |
|
230 |
my ($in, @in, $timeout); |
231 |
my $select = IO::Select->new($io) ; |
232 |
while ( "FOREVER" ) { |
233 |
if ( $select->can_read(CLIENT_READ_TIMEOUT) ){ |
234 |
$io->recv($in, CLIENT_READ_BUFFER_SIZE); |
235 |
last unless $in; |
236 |
|
237 |
# XXX ignore after NULL |
238 |
if ( $in =~ m/^(.*)\000/so ) { # null received, EOT |
239 |
push @in, $1; |
240 |
last; |
241 |
} |
242 |
push @in, $in; |
243 |
} |
244 |
else { |
245 |
$timeout = 1; |
246 |
last; |
247 |
} |
248 |
} |
249 |
|
250 |
$in = join '', @in; |
251 |
|
252 |
my ($xml, $user, $password, $local_user); |
253 |
my $data = $in; # copy for diagmostic purposes |
254 |
while ( my $first = substr( $data, 0, 1 ) ) { |
255 |
$first eq 'U' && do { |
256 |
($user, $data) = _trim_identifier($data); |
257 |
next; |
258 |
}; |
259 |
$first eq 'A' && do { |
260 |
($local_user, $data) = _trim_identifier($data); |
261 |
next; |
262 |
}; |
263 |
$first eq 'P' && do { |
264 |
($password,, $data) = _trim_identifier($data); |
265 |
next; |
266 |
}; |
267 |
$first eq ' ' && do { |
268 |
$data = substr( $data, 1 ); |
269 |
next; |
270 |
}; |
271 |
$first eq '<' && do { |
272 |
$xml = $data; |
273 |
last; |
274 |
}; |
275 |
|
276 |
last; # unexpected input |
277 |
} |
278 |
|
279 |
my @details; |
280 |
push @details, "Timeout" if $timeout; |
281 |
push @details, "User: $user" if $user; |
282 |
push @details, "Password: " . ( $self->{debug} ? $password : ("x" x length($password)) ) if $password; |
283 |
push @details, "Local user: $local_user" if $local_user; |
284 |
unless ($xml) { |
285 |
$self->log("Invalid request", $in, @details); |
286 |
return; |
287 |
} |
288 |
|
289 |
$self->log("Request", @details); |
290 |
$self->log($in) if $self->{debug}; |
291 |
return ($xml, $user, $password); |
292 |
} |
293 |
|
294 |
sub _trim_identifier { |
295 |
my ($a, $len) = unpack "cc", substr( $_[0], 0, 2 ); |
296 |
|
297 |
return ( substr( $_[0], 2, $len ), substr( $_[0], 2 + $len ) ); |
298 |
} |
299 |
|
300 |
sub handle_request { |
301 |
my ( $self, $io ) = @_; |
302 |
|
303 |
my ($data, $user, $password) = $self->read_request($io) |
304 |
or return $self->error_response("Bad request"); |
305 |
|
306 |
$data = $self->{prefix} . $data; |
307 |
|
308 |
my $ua; |
309 |
if ($self->{user}) { |
310 |
$user = $self->{user}; |
311 |
$password = $self->{password}; |
312 |
$ua = $self->{ua}; |
313 |
} |
314 |
else { |
315 |
$ua = _ua(); # fresh one, needs to authenticate |
316 |
} |
317 |
|
318 |
my $base_url = $self->{koha}; |
319 |
my $resp = $ua->post( $base_url.IMPORT_SVC_URI, 'Content-Type' => 'text/plain', Content => $data ); |
320 |
my $status = $resp->code; |
321 |
if ($status == HTTP_UNAUTHORIZED || $status == HTTP_FORBIDDEN) { |
322 |
my $user = $self->{user}; |
323 |
my $password = $self->{password}; |
324 |
$resp = $ua->post( $base_url.AUTH_URI, { userid => $user, password => $password } ); |
325 |
$resp = $ua->post( $base_url.IMPORT_SVC_URI, 'Content-Type' => 'text/plain', Content => $data ) |
326 |
if $resp->is_success; |
327 |
} |
328 |
unless ($resp->is_success) { |
329 |
$self->log("Unsuccessful request", $resp->request->as_string, $resp->as_string); |
330 |
return $self->error_response("Unsuccessful request"); |
331 |
} |
332 |
|
333 |
my ($koha_status, $bib, $batch_id, $error); |
334 |
if ( my $r = eval { XMLin($resp->content) } ) { |
335 |
$koha_status = $r->{status}; |
336 |
$batch_id = $r->{import_batch_id}; |
337 |
$error = $r->{error}; |
338 |
} |
339 |
else { |
340 |
$koha_status = "error"; |
341 |
$self->log("Response format error:\n$resp->content"); |
342 |
return $self->error_response("Invalid response"); |
343 |
} |
344 |
|
345 |
if ($koha_status eq "ok") { |
346 |
return $self->response( sprintf( "Success. Import batch id: %s", $batch_id ) ); |
347 |
} |
348 |
|
349 |
return $self->error_response( sprintf( "%s. Please contact administrator.", $error ) ); |
350 |
} |
351 |
|
352 |
sub error_response { |
353 |
my $self = shift; |
354 |
$self->response(@_); |
355 |
} |
356 |
|
357 |
sub response { |
358 |
my $self = shift; |
359 |
printf $_[0] . "\0"; |
360 |
} |
361 |
|
362 |
|
363 |
} # package |
364 |
|