[Web, Postfix, Compose] Allow to add relayhosts per domain (+ plain and login authentication)
This commit is contained in:
@@ -260,8 +260,8 @@ function mailbox($_action, $_type, $_data = null) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
$stmt = $pdo->prepare("INSERT INTO `domain` (`domain`, `description`, `aliases`, `mailboxes`, `maxquota`, `quota`, `transport`, `backupmx`, `active`, `relay_all_recipients`)
|
||||
VALUES (:domain, :description, :aliases, :mailboxes, :maxquota, :quota, 'virtual', :backupmx, :active, :relay_all_recipients)");
|
||||
$stmt = $pdo->prepare("INSERT INTO `domain` (`domain`, `description`, `aliases`, `mailboxes`, `maxquota`, `quota`, `backupmx`, `active`, `relay_all_recipients`)
|
||||
VALUES (:domain, :description, :aliases, :mailboxes, :maxquota, :quota, :backupmx, :active, :relay_all_recipients)");
|
||||
$stmt->execute(array(
|
||||
':domain' => $domain,
|
||||
':description' => $description,
|
||||
@@ -1441,6 +1441,7 @@ function mailbox($_action, $_type, $_data = null) {
|
||||
$active = (isset($_data['active'])) ? intval($_data['active']) : $is_now['active_int'];
|
||||
$backupmx = (isset($_data['backupmx'])) ? intval($_data['backupmx']) : $is_now['backupmx_int'];
|
||||
$relay_all_recipients = (isset($_data['relay_all_recipients'])) ? intval($_data['relay_all_recipients']) : $is_now['relay_all_recipients_int'];
|
||||
$relayhost = (isset($_data['relayhost'])) ? intval($_data['relayhost']) : $is_now['relayhost'];
|
||||
$aliases = (!empty($_data['aliases'])) ? $_data['aliases'] : $is_now['max_num_aliases_for_domain'];
|
||||
$mailboxes = (!empty($_data['mailboxes'])) ? $_data['mailboxes'] : $is_now['max_num_mboxes_for_domain'];
|
||||
$maxquota = (!empty($_data['maxquota'])) ? $_data['maxquota'] : ($is_now['max_quota_for_mbox'] / 1048576);
|
||||
@@ -1531,6 +1532,7 @@ function mailbox($_action, $_type, $_data = null) {
|
||||
`active` = :active,
|
||||
`quota` = :quota,
|
||||
`maxquota` = :maxquota,
|
||||
`relayhost` = :relayhost,
|
||||
`mailboxes` = :mailboxes,
|
||||
`aliases` = :aliases,
|
||||
`description` = :description
|
||||
@@ -1541,6 +1543,7 @@ function mailbox($_action, $_type, $_data = null) {
|
||||
':active' => $active,
|
||||
':quota' => $quota,
|
||||
':maxquota' => $maxquota,
|
||||
':relayhost' => $relayhost,
|
||||
':mailboxes' => $mailboxes,
|
||||
':aliases' => $aliases,
|
||||
':description' => $description,
|
||||
@@ -2470,7 +2473,7 @@ function mailbox($_action, $_type, $_data = null) {
|
||||
':domain' => $_data
|
||||
));
|
||||
$row = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
if (!empty($row)) {
|
||||
if (!empty($row)) {
|
||||
$_data = $row['target_domain'];
|
||||
}
|
||||
$stmt = $pdo->prepare("SELECT
|
||||
@@ -2480,6 +2483,7 @@ function mailbox($_action, $_type, $_data = null) {
|
||||
`mailboxes`,
|
||||
`maxquota`,
|
||||
`quota`,
|
||||
`relayhost`,
|
||||
`relay_all_recipients` as `relay_all_recipients_int`,
|
||||
`backupmx` as `backupmx_int`,
|
||||
`active` as `active_int`,
|
||||
@@ -2514,6 +2518,7 @@ function mailbox($_action, $_type, $_data = null) {
|
||||
$domaindata['max_num_mboxes_for_domain'] = $row['mailboxes'];
|
||||
$domaindata['max_quota_for_mbox'] = $row['maxquota'] * 1048576;
|
||||
$domaindata['max_quota_for_domain'] = $row['quota'] * 1048576;
|
||||
$domaindata['relayhost'] = $row['relayhost'];
|
||||
$domaindata['backupmx'] = $row['backupmx'];
|
||||
$domaindata['backupmx_int'] = $row['backupmx_int'];
|
||||
$domaindata['active'] = $row['active'];
|
||||
|
179
data/web/inc/functions.relayhost.inc.php
Normal file
179
data/web/inc/functions.relayhost.inc.php
Normal file
@@ -0,0 +1,179 @@
|
||||
<?php
|
||||
function relayhost($_action, $_data = null) {
|
||||
global $pdo;
|
||||
global $lang;
|
||||
switch ($_action) {
|
||||
case 'add':
|
||||
if ($_SESSION['mailcow_cc_role'] != "admin") {
|
||||
$_SESSION['return'] = array(
|
||||
'type' => 'danger',
|
||||
'msg' => sprintf($lang['danger']['access_denied'])
|
||||
);
|
||||
return false;
|
||||
}
|
||||
$hostname = trim($_data['hostname']);
|
||||
$username = str_replace(':', '\:', trim($_data['username']));
|
||||
$password = str_replace(':', '\:', trim($_data['password']));
|
||||
if (empty($hostname)) {
|
||||
$_SESSION['return'] = array(
|
||||
'type' => 'danger',
|
||||
'msg' => 'Invalid host specified: '. htmlspecialchars($host)
|
||||
);
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
$stmt = $pdo->prepare("INSERT INTO `relayhosts` (`hostname`, `username` ,`password`, `active`)
|
||||
VALUES (:hostname, :username, :password, :active)");
|
||||
$stmt->execute(array(
|
||||
':hostname' => $hostname,
|
||||
':username' => $username,
|
||||
':password' => $password,
|
||||
':active' => '1'
|
||||
));
|
||||
}
|
||||
catch (PDOException $e) {
|
||||
$_SESSION['return'] = array(
|
||||
'type' => 'danger',
|
||||
'msg' => 'MySQL: '.$e
|
||||
);
|
||||
return false;
|
||||
}
|
||||
$_SESSION['return'] = array(
|
||||
'type' => 'success',
|
||||
'msg' => sprintf($lang['success']['relayhost_added'], htmlspecialchars(implode(', ', $hosts)))
|
||||
);
|
||||
break;
|
||||
case 'edit':
|
||||
if ($_SESSION['mailcow_cc_role'] != "admin") {
|
||||
$_SESSION['return'] = array(
|
||||
'type' => 'danger',
|
||||
'msg' => sprintf($lang['danger']['access_denied'])
|
||||
);
|
||||
return false;
|
||||
}
|
||||
$ids = (array)$_data['id'];
|
||||
foreach ($ids as $id) {
|
||||
$is_now = relayhost('details', $id);
|
||||
if (!empty($is_now)) {
|
||||
$hostname = (!empty($_data['hostname'])) ? trim($_data['hostname']) : $is_now['hostname'];
|
||||
$username = (!empty($_data['username'])) ? trim($_data['username']) : $is_now['username'];
|
||||
$password = (!empty($_data['password'])) ? trim($_data['password']) : $is_now['password'];
|
||||
$active = (isset($_data['active'])) ? intval($_data['active']) : $is_now['active_int'];
|
||||
}
|
||||
else {
|
||||
$_SESSION['return'] = array(
|
||||
'type' => 'danger',
|
||||
'msg' => 'Relayhost invalid'
|
||||
);
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
$stmt = $pdo->prepare("UPDATE `relayhosts` SET
|
||||
`hostname` = :hostname,
|
||||
`username` = :username,
|
||||
`password` = :password,
|
||||
`active` = :active
|
||||
WHERE `id` = :id");
|
||||
$stmt->execute(array(
|
||||
':id' => $id,
|
||||
':hostname' => $hostname,
|
||||
':username' => $username,
|
||||
':password' => $password,
|
||||
':active' => $active
|
||||
));
|
||||
}
|
||||
catch (PDOException $e) {
|
||||
$_SESSION['return'] = array(
|
||||
'type' => 'danger',
|
||||
'msg' => 'MySQL: '.$e
|
||||
);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
$_SESSION['return'] = array(
|
||||
'type' => 'success',
|
||||
'msg' => sprintf($lang['success']['object_modified'], htmlspecialchars(implode(', ', $hostnames)))
|
||||
);
|
||||
break;
|
||||
case 'delete':
|
||||
if ($_SESSION['mailcow_cc_role'] != "admin") {
|
||||
$_SESSION['return'] = array(
|
||||
'type' => 'danger',
|
||||
'msg' => sprintf($lang['danger']['access_denied'])
|
||||
);
|
||||
return false;
|
||||
}
|
||||
$ids = (array)$_data['id'];
|
||||
foreach ($ids as $id) {
|
||||
try {
|
||||
$stmt = $pdo->prepare("DELETE FROM `relayhosts` WHERE `id`= :id");
|
||||
$stmt->execute(array(':id' => $id));
|
||||
$stmt = $pdo->prepare("UPDATE `domain` SET `relayhost` = '0' WHERE `relayhost`= :id");
|
||||
$stmt->execute(array(':id' => $id));
|
||||
}
|
||||
catch (PDOException $e) {
|
||||
$_SESSION['return'] = array(
|
||||
'type' => 'danger',
|
||||
'msg' => 'MySQL: '.$e
|
||||
);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
$_SESSION['return'] = array(
|
||||
'type' => 'success',
|
||||
'msg' => sprintf($lang['success']['relayhost_removed'], htmlspecialchars(implode(', ', $hostnames)))
|
||||
);
|
||||
break;
|
||||
case 'get':
|
||||
if ($_SESSION['mailcow_cc_role'] != "admin") {
|
||||
return false;
|
||||
}
|
||||
$relayhosts = array();
|
||||
try {
|
||||
$stmt = $pdo->query("SELECT `id`, `hostname`, `username` FROM `relayhosts`");
|
||||
$relayhosts = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
}
|
||||
catch(PDOException $e) {
|
||||
$_SESSION['return'] = array(
|
||||
'type' => 'danger',
|
||||
'msg' => 'MySQL: '.$e
|
||||
);
|
||||
}
|
||||
return $relayhosts;
|
||||
break;
|
||||
case 'details':
|
||||
if ($_SESSION['mailcow_cc_role'] != "admin" || !isset($_data)) {
|
||||
return false;
|
||||
}
|
||||
$relayhostdata = array();
|
||||
try {
|
||||
$stmt = $pdo->prepare("SELECT `id`,
|
||||
`hostname`,
|
||||
`username`,
|
||||
`password`,
|
||||
`active` AS `active_int`,
|
||||
CONCAT(LEFT(`password`, 3), '...') AS `password_short`,
|
||||
CASE `active` WHEN 1 THEN '".$lang['mailbox']['yes']."' ELSE '".$lang['mailbox']['no']."' END AS `active`
|
||||
FROM `relayhosts`
|
||||
WHERE `id` = :id");
|
||||
$stmt->execute(array(':id' => $_data));
|
||||
$relayhostdata = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if (!empty($relayhostdata)) {
|
||||
$stmt = $pdo->prepare("SELECT GROUP_CONCAT(`domain` SEPARATOR ', ') AS `used_by_domains` FROM `domain` WHERE `relayhost` = :id");
|
||||
$stmt->execute(array(':id' => $_data));
|
||||
$used_by_domains = $stmt->fetch(PDO::FETCH_ASSOC)['used_by_domains'];
|
||||
$used_by_domains = (empty($used_by_domains)) ? '' : $used_by_domains;
|
||||
$relayhostdata['used_by_domains'] = $used_by_domains;
|
||||
}
|
||||
}
|
||||
catch(PDOException $e) {
|
||||
$_SESSION['return'] = array(
|
||||
'type' => 'danger',
|
||||
'msg' => 'MySQL: '.$e
|
||||
);
|
||||
}
|
||||
return $relayhostdata;
|
||||
break;
|
||||
}
|
||||
}
|
@@ -29,7 +29,7 @@
|
||||
<link rel="shortcut icon" href="/favicon.png" type="image/png">
|
||||
<link rel="icon" href="/favicon.png" type="image/png">
|
||||
</head>
|
||||
<body style="padding-top: 70px;">
|
||||
<body style="padding-top: 70px;" id="top">
|
||||
<nav class="navbar navbar-default navbar-fixed-top" role="navigation">
|
||||
<div class="container-fluid">
|
||||
<div class="navbar-header">
|
||||
|
@@ -3,7 +3,7 @@ function init_db_schema() {
|
||||
try {
|
||||
global $pdo;
|
||||
|
||||
$db_version = "18052017_1017";
|
||||
$db_version = "20072107_1029";
|
||||
|
||||
$stmt = $pdo->query("SHOW TABLES LIKE 'versions'");
|
||||
$num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC));
|
||||
@@ -60,6 +60,24 @@ function init_db_schema() {
|
||||
),
|
||||
"attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
|
||||
),
|
||||
"relayhosts" => array(
|
||||
"cols" => array(
|
||||
"id" => "INT NOT NULL AUTO_INCREMENT",
|
||||
"hostname" => "VARCHAR(255) NOT NULL",
|
||||
"username" => "VARCHAR(255) NOT NULL",
|
||||
"password" => "VARCHAR(255) NOT NULL",
|
||||
"active" => "TINYINT(1) NOT NULL DEFAULT '1'"
|
||||
),
|
||||
"keys" => array(
|
||||
"primary" => array(
|
||||
"" => array("id")
|
||||
),
|
||||
"key" => array(
|
||||
"hostname" => array("hostname")
|
||||
)
|
||||
),
|
||||
"attr" => "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC"
|
||||
),
|
||||
"alias" => array(
|
||||
"cols" => array(
|
||||
"address" => "VARCHAR(255) NOT NULL",
|
||||
@@ -95,7 +113,7 @@ function init_db_schema() {
|
||||
"mailboxes" => "INT(10) NOT NULL DEFAULT '0'",
|
||||
"maxquota" => "BIGINT(20) NOT NULL DEFAULT '0'",
|
||||
"quota" => "BIGINT(20) NOT NULL DEFAULT '102400'",
|
||||
"transport" => "VARCHAR(255) NOT NULL",
|
||||
"relayhost" => "VARCHAR(255) NOT NULL DEFAULT '0'",
|
||||
"backupmx" => "TINYINT(1) NOT NULL DEFAULT '0'",
|
||||
"relay_all_recipients" => "TINYINT(1) NOT NULL DEFAULT '0'",
|
||||
"created" => "DATETIME(0) NOT NULL DEFAULT NOW(0)",
|
||||
|
@@ -65,6 +65,7 @@ require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.domain_admin.inc.php';
|
||||
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.policy.inc.php';
|
||||
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.dkim.inc.php';
|
||||
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.fwdhost.inc.php';
|
||||
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.relayhost.inc.php';
|
||||
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.fail2ban.inc.php';
|
||||
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/init_db.inc.php';
|
||||
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/triggers.inc.php';
|
||||
|
Reference in New Issue
Block a user