Koozali.org: home of the SME Server

Permission to run /sbin/e-smith/db from a perl cgi script.

Offline mrkiwi

  • *
  • 12
  • +0/-0
Permission to run /sbin/e-smith/db from a perl cgi script.
« on: July 12, 2012, 12:17:15 AM »
Hi All.

The Goal.
-----------
Im attempting to write a contrib which will enable autoconfig for email. Basically modern email clients query the server for their imap/pop/smtp/security settings so that users dont have to know them.

This is implemented as a perl script which takes one URL parameter (emailaddress) and returns an xml fragment containing elements with config info for imap, imaps, pop, pops, smtp, etc.

The problem
--------------

My script (below) runs fine from the command line, but from the cgi-bin folder via apache the script fails to run the line
Code: [Select]
my $d = esmith::DomainsDB->open();
I think that i have a permission problem where i dont have the privileges to open the domains database (or the accounts database).
Remember that this script is called *not* from within /server-manager (i've got it at http://myserver/cgi-bin/config-v1.1.xml?emailaddress=blah@domain.com ).

I've looked at the scripts in server-manager, and tried to work it out from there (as these scripts obviously have no problem accessing the accounts and domains databases) but to no avail so far.

Things i've tried
------------------
setuid - was not successful, and anyway, /server-manager scripts don't seem to use setuid to do their stuff.
chmod - the script is already executable (has to be to run), and the server executes all other perl commands in the script, so this is not the problem.
search google/forums - done that, couldn't find any solution.

I'm sure that im doing something daft somewhere - just cant spot it.

Thanks in advance,

MrKiwi.



The Script;

Code: [Select]

#!/usr/bin/perl -wT
use strict;
use warnings;

use CGI;
#use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
local $SIG{__WARN__} = \&Carp::cluck;

print "Content-Type: text/xml\r\n";
print "\r\n";

#my $q = new CGI;
#my $email = $q->param('emailaddress') || 'user@domain.co.nz';
my $email = 'test.user@mycompany.local';
my($fqdn)="";
my($desc)="";
my($username)="";



use esmith::AccountsDB;
use esmith::DomainsDB;

my $d = esmith::DomainsDB->open();
my $a = esmith::AccountsDB->open();

eval{
        my @domains = $d->domains();
        my @users = $a->users();

        foreach my $dm (@domains)
        {
            if (($dm->prop('SystemPrimaryDomain') || 'no') eq 'yes' ) {
                $fqdn = $dm->key();
                $desc = $dm->prop('Description');
            }
        }

        foreach my $u (@users)
        {
            if ($u->prop('ForwardAddress') eq $email ) {
                $username = $u->key();
            }
        }
};
print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<clientConfig version=\"1.1\">
  <emailProvider id=\"freenet.de\">
    <domain>$fqdn</domain>
    <displayName>$desc</displayName>
    <displayShortName>$fqdn</displayShortName>
    <incomingServer type=\"imap\">
      <hostname>imap.$fqdn</hostname>
      <port>993</port>
      <socketType>SSL</socketType>
      <authentication>plain</authentication>
      <username>$username</username>
    </incomingServer>
    <incomingServer type=\"imap\">
      <hostname>imap.$fqdn</hostname>
      <port>143</port>
      <socketType>STARTTLS</socketType>
      <authentication>password-encrypted</authentication>
      <username>$username</username>
    </incomingServer>
    <outgoingServer type=\"smtp\">
      <hostname>smtp.$fqdn</hostname>
      <port>465</port>
      <socketType>SSL</socketType>
      <authentication>plain</authentication>
      <username>$username</username>
    </outgoingServer>
    <documentation   
    url=\"http://imap.$fqdn/help/email/config/index.html\">
      <descr lang=\"de\">Allgemeine Beschreibung der Einstellungen</descr>
      <descr lang=\"en\">Generic settings page</descr>
    </documentation>
    <documentation 
    url=\"http://smtp.$fqdn/help/email/config/thunderbird/imap-thunderbird/imap/index.html\">
      <descr lang=\"de\">TB 2.0 IMAP-Einstellungen</descr>
      <descr lang=\"en\">TB 2.0 IMAP settings</descr>
    </documentation>
  </emailProvider> 
</clientConfig>     
";


Offline mrkiwi

  • *
  • 12
  • +0/-0
Re: Permission to run /sbin/e-smith/db from a perl cgi script.
« Reply #1 on: July 12, 2012, 01:00:30 AM »
I have proven that the cgi scripts are running as 'www' which is to be expected.

Can anyone shed any light on how the /server-manager scripts (which i presume are also executed as 'www') are able to access the databases whereas my test script cant?

Code: [Select]
#!/usr/bin/perl -w
package esmith;
use strict;
use warnings;

use CGI;
#use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
local $SIG{__WARN__} = \&Carp::cluck;

print "Content-Type: text/html\r\n";
print "\r\n";

print "Current user is " . getlogin() . "\n";
print "The active user is: ", `whoami`;

use esmith::AccountsDB;
my $db = esmith::AccountsDB->open or die "Couldn't open AccountsDB\n";
my $admin = $db->get("admin") or die "admin account missing from AccountsDB\n";
print $admin->show();