msk_0984 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I am trying to send a mail and also trying to append the data to a File but i am getting a problem when i saying
Permission is Denied
i have changed the guestbook.txt permission to 777 and also tried but i am getting this prob i hav also tried to change teh user and th e group but that did not work. plz help me out .

#!/usr/bin/perl -wT use CGI qw(:standard); use CGI::Carp qw(warningsToBrowser fatalsToBrowser); use strict; print header; print start_html("Results"); # first print the mail message... $ENV{PATH} = "/usr/sbin"; open (MAIL, "|/usr/sbin/sendmail -oi -t -odq") or &dienice("Can't fork for sendmail: $!\n"); print MAIL "To: recipient\@cgi101.com\n"; print MAIL "From: nobody\@cgi101.com\n"; print MAIL "Subject: Form Data\n\n"; foreach my $p (param()) { print MAIL "$p = ", param($p), "\n"; } close(MAIL); # now write (append) to the file open(OUT, ">>guestbook.txt") or &dienice("Couldn't open output file: $ +!"); foreach my $p (param()) { print OUT param($p), "|"; } print OUT "\n"; close(OUT); print <<EndHTML; <h2>Thank You</h2> <p>Thank you for writing!</p> <p>Return to our <a href="index.html">home page</a>.</p> EndHTML print end_html; sub dienice { my($errmsg) = @_; print "<h2>Error</h2>\n"; print "<p>$errmsg</p>\n"; print end_html; exit; }

Replies are listed 'Best First'.
Re: Writing to the File
by shmem (Chancellor) on Jun 28, 2006 at 13:43 UTC
    Permission denied must not relate only to the directory where you want to write files, but the entire path.

    Check whether the directories which are components of the path are executable for the given UID/GID, for the process which writes has to chdir to that directory (i.e. execute all directories along the path).

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: Writing to the File
by blue_cowdawg (Monsignor) on Jun 28, 2006 at 13:59 UTC
        I am trying to send a mail and also trying to append the data to a File but i am getting a problem when i saying Permission is Denied

    OK, permissioning in a CGI script sometimes can be tricky. First off: what userid is the CGI running as? I know a lot of sites that run their webservers as "nobody" which doesn't have permission to write files to most places on the system. A nice way to keep the script kiddies honest.

    Next: who owns the directory you are trying to write the file to? If the file was already written once, who owns it now and answer the "First off" question.

    Lastly and possibly the first question I should have asked what platform what web server, in other words what is the environment? Based on your original post it looks like some flavor of *nix. Are there Access Control Lists involved?


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
      Hi Monks

      Firstly , vry much Thanks for ur response actually after i posted my question we got a Network problem and i was not able work and cannot reply u back So, now i was seeing th response then i tried it by changing the group and the user name there it was working.

      Anywway thank you very much for ur feedback and you r help thst wat the main motto of our Group to help out each other and acquire knowledge.

      Sushil

Re: Writing to the File
by jesuashok (Curate) on Jun 28, 2006 at 13:05 UTC
    Hi

    when you specify the file name in open ( Better for CGI programs ), Please specify it with the absolute path. It is always a good practice.

    Example :

    open ( OUT , ">>/home/user/cgi/mail_file/guestbook.txt" ) or die "cann +ot append to <file> :$!:";
    and make sure the user has permission to access the directory.

    "Keep pouring your ideas"
      Yes

      I have even tried that one also but it did not work
      i have given teh full path name

        Hi

        did you check whether the whole directory is having the permission to write into that file ?

        You have to check whether the cgi user has the permission to create a file or there not ?

        Hope this helps

        "Keep pouring your ideas"
Re: Writing to the File
by Moron (Curate) on Jun 28, 2006 at 15:55 UTC
    It is a huge subject, but I would start with the apache documentation in relation to such topics as httpd.conf, aliases and virtual hosts. With apache, each filetype will reference different relative paths, by default, subdirectories of the default directory configured for your webserver - the actual location your file refers should be consciously configured for the webserver and then the permission for the relevant directory controlled.

    -M

    Free your mind