completed about 20% of email api

This commit is contained in:
Serghey Rodin 2012-02-22 23:14:01 +02:00
commit 8f6fbfc0e8
12 changed files with 668 additions and 59 deletions

View file

@ -87,6 +87,7 @@ echo "$dns_rec" >> $zone
sort_dns_records
# Updating zone
conf="$V_HOME/$user/conf/dns/$domain.db"
update_domain_zone

View file

@ -1,6 +1,6 @@
#!/bin/bash
# info: add mail domain
# options: user domain [antispam] [antivirus]
# options: user domain [antispam] [antivirus] [dkim] [dkim_size]
#
# The function adds MAIL domain.
@ -15,7 +15,9 @@ domain=$(idn -t --quiet -u "$2" )
domain=$(echo $domain | tr '[:upper:]' '[:lower:]')
domain_idn=$(idn -t --quiet -a "$domain")
antispam=${3-yes}
antivirus=${3-yes}
antivirus=${4-yes}
dkim=${5-yes}
dkim_size=${6-512}
# Importing variables
source $VESTA/conf/vars.conf
@ -29,10 +31,10 @@ source $V_FUNC/domain.func
#----------------------------------------------------------#
# Checking arg number
check_args '2' "$#" 'user domain [antispam] [antivirus]'
check_args '2' "$#" 'user domain [antispam] [antivirus] [dkim] [dkim_size]'
# Checking argument format
format_validation 'user' 'domain' 'antispam' 'antivirus'
format_validation 'user' 'domain' 'antispam' 'antivirus' 'dkim' 'dkim_size'
# Checking dns system is enabled
is_system_enabled 'MAIL_SYSTEM'
@ -50,56 +52,61 @@ is_domain_new 'mail'
is_package_full 'MAIL_DOMAINS'
exit
#----------------------------------------------------------#
# Action #
#----------------------------------------------------------#
# Defining variables
i=1
ns=$(get_user_value '$NS')
for nameserver in ${ns//,/ };do
eval ns$i=$nameserver
i=$((i + 1))
done
# Adding domain directory
mkdir $V_HOME/$user/conf/mail/$domain
touch $V_HOME/$user/conf/mail/$domain/aliases
touch $V_HOME/$user/conf/mail/$domain/protection
touch $V_HOME/$user/conf/mail/$domain/passwd
chown -R root:mail $V_HOME/$user/conf/mail/$domain
chmod 770 $V_HOME/$user/conf/mail/$domain
chmod 660 $V_HOME/$user/conf/mail/$domain*
if [ -z "$soa" ]; then
soa="$ns1"
# Adding antispam protection
if [ "$antispam" = 'yes' ]; then
echo 'antispam' >> $V_HOME/$user/conf/mail/$domain/protection
fi
# Adding zone to dns dir
cat $V_DNSTPL/$template.tpl |\
sed -e "s/%ip%/$ip/g" \
-e "s/%domain_idn%/$domain_idn/g" \
-e "s/%domain%/$domain/g" \
-e "s/%ns1%/$ns1/g" \
-e "s/%ns2%/$ns2/g" \
-e "s/%ns3%/$ns3/g" \
-e "s/%ns4%/$ns4/g" \
-e "s/%ns5%/$ns5/g" \
-e "s/%ns6%/$ns6/g" \
-e "s/%ns7%/$ns7/g" \
-e "s/%ns8%/$ns8/g" \
-e "s/%date%/$V_DATE/g" > $V_USERS/$user/dns/$domain
# Adding antivirus protection
if [ "$antivirus" = 'yes' ]; then
echo 'antivirus' >> $V_HOME/$user/conf/mail/$domain/protection
fi
# Adding dns.conf record
dns_rec="DOMAIN='$domain' IP='$ip' TPL='$template' TTL='$ttl' EXP='$exp'"
dns_rec="$dns_rec SOA='$soa' SUSPENDED='no' DATE='$V_DATE'"
echo "$dns_rec" >> $V_USERS/$user/dns.conf
chmod 660 $V_USERS/$user/dns.conf
# Adding dkim
if [ "$dkim" = 'yes' ]; then
openssl genrsa -out $V_USERS/$user/mail/$domain.pem $dkim_size 2>/dev/null
openssl rsa -pubout -in $V_USERS/$user/mail/$domain.pem \
-out $V_USERS/$user/mail/$domain.pub 2>/dev/null
chmod 660 $V_USERS/$user/mail/$domain.*
# Adding zone in named.conf
named="zone \"$domain_idn\" {type master; file"
named="$named \"$V_HOME/$user/conf/dns/$domain.db\";};"
echo "$named" >> /etc/named.conf
cp $V_USERS/$user/mail/$domain.pem $V_HOME/$user/conf/mail/$domain/dkim.pem
chown root:mail $V_HOME/$user/conf/mail/$domain/dkim.pem
chmod 660 $V_HOME/$user/conf/mail/$domain/dkim.pem
# Updating domain dns zone
conf="$V_HOME/$user/conf/dns/$domain.db"
update_domain_zone
# Adding dkim dns records
check_dns_domain=$(is_domain_valid 'dns')
if [ "$?" -eq 0 ]; then
p=$(cat $V_USERS/$user/mail/$domain.pub|grep -v ' KEY---'|tr -d '\n')
record='_domainkey'
policy="\"t=y; o=~;\""
$V_BIN/v_add_dns_domain_record $user $domain $record TXT "$policy"
chmod 640 $conf
chown root:named $conf
record='mail._domainkey'
selector="\"k=rsa\; p=$p\""
$V_BIN/v_add_dns_domain_record $user $domain $record TXT "$selector"
fi
fi
# Adding domain to vesta db
s="DOMAIN='$domain' ANTIVIRUS='$antivirus' ANTISPAM='$antispam' DKIM='$dkim'"
s="$s ACCOUNTS='0' U_DISK='0' CATCHALL='' SUSPENDED='no' DATE='$V_DATE'"
echo $s >> $V_USERS/$user/mail.conf
touch $V_USERS/$user/mail/$domain
chmod 660 $V_USERS/$user/mail.conf
chmod 660 $V_USERS/$user/mail/$domain
#----------------------------------------------------------#
@ -107,13 +114,10 @@ chown root:named $conf
#----------------------------------------------------------#
# Increasing domain value
increase_user_value "$user" '$U_DNS_DOMAINS'
# Adding task to the vesta pipe
restart_schedule 'dns'
increase_user_value "$user" '$U_MAIL_DOMAINS'
# Logging
log_history "$V_EVENT" "v_delete_dns_domain $user $domain"
log_history "$V_EVENT" "v_delete_mail_domain $user $domain"
log_event 'system' "$V_EVENT"
exit

74
bin/v_add_mail_domain_antispam Executable file
View file

@ -0,0 +1,74 @@
#!/bin/bash
# info: add mail domain antispam support
# options: user domain
#
# The function enables spamassasin for incomming emails.
#----------------------------------------------------------#
# Variable&Function #
#----------------------------------------------------------#
# Argument defenition
user=$1
domain=$(idn -t --quiet -u "$2" )
domain=$(echo $domain | tr '[:upper:]' '[:lower:]')
domain_idn=$(idn -t --quiet -a "$domain")
# Importing variables
source $VESTA/conf/vars.conf
source $V_CONF/vesta.conf
source $V_FUNC/shared.func
source $V_FUNC/domain.func
#----------------------------------------------------------#
# Verifications #
#----------------------------------------------------------#
# Checking arg number
check_args '2' "$#" 'user domain'
# Checking argument format
format_validation 'user' 'domain'
# Checking dns system is enabled
is_system_enabled 'MAIL_SYSTEM'
# Checking user
is_user_valid
# Checking user is active
is_user_suspended
# Checking domain
is_domain_valid 'mail'
# Checking domain is not suspened
is_domain_suspended 'mail'
# Checking errorlog is not added
is_domain_key_empty 'mail' '$ANTISPAM'
#----------------------------------------------------------#
# Action #
#----------------------------------------------------------#
# Adding antispam key to config
if [ -z "$(grep 'antispam' $V_HOME/$user/conf/mail/$domain/protection)" ]; then
echo 'antispam' >> $V_HOME/$user/conf/mail/$domain/protection
fi
#----------------------------------------------------------#
# Vesta #
#----------------------------------------------------------#
# Adding antispam in config
update_domain_value 'mail' '$ANTISPAM' 'yes'
# Logging
log_history "$V_EVENT" "v_delete_mail_domain_antispam $user $domain"
log_event 'system' "$V_EVENT"
exit

74
bin/v_add_mail_domain_antivirus Executable file
View file

@ -0,0 +1,74 @@
#!/bin/bash
# info: add mail domain antivirus support
# options: user domain
#
# The function enables clamav for incomming emails.
#----------------------------------------------------------#
# Variable&Function #
#----------------------------------------------------------#
# Argument defenition
user=$1
domain=$(idn -t --quiet -u "$2" )
domain=$(echo $domain | tr '[:upper:]' '[:lower:]')
domain_idn=$(idn -t --quiet -a "$domain")
# Importing variables
source $VESTA/conf/vars.conf
source $V_CONF/vesta.conf
source $V_FUNC/shared.func
source $V_FUNC/domain.func
#----------------------------------------------------------#
# Verifications #
#----------------------------------------------------------#
# Checking arg number
check_args '2' "$#" 'user domain'
# Checking argument format
format_validation 'user' 'domain'
# Checking dns system is enabled
is_system_enabled 'MAIL_SYSTEM'
# Checking user
is_user_valid
# Checking user is active
is_user_suspended
# Checking domain
is_domain_valid 'mail'
# Checking domain is not suspened
is_domain_suspended 'mail'
# Checking errorlog is not added
is_domain_key_empty 'mail' '$ANTIVIRUS'
#----------------------------------------------------------#
# Action #
#----------------------------------------------------------#
# Adding antispam key to config
if [ -z "$(grep 'virus' $V_HOME/$user/conf/mail/$domain/protection)" ]; then
echo 'antivirus' >> $V_HOME/$user/conf/mail/$domain/protection
fi
#----------------------------------------------------------#
# Vesta #
#----------------------------------------------------------#
# Adding antispam in config
update_domain_value 'mail' '$ANTIVIRUS' 'yes'
# Logging
log_history "$V_EVENT" "v_delete_mail_domain_antivirus $user $domain"
log_event 'system' "$V_EVENT"
exit

76
bin/v_add_mail_domain_cactchall Executable file
View file

@ -0,0 +1,76 @@
#!/bin/bash
# info: add mail domain catchall account
# options: user domain email
#
# The function enables catchall account for incomming emails.
#----------------------------------------------------------#
# Variable&Function #
#----------------------------------------------------------#
# Argument defenition
user=$1
domain=$(idn -t --quiet -u "$2" )
domain=$(echo $domain | tr '[:upper:]' '[:lower:]')
domain_idn=$(idn -t --quiet -a "$domain")
email="$3"
# Importing variables
source $VESTA/conf/vars.conf
source $V_CONF/vesta.conf
source $V_FUNC/shared.func
source $V_FUNC/domain.func
#----------------------------------------------------------#
# Verifications #
#----------------------------------------------------------#
# Checking arg number
check_args '3' "$#" 'user domain email'
# Checking argument format
format_validation 'user' 'domain' 'email'
exit
# Checking dns system is enabled
is_system_enabled 'MAIL_SYSTEM'
# Checking user
is_user_valid
# Checking user is active
is_user_suspended
# Checking domain
is_domain_valid 'mail'
# Checking domain is not suspened
is_domain_suspended 'mail'
# Checking errorlog is not added
is_domain_key_empty 'mail' '$ANTIVIRUS'
#----------------------------------------------------------#
# Action #
#----------------------------------------------------------#
# Adding antispam key to config
if [ -z "$(grep 'virus' $V_HOME/$user/conf/mail/$domain/protection)" ]; then
echo 'antivirus' >> $V_HOME/$user/conf/mail/$domain/protection
fi
#----------------------------------------------------------#
# Vesta #
#----------------------------------------------------------#
# Adding antispam in config
update_domain_value 'mail' '$ANTIVIRUS' 'yes'
# Logging
log_history "$V_EVENT" "v_delete_mail_domain_antivirus $user $domain"
log_event 'system' "$V_EVENT"
exit

96
bin/v_add_mail_domain_dkim Executable file
View file

@ -0,0 +1,96 @@
#!/bin/bash
# info: add mail domain dkim support
# options: user domain [dkim_size]
#
# The function adds DKIM signature to outgoing domain emails.
#----------------------------------------------------------#
# Variable&Function #
#----------------------------------------------------------#
# Argument defenition
user=$1
domain=$(idn -t --quiet -u "$2" )
domain=$(echo $domain | tr '[:upper:]' '[:lower:]')
domain_idn=$(idn -t --quiet -a "$domain")
dkim_size=${3-512}
# Importing variables
source $VESTA/conf/vars.conf
source $V_CONF/vesta.conf
source $V_FUNC/shared.func
source $V_FUNC/domain.func
#----------------------------------------------------------#
# Verifications #
#----------------------------------------------------------#
# Checking arg number
check_args '2' "$#" 'user domain [dkim_size]'
# Checking argument format
format_validation 'user' 'domain' 'dkim_size'
# Checking dns system is enabled
is_system_enabled 'MAIL_SYSTEM'
# Checking user
is_user_valid
# Checking user is active
is_user_suspended
# Checking domain
is_domain_valid 'mail'
# Checking domain is not suspened
is_domain_suspended 'mail'
# Checking errorlog is not added
is_domain_key_empty 'mail' '$DKIM'
#----------------------------------------------------------#
# Action #
#----------------------------------------------------------#
# Generating dkim
openssl genrsa -out $V_USERS/$user/mail/$domain.pem $dkim_size 2>/dev/null
openssl rsa -pubout -in $V_USERS/$user/mail/$domain.pem \
-out $V_USERS/$user/mail/$domain.pub 2>/dev/null
chmod 660 $V_USERS/$user/mail/$domain.*
# Adding dkim to config
cp $V_USERS/$user/mail/$domain.pem $V_HOME/$user/conf/mail/$domain/dkim.pem
chown root:mail $V_HOME/$user/conf/mail/$domain/dkim.pem
chmod 660 $V_HOME/$user/conf/mail/$domain/dkim.pem
# Checking dns domain
check_dns_domain=$(is_domain_valid 'dns')
if [ "$?" -eq 0 ]; then
# Adding dkim dns records
p=$(cat $V_USERS/$user/mail/$domain.pub|grep -v ' KEY---'|tr -d '\n')
record='_domainkey'
policy="\"t=y; o=~;\""
$V_BIN/v_add_dns_domain_record $user $domain $record TXT "$policy"
record='mail._domainkey'
selector="\"k=rsa\; p=$p\""
$V_BIN/v_add_dns_domain_record $user $domain $record TXT "$selector"
fi
#----------------------------------------------------------#
# Vesta #
#----------------------------------------------------------#
# Adding dkim in config
update_domain_value 'mail' '$DKIM' 'yes'
# Logging
log_history "$V_EVENT" "v_delete_mail_domain_dkim $user $domain"
log_event 'system' "$V_EVENT"
exit

View file

@ -65,15 +65,22 @@ ns2=$(get_user_value '$NS2')
# Changing tpl
update_domain_value 'dns' '$TPL' "$template"
cat $V_DNSTPL/$template.tpl |\
sed -e "s/%ip%/$ip/g" \
-e "s/%domain_idn%/$domain_idn/g" \
-e "s/%domain%/$domain/g" \
-e "s/%ns1%/$ns1/g" \
-e "s/%ns2%/$ns2/g" > $V_USERS/$user/dns/$domain
-e "s/%ns2%/$ns2/g" \
-e "s/%ns3%/$ns3/g" \
-e "s/%ns4%/$ns4/g" \
-e "s/%ns5%/$ns5/g" \
-e "s/%ns6%/$ns6/g" \
-e "s/%ns7%/$ns7/g" \
-e "s/%ns8%/$ns8/g" \
-e "s/%date%/$V_DATE/g" > $V_USERS/$user/dns/$domain
# Updating zone
conf="$V_HOME/$user/conf/dns/$domain.db"
update_domain_zone

115
bin/v_list_mail_domain Executable file
View file

@ -0,0 +1,115 @@
#!/bin/bash
# info: list web domain
# options: user domain [format]
#
# The function of obtaining the list of domain parameters. This call, just as
# all v_list_* calls, supports 3 formats - json, shell and plain.
#----------------------------------------------------------#
# Variable&Function #
#----------------------------------------------------------#
# Argument defenition
user=$1
domain=$2
format=${3-shell}
# Importing variables
source $VESTA/conf/vars.conf
source $V_FUNC/shared.func
source $V_FUNC/domain.func
# Json function
json_list_domain() {
i=1
fileds_count=$(echo "$fields" | wc -w)
line=$(grep "DOMAIN='$domain'" $conf)
# Print top bracket
echo '{'
# Assing key=value
eval $line
# Starting output loop
for field in $fields; do
# Parsing key=value
eval value=$field
# Checking first field
if [ "$i" -eq 1 ]; then
echo -e "\t\"$value\": {"
else
if [ "$fileds_count" -eq "$i" ]; then
echo -e "\t\t\"${field//$/}\": \"$value\","
else
echo -e "\t\t\"${field//$/}\": \"$value\""
fi
fi
# Updating iterator
(( ++i))
done
# If there was any output
if [ -n "$value" ]; then
echo -e ' }'
fi
# Printing bottom json bracket
echo -e "}"
}
# Shell function
shell_list_domain() {
line=$(grep "DOMAIN='$domain'" $conf)
# Parsing key=value
eval $line
# Print result line
for field in $fields; do
eval key="$field"
echo "${field//$/}: $key "
done
}
#----------------------------------------------------------#
# Verifications #
#----------------------------------------------------------#
# Checking args
check_args '2' "$#" 'user domain [format]'
# Checking user
is_user_valid
# Checking domain exist
is_domain_valid 'web'
#----------------------------------------------------------#
# Action #
#----------------------------------------------------------#
# Defining config
conf=$V_USERS/$user/mail.conf
# Defining fileds to select
fields='$DOMAIN $ANTIVIRUS $ANTISPAM $DKIM $ACCOUNTS $U_DISK $CATCHALL
$SUSPENDED $DATE'
# Listing domains
case $format in
json) json_list_domain ;;
plain) nohead=1; shell_list_domain ;;
shell) shell_list_domain |column -t ;;
*) check_args '2' '0' 'user domain [format]'
esac
#----------------------------------------------------------#
# Vesta #
#----------------------------------------------------------#
exit

