security.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. <?php
  2. /* Copyright (C) 2004-2009 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. require '../main.inc.php';
  25. require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
  26. require_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
  27. $action = GETPOST('action', 'aZ09');
  28. // Load translation files required by the page
  29. $langs->loadLangs(array("users", "admin", "other"));
  30. if (!$user->admin) {
  31. accessforbidden();
  32. }
  33. // Allow/Disallow change to clear passwords once passwords are crypted
  34. $allow_disable_encryption = true;
  35. /*
  36. * Actions
  37. */
  38. if ($action == 'setgeneraterule') {
  39. if (!dolibarr_set_const($db, 'USER_PASSWORD_GENERATED', $_GET["value"], 'chaine', 0, '', $conf->entity)) {
  40. dol_print_error($db);
  41. } else {
  42. header("Location: ".$_SERVER["PHP_SELF"]);
  43. exit;
  44. }
  45. }
  46. if ($action == 'activate_encrypt') {
  47. $error = 0;
  48. $db->begin();
  49. dolibarr_set_const($db, "DATABASE_PWD_ENCRYPTED", "1", 'chaine', 0, '', $conf->entity);
  50. $sql = "SELECT u.rowid, u.pass, u.pass_crypted";
  51. $sql .= " FROM ".MAIN_DB_PREFIX."user as u";
  52. $sql .= " WHERE u.pass IS NOT NULL AND LENGTH(u.pass) < 32"; // Not a MD5 value
  53. $resql = $db->query($sql);
  54. if ($resql) {
  55. $numrows = $db->num_rows($resql);
  56. $i = 0;
  57. while ($i < $numrows) {
  58. $obj = $db->fetch_object($resql);
  59. if (dol_hash($obj->pass)) {
  60. $sql = "UPDATE ".MAIN_DB_PREFIX."user";
  61. $sql .= " SET pass_crypted = '".dol_hash($obj->pass)."', pass = NULL";
  62. $sql .= " WHERE rowid=".((int) $obj->rowid);
  63. //print $sql;
  64. $resql2 = $db->query($sql);
  65. if (!$resql2) {
  66. dol_print_error($db);
  67. $error++;
  68. break;
  69. }
  70. $i++;
  71. }
  72. }
  73. } else {
  74. dol_print_error($db);
  75. }
  76. //print $error." ".$sql;
  77. //exit;
  78. if (!$error) {
  79. $db->commit();
  80. header("Location: security.php");
  81. exit;
  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. header("Location: security.php");
  93. exit;
  94. }
  95. if ($action == 'activate_encryptdbpassconf') {
  96. $result = encodedecode_dbpassconf(1);
  97. if ($result > 0) {
  98. 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.
  99. // database value not required
  100. //dolibarr_set_const($db, "MAIN_DATABASE_PWD_CONFIG_ENCRYPTED", "1");
  101. header("Location: security.php");
  102. exit;
  103. } else {
  104. setEventMessages($langs->trans('InstrucToEncodePass', dol_encode($dolibarr_main_db_pass)), null, 'warnings');
  105. }
  106. } elseif ($action == 'disable_encryptdbpassconf') {
  107. $result = encodedecode_dbpassconf(0);
  108. if ($result > 0) {
  109. 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.
  110. // database value not required
  111. //dolibarr_del_const($db, "MAIN_DATABASE_PWD_CONFIG_ENCRYPTED",$conf->entity);
  112. header("Location: security.php");
  113. exit;
  114. } else {
  115. setEventMessages($langs->trans('InstrucToClearPass', $dolibarr_main_db_pass), null, 'warnings');
  116. }
  117. }
  118. if ($action == 'activate_MAIN_SECURITY_DISABLEFORGETPASSLINK') {
  119. dolibarr_set_const($db, "MAIN_SECURITY_DISABLEFORGETPASSLINK", '1', 'chaine', 0, '', $conf->entity);
  120. header("Location: security.php");
  121. exit;
  122. } elseif ($action == 'disable_MAIN_SECURITY_DISABLEFORGETPASSLINK') {
  123. dolibarr_del_const($db, "MAIN_SECURITY_DISABLEFORGETPASSLINK", $conf->entity);
  124. header("Location: security.php");
  125. exit;
  126. }
  127. if ($action == 'updatepattern') {
  128. $pattern = GETPOST("pattern", "alpha");
  129. $explodePattern = explode(';', $pattern);
  130. $patternInError = false;
  131. if ($explodePattern[0] < 1 || $explodePattern[4] < 0) {
  132. $patternInError = true;
  133. }
  134. if ($explodePattern[0] < $explodePattern[1] + $explodePattern[2] + $explodePattern[3]) {
  135. $patternInError = true;
  136. }
  137. if (!$patternInError) {
  138. dolibarr_set_const($db, "USER_PASSWORD_PATTERN", $pattern, 'chaine', 0, '', $conf->entity);
  139. setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
  140. header("Location: security.php");
  141. exit;
  142. }
  143. }
  144. /*
  145. * View
  146. */
  147. $form = new Form($db);
  148. $wikihelp = 'EN:Setup_Security|FR:Paramétrage_Sécurité|ES:Configuración_Seguridad';
  149. llxHeader('', $langs->trans("Passwords"), $wikihelp);
  150. print load_fiche_titre($langs->trans("SecuritySetup"), '', 'title_setup');
  151. print '<span class="opacitymedium">'.$langs->trans("GeneratedPasswordDesc")."</span><br>\n";
  152. print "<br>\n";
  153. $head = security_prepare_head();
  154. print dol_get_fiche_head($head, 'passwords', '', -1);
  155. // Choix du gestionnaire du generateur de mot de passe
  156. print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
  157. print '<input type="hidden" name="token" value="'.newToken().'">';
  158. print '<input type="hidden" name="action" value="update">';
  159. print '<input type="hidden" name="constname" value="USER_PASSWORD_GENERATED">';
  160. print '<input type="hidden" name="consttype" value="yesno">';
  161. // Charge tableau des modules generation
  162. $dir = "../core/modules/security/generate";
  163. clearstatcache();
  164. $handle = opendir($dir);
  165. $i = 1;
  166. if (is_resource($handle)) {
  167. while (($file = readdir($handle)) !== false) {
  168. if (preg_match('/(modGeneratePass[a-z]+)\.class\.php$/i', $file, $reg)) {
  169. // Charging the numbering class
  170. $classname = $reg[1];
  171. require_once $dir.'/'.$file;
  172. $obj = new $classname($db, $conf, $langs, $user);
  173. $arrayhandler[$obj->id] = $obj;
  174. $i++;
  175. }
  176. }
  177. closedir($handle);
  178. }
  179. asort($arrayhandler);
  180. print '<div class="div-table-responsive-no-min">';
  181. print '<table class="noborder centpercent">';
  182. print '<tr class="liste_titre">';
  183. print '<td colspan="2">'.$langs->trans("RuleForGeneratedPasswords").'</td>';
  184. print '<td>'.$langs->trans("Example").'</td>';
  185. print '<td class="center">'.$langs->trans("Activated").'</td>';
  186. print '</tr>';
  187. foreach ($arrayhandler as $key => $module) {
  188. // Show modules according to features level
  189. if (!empty($module->version) && $module->version == 'development' && $conf->global->MAIN_FEATURES_LEVEL < 2) {
  190. continue;
  191. }
  192. if (!empty($module->version) && $module->version == 'experimental' && $conf->global->MAIN_FEATURES_LEVEL < 1) {
  193. continue;
  194. }
  195. if ($module->isEnabled()) {
  196. print '<tr class="oddeven"><td width="100">';
  197. print ucfirst($key);
  198. print "</td><td>\n";
  199. print $module->getDescription().'<br>';
  200. print $langs->trans("MinLength").': '.$module->length;
  201. print '</td>';
  202. // Show example of numbering module
  203. print '<td class="nowrap">';
  204. $tmp = $module->getExample();
  205. if (preg_match('/^Error/', $tmp)) {
  206. $langs->load("errors");
  207. print '<div class="error">'.$langs->trans($tmp).'</div>';
  208. } elseif ($tmp == 'NotConfigured') {
  209. print $langs->trans($tmp);
  210. } else {
  211. print '<span class="opacitymedium">'.$tmp.'</span>';
  212. }
  213. print '</td>'."\n";
  214. print '<td width="100" align="center">';
  215. if ($conf->global->USER_PASSWORD_GENERATED == $key) {
  216. //print img_picto('', 'tick');
  217. print img_picto($langs->trans("Enabled"), 'switch_on');
  218. } else {
  219. print '<a href="'.$_SERVER['PHP_SELF'].'?action=setgeneraterule&amp;token='.newToken().'&amp;value='.$key.'">';
  220. //print $langs->trans("Activate");
  221. print img_picto($langs->trans("Disabled"), 'switch_off');
  222. print '</a>';
  223. }
  224. print "</td></tr>\n";
  225. }
  226. }
  227. print '</table>';
  228. print '</div>';
  229. print '</form>';
  230. //if($conf->global->MAIN_SECURITY_DISABLEFORGETPASSLINK == 1)
  231. // Patter for Password Perso
  232. if ($conf->global->USER_PASSWORD_GENERATED == "Perso") {
  233. $tabConf = explode(";", $conf->global->USER_PASSWORD_PATTERN);
  234. print '<br>';
  235. print '<div class="div-table-responsive-no-min">';
  236. print '<table class="noborder centpercent">';
  237. print '<tr class="liste_titre">';
  238. print '<td colspan="2"> '.$langs->trans("PasswordPatternDesc").'</td>';
  239. print '</tr>';
  240. print '<tr class="oddeven">';
  241. print '<td>'.$langs->trans("MinLength")."</td>";
  242. print '<td><input type="number" value="'.$tabConf[0].'" id="minlenght" min="1"></td>';
  243. print '</tr>';
  244. print '<tr class="oddeven">';
  245. print '<td>'.$langs->trans("NbMajMin")."</td>";
  246. print '<td><input type="number" value="'.$tabConf[1].'" id="NbMajMin" min="0"></td>';
  247. print '</tr>';
  248. print '<tr class="oddeven">';
  249. print '<td>'.$langs->trans("NbNumMin")."</td>";
  250. print '<td><input type="number" value="'.$tabConf[2].'" id="NbNumMin" min="0"></td>';
  251. print '</tr>';
  252. print '<tr class="oddeven">';
  253. print '<td>'.$langs->trans("NbSpeMin")."</td>";
  254. print '<td><input type="number" value="'.$tabConf[3].'" id="NbSpeMin" min="0"></td>';
  255. print '</tr>';
  256. print '<tr class="oddeven">';
  257. print '<td>'.$langs->trans("NbIteConsecutive")."</td>";
  258. print '<td><input type="number" value="'.$tabConf[4].'" id="NbIteConsecutive" min="0"></td>';
  259. print '</tr>';
  260. print '<tr class="oddeven">';
  261. print '<td>'.$langs->trans("NoAmbiCaracAutoGeneration")."</td>";
  262. print '<td><input type="checkbox" id="NoAmbiCaracAutoGeneration" '.($tabConf[5] ? "checked" : "").' min="0"> <span id="textcheckbox">'.($tabConf[5] ? $langs->trans("Activated") : $langs->trans("Disabled")).'</span></td>';
  263. print '</tr>';
  264. print '</table>';
  265. print '<br>';
  266. print '<div class="center">';
  267. print '<a class="button button-save" id="linkChangePattern">'.$langs->trans("Save").'</a>';
  268. print '</div>';
  269. print '<br><br>';
  270. print '<script type="text/javascript">';
  271. print ' function getStringArg(){';
  272. print ' var pattern = "";';
  273. print ' pattern += $("#minlenght").val() + ";";';
  274. print ' pattern += $("#NbMajMin").val() + ";";';
  275. print ' pattern += $("#NbNumMin").val() + ";";';
  276. print ' pattern += $("#NbSpeMin").val() + ";";';
  277. print ' pattern += $("#NbIteConsecutive").val() + ";";';
  278. print ' pattern += $("#NoAmbiCaracAutoGeneration")[0].checked ? "1" : "0";';
  279. print ' return pattern;';
  280. print ' }';
  281. print ' function valuePossible(){';
  282. print ' var fields = ["#minlenght", "#NbMajMin", "#NbNumMin", "#NbSpeMin", "#NbIteConsecutive"];';
  283. print ' for(var i = 0 ; i < fields.length ; i++){';
  284. print ' if($(fields[i]).val() < $(fields[i]).attr("min")){';
  285. print ' return false;';
  286. print ' }';
  287. print ' }';
  288. print ' ';
  289. print ' var length = parseInt($("#minlenght").val());';
  290. print ' var length_mini = parseInt($("#NbMajMin").val()) + parseInt($("#NbNumMin").val()) + parseInt($("#NbSpeMin").val());';
  291. print ' return length >= length_mini;';
  292. print ' }';
  293. print ' function generatelink(){';
  294. print ' return "security.php?action=updatepattern&pattern="+getStringArg();';
  295. print ' }';
  296. print ' function valuePatternChange(){';
  297. print ' console.log("valuePatternChange");';
  298. print ' var lang_save = "'.$langs->trans("Save").'";';
  299. print ' var lang_error = "'.$langs->trans("Error").'";';
  300. print ' var lang_Disabled = "'.$langs->trans("Disabled").'";';
  301. print ' var lang_Activated = "'.$langs->trans("Activated").'";';
  302. print ' $("#textcheckbox").html($("#NoAmbiCaracAutoGeneration")[0].checked ? unescape(lang_Activated) : unescape(lang_Disabled));';
  303. print ' if(valuePossible()){';
  304. print ' $("#linkChangePattern").attr("href",generatelink()).text(lang_save);';
  305. print ' }';
  306. print ' else{';
  307. print ' $("#linkChangePattern").attr("href", null).text(lang_error);';
  308. print ' }';
  309. print ' }';
  310. print ' $("#minlenght").change(function(){valuePatternChange();});';
  311. print ' $("#NbMajMin").change(function(){valuePatternChange();});';
  312. print ' $("#NbNumMin").change(function(){valuePatternChange();});';
  313. print ' $("#NbSpeMin").change(function(){valuePatternChange();});';
  314. print ' $("#NbIteConsecutive").change(function(){valuePatternChange();});';
  315. print ' $("#NoAmbiCaracAutoGeneration").change(function(){valuePatternChange();});';
  316. print '</script>';
  317. }
  318. // Cryptage mot de passe
  319. print '<br>';
  320. print "<form method=\"post\" action=\"".$_SERVER["PHP_SELF"]."\">";
  321. print '<input type="hidden" name="token" value="'.newToken().'">';
  322. print "<input type=\"hidden\" name=\"action\" value=\"encrypt\">";
  323. print '<table class="noborder centpercent">';
  324. print '<tr class="liste_titre">';
  325. print '<td colspan="3">'.$langs->trans("Parameters").'</td>';
  326. print '<td class="center">'.$langs->trans("Activated").'</td>';
  327. print '<td class="center">'.$langs->trans("Action").'</td>';
  328. print '</tr>';
  329. // Disable clear password in database
  330. print '<tr class="oddeven">';
  331. print '<td colspan="3">'.$langs->trans("DoNotStoreClearPassword").'</td>';
  332. print '<td align="center" width="60">';
  333. if (getDolGlobalString('DATABASE_PWD_ENCRYPTED')) {
  334. print img_picto($langs->trans("Active"), 'tick');
  335. }
  336. print '</td>';
  337. if (!getDolGlobalString('DATABASE_PWD_ENCRYPTED')) {
  338. print '<td align="center" width="100">';
  339. print '<a href="security.php?action=activate_encrypt">'.$langs->trans("Activate").'</a>';
  340. print "</td>";
  341. }
  342. // Database conf file encryption
  343. if (getDolGlobalString('DATABASE_PWD_ENCRYPTED')) {
  344. print '<td align="center" width="100">';
  345. if ($allow_disable_encryption) {
  346. //On n'autorise pas l'annulation de l'encryption car les mots de passe ne peuvent pas etre decodes
  347. //Do not allow "disable encryption" as passwords cannot be decrypted
  348. print '<a href="'.$_SERVER["PHP_SELF"].'?action=disable_encrypt&token='.newToken().'">'.$langs->trans("Disable").'</a>';
  349. } else {
  350. print '-';
  351. }
  352. print "</td>";
  353. }
  354. print "</td>";
  355. print '</tr>';
  356. // Cryptage du mot de base de la base dans conf.php
  357. print '<tr class="oddeven">';
  358. print '<td colspan="3">'.$langs->trans("MainDbPasswordFileConfEncrypted").'</td>';
  359. print '<td align="center" width="60">';
  360. if (preg_match('/crypted:/i', $dolibarr_main_db_pass) || !empty($dolibarr_main_db_encrypted_pass)) {
  361. print img_picto($langs->trans("Active"), 'tick');
  362. }
  363. print '</td>';
  364. print '<td align="center" width="100">';
  365. if (empty($dolibarr_main_db_pass) && empty($dolibarr_main_db_encrypted_pass)) {
  366. $langs->load("errors");
  367. print img_warning($langs->trans("WarningPassIsEmpty"));
  368. } else {
  369. if (empty($dolibarr_main_db_encrypted_pass)) {
  370. print '<a href="'.$_SERVER["PHP_SELF"].'?action=activate_encryptdbpassconf&token='.newToken().'">'.$langs->trans("Activate").'</a>';
  371. }
  372. if (!empty($dolibarr_main_db_encrypted_pass)) {
  373. print '<a href="'.$_SERVER["PHP_SELF"].'?action=disable_encryptdbpassconf&token='.newToken().'">'.$langs->trans("Disable").'</a>';
  374. }
  375. }
  376. print "</td>";
  377. print "</td>";
  378. print '</tr>';
  379. // Disable link "Forget password" on logon
  380. print '<tr class="oddeven">';
  381. print '<td colspan="3">'.$langs->trans("DisableForgetPasswordLinkOnLogonPage").'</td>';
  382. print '<td align="center" width="60">';
  383. if (getDolGlobalString('MAIN_SECURITY_DISABLEFORGETPASSLINK')) {
  384. print img_picto($langs->trans("Active"), 'tick');
  385. }
  386. print '</td>';
  387. if (!getDolGlobalString('MAIN_SECURITY_DISABLEFORGETPASSLINK')) {
  388. print '<td align="center" width="100">';
  389. print '<a href="'.$_SERVER["PHP_SELF"].'?action=activate_MAIN_SECURITY_DISABLEFORGETPASSLINK&token='.newToken().'">'.$langs->trans("Activate").'</a>';
  390. print "</td>";
  391. }
  392. if (getDolGlobalString('MAIN_SECURITY_DISABLEFORGETPASSLINK')) {
  393. print '<td align="center" width="100">';
  394. print '<a href="'.$_SERVER["PHP_SELF"].'?action=disable_MAIN_SECURITY_DISABLEFORGETPASSLINK&token='.newToken().'">'.$langs->trans("Disable").'</a>';
  395. print "</td>";
  396. }
  397. print "</td>";
  398. print '</tr>';
  399. print '</table>';
  400. print '</form>';
  401. print '<br>';
  402. if (GETPOST('info', 'int') > 0) {
  403. if (function_exists('password_hash')) {
  404. print $langs->trans("Note: The function password_hash exists on your PHP")."<br>\n";
  405. } else {
  406. print $langs->trans("Note: The function password_hash does not exists on your PHP")."<br>\n";
  407. }
  408. print 'MAIN_SECURITY_HASH_ALGO = '.getDolGlobalString('MAIN_SECURITY_HASH_ALGO')."<br>\n";
  409. print 'MAIN_SECURITY_SALT = '.getDolGlobalString('MAIN_SECURITY_SALT')."<br>\n";
  410. }
  411. print '</div>';
  412. // End of page
  413. llxFooter();
  414. $db->close();