functions.domain_admin.inc.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. <?php
  2. function domain_admin($_action, $_data = null) {
  3. global $pdo;
  4. global $lang;
  5. $_data_log = $_data;
  6. !isset($_data_log['password']) ?: $_data_log['password'] = '*';
  7. !isset($_data_log['password2']) ?: $_data_log['password2'] = '*';
  8. !isset($_data_log['user_old_pass']) ?: $_data_log['user_old_pass'] = '*';
  9. !isset($_data_log['user_new_pass']) ?: $_data_log['user_new_pass'] = '*';
  10. !isset($_data_log['user_new_pass2']) ?: $_data_log['user_new_pass2'] = '*';
  11. switch ($_action) {
  12. case 'add':
  13. $username = strtolower(trim($_data['username']));
  14. $password = $_data['password'];
  15. $password2 = $_data['password2'];
  16. $domains = (array)$_data['domains'];
  17. $active = intval($_data['active']);
  18. if ($_SESSION['mailcow_cc_role'] != "admin") {
  19. $_SESSION['return'][] = array(
  20. 'type' => 'danger',
  21. 'log' => array(__FUNCTION__, $_action, $_data_log),
  22. 'msg' => 'access_denied'
  23. );
  24. return false;
  25. }
  26. if (empty($domains)) {
  27. $_SESSION['return'][] = array(
  28. 'type' => 'danger',
  29. 'log' => array(__FUNCTION__, $_action, $_data_log),
  30. 'msg' => 'domain_invalid'
  31. );
  32. return false;
  33. }
  34. if (!ctype_alnum(str_replace(array('_', '.', '-'), '', $username)) || empty ($username) || $username == 'API') {
  35. $_SESSION['return'][] = array(
  36. 'type' => 'danger',
  37. 'log' => array(__FUNCTION__, $_action, $_data_log),
  38. 'msg' => array('username_invalid', $username)
  39. );
  40. return false;
  41. }
  42. $stmt = $pdo->prepare("SELECT `username` FROM `mailbox`
  43. WHERE `username` = :username");
  44. $stmt->execute(array(':username' => $username));
  45. $num_results[] = count($stmt->fetchAll(PDO::FETCH_ASSOC));
  46. $stmt = $pdo->prepare("SELECT `username` FROM `admin`
  47. WHERE `username` = :username");
  48. $stmt->execute(array(':username' => $username));
  49. $num_results[] = count($stmt->fetchAll(PDO::FETCH_ASSOC));
  50. $stmt = $pdo->prepare("SELECT `username` FROM `domain_admins`
  51. WHERE `username` = :username");
  52. $stmt->execute(array(':username' => $username));
  53. $num_results[] = count($stmt->fetchAll(PDO::FETCH_ASSOC));
  54. foreach ($num_results as $num_results_each) {
  55. if ($num_results_each != 0) {
  56. $_SESSION['return'][] = array(
  57. 'type' => 'danger',
  58. 'log' => array(__FUNCTION__, $_action, $_data_log),
  59. 'msg' => array('object_exists', htmlspecialchars($username))
  60. );
  61. return false;
  62. }
  63. }
  64. if (password_check($password, $password2) !== true) {
  65. continue;
  66. }
  67. $password_hashed = hash_password($password);
  68. $valid_domains = 0;
  69. foreach ($domains as $domain) {
  70. if (!is_valid_domain_name($domain) || mailbox('get', 'domain_details', $domain) === false) {
  71. $_SESSION['return'][] = array(
  72. 'type' => 'danger',
  73. 'log' => array(__FUNCTION__, $_action, $_data_log),
  74. 'msg' => array('domain_invalid', htmlspecialchars($domain))
  75. );
  76. continue;
  77. }
  78. $valid_domains++;
  79. $stmt = $pdo->prepare("INSERT INTO `domain_admins` (`username`, `domain`, `created`, `active`)
  80. VALUES (:username, :domain, :created, :active)");
  81. $stmt->execute(array(
  82. ':username' => $username,
  83. ':domain' => $domain,
  84. ':created' => date('Y-m-d H:i:s'),
  85. ':active' => $active
  86. ));
  87. }
  88. if ($valid_domains != 0) {
  89. $stmt = $pdo->prepare("INSERT INTO `admin` (`username`, `password`, `superadmin`, `active`)
  90. VALUES (:username, :password_hashed, '0', :active)");
  91. $stmt->execute(array(
  92. ':username' => $username,
  93. ':password_hashed' => $password_hashed,
  94. ':active' => $active
  95. ));
  96. }
  97. $stmt = $pdo->prepare("INSERT INTO `da_acl` (`username`) VALUES (:username)");
  98. $stmt->execute(array(
  99. ':username' => $username
  100. ));
  101. $_SESSION['return'][] = array(
  102. 'type' => 'success',
  103. 'log' => array(__FUNCTION__, $_action, $_data_log),
  104. 'msg' => array('domain_admin_added', htmlspecialchars($username))
  105. );
  106. break;
  107. case 'edit':
  108. if ($_SESSION['mailcow_cc_role'] != "admin" && $_SESSION['mailcow_cc_role'] != "domainadmin") {
  109. $_SESSION['return'][] = array(
  110. 'type' => 'danger',
  111. 'log' => array(__FUNCTION__, $_action, $_data_log),
  112. 'msg' => 'access_denied'
  113. );
  114. return false;
  115. }
  116. // Administrator
  117. if ($_SESSION['mailcow_cc_role'] == "admin") {
  118. if (!is_array($_data['username'])) {
  119. $usernames = array();
  120. $usernames[] = $_data['username'];
  121. }
  122. else {
  123. $usernames = $_data['username'];
  124. }
  125. foreach ($usernames as $username) {
  126. $is_now = domain_admin('details', $username);
  127. $domains = (isset($_data['domains'])) ? (array)$_data['domains'] : null;
  128. if (!empty($is_now)) {
  129. $active = (isset($_data['active'])) ? intval($_data['active']) : $is_now['active'];
  130. $domains = (!empty($domains)) ? $domains : $is_now['selected_domains'];
  131. $username_new = (!empty($_data['username_new'])) ? $_data['username_new'] : $is_now['username'];
  132. }
  133. else {
  134. $_SESSION['return'][] = array(
  135. 'type' => 'danger',
  136. 'log' => array(__FUNCTION__, $_action, $_data_log),
  137. 'msg' => 'access_denied'
  138. );
  139. continue;
  140. }
  141. $password = $_data['password'];
  142. $password2 = $_data['password2'];
  143. if (!empty($domains)) {
  144. foreach ($domains as $domain) {
  145. if (!is_valid_domain_name($domain) || mailbox('get', 'domain_details', $domain) === false) {
  146. $_SESSION['return'][] = array(
  147. 'type' => 'danger',
  148. 'log' => array(__FUNCTION__, $_action, $_data_log),
  149. 'msg' => array('domain_invalid', htmlspecialchars($domain))
  150. );
  151. continue 2;
  152. }
  153. }
  154. }
  155. if (!ctype_alnum(str_replace(array('_', '.', '-'), '', $username_new))) {
  156. $_SESSION['return'][] = array(
  157. 'type' => 'danger',
  158. 'log' => array(__FUNCTION__, $_action, $_data_log),
  159. 'msg' => array('username_invalid', $username_new)
  160. );
  161. continue;
  162. }
  163. if ($username_new != $username) {
  164. if (!empty(domain_admin('details', $username_new)['username'])) {
  165. $_SESSION['return'][] = array(
  166. 'type' => 'danger',
  167. 'log' => array(__FUNCTION__, $_action, $_data_log),
  168. 'msg' => array('username_invalid', $username_new)
  169. );
  170. continue;
  171. }
  172. }
  173. $stmt = $pdo->prepare("DELETE FROM `domain_admins` WHERE `username` = :username");
  174. $stmt->execute(array(
  175. ':username' => $username,
  176. ));
  177. $stmt = $pdo->prepare("UPDATE `da_acl` SET `username` = :username_new WHERE `username` = :username");
  178. $stmt->execute(array(
  179. ':username_new' => $username_new,
  180. ':username' => $username
  181. ));
  182. if (!empty($domains)) {
  183. foreach ($domains as $domain) {
  184. $stmt = $pdo->prepare("INSERT INTO `domain_admins` (`username`, `domain`, `created`, `active`)
  185. VALUES (:username_new, :domain, :created, :active)");
  186. $stmt->execute(array(
  187. ':username_new' => $username_new,
  188. ':domain' => $domain,
  189. ':created' => date('Y-m-d H:i:s'),
  190. ':active' => $active
  191. ));
  192. }
  193. }
  194. if (!empty($password)) {
  195. if (password_check($password, $password2) !== true) {
  196. return false;
  197. }
  198. $password_hashed = hash_password($password);
  199. $stmt = $pdo->prepare("UPDATE `admin` SET `username` = :username_new, `active` = :active, `password` = :password_hashed WHERE `username` = :username");
  200. $stmt->execute(array(
  201. ':password_hashed' => $password_hashed,
  202. ':username_new' => $username_new,
  203. ':username' => $username,
  204. ':active' => $active
  205. ));
  206. if (isset($_data['disable_tfa'])) {
  207. $stmt = $pdo->prepare("UPDATE `tfa` SET `active` = '0' WHERE `username` = :username");
  208. $stmt->execute(array(':username' => $username));
  209. }
  210. else {
  211. $stmt = $pdo->prepare("UPDATE `tfa` SET `username` = :username_new WHERE `username` = :username");
  212. $stmt->execute(array(':username_new' => $username_new, ':username' => $username));
  213. }
  214. }
  215. else {
  216. $stmt = $pdo->prepare("UPDATE `admin` SET `username` = :username_new, `active` = :active WHERE `username` = :username");
  217. $stmt->execute(array(
  218. ':username_new' => $username_new,
  219. ':username' => $username,
  220. ':active' => $active
  221. ));
  222. if (isset($_data['disable_tfa'])) {
  223. $stmt = $pdo->prepare("UPDATE `tfa` SET `active` = '0' WHERE `username` = :username");
  224. $stmt->execute(array(':username' => $username));
  225. }
  226. else {
  227. $stmt = $pdo->prepare("UPDATE `tfa` SET `username` = :username_new WHERE `username` = :username");
  228. $stmt->execute(array(':username_new' => $username_new, ':username' => $username));
  229. }
  230. }
  231. $_SESSION['return'][] = array(
  232. 'type' => 'success',
  233. 'log' => array(__FUNCTION__, $_action, $_data_log),
  234. 'msg' => array('domain_admin_modified', htmlspecialchars($username))
  235. );
  236. }
  237. return true;
  238. }
  239. // Domain administrator
  240. // Can only edit itself
  241. elseif ($_SESSION['mailcow_cc_role'] == "domainadmin") {
  242. $username = $_SESSION['mailcow_cc_username'];
  243. $password_old = $_data['user_old_pass'];
  244. $password_new = $_data['user_new_pass'];
  245. $password_new2 = $_data['user_new_pass2'];
  246. $stmt = $pdo->prepare("SELECT `password` FROM `admin`
  247. WHERE `username` = :user");
  248. $stmt->execute(array(':user' => $username));
  249. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  250. if (!verify_hash($row['password'], $password_old)) {
  251. $_SESSION['return'][] = array(
  252. 'type' => 'danger',
  253. 'log' => array(__FUNCTION__, $_action, $_data_log),
  254. 'msg' => 'access_denied'
  255. );
  256. return false;
  257. }
  258. if (password_check($password_new, $password_new2) !== true) {
  259. return false;
  260. }
  261. $password_hashed = hash_password($password_new);
  262. $stmt = $pdo->prepare("UPDATE `admin` SET `password` = :password_hashed WHERE `username` = :username");
  263. $stmt->execute(array(
  264. ':password_hashed' => $password_hashed,
  265. ':username' => $username
  266. ));
  267. $_SESSION['return'][] = array(
  268. 'type' => 'success',
  269. 'log' => array(__FUNCTION__, $_action, $_data_log),
  270. 'msg' => array('domain_admin_modified', htmlspecialchars($username))
  271. );
  272. }
  273. break;
  274. case 'delete':
  275. if ($_SESSION['mailcow_cc_role'] != "admin") {
  276. $_SESSION['return'][] = array(
  277. 'type' => 'danger',
  278. 'log' => array(__FUNCTION__, $_action, $_data_log),
  279. 'msg' => 'access_denied'
  280. );
  281. return false;
  282. }
  283. $usernames = (array)$_data['username'];
  284. foreach ($usernames as $username) {
  285. if (empty(domain_admin('details', $username))) {
  286. $_SESSION['return'][] = array(
  287. 'type' => 'danger',
  288. 'log' => array(__FUNCTION__, $_action, $_data_log),
  289. 'msg' => array('username_invalid', $username)
  290. );
  291. continue;
  292. }
  293. $stmt = $pdo->prepare("DELETE FROM `domain_admins` WHERE `username` = :username");
  294. $stmt->execute(array(
  295. ':username' => $username,
  296. ));
  297. $stmt = $pdo->prepare("DELETE FROM `admin` WHERE `username` = :username");
  298. $stmt->execute(array(
  299. ':username' => $username,
  300. ));
  301. $stmt = $pdo->prepare("DELETE FROM `da_acl` WHERE `username` = :username");
  302. $stmt->execute(array(
  303. ':username' => $username,
  304. ));
  305. $stmt = $pdo->prepare("DELETE FROM `tfa` WHERE `username` = :username");
  306. $stmt->execute(array(
  307. ':username' => $username,
  308. ));
  309. $stmt = $pdo->prepare("DELETE FROM `fido2` WHERE `username` = :username");
  310. $stmt->execute(array(
  311. ':username' => $username,
  312. ));
  313. $_SESSION['return'][] = array(
  314. 'type' => 'success',
  315. 'log' => array(__FUNCTION__, $_action, $_data_log),
  316. 'msg' => array('domain_admin_removed', htmlspecialchars($username))
  317. );
  318. }
  319. break;
  320. case 'get':
  321. $domainadmins = array();
  322. if ($_SESSION['mailcow_cc_role'] != "admin") {
  323. $_SESSION['return'][] = array(
  324. 'type' => 'danger',
  325. 'log' => array(__FUNCTION__, $_action, $_data_log),
  326. 'msg' => 'access_denied'
  327. );
  328. return false;
  329. }
  330. $stmt = $pdo->query("SELECT DISTINCT
  331. `username`
  332. FROM `domain_admins`
  333. WHERE `username` IN (
  334. SELECT `username` FROM `admin`
  335. WHERE `superadmin`!='1'
  336. )");
  337. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  338. while ($row = array_shift($rows)) {
  339. $domainadmins[] = $row['username'];
  340. }
  341. return $domainadmins;
  342. break;
  343. case 'details':
  344. $domainadmindata = array();
  345. if ($_SESSION['mailcow_cc_role'] == "domainadmin" && $_data != $_SESSION['mailcow_cc_username']) {
  346. return false;
  347. }
  348. elseif ($_SESSION['mailcow_cc_role'] != "admin" || !isset($_data)) {
  349. return false;
  350. }
  351. if (!ctype_alnum(str_replace(array('_', '.', '-'), '', $_data))) {
  352. return false;
  353. }
  354. $stmt = $pdo->prepare("SELECT
  355. `tfa`.`active` AS `tfa_active`,
  356. `domain_admins`.`username`,
  357. `domain_admins`.`created`,
  358. `domain_admins`.`active` AS `active`
  359. FROM `domain_admins`
  360. LEFT OUTER JOIN `tfa` ON `tfa`.`username`=`domain_admins`.`username`
  361. WHERE `domain_admins`.`username`= :domain_admin");
  362. $stmt->execute(array(
  363. ':domain_admin' => $_data
  364. ));
  365. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  366. if (empty($row)) {
  367. return false;
  368. }
  369. $domainadmindata['username'] = $row['username'];
  370. $domainadmindata['tfa_active'] = (is_null($row['tfa_active'])) ? 0 : $row['tfa_active'];
  371. $domainadmindata['tfa_active_int'] = (is_null($row['tfa_active'])) ? 0 : $row['tfa_active'];
  372. $domainadmindata['active'] = $row['active'];
  373. $domainadmindata['active_int'] = $row['active'];
  374. $domainadmindata['created'] = $row['created'];
  375. // GET SELECTED
  376. $stmt = $pdo->prepare("SELECT `domain` FROM `domain`
  377. WHERE `domain` IN (
  378. SELECT `domain` FROM `domain_admins`
  379. WHERE `username`= :domain_admin)");
  380. $stmt->execute(array(':domain_admin' => $_data));
  381. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  382. while($row = array_shift($rows)) {
  383. $domainadmindata['selected_domains'][] = $row['domain'];
  384. }
  385. // GET UNSELECTED
  386. $stmt = $pdo->prepare("SELECT `domain` FROM `domain`
  387. WHERE `domain` NOT IN (
  388. SELECT `domain` FROM `domain_admins`
  389. WHERE `username`= :domain_admin)");
  390. $stmt->execute(array(':domain_admin' => $_data));
  391. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  392. while($row = array_shift($rows)) {
  393. $domainadmindata['unselected_domains'][] = $row['domain'];
  394. }
  395. if (!isset($domainadmindata['unselected_domains'])) {
  396. $domainadmindata['unselected_domains'] = "";
  397. }
  398. return $domainadmindata;
  399. break;
  400. }
  401. }
  402. function domain_admin_sso($_action, $_data) {
  403. global $pdo;
  404. switch ($_action) {
  405. case 'check':
  406. $token = $_data;
  407. $stmt = $pdo->prepare("SELECT `t1`.`username` FROM `da_sso` AS `t1` JOIN `admin` AS `t2` ON `t1`.`username` = `t2`.`username` WHERE `t1`.`token` = :token AND `t1`.`created` > DATE_SUB(NOW(), INTERVAL '30' SECOND) AND `t2`.`active` = 1 AND `t2`.`superadmin` = 0;");
  408. $stmt->execute(array(
  409. ':token' => preg_replace('/[^a-zA-Z0-9-]/', '', $token)
  410. ));
  411. $return = $stmt->fetch(PDO::FETCH_ASSOC);
  412. return empty($return['username']) ? false : $return['username'];
  413. case 'issue':
  414. if ($_SESSION['mailcow_cc_role'] != "admin") {
  415. $_SESSION['return'][] = array(
  416. 'type' => 'danger',
  417. 'log' => array(__FUNCTION__, $_action, $_data),
  418. 'msg' => 'access_denied'
  419. );
  420. return false;
  421. }
  422. $username = $_data['username'];
  423. $stmt = $pdo->prepare("SELECT `username` FROM `domain_admins`
  424. WHERE `username` = :username");
  425. $stmt->execute(array(':username' => $username));
  426. $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
  427. if ($num_results < 1) {
  428. $_SESSION['return'][] = array(
  429. 'type' => 'danger',
  430. 'log' => array(__FUNCTION__, $_action, $_data),
  431. 'msg' => array('object_doesnt_exist', htmlspecialchars($username))
  432. );
  433. return false;
  434. }
  435. $token = implode('-', array(
  436. strtoupper(bin2hex(random_bytes(3))),
  437. strtoupper(bin2hex(random_bytes(3))),
  438. strtoupper(bin2hex(random_bytes(3))),
  439. strtoupper(bin2hex(random_bytes(3))),
  440. strtoupper(bin2hex(random_bytes(3)))
  441. ));
  442. $stmt = $pdo->prepare("INSERT INTO `da_sso` (`username`, `token`)
  443. VALUES (:username, :token)");
  444. $stmt->execute(array(
  445. ':username' => $username,
  446. ':token' => $token
  447. ));
  448. // perform cleanup
  449. $pdo->query("DELETE FROM `da_sso` WHERE created < DATE_SUB(NOW(), INTERVAL '30' SECOND);");
  450. return ['token' => $token];
  451. break;
  452. }
  453. }