80
bin/v_list_mail_domain_dkim Executable file
View file

@ -0,0 +1,80 @@
#!/bin/bash
# info: list mail domain dkim
# options: user domain [format]
#
# The function of obtaining domain dkim files.
#----------------------------------------------------------#
# Variable&Function #
#----------------------------------------------------------#
# Argument defenition
user=$1
domain=$2
format=${3-shell}
# Importing variables
source $VESTA/conf/vars.conf
source $V_FUNC/shared.func
source $V_FUNC/domain.func
# Json function
json_list_ssl() {
echo '{'
echo -e "\t\"$domain\": {"
echo " \"PEM\": \"$pem\","
echo " \"PUB\": \"$pub\","
echo -e "\t}\n}"
}
# Shell function
shell_list_ssl() {
if [ ! -z "$pem" ]; then
echo -e "$pem"
fi
if [ ! -z "$pub" ]; then
echo -e "\n$pub"
fi
}
#----------------------------------------------------------#
# Verifications #
#----------------------------------------------------------#
# Checking args
check_args '2' "$#" 'user domain [format]'
# Checking user
is_user_valid
# Checking domain exist
is_domain_valid 'mail'
#----------------------------------------------------------#
# Action #
#----------------------------------------------------------#
if [ -e "$V_USERS/$user/mail/$domain.pem" ]; then
pem=$(cat $V_USERS/$user/mail/$domain.pem |sed -e ':a;N;$!ba;s/\n/\\n/g')
fi
if [ -e "$V_USERS/$user/mail/$domain.pub" ]; then
pub=$(cat $V_USERS/$user/mail/$domain.pub |sed -e ':a;N;$!ba;s/\n/\\n/g')
fi
# Listing domains
case $format in
json) json_list_ssl ;;
plain) nohead=1; shell_list_ssl ;;
shell) shell_list_ssl ;;
*) check_args '1' '0' '[format]'
esac
#----------------------------------------------------------#
# Vesta #
#----------------------------------------------------------#
exit

