1 | #!perl -w
|
---|
2 | # Need to specify the full path of Perl above on Unix
|
---|
3 |
|
---|
4 |
|
---|
5 | use gsdlCGI;
|
---|
6 | use strict;
|
---|
7 |
|
---|
8 |
|
---|
9 | my $authentication_enabled = 1;
|
---|
10 | my $debugging_enabled = 0;
|
---|
11 |
|
---|
12 | my $mail_enabled = 0;
|
---|
13 | my $mail_to_address = "user\@server"; # Set this appropriately
|
---|
14 | my $mail_from_address = "user\@server"; # Set this appropriately
|
---|
15 | my $mail_smtp_server = "smtp.server"; # Set this appropriately
|
---|
16 |
|
---|
17 |
|
---|
18 | sub main
|
---|
19 | {
|
---|
20 | my $gsdl_cgi = new gsdlCGI();
|
---|
21 |
|
---|
22 | # Load the Greenstone modules that we need to use
|
---|
23 | $gsdl_cgi->setup_gsdl();
|
---|
24 | my $gsdlhome = $ENV{'GSDLHOME'};
|
---|
25 | $gsdl_cgi->checked_chdir($gsdlhome);
|
---|
26 | require "$gsdlhome/perllib/util.pm"; # This is OK on Windows
|
---|
27 | require "$gsdlhome/perllib/cpan/Crypt/UnixCrypt.pm"; # This is OK on Windows
|
---|
28 |
|
---|
29 | # Encrypt the password
|
---|
30 | if (defined $gsdl_cgi->param("pw")) {
|
---|
31 | $gsdl_cgi->param('-name' => "pw", '-value' => crypt($gsdl_cgi->clean_param("pw"), "Tp"));
|
---|
32 | }
|
---|
33 |
|
---|
34 | $gsdl_cgi->parse_cgi_args();
|
---|
35 |
|
---|
36 | my $cmd = $gsdl_cgi->clean_param("cmd");
|
---|
37 | if (!defined $cmd) {
|
---|
38 | $gsdl_cgi->generate_error("No command specified.");
|
---|
39 | }
|
---|
40 | $gsdl_cgi->delete("cmd");
|
---|
41 |
|
---|
42 | # All commands require a username, for locking and authentication
|
---|
43 | my $username = $gsdl_cgi->clean_param("un");
|
---|
44 | if ((!defined $username) || ($username =~ m/^\s*$/)) {
|
---|
45 | $gsdl_cgi->generate_error("No username specified.");
|
---|
46 | }
|
---|
47 | $gsdl_cgi->delete("un");
|
---|
48 |
|
---|
49 | if ($cmd eq "delete-collection") {
|
---|
50 | &delete_collection($gsdl_cgi, $username);
|
---|
51 | }
|
---|
52 | elsif ($cmd eq "download-collection") {
|
---|
53 | &download_collection($gsdl_cgi, $username);
|
---|
54 | }
|
---|
55 | elsif ($cmd eq "download-collection-archives") {
|
---|
56 | &download_collection_archives($gsdl_cgi, $username);
|
---|
57 | }
|
---|
58 | elsif ($cmd eq "download-collection-configurations") {
|
---|
59 | &download_collection_configurations($gsdl_cgi, $username);
|
---|
60 | }
|
---|
61 | elsif ($cmd eq "download-collection-file") {
|
---|
62 | &download_collection_file($gsdl_cgi, $username);
|
---|
63 | }
|
---|
64 | elsif ($cmd eq "delete-collection-file") {
|
---|
65 | &delete_collection_file($gsdl_cgi, $username);
|
---|
66 | }
|
---|
67 | elsif ($cmd eq "get-script-options") {
|
---|
68 | &get_script_options($gsdl_cgi, $username);
|
---|
69 | }
|
---|
70 | elsif ($cmd eq "move-collection-file") {
|
---|
71 | &move_collection_file($gsdl_cgi, $username);
|
---|
72 | }
|
---|
73 | elsif ($cmd eq "new-collection-directory") {
|
---|
74 | &new_collection_directory($gsdl_cgi, $username);
|
---|
75 | }
|
---|
76 | elsif ($cmd eq "run-script") {
|
---|
77 | &run_script($gsdl_cgi, $username);
|
---|
78 | }
|
---|
79 | elsif ($cmd eq "timeout-test") {
|
---|
80 | while (1) { }
|
---|
81 | }
|
---|
82 | elsif ($cmd eq "upload-collection-file") {
|
---|
83 | &upload_collection_file($gsdl_cgi, $username);
|
---|
84 | }
|
---|
85 | else {
|
---|
86 | $gsdl_cgi->generate_error("Unrecognised command: '$cmd'");
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 |
|
---|
91 | sub authenticate_user
|
---|
92 | {
|
---|
93 | my $gsdl_cgi = shift(@_);
|
---|
94 | my $username = shift(@_);
|
---|
95 | my $user_group_required = shift(@_);
|
---|
96 |
|
---|
97 | # Even if we're not authenticating remove the un and pw arguments, since these can mess up other scripts
|
---|
98 | my $user_password = $gsdl_cgi->clean_param("pw");
|
---|
99 | $gsdl_cgi->delete("pw");
|
---|
100 |
|
---|
101 | # Only authenticate if it is enabled
|
---|
102 | return if (!$authentication_enabled);
|
---|
103 |
|
---|
104 | if ((!defined $user_password) || ($user_password =~ m/^\s*$/)) {
|
---|
105 | $gsdl_cgi->generate_error("Authentication failed: no password specified.");
|
---|
106 | }
|
---|
107 |
|
---|
108 | my $gsdlhome = $ENV{'GSDLHOME'};
|
---|
109 | my $etc_directory = &util::filename_cat($gsdlhome, "etc");
|
---|
110 | my $users_db_file_path = &util::filename_cat($etc_directory, "users.db");
|
---|
111 |
|
---|
112 | # Use db2txt instead of GDBM_File to get the user accounts information
|
---|
113 | my $users_db_content = "";
|
---|
114 | open(USERS_DB, "db2txt \"$users_db_file_path\" |");
|
---|
115 | while (<USERS_DB>) {
|
---|
116 | $users_db_content .= $_;
|
---|
117 | }
|
---|
118 |
|
---|
119 | # Get the user account information from the users.db database
|
---|
120 | my %users_db_data = ();
|
---|
121 | foreach my $users_db_entry (split(/-{70}/, $users_db_content)) {
|
---|
122 | if ($users_db_entry =~ /\n?\[(.+)\]\n/) {
|
---|
123 | $users_db_data{$1} = $users_db_entry;
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | # Check username
|
---|
128 | my $user_data = $users_db_data{$username};
|
---|
129 | if (!defined $user_data) {
|
---|
130 | $gsdl_cgi->generate_error("Authentication failed: no account for user '$username'.");
|
---|
131 | }
|
---|
132 |
|
---|
133 | # Check password
|
---|
134 | my ($valid_user_password) = ($user_data =~ /\<password\>(.*)/);
|
---|
135 | if ($user_password ne $valid_user_password) {
|
---|
136 | $gsdl_cgi->generate_error("Authentication failed: incorrect password.");
|
---|
137 | }
|
---|
138 |
|
---|
139 | # Check group
|
---|
140 | my ($user_groups) = ($user_data =~ /\<groups\>(.*)/);
|
---|
141 | foreach my $user_group (split(/\,/, $user_groups)) {
|
---|
142 | if ($user_group eq $user_group_required || $user_group eq "remote-superuser") {
|
---|
143 | # Authentication successful!
|
---|
144 | return;
|
---|
145 | }
|
---|
146 | }
|
---|
147 |
|
---|
148 | $gsdl_cgi->generate_error("Authentication failed: user is not in the required group.");
|
---|
149 | }
|
---|
150 |
|
---|
151 |
|
---|
152 | sub lock_collection
|
---|
153 | {
|
---|
154 | my $gsdl_cgi = shift(@_);
|
---|
155 | my $collection = shift(@_);
|
---|
156 | my $username = shift(@_);
|
---|
157 |
|
---|
158 | my $steal_lock = $gsdl_cgi->clean_param("steal_lock");
|
---|
159 | $gsdl_cgi->delete("steal_lock");
|
---|
160 |
|
---|
161 | my $gsdlhome = $ENV{'GSDLHOME'};
|
---|
162 | my $collection_directory = &util::filename_cat($gsdlhome, "collect", $collection);
|
---|
163 | $gsdl_cgi->checked_chdir($collection_directory);
|
---|
164 |
|
---|
165 | # Check if a lock file already exists for this collection
|
---|
166 | my $lock_file_name = "gli.lck";
|
---|
167 | if (-e $lock_file_name) {
|
---|
168 | # A lock file already exists... check if it's ours
|
---|
169 | open(LOCK_FILE, "<$lock_file_name");
|
---|
170 | my $lock_file_owner = <LOCK_FILE>;
|
---|
171 | close(LOCK_FILE);
|
---|
172 |
|
---|
173 | # The lock file is ours, so there is no problem
|
---|
174 | if ($lock_file_owner eq $username) {
|
---|
175 | return;
|
---|
176 | }
|
---|
177 |
|
---|
178 | # The lock file is not ours, so throw an error unless "steal_lock" is set
|
---|
179 | unless (defined $steal_lock) {
|
---|
180 | $gsdl_cgi->generate_error("Collection is locked by: $lock_file_owner");
|
---|
181 | }
|
---|
182 | }
|
---|
183 |
|
---|
184 | # Create a lock file for us and we're done
|
---|
185 | open(LOCK_FILE, ">$lock_file_name");
|
---|
186 | print LOCK_FILE "$username";
|
---|
187 | close(LOCK_FILE);
|
---|
188 | }
|
---|
189 |
|
---|
190 |
|
---|
191 | # ----------------------------------------------------------------------------------------------------
|
---|
192 | # ACTIONS
|
---|
193 | # ----------------------------------------------------------------------------------------------------
|
---|
194 |
|
---|
195 |
|
---|
196 | sub delete_collection
|
---|
197 | {
|
---|
198 | my ($gsdl_cgi, $username) = @_;
|
---|
199 |
|
---|
200 | my $collection = $gsdl_cgi->clean_param("c");
|
---|
201 | if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
|
---|
202 | $gsdl_cgi->generate_error("No collection specified.");
|
---|
203 | }
|
---|
204 |
|
---|
205 | # Users must be in the <collection>-maintainer group to perform this action
|
---|
206 | &authenticate_user($gsdl_cgi, $username, "$collection-maintainer");
|
---|
207 |
|
---|
208 | my $gsdlhome = $ENV{'GSDLHOME'};
|
---|
209 | my $collect_directory = &util::filename_cat($gsdlhome, "collect");
|
---|
210 | $gsdl_cgi->checked_chdir($collect_directory);
|
---|
211 |
|
---|
212 | # Check that the collection exists
|
---|
213 | if (!-d $collection) {
|
---|
214 | $gsdl_cgi->generate_error("Collection $collection does not exist.");
|
---|
215 | }
|
---|
216 |
|
---|
217 | # Make sure the collection isn't locked by someone else
|
---|
218 | &lock_collection($gsdl_cgi, $collection, $username);
|
---|
219 |
|
---|
220 | $gsdl_cgi->checked_chdir($collect_directory);
|
---|
221 | $gsdl_cgi->local_rm_r("$collection");
|
---|
222 |
|
---|
223 | # Check that the collection was deleted
|
---|
224 | if (-e $collection) {
|
---|
225 | $gsdl_cgi->generate_error("Could not delete collection $collection.");
|
---|
226 | }
|
---|
227 |
|
---|
228 | $gsdl_cgi->generate_ok_message("Collection $collection deleted successfully.");
|
---|
229 | }
|
---|
230 |
|
---|
231 |
|
---|
232 | sub delete_collection_file
|
---|
233 | {
|
---|
234 | my ($gsdl_cgi, $username) = @_;
|
---|
235 |
|
---|
236 | my $collection = $gsdl_cgi->clean_param("c");
|
---|
237 | if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
|
---|
238 | $gsdl_cgi->generate_error("No collection specified.");
|
---|
239 | }
|
---|
240 | my $file = $gsdl_cgi->clean_param("file");
|
---|
241 | if ((!defined $file) || ($file =~ m/^\s*$/)) {
|
---|
242 | $gsdl_cgi->generate_error("No file specified.");
|
---|
243 | }
|
---|
244 | $file =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
|
---|
245 |
|
---|
246 | # Make sure we don't try to delete anything outside the collection
|
---|
247 | if ($file =~ /\.\./) {
|
---|
248 | $gsdl_cgi->generate_error("Illegal file specified.");
|
---|
249 | }
|
---|
250 |
|
---|
251 | # Users must be in the <collection>-maintainer group to perform this action
|
---|
252 | &authenticate_user($gsdl_cgi, $username, "$collection-maintainer");
|
---|
253 |
|
---|
254 | my $gsdlhome = $ENV{'GSDLHOME'};
|
---|
255 | my $collection_directory = &util::filename_cat($gsdlhome, "collect", $collection);
|
---|
256 | $gsdl_cgi->checked_chdir($collection_directory);
|
---|
257 |
|
---|
258 | # Make sure the collection isn't locked by someone else
|
---|
259 | &lock_collection($gsdl_cgi, $collection, $username);
|
---|
260 |
|
---|
261 | # Check that the collection file exists
|
---|
262 | if (!-e $file) {
|
---|
263 | $gsdl_cgi->generate_error("Collection file $file does not exist.");
|
---|
264 | }
|
---|
265 | $gsdl_cgi->local_rm_r("$file");
|
---|
266 |
|
---|
267 | # Check that the collection file was deleted
|
---|
268 | if (-e $file) {
|
---|
269 | $gsdl_cgi->generate_error("Could not delete collection file $file.");
|
---|
270 | }
|
---|
271 |
|
---|
272 | $gsdl_cgi->generate_ok_message("Collection file $file deleted successfully.");
|
---|
273 | }
|
---|
274 |
|
---|
275 |
|
---|
276 | sub download_collection
|
---|
277 | {
|
---|
278 | my ($gsdl_cgi, $username) = @_;
|
---|
279 |
|
---|
280 | my $collection = $gsdl_cgi->clean_param("c");
|
---|
281 | if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
|
---|
282 | $gsdl_cgi->generate_error("No collection specified.");
|
---|
283 | }
|
---|
284 |
|
---|
285 | # Users must be in the <collection>-maintainer group to perform this action
|
---|
286 | &authenticate_user($gsdl_cgi, $username, "$collection-maintainer");
|
---|
287 |
|
---|
288 | my $gsdlhome = $ENV{'GSDLHOME'};
|
---|
289 | my $collect_directory = &util::filename_cat($gsdlhome, "collect");
|
---|
290 | $gsdl_cgi->checked_chdir($collect_directory);
|
---|
291 |
|
---|
292 | # Check that the collection exists
|
---|
293 | if (!-d $collection) {
|
---|
294 | $gsdl_cgi->generate_error("Collection $collection does not exist.");
|
---|
295 | }
|
---|
296 |
|
---|
297 | # Make sure the collection isn't locked by someone else
|
---|
298 | &lock_collection($gsdl_cgi, $collection, $username);
|
---|
299 |
|
---|
300 | # Zip up the collection
|
---|
301 | my $java = $gsdl_cgi->get_java_path();
|
---|
302 | my $java_classpath = &util::filename_cat($gsdlhome, "bin", "java", "GLIServer.jar");
|
---|
303 | my $zip_file_path = &util::filename_cat($collect_directory, $username . "-" . $collection . ".zip");
|
---|
304 | my $java_args = "\"$zip_file_path\" \"$collect_directory\" \"$collection\"";
|
---|
305 | my $java_command = "$java -classpath \"$java_classpath\" org.greenstone.gatherer.remote.ZipCollectionShell $java_args";
|
---|
306 |
|
---|
307 | my $java_output = `$java_command`;
|
---|
308 | my $java_status = $?;
|
---|
309 | if ($java_status > 0) {
|
---|
310 | $gsdl_cgi->generate_error("Java failed: $java_command\n--\n$java_output\nExit status: " . ($java_status / 256) . "\n" . $gsdl_cgi->check_java_home());
|
---|
311 | }
|
---|
312 |
|
---|
313 | # Check that the zip file was created successfully
|
---|
314 | if (!-e $zip_file_path || -z $zip_file_path) {
|
---|
315 | $gsdl_cgi->generate_error("Collection zip file $zip_file_path could not be created.");
|
---|
316 | }
|
---|
317 |
|
---|
318 | &put_file($gsdl_cgi, $zip_file_path, "application/zip");
|
---|
319 | unlink("$zip_file_path") unless $debugging_enabled;
|
---|
320 | }
|
---|
321 |
|
---|
322 |
|
---|
323 | sub download_collection_archives
|
---|
324 | {
|
---|
325 | my ($gsdl_cgi, $username) = @_;
|
---|
326 |
|
---|
327 | my $collection = $gsdl_cgi->clean_param("c");
|
---|
328 | if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
|
---|
329 | $gsdl_cgi->generate_error("No collection specified.");
|
---|
330 | }
|
---|
331 |
|
---|
332 | # Users must be in the <collection>-maintainer group to perform this action
|
---|
333 | &authenticate_user($gsdl_cgi, $username, "$collection-maintainer");
|
---|
334 |
|
---|
335 | my $gsdlhome = $ENV{'GSDLHOME'};
|
---|
336 | my $collect_directory = &util::filename_cat($gsdlhome, "collect");
|
---|
337 | $gsdl_cgi->checked_chdir($collect_directory);
|
---|
338 |
|
---|
339 | # Check that the collection archives exist
|
---|
340 | if (!-d &util::filename_cat($collection, "archives")) {
|
---|
341 | $gsdl_cgi->generate_error("Collection archives do not exist.");
|
---|
342 | }
|
---|
343 |
|
---|
344 | # Make sure the collection isn't locked by someone else
|
---|
345 | &lock_collection($gsdl_cgi, $collection, $username);
|
---|
346 |
|
---|
347 | # Zip up the collection archives
|
---|
348 | my $java = $gsdl_cgi->get_java_path();
|
---|
349 | my $java_classpath = &util::filename_cat($gsdlhome, "bin", "java", "GLIServer.jar");
|
---|
350 | my $zip_file_path = &util::filename_cat($collect_directory, $username . "-" . $collection . "-archives.zip");
|
---|
351 | my $java_args = "\"$zip_file_path\" \"$collect_directory\" \"$collection\"";
|
---|
352 | my $java_command = "$java -classpath \"$java_classpath\" org.greenstone.gatherer.remote.ZipCollectionArchives $java_args";
|
---|
353 |
|
---|
354 | my $java_output = `$java_command`;
|
---|
355 | my $java_status = $?;
|
---|
356 | if ($java_status > 0) {
|
---|
357 | $gsdl_cgi->generate_error("Java failed: $java_command\n--\n$java_output\nExit status: " . ($java_status / 256) . "\n" . $gsdl_cgi->check_java_home());
|
---|
358 | }
|
---|
359 |
|
---|
360 | # Check that the zip file was created successfully
|
---|
361 | if (!-e $zip_file_path || -z $zip_file_path) {
|
---|
362 | $gsdl_cgi->generate_error("Collection archives zip file $zip_file_path could not be created.");
|
---|
363 | }
|
---|
364 |
|
---|
365 | &put_file($gsdl_cgi, $zip_file_path, "application/zip");
|
---|
366 | unlink("$zip_file_path") unless $debugging_enabled;
|
---|
367 | }
|
---|
368 |
|
---|
369 |
|
---|
370 | # Collection locking unnecessary because this action isn't related to a particular collection
|
---|
371 | sub download_collection_configurations
|
---|
372 | {
|
---|
373 | my ($gsdl_cgi, $username) = @_;
|
---|
374 |
|
---|
375 | # Users must be in the remote-collection-builder group to perform this action
|
---|
376 | &authenticate_user($gsdl_cgi, $username, "remote-collection-builder");
|
---|
377 |
|
---|
378 | my $gsdlhome = $ENV{'GSDLHOME'};
|
---|
379 | my $collect_directory = &util::filename_cat($gsdlhome, "collect");
|
---|
380 | $gsdl_cgi->checked_chdir($collect_directory);
|
---|
381 |
|
---|
382 | # Zip up the collection configurations
|
---|
383 | my $java = $gsdl_cgi->get_java_path();
|
---|
384 | my $java_classpath = &util::filename_cat($gsdlhome, "bin", "java", "GLIServer.jar");
|
---|
385 | my $zip_file_path = &util::filename_cat($collect_directory, $username . "-" . "collection-configurations.zip");
|
---|
386 | my $java_args = "\"$zip_file_path\" \"$collect_directory\"";
|
---|
387 | my $java_command = "$java -classpath \"$java_classpath\" org.greenstone.gatherer.remote.ZipCollectionConfigurations $java_args";
|
---|
388 |
|
---|
389 | my $java_output = `$java_command`;
|
---|
390 | my $java_status = $?;
|
---|
391 | if ($java_status > 0) {
|
---|
392 | $gsdl_cgi->generate_error("Java failed: $java_command\n--\n$java_output\nExit status: " . ($java_status / 256) . "\n" . $gsdl_cgi->check_java_home());
|
---|
393 | }
|
---|
394 |
|
---|
395 | # Check that the zip file was created successfully
|
---|
396 | if (!-e $zip_file_path || -z $zip_file_path) {
|
---|
397 | $gsdl_cgi->generate_error("Collection configurations zip file $zip_file_path could not be created.");
|
---|
398 | }
|
---|
399 |
|
---|
400 | &put_file($gsdl_cgi, $zip_file_path, "application/zip");
|
---|
401 | unlink("$zip_file_path") unless $debugging_enabled;
|
---|
402 | }
|
---|
403 |
|
---|
404 |
|
---|
405 | sub download_collection_file
|
---|
406 | {
|
---|
407 | my ($gsdl_cgi, $username) = @_;
|
---|
408 |
|
---|
409 | my $collection = $gsdl_cgi->clean_param("c");
|
---|
410 | if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
|
---|
411 | $gsdl_cgi->generate_error("No collection specified.");
|
---|
412 | }
|
---|
413 | my $file = $gsdl_cgi->clean_param("file");
|
---|
414 | if ((!defined $file) || ($file =~ m/^\s*$/)) {
|
---|
415 | $gsdl_cgi->generate_error("No file specified.");
|
---|
416 | }
|
---|
417 | $file =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
|
---|
418 |
|
---|
419 | # Make sure we don't try to download anything outside the collection
|
---|
420 | if ($file =~ /\.\./) {
|
---|
421 | $gsdl_cgi->generate_error("Illegal file specified.");
|
---|
422 | }
|
---|
423 |
|
---|
424 | # Users must be in the <collection>-maintainer group to perform this action
|
---|
425 | &authenticate_user($gsdl_cgi, $username, "$collection-maintainer");
|
---|
426 |
|
---|
427 | my $gsdlhome = $ENV{'GSDLHOME'};
|
---|
428 | my $collection_directory = &util::filename_cat($gsdlhome, "collect", $collection);
|
---|
429 | $gsdl_cgi->checked_chdir($collection_directory);
|
---|
430 |
|
---|
431 | # Check that the collection file exists
|
---|
432 | if (!-e $file) {
|
---|
433 | $gsdl_cgi->generate_error("Collection file $file does not exist.");
|
---|
434 | }
|
---|
435 |
|
---|
436 | # Make sure the collection isn't locked by someone else
|
---|
437 | &lock_collection($gsdl_cgi, $collection, $username);
|
---|
438 |
|
---|
439 | # Zip up the collection file
|
---|
440 | my $java = $gsdl_cgi->get_java_path();
|
---|
441 | my $java_classpath = &util::filename_cat($gsdlhome, "bin", "java", "GLIServer.jar");
|
---|
442 | my $zip_file_path = &util::filename_cat($collection_directory, $username . "-" . $collection . "-file.zip");
|
---|
443 | my $java_args = "\"$zip_file_path\" \"$collection_directory\" \"$file\"";
|
---|
444 | my $java_command = "$java -classpath \"$java_classpath\" org.greenstone.gatherer.remote.ZipFiles $java_args";
|
---|
445 |
|
---|
446 | my $java_output = `$java_command`;
|
---|
447 | my $java_status = $?;
|
---|
448 | if ($java_status > 0) {
|
---|
449 | $gsdl_cgi->generate_error("Java failed: $java_command\n--\n$java_output\nExit status: " . ($java_status / 256) . "\n" . $gsdl_cgi->check_java_home());
|
---|
450 | }
|
---|
451 |
|
---|
452 | # Check that the zip file was created successfully
|
---|
453 | if (!-e $zip_file_path || -z $zip_file_path) {
|
---|
454 | $gsdl_cgi->generate_error("Collection archives zip file $zip_file_path could not be created.");
|
---|
455 | }
|
---|
456 |
|
---|
457 | &put_file($gsdl_cgi, $zip_file_path, "application/zip");
|
---|
458 | unlink("$zip_file_path") unless $debugging_enabled;
|
---|
459 | }
|
---|
460 |
|
---|
461 |
|
---|
462 | # Collection locking unnecessary because this action isn't related to a particular collection
|
---|
463 | sub get_script_options
|
---|
464 | {
|
---|
465 | my ($gsdl_cgi, $username) = @_;
|
---|
466 |
|
---|
467 | my $script = $gsdl_cgi->clean_param("script");
|
---|
468 | if ((!defined $script) || ($script =~ m/^\s*$/)) {
|
---|
469 | $gsdl_cgi->generate_error("No script specified.");
|
---|
470 | }
|
---|
471 | $gsdl_cgi->delete("script");
|
---|
472 |
|
---|
473 | # Users must be in the remote-collection-builder group to perform this action
|
---|
474 | &authenticate_user($gsdl_cgi, $username, "remote-collection-builder");
|
---|
475 |
|
---|
476 | my $perl_args = "";
|
---|
477 | if ($script eq "classinfo.pl") {
|
---|
478 | $perl_args = $gsdl_cgi->clean_param("classifier") || "";
|
---|
479 | $gsdl_cgi->delete("classifier");
|
---|
480 | }
|
---|
481 | if ($script eq "pluginfo.pl") {
|
---|
482 | $perl_args = $gsdl_cgi->clean_param("plugin") || "";
|
---|
483 | $gsdl_cgi->delete("plugin");
|
---|
484 | }
|
---|
485 |
|
---|
486 | foreach my $cgi_arg_name ($gsdl_cgi->param) {
|
---|
487 | my $cgi_arg_value = $gsdl_cgi->safe_val($gsdl_cgi->clean_param($cgi_arg_name));
|
---|
488 | if ($cgi_arg_value eq "") {
|
---|
489 | $perl_args = "-$cgi_arg_name " . $perl_args;
|
---|
490 | }
|
---|
491 | else {
|
---|
492 | $perl_args = "-$cgi_arg_name \"$cgi_arg_value\" " . $perl_args;
|
---|
493 | }
|
---|
494 | }
|
---|
495 |
|
---|
496 | my $perl_command = "perl -S $script $perl_args 2>&1";
|
---|
497 | my $perl_output = `$perl_command`;
|
---|
498 | my $perl_status = $?;
|
---|
499 | if ($perl_status > 0) {
|
---|
500 | $gsdl_cgi->generate_error("Perl failed: $perl_command\n--\n$perl_output\nExit status: " . ($perl_status / 256));
|
---|
501 | }
|
---|
502 |
|
---|
503 | print STDOUT "Content-type:text/plain\n\n";
|
---|
504 | print STDOUT $perl_output;
|
---|
505 | }
|
---|
506 |
|
---|
507 |
|
---|
508 | sub move_collection_file
|
---|
509 | {
|
---|
510 | my ($gsdl_cgi, $username) = @_;
|
---|
511 |
|
---|
512 | my $collection = $gsdl_cgi->clean_param("c");
|
---|
513 | if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
|
---|
514 | $gsdl_cgi->generate_error("No collection specified.");
|
---|
515 | }
|
---|
516 | my $source_file = $gsdl_cgi->clean_param("source");
|
---|
517 | if ((!defined $source_file) || ($source_file =~ m/^\s*$/)) {
|
---|
518 | $gsdl_cgi->generate_error("No source file specified.");
|
---|
519 | }
|
---|
520 | $source_file =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
|
---|
521 | my $target_file = $gsdl_cgi->clean_param("target");
|
---|
522 | if ((!defined $target_file) || ($target_file =~ m/^\s*$/)) {
|
---|
523 | $gsdl_cgi->generate_error("No target file specified.");
|
---|
524 | }
|
---|
525 | $target_file =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
|
---|
526 |
|
---|
527 | # Make sure we don't try to move anything outside the collection
|
---|
528 | if ($source_file =~ /\.\./ || $target_file =~ /\.\./) {
|
---|
529 | $gsdl_cgi->generate_error("Illegal file specified.");
|
---|
530 | }
|
---|
531 |
|
---|
532 | # Users must be in the <collection>-maintainer group to perform this action
|
---|
533 | &authenticate_user($gsdl_cgi, $username, "$collection-maintainer");
|
---|
534 |
|
---|
535 | my $gsdlhome = $ENV{'GSDLHOME'};
|
---|
536 | my $collection_directory = &util::filename_cat($gsdlhome, "collect", $collection);
|
---|
537 | $gsdl_cgi->checked_chdir($collection_directory);
|
---|
538 |
|
---|
539 | # Check that the collection source file exists
|
---|
540 | if (!-e $source_file) {
|
---|
541 | $gsdl_cgi->generate_error("Collection file $source_file does not exist.");
|
---|
542 | }
|
---|
543 |
|
---|
544 | # Make sure the collection isn't locked by someone else
|
---|
545 | &lock_collection($gsdl_cgi, $collection, $username);
|
---|
546 |
|
---|
547 | &util::mv($source_file, $target_file);
|
---|
548 |
|
---|
549 | # Check that the collection source file was moved
|
---|
550 | if (-e $source_file || !-e $target_file) {
|
---|
551 | $gsdl_cgi->generate_error("Could not move collection file $source_file to $target_file.");
|
---|
552 | }
|
---|
553 |
|
---|
554 | $gsdl_cgi->generate_ok_message("Collection file $source_file moved to $target_file successfully.");
|
---|
555 | }
|
---|
556 |
|
---|
557 |
|
---|
558 | sub new_collection_directory
|
---|
559 | {
|
---|
560 | my ($gsdl_cgi, $username) = @_;
|
---|
561 |
|
---|
562 | my $collection = $gsdl_cgi->clean_param("c");
|
---|
563 | if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
|
---|
564 | $gsdl_cgi->generate_error("No collection specified.");
|
---|
565 | }
|
---|
566 | my $directory = $gsdl_cgi->clean_param("directory");
|
---|
567 | if ((!defined $directory) || ($directory =~ m/^\s*$/)) {
|
---|
568 | $gsdl_cgi->generate_error("No directory specified.");
|
---|
569 | }
|
---|
570 | $directory =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
|
---|
571 |
|
---|
572 | # Make sure we don't try to create anything outside the collection
|
---|
573 | if ($directory =~ /\.\./) {
|
---|
574 | $gsdl_cgi->generate_error("Illegal directory specified.");
|
---|
575 | }
|
---|
576 |
|
---|
577 | # Users must be in the <collection>-maintainer group to perform this action
|
---|
578 | &authenticate_user($gsdl_cgi, $username, "$collection-maintainer");
|
---|
579 |
|
---|
580 | my $gsdlhome = $ENV{'GSDLHOME'};
|
---|
581 | my $collection_directory = &util::filename_cat($gsdlhome, "collect", $collection);
|
---|
582 | $gsdl_cgi->checked_chdir($collection_directory);
|
---|
583 |
|
---|
584 | # Check that the collection directory doesn't already exist
|
---|
585 | if (-d $directory) {
|
---|
586 | $gsdl_cgi->generate_error("Collection directory $directory already exists.");
|
---|
587 | }
|
---|
588 |
|
---|
589 | # Make sure the collection isn't locked by someone else
|
---|
590 | &lock_collection($gsdl_cgi, $collection, $username);
|
---|
591 |
|
---|
592 | &util::mk_dir($directory);
|
---|
593 |
|
---|
594 | # Check that the collection directory was created
|
---|
595 | if (!-d $directory) {
|
---|
596 | $gsdl_cgi->generate_error("Could not create collection directory $directory.");
|
---|
597 | }
|
---|
598 |
|
---|
599 | $gsdl_cgi->generate_ok_message("Collection directory $directory created successfully.");
|
---|
600 | }
|
---|
601 |
|
---|
602 |
|
---|
603 | sub run_script
|
---|
604 | {
|
---|
605 | my ($gsdl_cgi, $username) = @_;
|
---|
606 |
|
---|
607 | my $script = $gsdl_cgi->clean_param("script");
|
---|
608 | if ((!defined $script) || ($script =~ m/^\s*$/)) {
|
---|
609 | $gsdl_cgi->generate_error("No script specified.");
|
---|
610 | }
|
---|
611 | $gsdl_cgi->delete("script");
|
---|
612 | my $collection = $gsdl_cgi->clean_param("c");
|
---|
613 | if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
|
---|
614 | $gsdl_cgi->generate_error("No collection specified.");
|
---|
615 | }
|
---|
616 | $gsdl_cgi->delete("c");
|
---|
617 |
|
---|
618 | # Users must be in the <collection>-maintainer group to perform this action
|
---|
619 | &authenticate_user($gsdl_cgi, $username, "$collection-maintainer");
|
---|
620 |
|
---|
621 | # Make sure the collection isn't locked by someone else (unless we're running mkcol.pl, of course)
|
---|
622 | &lock_collection($gsdl_cgi, $collection, $username) unless ($script eq "mkcol.pl");
|
---|
623 |
|
---|
624 | my $perl_args = $collection;
|
---|
625 | foreach my $cgi_arg_name ($gsdl_cgi->param) {
|
---|
626 | my $cgi_arg_value = $gsdl_cgi->safe_val($gsdl_cgi->clean_param($cgi_arg_name));
|
---|
627 | if ($cgi_arg_value eq "") {
|
---|
628 | $perl_args = "-$cgi_arg_name " . $perl_args;
|
---|
629 | }
|
---|
630 | else {
|
---|
631 | $perl_args = "-$cgi_arg_name \"$cgi_arg_value\" " . $perl_args;
|
---|
632 | }
|
---|
633 | }
|
---|
634 |
|
---|
635 | my $perl_command = "perl -S $script $perl_args 2>&1";
|
---|
636 | if (!open(PIN, "$perl_command |")) {
|
---|
637 | $gsdl_cgi->generate_error("Unable to execute command: $perl_command");
|
---|
638 | }
|
---|
639 |
|
---|
640 | print STDOUT "Content-type:text/plain\n\n";
|
---|
641 | while (defined (my $perl_output_line = <PIN>)) {
|
---|
642 | print STDOUT $perl_output_line;
|
---|
643 | }
|
---|
644 | close(PIN);
|
---|
645 |
|
---|
646 | my $perl_status = $?;
|
---|
647 | if ($perl_status > 0) {
|
---|
648 | $gsdl_cgi->generate_error("Perl failed: $perl_command\n--\nExit status: " . ($perl_status / 256));
|
---|
649 | }
|
---|
650 | elsif ($mail_enabled) {
|
---|
651 | if ($script eq "buildcol.pl") {
|
---|
652 | &send_mail($gsdl_cgi, "Remote Greenstone building event", "Build of collection '$collection' complete.");
|
---|
653 | }
|
---|
654 | }
|
---|
655 | }
|
---|
656 |
|
---|
657 |
|
---|
658 | sub upload_collection_file
|
---|
659 | {
|
---|
660 | my ($gsdl_cgi, $username) = @_;
|
---|
661 |
|
---|
662 | my $collection = $gsdl_cgi->clean_param("c");
|
---|
663 | if ((!defined $collection) || ($collection =~ m/^\s*$/)) {
|
---|
664 | $gsdl_cgi->generate_error("No collection specified.");
|
---|
665 | }
|
---|
666 | my $file = $gsdl_cgi->clean_param("file");
|
---|
667 | if ((!defined $file) || ($file =~ m/^\s*$/)) {
|
---|
668 | $gsdl_cgi->generate_error("No file specified.");
|
---|
669 | }
|
---|
670 | my $directory = $gsdl_cgi->clean_param("directory") || "";
|
---|
671 | $directory =~ s/\|/&util::get_dirsep()/eg; # Convert the '|' characters into whatever is right for this OS
|
---|
672 | my $zip = $gsdl_cgi->clean_param("zip");
|
---|
673 |
|
---|
674 | # Make sure we don't try to upload anything outside the collection
|
---|
675 | if ($file =~ /\.\./) {
|
---|
676 | $gsdl_cgi->generate_error("Illegal file specified.");
|
---|
677 | }
|
---|
678 | if ($directory =~ /\.\./) {
|
---|
679 | $gsdl_cgi->generate_error("Illegal directory specified.");
|
---|
680 | }
|
---|
681 |
|
---|
682 | # Users must be in the <collection>-maintainer group to perform this action
|
---|
683 | &authenticate_user($gsdl_cgi, $username, "$collection-maintainer");
|
---|
684 |
|
---|
685 | my $gsdlhome = $ENV{'GSDLHOME'};
|
---|
686 | my $collection_directory = &util::filename_cat($gsdlhome, "collect", $collection);
|
---|
687 | $gsdl_cgi->checked_chdir($collection_directory);
|
---|
688 |
|
---|
689 | # Make sure the collection isn't locked by someone else
|
---|
690 | &lock_collection($gsdl_cgi, $collection, $username);
|
---|
691 |
|
---|
692 | my $directory_path = &util::filename_cat($collection_directory, $directory);
|
---|
693 | &util::mk_dir($directory_path);
|
---|
694 | if (!-e $directory_path) {
|
---|
695 | $gsdl_cgi->generate_error("Could not create directory $directory_path.");
|
---|
696 | }
|
---|
697 |
|
---|
698 | my $file_path = &util::filename_cat($directory_path, $username . "-" . $file);
|
---|
699 | if (!open(FOUT, ">$file_path")) {
|
---|
700 | $gsdl_cgi->generate_error("Unable to write file $file_path");
|
---|
701 | }
|
---|
702 |
|
---|
703 | # Read the uploaded data and write it out to file
|
---|
704 | my $buf;
|
---|
705 | my $num_bytes = 0;
|
---|
706 | binmode(FOUT);
|
---|
707 | while (read(STDIN, $buf, 1024) > 0) {
|
---|
708 | print FOUT $buf;
|
---|
709 | $num_bytes += length($buf);
|
---|
710 | }
|
---|
711 | close(FOUT);
|
---|
712 |
|
---|
713 | # If we have downloaded a zip file, unzip it
|
---|
714 | if (defined $zip) {
|
---|
715 | my $java = $gsdl_cgi->get_java_path();
|
---|
716 | my $java_classpath = &util::filename_cat($gsdlhome, "bin", "java", "GLIServer.jar");
|
---|
717 | my $java_args = "\"$file_path\" \"$directory_path\"";
|
---|
718 | my $java_command = "$java -classpath \"$java_classpath\" org.greenstone.gatherer.util.Unzip $java_args";
|
---|
719 |
|
---|
720 | my $java_output = `$java_command`;
|
---|
721 | my $java_status = $?;
|
---|
722 |
|
---|
723 | # Remove the zip file once we have unzipped it, since it is an intermediate file only
|
---|
724 | unlink("$file_path");
|
---|
725 |
|
---|
726 | if ($java_status > 0) {
|
---|
727 | $gsdl_cgi->generate_error("Java failed: $java_command\n--\n$java_output\nExit status: " . ($java_status / 256) . "\n" . $gsdl_cgi->check_java_home());
|
---|
728 | }
|
---|
729 | }
|
---|
730 |
|
---|
731 | $gsdl_cgi->generate_ok_message("Collection file $file uploaded successfully.");
|
---|
732 | }
|
---|
733 |
|
---|
734 |
|
---|
735 | sub put_file
|
---|
736 | {
|
---|
737 | my $gsdl_cgi = shift(@_);
|
---|
738 | my $file_path = shift(@_);
|
---|
739 | my $content_type = shift(@_);
|
---|
740 |
|
---|
741 | if (open(PIN, "<$file_path")) {
|
---|
742 | print STDOUT "Content-type:$content_type\n\n";
|
---|
743 |
|
---|
744 | my $buf;
|
---|
745 | my $num_bytes = 0;
|
---|
746 | binmode(PIN);
|
---|
747 | while (read(PIN, $buf, 1024) > 0) {
|
---|
748 | print STDOUT $buf;
|
---|
749 | $num_bytes += length($buf);
|
---|
750 | }
|
---|
751 |
|
---|
752 | close(PIN);
|
---|
753 | }
|
---|
754 | else {
|
---|
755 | $gsdl_cgi->generate_error("Unable to read file $file_path\n $!");
|
---|
756 | }
|
---|
757 | }
|
---|
758 |
|
---|
759 |
|
---|
760 | sub send_mail
|
---|
761 | {
|
---|
762 | my $gsdl_cgi = shift(@_);
|
---|
763 | my $mail_subject = shift(@_);
|
---|
764 | my $mail_content = shift(@_);
|
---|
765 |
|
---|
766 | my $sendmail_command = "perl -S sendmail.pl";
|
---|
767 | $sendmail_command .= " -to \"" . $mail_to_address . "\"";
|
---|
768 | $sendmail_command .= " -from \"" . $mail_from_address . "\"";
|
---|
769 | $sendmail_command .= " -smtp \"" . $mail_smtp_server . "\"";
|
---|
770 | $sendmail_command .= " -subject \"" . $mail_subject . "\"";
|
---|
771 |
|
---|
772 | if (!open(POUT, "| $sendmail_command")) {
|
---|
773 | $gsdl_cgi->generate_error("Unable to execute command: $sendmail_command");
|
---|
774 | }
|
---|
775 | print POUT $mail_content . "\n";
|
---|
776 | close(POUT);
|
---|
777 | }
|
---|
778 |
|
---|
779 |
|
---|
780 | &main();
|
---|