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