json_api.php 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116
  1. <?php
  2. /*
  3. see /api
  4. */
  5. require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/prerequisites.inc.php';
  6. cors("set_headers");
  7. header('Content-Type: application/json');
  8. error_reporting(0);
  9. function api_log($_data) {
  10. global $redis;
  11. $data_var = array();
  12. foreach ($_data as $data => &$value) {
  13. if ($data == 'csrf_token') {
  14. continue;
  15. }
  16. $value = json_decode($value, true);
  17. if ($value) {
  18. if (is_array($value)) unset($value["csrf_token"]);
  19. foreach ($value as $key => &$val) {
  20. if(preg_match("/pass/i", $key)) {
  21. $val = '*';
  22. }
  23. }
  24. $value = json_encode($value);
  25. }
  26. $data_var[] = $data . "='" . $value . "'";
  27. }
  28. try {
  29. $log_line = array(
  30. 'time' => time(),
  31. 'uri' => $_SERVER['REQUEST_URI'],
  32. 'method' => $_SERVER['REQUEST_METHOD'],
  33. 'remote' => get_remote_ip(),
  34. 'data' => implode(', ', $data_var)
  35. );
  36. $redis->lPush('API_LOG', json_encode($log_line));
  37. }
  38. catch (RedisException $e) {
  39. $_SESSION['return'][] = array(
  40. 'type' => 'danger',
  41. 'msg' => 'Redis: '.$e
  42. );
  43. return false;
  44. }
  45. }
  46. if (isset($_GET['query'])) {
  47. $query = explode('/', $_GET['query']);
  48. $action = (isset($query[0])) ? $query[0] : null;
  49. $category = (isset($query[1])) ? $query[1] : null;
  50. $object = (isset($query[2])) ? $query[2] : null;
  51. $extra = (isset($query[3])) ? $query[3] : null;
  52. // accept json in request body
  53. if(strpos($_SERVER['HTTP_CONTENT_TYPE'], 'application/json') !== false) {
  54. $request = file_get_contents('php://input');
  55. $requestDecoded = json_decode($request, true);
  56. // check for valid json
  57. if ($action != 'get' && $requestDecoded === null) {
  58. http_response_code(400);
  59. echo json_encode(array(
  60. 'type' => 'error',
  61. 'msg' => 'Request body doesn\'t contain valid json!'
  62. ));
  63. exit;
  64. }
  65. // add
  66. if ($action == 'add') {
  67. $_POST['attr'] = $request;
  68. }
  69. // edit
  70. if ($action == 'edit') {
  71. $_POST['attr'] = json_encode($requestDecoded['attr']);
  72. $_POST['items'] = json_encode($requestDecoded['items']);
  73. }
  74. // delete
  75. if ($action == 'delete') {
  76. $_POST['items'] = $request;
  77. }
  78. }
  79. api_log($_POST);
  80. $request_incomplete = json_encode(array(
  81. 'type' => 'error',
  82. 'msg' => 'Cannot find attributes in post data'
  83. ));
  84. switch ($action) {
  85. case "add":
  86. if ($_SESSION['mailcow_cc_api_access'] == 'ro' || isset($_SESSION['pending_mailcow_cc_username'])) {
  87. http_response_code(403);
  88. echo json_encode(array(
  89. 'type' => 'error',
  90. 'msg' => 'API read/write access denied'
  91. ));
  92. exit();
  93. }
  94. function process_add_return($return) {
  95. $generic_failure = json_encode(array(
  96. 'type' => 'error',
  97. 'msg' => 'Cannot add item'
  98. ));
  99. $generic_success = json_encode(array(
  100. 'type' => 'success',
  101. 'msg' => 'Task completed'
  102. ));
  103. if ($return === false) {
  104. echo isset($_SESSION['return']) ? json_encode($_SESSION['return']) : $generic_failure;
  105. }
  106. else {
  107. echo isset($_SESSION['return']) ? json_encode($_SESSION['return']) : $generic_success;
  108. }
  109. }
  110. if (!isset($_POST['attr']) && $category != "fido2-registration" && $category != "webauthn-tfa-registration") {
  111. echo $request_incomplete;
  112. exit;
  113. }
  114. else {
  115. if ($category != "fido2-registration" && $category != "webauthn-tfa-registration") {
  116. $attr = (array)json_decode($_POST['attr'], true);
  117. }
  118. unset($attr['csrf_token']);
  119. }
  120. // only allow POST requests to POST API endpoints
  121. if ($_SERVER['REQUEST_METHOD'] != 'POST') {
  122. http_response_code(405);
  123. echo json_encode(array(
  124. 'type' => 'error',
  125. 'msg' => 'only POST method is allowed'
  126. ));
  127. exit();
  128. }
  129. switch ($category) {
  130. // fido2-registration via POST
  131. case "fido2-registration":
  132. header('Content-Type: application/json');
  133. if (isset($_SESSION["mailcow_cc_role"])) {
  134. $post = trim(file_get_contents('php://input'));
  135. if ($post) {
  136. $post = json_decode($post);
  137. }
  138. $clientDataJSON = base64_decode($post->clientDataJSON);
  139. $attestationObject = base64_decode($post->attestationObject);
  140. $challenge = $_SESSION['challenge'];
  141. try {
  142. $data = $WebAuthn->processCreate($clientDataJSON, $attestationObject, $challenge, $GLOBALS['FIDO2_UV_FLAG_REGISTER'], $GLOBALS['FIDO2_USER_PRESENT_FLAG']);
  143. }
  144. catch (Throwable $ex) {
  145. $return = new stdClass();
  146. $return->success = false;
  147. $return->msg = $ex->getMessage();
  148. echo json_encode($return);
  149. exit;
  150. }
  151. fido2(array("action" => "register", "registration" => $data));
  152. $return = new stdClass();
  153. $return->success = true;
  154. echo json_encode($return);
  155. exit;
  156. }
  157. else {
  158. echo $request_incomplete;
  159. exit;
  160. }
  161. break;
  162. case "webauthn-tfa-registration":
  163. if (isset($_SESSION["mailcow_cc_role"])) {
  164. // parse post data
  165. $post = trim(file_get_contents('php://input'));
  166. if ($post) $post = json_decode($post);
  167. // process registration data from authenticator
  168. try {
  169. // decode base64 strings
  170. $clientDataJSON = base64_decode($post->clientDataJSON);
  171. $attestationObject = base64_decode($post->attestationObject);
  172. // processCreate($clientDataJSON, $attestationObject, $challenge, $requireUserVerification=false, $requireUserPresent=true, $failIfRootMismatch=true)
  173. $data = $WebAuthn->processCreate($clientDataJSON, $attestationObject, $_SESSION['challenge'], false, true);
  174. // safe authenticator in mysql `tfa` table
  175. $_data['tfa_method'] = $post->tfa_method;
  176. $_data['key_id'] = $post->key_id;
  177. $_data['confirm_password'] = $post->confirm_password;
  178. $_data['registration'] = $data;
  179. set_tfa($_data);
  180. }
  181. catch (Throwable $ex) {
  182. // err
  183. $return = new stdClass();
  184. $return->success = false;
  185. $return->msg = $ex->getMessage();
  186. echo json_encode($return);
  187. exit;
  188. }
  189. // send response
  190. $return = new stdClass();
  191. $return->success = true;
  192. echo json_encode($return);
  193. exit;
  194. }
  195. else {
  196. // err - request incomplete
  197. echo $request_incomplete;
  198. exit;
  199. }
  200. break;
  201. case "time_limited_alias":
  202. process_add_return(mailbox('add', 'time_limited_alias', $attr));
  203. break;
  204. case "relayhost":
  205. process_add_return(relayhost('add', $attr));
  206. break;
  207. case "transport":
  208. process_add_return(transport('add', $attr));
  209. break;
  210. case "rsetting":
  211. process_add_return(rsettings('add', $attr));
  212. break;
  213. case "mailbox":
  214. switch ($object) {
  215. case "template":
  216. process_add_return(mailbox('add', 'mailbox_templates', $attr));
  217. break;
  218. default:
  219. process_add_return(mailbox('add', 'mailbox', $attr));
  220. break;
  221. }
  222. break;
  223. case "oauth2-client":
  224. process_add_return(oauth2('add', 'client', $attr));
  225. break;
  226. case "domain":
  227. switch ($object) {
  228. case "template":
  229. process_add_return(mailbox('add', 'domain_templates', $attr));
  230. break;
  231. default:
  232. process_add_return(mailbox('add', 'domain', $attr));
  233. break;
  234. }
  235. break;
  236. case "resource":
  237. process_add_return(mailbox('add', 'resource', $attr));
  238. break;
  239. case "alias":
  240. process_add_return(mailbox('add', 'alias', $attr));
  241. break;
  242. case "filter":
  243. process_add_return(mailbox('add', 'filter', $attr));
  244. break;
  245. case "global-filter":
  246. process_add_return(mailbox('add', 'global_filter', $attr));
  247. break;
  248. case "domain-policy":
  249. process_add_return(policy('add', 'domain', $attr));
  250. break;
  251. case "mailbox-policy":
  252. process_add_return(policy('add', 'mailbox', $attr));
  253. break;
  254. case "alias-domain":
  255. process_add_return(mailbox('add', 'alias_domain', $attr));
  256. break;
  257. case "fwdhost":
  258. process_add_return(fwdhost('add', $attr));
  259. break;
  260. case "dkim":
  261. process_add_return(dkim('add', $attr));
  262. break;
  263. case "dkim_duplicate":
  264. process_add_return(dkim('duplicate', $attr));
  265. break;
  266. case "dkim_import":
  267. process_add_return(dkim('import', $attr));
  268. break;
  269. case "domain-admin":
  270. process_add_return(domain_admin('add', $attr));
  271. break;
  272. case "sso":
  273. switch ($object) {
  274. case "domain-admin":
  275. $data = domain_admin_sso('issue', $attr);
  276. if($data) {
  277. echo json_encode($data);
  278. exit(0);
  279. }
  280. process_add_return($data);
  281. break;
  282. }
  283. break;
  284. case "admin":
  285. process_add_return(admin('add', $attr));
  286. break;
  287. case "syncjob":
  288. process_add_return(mailbox('add', 'syncjob', $attr));
  289. break;
  290. case "bcc":
  291. process_add_return(bcc('add', $attr));
  292. break;
  293. case "recipient_map":
  294. process_add_return(recipient_map('add', $attr));
  295. break;
  296. case "tls-policy-map":
  297. process_add_return(tls_policy_maps('add', $attr));
  298. break;
  299. case "app-passwd":
  300. process_add_return(app_passwd('add', $attr));
  301. break;
  302. // return no route found if no case is matched
  303. default:
  304. http_response_code(404);
  305. echo json_encode(array(
  306. 'type' => 'error',
  307. 'msg' => 'route not found'
  308. ));
  309. exit();
  310. }
  311. break;
  312. case "process":
  313. // only allow POST requests to process API endpoints
  314. if ($_SERVER['REQUEST_METHOD'] != 'POST') {
  315. http_response_code(405);
  316. echo json_encode(array(
  317. 'type' => 'error',
  318. 'msg' => 'only POST method is allowed'
  319. ));
  320. exit();
  321. }
  322. switch ($category) {
  323. case "fido2-args":
  324. header('Content-Type: application/json');
  325. $post = trim(file_get_contents('php://input'));
  326. if ($post) {
  327. $post = json_decode($post);
  328. }
  329. $clientDataJSON = base64_decode($post->clientDataJSON);
  330. $authenticatorData = base64_decode($post->authenticatorData);
  331. $signature = base64_decode($post->signature);
  332. $id = base64_decode($post->id);
  333. $challenge = $_SESSION['challenge'];
  334. $process_fido2 = fido2(array("action" => "get_by_b64cid", "cid" => $post->id));
  335. if ($process_fido2['pub_key'] === false) {
  336. $return = new stdClass();
  337. $return->success = false;
  338. echo json_encode($return);
  339. exit;
  340. }
  341. try {
  342. $WebAuthn->processGet($clientDataJSON, $authenticatorData, $signature, $process_fido2['pub_key'], $challenge, null, $GLOBALS['FIDO2_UV_FLAG_LOGIN'], $GLOBALS['FIDO2_USER_PRESENT_FLAG']);
  343. }
  344. catch (Throwable $ex) {
  345. unset($process_fido2);
  346. $return = new stdClass();
  347. $return->success = false;
  348. echo json_encode($return);
  349. exit;
  350. }
  351. $return = new stdClass();
  352. $return->success = true;
  353. $stmt = $pdo->prepare("SELECT `superadmin` FROM `admin` WHERE `username` = :username");
  354. $stmt->execute(array(':username' => $process_fido2['username']));
  355. $obj_props = $stmt->fetch(PDO::FETCH_ASSOC);
  356. if ($obj_props['superadmin'] === 1) {
  357. $_SESSION["mailcow_cc_role"] = "admin";
  358. }
  359. elseif ($obj_props['superadmin'] === 0) {
  360. $_SESSION["mailcow_cc_role"] = "domainadmin";
  361. }
  362. else {
  363. $stmt = $pdo->prepare("SELECT `username` FROM `mailbox` WHERE `username` = :username");
  364. $stmt->execute(array(':username' => $process_fido2['username']));
  365. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  366. if ($row['username'] == $process_fido2['username']) {
  367. $_SESSION["mailcow_cc_role"] = "user";
  368. }
  369. }
  370. if (empty($_SESSION["mailcow_cc_role"])) {
  371. session_unset();
  372. session_destroy();
  373. exit;
  374. }
  375. $_SESSION["mailcow_cc_username"] = $process_fido2['username'];
  376. $_SESSION["fido2_cid"] = $process_fido2['cid'];
  377. unset($_SESSION["challenge"]);
  378. $_SESSION['return'][] = array(
  379. 'type' => 'success',
  380. 'log' => array("fido2_login"),
  381. 'msg' => array('logged_in_as', $process_fido2['username'])
  382. );
  383. echo json_encode($return);
  384. break;
  385. }
  386. break;
  387. case "get":
  388. function process_get_return($data, $object = true) {
  389. if ($object === true) {
  390. $ret_str = '{}';
  391. }
  392. else {
  393. $ret_str = '[]';
  394. }
  395. echo (!isset($data) || empty($data)) ? $ret_str : json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  396. }
  397. // only allow GET requests to GET API endpoints
  398. if ($_SERVER['REQUEST_METHOD'] != 'GET') {
  399. http_response_code(405);
  400. echo json_encode(array(
  401. 'type' => 'error',
  402. 'msg' => 'only GET method is allowed'
  403. ));
  404. exit();
  405. }
  406. switch ($category) {
  407. // fido2
  408. case "fido2-registration":
  409. header('Content-Type: application/json');
  410. if (isset($_SESSION["mailcow_cc_role"])) {
  411. // Exclude existing CredentialIds, if any
  412. $excludeCredentialIds = fido2(array("action" => "get_user_cids"));
  413. $createArgs = $WebAuthn->getCreateArgs($_SESSION["mailcow_cc_username"], $_SESSION["mailcow_cc_username"], $_SESSION["mailcow_cc_username"], 30, true, $GLOBALS['FIDO2_UV_FLAG_REGISTER'], null, $excludeCredentialIds);
  414. print(json_encode($createArgs));
  415. $_SESSION['challenge'] = $WebAuthn->getChallenge();
  416. return;
  417. }
  418. else {
  419. return;
  420. }
  421. break;
  422. case "fido2-get-args":
  423. header('Content-Type: application/json');
  424. // Login without username, no ids!
  425. // $ids = fido2(array("action" => "get_all_cids"));
  426. // if (count($ids) == 0) {
  427. // return;
  428. // }
  429. $ids = NULL;
  430. $getArgs = $WebAuthn->getGetArgs($ids, 30, false, false, false, false, $GLOBALS['FIDO2_UV_FLAG_LOGIN']);
  431. print(json_encode($getArgs));
  432. $_SESSION['challenge'] = $WebAuthn->getChallenge();
  433. return;
  434. break;
  435. // webauthn two factor authentication
  436. case "webauthn-tfa-registration":
  437. if (isset($_SESSION["mailcow_cc_role"])) {
  438. // Exclude existing CredentialIds, if any
  439. $stmt = $pdo->prepare("SELECT `keyHandle` FROM `tfa` WHERE username = :username AND authmech = :authmech");
  440. $stmt->execute(array(
  441. ':username' => $_SESSION['mailcow_cc_username'],
  442. ':authmech' => 'webauthn'
  443. ));
  444. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  445. while($row = array_shift($rows)) {
  446. $excludeCredentialIds[] = base64_decode($row['keyHandle']);
  447. }
  448. // getCreateArgs($userId, $userName, $userDisplayName, $timeout=20, $requireResidentKey=false, $requireUserVerification=false, $crossPlatformAttachment=null, $excludeCredentialIds=array())
  449. // cross-platform: true, if type internal is not allowed
  450. // false, if only internal is allowed
  451. // null, if internal and cross-platform is allowed
  452. $createArgs = $WebAuthn->getCreateArgs($_SESSION["mailcow_cc_username"], $_SESSION["mailcow_cc_username"], $_SESSION["mailcow_cc_username"], 30, false, $GLOBALS['WEBAUTHN_UV_FLAG_REGISTER'], null, $excludeCredentialIds);
  453. print(json_encode($createArgs));
  454. $_SESSION['challenge'] = $WebAuthn->getChallenge();
  455. return;
  456. }
  457. else {
  458. return;
  459. }
  460. break;
  461. case "webauthn-tfa-get-args":
  462. $stmt = $pdo->prepare("SELECT `keyHandle` FROM `tfa` WHERE username = :username AND authmech = :authmech");
  463. $stmt->execute(array(
  464. ':username' => $_SESSION['pending_mailcow_cc_username'],
  465. ':authmech' => 'webauthn'
  466. ));
  467. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  468. if (count($rows) == 0) {
  469. print(json_encode(array(
  470. 'type' => 'error',
  471. 'msg' => 'Cannot find matching credentialIds'
  472. )));
  473. exit;
  474. }
  475. while($row = array_shift($rows)) {
  476. $cids[] = base64_decode($row['keyHandle']);
  477. }
  478. $getArgs = $WebAuthn->getGetArgs($cids, 30, false, false, false, false, $GLOBALS['WEBAUTHN_UV_FLAG_LOGIN']);
  479. $getArgs->publicKey->extensions = array('appid' => "https://".$getArgs->publicKey->rpId);
  480. print(json_encode($getArgs));
  481. $_SESSION['challenge'] = $WebAuthn->getChallenge();
  482. return;
  483. break;
  484. case "fail2ban":
  485. if (!isset($_SESSION['mailcow_cc_role'])){
  486. switch ($object) {
  487. case 'banlist':
  488. header('Content-Type: text/plain');
  489. echo fail2ban('banlist', 'get', $extra);
  490. break;
  491. }
  492. }
  493. break;
  494. }
  495. if (isset($_SESSION['mailcow_cc_role'])) {
  496. switch ($category) {
  497. case "rspamd":
  498. switch ($object) {
  499. case "actions":
  500. $data = rspamd_actions();
  501. if ($data) {
  502. echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  503. }
  504. else {
  505. echo '{}';
  506. }
  507. break;
  508. }
  509. break;
  510. case "domain":
  511. switch ($object) {
  512. case "datatables":
  513. $table = ['domain', 'd'];
  514. $primaryKey = 'domain';
  515. $columns = [
  516. ['db' => 'domain', 'dt' => 2],
  517. ['db' => 'aliases', 'dt' => 3, 'order_subquery' => "SELECT COUNT(*) FROM `alias` WHERE (`domain`= `d`.`domain` OR `domain` IN (SELECT `alias_domain` FROM `alias_domain` WHERE `target_domain` = `d`.`domain`)) AND `address` NOT IN (SELECT `username` FROM `mailbox`)"],
  518. ['db' => 'mailboxes', 'dt' => 4, 'order_subquery' => "SELECT COUNT(*) FROM `mailbox` WHERE `mailbox`.`domain` = `d`.`domain` AND (`mailbox`.`kind` = '' OR `mailbox`.`kind` = NULL)"],
  519. ['db' => 'quota', 'dt' => 5, 'order_subquery' => "SELECT COALESCE(SUM(`mailbox`.`quota`), 0) FROM `mailbox` WHERE `mailbox`.`domain` = `d`.`domain` AND (`mailbox`.`kind` = '' OR `mailbox`.`kind` = NULL)"],
  520. ['db' => 'stats', 'dt' => 6, 'dummy' => true, 'order_subquery' => "SELECT SUM(bytes) FROM `quota2` WHERE `quota2`.`username` IN (SELECT `username` FROM `mailbox` WHERE `domain` = `d`.`domain`)"],
  521. ['db' => 'defquota', 'dt' => 7],
  522. ['db' => 'maxquota', 'dt' => 8],
  523. ['db' => 'backupmx', 'dt' => 10],
  524. ['db' => 'tags', 'dt' => 14, 'dummy' => true, 'search' => ['join' => 'LEFT JOIN `tags_domain` AS `td` ON `td`.`domain` = `d`.`domain`', 'where_column' => '`td`.`tag_name`']],
  525. ['db' => 'active', 'dt' => 15],
  526. ];
  527. require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/lib/ssp.class.php';
  528. global $pdo;
  529. if($_SESSION['mailcow_cc_role'] === 'admin') {
  530. $data = SSP::simple($_GET, $pdo, $table, $primaryKey, $columns);
  531. } elseif ($_SESSION['mailcow_cc_role'] === 'domainadmin') {
  532. $data = SSP::complex($_GET, $pdo, $table, $primaryKey, $columns,
  533. 'INNER JOIN domain_admins as da ON da.domain = d.domain',
  534. [
  535. 'condition' => 'da.active = 1 and da.username = :username',
  536. 'bindings' => ['username' => $_SESSION['mailcow_cc_username']]
  537. ]);
  538. }
  539. if (!empty($data['data'])) {
  540. $domainsData = [];
  541. foreach ($data['data'] as $domain) {
  542. if ($details = mailbox('get', 'domain_details', $domain[2])) {
  543. $domainsData[] = $details;
  544. }
  545. }
  546. $data['data'] = $domainsData;
  547. }
  548. process_get_return($data);
  549. break;
  550. case "all":
  551. $tags = null;
  552. if (isset($_GET['tags']) && $_GET['tags'] != '')
  553. $tags = explode(',', $_GET['tags']);
  554. $domains = mailbox('get', 'domains', null, $tags);
  555. if (!empty($domains)) {
  556. foreach ($domains as $domain) {
  557. if ($details = mailbox('get', 'domain_details', $domain)) {
  558. $data[] = $details;
  559. }
  560. else {
  561. continue;
  562. }
  563. }
  564. process_get_return($data);
  565. }
  566. else {
  567. echo '{}';
  568. }
  569. break;
  570. case "template":
  571. switch ($extra){
  572. case "all":
  573. process_get_return(mailbox('get', 'domain_templates'));
  574. break;
  575. default:
  576. process_get_return(mailbox('get', 'domain_templates', $extra));
  577. break;
  578. }
  579. break;
  580. default:
  581. $data = mailbox('get', 'domain_details', $object);
  582. process_get_return($data);
  583. break;
  584. }
  585. break;
  586. case "passwordpolicy":
  587. switch ($object) {
  588. case "html":
  589. $password_complexity_rules = password_complexity('html');
  590. if ($password_complexity_rules !== false) {
  591. process_get_return($password_complexity_rules);
  592. }
  593. else {
  594. echo '{}';
  595. }
  596. break;
  597. default:
  598. $password_complexity_rules = password_complexity('get');
  599. if ($password_complexity_rules !== false) {
  600. process_get_return($password_complexity_rules);
  601. }
  602. else {
  603. echo '{}';
  604. }
  605. break;
  606. }
  607. break;
  608. case "app-passwd":
  609. switch ($object) {
  610. case "all":
  611. if (empty($extra)) {
  612. $app_passwds = app_passwd('get');
  613. }
  614. else {
  615. $app_passwds = app_passwd('get', array('username' => $extra));
  616. }
  617. if (!empty($app_passwds)) {
  618. foreach ($app_passwds as $app_passwd) {
  619. $details = app_passwd('details', $app_passwd['id']);
  620. if ($details !== false) {
  621. $data[] = $details;
  622. }
  623. else {
  624. continue;
  625. }
  626. }
  627. process_get_return($data);
  628. }
  629. else {
  630. echo '{}';
  631. }
  632. break;
  633. default:
  634. $data = app_passwd('details', array('id' => $object['id']));
  635. process_get_return($data);
  636. break;
  637. }
  638. break;
  639. case "mailq":
  640. switch ($object) {
  641. case "all":
  642. $mailq = mailq('get');
  643. if (!empty($mailq)) {
  644. echo $mailq;
  645. }
  646. else {
  647. echo '[]';
  648. }
  649. break;
  650. }
  651. break;
  652. case "postcat":
  653. switch ($object) {
  654. default:
  655. $data = mailq('cat', array('qid' => $object));
  656. echo $data;
  657. break;
  658. }
  659. break;
  660. case "global_filters":
  661. $global_filters = mailbox('get', 'global_filter_details');
  662. switch ($object) {
  663. case "all":
  664. if (!empty($global_filters)) {
  665. process_get_return($global_filters);
  666. }
  667. else {
  668. echo '{}';
  669. }
  670. break;
  671. case "prefilter":
  672. if (!empty($global_filters['prefilter'])) {
  673. process_get_return($global_filters['prefilter']);
  674. }
  675. else {
  676. echo '{}';
  677. }
  678. break;
  679. case "postfilter":
  680. if (!empty($global_filters['postfilter'])) {
  681. process_get_return($global_filters['postfilter']);
  682. }
  683. else {
  684. echo '{}';
  685. }
  686. break;
  687. }
  688. break;
  689. case "rl-domain":
  690. switch ($object) {
  691. case "all":
  692. $domains = array_merge(mailbox('get', 'domains'), mailbox('get', 'alias_domains'));
  693. if (!empty($domains)) {
  694. foreach ($domains as $domain) {
  695. if ($details = ratelimit('get', 'domain', $domain)) {
  696. $details['domain'] = $domain;
  697. $data[] = $details;
  698. }
  699. else {
  700. continue;
  701. }
  702. }
  703. process_get_return($data);
  704. }
  705. else {
  706. echo '{}';
  707. }
  708. break;
  709. default:
  710. $data = ratelimit('get', 'domain', $object);
  711. process_get_return($data);
  712. break;
  713. }
  714. break;
  715. case "rl-mbox":
  716. switch ($object) {
  717. case "all":
  718. $domains = mailbox('get', 'domains');
  719. if (!empty($domains)) {
  720. foreach ($domains as $domain) {
  721. $mailboxes = mailbox('get', 'mailboxes', $domain);
  722. if (!empty($mailboxes)) {
  723. foreach ($mailboxes as $mailbox) {
  724. if ($details = ratelimit('get', 'mailbox', $mailbox)) {
  725. $details['mailbox'] = $mailbox;
  726. $data[] = $details;
  727. }
  728. else {
  729. continue;
  730. }
  731. }
  732. }
  733. }
  734. process_get_return($data);
  735. }
  736. else {
  737. echo '{}';
  738. }
  739. break;
  740. default:
  741. $data = ratelimit('get', 'mailbox', $object);
  742. process_get_return($data);
  743. break;
  744. }
  745. break;
  746. case "relayhost":
  747. switch ($object) {
  748. case "all":
  749. $relayhosts = relayhost('get');
  750. if (!empty($relayhosts)) {
  751. foreach ($relayhosts as $relayhost) {
  752. if ($details = relayhost('details', $relayhost['id'])) {
  753. $data[] = $details;
  754. }
  755. else {
  756. continue;
  757. }
  758. }
  759. process_get_return($data);
  760. }
  761. else {
  762. echo '{}';
  763. }
  764. break;
  765. default:
  766. $data = relayhost('details', $object);
  767. process_get_return($data);
  768. break;
  769. }
  770. break;
  771. case "last-login":
  772. if ($object) {
  773. // extra == days
  774. if (isset($extra) && intval($extra) >= 1) {
  775. $data = last_login('get', $object, intval($extra));
  776. }
  777. else {
  778. $data = last_login('get', $object);
  779. }
  780. process_get_return($data);
  781. }
  782. break;
  783. // Todo: move to delete
  784. case "reset-last-login":
  785. if ($object) {
  786. $data = last_login('reset', $object);
  787. process_get_return($data);
  788. }
  789. break;
  790. case "transport":
  791. switch ($object) {
  792. case "all":
  793. $transports = transport('get');
  794. if (!empty($transports)) {
  795. foreach ($transports as $transport) {
  796. if ($details = transport('details', $transport['id'])) {
  797. $data[] = $details;
  798. }
  799. else {
  800. continue;
  801. }
  802. }
  803. process_get_return($data);
  804. }
  805. else {
  806. echo '{}';
  807. }
  808. break;
  809. default:
  810. $data = transport('details', $object);
  811. process_get_return($data);
  812. break;
  813. }
  814. break;
  815. case "rsetting":
  816. switch ($object) {
  817. case "all":
  818. $rsettings = rsettings('get');
  819. if (!empty($rsettings)) {
  820. foreach ($rsettings as $rsetting) {
  821. if ($details = rsettings('details', $rsetting['id'])) {
  822. $data[] = $details;
  823. }
  824. else {
  825. continue;
  826. }
  827. }
  828. process_get_return($data);
  829. }
  830. else {
  831. echo '{}';
  832. }
  833. break;
  834. default:
  835. $data = rsettings('details', $object);
  836. process_get_return($data);
  837. break;
  838. }
  839. break;
  840. case "oauth2-client":
  841. switch ($object) {
  842. case "all":
  843. $clients = oauth2('get', 'clients');
  844. if (!empty($clients)) {
  845. foreach ($clients as $client) {
  846. if ($details = oauth2('details', 'client', $client)) {
  847. $data[] = $details;
  848. }
  849. else {
  850. continue;
  851. }
  852. }
  853. process_get_return($data);
  854. }
  855. else {
  856. echo '{}';
  857. }
  858. break;
  859. default:
  860. $data = oauth2('details', 'client', $object);
  861. process_get_return($data);
  862. break;
  863. }
  864. break;
  865. case "logs":
  866. switch ($object) {
  867. case "dovecot":
  868. // 0 is first record, so empty is fine
  869. if (isset($extra)) {
  870. $extra = preg_replace('/[^\d\-]/i', '', $extra);
  871. $logs = get_logs('dovecot-mailcow', $extra);
  872. }
  873. else {
  874. $logs = get_logs('dovecot-mailcow');
  875. }
  876. echo (isset($logs) && !empty($logs)) ? json_encode($logs, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) : '{}';
  877. break;
  878. case "ratelimited":
  879. // 0 is first record, so empty is fine
  880. if (isset($extra)) {
  881. $extra = preg_replace('/[^\d\-]/i', '', $extra);
  882. $logs = get_logs('ratelimited', $extra);
  883. }
  884. else {
  885. $logs = get_logs('ratelimited');
  886. }
  887. echo (isset($logs) && !empty($logs)) ? json_encode($logs, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) : '{}';
  888. break;
  889. case "netfilter":
  890. // 0 is first record, so empty is fine
  891. if (isset($extra)) {
  892. $extra = preg_replace('/[^\d\-]/i', '', $extra);
  893. $logs = get_logs('netfilter-mailcow', $extra);
  894. }
  895. else {
  896. $logs = get_logs('netfilter-mailcow');
  897. }
  898. echo (isset($logs) && !empty($logs)) ? json_encode($logs, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) : '{}';
  899. break;
  900. case "cron":
  901. // 0 is first record, so empty is fine
  902. if (isset($extra)) {
  903. $extra = preg_replace('/[^\d\-]/i', '', $extra);
  904. $logs = get_logs('cron-mailcow', $extra);
  905. }
  906. else {
  907. $logs = get_logs('cron-mailcow');
  908. }
  909. echo (isset($logs) && !empty($logs)) ? json_encode($logs, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) : '{}';
  910. break;
  911. case "postfix":
  912. // 0 is first record, so empty is fine
  913. if (isset($extra)) {
  914. $extra = preg_replace('/[^\d\-]/i', '', $extra);
  915. $logs = get_logs('postfix-mailcow', $extra);
  916. }
  917. else {
  918. $logs = get_logs('postfix-mailcow');
  919. }
  920. echo (isset($logs) && !empty($logs)) ? json_encode($logs, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) : '{}';
  921. break;
  922. case "autodiscover":
  923. // 0 is first record, so empty is fine
  924. if (isset($extra)) {
  925. $extra = preg_replace('/[^\d\-]/i', '', $extra);
  926. $logs = get_logs('autodiscover-mailcow', $extra);
  927. }
  928. else {
  929. $logs = get_logs('autodiscover-mailcow');
  930. }
  931. echo (isset($logs) && !empty($logs)) ? json_encode($logs, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) : '{}';
  932. break;
  933. case "sogo":
  934. // 0 is first record, so empty is fine
  935. if (isset($extra)) {
  936. $extra = preg_replace('/[^\d\-]/i', '', $extra);
  937. $logs = get_logs('sogo-mailcow', $extra);
  938. }
  939. else {
  940. $logs = get_logs('sogo-mailcow');
  941. }
  942. echo (isset($logs) && !empty($logs)) ? json_encode($logs, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) : '{}';
  943. break;
  944. case "ui":
  945. // 0 is first record, so empty is fine
  946. if (isset($extra)) {
  947. $extra = preg_replace('/[^\d\-]/i', '', $extra);
  948. $logs = get_logs('mailcow-ui', $extra);
  949. }
  950. else {
  951. $logs = get_logs('mailcow-ui');
  952. }
  953. echo (isset($logs) && !empty($logs)) ? json_encode($logs, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) : '{}';
  954. break;
  955. case "sasl":
  956. // 0 is first record, so empty is fine
  957. if (isset($extra)) {
  958. $extra = preg_replace('/[^\d\-]/i', '', $extra);
  959. $logs = get_logs('sasl', $extra);
  960. }
  961. else {
  962. $logs = get_logs('sasl');
  963. }
  964. echo (isset($logs) && !empty($logs)) ? json_encode($logs, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) : '{}';
  965. break;
  966. case "watchdog":
  967. // 0 is first record, so empty is fine
  968. if (isset($extra)) {
  969. $extra = preg_replace('/[^\d\-]/i', '', $extra);
  970. $logs = get_logs('watchdog-mailcow', $extra);
  971. }
  972. else {
  973. $logs = get_logs('watchdog-mailcow');
  974. }
  975. echo (isset($logs) && !empty($logs)) ? json_encode($logs, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) : '{}';
  976. break;
  977. case "acme":
  978. // 0 is first record, so empty is fine
  979. if (isset($extra)) {
  980. $extra = preg_replace('/[^\d\-]/i', '', $extra);
  981. $logs = get_logs('acme-mailcow', $extra);
  982. }
  983. else {
  984. $logs = get_logs('acme-mailcow');
  985. }
  986. echo (isset($logs) && !empty($logs)) ? json_encode($logs, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) : '{}';
  987. break;
  988. case "api":
  989. // 0 is first record, so empty is fine
  990. if (isset($extra)) {
  991. $extra = preg_replace('/[^\d\-]/i', '', $extra);
  992. $logs = get_logs('api-mailcow', $extra);
  993. }
  994. else {
  995. $logs = get_logs('api-mailcow');
  996. }
  997. echo (isset($logs) && !empty($logs)) ? json_encode($logs, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) : '{}';
  998. break;
  999. case "rspamd-history":
  1000. // 0 is first record, so empty is fine
  1001. if (isset($extra)) {
  1002. $extra = preg_replace('/[^\d\-]/i', '', $extra);
  1003. $logs = get_logs('rspamd-history', $extra);
  1004. }
  1005. else {
  1006. $logs = get_logs('rspamd-history');
  1007. }
  1008. echo (isset($logs) && !empty($logs)) ? json_encode($logs, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) : '{}';
  1009. break;
  1010. case "rspamd-stats":
  1011. $logs = get_logs('rspamd-stats');
  1012. echo (isset($logs) && !empty($logs)) ? json_encode($logs, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) : '{}';
  1013. break;
  1014. // return no route found if no case is matched
  1015. default:
  1016. http_response_code(404);
  1017. echo json_encode(array(
  1018. 'type' => 'error',
  1019. 'msg' => 'route not found'
  1020. ));
  1021. exit();
  1022. }
  1023. break;
  1024. case "mailbox":
  1025. switch ($object) {
  1026. case "datatables":
  1027. $table = ['mailbox', 'm'];
  1028. $primaryKey = 'username';
  1029. $columns = [
  1030. ['db' => 'username', 'dt' => 2],
  1031. ['db' => 'quota', 'dt' => 3],
  1032. ['db' => 'last_mail_login', 'dt' => 4, 'dummy' => true, 'order_subquery' => "SELECT MAX(`datetime`) FROM `sasl_log` WHERE `service` != 'SSO' AND `username` = `m`.`username`"],
  1033. ['db' => 'last_pw_change', 'dt' => 5, 'dummy' => true, 'order_subquery' => "JSON_EXTRACT(attributes, '$.passwd_update')"],
  1034. ['db' => 'in_use', 'dt' => 6, 'dummy' => true, 'order_subquery' => "(SELECT SUM(bytes) FROM `quota2` WHERE `quota2`.`username` = `m`.`username`) / `m`.`quota`"],
  1035. ['db' => 'messages', 'dt' => 17, 'dummy' => true, 'order_subquery' => "SELECT SUM(messages) FROM `quota2` WHERE `quota2`.`username` = `m`.`username`"],
  1036. ['db' => 'tags', 'dt' => 20, 'dummy' => true, 'search' => ['join' => 'LEFT JOIN `tags_mailbox` AS `tm` ON `tm`.`username` = `m`.`username`', 'where_column' => '`tm`.`tag_name`']],
  1037. ['db' => 'active', 'dt' => 21]
  1038. ];
  1039. require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/lib/ssp.class.php';
  1040. global $pdo;
  1041. if($_SESSION['mailcow_cc_role'] === 'admin') {
  1042. $data = SSP::complex($_GET, $pdo, $table, $primaryKey, $columns, null, "(`m`.`kind` = '' OR `m`.`kind` = NULL)");
  1043. } elseif ($_SESSION['mailcow_cc_role'] === 'domainadmin') {
  1044. $data = SSP::complex($_GET, $pdo, $table, $primaryKey, $columns,
  1045. 'INNER JOIN domain_admins as da ON da.domain = m.domain',
  1046. [
  1047. 'condition' => "(`m`.`kind` = '' OR `m`.`kind` = NULL) AND `da`.`active` = 1 AND `da`.`username` = :username",
  1048. 'bindings' => ['username' => $_SESSION['mailcow_cc_username']]
  1049. ]);
  1050. }
  1051. if (!empty($data['data'])) {
  1052. $mailboxData = [];
  1053. foreach ($data['data'] as $mailbox) {
  1054. if ($details = mailbox('get', 'mailbox_details', $mailbox[2])) {
  1055. $mailboxData[] = $details;
  1056. }
  1057. }
  1058. $data['data'] = $mailboxData;
  1059. }
  1060. process_get_return($data);
  1061. break;
  1062. case "all":
  1063. case "reduced":
  1064. $tags = null;
  1065. if (isset($_GET['tags']) && $_GET['tags'] != '')
  1066. $tags = explode(',', $_GET['tags']);
  1067. if (empty($extra)) $domains = mailbox('get', 'domains');
  1068. else $domains = explode(',', $extra);
  1069. if (!empty($domains)) {
  1070. foreach ($domains as $domain) {
  1071. $mailboxes = mailbox('get', 'mailboxes', $domain, $tags);
  1072. if (!empty($mailboxes)) {
  1073. foreach ($mailboxes as $mailbox) {
  1074. if ($details = mailbox('get', 'mailbox_details', $mailbox, $object)) $data[] = $details;
  1075. else continue;
  1076. }
  1077. }
  1078. }
  1079. process_get_return($data);
  1080. }
  1081. else {
  1082. echo '{}';
  1083. }
  1084. break;
  1085. case "template":
  1086. switch ($extra){
  1087. case "all":
  1088. process_get_return(mailbox('get', 'mailbox_templates'));
  1089. break;
  1090. default:
  1091. process_get_return(mailbox('get', 'mailbox_templates', $extra));
  1092. break;
  1093. }
  1094. break;
  1095. default:
  1096. $tags = null;
  1097. if (isset($_GET['tags']) && $_GET['tags'] != '')
  1098. $tags = explode(',', $_GET['tags']);
  1099. if ($tags === null) {
  1100. $data = mailbox('get', 'mailbox_details', $object);
  1101. process_get_return($data);
  1102. } else {
  1103. $mailboxes = mailbox('get', 'mailboxes', $object, $tags);
  1104. if (is_array($mailboxes)) {
  1105. foreach ($mailboxes as $mailbox) {
  1106. if ($details = mailbox('get', 'mailbox_details', $mailbox))
  1107. $data[] = $details;
  1108. }
  1109. }
  1110. process_get_return($data, false);
  1111. }
  1112. break;
  1113. }
  1114. break;
  1115. case "bcc-destination-options":
  1116. $domains = mailbox('get', 'domains');
  1117. $alias_domains = mailbox('get', 'alias_domains');
  1118. $data = array();
  1119. if (!empty($domains)) {
  1120. foreach ($domains as $domain) {
  1121. $data['domains'][] = $domain;
  1122. $mailboxes = mailbox('get', 'mailboxes', $domain);
  1123. foreach ($mailboxes as $mailbox) {
  1124. $data['mailboxes'][$mailbox][] = $mailbox;
  1125. $user_alias_details = user_get_alias_details($mailbox);
  1126. foreach ($user_alias_details['direct_aliases'] as $k => $v) {
  1127. $data['mailboxes'][$mailbox][] = $k;
  1128. }
  1129. foreach ($user_alias_details['shared_aliases'] as $k => $v) {
  1130. $data['mailboxes'][$mailbox][] = $k;
  1131. }
  1132. }
  1133. }
  1134. }
  1135. if (!empty($alias_domains)) {
  1136. foreach ($alias_domains as $alias_domain) {
  1137. $data['alias_domains'][] = $alias_domain;
  1138. }
  1139. }
  1140. process_get_return($data);
  1141. break;
  1142. case "syncjobs":
  1143. switch ($object) {
  1144. case "all":
  1145. $domains = mailbox('get', 'domains');
  1146. if (!empty($domains)) {
  1147. foreach ($domains as $domain) {
  1148. $mailboxes = mailbox('get', 'mailboxes', $domain);
  1149. if (!empty($mailboxes)) {
  1150. foreach ($mailboxes as $mailbox) {
  1151. $syncjobs = mailbox('get', 'syncjobs', $mailbox);
  1152. if (!empty($syncjobs)) {
  1153. foreach ($syncjobs as $syncjob) {
  1154. if (isset($extra)) {
  1155. $details = mailbox('get', 'syncjob_details', $syncjob, explode(',', $extra));
  1156. }
  1157. else {
  1158. $details = mailbox('get', 'syncjob_details', $syncjob);
  1159. }
  1160. if ($details) {
  1161. $data[] = $details;
  1162. }
  1163. else {
  1164. continue;
  1165. }
  1166. }
  1167. }
  1168. }
  1169. }
  1170. }
  1171. process_get_return($data);
  1172. }
  1173. else {
  1174. echo '{}';
  1175. }
  1176. break;
  1177. default:
  1178. $syncjobs = mailbox('get', 'syncjobs', $object);
  1179. if (!empty($syncjobs)) {
  1180. foreach ($syncjobs as $syncjob) {
  1181. if (isset($extra)) {
  1182. $details = mailbox('get', 'syncjob_details', $syncjob, explode(',', $extra));
  1183. }
  1184. else {
  1185. $details = mailbox('get', 'syncjob_details', $syncjob);
  1186. }
  1187. if ($details) {
  1188. $data[] = $details;
  1189. }
  1190. else {
  1191. continue;
  1192. }
  1193. }
  1194. }
  1195. process_get_return($data);
  1196. break;
  1197. }
  1198. break;
  1199. case "active-user-sieve":
  1200. if (isset($object)) {
  1201. $sieve_filter = mailbox('get', 'active_user_sieve', $object);
  1202. if (!empty($sieve_filter)) {
  1203. $data[] = $sieve_filter;
  1204. }
  1205. }
  1206. process_get_return($data);
  1207. break;
  1208. case "filters":
  1209. switch ($object) {
  1210. case "all":
  1211. $domains = mailbox('get', 'domains');
  1212. if (!empty($domains)) {
  1213. foreach ($domains as $domain) {
  1214. $mailboxes = mailbox('get', 'mailboxes', $domain);
  1215. if (!empty($mailboxes)) {
  1216. foreach ($mailboxes as $mailbox) {
  1217. $filters = mailbox('get', 'filters', $mailbox);
  1218. if (!empty($filters)) {
  1219. foreach ($filters as $filter) {
  1220. if ($details = mailbox('get', 'filter_details', $filter)) {
  1221. $data[] = $details;
  1222. }
  1223. else {
  1224. continue;
  1225. }
  1226. }
  1227. }
  1228. }
  1229. }
  1230. }
  1231. process_get_return($data);
  1232. }
  1233. else {
  1234. echo '{}';
  1235. }
  1236. break;
  1237. default:
  1238. $filters = mailbox('get', 'filters', $object);
  1239. if (!empty($filters)) {
  1240. foreach ($filters as $filter) {
  1241. if ($details = mailbox('get', 'filter_details', $filter)) {
  1242. $data[] = $details;
  1243. }
  1244. else {
  1245. continue;
  1246. }
  1247. }
  1248. }
  1249. process_get_return($data);
  1250. break;
  1251. }
  1252. break;
  1253. case "bcc":
  1254. switch ($object) {
  1255. case "all":
  1256. $bcc_items = bcc('get');
  1257. if (!empty($bcc_items)) {
  1258. foreach ($bcc_items as $bcc_item) {
  1259. if ($details = bcc('details', $bcc_item)) {
  1260. $data[] = $details;
  1261. }
  1262. else {
  1263. continue;
  1264. }
  1265. }
  1266. }
  1267. process_get_return($data);
  1268. break;
  1269. default:
  1270. $data = bcc('details', $object);
  1271. if (!empty($data)) {
  1272. $data[] = $details;
  1273. }
  1274. process_get_return($data);
  1275. break;
  1276. }
  1277. break;
  1278. case "recipient_map":
  1279. switch ($object) {
  1280. case "all":
  1281. $recipient_map_items = recipient_map('get');
  1282. if (!empty($recipient_map_items)) {
  1283. foreach ($recipient_map_items as $recipient_map_item) {
  1284. if ($details = recipient_map('details', $recipient_map_item)) {
  1285. $data[] = $details;
  1286. }
  1287. else {
  1288. continue;
  1289. }
  1290. }
  1291. }
  1292. process_get_return($data);
  1293. break;
  1294. default:
  1295. $data = recipient_map('details', $object);
  1296. if (!empty($data)) {
  1297. $data[] = $details;
  1298. }
  1299. process_get_return($data);
  1300. break;
  1301. }
  1302. break;
  1303. case "tls-policy-map":
  1304. switch ($object) {
  1305. case "all":
  1306. $tls_policy_maps_items = tls_policy_maps('get');
  1307. if (!empty($tls_policy_maps_items)) {
  1308. foreach ($tls_policy_maps_items as $tls_policy_maps_item) {
  1309. if ($details = tls_policy_maps('details', $tls_policy_maps_item)) {
  1310. $data[] = $details;
  1311. }
  1312. else {
  1313. continue;
  1314. }
  1315. }
  1316. }
  1317. process_get_return($data);
  1318. break;
  1319. default:
  1320. $data = tls_policy_maps('details', $object);
  1321. if (!empty($data)) {
  1322. $data[] = $details;
  1323. }
  1324. process_get_return($data);
  1325. break;
  1326. }
  1327. break;
  1328. case "policy_wl_mailbox":
  1329. switch ($object) {
  1330. default:
  1331. $data = policy('get', 'mailbox', $object)['whitelist'];
  1332. process_get_return($data);
  1333. break;
  1334. }
  1335. break;
  1336. case "policy_bl_mailbox":
  1337. switch ($object) {
  1338. default:
  1339. $data = policy('get', 'mailbox', $object)['blacklist'];
  1340. process_get_return($data);
  1341. break;
  1342. }
  1343. break;
  1344. case "policy_wl_domain":
  1345. switch ($object) {
  1346. default:
  1347. $data = policy('get', 'domain', $object)['whitelist'];
  1348. process_get_return($data);
  1349. break;
  1350. }
  1351. break;
  1352. case "policy_bl_domain":
  1353. switch ($object) {
  1354. default:
  1355. $data = policy('get', 'domain', $object)['blacklist'];
  1356. process_get_return($data);
  1357. break;
  1358. }
  1359. break;
  1360. case "time_limited_aliases":
  1361. switch ($object) {
  1362. default:
  1363. $data = mailbox('get', 'time_limited_aliases', $object);
  1364. process_get_return($data);
  1365. break;
  1366. }
  1367. break;
  1368. case "fail2ban":
  1369. switch ($object) {
  1370. case 'banlist':
  1371. header('Content-Type: text/plain');
  1372. echo fail2ban('banlist', 'get', $extra);
  1373. break;
  1374. default:
  1375. $data = fail2ban('get');
  1376. process_get_return($data);
  1377. break;
  1378. }
  1379. break;
  1380. case "resource":
  1381. switch ($object) {
  1382. case "all":
  1383. $domains = mailbox('get', 'domains');
  1384. if (!empty($domains)) {
  1385. foreach ($domains as $domain) {
  1386. $resources = mailbox('get', 'resources', $domain);
  1387. if (!empty($resources)) {
  1388. foreach ($resources as $resource) {
  1389. if ($details = mailbox('get', 'resource_details', $resource)) {
  1390. $data[] = $details;
  1391. }
  1392. else {
  1393. continue;
  1394. }
  1395. }
  1396. }
  1397. }
  1398. process_get_return($data);
  1399. }
  1400. else {
  1401. echo '{}';
  1402. }
  1403. break;
  1404. default:
  1405. $data = mailbox('get', 'resource_details', $object);
  1406. process_get_return($data);
  1407. break;
  1408. }
  1409. break;
  1410. case "fwdhost":
  1411. switch ($object) {
  1412. case "all":
  1413. process_get_return(fwdhost('get'));
  1414. break;
  1415. default:
  1416. process_get_return(fwdhost('details', $object));
  1417. break;
  1418. }
  1419. break;
  1420. case "quarantine":
  1421. // "all" will not print details
  1422. switch ($object) {
  1423. case "all":
  1424. process_get_return(quarantine('get'), false);
  1425. break;
  1426. default:
  1427. process_get_return(quarantine('details', $object), false);
  1428. break;
  1429. }
  1430. break;
  1431. case "alias-domain":
  1432. switch ($object) {
  1433. case "all":
  1434. $alias_domains = mailbox('get', 'alias_domains');
  1435. if (!empty($alias_domains)) {
  1436. foreach ($alias_domains as $alias_domain) {
  1437. if ($details = mailbox('get', 'alias_domain_details', $alias_domain)) {
  1438. $data[] = $details;
  1439. }
  1440. else {
  1441. continue;
  1442. }
  1443. }
  1444. }
  1445. process_get_return($data);
  1446. break;
  1447. default:
  1448. process_get_return(mailbox('get', 'alias_domain_details', $object));
  1449. break;
  1450. }
  1451. break;
  1452. case "alias":
  1453. switch ($object) {
  1454. case "all":
  1455. if (empty($extra)) {
  1456. $domains = array_merge(mailbox('get', 'domains'), mailbox('get', 'alias_domains'));
  1457. }
  1458. else {
  1459. $domains = explode(',', $extra);
  1460. }
  1461. if (!empty($domains)) {
  1462. foreach ($domains as $domain) {
  1463. $aliases = mailbox('get', 'aliases', $domain);
  1464. if (!empty($aliases)) {
  1465. foreach ($aliases as $alias) {
  1466. if ($details = mailbox('get', 'alias_details', $alias)) {
  1467. $data[] = $details;
  1468. }
  1469. else {
  1470. continue;
  1471. }
  1472. }
  1473. }
  1474. }
  1475. process_get_return($data);
  1476. }
  1477. else {
  1478. echo '{}';
  1479. }
  1480. break;
  1481. default:
  1482. process_get_return(mailbox('get', 'alias_details', $object));
  1483. break;
  1484. }
  1485. break;
  1486. case "domain-admin":
  1487. switch ($object) {
  1488. case "all":
  1489. $domain_admins = domain_admin('get');
  1490. if (!empty($domain_admins)) {
  1491. foreach ($domain_admins as $domain_admin) {
  1492. if ($details = domain_admin('details', $domain_admin)) {
  1493. $data[] = $details;
  1494. }
  1495. else {
  1496. continue;
  1497. }
  1498. }
  1499. process_get_return($data);
  1500. }
  1501. else {
  1502. echo '{}';
  1503. }
  1504. break;
  1505. default:
  1506. process_get_return(domain_admin('details', $object));
  1507. break;
  1508. }
  1509. break;
  1510. case "admin":
  1511. switch ($object) {
  1512. case "all":
  1513. $admins = admin('get');
  1514. if (!empty($admins)) {
  1515. foreach ($admins as $admin) {
  1516. if ($details = admin('details', $admin)) {
  1517. $data[] = $details;
  1518. }
  1519. else {
  1520. continue;
  1521. }
  1522. }
  1523. process_get_return($data);
  1524. }
  1525. else {
  1526. echo '{}';
  1527. }
  1528. break;
  1529. default:
  1530. process_get_return(admin('details', $object));
  1531. break;
  1532. }
  1533. break;
  1534. case "dkim":
  1535. switch ($object) {
  1536. default:
  1537. $data = dkim('details', $object);
  1538. process_get_return($data);
  1539. break;
  1540. }
  1541. break;
  1542. case "presets":
  1543. switch ($object) {
  1544. case "rspamd":
  1545. process_get_return(presets('get', 'rspamd'));
  1546. break;
  1547. case "sieve":
  1548. process_get_return(presets('get', 'sieve'));
  1549. break;
  1550. }
  1551. break;
  1552. case "status":
  1553. if ($_SESSION['mailcow_cc_role'] == "admin") {
  1554. switch ($object) {
  1555. case "containers":
  1556. $containers = (docker('info'));
  1557. foreach ($containers as $container => $container_info) {
  1558. $container . ' (' . $container_info['Config']['Image'] . ')';
  1559. $containerstarttime = ($container_info['State']['StartedAt']);
  1560. $containerstate = ($container_info['State']['Status']);
  1561. $containerimage = ($container_info['Config']['Image']);
  1562. $temp[$container] = array(
  1563. 'type' => 'info',
  1564. 'container' => $container,
  1565. 'state' => $containerstate,
  1566. 'started_at' => $containerstarttime,
  1567. 'image' => $containerimage
  1568. );
  1569. }
  1570. echo json_encode($temp, JSON_UNESCAPED_SLASHES);
  1571. break;
  1572. case "container":
  1573. $container_stats = docker('container_stats', $extra);
  1574. echo json_encode($container_stats);
  1575. break;
  1576. case "vmail":
  1577. $exec_fields_vmail = array('cmd' => 'system', 'task' => 'df', 'dir' => '/var/vmail');
  1578. $vmail_df = explode(',', json_decode(docker('post', 'dovecot-mailcow', 'exec', $exec_fields_vmail), true));
  1579. $temp = array(
  1580. 'type' => 'info',
  1581. 'disk' => $vmail_df[0],
  1582. 'used' => $vmail_df[2],
  1583. 'total'=> $vmail_df[1],
  1584. 'used_percent' => $vmail_df[4]
  1585. );
  1586. echo json_encode($temp, JSON_UNESCAPED_SLASHES);
  1587. break;
  1588. case "solr":
  1589. $solr_status = solr_status();
  1590. $solr_size = ($solr_status['status']['dovecot-fts']['index']['size']);
  1591. $solr_documents = ($solr_status['status']['dovecot-fts']['index']['numDocs']);
  1592. if (strtolower(getenv('SKIP_SOLR')) != 'n') {
  1593. $solr_enabled = false;
  1594. }
  1595. else {
  1596. $solr_enabled = true;
  1597. }
  1598. echo json_encode(array(
  1599. 'type' => 'info',
  1600. 'solr_enabled' => $solr_enabled,
  1601. 'solr_size' => $solr_size,
  1602. 'solr_documents' => $solr_documents
  1603. ));
  1604. break;
  1605. case "host":
  1606. if (!$extra){
  1607. $stats = docker("host_stats");
  1608. echo json_encode($stats);
  1609. }
  1610. else if ($extra == "ip") {
  1611. // get public ips
  1612. $curl = curl_init();
  1613. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  1614. curl_setopt($curl, CURLOPT_POST, 0);
  1615. curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
  1616. curl_setopt($curl, CURLOPT_TIMEOUT, 15);
  1617. curl_setopt($curl, CURLOPT_URL, 'http://ipv4.mailcow.email');
  1618. $ipv4 = curl_exec($curl);
  1619. curl_setopt($curl, CURLOPT_URL, 'http://ipv6.mailcow.email');
  1620. $ipv6 = curl_exec($curl);
  1621. $ips = array(
  1622. "ipv4" => $ipv4,
  1623. "ipv6" => $ipv6
  1624. );
  1625. curl_close($curl);
  1626. echo json_encode($ips);
  1627. }
  1628. break;
  1629. case "version":
  1630. echo json_encode(array(
  1631. 'version' => $GLOBALS['MAILCOW_GIT_VERSION']
  1632. ));
  1633. break;
  1634. }
  1635. }
  1636. break;
  1637. case "spam-score":
  1638. $score = mailbox('get', 'spam_score', $object);
  1639. if ($score)
  1640. $score = array("score" => preg_replace("/\s+/", "", $score));
  1641. process_get_return($score);
  1642. case "identity_provider":
  1643. process_get_return(identity_provider('get'));
  1644. break;
  1645. break;
  1646. // return no route found if no case is matched
  1647. default:
  1648. http_response_code(404);
  1649. echo json_encode(array(
  1650. 'type' => 'error',
  1651. 'msg' => 'route not found'
  1652. ));
  1653. exit();
  1654. }
  1655. }
  1656. break;
  1657. case "delete":
  1658. if ($_SESSION['mailcow_cc_api_access'] == 'ro' || isset($_SESSION['pending_mailcow_cc_username']) || !isset($_SESSION["mailcow_cc_username"])) {
  1659. http_response_code(403);
  1660. echo json_encode(array(
  1661. 'type' => 'error',
  1662. 'msg' => 'API read/write access denied'
  1663. ));
  1664. exit();
  1665. }
  1666. function process_delete_return($return) {
  1667. $generic_failure = json_encode(array(
  1668. 'type' => 'error',
  1669. 'msg' => 'Cannot delete item'
  1670. ));
  1671. $generic_success = json_encode(array(
  1672. 'type' => 'success',
  1673. 'msg' => 'Task completed'
  1674. ));
  1675. if ($return === false) {
  1676. echo isset($_SESSION['return']) ? json_encode($_SESSION['return']) : $generic_failure;
  1677. }
  1678. else {
  1679. echo isset($_SESSION['return']) ? json_encode($_SESSION['return']) : $generic_success;
  1680. }
  1681. }
  1682. if (!isset($_POST['items'])) {
  1683. echo $request_incomplete;
  1684. exit;
  1685. }
  1686. else {
  1687. $items = (array)json_decode($_POST['items'], true);
  1688. }
  1689. // only allow POST requests to POST API endpoints
  1690. if ($_SERVER['REQUEST_METHOD'] != 'POST') {
  1691. http_response_code(405);
  1692. echo json_encode(array(
  1693. 'type' => 'error',
  1694. 'msg' => 'only POST method is allowed'
  1695. ));
  1696. exit();
  1697. }
  1698. switch ($category) {
  1699. case "alias":
  1700. process_delete_return(mailbox('delete', 'alias', array('id' => $items)));
  1701. break;
  1702. case "oauth2-client":
  1703. process_delete_return(oauth2('delete', 'client', array('id' => $items)));
  1704. break;
  1705. case "app-passwd":
  1706. process_delete_return(app_passwd('delete', array('id' => $items)));
  1707. break;
  1708. case "relayhost":
  1709. process_delete_return(relayhost('delete', array('id' => $items)));
  1710. break;
  1711. case "transport":
  1712. process_delete_return(transport('delete', array('id' => $items)));
  1713. break;
  1714. case "rsetting":
  1715. process_delete_return(rsettings('delete', array('id' => $items)));
  1716. break;
  1717. case "syncjob":
  1718. process_delete_return(mailbox('delete', 'syncjob', array('id' => $items)));
  1719. break;
  1720. case "filter":
  1721. process_delete_return(mailbox('delete', 'filter', array('id' => $items)));
  1722. break;
  1723. case "mailq":
  1724. process_delete_return(mailq('delete', array('qid' => $items)));
  1725. break;
  1726. case "qitem":
  1727. process_delete_return(quarantine('delete', array('id' => $items)));
  1728. break;
  1729. case "bcc":
  1730. process_delete_return(bcc('delete', array('id' => $items)));
  1731. break;
  1732. case "recipient_map":
  1733. process_delete_return(recipient_map('delete', array('id' => $items)));
  1734. break;
  1735. case "tls-policy-map":
  1736. process_delete_return(tls_policy_maps('delete', array('id' => $items)));
  1737. break;
  1738. case "fwdhost":
  1739. process_delete_return(fwdhost('delete', array('forwardinghost' => $items)));
  1740. break;
  1741. case "dkim":
  1742. process_delete_return(dkim('delete', array('domains' => $items)));
  1743. break;
  1744. case "domain":
  1745. switch ($object){
  1746. case "tag":
  1747. process_delete_return(mailbox('delete', 'tags_domain', array('tags' => $items, 'domain' => $extra)));
  1748. break;
  1749. case "template":
  1750. process_delete_return(mailbox('delete', 'domain_templates', array('ids' => $items)));
  1751. break;
  1752. default:
  1753. process_delete_return(mailbox('delete', 'domain', array('domain' => $items)));
  1754. }
  1755. break;
  1756. case "alias-domain":
  1757. process_delete_return(mailbox('delete', 'alias_domain', array('alias_domain' => $items)));
  1758. break;
  1759. case "mailbox":
  1760. switch ($object){
  1761. case "tag":
  1762. process_delete_return(mailbox('delete', 'tags_mailbox', array('tags' => $items, 'username' => $extra)));
  1763. break;
  1764. case "template":
  1765. process_delete_return(mailbox('delete', 'mailbox_templates', array('ids' => $items)));
  1766. break;
  1767. default:
  1768. process_delete_return(mailbox('delete', 'mailbox', array('username' => $items)));
  1769. }
  1770. break;
  1771. case "resource":
  1772. process_delete_return(mailbox('delete', 'resource', array('name' => $items)));
  1773. break;
  1774. case "mailbox-policy":
  1775. process_delete_return(policy('delete', 'mailbox', array('prefid' => $items)));
  1776. break;
  1777. case "domain-policy":
  1778. process_delete_return(policy('delete', 'domain', array('prefid' => $items)));
  1779. break;
  1780. case "time_limited_alias":
  1781. process_delete_return(mailbox('delete', 'time_limited_alias', array('address' => $items)));
  1782. break;
  1783. case "eas_cache":
  1784. process_delete_return(mailbox('delete', 'eas_cache', array('username' => $items)));
  1785. break;
  1786. case "sogo_profile":
  1787. process_delete_return(mailbox('delete', 'sogo_profile', array('username' => $items)));
  1788. break;
  1789. case "domain-admin":
  1790. process_delete_return(domain_admin('delete', array('username' => $items)));
  1791. break;
  1792. case "admin":
  1793. process_delete_return(admin('delete', array('username' => $items)));
  1794. break;
  1795. case "rlhash":
  1796. echo ratelimit('delete', null, implode($items));
  1797. break;
  1798. case "identity-provider":
  1799. process_delete_return(identity_provider('delete'));
  1800. break;
  1801. // return no route found if no case is matched
  1802. default:
  1803. http_response_code(404);
  1804. echo json_encode(array(
  1805. 'type' => 'error',
  1806. 'msg' => 'route not found'
  1807. ));
  1808. exit();
  1809. }
  1810. break;
  1811. case "edit":
  1812. if ($_SESSION['mailcow_cc_api_access'] == 'ro' || isset($_SESSION['pending_mailcow_cc_username']) || !isset($_SESSION["mailcow_cc_username"])) {
  1813. http_response_code(403);
  1814. echo json_encode(array(
  1815. 'type' => 'error',
  1816. 'msg' => 'API read/write access denied'
  1817. ));
  1818. exit();
  1819. }
  1820. function process_edit_return($return) {
  1821. $generic_failure = json_encode(array(
  1822. 'type' => 'error',
  1823. 'msg' => 'Cannot edit item'
  1824. ));
  1825. $generic_success = json_encode(array(
  1826. 'type' => 'success',
  1827. 'msg' => 'Task completed'
  1828. ));
  1829. if ($return === false) {
  1830. echo isset($_SESSION['return']) ? json_encode($_SESSION['return']) : $generic_failure;
  1831. }
  1832. else {
  1833. echo isset($_SESSION['return']) ? json_encode($_SESSION['return']) : $generic_success;
  1834. }
  1835. }
  1836. if (!isset($_POST['attr'])) {
  1837. echo $request_incomplete;
  1838. exit;
  1839. }
  1840. else {
  1841. $attr = (array)json_decode($_POST['attr'], true);
  1842. unset($attr['csrf_token']);
  1843. $items = isset($_POST['items']) ? (array)json_decode($_POST['items'], true) : null;
  1844. }
  1845. // only allow POST requests to POST API endpoints
  1846. if ($_SERVER['REQUEST_METHOD'] != 'POST') {
  1847. http_response_code(405);
  1848. echo json_encode(array(
  1849. 'type' => 'error',
  1850. 'msg' => 'only POST method is allowed'
  1851. ));
  1852. exit();
  1853. }
  1854. switch ($category) {
  1855. case "bcc":
  1856. process_edit_return(bcc('edit', array_merge(array('id' => $items), $attr)));
  1857. break;
  1858. case "pushover":
  1859. process_edit_return(pushover('edit', array_merge(array('username' => $items), $attr)));
  1860. break;
  1861. case "pushover-test":
  1862. process_edit_return(pushover('test', array_merge(array('username' => $items), $attr)));
  1863. break;
  1864. case "oauth2-client":
  1865. process_edit_return(oauth2('edit', 'client', array_merge(array('id' => $items), $attr)));
  1866. break;
  1867. case "recipient_map":
  1868. process_edit_return(recipient_map('edit', array_merge(array('id' => $items), $attr)));
  1869. break;
  1870. case "app-passwd":
  1871. process_edit_return(app_passwd('edit', array_merge(array('id' => $items), $attr)));
  1872. break;
  1873. case "tls-policy-map":
  1874. process_edit_return(tls_policy_maps('edit', array_merge(array('id' => $items), $attr)));
  1875. break;
  1876. case "alias":
  1877. process_edit_return(mailbox('edit', 'alias', array_merge(array('id' => $items), $attr)));
  1878. break;
  1879. case "rspamd-map":
  1880. process_edit_return(rspamd_maps('edit', array_merge(array('map' => $items), $attr)));
  1881. break;
  1882. case "fido2-fn":
  1883. process_edit_return(fido2(array('action' => 'edit_fn', 'fido2_attrs' => $attr)));
  1884. break;
  1885. case "app_links":
  1886. process_edit_return(customize('edit', 'app_links', $attr));
  1887. break;
  1888. case "passwordpolicy":
  1889. process_edit_return(password_complexity('edit', $attr));
  1890. break;
  1891. case "relayhost":
  1892. process_edit_return(relayhost('edit', array_merge(array('id' => $items), $attr)));
  1893. break;
  1894. case "transport":
  1895. process_edit_return(transport('edit', array_merge(array('id' => $items), $attr)));
  1896. break;
  1897. case "rsetting":
  1898. process_edit_return(rsettings('edit', array_merge(array('id' => $items), $attr)));
  1899. break;
  1900. case "delimiter_action":
  1901. process_edit_return(mailbox('edit', 'delimiter_action', array_merge(array('username' => $items), $attr)));
  1902. break;
  1903. case "tls_policy":
  1904. process_edit_return(mailbox('edit', 'tls_policy', array_merge(array('username' => $items), $attr)));
  1905. break;
  1906. case "quarantine_notification":
  1907. process_edit_return(mailbox('edit', 'quarantine_notification', array_merge(array('username' => $items), $attr)));
  1908. break;
  1909. case "quarantine_category":
  1910. process_edit_return(mailbox('edit', 'quarantine_category', array_merge(array('username' => $items), $attr)));
  1911. break;
  1912. case "qitem":
  1913. process_edit_return(quarantine('edit', array_merge(array('id' => $items), $attr)));
  1914. break;
  1915. case "quarantine":
  1916. process_edit_return(quarantine('edit', $attr));
  1917. break;
  1918. case "quota_notification":
  1919. process_edit_return(quota_notification('edit', $attr));
  1920. break;
  1921. case "quota_notification_bcc":
  1922. process_edit_return(quota_notification_bcc('edit', $attr));
  1923. break;
  1924. break;
  1925. case "mailq":
  1926. process_edit_return(mailq('edit', array_merge(array('qid' => $items), $attr)));
  1927. break;
  1928. case "time_limited_alias":
  1929. process_edit_return(mailbox('edit', 'time_limited_alias', array_merge(array('address' => $items), $attr)));
  1930. break;
  1931. case "mailbox":
  1932. switch ($object) {
  1933. case "template":
  1934. process_edit_return(mailbox('edit', 'mailbox_templates', array_merge(array('ids' => $items), $attr)));
  1935. break;
  1936. case "custom-attribute":
  1937. process_edit_return(mailbox('edit', 'mailbox_custom_attribute', array_merge(array('mailboxes' => $items), $attr)));
  1938. break;
  1939. default:
  1940. process_edit_return(mailbox('edit', 'mailbox', array_merge(array('username' => $items), $attr)));
  1941. break;
  1942. }
  1943. break;
  1944. case "syncjob":
  1945. process_edit_return(mailbox('edit', 'syncjob', array_merge(array('id' => $items), $attr)));
  1946. break;
  1947. case "filter":
  1948. process_edit_return(mailbox('edit', 'filter', array_merge(array('id' => $items), $attr)));
  1949. break;
  1950. case "resource":
  1951. process_edit_return(mailbox('edit', 'resource', array_merge(array('name' => $items), $attr)));
  1952. break;
  1953. case "domain":
  1954. switch ($object) {
  1955. case "template":
  1956. process_edit_return(mailbox('edit', 'domain_templates', array_merge(array('ids' => $items), $attr)));
  1957. break;
  1958. case "footer":
  1959. process_edit_return(mailbox('edit', 'domain_wide_footer', array_merge(array('domains' => $items), $attr)));
  1960. break;
  1961. default:
  1962. process_edit_return(mailbox('edit', 'domain', array_merge(array('domain' => $items), $attr)));
  1963. break;
  1964. }
  1965. break;
  1966. case "rl-domain":
  1967. process_edit_return(ratelimit('edit', 'domain', array_merge(array('object' => $items), $attr)));
  1968. break;
  1969. case "rl-mbox":
  1970. process_edit_return(ratelimit('edit', 'mailbox', array_merge(array('object' => $items), $attr)));
  1971. break;
  1972. case "user-acl":
  1973. process_edit_return(acl('edit', 'user', array_merge(array('username' => $items), $attr)));
  1974. break;
  1975. case "da-acl":
  1976. process_edit_return(acl('edit', 'domainadmin', array_merge(array('username' => $items), $attr)));
  1977. break;
  1978. case "alias-domain":
  1979. process_edit_return(mailbox('edit', 'alias_domain', array_merge(array('alias_domain' => $items), $attr)));
  1980. break;
  1981. case "spam-score":
  1982. process_edit_return(mailbox('edit', 'spam_score', array_merge(array('username' => $items), $attr)));
  1983. break;
  1984. case "domain-admin":
  1985. process_edit_return(domain_admin('edit', array_merge(array('username' => $items), $attr)));
  1986. break;
  1987. case "admin":
  1988. process_edit_return(admin('edit', array_merge(array('username' => $items), $attr)));
  1989. break;
  1990. case "fwdhost":
  1991. process_edit_return(fwdhost('edit', array_merge(array('fwdhost' => $items), $attr)));
  1992. break;
  1993. case "fail2ban":
  1994. switch ($object) {
  1995. case 'banlist':
  1996. process_edit_return(fail2ban('banlist', 'refresh', $items));
  1997. break;
  1998. default:
  1999. process_edit_return(fail2ban('edit', array_merge(array('network' => $items), $attr)));
  2000. break;
  2001. }
  2002. break;
  2003. case "ui_texts":
  2004. process_edit_return(customize('edit', 'ui_texts', $attr));
  2005. break;
  2006. case "ip_check":
  2007. process_edit_return(customize('edit', 'ip_check', $attr));
  2008. break;
  2009. case "self":
  2010. if ($_SESSION['mailcow_cc_role'] == "domainadmin") {
  2011. process_edit_return(domain_admin('edit', $attr));
  2012. }
  2013. elseif ($_SESSION['mailcow_cc_role'] == "user") {
  2014. process_edit_return(edit_user_account($attr));
  2015. }
  2016. break;
  2017. case "cors":
  2018. process_edit_return(cors('edit', $attr));
  2019. case "identity_provider":
  2020. case "identity-provider":
  2021. process_edit_return(identity_provider('edit', $attr));
  2022. break;
  2023. case "identity-provider-test":
  2024. process_edit_return(identity_provider('test', $attr));
  2025. break;
  2026. case "cors":
  2027. process_edit_return(cors('edit', $attr));
  2028. break;
  2029. // return no route found if no case is matched
  2030. default:
  2031. http_response_code(404);
  2032. echo json_encode(array(
  2033. 'type' => 'error',
  2034. 'msg' => 'route not found'
  2035. ));
  2036. exit();
  2037. }
  2038. break;
  2039. // return no route found if no case is matched
  2040. default:
  2041. http_response_code(404);
  2042. echo json_encode(array(
  2043. 'type' => 'error',
  2044. 'msg' => 'route not found'
  2045. ));
  2046. exit();
  2047. }
  2048. }
  2049. if (array_key_exists('mailcow_cc_api', $_SESSION) && $_SESSION['mailcow_cc_api'] === true) {
  2050. if (isset($_SESSION['mailcow_cc_api']) && $_SESSION['mailcow_cc_api'] === true) {
  2051. unset($_SESSION['return']);
  2052. }
  2053. }