Writing an Shell-Script for Creating Dovecot-Users

Issues related to applications and software problems
Post Reply
maze-m
Posts: 7
Joined: 2019/10/23 20:11:29

Writing an Shell-Script for Creating Dovecot-Users

Post by maze-m » 2019/11/17 22:15:33

Hi there!

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

Code: Select all


#!/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
Last edited by maze-m on 2019/11/18 08:39:16, edited 1 time in total.

User avatar
TrevorH
Site Admin
Posts: 33215
Joined: 2009/09/24 10:40:56
Location: Brighton, UK

Re: Writing an Shell-Script for Creating Dovecot-Users

Post by TrevorH » 2019/11/17 23:24:50

POSTFIX_ETC="/usr/local/etc/postfix"
We already package and supply postfix and it doesn't install anything under /usr/local. Why did you replace it and why does it look like you did a source install for it? This is going to cause you problems as there will be other things on the system that Require: MTA and your source install postfix will not count so it will pull in the distro postfix and install that, potentially overwriting files that you have.
The future appears to be RHEL or Debian. I think I'm going Debian.
Info for USB installs on http://wiki.centos.org/HowTos/InstallFromUSBkey
CentOS 5 and 6 are deadest, do not use them.
Use the FAQ Luke

maze-m
Posts: 7
Joined: 2019/10/23 20:11:29

Re: Writing an Shell-Script for Creating Dovecot-Users

Post by maze-m » 2019/11/18 09:07:54

Sorry, my mistake. I thought, we've an CentOS-Installation on the Server but it's an FreeBSD running on it. That's the reason, why
we install this under that directory:
POSTFIX_ETC="/usr/local/etc/postfix"
Sorry, I can understand if you wouldn't help me because of the wrong distribution. But I would be happy if someone could give me any tipps regarding my shell-script :)....

User avatar
KernelOops
Posts: 428
Joined: 2013/12/18 15:04:03
Location: xfs file system

Re: Writing an Shell-Script for Creating Dovecot-Users

Post by KernelOops » 2019/11/18 10:02:41

I'm using postfix and dovecot together. Users (or email addresses accounts) are handled by dovecot, not postfix. When you want to add a new email address, you need to modify your dovecot setup.

For example, postfix knows about domains (@example.tld) but not individual email accounts (user@example.tld). When an email arrives, postfix uses LMTP to check on the validity of the recipient address and if everything is ok, then it passes the email to dovecot. So dovecot is the one that handles email addresses.

In my case, I use /home directories per domain, so @example.tld would be stored under /home/example/ and dovecot would store email addresses under /home/example/mail/example.tld/. This scheme allows me to have parked domains with their own email addresses.
--
R.I.P. CentOS :cry:
--

maze-m
Posts: 7
Joined: 2019/10/23 20:11:29

Re: Writing an Shell-Script for Creating Dovecot-Users

Post by maze-m » 2019/11/18 12:41:10

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

Code: Select all

# 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: Select all

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

Code: Select all

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

User avatar
KernelOops
Posts: 428
Joined: 2013/12/18 15:04:03
Location: xfs file system

Re: Writing an Shell-Script for Creating Dovecot-Users

Post by KernelOops » 2019/11/18 20:18:52

From what little I understand, you have an SQL database table which holds user/email data, your script extracts the SQL rows and saves them as a dovecot formatted users file.

So, you actually want another script that inserts new users/emails into the database table `mail_exporter.context_addon_queues`. I would guess...
--
R.I.P. CentOS :cry:
--

maze-m
Posts: 7
Joined: 2019/10/23 20:11:29

Re: Writing an Shell-Script for Creating Dovecot-Users

Post by maze-m » 2019/11/19 16:26:06

KernelOops wrote:
2019/11/18 20:18:52
From what little I understand, you have an SQL database table which holds user/email data, your script extracts the SQL rows and saves them as a dovecot formatted users file.

So, you actually want another script that inserts new users/emails into the database table `mail_exporter.context_addon_queues`. I would guess...
Yes, we've an SQL-Database table which holdes the current users and we wanna extract in an dovecot formated file because my collegue - who've setup the postfix / dovecot installation has no support for MySQL build in.
And therefore we've to extract in an file.

Another collegue build the following script to get an formated dovecot user:

Code: Select all

#/bin/sh
  
DOVECOT_USER_FILE=/tmp/aa
DOVECOT_ADMIN=/usr/local/bin/doveadm
USER="$1"
PASS="$2"
UID='1001'
GID='1001'
MAILDIR=/var/vmail

usage() {
        echo "$0 user pass"
}

if [ ! -f $DOVECOT_USER_FILE  ]
then
        echo "cant find file $DOVECOT_USER_FILE"
        exit 1;
fi

if [ "$USER" == "" ]
then
        echo "No USER"
        usage
        exit 1
fi

if [ "$PASS" == "" ]
then
        echo "No PASSWORD"
        usage
        exit 1
fi

if [ `grep -c ^$USER $DOVECOT_USER_FILE` -gt 0 ]
then
        echo "User: $USER already exists"
        exit 1
fi



CRYPTEDPW=`(echo $1|$DOVECOT_ADMIN pw -s SHA512-CRYPT ) `
The old installation is an sendmail-Installation which has all postboxes under /home... How can I create an skript with which I get an list with all users uder /home from the old installation to compare it with my Users from the database...

User avatar
KernelOops
Posts: 428
Joined: 2013/12/18 15:04:03
Location: xfs file system

Re: Writing an Shell-Script for Creating Dovecot-Users

Post by KernelOops » 2019/11/20 12:29:44

To be honest, I can't actually write a script out of nowhere and expect it will work on your system. Maybe you should contact the dovecot mailing list instead? maybe they can give you more specific help.
--
R.I.P. CentOS :cry:
--

Post Reply