Writing an Shell-Script for Creating Dovecot-Users

Hi there!

We've the following Shell-Script, which creates an transport-File (transport_pop3.in) via an Database-Query:

Code:
#!/usr/bin/env bash
export PATH=$PATH:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/root/bin

#
#
# Afer the changes have been made we issue a postfix reload to propagate our changes
#
date

#
# CONFIG
#
POSTFIX_ETC="/usr/local/etc/postfix"

# Our internal IP ranges, Postfix passes all mail unfiltered/unchecked
NETWORK_TABLE=("192.168.0.0/24")

# Address of Mail archive
BCC_MAIL_ARCHIVE="bcc-all@mailgate.intern.hoster.com"

# Static local addresses to handle by our dovecot lda
TRANSPORT_STATIC_DOVECOT=("bulk-api@mailgate.intern.hoster.com" "bcc-all@mailgate.intern.hoster.com")

# The location of our static list of old ps3 domains
STATIC_PS3_HOSTS="${POSTFIX_ETC}/STATIC_PS3_HOSTS"

# Our hardcore hadcoded mailadresses for debugging purpose
STATIC_HARDCODE_HARDCODED_MAIL_ADDRESSES_WANTED_BY_USER_WITH_A_RIDICULOUS_LONG_VARIABLE_NAME=("bulk_
mw1@hoster.de" "bulk_mw2@hoster.de")

# GLOBALS
DB_HOST='dbmaster.intern.hoster.com'
DB_USER='mail_exporter'
DB_PASS=`cat /root/.db.passwd`
DB_NAME='mail_exporter_db'
MYSQL_CMD='/usr/local/bin/mysql'
MYSQL_PRM="-s --batch -h ${DB_HOST} -u ${DB_USER} -p${DB_PASS} ${DB_NAME}"

# FUNCTIONS
function error()
{
    red='\033[0;31m'
    NC='\033[0m' # No Color
    echo -e "�~X|  ${red}$1$NC"
    exit 1
}
function ok()
{
    green='\033[0;32m'
    NC='\033[0m' # No Color
    echo -e "* ${white}$1$NC"
}

# check if given file is not empty
function notEmpty()
{
    LIMIT=${2}
    if [ "${LIMIT}" == "" ]
    then
        LIMIT="1"
    fi
    LINES=`wc -l $1 | awk '{print $1}'`
    if [[ "${LINES}" -lt "${LIMIT}" ]]
    then
        error "${1} is too small! (below ${LIMIT} lines)"
    fi
}

#
# MAIN
#
cd "${POSTFIX_ETC}" || error "cant change to ${POSTFIX_ETC}"

# transport_relay
FILE="${POSTFIX_ETC}/transport_pop3.in"
rm -f "${FILE}"
SELECT='SELECT concat(email,"|",user,"|",pwd) FROM mail_exporter.context_addon_queues where server = "smtp.intern.hoster.com"'
for line in `echo "${SELECT}" | "${MYSQL_CMD}" ${MYSQL_PRM} `
do
        email=`echo $line|cut -f 1 -d\|`
        # user=`echo $line|cut -f 2 -d\|`
        pass=`echo $line|cut -f 3 -d\|`
        # echo "found $email = $user = $pass";
        echo "$email dovecot:" >> "${FILE}"

done

#notEmpty "${FILE}" 800
ok "exported transport_pop3.in"
exit;

#
# Propagate our changes
#
make clean && make && service postfix reload && ok "Postfix reloaded."

How can I create now an Script which creates me an User for Dovecot? My script writes me the User in the File 'transport_pop3.in' but I wanna create an Dovecot-User as discribed here ---> https://wiki.dovecot.org/HowTo/SimpleVirtualInstall...

I think I've to insert it after the line 'echo "$email dovecot:" >> "${FILE}" ' of my script but I don't know how to get the User from my ${FILE} an to work on with it!?

Can someone of you help me and has any ideas?

Thanks and best regards,

Maze-M
 
I've any additional information regarding my problem:


Currently we create a User-File for a list of all domains in this part:

Code:
# transport_relay
FILE="${POSTFIX_ETC}/transport_pop3.in"
rm -f "${FILE}"
SELECT='SELECT concat(email,"|",user,"|",pwd) FROM mail_exporter.context_addon_queues where server = "smtp.intern.hoster.com"'
for line in `echo "${SELECT}" | "${MYSQL_CMD}" ${MYSQL_PRM} `
do
        email=`echo $line|cut -f 1 -d\|`
        # user=`echo $line|cut -f 2 -d\|`
        pass=`echo $line|cut -f 3 -d\|`
        # echo "found $email = $user = $pass";
        echo "$email dovecot:" >> "${FILE}"

done

But how can I work with the content from my created '${FILE} to 'put' it in an 'dovecot-Working' file?

My currency file (transport_pop3.in) has this content:

Code:
usertest@hoster.de dovecot:

...and I wanna move it in an File (/etc/dovecot/users) like this:

Code:
usertest@hoster.de:{SHA512-CRYPT}$6$mxladsdfsdfsdfsxcvrtzrtuizuikjafkjakfjafbnbnbafjkjkalsjfkjakjkjafklkajsfkjkafjwerwerwersdfalk/6.tlA/:1001:1001::/var/vmail

But I don't know how I can modifiy this script. I've to modify my for-in part, right?
--->
for line in `echo "${SELECT}" | "${MYSQL_CMD}" ${MYSQL_PRM} `
do
email=`echo $line|cut -f 1 -d\|`
# user=`echo $line|cut -f 2 -d\|`
pass=`echo $line|cut -f 3 -d\|`
# echo "found $email = $user = $pass";
echo "$email dovecot:" >> "${FILE}"

done
 
Why are you using scripts when Dovecot can read from MySQL directly? It will take a bit of fiddling but both Postfix and Dovecot can be configured to use the same database and user tables from MySQL. There's no need to configure any local configuration files to add/remove users as the entire user management is done from a MySQL database.
 
Back
Top