We've written this plugin for check_delivery, in php! (yes  yes... i
know...):

#!/service/qpsmtpd/plugins/php4 -q
<?php
error_reporting(EX_ALL);
require_once('DB.php');

define('EX_ERROR', 255);
define('EX_FOUND', 0);
define('EX_NOTFOUND', 1);

define('DSN', 'mysql://vpopmail:[EMAIL PROTECTED]/vpopmaildb');
define('VPOPMAIL_PATH', '/home/vpopmail/domains/%s/.qmail-%s');

$db = DB::Connect(DSN);
if (DB::isError($db))
{
    printf($db->getUserInfo());
    exit(EX_ERROR);
}

function checkDB($user='', $domain='')
{
    if (!strlen($user) || !strlen($domain))
    {
        printf('User/domain not given.'."\n");
        return EX_ERROR;
    }

    global $db;
    $q = "SELECT count(*) AS n FROM vpopmail WHERE pw_name = '%s' AND
pw_domain = '%s'";
    $q = sprintf($q, addslashes($user), addslashes($domain));
    $r = $db->getAll($q, array(), DB_FETCHMODE_ASSOC);
    if (DB::isError($r))
    {
        printf($r->getUserInfo()."\n");
        return EX_ERROR;
    }
    if (empty($r[0]) || !isset($r[0]['n']) || $r[0]['n'] == 0)
        return EX_NOTFOUND;
    return EX_FOUND;
}

function checkFile($user='', $domain='')
{
        if (!strlen($user) || !strlen($domain))
                return EX_ERROR;
    $domain = strtolower($domain);

    $path = sprintf(VPOPMAIL_PATH, $domain, $user);
    if (file_exists($path) && is_file($path))
        return EX_FOUND;

    $path = sprintf(VPOPMAIL_PATH, $domain, 'default');
    if (file_exists($path) && is_file($path))
    {
        $s = file_get_contents($path);
        if (is_integer(strpos(strtolower(trim($s)), 'bounce-no-mailbox')))
            return EX_NOTFOUND;
                return EX_FOUND;
    }

    return EX_NOTFOUND;
}

function main()
{
    $mail = $_SERVER['argv'][1];
    $toks = explode('@', $mail);

    if (count($toks) < 2)
    {
        printf("Not found.\n");
        return EX_ERROR;
    }

    $user = $toks[0];
    $domain = $toks[1];
    if (!strlen($user) || !strlen($domain))
    {
        printf("User/domain not given.\n");
        return EX_ERROR;
    }

    $ret = checkDB($user, $domain);
    switch($ret)
    {
        case EX_ERROR:
        case EX_FOUND:
            printf("Exists.\n");
            return $ret;
        case EX_NOTFOUND:
        default:
            $ret = checkFile($user, $domain);
    }

    switch($ret)
        {
                case EX_ERROR:
                case EX_FOUND:
                        printf("Exists.\n");
                        return $ret;
                case EX_NOTFOUND:
                default:
            printf("Not found.\n");
        }
}

if ($_SERVER['argc'] < 2)
    exit(EX_ERROR);

$ret = main();
exit($ret);
?>

This one does a better job than the originally attached
vpopmail_check_user.pl because it does all of the following:

- Checks that the domain exists
- Checks for .qmail-* files
- Checks for users in vpopmail stored in a mysql db
- If .qmail-default specifies a catch-all to a local mailbox, says 'Exists'
for all "To:" fields, on that domain.

vpopmail_check_user.pl did not do all this, however my knowledge in perl is
not sufficient to write this code.
the other problem is that I can't seem to get over the setuid problem. No
matter what I tried, this won't run as the correct user, so it does not have
the correct permissions to access the files. When I run it myself, manually,
as root or as user smtpd or user vpopmail, it works great, but not when
check_delivery runs it. I've no idea why this happens.

Thanks for any help with this.

    Skaag


Reply via email to