functions.dkim.inc.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?php
  2. function dkim($_action, $_data = null, $privkey = false) {
  3. global $redis;
  4. global $lang;
  5. switch ($_action) {
  6. case 'add':
  7. $key_length = intval($_data['key_size']);
  8. $dkim_selector = (isset($_data['dkim_selector'])) ? $_data['dkim_selector'] : 'dkim';
  9. $domains = array_map('trim', preg_split( "/( |,|;|\n)/", $_data['domains']));
  10. $domains = array_filter($domains);
  11. foreach ($domains as $domain) {
  12. if (!is_valid_domain_name($domain) || !is_numeric($key_length)) {
  13. $_SESSION['return'][] = array(
  14. 'type' => 'danger',
  15. 'log' => array(__FUNCTION__, $_action, $_data),
  16. 'msg' => array('dkim_domain_or_sel_invalid', $domain)
  17. );
  18. continue;
  19. }
  20. if ($redis->hGet('DKIM_PUB_KEYS', $domain)) {
  21. $_SESSION['return'][] = array(
  22. 'type' => 'danger',
  23. 'log' => array(__FUNCTION__, $_action, $_data),
  24. 'msg' => array('dkim_domain_or_sel_invalid', $domain)
  25. );
  26. continue;
  27. }
  28. if (!ctype_alnum(str_replace(['-', '_'], '', $dkim_selector))) {
  29. $_SESSION['return'][] = array(
  30. 'type' => 'danger',
  31. 'log' => array(__FUNCTION__, $_action, $_data),
  32. 'msg' => array('dkim_domain_or_sel_invalid', $domain)
  33. );
  34. continue;
  35. }
  36. if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $domain)) {
  37. $_SESSION['return'][] = array(
  38. 'type' => 'danger',
  39. 'log' => array(__FUNCTION__, $_action, $_data),
  40. 'msg' => array('access_denied', $domain)
  41. );
  42. continue;
  43. }
  44. $config = array(
  45. "digest_alg" => "sha256",
  46. "private_key_bits" => $key_length,
  47. "private_key_type" => OPENSSL_KEYTYPE_RSA,
  48. );
  49. if ($keypair_ressource = openssl_pkey_new($config)) {
  50. $key_details = openssl_pkey_get_details($keypair_ressource);
  51. $pubKey = implode(array_slice(
  52. array_filter(
  53. explode(PHP_EOL, $key_details['key'])
  54. ), 1, -1)
  55. );
  56. // Save public key and selector to redis
  57. try {
  58. $redis->hSet('DKIM_PUB_KEYS', $domain, $pubKey);
  59. $redis->hSet('DKIM_SELECTORS', $domain, $dkim_selector);
  60. }
  61. catch (RedisException $e) {
  62. $_SESSION['return'][] = array(
  63. 'type' => 'danger',
  64. 'log' => array(__FUNCTION__, $_action, $_data),
  65. 'msg' => array('redis_error', $e)
  66. );
  67. continue;
  68. }
  69. // Export private key and save private key to redis
  70. openssl_pkey_export($keypair_ressource, $privKey);
  71. if (isset($privKey) && !empty($privKey)) {
  72. try {
  73. $redis->hSet('DKIM_PRIV_KEYS', $dkim_selector . '.' . $domain, trim($privKey));
  74. }
  75. catch (RedisException $e) {
  76. $_SESSION['return'][] = array(
  77. 'type' => 'danger',
  78. 'log' => array(__FUNCTION__, $_action, $_data),
  79. 'msg' => array('redis_error', $e)
  80. );
  81. continue;
  82. }
  83. }
  84. $_SESSION['return'][] = array(
  85. 'type' => 'success',
  86. 'log' => array(__FUNCTION__, $_action, $_data),
  87. 'msg' => array('dkim_added', $domain)
  88. );
  89. }
  90. else {
  91. $_SESSION['return'][] = array(
  92. 'type' => 'danger',
  93. 'log' => array(__FUNCTION__, $_action, $_data),
  94. 'msg' => array('dkim_domain_or_sel_invalid', $domain)
  95. );
  96. continue;
  97. }
  98. }
  99. break;
  100. case 'duplicate':
  101. if ($_SESSION['mailcow_cc_role'] != "admin") {
  102. $_SESSION['return'][] = array(
  103. 'type' => 'danger',
  104. 'log' => array(__FUNCTION__, $_action, $_data),
  105. 'msg' => 'access_denied'
  106. );
  107. return false;
  108. }
  109. $from_domain = $_data['from_domain'];
  110. $from_domain_dkim = dkim('details', $from_domain, true);
  111. if (empty($from_domain_dkim)) {
  112. $_SESSION['return'][] = array(
  113. 'type' => 'danger',
  114. 'log' => array(__FUNCTION__, $_action, $_data),
  115. 'msg' => array('dkim_domain_or_sel_invalid', $from_domain)
  116. );
  117. continue;
  118. }
  119. $to_domains = (array)$_data['to_domain'];
  120. $to_domains = array_filter($to_domains);
  121. foreach ($to_domains as $to_domain) {
  122. try {
  123. $redis->hSet('DKIM_PUB_KEYS', $to_domain, $from_domain_dkim['pubkey']);
  124. $redis->hSet('DKIM_SELECTORS', $to_domain, $from_domain_dkim['dkim_selector']);
  125. $redis->hSet('DKIM_PRIV_KEYS', $from_domain_dkim['dkim_selector'] . '.' . $to_domain, base64_decode(trim($from_domain_dkim['privkey'])));
  126. }
  127. catch (RedisException $e) {
  128. $_SESSION['return'][] = array(
  129. 'type' => 'danger',
  130. 'log' => array(__FUNCTION__, $_action, $_data),
  131. 'msg' => array('redis_error', $e)
  132. );
  133. continue;
  134. }
  135. $_SESSION['return'][] = array(
  136. 'type' => 'success',
  137. 'log' => array(__FUNCTION__, $_action, $_data),
  138. 'msg' => array('dkim_duplicated', $from_domain, $to_domain)
  139. );
  140. }
  141. break;
  142. case 'import':
  143. if ($_SESSION['mailcow_cc_role'] != "admin") {
  144. $_SESSION['return'][] = array(
  145. 'type' => 'danger',
  146. 'log' => array(__FUNCTION__, $_action, $_data),
  147. 'msg' => 'access_denied'
  148. );
  149. return false;
  150. }
  151. $private_key_input = trim($_data['private_key_file']);
  152. $overwrite_existing = intval($_data['overwrite_existing']);
  153. $private_key_normalized = preg_replace('~\r\n?~', "\n", $private_key_input);
  154. $private_key = openssl_pkey_get_private($private_key_normalized);
  155. if ($ssl_error = openssl_error_string()) {
  156. $_SESSION['return'][] = array(
  157. 'type' => 'danger',
  158. 'log' => array(__FUNCTION__, $_action, $_data),
  159. 'msg' => array('private_key_error', $ssl_error)
  160. );
  161. return false;
  162. }
  163. // Explode by nl
  164. $pem_public_key_array = explode(PHP_EOL, trim(openssl_pkey_get_details($private_key)['key']));
  165. // Remove first and last line/item
  166. array_shift($pem_public_key_array);
  167. array_pop($pem_public_key_array);
  168. // Implode as single string
  169. $pem_public_key = implode('', (array)$pem_public_key_array);
  170. $dkim_selector = (isset($_data['dkim_selector'])) ? $_data['dkim_selector'] : 'dkim';
  171. $domain = $_data['domain'];
  172. if (!is_valid_domain_name($domain)) {
  173. $_SESSION['return'][] = array(
  174. 'type' => 'danger',
  175. 'log' => array(__FUNCTION__, $_action, $_data),
  176. 'msg' => array('dkim_domain_or_sel_invalid', $domain)
  177. );
  178. return false;
  179. }
  180. if ($redis->hGet('DKIM_PUB_KEYS', $domain)) {
  181. if ($overwrite_existing == 0) {
  182. $_SESSION['return'][] = array(
  183. 'type' => 'danger',
  184. 'log' => array(__FUNCTION__, $_action, $_data),
  185. 'msg' => array('dkim_domain_or_sel_exists', $domain)
  186. );
  187. return false;
  188. }
  189. }
  190. if (!ctype_alnum($dkim_selector)) {
  191. $_SESSION['return'][] = array(
  192. 'type' => 'danger',
  193. 'log' => array(__FUNCTION__, $_action, $_data),
  194. 'msg' => array('dkim_domain_or_sel_invalid', $domain)
  195. );
  196. return false;
  197. }
  198. try {
  199. dkim('delete', array('domains' => $domain));
  200. $redis->hSet('DKIM_PUB_KEYS', $domain, $pem_public_key);
  201. $redis->hSet('DKIM_SELECTORS', $domain, $dkim_selector);
  202. $redis->hSet('DKIM_PRIV_KEYS', $dkim_selector . '.' . $domain, $private_key_normalized);
  203. }
  204. catch (RedisException $e) {
  205. $_SESSION['return'][] = array(
  206. 'type' => 'danger',
  207. 'log' => array(__FUNCTION__, $_action, $_data),
  208. 'msg' => array('redis_error', $e)
  209. );
  210. return false;
  211. }
  212. unset($private_key_normalized);
  213. unset($private_key);
  214. unset($private_key_input);
  215. try {
  216. }
  217. catch (RedisException $e) {
  218. $_SESSION['return'][] = array(
  219. 'type' => 'danger',
  220. 'log' => array(__FUNCTION__, $_action, $_data),
  221. 'msg' => array('redis_error', $e)
  222. );
  223. return false;
  224. }
  225. $_SESSION['return'][] = array(
  226. 'type' => 'success',
  227. 'log' => array(__FUNCTION__, $_action, $_data),
  228. 'msg' => array('dkim_added', $domain)
  229. );
  230. return true;
  231. break;
  232. case 'details':
  233. if (!hasDomainAccess($_SESSION['mailcow_cc_username'], $_SESSION['mailcow_cc_role'], $_data) && $_SESSION['mailcow_cc_role'] != "admin") {
  234. return false;
  235. }
  236. $dkimdata = array();
  237. if ($redis_dkim_key_data = $redis->hGet('DKIM_PUB_KEYS', $_data)) {
  238. $dkimdata['pubkey'] = $redis_dkim_key_data;
  239. if (strlen($dkimdata['pubkey']) < 391) {
  240. $dkimdata['length'] = "1024";
  241. }
  242. elseif (strlen($dkimdata['pubkey']) < 736) {
  243. $dkimdata['length'] = "2048";
  244. }
  245. elseif (strlen($dkimdata['pubkey']) < 1416) {
  246. $dkimdata['length'] = "4096";
  247. }
  248. else {
  249. $dkimdata['length'] = ">= 8192";
  250. }
  251. if ($GLOBALS['SPLIT_DKIM_255'] === true) {
  252. $dkim_txt_tmp = str_split('v=DKIM1;k=rsa;t=s;s=email;p=' . $redis_dkim_key_data, 255);
  253. $dkimdata['dkim_txt'] = sprintf('"%s"', implode('" "', (array)$dkim_txt_tmp ) );
  254. }
  255. else {
  256. $dkimdata['dkim_txt'] = 'v=DKIM1;k=rsa;t=s;s=email;p=' . $redis_dkim_key_data;
  257. }
  258. $dkimdata['dkim_selector'] = $redis->hGet('DKIM_SELECTORS', $_data);
  259. if ($GLOBALS['SHOW_DKIM_PRIV_KEYS'] || $privkey == true) {
  260. $dkimdata['privkey'] = base64_encode($redis->hGet('DKIM_PRIV_KEYS', $dkimdata['dkim_selector'] . '.' . $_data));
  261. }
  262. else {
  263. $dkimdata['privkey'] = '';
  264. }
  265. }
  266. return $dkimdata;
  267. break;
  268. case 'blind':
  269. if ($_SESSION['mailcow_cc_role'] != "admin") {
  270. $_SESSION['return'][] = array(
  271. 'type' => 'danger',
  272. 'log' => array(__FUNCTION__, $_action, $_data),
  273. 'msg' => 'access_denied'
  274. );
  275. return false;
  276. }
  277. $blinddkim = array();
  278. foreach ($redis->hKeys('DKIM_PUB_KEYS') as $redis_dkim_domain) {
  279. $blinddkim[] = $redis_dkim_domain;
  280. }
  281. return array_diff($blinddkim, array_merge(mailbox('get', 'domains'), mailbox('get', 'alias_domains')));
  282. break;
  283. case 'delete':
  284. $domains = (array)$_data['domains'];
  285. if ($_SESSION['mailcow_cc_role'] != "admin") {
  286. $_SESSION['return'][] = array(
  287. 'type' => 'danger',
  288. 'log' => array(__FUNCTION__, $_action, $_data),
  289. 'msg' => 'access_denied'
  290. );
  291. return false;
  292. }
  293. foreach ($domains as $domain) {
  294. if (!is_valid_domain_name($domain)) {
  295. $_SESSION['return'][] = array(
  296. 'type' => 'danger',
  297. 'log' => array(__FUNCTION__, $_action, $_data),
  298. 'msg' => array('dkim_domain_or_sel_invalid', $domain)
  299. );
  300. continue;
  301. }
  302. try {
  303. $selector = $redis->hGet('DKIM_SELECTORS', $domain);
  304. $redis->hDel('DKIM_PUB_KEYS', $domain);
  305. $redis->hDel('DKIM_PRIV_KEYS', $selector . '.' . $domain);
  306. $redis->hDel('DKIM_SELECTORS', $domain);
  307. }
  308. catch (RedisException $e) {
  309. $_SESSION['return'][] = array(
  310. 'type' => 'danger',
  311. 'log' => array(__FUNCTION__, $_action, $_data),
  312. 'msg' => array('redis_error', $e)
  313. );
  314. continue;
  315. }
  316. $_SESSION['return'][] = array(
  317. 'type' => 'success',
  318. 'log' => array(__FUNCTION__, $_action, $_data),
  319. 'msg' => array('dkim_removed', htmlspecialchars($domain))
  320. );
  321. }
  322. break;
  323. }
  324. }