json_api.php 79 KB

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