Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Directory creation failing

by Anonymous Monk
on Feb 27, 2004 at 09:54 UTC ( #332218=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

This my sound like a dumb question, so you dont have to answer it. But im trying to create a directory outside of the cgi bin on a web server, but no matter what I put in as the path in the mkdir, nothing gets created. what am i doing wrong? thanks.

20040227 Edit by BazB: Changed title from 'mkdir'

Comment on Directory creation failing
Re: Directory creation failing
by Taulmarill (Deacon) on Feb 27, 2004 at 10:00 UTC
    maybe you just have not the permission to create dirs outside your cgi-bin?
    ask you root/sysadmin about this.
Re: Directory creation failing
by BrowserUk (Pope) on Feb 27, 2004 at 10:06 UTC

    Are you checking the return code? If the call is failing, then $! should give some error text to identify the cause.

    The likelyhood is that when your script is run via the webserver, it is running under a user account (nobody or ISUR_your_machine or similar) that is specifically disabled from creating files outside of cgi-bin.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    Timing (and a little luck) are everything!
Re: Directory creation failing
by BazB (Priest) on Feb 27, 2004 at 10:12 UTC

    Try mkdir($dir) or die "Unable to create directory $dir: $!"; (obviously assigning $dir first).
    $! should give you a clue.

    If you're trying to create a hierarchy/tree of directories, you should look at File::Path's mkpath call.
    File::Path is a standard module.

    If you're not doing so already, Use strict warnings and diagnostics or die.


    If the information in this post is inaccurate, or just plain wrong, don't just downvote - please post explaining what's wrong.
    That way everyone learns.

Re: Directory creation failing
by maa (Pilgrim) on Feb 27, 2004 at 10:17 UTC
    the typical username of the httpd daemon is nobody... and nobody has basically no rights!

      nobody has the same rights as any other user id (able to open or write files to which it has access, etc.). The difference is that on a well configured server what it can access is very limited.</pedant>

Re: Directory creation failing
by thospel (Hermit) on Feb 27, 2004 at 17:27 UTC
    Well, first of all this indicates you are probably not checking the returncode of your mkdir properly, otherwise you would at least see the (superficial) reason. The standard idiom (not just for mkdir but for almost all systemcalls) is:

    mkdir($dir) || die "Could not create $dir: $!"

    In case you don't have access to the web server logs, you might want to look at CGI::Carp, especially fatalsToBrowser. To give users of your CGI running into such an error a more pleasant experience, it's also often a good idea to wrap the CGI processing in an eval and write the real error to the log and present the user with some nice failure message.

    What you will see is most likely "permission denied", and the cause is that the webserver is running as some user (for example "nobody"), the CGI programs get started as that same user, and he doesn't have the permission to create directories at the place you are trying to use.

    So from here on I'll assume you are running on UNIX and this is the actual problem. If not, ignore the rest

    If you just need a once per run temporary directory, you can probably create it in /tmp, preferrably using File::Temp since securily creating things in /tmp can be a bit tricky.

    If you need some permanent working directory and want it in your homedirectory /home/user, you need to get a bit more tricky. Here is one way:

    - As "user", create a directory /home/user/a and chmod it to permissions 777 - Make a CGI script that when run creates a subdirectory /home/user/a/cgi_work with permissions 777 again - As "user", move that directory from /home/user/a/cgi_work to /home/user/cgi_work - As "user" remove the now empty directory /home/user/a (well, since it existed without protections for a bit of time, someone else can have snuck stuff into it in the mean time. Scream loudly to the admin and/or that person in that case) - Make yet another CGI script that changes the permissions of /home/user/cgi_work to something sane again (probably 755). - Make sure /home/user/cgi_work is empty (otherwise repeat the scream ploy)
    Now you will have a directory /home/user/cgi_work with proper permissions owned by the webserver user that you can use as persistent storage.

    If however other people can also do CGI on this server, you should keep in mind that their programs will run as the webserver user too, so they will have full rights into this directory too.

    In that scenario I consider that server badly set up. Each person should be running as a different user really, using something like suexec for apache.

    My personal opinion is actually that not using something like suexec is always the wrong thing to do. CGI is one of the most likely vectors that allows a hacker access to your system, and if you run CGI as the webserver user, he directly gets control of the server (think of the fun the hacker can have with ptrace for example).

Re: Directory creation failing
by Hutch (Acolyte) on Feb 28, 2004 at 17:39 UTC

    Hey,

    Obviously this won't work if you absolutely need to create a directory in the main path outside of the cgi-bin, but if you're just looking to create one or a series of directories that are accessable by Perl or by HTML, you may just decide to create a directory with all permissions in the main path, then write to that directory instead of the main path in Perl. That may help you to avoid your permissions issue.

    You should also probably put a condition on the directory creation code:

    or die "Error with directory creation: $!\n";
    This may help you isolate the problem. Hope I was helpful.

    - Hutch

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://332218]
Approved by BazB
Front-paged by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2014-08-23 03:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (171 votes), past polls