OSDN Git Service

BugTrack/709: Investigating multi-scheme password
[pukiwiki/pukiwiki.git] / plugin / md5.inc.php
1 <?php
2 // PukiWiki - Yet another WikiWikiWeb clone.
3 // $Id: md5.inc.php,v 1.8 2005/03/28 14:23:41 henoheno Exp $
4 //  MD5 plugin
5
6 define('PLUGIN_MD5_LIMIT_LENGTH', 512);
7
8 function plugin_md5_action()
9 {
10         global $get, $post;
11
12         if (PKWK_SAFE_MODE || PKWK_READONLY) die_message('Prohibited');
13
14         // Wait POST
15         $key    = isset($post['key']) ? $post['key'] : '';
16         $submit = isset($post['key']);
17         if ($key != '') {
18                 // Compute (Don't show its $key at the same time)
19                 $scheme = isset($post['scheme']) ? $post['scheme'] : '';
20                 $prefix = isset($post['prefix']);
21                 $body   = plugin_md5_compute($scheme, $key, $prefix);
22                 return array('msg'=>'MD5', 'body'=>$body);
23
24         } else {
25                 // If plugin=md4&md5=password, only set it (Don't compute)
26                 $value = isset($get['md5']) ? $get['md5'] : '';
27
28                 plugin_md5_checklimit($value);
29                 if ($value != '') $value  = 'value="' . htmlspecialchars($value) . '" ';
30                 $self = get_script_uri();
31                 $form = '';
32                 if ($submit) $form .= '<strong>NO PHRASE</strong><br />';
33                 $form .= <<<EOD
34 <form action="$self" method="post">
35  <div>
36   <input type="hidden" name="plugin" value="md5" />
37   <label for="_p_md5_phrase">Phrase:</label>
38   <input type="text"  name="key"    id="_p_md5_phrase" size="30" $value/><br />
39   <input type="radio" name="scheme" id="_p_md5_sha1" value="php_sha1" />
40   <label for="_p_md5_sha1">PHP sha1()</label><br />
41   <input type="radio" name="scheme" id="_p_md5_md5"  value="php_md5" checked="checked" />
42   <label for="_p_md5_md5">PHP md5()</label><br />
43   <input type="radio" name="scheme" id="_p_md5_crpt" value="php_crypt" />
44   <label for="_p_md5_crpt">PHP crypt()</label><br />
45   <input type="radio" name="scheme" id="_p_md5_lmd5" value="ldap_md5" />
46   <label for="_p_md5_lmd5">OpenLDAP MD5</label><br />
47   <input type="radio" name="scheme" id="_p_md5_lsha" value="ldap_sha" />
48   <label for="_p_md5_lsha">OpenLDAP SHA (sha1)</label><br />
49   <input type="checkbox" name="prefix" id="_p_md5_prefix" checked="checked" />
50   <label for="_p_md5_prefix">Add scheme prefix (RFC2307, Using LDAP as NIS)</label><br />
51   <input type="submit" value="Compute" />
52  </div>
53 </form>
54 EOD;
55                 return array('msg'=>'MD5', 'body'=>$form);
56         }
57 }
58
59 // Compute hash with php-functions, or compute like slappasswd (OpenLDAP)
60 function plugin_md5_compute($scheme = 'php_md5', $key = '', $prefix = FALSE)
61 {
62         plugin_md5_checklimit($key);
63
64         switch (strtolower($scheme)) {
65         case 'x-php-crypt' : /* FALLTHROUGH */
66         case 'php_crypt'   :
67                 $hash = ($prefix ? '{x-php-crypt}' : '') . crypt($key); break;
68         case 'x-php-md5'   : /* FALLTHROUGH */
69         case 'php_md5'     :
70                 $hash = ($prefix ? '{x-php-md5}'   : '') . md5($key);  break;
71         case 'x-php-sha1'  : /* FALLTHROUGH */
72         case 'php_sha1'    :
73                 $hash = ($prefix ? '{x-php-sha1}'  : '') . sha1($key); break;
74         case 'md5'         : /* FALLTHROUGH */
75         case 'ldap_md5'    :
76                 $hash = ($prefix ? '{MD5}' : '') . base64_encode(hex2bin(md5($key)));  break;
77         case 'sha'         : /* FALLTHROUGH */
78         case 'ldap_sha'    :
79                 $hash = ($prefix ? '{SHA}' : '') . base64_encode(hex2bin(sha1($key))); break;
80         default: $hash = ''; break;
81         }
82
83         return $hash;
84 }
85
86 function plugin_md5_checklimit($text)
87 {
88         if (strlen($text) > PLUGIN_MD5_LIMIT_LENGTH)
89                 die_message('Limit: malicious message length');
90 }
91 ?>