security.php 15 KB

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