UserHooks.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bjoern Schiessle <bjoern@schiessle.org>
  6. * @author Björn Schießle <bjoern@schiessle.org>
  7. * @author Clark Tomlinson <fallen013@gmail.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCA\Encryption\Hooks;
  27. use OC\Files\Filesystem;
  28. use OCP\IUserManager;
  29. use OCP\Util as OCUtil;
  30. use OCA\Encryption\Hooks\Contracts\IHook;
  31. use OCA\Encryption\KeyManager;
  32. use OCA\Encryption\Crypto\Crypt;
  33. use OCA\Encryption\Users\Setup;
  34. use OCP\App;
  35. use OCP\ILogger;
  36. use OCP\IUserSession;
  37. use OCA\Encryption\Util;
  38. use OCA\Encryption\Session;
  39. use OCA\Encryption\Recovery;
  40. class UserHooks implements IHook {
  41. /**
  42. * list of user for which we perform a password reset
  43. * @var array
  44. */
  45. protected static $passwordResetUsers = [];
  46. /**
  47. * @var KeyManager
  48. */
  49. private $keyManager;
  50. /**
  51. * @var IUserManager
  52. */
  53. private $userManager;
  54. /**
  55. * @var ILogger
  56. */
  57. private $logger;
  58. /**
  59. * @var Setup
  60. */
  61. private $userSetup;
  62. /**
  63. * @var IUserSession
  64. */
  65. private $user;
  66. /**
  67. * @var Util
  68. */
  69. private $util;
  70. /**
  71. * @var Session
  72. */
  73. private $session;
  74. /**
  75. * @var Recovery
  76. */
  77. private $recovery;
  78. /**
  79. * @var Crypt
  80. */
  81. private $crypt;
  82. /**
  83. * UserHooks constructor.
  84. *
  85. * @param KeyManager $keyManager
  86. * @param IUserManager $userManager
  87. * @param ILogger $logger
  88. * @param Setup $userSetup
  89. * @param IUserSession $user
  90. * @param Util $util
  91. * @param Session $session
  92. * @param Crypt $crypt
  93. * @param Recovery $recovery
  94. */
  95. public function __construct(KeyManager $keyManager,
  96. IUserManager $userManager,
  97. ILogger $logger,
  98. Setup $userSetup,
  99. IUserSession $user,
  100. Util $util,
  101. Session $session,
  102. Crypt $crypt,
  103. Recovery $recovery) {
  104. $this->keyManager = $keyManager;
  105. $this->userManager = $userManager;
  106. $this->logger = $logger;
  107. $this->userSetup = $userSetup;
  108. $this->user = $user;
  109. $this->util = $util;
  110. $this->session = $session;
  111. $this->recovery = $recovery;
  112. $this->crypt = $crypt;
  113. }
  114. /**
  115. * Connects Hooks
  116. *
  117. * @return null
  118. */
  119. public function addHooks() {
  120. OCUtil::connectHook('OC_User', 'post_login', $this, 'login');
  121. OCUtil::connectHook('OC_User', 'logout', $this, 'logout');
  122. // this hooks only make sense if no master key is used
  123. if ($this->util->isMasterKeyEnabled() === false) {
  124. OCUtil::connectHook('OC_User',
  125. 'post_setPassword',
  126. $this,
  127. 'setPassphrase');
  128. OCUtil::connectHook('OC_User',
  129. 'pre_setPassword',
  130. $this,
  131. 'preSetPassphrase');
  132. OCUtil::connectHook('\OC\Core\LostPassword\Controller\LostController',
  133. 'post_passwordReset',
  134. $this,
  135. 'postPasswordReset');
  136. OCUtil::connectHook('\OC\Core\LostPassword\Controller\LostController',
  137. 'pre_passwordReset',
  138. $this,
  139. 'prePasswordReset');
  140. OCUtil::connectHook('OC_User',
  141. 'post_createUser',
  142. $this,
  143. 'postCreateUser');
  144. OCUtil::connectHook('OC_User',
  145. 'post_deleteUser',
  146. $this,
  147. 'postDeleteUser');
  148. }
  149. }
  150. /**
  151. * Startup encryption backend upon user login
  152. *
  153. * @note This method should never be called for users using client side encryption
  154. * @param array $params
  155. * @return boolean|null
  156. */
  157. public function login($params) {
  158. // ensure filesystem is loaded
  159. if (!\OC\Files\Filesystem::$loaded) {
  160. $this->setupFS($params['uid']);
  161. }
  162. if ($this->util->isMasterKeyEnabled() === false) {
  163. $this->userSetup->setupUser($params['uid'], $params['password']);
  164. }
  165. $this->keyManager->init($params['uid'], $params['password']);
  166. }
  167. /**
  168. * remove keys from session during logout
  169. */
  170. public function logout() {
  171. $this->session->clear();
  172. }
  173. /**
  174. * setup encryption backend upon user created
  175. *
  176. * @note This method should never be called for users using client side encryption
  177. * @param array $params
  178. */
  179. public function postCreateUser($params) {
  180. $this->userSetup->setupUser($params['uid'], $params['password']);
  181. }
  182. /**
  183. * cleanup encryption backend upon user deleted
  184. *
  185. * @param array $params : uid, password
  186. * @note This method should never be called for users using client side encryption
  187. */
  188. public function postDeleteUser($params) {
  189. $this->keyManager->deletePublicKey($params['uid']);
  190. }
  191. public function prePasswordReset($params) {
  192. $user = $params['uid'];
  193. self::$passwordResetUsers[$user] = true;
  194. }
  195. public function postPasswordReset($params) {
  196. $uid = $params['uid'];
  197. $password = $params['password'];
  198. $this->keyManager->backupUserKeys('passwordReset', $uid);
  199. $this->keyManager->deleteUserKeys($uid);
  200. $this->userSetup->setupUser($uid, $password);
  201. unset(self::$passwordResetUsers[$uid]);
  202. }
  203. /**
  204. * If the password can't be changed within Nextcloud, than update the key password in advance.
  205. *
  206. * @param array $params : uid, password
  207. * @return boolean|null
  208. */
  209. public function preSetPassphrase($params) {
  210. $user = $this->userManager->get($params['uid']);
  211. if ($user && !$user->canChangePassword()) {
  212. $this->setPassphrase($params);
  213. }
  214. }
  215. /**
  216. * Change a user's encryption passphrase
  217. *
  218. * @param array $params keys: uid, password
  219. * @return boolean|null
  220. */
  221. public function setPassphrase($params) {
  222. // if we are in the process to resetting a user password, we have nothing
  223. // to do here
  224. if (isset(self::$passwordResetUsers[$params['uid']])) {
  225. return true;
  226. }
  227. // Get existing decrypted private key
  228. $privateKey = $this->session->getPrivateKey();
  229. $user = $this->user->getUser();
  230. // current logged in user changes his own password
  231. if ($user && $params['uid'] === $user->getUID() && $privateKey) {
  232. // Encrypt private key with new user pwd as passphrase
  233. $encryptedPrivateKey = $this->crypt->encryptPrivateKey($privateKey, $params['password'], $params['uid']);
  234. // Save private key
  235. if ($encryptedPrivateKey) {
  236. $this->keyManager->setPrivateKey($this->user->getUser()->getUID(),
  237. $this->crypt->generateHeader() . $encryptedPrivateKey);
  238. } else {
  239. $this->logger->error('Encryption could not update users encryption password');
  240. }
  241. // NOTE: Session does not need to be updated as the
  242. // private key has not changed, only the passphrase
  243. // used to decrypt it has changed
  244. } else { // admin changed the password for a different user, create new keys and re-encrypt file keys
  245. $user = $params['uid'];
  246. $this->initMountPoints($user);
  247. $recoveryPassword = isset($params['recoveryPassword']) ? $params['recoveryPassword'] : null;
  248. // we generate new keys if...
  249. // ...we have a recovery password and the user enabled the recovery key
  250. // ...encryption was activated for the first time (no keys exists)
  251. // ...the user doesn't have any files
  252. if (
  253. ($this->recovery->isRecoveryEnabledForUser($user) && $recoveryPassword)
  254. || !$this->keyManager->userHasKeys($user)
  255. || !$this->util->userHasFiles($user)
  256. ) {
  257. // backup old keys
  258. //$this->backupAllKeys('recovery');
  259. $newUserPassword = $params['password'];
  260. $keyPair = $this->crypt->createKeyPair();
  261. // Save public key
  262. $this->keyManager->setPublicKey($user, $keyPair['publicKey']);
  263. // Encrypt private key with new password
  264. $encryptedKey = $this->crypt->encryptPrivateKey($keyPair['privateKey'], $newUserPassword, $user);
  265. if ($encryptedKey) {
  266. $this->keyManager->setPrivateKey($user, $this->crypt->generateHeader() . $encryptedKey);
  267. if ($recoveryPassword) { // if recovery key is set we can re-encrypt the key files
  268. $this->recovery->recoverUsersFiles($recoveryPassword, $user);
  269. }
  270. } else {
  271. $this->logger->error('Encryption Could not update users encryption password');
  272. }
  273. }
  274. }
  275. }
  276. /**
  277. * init mount points for given user
  278. *
  279. * @param string $user
  280. * @throws \OC\User\NoUserException
  281. */
  282. protected function initMountPoints($user) {
  283. Filesystem::initMountPoints($user);
  284. }
  285. /**
  286. * setup file system for user
  287. *
  288. * @param string $uid user id
  289. */
  290. protected function setupFS($uid) {
  291. \OC_Util::setupFS($uid);
  292. }
  293. }