PageRenderTime 48ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/admin/security.php

https://github.com/asterix14/dolibarr
PHP | 433 lines | 321 code | 62 blank | 50 comment | 70 complexity | c26cd233c13e1b21d37b12fac8b8c296 MD5 | raw file
Possible License(s): LGPL-2.0
  1. <?php
  2. /* Copyright (C) 2004-2009 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2005-2007 Regis Houssin <regis@dolibarr.fr>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/admin/security.php
  20. * \ingroup setup
  21. * \brief Page de configuration du module securite
  22. */
  23. require("../main.inc.php");
  24. require_once(DOL_DOCUMENT_ROOT."/core/lib/admin.lib.php");
  25. $langs->load("users");
  26. $langs->load("admin");
  27. $langs->load("other");
  28. if (!$user->admin) accessforbidden();
  29. // Allow/Disallow change to clear passwords once passwords are crypted
  30. $allow_disable_encryption=true;
  31. $mesg = '';
  32. /*
  33. * Actions
  34. */
  35. if ($_GET["action"] == 'setgeneraterule')
  36. {
  37. if (! dolibarr_set_const($db, 'USER_PASSWORD_GENERATED',$_GET["value"],'chaine',0,'',$conf->entity))
  38. {
  39. dol_print_error($db);
  40. }
  41. else
  42. {
  43. Header("Location: ".$_SERVER["PHP_SELF"]);
  44. exit;
  45. }
  46. }
  47. if ($_GET["action"] == 'activate_encrypt')
  48. {
  49. $error=0;
  50. $db->begin();
  51. dolibarr_set_const($db, "DATABASE_PWD_ENCRYPTED", "1",'chaine',0,'',$conf->entity);
  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. {
  58. $numrows=$db->num_rows($resql);
  59. $i=0;
  60. while ($i < $numrows)
  61. {
  62. $obj=$db->fetch_object($resql);
  63. if (dol_hash($obj->pass))
  64. {
  65. $sql = "UPDATE ".MAIN_DB_PREFIX."user";
  66. $sql.= " SET pass_crypted = '".dol_hash($obj->pass)."', pass = NULL";
  67. $sql.= " WHERE rowid=".$obj->rowid;
  68. //print $sql;
  69. $resql2 = $db->query($sql);
  70. if (! $resql2)
  71. {
  72. dol_print_error($db);
  73. $error++;
  74. break;
  75. }
  76. $i++;
  77. }
  78. }
  79. }
  80. else dol_print_error($db);
  81. //print $error." ".$sql;
  82. //exit;
  83. if (! $error)
  84. {
  85. $db->commit();
  86. Header("Location: security.php");
  87. exit;
  88. }
  89. else
  90. {
  91. $db->rollback();
  92. dol_print_error($db,'');
  93. }
  94. }
  95. else if ($_GET["action"] == 'disable_encrypt')
  96. {
  97. //On n'autorise pas l'annulation de l'encryption car les mots de passe ne peuvent pas etre decodes
  98. //Do not allow "disable encryption" as passwords cannot be decrypted
  99. if ($allow_disable_encryption)
  100. {
  101. dolibarr_del_const($db, "DATABASE_PWD_ENCRYPTED",$conf->entity);
  102. }
  103. Header("Location: security.php");
  104. exit;
  105. }
  106. if ($_GET["action"] == 'activate_encryptdbpassconf')
  107. {
  108. $result = encodedecode_dbpassconf(1);
  109. if ($result > 0)
  110. {
  111. // database value not required
  112. //dolibarr_set_const($db, "MAIN_DATABASE_PWD_CONFIG_ENCRYPTED", "1");
  113. Header("Location: security.php");
  114. exit;
  115. }
  116. else
  117. {
  118. $mesg='<div class="warning">'.$langs->trans('InstrucToEncodePass',dol_encode($dolibarr_main_db_pass)).'</div>';
  119. }
  120. }
  121. else if ($_GET["action"] == 'disable_encryptdbpassconf')
  122. {
  123. $result = encodedecode_dbpassconf(0);
  124. if ($result > 0)
  125. {
  126. // database value not required
  127. //dolibarr_del_const($db, "MAIN_DATABASE_PWD_CONFIG_ENCRYPTED",$conf->entity);
  128. Header("Location: security.php");
  129. exit;
  130. }
  131. else
  132. {
  133. $mesg='<div class="warning">'.$langs->trans('InstrucToClearPass',$dolibarr_main_db_pass).'</div>';
  134. }
  135. }
  136. if ($_GET["action"] == 'activate_pdfsecurity')
  137. {
  138. dolibarr_set_const($db, "PDF_SECURITY_ENCRYPTION", "1",'chaine',0,'',$conf->entity);
  139. Header("Location: security.php");
  140. exit;
  141. }
  142. else if ($_GET["action"] == 'disable_pdfsecurity')
  143. {
  144. dolibarr_del_const($db, "PDF_SECURITY_ENCRYPTION",$conf->entity);
  145. Header("Location: security.php");
  146. exit;
  147. }
  148. if ($_GET["action"] == 'activate_MAIN_SECURITY_DISABLEFORGETPASSLINK')
  149. {
  150. dolibarr_set_const($db, "MAIN_SECURITY_DISABLEFORGETPASSLINK", '1','chaine',0,'',$conf->entity);
  151. Header("Location: security.php");
  152. exit;
  153. }
  154. else if ($_GET["action"] == 'disable_MAIN_SECURITY_DISABLEFORGETPASSLINK')
  155. {
  156. dolibarr_del_const($db, "MAIN_SECURITY_DISABLEFORGETPASSLINK",$conf->entity);
  157. Header("Location: security.php");
  158. exit;
  159. }
  160. /*
  161. * View
  162. */
  163. $form = new Form($db);
  164. llxHeader('',$langs->trans("Passwords"));
  165. print_fiche_titre($langs->trans("SecuritySetup"),'','setup');
  166. if ($mesg) print $mesg."<br>\n";
  167. print $langs->trans("GeneratedPasswordDesc")."<br>\n";
  168. print "<br>\n";
  169. $head=security_prepare_head();
  170. dol_fiche_head($head, 'passwords', $langs->trans("Security"));
  171. $var=false;
  172. // Choix du gestionnaire du generateur de mot de passe
  173. print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
  174. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  175. print '<input type="hidden" name="action" value="update">';
  176. print '<input type="hidden" name="constname" value="USER_PASSWORD_GENERATED">';
  177. print '<input type="hidden" name="consttype" value="yesno">';
  178. // Charge tableau des modules generation
  179. $dir = "../core/modules/security/generate";
  180. clearstatcache();
  181. $handle=opendir($dir);
  182. $i=1;
  183. if (is_resource($handle))
  184. {
  185. while (($file = readdir($handle))!==false)
  186. {
  187. if (preg_match('/(modGeneratePass[a-z]+)\.class\.php/i',$file,$reg))
  188. {
  189. // Chargement de la classe de numerotation
  190. $classname = $reg[1];
  191. require_once($dir.'/'.$file);
  192. $obj = new $classname($db,$conf,$langs,$user);
  193. $arrayhandler[$obj->id]=$obj;
  194. $i++;
  195. }
  196. }
  197. closedir($handle);
  198. }
  199. print '<table class="noborder" width="100%">';
  200. print '<tr class="liste_titre">';
  201. print '<td colspan="2">'.$langs->trans("RuleForGeneratedPasswords").'</td>';
  202. print '<td>'.$langs->trans("Example").'</td>';
  203. print '<td align="center">'.$langs->trans("Activated").'</td>';
  204. print '</tr>';
  205. foreach ($arrayhandler as $key => $module)
  206. {
  207. // Show modules according to features level
  208. if ($module->version == 'development' && $conf->global->MAIN_FEATURES_LEVEL < 2) continue;
  209. if ($module->version == 'experimental' && $conf->global->MAIN_FEATURES_LEVEL < 1) continue;
  210. if ($module->isEnabled())
  211. {
  212. $var = !$var;
  213. print '<tr '.$bc[$var].'><td width="100">';
  214. print ucfirst($key);
  215. print "</td><td>\n";
  216. print $module->getDescription().'<br>';
  217. print $langs->trans("MinLength").': '.$module->length;
  218. print '</td>';
  219. // Show example of numbering module
  220. print '<td nowrap="nowrap">';
  221. $tmp=$module->getExample();
  222. if (preg_match('/^Error/',$tmp)) { $langs->load("errors"); print '<div class="error">'.$langs->trans($tmp).'</div>'; }
  223. elseif ($tmp=='NotConfigured') print $langs->trans($tmp);
  224. else print $tmp;
  225. print '</td>'."\n";
  226. print '<td width="100" align="center">';
  227. if ($conf->global->USER_PASSWORD_GENERATED == $key)
  228. {
  229. print img_picto('','tick');
  230. }
  231. else
  232. {
  233. print '<a href="'.$_SERVER['PHP_SELF'].'?action=setgeneraterule&amp;value='.$key.'">'.$langs->trans("Activate").'</a>';
  234. }
  235. print "</td></tr>\n";
  236. }
  237. }
  238. print '</table>';
  239. print '</form>';
  240. // Cryptage mot de passe
  241. print '<br>';
  242. $var=true;
  243. print "<form method=\"post\" action=\"security.php\">";
  244. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  245. print "<input type=\"hidden\" name=\"action\" value=\"encrypt\">";
  246. print '<table class="noborder" width="100%">';
  247. print '<tr class="liste_titre">';
  248. print '<td colspan="3">'.$langs->trans("Parameters").'</td>';
  249. print '<td align="center">'.$langs->trans("Activated").'</td>';
  250. print '<td align="center">'.$langs->trans("Action").'</td>';
  251. print '</tr>';
  252. // Disable clear password in database
  253. $var=!$var;
  254. print "<tr ".$bc[$var].">";
  255. print '<td colspan="3">'.$langs->trans("DoNotStoreClearPassword").'</td>';
  256. print '<td align="center" width="60">';
  257. if ($conf->global->DATABASE_PWD_ENCRYPTED)
  258. {
  259. print img_picto($langs->trans("Active"),'tick');
  260. }
  261. print '</td>';
  262. if (! $conf->global->DATABASE_PWD_ENCRYPTED)
  263. {
  264. print '<td align="center" width="100">';
  265. print '<a href="security.php?action=activate_encrypt">'.$langs->trans("Activate").'</a>';
  266. print "</td>";
  267. }
  268. if($conf->global->DATABASE_PWD_ENCRYPTED)
  269. {
  270. print '<td align="center" width="100">';
  271. if ($allow_disable_encryption)
  272. {
  273. //On n'autorise pas l'annulation de l'encryption car les mots de passe ne peuvent pas etre decodes
  274. //Do not allow "disable encryption" as passwords cannot be decrypted
  275. print '<a href="security.php?action=disable_encrypt">'.$langs->trans("Disable").'</a>';
  276. }
  277. else
  278. {
  279. print '-';
  280. }
  281. print "</td>";
  282. }
  283. print "</td>";
  284. print '</tr>';
  285. // Cryptage du mot de base de la base dans conf.php
  286. $var=!$var;
  287. print "<tr ".$bc[$var].">";
  288. print '<td colspan="3">'.$langs->trans("MainDbPasswordFileConfEncrypted").'</td>';
  289. print '<td align="center" width="60">';
  290. if (preg_match('/crypted:/i',$dolibarr_main_db_pass) || ! empty($dolibarr_main_db_encrypted_pass))
  291. {
  292. print img_picto($langs->trans("Active"),'tick');
  293. }
  294. print '</td>';
  295. print '<td align="center" width="100">';
  296. if (empty($dolibarr_main_db_pass) && empty($dolibarr_main_db_encrypted_pass))
  297. {
  298. $langs->load("errors");
  299. print img_warning($langs->trans("WarningPassIsEmpty"));
  300. }
  301. else
  302. {
  303. if (empty($dolibarr_main_db_encrypted_pass))
  304. {
  305. print '<a href="security.php?action=activate_encryptdbpassconf">'.$langs->trans("Activate").'</a>';
  306. }
  307. if (! empty($dolibarr_main_db_encrypted_pass))
  308. {
  309. print '<a href="security.php?action=disable_encryptdbpassconf">'.$langs->trans("Disable").'</a>';
  310. }
  311. }
  312. print "</td>";
  313. print "</td>";
  314. print '</tr>';
  315. // Encryption et protection des PDF
  316. $var=!$var;
  317. print "<tr ".$bc[$var].">";
  318. print '<td colspan="3">';
  319. $text = $langs->trans("ProtectAndEncryptPdfFiles");
  320. $desc = $form->textwithpicto($text,$langs->transnoentities("ProtectAndEncryptPdfFilesDesc"),1);
  321. print $desc;
  322. print '</td>';
  323. print '<td align="center" width="60">';
  324. if($conf->global->PDF_SECURITY_ENCRYPTION == 1)
  325. {
  326. print img_picto($langs->trans("Active"),'tick');
  327. }
  328. print '</td>';
  329. print '<td align="center" width="100">';
  330. if ($conf->global->PDF_SECURITY_ENCRYPTION == 0)
  331. {
  332. print '<a href="security.php?action=activate_pdfsecurity">'.$langs->trans("Activate").'</a>';
  333. }
  334. if($conf->global->PDF_SECURITY_ENCRYPTION == 1)
  335. {
  336. print '<a href="security.php?action=disable_pdfsecurity">'.$langs->trans("Disable").'</a>';
  337. }
  338. print "</td>";
  339. print "</td>";
  340. print '</tr>';
  341. // Disable link "Forget password" on logon
  342. $var=!$var;
  343. print "<tr ".$bc[$var].">";
  344. print '<td colspan="3">'.$langs->trans("DisableForgetPasswordLinkOnLogonPage").'</td>';
  345. print '<td align="center" width="60">';
  346. if($conf->global->MAIN_SECURITY_DISABLEFORGETPASSLINK == 1)
  347. {
  348. print img_picto($langs->trans("Active"),'tick');
  349. }
  350. print '</td>';
  351. if ($conf->global->MAIN_SECURITY_DISABLEFORGETPASSLINK == 0)
  352. {
  353. print '<td align="center" width="100">';
  354. print '<a href="security.php?action=activate_MAIN_SECURITY_DISABLEFORGETPASSLINK">'.$langs->trans("Activate").'</a>';
  355. print "</td>";
  356. }
  357. if($conf->global->MAIN_SECURITY_DISABLEFORGETPASSLINK == 1)
  358. {
  359. print '<td align="center" width="100">';
  360. print '<a href="security.php?action=disable_MAIN_SECURITY_DISABLEFORGETPASSLINK">'.$langs->trans("Disable").'</a>';
  361. print "</td>";
  362. }
  363. print "</td>";
  364. print '</tr>';
  365. print '</table>';
  366. print '</form>';
  367. //print '<tr><td colspan="2" align="center"><input type="submit" class="button" value="'.$langs->trans("Save").'"></td></tr>';
  368. print '</div>';
  369. $db->close();
  370. llxFooter();
  371. ?>