certificatemanager.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * @author Björn Schießle <schiessle@owncloud.com>
  4. * @author Joas Schilling <nickvergessen@owncloud.com>
  5. * @author Lukas Reschke <lukas@owncloud.com>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Robin Appelman <icewind@owncloud.com>
  8. *
  9. * @copyright Copyright (c) 2015, ownCloud, Inc.
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OC\Security;
  26. use OC\Files\Filesystem;
  27. use OCP\ICertificateManager;
  28. /**
  29. * Manage trusted certificates for users
  30. */
  31. class CertificateManager implements ICertificateManager {
  32. /**
  33. * @var string
  34. */
  35. protected $uid;
  36. /**
  37. * @var \OC\Files\View
  38. */
  39. protected $view;
  40. /**
  41. * @param string $uid
  42. * @param \OC\Files\View $view relative zu data/
  43. */
  44. public function __construct($uid, \OC\Files\View $view) {
  45. $this->uid = $uid;
  46. $this->view = $view;
  47. }
  48. /**
  49. * Returns all certificates trusted by the user
  50. *
  51. * @return \OCP\ICertificate[]
  52. */
  53. public function listCertificates() {
  54. $path = $this->getPathToCertificates() . 'uploads/';
  55. if (!$this->view->is_dir($path)) {
  56. return array();
  57. }
  58. $result = array();
  59. $handle = $this->view->opendir($path);
  60. if (!is_resource($handle)) {
  61. return array();
  62. }
  63. while (false !== ($file = readdir($handle))) {
  64. if ($file != '.' && $file != '..') {
  65. try {
  66. $result[] = new Certificate($this->view->file_get_contents($path . $file), $file);
  67. } catch(\Exception $e) {}
  68. }
  69. }
  70. closedir($handle);
  71. return $result;
  72. }
  73. /**
  74. * create the certificate bundle of all trusted certificated
  75. */
  76. public function createCertificateBundle() {
  77. $path = $this->getPathToCertificates();
  78. $certs = $this->listCertificates();
  79. $fh_certs = $this->view->fopen($path . '/rootcerts.crt', 'w');
  80. // Write user certificates
  81. foreach ($certs as $cert) {
  82. $file = $path . '/uploads/' . $cert->getName();
  83. $data = $this->view->file_get_contents($file);
  84. if (strpos($data, 'BEGIN CERTIFICATE')) {
  85. fwrite($fh_certs, $data);
  86. fwrite($fh_certs, "\r\n");
  87. }
  88. }
  89. // Append the default certificates
  90. $defaultCertificates = file_get_contents(\OC::$SERVERROOT . '/config/ca-bundle.crt');
  91. fwrite($fh_certs, $defaultCertificates);
  92. fclose($fh_certs);
  93. }
  94. /**
  95. * Save the certificate and re-generate the certificate bundle
  96. *
  97. * @param string $certificate the certificate data
  98. * @param string $name the filename for the certificate
  99. * @return \OCP\ICertificate
  100. * @throws \Exception If the certificate could not get added
  101. */
  102. public function addCertificate($certificate, $name) {
  103. if (!Filesystem::isValidPath($name) or Filesystem::isFileBlacklisted($name)) {
  104. throw new \Exception('Filename is not valid');
  105. }
  106. $dir = $this->getPathToCertificates() . 'uploads/';
  107. if (!$this->view->file_exists($dir)) {
  108. $this->view->mkdir($dir);
  109. }
  110. try {
  111. $file = $dir . $name;
  112. $certificateObject = new Certificate($certificate, $name);
  113. $this->view->file_put_contents($file, $certificate);
  114. $this->createCertificateBundle();
  115. return $certificateObject;
  116. } catch (\Exception $e) {
  117. throw $e;
  118. }
  119. }
  120. /**
  121. * Remove the certificate and re-generate the certificate bundle
  122. *
  123. * @param string $name
  124. * @return bool
  125. */
  126. public function removeCertificate($name) {
  127. if (!Filesystem::isValidPath($name)) {
  128. return false;
  129. }
  130. $path = $this->getPathToCertificates() . 'uploads/';
  131. if ($this->view->file_exists($path . $name)) {
  132. $this->view->unlink($path . $name);
  133. $this->createCertificateBundle();
  134. }
  135. return true;
  136. }
  137. /**
  138. * Get the path to the certificate bundle for this user
  139. *
  140. * @return string
  141. */
  142. public function getCertificateBundle() {
  143. return $this->getPathToCertificates() . 'rootcerts.crt';
  144. }
  145. /**
  146. * @return string
  147. */
  148. private function getPathToCertificates() {
  149. $path = is_null($this->uid) ? '/files_external/' : '/' . $this->uid . '/files_external/';
  150. return $path;
  151. }
  152. }