security.php 15 KB

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