Rev 448 | Rev 476 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
5 | daniel-mar | 1 | /* |
2 | * OIDplus 2.0 |
||
3 | * Copyright 2019 Daniel Marschall, ViaThinkSoft |
||
4 | * |
||
5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
||
6 | * you may not use this file except in compliance with the License. |
||
7 | * You may obtain a copy of the License at |
||
8 | * |
||
9 | * http://www.apache.org/licenses/LICENSE-2.0 |
||
10 | * |
||
11 | * Unless required by applicable law or agreed to in writing, software |
||
12 | * distributed under the License is distributed on an "AS IS" BASIS, |
||
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||
14 | * See the License for the specific language governing permissions and |
||
15 | * limitations under the License. |
||
16 | */ |
||
17 | |||
362 | daniel-mar | 18 | // DEFAULT_LANGUAGE will be set by setup.js.php |
19 | // language_messages will be set by setup.js.php |
||
20 | // language_tblprefix will be set by setup.js.php |
||
21 | |||
149 | daniel-mar | 22 | min_password_length = 10; // see also plugins/publicPages/092_forgot_password_admin/script.js |
421 | daniel-mar | 23 | password_salt_length = 10; |
80 | daniel-mar | 24 | |
2 | daniel-mar | 25 | function btoa(bin) { |
26 | var tableStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
||
27 | var table = tableStr.split(""); |
||
28 | for (var i = 0, j = 0, len = bin.length / 3, base64 = []; i < len; ++i) { |
||
29 | var a = bin.charCodeAt(j++), b = bin.charCodeAt(j++), c = bin.charCodeAt(j++); |
||
362 | daniel-mar | 30 | if ((a | b | c) > 255) throw new Error(_L('String contains an invalid character')); |
2 | daniel-mar | 31 | base64[base64.length] = table[a >> 2] + table[((a << 4) & 63) | (b >> 4)] + |
32 | (isNaN(b) ? "=" : table[((b << 2) & 63) | (c >> 6)]) + |
||
33 | (isNaN(b + c) ? "=" : table[c & 63]); |
||
34 | } |
||
35 | return base64.join(""); |
||
36 | }; |
||
37 | |||
38 | function hexToBase64(str) { |
||
39 | return btoa(String.fromCharCode.apply(null, |
||
40 | str.replace(/\r|\n/g, "").replace(/([\da-fA-F]{2}) ?/g, "0x$1 ").replace(/ +$/, "").split(" "))); |
||
41 | } |
||
42 | |||
43 | function b64EncodeUnicode(str) { |
||
44 | // first we use encodeURIComponent to get percent-encoded UTF-8, |
||
45 | // then we convert the percent encodings into raw bytes which |
||
46 | // can be fed into btoa. |
||
47 | return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, |
||
48 | function toSolidBytes(match, p1) { |
||
49 | return String.fromCharCode('0x' + p1); |
||
50 | })); |
||
51 | } |
||
52 | |||
53 | function generateRandomString(length) { |
||
54 | var charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", |
||
55 | retVal = ""; |
||
56 | for (var i = 0, n = charset.length; i < length; ++i) { |
||
57 | retVal += charset.charAt(Math.floor(Math.random() * n)); |
||
58 | } |
||
59 | return retVal; |
||
60 | } |
||
61 | |||
62 | String.prototype.replaceAll = function(search, replacement) { |
||
63 | var target = this; |
||
64 | return target.replace(new RegExp(search, 'g'), replacement); |
||
65 | }; |
||
66 | |||
421 | daniel-mar | 67 | function adminGeneratePassword(password) { |
68 | var salt = generateRandomString(password_salt_length); |
||
69 | return salt+'$'+hexToBase64(sha3_512(salt+password)); |
||
70 | } |
||
71 | |||
456 | daniel-mar | 72 | var bCryptWorker = null; |
73 | var g_prevBcryptPw = null; |
||
74 | var g_last_admPwdHash = null; |
||
75 | var g_last_pwComment = null; |
||
76 | |||
2 | daniel-mar | 77 | function rebuild() { |
456 | daniel-mar | 78 | var pw = document.getElementById('admin_password').value; |
79 | |||
80 | if (pw != g_prevBcryptPw) { |
||
81 | // sync call to calculate SHA3 |
||
82 | var admPwdHash = adminGeneratePassword(pw); |
||
83 | var pwComment = 'salted, base64 encoded SHA3-512 hash'; |
||
84 | doRebuild(admPwdHash, pwComment); |
||
85 | |||
86 | // "async" call to calculate bcrypt (via web-worker) |
||
87 | if (bCryptWorker != null) { |
||
88 | g_prevBcryptPw = null; |
||
89 | bCryptWorker.terminate(); |
||
90 | } |
||
91 | bCryptWorker = new Worker('bcrypt_worker.js'); |
||
92 | bCryptWorker.postMessage(pw); |
||
93 | bCryptWorker.onmessage = function (event) { |
||
94 | var admPwdHash = event.data; |
||
95 | var pwComment = 'bcrypt encoded hash'; |
||
96 | doRebuild(admPwdHash, pwComment); |
||
97 | g_prevBcryptPw = pw; |
||
98 | }; |
||
99 | } else { |
||
100 | doRebuild(g_last_admPwdHash, g_last_pwComment); |
||
101 | } |
||
102 | } |
||
103 | |||
104 | function doRebuild(admPwdHash, pwComment) { |
||
105 | g_last_admPwdHash = admPwdHash; |
||
106 | g_last_pwComment = pwComment; |
||
107 | |||
2 | daniel-mar | 108 | var error = false; |
109 | |||
150 | daniel-mar | 110 | if (document.getElementById('config') == null) return; |
111 | |||
81 | daniel-mar | 112 | // Check 1: Has the password the correct length? |
113 | if (document.getElementById('admin_password').value.length < min_password_length) |
||
114 | { |
||
362 | daniel-mar | 115 | document.getElementById('password_warn').innerHTML = '<font color="red">'+_L('Password must be at least %1 characters long',min_password_length)+'</font>'; |
116 | document.getElementById('config').innerHTML = '<b><?php</b><br><br><i>// ERROR: Password must be at least '+min_password_length+' characters long</i>'; // do not translate |
||
81 | daniel-mar | 117 | error = true; |
118 | } else { |
||
119 | document.getElementById('password_warn').innerHTML = ''; |
||
120 | } |
||
121 | |||
122 | // Check 2: Do the passwords match? |
||
2 | daniel-mar | 123 | if (document.getElementById('admin_password').value != document.getElementById('admin_password2').value) { |
362 | daniel-mar | 124 | document.getElementById('password_warn2').innerHTML = '<font color="red">'+_L('The passwords do not match!')+'</font>'; |
2 | daniel-mar | 125 | error = true; |
126 | } else { |
||
127 | document.getElementById('password_warn2').innerHTML = ''; |
||
128 | } |
||
129 | |||
150 | daniel-mar | 130 | // Check 3: Ask the database plugins for verification of their data |
131 | for (var i = 0; i < rebuild_callbacks.length; i++) { |
||
132 | var f = rebuild_callbacks[i]; |
||
133 | if (!f()) { |
||
134 | error = true; |
||
135 | } |
||
2 | daniel-mar | 136 | } |
81 | daniel-mar | 137 | |
149 | daniel-mar | 138 | // Continue |
81 | daniel-mar | 139 | if (!error) |
140 | { |
||
150 | daniel-mar | 141 | var e = document.getElementById("db_plugin"); |
142 | var strPlugin = e.options[e.selectedIndex].value; |
||
143 | |||
2 | daniel-mar | 144 | document.getElementById('config').innerHTML = '<b><?php</b><br><br>' + |
362 | daniel-mar | 145 | '<i>// To renew this file, please run setup/ in your browser.</i><br>' + // do not translate |
146 | '<i>// If you don\'t want to run setup again, you can also change most of the settings directly in this file.</i><br>' + // do not translate |
||
2 | daniel-mar | 147 | '<br>' + |
261 | daniel-mar | 148 | 'OIDplus::baseConfig()->setValue(\'CONFIG_VERSION\', 2.1);<br>' + |
2 | daniel-mar | 149 | '<br>' + |
150 | daniel-mar | 150 | // Passwords are Base64 encoded to avoid that passwords can be read upon first sight, |
294 | daniel-mar | 151 | // e.g. if collegues are looking over your shoulder while you accidently open (and quickly close) userdata/baseconfig/config.inc.php |
456 | daniel-mar | 152 | 'OIDplus::baseConfig()->setValue(\'ADMIN_PASSWORD\', \'' + admPwdHash + '\'); // '+pwComment+'<br>' + |
2 | daniel-mar | 153 | '<br>' + |
261 | daniel-mar | 154 | 'OIDplus::baseConfig()->setValue(\'DATABASE_PLUGIN\', \''+strPlugin+'\');<br>'; |
150 | daniel-mar | 155 | for (var i = 0; i < rebuild_config_callbacks.length; i++) { |
156 | var f = rebuild_config_callbacks[i]; |
||
157 | var cont = f(); |
||
158 | if (cont) { |
||
159 | document.getElementById('config').innerHTML = document.getElementById('config').innerHTML + cont; |
||
160 | } |
||
161 | } |
||
239 | daniel-mar | 162 | document.getElementById('config').innerHTML = document.getElementById('config').innerHTML + |
2 | daniel-mar | 163 | '<br>' + |
261 | daniel-mar | 164 | 'OIDplus::baseConfig()->setValue(\'TABLENAME_PREFIX\', \''+document.getElementById('tablename_prefix').value+'\');<br>' + |
2 | daniel-mar | 165 | '<br>' + |
261 | daniel-mar | 166 | 'OIDplus::baseConfig()->setValue(\'SERVER_SECRET\', \''+generateRandomString(32)+'\');<br>' + |
2 | daniel-mar | 167 | '<br>' + |
261 | daniel-mar | 168 | 'OIDplus::baseConfig()->setValue(\'RECAPTCHA_ENABLED\', '+(document.getElementById('recaptcha_enabled').checked ? 'true' : 'false')+');<br>' + |
169 | 'OIDplus::baseConfig()->setValue(\'RECAPTCHA_PUBLIC\', \''+document.getElementById('recaptcha_public').value+'\');<br>' + |
||
170 | 'OIDplus::baseConfig()->setValue(\'RECAPTCHA_PRIVATE\', \''+document.getElementById('recaptcha_private').value+'\');<br>' + |
||
80 | daniel-mar | 171 | '<br>' + |
261 | daniel-mar | 172 | 'OIDplus::baseConfig()->setValue(\'ENFORCE_SSL\', '+document.getElementById('enforce_ssl').value+');<br>'; |
2 | daniel-mar | 173 | |
174 | document.getElementById('config').innerHTML = document.getElementById('config').innerHTML.replaceAll(' ', ' '); |
||
175 | } |
||
176 | |||
177 | // In case something is not good, do not allow the user to continue with the other configuration steps: |
||
178 | if (error) { |
||
150 | daniel-mar | 179 | document.getElementById('step2').style.display = "None"; |
180 | document.getElementById('step3').style.display = "None"; |
||
181 | document.getElementById('step4').style.display = "None"; |
||
2 | daniel-mar | 182 | } else { |
150 | daniel-mar | 183 | document.getElementById('step2').style.display = "Block"; |
184 | document.getElementById('step3').style.display = "Block"; |
||
185 | document.getElementById('step4').style.display = "Block"; |
||
2 | daniel-mar | 186 | } |
187 | } |
||
362 | daniel-mar | 188 | |
189 | function RemoveLastDirectoryPartOf(the_url) { |
||
190 | var the_arr = the_url.split('/'); |
||
191 | if (the_arr.pop() == '') the_arr.pop(); |
||
192 | return( the_arr.join('/') ); |
||
193 | } |
||
194 | |||
195 | function checkAccess(dir) { |
||
196 | var url = '../' + dir; |
||
197 | var visibleUrl = RemoveLastDirectoryPartOf(window.location.href) + '/' + dir; // xhr.responseURL not available in IE |
||
198 | |||
199 | var xhr = new XMLHttpRequest(); |
||
200 | xhr.onreadystatechange = function() { |
||
201 | if (xhr.readyState === 4) { |
||
202 | if (xhr.status === 200) { |
||
203 | document.getElementById('systemCheckCaption').style.display = 'block'; |
||
363 | daniel-mar | 204 | document.getElementById('dirAccessWarning').innerHTML = document.getElementById('dirAccessWarning').innerHTML + _L('Attention: The following directory is world-readable: %1 ! You need to configure your web server to restrict access to this directory! (For Apache see <i>.htaccess</i>, for Microsoft IIS see <i>web.config</i>, for Nginx see <i>nginx.conf</i>).','<a target="_blank" href="'+url+'">'+visibleUrl+'</a>') + '<br>'; |
362 | daniel-mar | 205 | } |
206 | } |
||
207 | }; |
||
208 | |||
209 | xhr.open('GET', url); |
||
210 | xhr.send(); |
||
211 | } |
||
212 | |||
213 | function dbplugin_changed() { |
||
214 | var e = document.getElementById("db_plugin"); |
||
215 | var strPlugin = e.options[e.selectedIndex].value; |
||
216 | |||
217 | for (var i = 0; i < plugin_combobox_change_callbacks.length; i++) { |
||
218 | var f = plugin_combobox_change_callbacks[i]; |
||
219 | f(strPlugin); |
||
220 | } |
||
221 | |||
222 | rebuild(); |
||
223 | } |
||
224 | |||
225 | function performAccessCheck() { |
||
226 | document.getElementById("dirAccessWarning").innerHTML = ""; |
||
227 | checkAccess("userdata/index.html"); |
||
228 | checkAccess("dev/index.html"); |
||
229 | checkAccess("includes/index.html"); |
||
448 | daniel-mar | 230 | checkAccess("setup/includes/index.html"); |
362 | daniel-mar | 231 | //checkAccess("plugins/publicPages/100_whois/whois/cli/index.html"); |
232 | } |
||
233 | |||
234 | function setupOnLoad() { |
||
235 | rebuild(); |
||
236 | dbplugin_changed(); |
||
237 | performAccessCheck(); |
||
238 | } |
||
239 | |||
240 | function getCookie(cname) { |
||
241 | // Source: https://www.w3schools.com/js/js_cookies.asp |
||
242 | var name = cname + "="; |
||
243 | var decodedCookie = decodeURIComponent(document.cookie); |
||
244 | var ca = decodedCookie.split(';'); |
||
245 | for(var i = 0; i <ca.length; i++) { |
||
246 | var c = ca[i]; |
||
247 | while (c.charAt(0) == ' ') { |
||
248 | c = c.substring(1); |
||
249 | } |
||
250 | if (c.indexOf(name) == 0) { |
||
251 | return c.substring(name.length, c.length); |
||
252 | } |
||
253 | } |
||
254 | return undefined; |
||
255 | } |
||
256 | |||
257 | function getCurrentLang() { |
||
258 | // Note: If the argument "?lang=" is used, PHP will automatically set a Cookie, so it is OK when we only check for the cookie |
||
259 | var lang = getCookie('LANGUAGE'); |
||
260 | return (typeof lang != "undefined") ? lang : DEFAULT_LANGUAGE; |
||
261 | } |
||
262 | |||
263 | function _L() { |
||
264 | var args = Array.prototype.slice.call(arguments); |
||
265 | var str = args.shift(); |
||
266 | |||
267 | var tmp = ""; |
||
268 | if (typeof language_messages[getCurrentLang()] == "undefined") { |
||
269 | tmp = str; |
||
270 | } else { |
||
271 | var msg = language_messages[getCurrentLang()][str]; |
||
272 | if (typeof msg != "undefined") { |
||
273 | tmp = msg; |
||
274 | } else { |
||
275 | tmp = str; |
||
276 | } |
||
277 | } |
||
278 | |||
279 | tmp = tmp.replace('###', language_tblprefix); |
||
280 | |||
281 | var n = 1; |
||
282 | while (args.length > 0) { |
||
283 | var val = args.shift(); |
||
284 | tmp = tmp.replace("%"+n, val); |
||
285 | n++; |
||
286 | } |
||
287 | |||
370 | daniel-mar | 288 | tmp = tmp.replace("%%", "%"); |
289 | |||
362 | daniel-mar | 290 | return tmp; |
291 | } |
||
292 | |||
293 | window.onload = setupOnLoad; |