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

I'm currently developing a CGI script that will allow clients to browse a directory tree of files and stuff that will be posted on a webserver. Users will be able to see the contents of the current directory and will be able to browse to other directories within the tree (with some bonus features like custom file descriptions). Of course I would like to prevent users from "accidentally" browsing into directories that they shouldn't have access to (like the directory trees of other clients). I think the server currently does user authentication by directory using .htaccess files. Not every directory has one of these files, subdirectories inherit the properties of the parent. I want to avoid creating more than one script per client, thus I think I need the script to handle user authentication for each request to browse a particular directory. Any suggestions for how to check that the user isn't wandering around my entire filesystem? (sorry no code yet, still in design phase) Thanks.

Replies are listed 'Best First'.
Re: Dir Browsing User Authentication
by belg4mit (Prior) on Aug 19, 2003 at 20:58 UTC
    Why not let the server do the heavy lifting? You oughn't need to do any authorization (not authentication) yourself. As for the script you can make things easier, assuming you're using Apache, if you set it up as a handler for the directory MIME type httpd/unix-directory.

    --
    I'm not belgian but I play one on TV.

Re: Dir Browsing User Authentication
by bean (Monk) on Aug 19, 2003 at 21:56 UTC
    You don't need Perl for this - look into Dynamically configured mass virtual hosting - here's a particularly relevant snippet:

    A homepages system using mod_rewrite

    <snip>
    RewriteEngine on RewriteMap lowercase int:tolower # allow CGIs to work RewriteCond %{REQUEST_URI} !^/cgi-bin/ # check the hostname is right so that the RewriteRule works RewriteCond ${lowercase:%{SERVER_NAME}} ^www\.[a-z-]+\.isp\.com$ # concatenate the virtual host name onto the start of the URI # the [C] means do the next rewrite on the result of this one RewriteRule ^(.+) ${lowercase:%{SERVER_NAME}}$1 [C] # now create the real file name RewriteRule ^www\.([a-z-]+)\.isp\.com/(.*) /home/$1/$2 # define the global CGI directory ScriptAlias /cgi-bin/ /www/std-cgi/
    This also changes the url to www.username.yourdomain.com, which your users would probably love...
    Update
    I just realized this doesn't solve the question of user authentication. <sigh> I'm tired. I need a vacation.

    For authentication, combine this with mod_auth (not sure if that can be done dynamically) or .htaccess (which you were already doing).
    Update 2 Another swing and a miss by bean! Strike two! While my suggestions are, of course, brilliant, I guess I didn't read the question very well - you want to use your perl script to show the users' directories. While the wisdom of doing this is debateable, I'll answer the question posed (perhaps you want to serve advertising on the directory listings, I don't know).

    If your users can access their directory trees directly, follow my previous advice - otherwise ignore it.

    .htaccess files won't help if the perl script is accessing the directories instead of apache. I'd suggest giving the user an encrypted cookie with her name in it (or a key the server can use to find the username) - then just check the name against the directory being accessed (I'm going to assume the username maps to the filesystem somehow). Be sure to collapse "/../" into "/" - and watch out for url encoded dots (you know how %20 is a space?). I think someone already suggested this...
    Update 3
    Ok, I thought of good reasons to do this in Perl - you want to provide filemanager-style access, right? So the user will be able to add/delete/move directories/files, upload, etc. In which case you will have to do some sort of cookie check against directory thing, and will have to deal with people trying to mess with the files of other people via "/../". I'm going to assume people will be able to access their files and therefore their directories directly (it would be sheer folly to try to reinvent the most basic function of Apache), so you'll have to do something like my previous suggestions as well if you want to keep them from seeing each other's files. I'm going to stop adding to this comment now - it's just near and dear to my heart, because I was once the head of the Orbita homepages system <sniff>. Good times... good times. Over and out.
Re: Dir Browsing User Authentication
by tcf22 (Priest) on Aug 19, 2003 at 20:53 UTC
    You could use a config file that has patterns that match directories to allow or deny. Like this:
    Allow=/home/you/openfolder Deny=/usr/.* Deny=/bin/.* Default=Allow
    Then just run the pattern against the current directory, if it matched and Allow pattern then show it, if it matches a deny pattern then show an error. If it doesn't match either, you could set up a default directive.

    Each user that uses the script would only need to setup one of these files or you could just keep track of their rights in a DB. You could authenticate, using a user/password and dropping a cookie.
      /me sets the query to /home/you/openfolder/../../../etc/passwd

      =)

      -Waswas
Re: Dir Browsing User Authentication
by MidLifeXis (Monsignor) on Aug 19, 2003 at 21:02 UTC

    You are probably better off using the .htaccess solution -- it is robust, time tested, and scrutinized. It is just far too easy to make mistakes when parsing directories. The .htaccess solution has already done the hard stuff for you. You just need to tell it what groups to allow in.

Re: Dir Browsing User Authentication
by ajdelore (Pilgrim) on Aug 19, 2003 at 21:23 UTC

    You can also configure Apache to have README files displayed as part of the directory listing, and to have custom icons and/or file descriptions for each file type.

    I agree with the others that this is a situation where you are better off using the existing platform, rather than re-inventing the wheel to run on top of it.

    </ajdelore>

Re: Dir Browsing User Authentication
by bear0053 (Hermit) on Aug 19, 2003 at 20:53 UTC
    You can set it up so that every time a user clicks on a directory it would call your script and check a cookie that is stored on the computer when they login. This cookie will contain the user login info and a session id or such. These values can be pulled and checked against the values stored in a DB. So in essence everytime a user attempts to navigate through the listing they relogin without knowing it. Now each directory should have permissions set for certain groups or users. So after the user's login info is verified you must verify that that specific user has access to the directory he is attempting to browse.