security.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. <?php
  2. /* Copyright (C) 2004-2022 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2005-2007 Regis Houssin <regis.houssin@inodbox.com>
  4. * Copyright (C) 2013-2015 Juanjo Menent <jmenent@2byte.es>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/admin/security.php
  21. * \ingroup setup
  22. * \brief Page de configuration du module securite
  23. */
  24. // Load Dolibarr environment
  25. require '../main.inc.php';
  26. require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
  28. $action = GETPOST('action', 'aZ09');
  29. // Load translation files required by the page
  30. $langs->loadLangs(array("users", "admin", "other"));
  31. if (!$user->admin) {
  32. accessforbidden();
  33. }
  34. // Allow/Disallow change to clear passwords once passwords are crypted
  35. $allow_disable_encryption = true;
  36. /*
  37. * Actions
  38. */
  39. if ($action == 'setgeneraterule') {
  40. if (!dolibarr_set_const($db, 'USER_PASSWORD_GENERATED', GETPOST("value", "alphanohtml"), 'chaine', 0, '', $conf->entity)) {
  41. dol_print_error($db);
  42. }
  43. }
  44. if ($action == 'activate_encrypt') {
  45. $error = 0;
  46. $db->begin();
  47. // On old version, a bug created the constant into user entity, so we delete it to be sure such entry won't exists. We want it in entity 0 or nowhere.
  48. dolibarr_del_const($db, "DATABASE_PWD_ENCRYPTED", $conf->entity);
  49. // We set entity=0 (all) because DATABASE_PWD_ENCRYPTED is a setup into conf file, so always shared for everybody
  50. $entityforall = 0;
  51. dolibarr_set_const($db, "DATABASE_PWD_ENCRYPTED", "1", 'chaine', 0, '', $entityforall);
  52. $sql = "SELECT u.rowid, u.pass, u.pass_crypted";
  53. $sql .= " FROM ".MAIN_DB_PREFIX."user as u";
  54. $sql .= " WHERE u.pass IS NOT NULL AND LENGTH(u.pass) < 32"; // Not a MD5 value
  55. $resql = $db->query($sql);
  56. if ($resql) {
  57. $numrows = $db->num_rows($resql);
  58. $i = 0;
  59. while ($i < $numrows) {
  60. $obj = $db->fetch_object($resql);
  61. if (dol_hash($obj->pass)) {
  62. $sql = "UPDATE ".MAIN_DB_PREFIX."user";
  63. $sql .= " SET pass_crypted = '".dol_hash($obj->pass)."', pass = NULL";
  64. $sql .= " WHERE rowid=".((int) $obj->rowid);
  65. //print $sql;
  66. $resql2 = $db->query($sql);
  67. if (!$resql2) {
  68. dol_print_error($db);
  69. $error++;
  70. break;
  71. }
  72. $i++;
  73. }
  74. }
  75. } else {
  76. dol_print_error($db);
  77. }
  78. //print $error." ".$sql;
  79. //exit;
  80. if (!$error) {
  81. $db->commit();
  82. } else {
  83. $db->rollback();
  84. dol_print_error($db, '');
  85. }
  86. } elseif ($action == 'disable_encrypt') {
  87. //On n'autorise pas l'annulation de l'encryption car les mots de passe ne peuvent pas etre decodes
  88. //Do not allow "disable encryption" as passwords cannot be decrypted
  89. if ($allow_disable_encryption) {
  90. dolibarr_del_const($db, "DATABASE_PWD_ENCRYPTED", $conf->entity);
  91. }
  92. }
  93. if ($action == 'activate_encryptdbpassconf') {
  94. $result = encodedecode_dbpassconf(1);
  95. if ($result > 0) {
  96. sleep(3); // Don't know why but we need to wait file is completely saved before making the reload. Even with flush and clearstatcache, we need to wait.
  97. // database value not required
  98. //dolibarr_set_const($db, "MAIN_DATABASE_PWD_CONFIG_ENCRYPTED", "1");
  99. header("Location: security.php");
  100. exit;
  101. } else {
  102. setEventMessages($langs->trans('InstrucToEncodePass', dol_encode($dolibarr_main_db_pass)), null, 'warnings');
  103. }
  104. } elseif ($action == 'disable_encryptdbpassconf') {
  105. $result = encodedecode_dbpassconf(0);
  106. if ($result > 0) {
  107. sleep(3); // Don't know why but we need to wait file is completely saved before making the reload. Even with flush and clearstatcache, we need to wait.
  108. // database value not required
  109. //dolibarr_del_const($db, "MAIN_DATABASE_PWD_CONFIG_ENCRYPTED",$conf->entity);
  110. header("Location: security.php");
  111. exit;
  112. } else {
  113. setEventMessages($langs->trans('InstrucToClearPass', $dolibarr_main_db_pass), null, 'warnings');
  114. }
  115. }
  116. if ($action == 'activate_MAIN_SECURITY_DISABLEFORGETPASSLINK') {
  117. dolibarr_set_const($db, "MAIN_SECURITY_DISABLEFORGETPASSLINK", '1', 'chaine', 0, '', $conf->entity);
  118. } elseif ($action == 'disable_MAIN_SECURITY_DISABLEFORGETPASSLINK') {
  119. dolibarr_del_const($db, "MAIN_SECURITY_DISABLEFORGETPASSLINK", $conf->entity);
  120. }
  121. if ($action == 'updatepattern') {
  122. $pattern = GETPOST("pattern", "alpha");
  123. $explodePattern = explode(';', $pattern);
  124. $patternInError = false;
  125. if ($explodePattern[0] < 1 || $explodePattern[4] < 0) {
  126. $patternInError = true;
  127. }
  128. if ($explodePattern[0] < $explodePattern[1] + $explodePattern[2] + $explodePattern[3]) {
  129. $patternInError = true;
  130. }
  131. if (!$patternInError) {
  132. dolibarr_set_const($db, "USER_PASSWORD_PATTERN", $pattern, 'chaine', 0, '', $conf->entity);
  133. setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
  134. header("Location: security.php");
  135. exit;
  136. }
  137. }
  138. /*
  139. * View
  140. */
  141. $form = new Form($db);
  142. $wikihelp = 'EN:Setup_Security|FR:Paramétrage_Sécurité|ES:Configuración_Seguridad';
  143. llxHeader('', $langs->trans("Passwords"), $wikihelp);
  144. print load_fiche_titre($langs->trans("SecuritySetup"), '', 'title_setup');
  145. print '<span class="opacitymedium">'.$langs->trans("GeneratedPasswordDesc")."</span><br>\n";
  146. print "<br>\n";
  147. $head = security_prepare_head();
  148. print dol_get_fiche_head($head, 'passwords', '', -1);
  149. print '<br>';
  150. // Select manager to generate passwords
  151. print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
  152. print '<input type="hidden" name="token" value="'.newToken().'">';
  153. print '<input type="hidden" name="action" value="update">';
  154. print '<input type="hidden" name="constname" value="USER_PASSWORD_GENERATED">';
  155. print '<input type="hidden" name="consttype" value="yesno">';
  156. // Charge tableau des modules generation
  157. $dir = "../core/modules/security/generate";
  158. clearstatcache();
  159. $handle = opendir($dir);
  160. $i = 1;
  161. if (is_resource($handle)) {
  162. while (($file = readdir($handle)) !== false) {
  163. if (preg_match('/(modGeneratePass[a-z]+)\.class\.php$/i', $file, $reg)) {
  164. // Charging the numbering class
  165. $classname = $reg[1];
  166. require_once $dir.'/'.$file;
  167. $obj = new $classname($db, $conf, $langs, $user);
  168. $arrayhandler[$obj->id] = $obj;
  169. $i++;
  170. }
  171. }
  172. closedir($handle);
  173. }
  174. asort($arrayhandler);
  175. print '<div class="div-table-responsive-no-min">';
  176. print '<table class="noborder centpercent">';
  177. print '<tr class="liste_titre">';
  178. print '<td colspan="2">'.$langs->trans("RuleForGeneratedPasswords").'</td>';
  179. print '<td>'.$langs->trans("Example").'</td>';
  180. print '<td class="center">'.$langs->trans("Activated").'</td>';
  181. print '</tr>';
  182. $tabConf = explode(";", getDolGlobalString('USER_PASSWORD_PATTERN'));
  183. foreach ($arrayhandler as $key => $module) {
  184. // Show modules according to features level
  185. if (!empty($module->version) && $module->version == 'development' && $conf->global->MAIN_FEATURES_LEVEL < 2) {
  186. continue;
  187. }
  188. if (!empty($module->version) && $module->version == 'experimental' && $conf->global->MAIN_FEATURES_LEVEL < 1) {
  189. continue;
  190. }
  191. if ($module->isEnabled()) {
  192. print '<tr class="oddeven"><td>';
  193. print img_picto('', $module->picto, 'class="width25 size15x"').' ';
  194. print ucfirst($key);
  195. print "</td><td>\n";
  196. print $module->getDescription().'<br>';
  197. print $langs->trans("MinLength").': <span class="opacitymedium">'.$module->length.'</span>';
  198. print '</td>';
  199. // Show example of numbering module
  200. print '<td class="nowraponall">';
  201. $tmp = $module->getExample();
  202. if (preg_match('/^Error/', $tmp)) {
  203. $langs->load("errors");
  204. print '<div class="error">'.$langs->trans($tmp).'</div>';
  205. } elseif ($tmp == 'NotConfigured') {
  206. print '<span class="opacitymedium">'.$langs->trans($tmp).'</span>';
  207. } else {
  208. print '<span class="opacitymedium">'.$tmp.'</span>';
  209. }
  210. print '</td>'."\n";
  211. print '<td class="center">';
  212. if ($conf->global->USER_PASSWORD_GENERATED == $key) {
  213. //print img_picto('', 'tick');
  214. print img_picto($langs->trans("Enabled"), 'switch_on');
  215. } else {
  216. print '<a href="'.$_SERVER['PHP_SELF'].'?action=setgeneraterule&token='.newToken().'&value='.$key.'">';
  217. //print $langs->trans("Activate");
  218. print img_picto($langs->trans("Disabled"), 'switch_off');
  219. print '</a>';
  220. }
  221. print "</td></tr>\n";
  222. }
  223. }
  224. print '</table>';
  225. print '</div>';
  226. print '</form>';
  227. // Pattern for Password Perso
  228. if ($conf->global->USER_PASSWORD_GENERATED == "Perso") {
  229. print '<br>';
  230. print '<div class="div-table-responsive-no-min">';
  231. print '<table class="noborder centpercent">';
  232. print '<tr class="liste_titre">';
  233. print '<td colspan="2"> '.$langs->trans("PasswordPatternDesc").'</td>';
  234. print '</tr>';
  235. print '<tr class="oddeven">';
  236. print '<td>'.$langs->trans("MinLength")."</td>";
  237. print '<td><input type="number" value="'.$tabConf[0].'" id="minlenght" min="1"></td>';
  238. print '</tr>';
  239. print '<tr class="oddeven">';
  240. print '<td>'.$langs->trans("NbMajMin")."</td>";
  241. print '<td><input type="number" value="'.$tabConf[1].'" id="NbMajMin" min="0"></td>';
  242. print '</tr>';
  243. print '<tr class="oddeven">';
  244. print '<td>'.$langs->trans("NbNumMin")."</td>";
  245. print '<td><input type="number" value="'.$tabConf[2].'" id="NbNumMin" min="0"></td>';
  246. print '</tr>';
  247. print '<tr class="oddeven">';
  248. print '<td>'.$langs->trans("NbSpeMin")."</td>";
  249. print '<td><input type="number" value="'.$tabConf[3].'" id="NbSpeMin" min="0"></td>';
  250. print '</tr>';
  251. print '<tr class="oddeven">';
  252. print '<td>'.$langs->trans("NbIteConsecutive")."</td>";
  253. print '<td><input type="number" value="'.$tabConf[4].'" id="NbIteConsecutive" min="0"></td>';
  254. print '</tr>';
  255. print '<tr class="oddeven">';
  256. print '<td>'.$langs->trans("NoAmbiCaracAutoGeneration")."</td>";
  257. print '<td><input type="checkbox" id="NoAmbiCaracAutoGeneration" '.($tabConf[5] ? "checked" : "").' min="0"> <label for="NoAmbiCaracAutoGeneration" id="textcheckbox">'.($tabConf[5] ? $langs->trans("Activated") : $langs->trans("Disabled")).'</label></td>';
  258. print '</tr>';
  259. print '</table>';
  260. print '<div class="center">';
  261. print '<a class="button button-save" id="linkChangePattern">'.$langs->trans("Save").'</a>';
  262. print '</div>';
  263. print '<br><br>';
  264. print '<script type="text/javascript">';
  265. print ' function getStringArg(){';
  266. print ' var pattern = "";';
  267. print ' pattern += $("#minlenght").val() + ";";';
  268. print ' pattern += $("#NbMajMin").val() + ";";';
  269. print ' pattern += $("#NbNumMin").val() + ";";';
  270. print ' pattern += $("#NbSpeMin").val() + ";";';
  271. print ' pattern += $("#NbIteConsecutive").val() + ";";';
  272. print ' pattern += $("#NoAmbiCaracAutoGeneration")[0].checked ? "1" : "0";';
  273. print ' return pattern;';
  274. print ' }';
  275. print ' function valuePossible(){';
  276. print ' var fields = ["#minlenght", "#NbMajMin", "#NbNumMin", "#NbSpeMin", "#NbIteConsecutive"];';
  277. print ' for(var i = 0 ; i < fields.length ; i++){';
  278. print ' if($(fields[i]).val() < $(fields[i]).attr("min")){';
  279. print ' return false;';
  280. print ' }';
  281. print ' }';
  282. print ' ';
  283. print ' var length = parseInt($("#minlenght").val());';
  284. print ' var length_mini = parseInt($("#NbMajMin").val()) + parseInt($("#NbNumMin").val()) + parseInt($("#NbSpeMin").val());';
  285. print ' return length >= length_mini;';
  286. print ' }';
  287. print ' function generatelink(){';
  288. print ' return "security.php?action=updatepattern&token='.newToken().'&pattern="+getStringArg();';
  289. print ' }';
  290. print ' function valuePatternChange(){';
  291. print ' console.log("valuePatternChange");';
  292. print ' var lang_save = "'.$langs->trans("Save").'";';
  293. print ' var lang_error = "'.$langs->trans("Error").'";';
  294. print ' var lang_Disabled = "'.$langs->trans("Disabled").'";';
  295. print ' var lang_Activated = "'.$langs->trans("Activated").'";';
  296. print ' $("#textcheckbox").html($("#NoAmbiCaracAutoGeneration")[0].checked ? unescape(lang_Activated) : unescape(lang_Disabled));';
  297. print ' if(valuePossible()){';
  298. print ' $("#linkChangePattern").attr("href",generatelink()).text(lang_save);';
  299. print ' }';
  300. print ' else{';
  301. print ' $("#linkChangePattern").attr("href", null).text(lang_error);';
  302. print ' }';
  303. print ' }';
  304. print ' $("#minlenght").change(function(){valuePatternChange();});';
  305. print ' $("#NbMajMin").change(function(){valuePatternChange();});';
  306. print ' $("#NbNumMin").change(function(){valuePatternChange();});';
  307. print ' $("#NbSpeMin").change(function(){valuePatternChange();});';
  308. print ' $("#NbIteConsecutive").change(function(){valuePatternChange();});';
  309. print ' $("#NoAmbiCaracAutoGeneration").change(function(){valuePatternChange();});';
  310. print '</script>';
  311. }
  312. // Crypt passwords in database
  313. print '<br>';
  314. print '<form method="post" action="'.$_SERVER["PHP_SELF"].'">';
  315. print '<input type="hidden" name="token" value="'.newToken().'">';
  316. print '<input type="hidden" name="action" value="encrypt">';
  317. print '<table class="noborder centpercent">';
  318. print '<tr class="liste_titre">';
  319. print '<td colspan="3">'.$langs->trans("Parameters").'</td>';
  320. print '<td class="center">'.$langs->trans("Activated").'</td>';
  321. print '<td class="center"></td>';
  322. print '</tr>';
  323. // Disable clear password in database
  324. print '<tr class="oddeven">';
  325. print '<td colspan="3">'.$langs->trans("DoNotStoreClearPassword").'</td>';
  326. print '<td class="center" width="60">';
  327. if (getDolGlobalString('DATABASE_PWD_ENCRYPTED')) {
  328. print img_picto($langs->trans("Active"), 'tick');
  329. }
  330. print '</td>';
  331. if (!getDolGlobalString('DATABASE_PWD_ENCRYPTED')) {
  332. print '<td class="center" width="100">';
  333. print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?action=activate_encrypt&token='.newToken().'">'.$langs->trans("Activate").'</a>';
  334. print "</td>";
  335. }
  336. // Database conf file encryption
  337. if (getDolGlobalString('DATABASE_PWD_ENCRYPTED')) {
  338. print '<td class="center" width="100">';
  339. if ($allow_disable_encryption) {
  340. //On n'autorise pas l'annulation de l'encryption car les mots de passe ne peuvent pas etre decodes
  341. //Do not allow "disable encryption" as passwords cannot be decrypted
  342. print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?action=disable_encrypt&token='.newToken().'">'.$langs->trans("Disable").'</a>';
  343. } else {
  344. print '-';
  345. }
  346. print "</td>";
  347. }
  348. print "</td>";
  349. print '</tr>';
  350. // Crypt password into config file conf.php
  351. print '<tr class="oddeven">';
  352. print '<td colspan="3">'.$langs->trans("MainDbPasswordFileConfEncrypted").'</td>';
  353. print '<td align="center" width="60">';
  354. if (preg_match('/crypted:/i', $dolibarr_main_db_pass) || !empty($dolibarr_main_db_encrypted_pass)) {
  355. print img_picto($langs->trans("Active"), 'tick');
  356. }
  357. print '</td>';
  358. print '<td class="center" width="100">';
  359. if (empty($dolibarr_main_db_pass) && empty($dolibarr_main_db_encrypted_pass)) {
  360. $langs->load("errors");
  361. print img_warning($langs->trans("WarningPassIsEmpty"));
  362. } else {
  363. if (empty($dolibarr_main_db_encrypted_pass)) {
  364. print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?action=activate_encryptdbpassconf&token='.newToken().'">'.$langs->trans("Activate").'</a>';
  365. }
  366. if (!empty($dolibarr_main_db_encrypted_pass)) {
  367. print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?action=disable_encryptdbpassconf&token='.newToken().'">'.$langs->trans("Disable").'</a>';
  368. }
  369. }
  370. print "</td>";
  371. print "</td>";
  372. print '</tr>';
  373. // Disable link "Forget password" on logon
  374. print '<tr class="oddeven">';
  375. print '<td colspan="3">'.$langs->trans("DisableForgetPasswordLinkOnLogonPage").'</td>';
  376. print '<td class="center" width="60">';
  377. if (getDolGlobalString('MAIN_SECURITY_DISABLEFORGETPASSLINK')) {
  378. print img_picto($langs->trans("Active"), 'tick');
  379. }
  380. print '</td>';
  381. if (!getDolGlobalString('MAIN_SECURITY_DISABLEFORGETPASSLINK')) {
  382. print '<td class="center" width="100">';
  383. print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?action=activate_MAIN_SECURITY_DISABLEFORGETPASSLINK&token='.newToken().'">'.$langs->trans("Activate").'</a>';
  384. print "</td>";
  385. }
  386. if (getDolGlobalString('MAIN_SECURITY_DISABLEFORGETPASSLINK')) {
  387. print '<td center="center" width="100">';
  388. print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?action=disable_MAIN_SECURITY_DISABLEFORGETPASSLINK&token='.newToken().'">'.$langs->trans("Disable").'</a>';
  389. print "</td>";
  390. }
  391. print "</td>";
  392. print '</tr>';
  393. print '</table>';
  394. print '</form>';
  395. print '<br>';
  396. if (GETPOST('info', 'int') > 0) {
  397. if (function_exists('password_hash')) {
  398. print $langs->trans("Note: The function password_hash exists on your PHP")."<br>\n";
  399. } else {
  400. print $langs->trans("Note: The function password_hash does not exists on your PHP")."<br>\n";
  401. }
  402. print 'MAIN_SECURITY_HASH_ALGO = '.getDolGlobalString('MAIN_SECURITY_HASH_ALGO')."<br>\n";
  403. print 'MAIN_SECURITY_SALT = '.getDolGlobalString('MAIN_SECURITY_SALT')."<br>\n";
  404. }
  405. print '</div>';
  406. // End of page
  407. llxFooter();
  408. $db->close();