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