hmbscully has asked for the wisdom of the Perl Monks concerning the following question:
I have a simple script being called from a webpage that grabs a couple fields of information and should put it into a .csv file.
Here's the code:
#!/usr/bin/perl # consent.cgi use strict; use warnings; use CGI qw(:standard); use CGI::Carp qw(fatalsToBrowser); use Fcntl ':flock'; use POSIX; #VARIABLES my $sys_time = POSIX::strftime("%D %T", localtime); my $data_path = "/home/account/cgi-bin/asewk"; my $jump_url = "http://mysite.com/ase-workkeys/consent-confirm.html"; my ($which_form, $consent, $user_name, $company, $user_email, @flatfie +lds, $flatfile); #SCRIPT &setValues; &writeCSVfile($which_form, $flatfile); print redirect($jump_url); exit; #SUBROUTINES sub setValues { #get values from form $which_form = param('which_form'); $consent = param('consent'); $user_name = param('name'); $company = param('company'); $user_email = param('email'); #build line for csv flatfile @flatfields = ("$sys_time", "$consent", "$user_name", "$company", +"$user_email"); $flatfile = join(",", map { local $_ = $_; s/"/""/g; qq("$_") } @f +latfields); } sub writeCSVfile { my($filename, $linename) = @_; #name subroutine variables my $log_file = "$data_path/$filename\.csv"; open (LO, ">>$log_file") or die "Could not open $log_file: $!"; flock LO, LOCK_EX; #exclusive lock print LO "$linename\n"; close LO; }
perl -c consent.cgi comes back OK.
This is a new system for me, running a 5.8 flavor of perl (I was previously on 5.004 *shudder*), and initially I was getting this error
Could not open /home/account/cgi-bin/asewk/employer.csv: Permission denied at /home/account/cgi-bin/asewk/consent.cgi line 42.
I chmod'ed all I knew to chmod and then got in touch with my network guys. His response was:
The problem appears to have been caused due to the site running as the 'apache' user, which didn't have write permissions. I modified Apache to run the site as the 'account' user which does have the proper credentials. The CGI runs now, but gives an Internal Server Error, and the logs don't show much. Can you add some debugging to the code to see if the problem is related to something with the server, or if it's code related?
The error logs just show Premature end of script headers: consent.cgi, referer: http://mysite.com/ase-workkeys/employerconsent.html.
Such a non-informative message.
My questions are:
Thank you!
UPDATE:
After looking in the suexec.log and giving that error to the network admin with my server hosting, he came back with this "solution."
It seems that the issue is that suexec REQUIRES that all executing CGI's be run from (or underneath) /var/www. This means that your current directory organization, eg /home/<something>, etc would have to be moved to something like /var/www/sites/<something> (as an example).
So it was a permissions thing, and not my perl code (though it can admittedly be improved upon.)
|
|---|