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

Hi all, I have difficulty getting a cgi script to start linux services (e.g. `/etc/init.d/ntpd start`). I'm using apache that's being run as daemon. I tried running with sudo but without success. What is the most appropriate way to start services via perl cgi script? I was thinking of using a perl script running in the background by root to pick up commands left by the cgi script and then executing them. But this seems like a silly way of doing things. Are there any suggestions? Thanks.
  • Comment on Perl CGI ability to start linux services

Replies are listed 'Best First'.
Re: Perl CGI ability to start linux services
by oko1 (Deacon) on Nov 29, 2008 at 03:09 UTC

    The "old-school" way of doing this - and this approach is still perfectly good, assuming you haven't done anything strange with the "mount" command for your current partition - is to create a compiled program that does just that one thing (i.e., start ntpd) and is set as 'chown root:www-data myprog; chmod 4750 myprog' - meaning that only users belonging to group 'www-data' (or the root user) could execute it, and that it was run as root (SUID). Then, that program is executed by the CGI program as appropriate. This is fairly secure, and works well.


    --
    "Language shapes the way we think, and determines what we can think about."
    -- B. L. Whorf
Re: Perl CGI ability to start linux services
by almut (Canon) on Nov 29, 2008 at 01:46 UTC
    I tried running with sudo but without success.

    What was the error?

    Have you configured sudo appropriately, granting the apache user the respective permissions?

      It returns me code 256. for example: my $val = `/etc/init.d/ntpd start`; The value of $val is 256.

        I don't see you using sudo anywhere... (that would be sudo /etc/init.d/ntpd start).

        To avoid having to mess with entering passwords, it's probably best to make use of the NOPASSWD: option, e.g. something like (in /etc/sudoers):

        apache localhost = NOPASSWD: /etc/init.d/ntpd

        (see man sudoers for the details)

        If an error occurred, its code will be found in $?, not $val. Moreover, the exit status will be found in the high byte:
        $? >> 8
        As an aside, if you are running this on a Red Hat derivative, this could be an selinux issue. See thread "Premature end of script headers more annoying than usual . . ." for suggestions on determining if selinux is the source of the problem.
Re: Perl CGI ability to start linux services
by Anonymous Monk on Nov 29, 2008 at 01:49 UTC
    I was thinking of using a perl script running in the background by root to pick up commands left by the cgi script and then executing them. But this seems like a silly way of doing things. Are there any suggestions? Thanks.
    Run it only periodically, with cron/at :)
      No it ca'nt run periodically, cos I need the command to be executed immediately when the user clicks on buttons in the cgi script.