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

On a unix box using perl cgi. How would I switch user on server from a Perl .cgi script? I have another server login via generic login to create dir/files, and with my cgi script I need to 'su' to the generic login and chmod on all dir/files they created. I have tried to Telnet from my cgi also , but cannot do it. I do not have control of generic login admin and admin does not want to make any general changes for the generic login. I can 'su' and chmod on dir/files via command line. Thank You Henry

Replies are listed 'Best First'.
Re: switch user
by salva (Canon) on Apr 27, 2005 at 15:01 UTC
    I would say, use sudo, but if your admin doesn't want to collaborate...

    One solution could be to use Expect module to pass the password to su and then launch any program you want.

Re: switch user
by Kevad (Scribe) on Apr 27, 2005 at 16:08 UTC
    Have you looked through the sudo cpan module? Obviously, sudo access needs to be set up for the particular user the perl script is run as. Assuming that, it should do exactly what you are looking for.
Re: switch user
by jhourcle (Prior) on Apr 28, 2005 at 01:42 UTC

    From the sounds of things, the issue is the permissions that the CGI runs as. If you can get the administrator to install a progrqam for you, I would suggest that you either get suEXEC (if you're running Apache), or CGIwrap (if you're running anything else).

    Basically, these programs run the CGI as a particular user, so that the files that are created as the script's user, and not the user that the webserver is running as.

    If the issue is just the permissions of the file, and not the ownership, then you can set a umask within your Perl script, or chmod files that have been created. (and, you won't need to involve your sysadmin)

    Other options include setting up a cron job to keep forcing the permissions of files in the directory in question (which is a hack...I don't suggest it, if any of the other suggestions will work, especially as your sysadmin may restrict cron usage), or some file systems may have ways to specify the ownership and permissions of files created within a directory. (eg, setfacl in Solaris)