OSDN Git Service

BugTrack/2565 Fix read_auth and edit_auth apply condition
[pukiwiki/pukiwiki.git] / plugin / md5.inc.php
1 <?php
2 // PukiWiki - Yet another WikiWikiWeb clone.
3 // md5.inc.php
4 // Copyright 2001-2018 PukiWiki Development Team
5 // License: GPL v2 or (at your option) any later version
6 //
7 //  MD5 plugin: Allow to convert password/passphrase
8 //      * PHP sha1() -- If you have sha1() or mhash extension
9 //      * PHP md5()
10 //      * PHP hash('sha256')
11 //      * PHP hash('sha512')
12 //      * LDAP SHA / SSHA -- If you have sha1() or mhash extension
13 //      * LDAP MD5 / SMD5
14
15 // User interface of pkwk_hash_compute() for system admin
16 function plugin_md5_action()
17 {
18         global $get, $post;
19         if (PKWK_SAFE_MODE || PKWK_READONLY) die_message('Prohibited by admin');
20         // Wait POST
21         $phrase = isset($post['phrase']) ? $post['phrase'] : '';
22         if ($phrase == '') {
23                 // Show the form
24                 // If plugin=md5&md5=password, only set it (Don't compute)
25                 $value  = isset($get['md5']) ? $get['md5'] : '';
26                 return array(
27                         'msg' =>'Compute userPassword',
28                         'body'=>plugin_md5_show_form(isset($post['phrase']), $value));
29         } else {
30                 // Compute (Don't show its $phrase at the same time)
31                 $is_output_prefix = isset($post['prefix']);
32                 $salt   = isset($post['salt']) ? $post['salt'] : '';
33                 $scheme = isset($post['scheme']) ? $post['scheme']: '';
34                 $algos_enabled = plugin_md5_get_algos_enabled();
35                 $scheme_list = array('x-php-md5', 'MD5', 'SMD5');
36                 if ($algos_enabled->sha1) {
37                         array_push($scheme_list, 'x-php-sha1', 'SHA', 'SSHA');
38                 }
39                 if ($algos_enabled->sha256) {
40                         array_push($scheme_list, 'x-php-sha256', 'SHA256', 'SSHA256');
41                 }
42                 if ($algos_enabled->sha512) {
43                         array_push($scheme_list, 'x-php-sha512', 'SHA512', 'SSHA512');
44                 }
45                 if (!in_array($scheme, $scheme_list)) {
46                         return array(
47                                 'msg' => 'Error',
48                                 'body' => 'Invalid scheme: ' . htmlsc($scheme),
49                         );
50                 }
51                 $scheme_with_salt = '{' . $scheme . '}' . $salt;
52                 return array(
53                         'msg' =>'Result',
54                         'body'=>
55                                 pkwk_hash_compute($phrase, $scheme_with_salt,
56                                         $is_output_prefix, TRUE));
57         }
58 }
59
60 // $nophrase = Passphrase is (submitted but) empty
61 // $value    = Default passphrase value
62 function plugin_md5_show_form($nophrase = FALSE, $value = '')
63 {
64         if (PKWK_SAFE_MODE || PKWK_READONLY) die_message('Prohibited');
65         if (strlen($value) > PKWK_PASSPHRASE_LIMIT_LENGTH) {
66                 die_message('Limit: malicious message length');
67         }
68         if ($value != '') $value = 'value="' . htmlsc($value) . '" ';
69         $algos_enabled = plugin_md5_get_algos_enabled();
70         $sha1_checked = $md5_checked = '';
71         if ($algos_enabled->sha1) {
72                 $sha1_checked = 'checked="checked" ';
73         } else {
74                 $md5_checked  = 'checked="checked" ';
75         }
76         $self = get_base_uri();
77         $form = <<<EOD
78 <p><strong>NOTICE: Don't use this feature via untrustful or unsure network</strong></p>
79 <hr>
80 EOD;
81         if ($nophrase) $form .= '<strong>NO PHRASE</strong><br />';
82         $form .= <<<EOD
83 <form action="$self" method="post">
84  <div>
85   <input type="hidden" name="plugin" value="md5" />
86   <label for="_p_md5_phrase">Phrase:</label>
87   <input type="text" name="phrase"  id="_p_md5_phrase" size="60" $value/><br />
88 EOD;
89         $form .= <<<EOD
90   <input type="radio" name="scheme" id="_p_md5_md5"  value="x-php-md5" />
91   <label for="_p_md5_md5">PHP md5</label><br />
92 EOD;
93         if ($algos_enabled->sha1) $form .= <<<EOD
94   <input type="radio" name="scheme" id="_p_md5_sha1" value="x-php-sha1" />
95   <label for="_p_md5_sha1">PHP sha1</label><br />
96 EOD;
97         if ($algos_enabled->sha256) $form .= <<<EOD
98   <input type="radio" name="scheme" id="_p_md5_sha256" value="x-php-sha256" />
99   <label for="_p_md5_sha256">PHP sha256</label><br />
100 EOD;
101         if ($algos_enabled->sha512) $form .= <<<EOD
102   <input type="radio" name="scheme" id="_p_md5_sha512" value="x-php-sha512" />
103   <label for="_p_md5_sha512">PHP sha512</label><br />
104 EOD;
105         if ($algos_enabled->sha1) $form .= <<<EOD
106   <input type="radio" name="scheme" id="_p_md5_lssha" value="SSHA" $sha1_checked/>
107   <label for="_p_md5_lssha">LDAP SSHA (sha-1 with a seed) *</label><br />
108   <input type="radio" name="scheme" id="_p_md5_lsha" value="SHA" />
109   <label for="_p_md5_lsha">LDAP SHA (sha-1)</label><br />
110 EOD;
111         $form .= <<<EOD
112   <input type="radio" name="scheme" id="_p_md5_lsmd5" value="SMD5" $md5_checked/>
113   <label for="_p_md5_lsmd5">LDAP SMD5 (md5 with a seed) *</label><br />
114   <input type="radio" name="scheme" id="_p_md5_lmd5" value="MD5" />
115   <label for="_p_md5_lmd5">LDAP MD5</label><br />
116 EOD;
117         if ($algos_enabled->sha256) $form .= <<<EOD
118   <input type="radio" name="scheme" id="_p_md5_lssha256" value="SSHA256"/>
119   <label for="_p_md5_lssha256">LDAP SSHA256 (sha256 with a seed) *</label><br />
120   <input type="radio" name="scheme" id="_p_md5_lsha256" value="SHA256" />
121   <label for="_p_md5_lsha256">LDAP SHA256</label><br />
122 EOD;
123         if ($algos_enabled->sha512) $form .= <<<EOD
124   <input type="radio" name="scheme" id="_p_md5_lssha512" value="SSHA512"/>
125   <label for="_p_md5_lssha512">LDAP SSHA512 (sha512 with a seed) *</label><br />
126   <input type="radio" name="scheme" id="_p_md5_lsha512" value="SHA512" />
127   <label for="_p_md5_lsha512">LDAP SHA512</label><br />
128 EOD;
129         $form .= <<<EOD
130   <input type="checkbox" name="prefix" id="_p_md5_prefix" checked="checked" />
131   <label for="_p_md5_prefix">Add scheme prefix (RFC2307, Using LDAP as NIS)</label><br />
132
133   <label for="_p_md5_salt">Salt:</label>
134   <input type="text" name="salt" id="_p_md5_salt" size="60" /><br />
135
136   <input type="submit" value="Compute" /><br />
137
138   <hr>
139   <p>* = Salt enabled<p/>
140  </div>
141 </form>
142 EOD;
143
144         return $form;
145 }
146
147 /**
148  * Get availabilites of algos.
149  */
150 function plugin_md5_get_algos_enabled()
151 {
152         $sha1_enabled = function_exists('sha1');
153         $sha256_enabled = false;
154         $sha512_enabled = false;
155         if (function_exists('hash') && function_exists('hash_algos')) {
156                 $algos = hash_algos();
157                 if (in_array('sha256', $algos)) {
158                         $sha256_enabled = true;
159                 }
160                 if (in_array('sha512', $algos)) {
161                         $sha512_enabled = true;
162                 }
163         }
164         return (object) array(
165                 'sha1' => $sha1_enabled,
166                 'sha256' => $sha256_enabled,
167                 'sha512' => $sha512_enabled,
168         );
169 }