This code is under the GNU Public License
See the file COPYING in this directory
*/
/*
* This code _ONLY_ makes sense if the client has a valid cert.
*/
if (!$ssl->valid_cert_found) {
echo "Warning: The Cyrus auto create is dangerous in the absence of ssl client certs.
\n";
} else {
/*
* This is quick and dirty code that should probably be cleaned up.
* Use at your own risk.
*/
if( empty( $ssl ) ) { global $ssl; }
class imap_admin {
var $fp = 0;
var $banner = "";
var $capabilities = "";
var $server = "127.0.0.1";
var $l = "";
var $debug = false;
function login( $username, $password ) {
$this->fp = fsockopen($this->server, 143, &$errno, &$errstr);
if(!$this->fp) {
echo "$errstr ($errno)
\n";
} else {
# Get the banner.
$banner = $this->getline();
if( $this->debug ) echo "
$banner
\n";
# Login
$this->command(". login $username $password");
$this->pcommand("Login",$this->l);
# Get capabilities
$this->command(". capability");
$this->capabilities = $this->l;
$this->pcommand("Capabilities",$this->capabilities);
$this->getline();
}
}
function createmb_and_acl( $mb_name, $authid, $acl ) {
# Issue the create command
$this->putline(". create $mb_name");
$this->pcommand("Create",$this->getline());
# Set acl
$this->putline(". setacl $mb_name $authid $acl");
$this->pcommand("SetACL",$this->getline());
}
function deletemb( $mb_name, $authid ) {
# Issue delete command
$this->command(". setacl $mb_name $authid d");
$this->command(". delete $mb_name");
$this->pcommand("Delete",$this->l);
}
function command($in) {
$this->putline($in);
return $this->getline();
}
function pcommand($command,$response) {
if( $this->debug ) echo "$command: $response
\n";
}
function getline() {
if( $this->debug ) echo "GET\n ";
$this->l=fgets($this->fp,128);
if( $this->debug ) { echo "$this->l
\n"; flush(); }
return $this->l;
}
function putline( $line ) {
if( $this->debug ) echo "PUT $line
\n";
$r=fputs($this->fp,"$line\n");
return $r;
}
function logout() {
$this->putline(". logout");
if( $this->debug ) echo $this->getline()."
\n";
fclose($this->fp);
$this->fp = 0;
}
}
// echo "Creating your new mailbox...
";
$ia = new imap_admin;
$ia->server = $default->server;
// $ia->debug = true;
$ia->login( $default->cyrus->admin_user, $default->cyrus->admin_pass );
$possible="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()-+_./?,:;";
srand((double)microtime()*1000000);
for($password="imp",$i=0;$i<8;$i++)
$password.=substr($possible,rand(0,strlen($possible)),1);
if (!isset($username)) $username = strtolower(preg_replace("|[.//\\///+ \t\n]|", '', $CN));
$ia->createmb_and_acl("user.$username","any","all");
$ia->logout();
/* FIXME!! The database routines need to be
* moved to mysql.lib.php3 and made generic.
*/
if (!($db = mysql_pconnect("localhost","www-data"))) {
status("Error in LOG __FILE__(__LINE__): DB not found.");
return false;
}
if (!($dbs = mysql_select_db("imp",$db))) {
status("Error in LOG __FILE__(__LINE__): Can't select DB.");
return false;
}
$res = mysql_db_query("imp","insert into ssl_to_imp values ('".$ssl->client_dn."','".$username."','".$password."','".$default->server."','')",$db);
if (!$res) {
status( "error creating ssl->imap mapping: ".mysql_error());
}
mysql_free_result($res);
}
?>