60
bin/v_list_mail_domains Executable file
View file

@ -0,0 +1,60 @@
#!/bin/bash
# info: list mail domains
# options user [format]
#
# The function of obtaining the list of all user domains.
#----------------------------------------------------------#
# Variable&Function #
#----------------------------------------------------------#
# Argument defenition
user=$1
format=${2-shell}
# Importing variables
source $VESTA/conf/vars.conf
source $V_FUNC/shared.func
#----------------------------------------------------------#
# Verifications #
#----------------------------------------------------------#
# Checking args
check_args '1' "$#" 'user [format]'
# Checking argument format
format_validation 'user'
# Checking user
is_user_valid
#----------------------------------------------------------#
# Action #
#----------------------------------------------------------#
# Defining config
conf=$V_USERS/$user/mail.conf
# Defining fileds to select
fields="\$DOMAIN \$ANTIVIRUS \$ANTISPAM \$DKIM \$ACCOUNTS \$U_DISK \$CATCHALL"
fields="$fields \$SUSPENDED \$DATE"
# Listing domains
case $format in
json) json_list ;;
plain) nohead=1; shell_list ;;
shell) fields='$DOMAIN $ANTIVIRUS $ANTISPAM $DKIM $ACCOUNTS $U_DISK $DATE';
shell_list | column -t ;;
*) check_args '1' '0' 'user [format]'
esac
#----------------------------------------------------------#
# Vesta #
#----------------------------------------------------------#
exit