security.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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."/lib/admin.lib.php");
  25. require_once(DOL_DOCUMENT_ROOT.'/lib/security.lib.php');
  26. $langs->load("users");
  27. $langs->load("admin");
  28. $langs->load("other");
  29. if (!$user->admin) accessforbidden();
  30. // Allow/Disallow change to clear passwords once passwords are crypted
  31. $allow_disable_encryption=true;
  32. $mesg = '';
  33. /*
  34. * Actions
  35. */
  36. if ($_GET["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 ($_GET["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 (md5($obj->pass))
  65. {
  66. $sql = "UPDATE ".MAIN_DB_PREFIX."user";
  67. $sql.= " SET pass_crypted = '".md5($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 ($_GET["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 ($_GET["action"] == 'activate_encryptdbpassconf')
  108. {
  109. $result = encodedecode_dbpassconf(1);
  110. if ($result > 0)
  111. {
  112. // database value not required
  113. //dolibarr_set_const($db, "MAIN_DATABASE_PWD_CONFIG_ENCRYPTED", "1");
  114. Header("Location: security.php");
  115. exit;
  116. }
  117. else
  118. {
  119. $mesg='<div class="warning">'.$langs->trans('InstrucToEncodePass',dol_encode($dolibarr_main_db_pass)).'</div>';
  120. }
  121. }
  122. else if ($_GET["action"] == 'disable_encryptdbpassconf')
  123. {
  124. $result = encodedecode_dbpassconf(0);
  125. if ($result > 0)
  126. {
  127. // database value not required
  128. //dolibarr_del_const($db, "MAIN_DATABASE_PWD_CONFIG_ENCRYPTED",$conf->entity);
  129. Header("Location: security.php");
  130. exit;
  131. }
  132. else
  133. {
  134. $mesg='<div class="warning">'.$langs->trans('InstrucToClearPass',$dolibarr_main_db_pass).'</div>';
  135. }
  136. }
  137. if ($_GET["action"] == 'activate_pdfsecurity')
  138. {
  139. dolibarr_set_const($db, "PDF_SECURITY_ENCRYPTION", "1",'chaine',0,'',$conf->entity);
  140. Header("Location: security.php");
  141. exit;
  142. }
  143. else if ($_GET["action"] == 'disable_pdfsecurity')
  144. {
  145. dolibarr_del_const($db, "PDF_SECURITY_ENCRYPTION",$conf->entity);
  146. Header("Location: security.php");
  147. exit;
  148. }
  149. if ($_GET["action"] == 'activate_MAIN_SECURITY_DISABLEFORGETPASSLINK')
  150. {
  151. dolibarr_set_const($db, "MAIN_SECURITY_DISABLEFORGETPASSLINK", '1','chaine',0,'',$conf->entity);
  152. Header("Location: security.php");
  153. exit;
  154. }
  155. else if ($_GET["action"] == 'disable_MAIN_SECURITY_DISABLEFORGETPASSLINK')
  156. {
  157. dolibarr_del_const($db, "MAIN_SECURITY_DISABLEFORGETPASSLINK",$conf->entity);
  158. Header("Location: security.php");
  159. exit;
  160. }
  161. /*
  162. * View
  163. */
  164. $html = new Form($db);
  165. llxHeader('',$langs->trans("Passwords"));
  166. print_fiche_titre($langs->trans("SecuritySetup"),'','setup');
  167. if ($mesg) print $mesg."<br>\n";
  168. print $langs->trans("GeneratedPasswordDesc")."<br>\n";
  169. print "<br>\n";
  170. $head=security_prepare_head();
  171. dol_fiche_head($head, 'passwords', $langs->trans("Security"));
  172. $var=false;
  173. // Choix du gestionnaire du generateur de mot de passe
  174. print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
  175. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  176. print '<input type="hidden" name="action" value="update">';
  177. print '<input type="hidden" name="constname" value="USER_PASSWORD_GENERATED">';
  178. print '<input type="hidden" name="consttype" value="yesno">';
  179. // Charge tableau des modules generation
  180. $dir = "../includes/modules/security/generate";
  181. clearstatcache();
  182. $handle=opendir($dir);
  183. $i=1;
  184. if (is_resource($handle))
  185. {
  186. while (($file = readdir($handle))!==false)
  187. {
  188. if (preg_match('/(modGeneratePass[a-z]+)\.class\.php/i',$file,$reg))
  189. {
  190. // Chargement de la classe de numerotation
  191. $classname = $reg[1];
  192. require_once($dir.'/'.$file);
  193. $obj = new $classname($db,$conf,$langs,$user);
  194. $arrayhandler[$obj->id]=$obj;
  195. $i++;
  196. }
  197. }
  198. closedir($handle);
  199. }
  200. print '<table class="noborder" width="100%">';
  201. print '<tr class="liste_titre">';
  202. print '<td colspan="2">'.$langs->trans("RuleForGeneratedPasswords").'</td>';
  203. print '<td>'.$langs->trans("Example").'</td>';
  204. print '<td align="center">'.$langs->trans("Activated").'</td>';
  205. print '</tr>';
  206. foreach ($arrayhandler as $key => $module)
  207. {
  208. // Show modules according to features level
  209. if ($module->version == 'development' && $conf->global->MAIN_FEATURES_LEVEL < 2) continue;
  210. if ($module->version == 'experimental' && $conf->global->MAIN_FEATURES_LEVEL < 1) continue;
  211. if ($module->isEnabled())
  212. {
  213. $var = !$var;
  214. print '<tr '.$bc[$var].'><td width="100">';
  215. print ucfirst($key);
  216. print "</td><td>\n";
  217. print $module->getDescription().'<br>';
  218. print $langs->trans("MinLength").': '.$module->length;
  219. print '</td>';
  220. // Show example of numbering module
  221. print '<td nowrap="nowrap">';
  222. $tmp=$module->getExample();
  223. if (preg_match('/^Error/',$tmp)) 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 = $html->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. ?>