certificatemanager.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace OC\Security;
  9. use OC\Files\Filesystem;
  10. use OCP\ICertificateManager;
  11. /**
  12. * Manage trusted certificates for users
  13. */
  14. class CertificateManager implements ICertificateManager {
  15. /**
  16. * @var string
  17. */
  18. protected $uid;
  19. /**
  20. * @var \OC\Files\View
  21. */
  22. protected $view;
  23. /**
  24. * @param string $uid
  25. * @param \OC\Files\View $view relative zu data/
  26. */
  27. public function __construct($uid, \OC\Files\View $view) {
  28. $this->uid = $uid;
  29. $this->view = $view;
  30. }
  31. /**
  32. * Returns all certificates trusted by the user
  33. *
  34. * @return \OCP\ICertificate[]
  35. */
  36. public function listCertificates() {
  37. $path = $this->getPathToCertificates() . 'uploads/';
  38. if (!$this->view->is_dir($path)) {
  39. return array();
  40. }
  41. $result = array();
  42. $handle = $this->view->opendir($path);
  43. if (!is_resource($handle)) {
  44. return array();
  45. }
  46. while (false !== ($file = readdir($handle))) {
  47. if ($file != '.' && $file != '..') {
  48. try {
  49. $result[] = new Certificate($this->view->file_get_contents($path . $file), $file);
  50. } catch(\Exception $e) {}
  51. }
  52. }
  53. closedir($handle);
  54. return $result;
  55. }
  56. /**
  57. * create the certificate bundle of all trusted certificated
  58. */
  59. protected function createCertificateBundle() {
  60. $path = $this->getPathToCertificates();
  61. $certs = $this->listCertificates();
  62. $fh_certs = $this->view->fopen($path . '/rootcerts.crt', 'w');
  63. foreach ($certs as $cert) {
  64. $file = $path . '/uploads/' . $cert->getName();
  65. $data = $this->view->file_get_contents($file);
  66. if (strpos($data, 'BEGIN CERTIFICATE')) {
  67. fwrite($fh_certs, $data);
  68. fwrite($fh_certs, "\r\n");
  69. }
  70. }
  71. fclose($fh_certs);
  72. }
  73. /**
  74. * Save the certificate and re-generate the certificate bundle
  75. *
  76. * @param string $certificate the certificate data
  77. * @param string $name the filename for the certificate
  78. * @return \OCP\ICertificate|void|bool
  79. * @throws \Exception If the certificate could not get added
  80. */
  81. public function addCertificate($certificate, $name) {
  82. if (!Filesystem::isValidPath($name) or Filesystem::isFileBlacklisted($name)) {
  83. return false;
  84. }
  85. $dir = $this->getPathToCertificates() . 'uploads/';
  86. if (!$this->view->file_exists($dir)) {
  87. $this->view->mkdir($dir);
  88. }
  89. try {
  90. $file = $dir . $name;
  91. $certificateObject = new Certificate($certificate, $name);
  92. $this->view->file_put_contents($file, $certificate);
  93. $this->createCertificateBundle();
  94. return $certificateObject;
  95. } catch (\Exception $e) {
  96. throw $e;
  97. }
  98. }
  99. /**
  100. * Remove the certificate and re-generate the certificate bundle
  101. *
  102. * @param string $name
  103. * @return bool
  104. */
  105. public function removeCertificate($name) {
  106. if (!Filesystem::isValidPath($name)) {
  107. return false;
  108. }
  109. $path = $this->getPathToCertificates() . 'uploads/';
  110. if ($this->view->file_exists($path . $name)) {
  111. $this->view->unlink($path . $name);
  112. $this->createCertificateBundle();
  113. }
  114. return true;
  115. }
  116. /**
  117. * Get the path to the certificate bundle for this user
  118. *
  119. * @return string
  120. */
  121. public function getCertificateBundle() {
  122. return $this->getPathToCertificates() . 'rootcerts.crt';
  123. }
  124. private function getPathToCertificates() {
  125. $path = is_null($this->uid) ? '/files_external/' : '/' . $this->uid . '/files_external/';
  126. return $path;
  127. }
  128. }