home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / admin / pref_gpg.php < prev    next >
Encoding:
PHP Script  |  2003-03-16  |  7.1 KB  |  220 lines

  1. <?php
  2. /////////////////////////////////////////////////////////
  3. //    
  4. //    source/pref_identities.php
  5. //
  6. //    (C)Copyright 2000-2002 Ryo Chijiiwa <Ryo@IlohaMail.org>
  7. //
  8. //        This file is part of IlohaMail.
  9. //        IlohaMail is free software released under the GPL 
  10. //        license.  See enclosed file COPYING for details,
  11. //        or see http://www.fsf.org/copyleft/gpl.html
  12. //
  13. /////////////////////////////////////////////////////////
  14.  
  15. /********************************************************
  16.  
  17.     AUTHOR: Ryo Chijiiwa <ryo@ilohamail.org>
  18.     FILE: source/pref_identities.php
  19.     PURPOSE:
  20.         Create/edit/delete identities
  21.     PRE-CONDITIONS:
  22.         $user - Session ID
  23.         
  24. ********************************************************/
  25.  
  26.     include("../include/super2global.inc");
  27.     include("../include/header_main.inc");
  28.     include("../include/langs.inc");
  29.     include("../include/icl.inc");    
  30.     include("../lang/".$my_prefs["lang"]."prefs.inc");
  31.     include("../include/gpg.inc");
  32.     include("../include/pref_header.inc");
  33.     
  34.     
  35.     //authenticate
  36.     $conn=iil_Connect($host, $loginID, $password, $AUTH_MODE);
  37.     if ($conn){
  38.         if ($ICL_CAPABILITY["folders"]){
  39.             if ($my_prefs["hideUnsubscribed"]){
  40.                 $mailboxes = iil_C_ListSubscribed($conn, $my_prefs["rootdir"], "*");
  41.             }else{
  42.                 $mailboxes = iil_C_ListMailboxes($conn, $my_prefs["rootdir"], "*");
  43.             }
  44.             sort($mailboxes);
  45.         }
  46.         iil_Close($conn);
  47.     }else{
  48.         echo "Authentication failed.";
  49.         echo "</body></html>\n";
  50.         exit;
  51.     }
  52.     
  53.     $gpg_home = str_replace("%h", $host, str_replace("%u", $loginID, $GPG_HOME_STR));
  54.     if (file_exists(realpath($gpg_home."/pubring.gpg"))) $key_exists = true;
  55.     else $key_exists = false;
  56.     
  57.     if (isset($genkey)){
  58.         if (empty($name)) $error .= "Name not specified\n";
  59.         if (empty($email)) $error .= "Password not specified\n";
  60.         if (strlen($passphrase)<8) $error .= "Passphrase must be ATLEAST 8 characters long\n";
  61.         
  62.         if (empty($error)){
  63.             //echo "Making dir: $gpg_home <br>";
  64.             //create file containing key specifications
  65.             if (!is_dir(realpath($gpg_home))){
  66.                 if (!mkdir($gpg_home, 0700)){
  67.                     $error .= "mkdir() failed <br>\n";
  68.                 }
  69.             }
  70.             
  71.             if (empty($error)){
  72.                 //echo "Made dir <br>\n";
  73.                 $temp_file = $gpg_home."/".$user.".tmp";
  74.                 $fp = fopen($temp_file, "w");
  75.                 if ($fp){
  76.                     echo "Opened temp file: $temp_file <br>\n";
  77.                     $output = "Key-Type: DSA\n";
  78.                     $output.= "Key-Length: 1024\n";
  79.                     $output.= "Subkey-Type: ELG-E\n";
  80.                     $output.= "Subkey-Length: 1024\n";
  81.                     $output.= "Name-Real: ".$name."\n";
  82.                     $output.= "#Name-Comment: test\n";
  83.                     $output.= "Name-Email: ".$email."\n";
  84.                     $output.= "Expire-Date: 0\n";
  85.                     $output.= "Passphrase: ".$passphrase."\n";
  86.                     $output.= "%commit\n";
  87.                     fputs($fp, $output);
  88.                     fclose($fp);
  89.                     //echo "Wrote: <pre>$output</pre><br>\n";
  90.                 }else{
  91.                     $error .= "Temp file not created: $temp_file <br>\n";
  92.                 }
  93.             }
  94.             //generate key
  95.             if (empty($error)){                
  96.                 $command = $GPG_PATH." --home=".realpath($gpg_home)." --batch --gen-key ".realpath($temp_file);
  97.                 //echo "Will run command: <tt>$command</tt><br>\n";
  98.                 $temp = exec($command, $result, $errorno);
  99.                 //echo "Got results: [$errorno]<pre>".implode("\n", $result)."</pre><br>\n";
  100.                 
  101.                 if ($errorno==0){
  102.                     /*
  103.                     $command = $GPG_PATH." --home=".realpath($gpg_home)." --export -a";
  104.                     //echo "Running: $command <br>\n";
  105.                     $temp = exec($command, $result, $errono);
  106.                     //echo "Got results: [$errorno]<pre>".implode("\n", $result)."</pre><br>\n";
  107.                     */
  108.                     if ($errorno==0) $key_exists = true;
  109.                 }else{
  110.                     $error.= "Key generation failed.  Erro code: $errorno <br>\n";
  111.                 }
  112.                 unlink($temp_file);
  113.             }
  114.         }
  115.     }
  116.     
  117.     if ($key_exists && isset($import)){
  118.         if (empty($public_key)) $error .= "Public key not specified...";
  119.         if (empty($error)){
  120.             $temp_file = $gpg_home."/".$user.".import.tmp";
  121.             $fp = fopen($temp_file, "w");
  122.             if ($fp){
  123.                 fputs($fp, $public_key);
  124.                 fclose($fp);
  125.                 echo "Wrote: <pre>$output</pre><br>\n";
  126.             }else{
  127.                 $error .= "Temp file not created: $temp_file <br>\n";
  128.             }
  129.         }
  130.         if (empty($error)){
  131.             $command = $GPG_PATH." --home=".realpath($gpg_home)." --import ".realpath($temp_file);
  132.             $temp = exec($command, $result, $errorno);
  133.             "Import: [$errorno] <pre>".implode("\n", $result)."</pre><br>\n";
  134.             unlink($temp_file);
  135.         }
  136.     }
  137.     
  138.     if (isset($show_key) && !empty($person)){
  139.         echo "Show key for $person <br>\n";
  140.         $public_key = gpg_export($person);
  141.     }
  142.     
  143.     $gpgStrings["genkey"] = "Generate Key";
  144.     $gpgStrings["name"] = "Name";
  145.     $gpgStrings["email"] = "Email";
  146.     $gpgStrings["passphrase"] = "Passphrase";
  147.     $gpgStrings["generate"] = "Generate Key";
  148.     ?>        
  149.     <font color="red"><?php echo $error?></font>
  150.     <p>
  151.     
  152.     <?php
  153.         if ($key_exists){
  154.     ?>
  155.             <form method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
  156.             <input type="hidden" name="user" value="<?php echo $user?>">
  157.             <input type="hidden" name="session" value="<?php echo $user?>">
  158.             <table border="0" cellspacing="1" cellpadding="1" bgcolor="<?php echo $my_colors["main_hilite"]?>" width="95%">
  159.             <tr bgcolor="<?php echo $my_colors["main_head_bg"]?>">
  160.             <td align="center"><span class="tblheader"><?php echo $gpgStrings["genkey"]?></span></td>
  161.             </tr><tr bgcolor="<?php echo $my_colors["main_bg"]?>">
  162.             <td align="center">
  163.                 Show public key for:
  164.                 <?php
  165.                     $keys = gpg_list_keys();
  166.                     $options = "";
  167.                     if (is_array($keys) && count($keys)>0){
  168.                         while (list($k,$str)=each($keys)){
  169.                             $options.= "<option value=\"$k\">$str\n";
  170.                         }
  171.                     }
  172.                 ?>
  173.                 <select name="person">
  174.                 <?php echo $options ?>
  175.                 </select>
  176.                 <input type="submit" name="show_key" value="Show Key">
  177.                 <p>Add public key:<br>
  178.                 <textarea cols=80 rows=15 name="public_key"><?php echo $public_key?></textarea><br>
  179.                 <input type="submit" name="import" value="Import Public Key">
  180.             </td>
  181.             </tr>
  182.             </table>
  183.             </form>
  184.     <?php
  185.         }else{
  186.     ?>
  187.             <form method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
  188.             <input type="hidden" name="user" value="<?php echo $user?>">
  189.             <input type="hidden" name="session" value="<?php echo $user?>">
  190.             <table border="0" cellspacing="1" cellpadding="1" bgcolor="<?php echo $my_colors["main_hilite"]?>" width="95%">
  191.             <tr bgcolor="<?php echo $my_colors["main_head_bg"]?>">
  192.             <td align="center"><span class="tblheader"><?php echo $gpgStrings["genkey"]?></span></td>
  193.             </tr><tr bgcolor="<?php echo $my_colors["main_bg"]?>">
  194.             <td align="center">
  195.                 <table>
  196.                     <tr>
  197.                         <td align="right"><?php echo $gpgStrings["name"]?>:</td>
  198.                         <td><input type="text" name="name" value="<?php echo stripslashes($name)?>" size="45"></td>
  199.                     </tr>
  200.                     <tr>
  201.                         <td align="right"><?php echo $gpgStrings["email"]?>:</td>
  202.                         <td><input type="text" name="email" value="<?php echo $email ?>" size="45"></td>
  203.                     </tr>
  204.                     <tr>
  205.                         <td align="right"><?php echo $gpgStrings["passphrase"]?>:</td>
  206.                         <td><input type="password" name="passphrase" value="" size="45"></td>
  207.                     </tr>
  208.                 </table>
  209.                 CAUTION!  Generating a key could take a long time (a minute or more).  If you stop loading before a key is generated, this will screw things up big time (you will have to remove your GPG_HOME folder).
  210.                 <input type="submit" name="genkey" value="<?php echo $gpgStrings["generate"]?>">
  211.             </td>
  212.             </tr>
  213.             </table>
  214.             </form>
  215.             
  216.             <?php
  217.             }
  218.             ?>
  219. </BODY></HTML>
  220.