<?php

/*
 
 File: cyrus_auto_create.php3
 $Author: chuck $
 $Revision: 1.7 $
 $Date: 1999/04/09 13:47:40 $
 
 IMP: Copyright 1998,1999 Charles J. Hagenbuch <chuck@horde.org>
 
 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 "<font color=red><b>Warning: </b></font>The Cyrus auto create is dangerous in the absence of ssl client certs.<br>\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)<br>\n";
	 } else {
# Get the banner.
	    $banner =  $this->getline();
	    if( $this->debug ) echo "<h2>$banner</h2>\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 "<font color=blue size=+2>$command:</font> $response<br>\n";
      }
      function getline() {
	 if( $this->debug ) echo "<font color=white>GET</font>\n ";
	 $this->l=fgets($this->fp,128);
	 if( $this->debug ) { echo "$this->l<br>\n"; flush(); }
	 return $this->l;
      }
      function putline( $line ) {
	 if( $this->debug ) echo "<font color=green>PUT</font> $line<br>\n";
	 $r=fputs($this->fp,"$line\n");
	 return $r;
      }
      
      function logout() {
	 $this->putline(". logout");
	 if( $this->debug ) echo $this->getline()."<br>\n";
	 fclose($this->fp);
	 $this->fp = 0;
      }
   }
   
   //  echo "<h2>Creating your new mailbox...</h2>";
   
   $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( ereg_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);  
}

?>
