PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/security/security-cas.php

https://github.com/muchael/expressolivre
PHP | 144 lines | 127 code | 3 blank | 14 comment | 21 complexity | 022e55f920994caf0f7186998d107e95 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-2-Clause, BSD-3-Clause, AGPL-3.0
  1. <?php
  2. $GLOBALS['phpgw_info'] = array();
  3. $GLOBALS['phpgw_info']['flags']['currentapp'] = 'admin';
  4. include('../header.inc.php');
  5. require_once('classes/CertificadoB.php');
  6. require_once('security-lib.php');
  7. $msgs_alertas = '';
  8. $path1 = $GLOBALS['arquivos_crls'];
  9. $path2 = $GLOBALS['log'];
  10. $path3 = $GLOBALS['CAs'];
  11. if(!is_dir(dirname($path3)))
  12. {
  13. echo lang('Erro.') . ' ' . lang('Configuration file not found in server.');
  14. exit();
  15. }
  16. $dir = dirname($path3);
  17. /*
  18. if($_POST['arquivo'] & $_POST['caminho'] )
  19. {
  20. if(substr($_POST['arquivo'],0,25) != '--Selecione um arquivo co')
  21. {
  22. $path3 = $_POST['arquivo'];
  23. $dir = $_POST['caminho'];
  24. }
  25. }
  26. */
  27. if ($_FILES['file'])
  28. {
  29. if($_FILES['file']['tmp_name'])
  30. {
  31. $saida = array();
  32. $arquivo = $_FILES['file']['tmp_name'];
  33. $w = file_get_contents($arquivo);
  34. if(strpos($w,'-----BEGIN CERTIFICATE-----') === false)
  35. {
  36. $w = '';
  37. if(count(explode(chr(0x0A),$w)) < 7 )
  38. {
  39. // Convertendo DER para PEM (Entrada deve ser um certificado x509).
  40. $w = shell_exec('openssl x509 -inform DER -in ' . $arquivo . ' 2>&1');
  41. }
  42. if(count(explode(chr(0x0A),$w)) < 7 )
  43. {
  44. // Convertendo PKCS7 para PEM (Entrada deve ser PEM iniciando com -----BEGIN PKCS7----- ).
  45. $w = shell_exec('openssl pkcs7 -inform PEM -outform PEM -print_certs -in ' . $arquivo . ' 2>&1');
  46. }
  47. if(count(explode(chr(0x0A),$w)) < 7 )
  48. {
  49. // Convertendo p7b para PEM ( Entrada deve ser DER).
  50. $w = shell_exec('openssl pkcs7 -inform DER -outform PEM -print_certs -in ' . $arquivo . ' 2>&1');
  51. }
  52. if(count(explode(chr(0x0A),$w)) < 7 )
  53. {
  54. $msgs_alertas .= lang('File') . ' '. $_FILES['file']['name'] . lang('not processed. Invalid format') . '.<br/>';
  55. }
  56. }
  57. if(!$msgs_alertas)
  58. {
  59. $conteudo = '';
  60. $saida = explode(chr(0x0A),$w);
  61. foreach ($saida as $linha)
  62. {
  63. if($linha != '')
  64. {
  65. if(substr($linha,0,7) != 'subject' && substr($linha,0,6) != 'issuer')
  66. {
  67. $conteudo .= $linha . chr(0x0A);
  68. }
  69. }
  70. }
  71. $todos = ler_certificados_CAS($conteudo,true);
  72. $conteudo = '';
  73. foreach ($todos as $cert)
  74. {
  75. // Trata sho certificados de CA?
  76. $a = new certificadoB();
  77. $a->certificado($cert);
  78. if($a->dados['CA'])
  79. {
  80. $conteudo .= chr(0x0D) . chr(0x0A) . $cert;
  81. $msgs_alertas .= lang('Certificate added to') . ' ' . $a->dados['NOME'] . ' .<br/>';
  82. }
  83. }
  84. if($conteudo)
  85. {
  86. $novo_nome = gera_nome_arquivo_bkp($path3);
  87. if($novo_nome != $path3)
  88. {
  89. $ret = salva_arquivo_bkp($path3,$novo_nome);
  90. if($ret == 0)
  91. {
  92. file_put_contents($path3,$conteudo,FILE_APPEND);
  93. $msgs_alertas .= lang('File updated and save') . '.';
  94. }
  95. else
  96. {
  97. $msgs_alertas .= lang('Failure on save file (CD04). The requested operation is not concluded') . '.<br/>';
  98. }
  99. }
  100. else
  101. {
  102. $msgs_alertas .= lang('Failure on save file (CD03). The requested operation is not concluded') . '.<br/>';
  103. }
  104. }
  105. else
  106. {
  107. $msgs_alertas .= lang('ACs certificates not found') . '.';
  108. }
  109. }
  110. }
  111. }
  112. echo '<script type="text/javascript" src="certificados.js"></script>';
  113. echo '<div style="padding-left:90px" >';
  114. echo '<form id="frm3" enctype="multipart/form-data" method="post" action="' . $_SERVER["PHP_SELF"] . '">';
  115. echo '<a href="../security/security_admin.php" style="text-decoration:none"><input type="button" value="' . lang('Back') . '"/></a>';
  116. $aux99 = explode('/',$path3);
  117. $path3 = $aux99[count($aux99)-1];
  118. echo '<br/><br/>';
  119. echo '<div id="msgs"/>';
  120. echo $msgs_alertas;
  121. echo '</div>';
  122. echo '<div id="files"/>';
  123. echo '<h4 style="color: #000066">' . lang('Choose a file with CAs to add') . ':<h5>';
  124. echo '<input id="file" type="file" name="file" />';
  125. echo '&nbsp;&nbsp;&nbsp;&nbsp;';
  126. echo '<input type="button" name="adicionar" value="' . lang('Add') . '" onclick="javascript:Submete_Cas(\'frm3\',\'' . lang('Add file contents to ACs file') . ' ?\')" />';
  127. echo '</div>';
  128. echo '</form>';
  129. echo '<h2 id="titulo1" style="color: #000066">' . lang('Certificates in') . ' ' . $path3 . ' :</h2>';
  130. echo '<div id="xdiv1" style="border: #000000 1px solid; overflow: auto; width: 870px; height: 160px; white-space: pre; padding: 3px; " >';
  131. echo '<br/><font color="#000066"><b> ' . lang('Loading ...') . '</b></font>';
  132. echo '</div>';
  133. echo '<br/><pre>';
  134. echo '<div id="xdiv2" style="border: #000000 1px solid; overflow: auto; width: 870px; height: 180px; white-space: pre; padding: 3px; " >';
  135. echo '<br/><font color="#000066"><b> ' . lang('Loading ...') . '</b></font>';
  136. echo '</div></pre>';
  137. echo '<br/>';
  138. echo '<a href="../security/security_admin.php" style="text-decoration:none"><input type="button" value="' . lang('Back') . '"/></a>';
  139. echo '<div>';
  140. echo '<script type="text/javascript"> Lista_de_Certificados(\'' . $path3 . '\'); </script>';
  141. ?>