Rev 702 | Rev 861 | 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 |
||
511 | daniel-mar | 3 | * Copyright 2019 - 2021 Daniel Marschall, ViaThinkSoft |
5 | daniel-mar | 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 | |||
695 | daniel-mar | 22 | // TODO: Put these settings in a "setup configuration file" (hardcoded) |
635 | daniel-mar | 23 | min_password_length = 10; // see also plugins/viathinksoft/publicPages/092_forgot_password_admin/script.js |
421 | daniel-mar | 24 | password_salt_length = 10; |
695 | daniel-mar | 25 | bcrypt_rounds = 10; |
80 | daniel-mar | 26 | |
2 | daniel-mar | 27 | function btoa(bin) { |
28 | var tableStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
||
29 | var table = tableStr.split(""); |
||
30 | for (var i = 0, j = 0, len = bin.length / 3, base64 = []; i < len; ++i) { |
||
31 | var a = bin.charCodeAt(j++), b = bin.charCodeAt(j++), c = bin.charCodeAt(j++); |
||
362 | daniel-mar | 32 | if ((a | b | c) > 255) throw new Error(_L('String contains an invalid character')); |
2 | daniel-mar | 33 | base64[base64.length] = table[a >> 2] + table[((a << 4) & 63) | (b >> 4)] + |
34 | (isNaN(b) ? "=" : table[((b << 2) & 63) | (c >> 6)]) + |
||
35 | (isNaN(b + c) ? "=" : table[c & 63]); |
||
36 | } |
||
37 | return base64.join(""); |
||
38 | }; |
||
39 | |||
40 | function hexToBase64(str) { |
||
41 | return btoa(String.fromCharCode.apply(null, |
||
42 | str.replace(/\r|\n/g, "").replace(/([\da-fA-F]{2}) ?/g, "0x$1 ").replace(/ +$/, "").split(" "))); |
||
43 | } |
||
44 | |||
45 | function b64EncodeUnicode(str) { |
||
46 | // first we use encodeURIComponent to get percent-encoded UTF-8, |
||
47 | // then we convert the percent encodings into raw bytes which |
||
48 | // can be fed into btoa. |
||
49 | return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, |
||
50 | function toSolidBytes(match, p1) { |
||
51 | return String.fromCharCode('0x' + p1); |
||
52 | })); |
||
53 | } |
||
54 | |||
55 | function generateRandomString(length) { |
||
56 | var charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", |
||
57 | retVal = ""; |
||
58 | for (var i = 0, n = charset.length; i < length; ++i) { |
||
59 | retVal += charset.charAt(Math.floor(Math.random() * n)); |
||
60 | } |
||
61 | return retVal; |
||
62 | } |
||
63 | |||
64 | String.prototype.replaceAll = function(search, replacement) { |
||
65 | var target = this; |
||
66 | return target.replace(new RegExp(search, 'g'), replacement); |
||
67 | }; |
||
68 | |||
421 | daniel-mar | 69 | function adminGeneratePassword(password) { |
70 | var salt = generateRandomString(password_salt_length); |
||
71 | return salt+'$'+hexToBase64(sha3_512(salt+password)); |
||
72 | } |
||
73 | |||
456 | daniel-mar | 74 | var bCryptWorker = null; |
75 | var g_prevBcryptPw = null; |
||
76 | var g_last_admPwdHash = null; |
||
77 | var g_last_pwComment = null; |
||
78 | |||
2 | daniel-mar | 79 | function rebuild() { |
561 | daniel-mar | 80 | var pw = $("#admin_password")[0].value; |
456 | daniel-mar | 81 | |
82 | if (pw != g_prevBcryptPw) { |
||
83 | // sync call to calculate SHA3 |
||
84 | var admPwdHash = adminGeneratePassword(pw); |
||
85 | var pwComment = 'salted, base64 encoded SHA3-512 hash'; |
||
86 | doRebuild(admPwdHash, pwComment); |
||
87 | |||
88 | // "async" call to calculate bcrypt (via web-worker) |
||
89 | if (bCryptWorker != null) { |
||
90 | g_prevBcryptPw = null; |
||
91 | bCryptWorker.terminate(); |
||
92 | } |
||
599 | daniel-mar | 93 | bCryptWorker = new Worker('../bcrypt_worker.js'); |
695 | daniel-mar | 94 | bCryptWorker.postMessage([pw, bcrypt_rounds]); |
456 | daniel-mar | 95 | bCryptWorker.onmessage = function (event) { |
96 | var admPwdHash = event.data; |
||
97 | var pwComment = 'bcrypt encoded hash'; |
||
98 | doRebuild(admPwdHash, pwComment); |
||
99 | g_prevBcryptPw = pw; |
||
100 | }; |
||
101 | } else { |
||
102 | doRebuild(g_last_admPwdHash, g_last_pwComment); |
||
103 | } |
||
104 | } |
||
105 | |||
106 | function doRebuild(admPwdHash, pwComment) { |
||
107 | g_last_admPwdHash = admPwdHash; |
||
108 | g_last_pwComment = pwComment; |
||
109 | |||
2 | daniel-mar | 110 | var error = false; |
111 | |||
561 | daniel-mar | 112 | if ($("#config")[0] == null) return; |
150 | daniel-mar | 113 | |
81 | daniel-mar | 114 | // Check 1: Has the password the correct length? |
561 | daniel-mar | 115 | if ($("#admin_password")[0].value.length < min_password_length) |
81 | daniel-mar | 116 | { |
561 | daniel-mar | 117 | $("#password_warn")[0].innerHTML = '<font color="red">'+_L('Password must be at least %1 characters long',min_password_length)+'</font>'; |
118 | $("#config")[0].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 | 119 | error = true; |
120 | } else { |
||
561 | daniel-mar | 121 | $("#password_warn")[0].innerHTML = ''; |
81 | daniel-mar | 122 | } |
123 | |||
124 | // Check 2: Do the passwords match? |
||
561 | daniel-mar | 125 | if ($("#admin_password")[0].value != $("#admin_password2")[0].value) { |
126 | $("#password_warn2")[0].innerHTML = '<font color="red">'+_L('The passwords do not match!')+'</font>'; |
||
2 | daniel-mar | 127 | error = true; |
128 | } else { |
||
561 | daniel-mar | 129 | $("#password_warn2")[0].innerHTML = ''; |
2 | daniel-mar | 130 | } |
131 | |||
702 | daniel-mar | 132 | // Check 3: Ask the database or captcha plugins for verification of their data |
150 | daniel-mar | 133 | for (var i = 0; i < rebuild_callbacks.length; i++) { |
134 | var f = rebuild_callbacks[i]; |
||
135 | if (!f()) { |
||
136 | error = true; |
||
137 | } |
||
2 | daniel-mar | 138 | } |
81 | daniel-mar | 139 | |
149 | daniel-mar | 140 | // Continue |
81 | daniel-mar | 141 | if (!error) |
142 | { |
||
561 | daniel-mar | 143 | var e = $("#db_plugin")[0]; |
702 | daniel-mar | 144 | var strDatabasePlugin = e.options[e.selectedIndex].value; |
145 | var e = $("#captcha_plugin")[0]; |
||
146 | var strCaptchaPlugin = e.options[e.selectedIndex].value; |
||
150 | daniel-mar | 147 | |
561 | daniel-mar | 148 | $("#config")[0].innerHTML = '<b><?php</b><br><br>' + |
362 | daniel-mar | 149 | '<i>// To renew this file, please run setup/ in your browser.</i><br>' + // do not translate |
150 | '<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 | 151 | '<br>' + |
261 | daniel-mar | 152 | 'OIDplus::baseConfig()->setValue(\'CONFIG_VERSION\', 2.1);<br>' + |
2 | daniel-mar | 153 | '<br>' + |
150 | daniel-mar | 154 | // Passwords are Base64 encoded to avoid that passwords can be read upon first sight, |
294 | daniel-mar | 155 | // e.g. if collegues are looking over your shoulder while you accidently open (and quickly close) userdata/baseconfig/config.inc.php |
456 | daniel-mar | 156 | 'OIDplus::baseConfig()->setValue(\'ADMIN_PASSWORD\', \'' + admPwdHash + '\'); // '+pwComment+'<br>' + |
2 | daniel-mar | 157 | '<br>' + |
702 | daniel-mar | 158 | 'OIDplus::baseConfig()->setValue(\'DATABASE_PLUGIN\', \''+strDatabasePlugin+'\');<br>'; |
150 | daniel-mar | 159 | for (var i = 0; i < rebuild_config_callbacks.length; i++) { |
160 | var f = rebuild_config_callbacks[i]; |
||
161 | var cont = f(); |
||
162 | if (cont) { |
||
561 | daniel-mar | 163 | $("#config")[0].innerHTML = $("#config")[0].innerHTML + cont; |
150 | daniel-mar | 164 | } |
165 | } |
||
561 | daniel-mar | 166 | $("#config")[0].innerHTML = $("#config")[0].innerHTML + |
2 | daniel-mar | 167 | '<br>' + |
561 | daniel-mar | 168 | 'OIDplus::baseConfig()->setValue(\'TABLENAME_PREFIX\', \''+$("#tablename_prefix")[0].value+'\');<br>' + |
2 | daniel-mar | 169 | '<br>' + |
261 | daniel-mar | 170 | 'OIDplus::baseConfig()->setValue(\'SERVER_SECRET\', \''+generateRandomString(32)+'\');<br>' + |
2 | daniel-mar | 171 | '<br>' + |
702 | daniel-mar | 172 | 'OIDplus::baseConfig()->setValue(\'CAPTCHA_PLUGIN\', \''+strCaptchaPlugin+'\');<br>'; |
173 | for (var i = 0; i < captcha_rebuild_config_callbacks.length; i++) { |
||
174 | var f = captcha_rebuild_config_callbacks[i]; |
||
175 | var cont = f(); |
||
176 | if (cont) { |
||
177 | $("#config")[0].innerHTML = $("#config")[0].innerHTML + cont; |
||
178 | } |
||
179 | } |
||
180 | |||
181 | $("#config")[0].innerHTML = $("#config")[0].innerHTML + |
||
80 | daniel-mar | 182 | '<br>' + |
561 | daniel-mar | 183 | 'OIDplus::baseConfig()->setValue(\'ENFORCE_SSL\', '+$("#enforce_ssl")[0].value+');<br>'; |
2 | daniel-mar | 184 | |
561 | daniel-mar | 185 | $("#config")[0].innerHTML = $("#config")[0].innerHTML.replaceAll(' ', ' '); |
2 | daniel-mar | 186 | } |
187 | |||
188 | // In case something is not good, do not allow the user to continue with the other configuration steps: |
||
189 | if (error) { |
||
561 | daniel-mar | 190 | $("#step2")[0].style.display = "None"; |
191 | $("#step3")[0].style.display = "None"; |
||
192 | $("#step4")[0].style.display = "None"; |
||
2 | daniel-mar | 193 | } else { |
561 | daniel-mar | 194 | $("#step2")[0].style.display = "Block"; |
195 | $("#step3")[0].style.display = "Block"; |
||
196 | $("#step4")[0].style.display = "Block"; |
||
2 | daniel-mar | 197 | } |
198 | } |
||
362 | daniel-mar | 199 | |
200 | function RemoveLastDirectoryPartOf(the_url) { |
||
201 | var the_arr = the_url.split('/'); |
||
202 | if (the_arr.pop() == '') the_arr.pop(); |
||
203 | return( the_arr.join('/') ); |
||
204 | } |
||
205 | |||
206 | function checkAccess(dir) { |
||
706 | daniel-mar | 207 | if (!dir.toLowerCase().startsWith('https:') && !dir.toLowerCase().startsWith('http:')) { |
208 | var url = '../' + dir; |
||
209 | var visibleUrl = RemoveLastDirectoryPartOf(window.location.href) + '/' + dir; // xhr.responseURL not available in IE |
||
210 | } else { |
||
211 | var url = dir; |
||
212 | var visibleUrl = dir; // xhr.responseURL not available in IE |
||
213 | } |
||
362 | daniel-mar | 214 | |
215 | var xhr = new XMLHttpRequest(); |
||
216 | xhr.onreadystatechange = function() { |
||
217 | if (xhr.readyState === 4) { |
||
218 | if (xhr.status === 200) { |
||
561 | daniel-mar | 219 | $("#systemCheckCaption")[0].style.display = 'block'; |
220 | $("#dirAccessWarning")[0].innerHTML = $("#dirAccessWarning")[0].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 | 221 | } |
222 | } |
||
223 | }; |
||
224 | |||
225 | xhr.open('GET', url); |
||
226 | xhr.send(); |
||
227 | } |
||
228 | |||
229 | function dbplugin_changed() { |
||
561 | daniel-mar | 230 | var e = $("#db_plugin")[0]; |
702 | daniel-mar | 231 | var strDatabasePlugin = e.options[e.selectedIndex].value; |
362 | daniel-mar | 232 | |
233 | for (var i = 0; i < plugin_combobox_change_callbacks.length; i++) { |
||
234 | var f = plugin_combobox_change_callbacks[i]; |
||
702 | daniel-mar | 235 | f(strDatabasePlugin); |
362 | daniel-mar | 236 | } |
237 | |||
238 | rebuild(); |
||
239 | } |
||
240 | |||
702 | daniel-mar | 241 | function captchaplugin_changed() { |
242 | var e = $("#captcha_plugin")[0]; |
||
243 | var strCaptchaPlugin = e.options[e.selectedIndex].value; |
||
244 | |||
245 | for (var i = 0; i < captcha_plugin_combobox_change_callbacks.length; i++) { |
||
246 | var f = captcha_plugin_combobox_change_callbacks[i]; |
||
247 | f(strCaptchaPlugin); |
||
248 | } |
||
249 | |||
250 | rebuild(); |
||
251 | } |
||
252 | |||
362 | daniel-mar | 253 | function performAccessCheck() { |
561 | daniel-mar | 254 | $("#dirAccessWarning")[0].innerHTML = ""; |
362 | daniel-mar | 255 | checkAccess("userdata/index.html"); |
476 | daniel-mar | 256 | checkAccess("res/ATTENTION.TXT"); |
362 | daniel-mar | 257 | checkAccess("dev/index.html"); |
258 | checkAccess("includes/index.html"); |
||
448 | daniel-mar | 259 | checkAccess("setup/includes/index.html"); |
635 | daniel-mar | 260 | //checkAccess("plugins/viathinksoft/publicPages/100_whois/whois/cli/index.html"); |
706 | daniel-mar | 261 | |
262 | if (window.location.href.toLowerCase().startsWith('https://')) { |
||
263 | $("#enforce_ssl").val(1); // enforce SSL (because we are already SSL) |
||
264 | } else { |
||
265 | // Do a SSL detection now. |
||
266 | // This is important because on XAMPP the SSL cert is invalid (self signed) and the user might |
||
267 | // be very confused if the PHP detection noticed the open 443 port and redirects the user to a |
||
268 | // big warning page of the browser! |
||
269 | var xhr = new XMLHttpRequest(); |
||
270 | xhr.onreadystatechange = function() { |
||
271 | if (xhr.readyState === 4) { |
||
272 | if (xhr.status === 200) { |
||
273 | $("#enforce_ssl").val(1); // enforce SSL (we checked that it loads correctly) |
||
274 | } else { |
||
275 | console.log("JS SSL detection result: "+xhr.status); |
||
276 | $("#enforce_ssl").val(0); // disable SSL (because it failed, e.g. because of invalid cert or closed port) |
||
277 | } |
||
278 | } |
||
279 | }; |
||
280 | var https_url = window.location.href.replace(/^http:/i, "https:"); |
||
281 | xhr.open('GET', https_url); |
||
282 | xhr.send(); |
||
283 | } |
||
362 | daniel-mar | 284 | } |
285 | |||
286 | function setupOnLoad() { |
||
287 | rebuild(); |
||
288 | dbplugin_changed(); |
||
702 | daniel-mar | 289 | captchaplugin_changed(); |
362 | daniel-mar | 290 | performAccessCheck(); |
291 | } |
||
292 | |||
293 | function getCookie(cname) { |
||
294 | // Source: https://www.w3schools.com/js/js_cookies.asp |
||
295 | var name = cname + "="; |
||
296 | var decodedCookie = decodeURIComponent(document.cookie); |
||
297 | var ca = decodedCookie.split(';'); |
||
298 | for(var i = 0; i <ca.length; i++) { |
||
299 | var c = ca[i]; |
||
300 | while (c.charAt(0) == ' ') { |
||
301 | c = c.substring(1); |
||
302 | } |
||
303 | if (c.indexOf(name) == 0) { |
||
304 | return c.substring(name.length, c.length); |
||
305 | } |
||
306 | } |
||
307 | return undefined; |
||
308 | } |
||
309 | |||
310 | function getCurrentLang() { |
||
311 | // Note: If the argument "?lang=" is used, PHP will automatically set a Cookie, so it is OK when we only check for the cookie |
||
312 | var lang = getCookie('LANGUAGE'); |
||
313 | return (typeof lang != "undefined") ? lang : DEFAULT_LANGUAGE; |
||
314 | } |
||
315 | |||
316 | function _L() { |
||
317 | var args = Array.prototype.slice.call(arguments); |
||
506 | daniel-mar | 318 | var str = args.shift().trim(); |
362 | daniel-mar | 319 | |
320 | var tmp = ""; |
||
321 | if (typeof language_messages[getCurrentLang()] == "undefined") { |
||
322 | tmp = str; |
||
323 | } else { |
||
324 | var msg = language_messages[getCurrentLang()][str]; |
||
325 | if (typeof msg != "undefined") { |
||
326 | tmp = msg; |
||
327 | } else { |
||
328 | tmp = str; |
||
329 | } |
||
330 | } |
||
331 | |||
332 | tmp = tmp.replace('###', language_tblprefix); |
||
333 | |||
334 | var n = 1; |
||
335 | while (args.length > 0) { |
||
336 | var val = args.shift(); |
||
337 | tmp = tmp.replace("%"+n, val); |
||
338 | n++; |
||
339 | } |
||
340 | |||
370 | daniel-mar | 341 | tmp = tmp.replace("%%", "%"); |
342 | |||
362 | daniel-mar | 343 | return tmp; |
344 | } |
||
345 | |||
346 | window.onload = setupOnLoad; |