Commit 4169243b authored by Vladimir Bashkirtsev's avatar Vladimir Bashkirtsev

Added sendmail configuration through CWMP

parent ed524520
...@@ -61,6 +61,9 @@ all: sendmail-config mail-aliases sendmail-default sm-client-service sendmail-se ...@@ -61,6 +61,9 @@ all: sendmail-config mail-aliases sendmail-default sm-client-service sendmail-se
systemctl enable sm-client.service systemctl enable sm-client.service
@echo "$$SENDMAIL_SERVICE" > /lib/systemd/system/sendmail.service @echo "$$SENDMAIL_SERVICE" > /lib/systemd/system/sendmail.service
systemctl enable sendmail.service systemctl enable sendmail.service
touch /etc/config/mail
install -v -Dm755 sendmail-init /usr/sbin/sendmail-init
install -v -Dm755 mail /usr/share/easycwmp/functions/mail
rm -rf sendmail-8.16.1 rm -rf sendmail-8.16.1
sendmail-config: sendmail-config:
...@@ -127,6 +130,7 @@ Wants=sm-client.service ...@@ -127,6 +130,7 @@ Wants=sm-client.service
Type=forking Type=forking
PIDFile=/run/sendmail.pid PIDFile=/run/sendmail.pid
EnvironmentFile=/etc/default/sendmail EnvironmentFile=/etc/default/sendmail
ExecStartPre=/usr/sbin/sendmail-init
ExecStart=/usr/sbin/sendmail -bd $$SENDMAIL_OPTS $$SENDMAIL_OPTARG ExecStart=/usr/sbin/sendmail -bd $$SENDMAIL_OPTS $$SENDMAIL_OPTARG
ExecStartPost=sleep 3 ExecStartPost=sleep 3
......
#!/bin/sh
#common_execute_method_param "$parameter" "$permission" "$get_cmd" "$set_cmd" "xsd:$type" "$forcedinform"
# $forcedinform should be set to 1 if the parameter is included in the inform message otherwise empty
# Default of $type = string
#############################
# Entry point functions #
#############################
prefix_list="$prefix_list $DMROOT.Mail."
entry_execute_method_list="$entry_execute_method_list entry_execute_method_root_Mail"
entry_execute_method_root_Mail() {
case "$1" in ""|"$DMROOT."|"$DMROOT.Mail."*)
common_execute_method_obj "$DMROOT.Mail." "0"
common_execute_method_obj "$DMROOT.Mail.Server." "1" "add_mail_server" "" "mail_server_browse_instances $1"
return 0
;;
esac
return $E_INVALID_PARAMETER_NAME;
}
sub_entry_mail_server() {
local i="$2"
case_param "$1" belongto "$DMROOT.Mail.Server.$i." && {
common_execute_method_obj "$DMROOT.Mail.Server.$i." "1" "" "delete_mail_server $i"
common_execute_method_param "$DMROOT.Mail.Server.$i.HostName" "1" "mail_server_get_hostname $i" "mail_server_set_hostname $i $1"
common_execute_method_obj "$DMROOT.Mail.Server.$i.User." "1" "add_mail_server_user $i" "" "users_browse $i $1"
return 0
}
return $E_INVALID_PARAMETER_NAME;
}
sub_entry_mail_server_users() {
local i="$1"
local j="$2"
case_param "$1" belongto "$DMROOT.Mail.Server.$i.User.$j." && {
common_execute_method_obj "$DMROOT.Mail.Server.$i.User.$j." "1" "" "delete_mail_server_user $i $j"
common_execute_method_param "$DMROOT.Mail.Server.$i.User.$j.Name" "1" "get_mail_server_user_name $i $j" "set_mail_server_user_name $i $j"
common_execute_method_param "$DMROOT.Mail.Server.$i.User.$j.Password" "1" "" "set_mail_server_user_password $i $j"
return 0
}
return $E_INVALID_PARAMETER_NAME;
}
#######################################
# Data model browse instances #
#######################################
mail_server_browse_instances() {
total=`uci -q show mail | grep -c "=server"`
for (( i=1; i<=$total; i++ )); do
sub_entry_mail_server "$1" "$i" "$2"
done
}
add_mail_server() {
$UCI_ADD mail server
$UCI_COMMIT
}
delete_mail_server() {
pos=$(($1 - 1))
$UCI_DELETE mail.@server[$pos]
$UCI_COMMIT
sendmail-init
systemctl restart sendmail
}
users_browse() {
pos=$(($1 - 1))
users=(`$UCI_SHOW mail.@server[$pos].user | sed -e 's/^.*=\(.*\)$/\1/g'`)
for (( i=1; i<=${#users[@]}; i++ )); do
sub_entry_mail_server_users "$1" "$i" "$2"
done
}
add_mail_server_user() {
pos=$(($1 - 1))
`$UCI_ADD_LIST mail.@server[$pos].user=`
$UCI_COMMIT
echo $pos
}
delete_mail_server_user() {
pos=$(($1 - 1))
idx=$(($2 - 1))
users=(`$UCI_SHOW mail.@server[$pos].user | sed -e 's/^.*=\(.*\)$/\1/g'`)
$UCI_DELETE mail.@server[$pos].user
for (( i=0; i<${#users[@]}; i++ )); do
if [ $i != $idx ]; then
`$UCI_ADD_LIST mail.@server[$pos].user=$(eval echo ${users[$i]})`
fi
done
$UCI_COMMIT
sendmail-init
systemctl restart sendmail
}
#######################################
# GET & SET Values #
#######################################
mail_server_get_hostname() {
pos=$(($1 - 1))
echo `$UCI_GET mail.@server[$pos].hostname`
}
mail_server_set_hostname() {
pos=$(($1 - 1))
$UCI_SET mail.@server[$pos].hostname=$3
}
get_mail_server_user_name() {
pos=$(($1 - 1))
idx=$(($2 - 1))
users=(`$UCI_SHOW mail.@server[$pos].user | sed -e 's/^.*=\(.*\)$/\1/g'`)
echo $(eval echo ${users[$idx]})
}
set_mail_server_user_name() {
pos=$(($1 - 1))
idx=$(($2 - 1))
users=(`$UCI_SHOW mail.@server[$pos].user | sed -e 's/^.*=\(.*\)$/\1/g'`)
users[$idx]=$3
$UCI_DELETE mail.@server[$pos].user
for (( i=0; i<${#users[@]}; i++ )); do
`$UCI_ADD_LIST mail.@server[$pos].user=$(eval echo ${users[$i]})`
done
$UCI_COMMIT
sendmail-init
systemctl restart sendmail
}
set_mail_server_user_password() {
pos=$(($1 - 1))
idx=$(($2 - 1))
users=(`$UCI_SHOW mail.@server[$pos].user | sed -e 's/^.*=\(.*\)$/\1/g'`)
user=$(eval echo ${users[$idx]})
sendmail-init
echo "$3 $user"
echo "$3" | saslpasswd2 $user
}
#!/bin/bash
UCI_SHOW="/sbin/uci -q show"
configure_sendmail() {
CONFIG=`$UCI_SHOW mail | awk '{ match($0, /\[([0-9]+)\]\.(hostname|user)=(.*)/, arr); if (arr[1] != "") print arr[1] " " arr[2] " " arr[3] }' | tr -d \'`
truncate --size 0 /data/etc/mail/local-host-names
truncate --size 0 /data/etc/mail/virtusertable
while read -a myarray
do
newpos=$((${myarray[0]} + 1))
if [ "$pos" != "$newpos" ]; then
hostname=""
fi
pos=$newpos
type=${myarray[1]}
values=${myarray[@]:2}
if [ "$type" = "hostname" ]; then
hostname=$values
elif [ "$hostname" != "" ] && [ "${values[@]}" != "" ]; then
echo $hostname >> /data/etc/mail/local-host-names
for user in ${values[@]}
do
echo "$user@$hostname $user@$(hostname)" >> /data/etc/mail/virtusertable
echo $user
done
echo "@$hostname error:nouser No such user here" >> /data/etc/mail/virtusertable
fi
done <<< $CONFIG
echo $(hostname) >> /data/etc/mail/local-host-names
echo "@$(hostname) error:nouser No such user here" >> /data/etc/mail/virtusertable
makemap hash /data/etc/mail/virtusertable.db < /data/etc/mail/virtusertable
}
existing_users() {
CONFIG=`/usr/sbin/sasldblistusers2 | awk '{ match($0, /^(.*)@/, arr); if (arr[1] != "") print arr[1] }' | sort | uniq`
echo $CONFIG
}
users=($(configure_sendmail))
existing=($(existing_users))
for user in $(printf '%s\n' "${users[@]}" | sort | uniq); do
if [[ ! " ${existing[@]} " =~ " ${user} " ]]; then
password=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 32)
echo $password | saslpasswd2 -p -c $user
fi
done
for user in $(printf '%s\n' "${existing[@]}" | sort | uniq); do
if [[ ! " ${users[@]} " =~ " ${user} " ]]; then
fullname=`/usr/sbin/sasldblistusers2 | sed -n "s/^\($user@.*\):.*/\\1/p" | sort | uniq`
saslpasswd2 -d $fullname
fi
done
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment