<?php
/*
Class to manage the users of a sendmail installation
Version : 0.1a-ndbm
Date : 09.30.2001
Author : Oliver Schlag <oliver.schlag@blue-online.de>
This class only handles the ndbm style tables. If you have
flat files or other then this class is the wrong one. Perhaps you
like to port this class to another data storage ;-) If you port
it please drop me a line im intrested in every port.
If you find an error please send me a message and i will try to
fix it. Please send me as many informations as possible in your
error message so i can try to reproduce and fix it.
You can use this class without any restrictions, you can modify
it and you can do what ever you want to do with it, BUT you're
not allowed to sell this class or use it in a product you sell.
Please note that this class is not for use in a webserver enviroment,
this class is only for the cgi version of php. This class is
to be used in PHP Scripts which will be run from shell or cron like
bash scripts.
Any other comments, except flames ;-), are welcome.
Functiontable :
sendmail_ndbm_class($aliastablepath,$virtusertablepath,$debuglevel,$log)
This is the constructor, the constructor will open
the files, create the array's.
Arguments :
$aliastablepath : Path to the alias file (normal /etc/mail/aliases)
$virtusertablepath : Path to the virtusertable file (normal /etc/mail/virtusertable)
$debuglevel : 0 = no Debug Output, 1 = Debug Output
$log : 1 = Log to the syslog, 0 = no logging
If you specify a log then the debug output will be
written into this log and there's no output on the
console.
sendmail_ndbm_close()
This is the destructor it will close the dbm connections and
open the files for the public
Arguments :
This function did not take any arguments.
add_virtual($address,$domain,$user)
This function will create a new entry in the virtual user
table.
If an error occurs the function returns -1 if all went
ok the function will return a 0.
Arguments :
$address : The part before the @ in the mail address
$domain : The part after the @ in the new mail address
$user : The user which should receive the mail to this address
del_virtual($address,$domain)
This function will delete an entry in the virtual user
table.
If an error occurs the function returns -1 if all went
ok the function will return a 0.
Arguments :
$address : The part before the @ in the mail address
$domain : The part after the @ in the mail address
add_route($source,$dest)
This function will create a virtual host route.
If an error occurs the function returns -1 if all went
ok the function will return a 0.
Arguments :
$source : The source domain
$dest : The destination domain
del_route($source)
This function will delete a virtual host route
If an error occurs the function returns -1 if all went
ok the function will return a 0.
Arguments :
$source : The source domain
$dest : The destination domain
add_alias($source,$dest)
This function will add an alias to the alias map
If an error occurs the function returns -1 if all went
ok the function will return a 0.
Arguments :
$source : The user which should be forwarded
$dest : The destination of the mails
del_alias($source)
This function will delete an alias from the alias map
If an error occurs the function returns -1 if all went
ok the function will return a 0.
Arguments :
$source : The user which should be forwarded
$dest : The destination of the mails
*/
class sendmail {
var $ALIAS;
var $VIRTUALLINK;
var $ALIASPATH;
var $VIRTUALPATH;
var $DEBUG;
var $LOG;
# Start of initalize function
function sendmail_ndbm_class($aliastablepath,$virtusertablepath,$debuglevel,$log)
{
if ( $aliastablepath != "")
{
$this->ALIASPATH = $aliastablepath;
}
else
{
return -1;
}
if ( $virtusertablepath != "")
{
$this->VIRTUALPATH = $virtusertablepath;
}
else
{
return -1;
}
if ( $debuglevel != "")
{
$this->DEBUG = 1;
}
else
{
$this->DEBUG = 0;
}
if ($log != "0")
{
$this->LOG = 1;
}
else
{
$this->LOG = 0;
}
$this->VIRTUALLINK = dba_open($this->VIRTUALPATH,'w','ndbm');
$ap = fopen($this->ALIASPATH,"r");
if(!$ap)
{
# Output the Debug informations
if ($this->LOG)
{
syslog(LOG_ERR,"Error opening alias file : $this->ALIASPATH");
}
return -1;
}
# Output the Debug informations
if ($this->DEBUG)
{
syslog(LOG_DEBUG,"Aliastable opened");
}
while(!feof($ap))
{
$data = trim(fgets($ap,4096));
if($data[0] != '#' && $data[0] != '')
{
$alias_temp = explode(":",$data);
$key = trim($alias_temp[0]);
$value = trim($alias_temp[1]);
$this->ALIAS[$key] = $value;
}
}
fclose($ap);
# Output the Debug informations
if ($this->DEBUG)
{
$cnr = count($this->ALIAS);
syslog(LOG_DEBUG,"$cnr aliases read");
syslog(LOG_DEBUG,"Aliastable closed");
}
}
function sendmail_ndbm_close()
{
# Write new alias Database (we copy the .new file later onto the original file)
$new_alias = $this->ALIASPATH.".new";
# Check if the .new file aread exist and delete it
if (file_exists($new_alias))
{
unlink($new_alias);
}
$ap = fopen($new_alias,"w");
if (!$ap)
{
if ($this->LOG)
{
syslog(LOG_ERR,"New aliastable create error");
return -1;
}
}
foreach ($this->ALIAS as $key => $value)
{
$temp_alias = $key.":\t".$value."\n";
fputs($ap,$temp_alias);
}
fclose($ap);
if ($this->DEBUG)
{
syslog(LOG_DEBUG,"New Aliastable File created");
}
$old_alias = $this->ALIASPATH.".old";
if (!rename($this->ALIASPATH,$old_alias))
{
if ($this->LOG)
{
syslog(LOG_ERR,"Error renaming $this->ALIASPATH");
return -1;
}
}
else
{
if ($this->DEBUG)
{
syslog(LOG_DEBUG,"$this->ALIASPATH renamed to $old_alias");
}
}
# Create the new alias table
if (!rename($new_alias, $this->ALIASPATH))
{
if ($this->LOG)
{
syslog(LOG_ERR,"Error renaming $new_alias");
return -1;
}
}
else
{
if ($this->DEBUG)
{
syslog(LOG_DEBUG,"$new_alias renamed to $this->ALIASPATH");
}
}
unlink($old_alias);
dba_close($this->VIRTUALLINK);
}
function add_virtual($address,$domain,$user)
{
if ($address == "" || $domain == "" || $user == "")
{
if ($this->DEBUG)
{
syslog(LOG_DEBUG,"Error creating virtual user, empty value recived");
}
return false;
}
if ($this->VIRTUALLINK == FALSE)
{
if ($this->DEBUG)
{
syslog(LOG_DEBUG,"Error creating virtual user, virtusertable not accessible");
}
return false;
}
$address = trim(str_replace(chr(13),"",$address));
$domain = trim(str_replace(chr(13),"",$domain));
$key = $address."@".$domain;
$value = trim(str_replace(chr(13),"",$user));
if (!dba_insert($key,$value,$this->VIRTUALLINK))
{
if ($this->DEBUG)
{
syslog(LOG_DEBUG,"Error creating virtual user, could not insert $key=>$value");
}
return false;
}
return true;
}
function del_virtual($address,$domain)
{
if ($address == "" || $domain == "")
{
if ($this->DEBUG)
{
syslog(LOG_DEBUG,"Error deleting virtual user, empty value recived");
}
return false;
}
if ($this->VIRTUALLINK == FALSE)
{
if ($this->DEBUG)
{
syslog(LOG_DEBUG,"Error deleting virtual user, virtusertable not accessible");
}
return false;
}
$address = trim(str_replace(chr(13),"",$address));
$domain = trim(str_replace(chr(13),"",$domain));
$key = $address."@".$domain;
if (!dba_exists($key,$this->VIRTUALLINK))
{
if ($this->DEBUG)
{
syslog(LOG_DEBUG,"Error deleting virtual user, $key does not exist");
}
return false;
}
if (!dba_delete($key,$this->VIRTUALLINK))
{
if ($this->DEBUG)
{
syslog(LOG_DEBUG,"Error deleting virtual user, $key is not deleteable");
}
return false;
}
return true;
}
function add_route($source,$dest)
{
if ($source == "" || $dest == "" )
{
if ($this->DEBUG)
{
syslog(LOG_DEBUG,"Error creating domain route, empty value recived");
}
return false;
}
if ($this->VIRTUALLINK == FALSE)
{
if ($this->DEBUG)
{
syslog(LOG_DEBUG,"Error creating domain route, virtusertable not accessible");
}
return false;
}
$source = trim(str_replace(chr(13),"",$source));
$dest = trim(str_replace(chr(13),"",$dest));
$key = "@".$source;
$value = "%1@".$dest;
if (!dba_insert($key,$value,$this->VIRTUALLINK))
{
if ($this->DEBUG)
{
syslog(LOG_DEBUG,"Error creating domain route, could not insert $key=>$value");
}
return false;
}
return true;
}
function del_route($source)
{
if ($source == "")
{
if ($this->DEBUG)
{
syslog(LOG_DEBUG,"Error deleting domain route, empty value recived");
}
return false;
}
if ($this->VIRTUALLINK == FALSE)
{
if ($this->DEBUG)
{
syslog(LOG_DEBUG,"Error deleting domain route, virtusertable not accessible");
}
return false;
}
$source = trim(str_replace(chr(13),"",$source));
$key = "@".$source;
if (!dba_exists($key,$this->VIRTUALLINK))
{
if ($this->DEBUG)
{
syslog(LOG_DEBUG,"Error deleting domain route, $key does not exist");
}
return false;
}
if (!dba_delete($key,$this->VIRTUALLINK))
{
if ($this->DEBUG)
{
syslog(LOG_DEBUG,"Error deleting domain route, $key is not deleteable");
}
return false;
}
return true;
}
function add_alias($source,$dest)
{
if ($source == "" || $dest == "")
{
if ($this->DEBUG)
{
syslog(LOG_DEBUG,"Error creating alias, empty value recived");
}
return false;
}
$source = trim(str_replace(chr(13),"",$source));
$dest = trim(str_replace(chr(13),"",$dest));
$key = $source;
$value = $dest;
if($this->ALIAS[$source] == "")
{
$this->ALIAS[$source] = $dest;
if ($this->LOG)
{
syslog(LOG_INFO,"New alias created : $source => $dest");
}
}
else
{
if ($this->LOG)
{
syslog(LOG_ERR,"Error creating new alias : $source => $dest");
return -1;
}
}
return 0;
}
function del_alias($source)
{
if ($source == "")
{
if ($this->DEBUG)
{
syslog(LOG_DEBUG,"Error deleting alias, empty value recived");
}
return false;
}
$source = trim(str_replace(chr(13),"",$source));
$key = $source;
if($this->ALIAS[$source] != "")
{
unset($this->ALIAS[$source]);
if ($this->LOG)
{
syslog(LOG_INFO,"Alias deleted : $source");
}
}
else
{
if ($this->LOG)
{
syslog(LOG_ERR,"Error deleting alias : $source");
return -1;
}
}
return 0;
}
} // Class End
?> |