/hostadmiral/trunk/backend/move_mailbox.sh |
---|
0,0 → 1,90 |
#!/bin/sh |
# |
# HostAdmiral backend. Executed with ROOT privileges |
# |
# The script must ensure the specified new maildir exists and |
# has correct structure. If old mailbox found it must be moved |
# to new one. In any case the new maildir has specified owner |
# |
# Params: |
# maildir_owner_uid (e.g. 1001) |
# old_maildir (e.g. example.com/john) |
# new_maildir (e.g. domain.net/merry) |
# |
# if old_maildir should not exist first param is equal to the second one |
# |
# Return: |
# 0 - nothing done, all OK |
# 1 - empty maildir created |
# 2 - old maildir moved or owner changed |
# 3 - wrong params |
# 4 - some error |
# 5 - error from system |
# |
### config ################################################ |
ROOT=/var/spool/mail |
DEFAULT_OWNER=202 |
### validate params ####################################### |
if [ -z "$3" -o -n "$4" ] ; then echo "Wrong params 1"; exit 3; fi |
OWNER="$1" |
if [ -z "${OWNER}" ] ; then |
OWNER=${DEFAULT_OWNER} |
elif [ ! "${OWNER}" -ge 1000 ] ; then |
echo "Wrong params 2"; exit 3 |
fi |
OLD="/$2/" |
echo "$OLD" | awk '$0 ~ "//" || $0 ~ "/\\./" || $0 ~ "/\\.\\./" || $0 !~ "^/(([a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?)\\.)*([a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?)/[a-zA-Z]([a-zA-Z0-9._-]*[a-zA-Z0-9])?/$" {exit 1} {exit 0}' |
if [ $? -ne 0 ]; then |
echo "Wrong params 3"; exit 3 |
fi |
NEW="/$3/" |
echo "$NEW" | awk '$0 ~ "//" || $0 ~ "/\\./" || $0 ~ "/\\.\\./" || $0 !~ "^/(([a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?)\\.)*([a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?)/[a-zA-Z]([a-zA-Z0-9._-]*[a-zA-Z0-9])?/$" {exit 1} {exit 0}' |
if [ $? -ne 0 ]; then |
echo "Wrong params 4"; exit 3 |
fi |
### work ################################################## |
if [ -d "${ROOT}${NEW}" ] ; then |
# the new maildir exists and is the same as old one - do nothing |
if [ "$OLD" = "$NEW" ] ; then |
WRONG_OWNER=`find "${ROOT}${NEW}" -not -user "${OWNER}"` |
if [ -z "$WRONG_OWNER" ] ; then |
exit 0; # correct owner |
else |
chown -R "${OWNER}" "${ROOT}${NEW}" || { echo "Cannot change owner"; exit 5; } |
exit 2; |
fi |
fi |
# the new maildir exists and differs from old one - error |
exit 4; |
else |
# the new maildir doesn't exist and is the same as old one or the old one doesn't exist |
# - create an empty |
if [ "$OLD" = "$NEW" -o ! -d "${ROOT}${OLD}" ] ; then |
mkdir -p "${ROOT}${NEW}cur" "${ROOT}${NEW}new" "${ROOT}${NEW}tmp" \ |
|| { echo "Cannot create maildir"; exit 5; } |
chmod 700 "${ROOT}${NEW}" || { echo "Cannot change mod"; exit 5; } |
chown -R "${OWNER}" "${ROOT}${NEW}" || { echo "Cannot change owner"; exit 5; } |
chown -R "${DEFAULT_OWNER}" `dirname "${ROOT}${NEW}"` || { echo "Cannot change owner"; exit 5; } |
exit 1; |
fi |
# the new maildir doesn't exist, differs from old one and the old one exists |
# - move it |
mkdir -p `dirname "${ROOT}${NEW}"` |
chown -R "${DEFAULT_OWNER}" `dirname "${ROOT}${NEW}"` || { echo "Cannot change owner"; exit 5; } |
mv "${ROOT}${OLD}" "${ROOT}${NEW}" || { echo "Cannot move maildir"; exit 5; } |
chown -R "${OWNER}" "${ROOT}${NEW}" || { echo "Cannot change owner"; exit 5; } |
exit 2; |
fi |
Property changes: |
Added: svn:executable |
+* |
\ No newline at end of property |
/hostadmiral/trunk/backend/start |
---|
0,0 → 1,4 |
#!/bin/sh |
sudo su -m ha-devel -c /home/www/hostadmiral.26th.net/project_devel/backend/backend.pl |
Property changes: |
Added: svn:executable |
+* |
\ No newline at end of property |
/hostadmiral/trunk/backend/backend.pl |
---|
19,6 → 19,8 |
# == configuration ============================= |
my $base_dir = dirname($0); |
our $host = '127.0.0.1'; |
our $port; |
our $password; |
26,8 → 28,9 |
our $db_user; |
our $db_password; |
our $log_level = 0; # 0 - none, 9 - all |
our $sudo = '/usr/local/bin/sudo'; # path to sudo |
my $config_name = dirname($0) . "/backend.conf"; |
my $config_name = "$base_dir/backend.conf"; |
require "$config_name"; # read the config |
# == constants ================================= |
63,6 → 66,7 |
my $code_db_error = 502; |
my $code_db_close_error = 503; |
my $code_db_inconsistent = 504; |
my $code_exec_error = 505; |
# == internal global variables ================= |
70,6 → 74,8 |
my $database_connection; |
my $database_in_use = 0; |
# == functions ================================= |
sub connection_loop |
{ |
# listen for connections |
236,13 → 242,16 |
my $res_action = save_to_db($request, "transport", |
{ domain => $oldName }, |
{ domain => $name, comment => $comment, transport => 'virtual:' } ); |
# FIXME: transport => 'procmail:'? |
# FIXME: transport => 'procmail:'? then restart mail system by transport change too |
if($res_action eq 'insert' || $res_action eq 'update') { |
if($res_action eq 'insert' || ($res_action eq 'update' && $name ne $oldName)) { |
return "error" unless(restart_mail_system()); |
} |
# FIXME: move maildirs, update users and aliases tables if 'update' |
if($oldName ne $name) { |
my $call_res = call_external_script($request, 'delete_domain.sh', [ $oldName ]); |
log_debug("delete_domain.sh: $call_res"); |
} |
return $res_action; |
} |
295,11 → 304,14 |
{ "name" => \&validate_domain } ); |
return unless(%params); |
# FIXME: delete maildirs, update users and aliases tables (or are they already deleted by frontend?) |
my $res_action = delete_from_db($request, "transport", |
{ domain => $params{"name"} } ); |
if($res_action ne 'error') { |
my $call_res = call_external_script($request, 'delete_domain.sh', [ $params{"name"} ]); |
log_debug("delete_domain.sh: $call_res"); |
} |
if($res_action eq 'delete') { |
return unless(restart_mail_system()); |
set_request_code($request, $code_ok, "Domain deleted"); |
360,8 → 372,9 |
# set mailid for the new record |
return "error" unless(update_mailbox_mailid($request, $id, $login, $domain)); |
# FIXME create an empty maildir if 'insert' |
# FIXME move the old maildir if 'update' |
my $call_res = call_external_script($request, 'move_mailbox.sh', |
[ (defined($systemUser) ? $systemUser : ""), "$oldDomain/$oldLogin", "$domain/$login" ]); |
log_debug("move_mailbox.sh: $call_res"); |
return $res_action; |
} |
436,7 → 449,9 |
"domain" => \&validate_domain } ); |
return unless(%params); |
# FIXME remove the maildir |
my $call_res = call_external_script($request, 'delete_mailbox.sh', |
[ "$params{'domain'}/$params{'login'}" ]); |
log_debug("delete_mailbox.sh: $call_res"); |
# get mailid |
my $dbh = db_begin($request); |
735,7 → 750,8 |
{ |
my $request = shift @_; |
log_debug("Mail system restarted"); |
my $call_res = call_external_script($request, 'restart_mail_system.sh'); |
log_debug("restart_mail_system.sh: $call_res"); |
return 1; |
} |
933,10 → 949,34 |
return $res_action; |
} |
sub call_external_script |
{ |
my $request = shift @_; |
my $script = shift @_; |
my $params = shift @_; |
my @args; |
push @args, "$base_dir/$script"; |
push @args, @$params if($params); |
my $res = system($sudo, @args); |
if($res == -1) { |
set_request_code($request, $code_exec_error, "Cannot execute script: $!"); |
return undef; |
} |
elsif($res & 127) { |
set_request_code($request, $code_exec_error, "Script died with signal " . ($res & 127)); |
return undef; |
} |
else { |
return ($res >> 8); |
} |
} |
sub strip_request_password |
{ |
$_ = shift @_; |
s/^$password_header.*$/$password_header*****/gm; |
s/^$password_header.*$/$password_header*****/gm; # comment this line out to see password in log |
return $_; |
} |
/hostadmiral/trunk/backend/delete_domain.sh |
---|
0,0 → 1,35 |
#!/bin/sh |
# |
# HostAdmiral backend. Executed with ROOT privileges |
# |
# The script deletes specified domain dir in mail spool |
# |
# Params: |
# domain (e.g. example.com) |
# |
# Return: |
# 0 - done |
# 3 - wrong params |
# 4 - some error |
# 5 - error from system |
# |
### config ################################################ |
ROOT=/var/spool/mail |
### validate params ####################################### |
if [ -z "$1" -o -n "$2" ] ; then echo "Wrong params 1"; exit 3; fi |
DIR="/$1/" |
echo "$DIR" | awk '$0 ~ "//" || $0 ~ "/\\./" || $0 ~ "/\\.\\./" || $0 !~ "^/(([a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?)\\.)*([a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?)/$" {exit 1} {exit 0}' |
if [ $? -ne 0 ]; then |
echo "Wrong params 2"; exit 3 |
fi |
### work ################################################## |
[ -d "${ROOT}${DIR}" ] && rmdir "${ROOT}${DIR}" || { echo "Cannot delete domain dir"; exit 5; } |
Property changes: |
Added: svn:executable |
+* |
\ No newline at end of property |
/hostadmiral/trunk/backend/restart_mail_system.sh |
---|
0,0 → 1,21 |
#!/bin/sh |
# |
# HostAdmiral backend. Executed with ROOT privileges |
# |
# The script forces postfix configuration reload |
# |
# Params: |
# no |
# |
# Return: |
# 0 - done |
# 3 - wrong params |
# 4 - some error |
# 5 - error from system |
# |
### work ################################################## |
/usr/local/sbin/postfix reload |
Property changes: |
Added: svn:executable |
+* |
\ No newline at end of property |
/hostadmiral/trunk/backend/delete_mailbox.sh |
---|
0,0 → 1,36 |
#!/bin/sh |
# |
# HostAdmiral backend. Executed with ROOT privileges |
# |
# The script deletes specified maildir |
# FIXME: do not delete - move to archive |
# |
# Params: |
# maildir (e.g. example.com/john) |
# |
# Return: |
# 0 - done |
# 3 - wrong params |
# 4 - some error |
# 5 - error from system |
# |
### config ################################################ |
ROOT=/var/spool/mail |
### validate params ####################################### |
if [ -z "$1" -o -n "$2" ] ; then echo "Wrong params 1"; exit 3; fi |
DIR="/$1/" |
echo "$DIR" | awk '$0 ~ "//" || $0 ~ "/\\./" || $0 ~ "/\\.\\./" || $0 !~ "^/(([a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?)\\.)*([a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?)/[a-zA-Z]([a-zA-Z0-9._-]*[a-zA-Z0-9])?/$" {exit 1} {exit 0}' |
if [ $? -ne 0 ]; then |
echo "Wrong params 2"; exit 3 |
fi |
### work ################################################## |
rm -rf "${ROOT}${DIR}" |
Property changes: |
Added: svn:executable |
+* |
\ No newline at end of property |