1

Topic: MySQL User Creation Script Update

Hi all,

I have updated this script to match the standard install dirs ad mail folders
and also have updated the sql to match the database tables and values inserted when creating an account via iredadmin.
hope this is useful to someone.

cheers Kris

Create_mail_user_MySQL.sh
##########################################################

# --------- CHANGE THESE VALUES ----------
# Storage base directory used to store users' mail.
STORAGE_BASE_DIRECTORY="/var/vmail"

# STORAGE_NODE has been added as it sits in the database in a slightly different column
# (standard mail folder is still /var/vmail/vmail1/)
STORAGE_NODE="vmail1"

# Password setting.
# Note: password will be crypted in MD5.
DEFAULT_PASSWD='password'
USE_DEFAULT_PASSWD='YES'     # If set to 'NO', password is the same as username.

# Default mail quota.
DEFAULT_QUOTA='100'   # 100 -> 100M

# -------------- You may not need to change variables below -------------------
# Mailbox format: mbox, Maildir.
MAILBOX_FORMAT='Maildir'

# ---- Maildir settings ----
# Maildir style: hashed, normal.
# Hashed maildir style, so that there won't be many large directories
# in your mail storage file system. Better performance in large scale
# deployment.
# Format: e.g. username@domain.td
#   hashed  -> domain.ltd/u/us/use/username/
#   normal  -> domain.ltd/username/
# Default hash level is 3.
MAILDIR_STYLE='hashed'      # hashed, normal.

# Time stamp, will be appended in maildir.
DATE="$(date +%Y.%m.%d.%H.%M.%S)"

# Path to SQL template file.
SQL="output.sql"
echo '' > ${SQL}

# Cyrpt the password.
if [ X"${USE_DEFAULT_PASSWD}" == X"YES" ]; then
    export CRYPT_PASSWD="$(openssl passwd -1 ${DEFAULT_PASSWD})"
else
    :
fi

generate_sql()
{
    # Get domain name.
    DOMAIN="$1"
    shift 1

    for i in $@; do
        username="$i"
        mail="${username}@${DOMAIN}"

        if [ X"${USE_DEFAULT_PASSWD}" != X"YES" ]; then
            export CRYPT_PASSWD="$(openssl passwd -1 ${username})"
        else
            :
        fi

        # Different maildir style: hashed, normal.
        if [ X"${MAILDIR_STYLE}" == X"hashed" ]; then
            length="$(echo ${username} | wc -L)"
            str1="$(echo ${username} | cut -c1)"
            str2="$(echo ${username} | cut -c2)"
            str3="$(echo ${username} | cut -c3)"

            if [ X"${length}" == X"1" ]; then
                str2="${str1}"
                str3="${str1}"
            elif [ X"${length}" == X"2" ]; then
                str3="${str2}"
            else
                :
            fi

            # Use mbox, will be changed later.
            maildir="${DOMAIN}/${str1}/${str2}/${str3}/${username}-${DATE}"
        else
            # Use mbox, will be changed later.
            maildir="${DOMAIN}/${username}-${DATE}"
        fi

        # Different maildir format: maildir, mbox.
        if [ X"${MAILBOX_FORMAT}" == X"Maildir" ]; then
            # Append slash to make it 'maildir' format.
            maildir="${maildir}/"
        else
            # It's already mbox format.
            :
        fi

        cat >> ${SQL} <<EOF
INSERT INTO mailbox (username, password, name, storagebasedirectory,storagenode, maildir, quota, domain, active, local_part, created)
    VALUES ('${mail}', '${CRYPT_PASSWD}', '${username}', '${STORAGE_BASE_DIRECTORY}','${STORAGE_NODE}', '${maildir}', '${DEFAULT_QUOTA}', '${DOMAIN}', '1','${username}', NOW());
INSERT INTO alias (address, goto, domain, created, active) VALUES ('${mail}', '${mail}','${DOMAIN}', NOW(), 1);
EOF
    done
}

if [ $# -lt 2 ]; then
    echo "Usage: $0 domain_name username [user2 user3 user4 ...]"
else
    # Generate SQL template.
    generate_sql $@ && \
    cat <<EOF

SQL template file was generated successfully, Please import it
*MANUALLY* after verify the records:

    - ${SQL}

Steps to import these users looks like below:

    # mysql -uroot -p
    mysql> USE vmail;
    mysql> SOURCE ${SQL};

EOF
fi

##################################################################
==== Provide basic information to help troubleshoot ====
- iRedMail version: iRedMail-0.7.3
- Linux/BSD distribution name and version: OEL 5.7
======

----

Spider Email Archiver: On-Premises, lightweight email archiving software developed by iRedMail team. Supports Amazon S3 compatible storage and custom branding.

2

Re: MySQL User Creation Script Update

Can you paste the differences between original file and modified one as a patch? It's hard to understand what you modified.

3

Re: MySQL User Creation Script Update

OK. I generated a patch manually and committed into iRedMail:
http://bitbucket.org/zhb/iredmail/chang … 8f39e63cd0

Thanks very much for your contribution. smile

4

Re: MySQL User Creation Script Update

Hi Sorry I didnt see your post until now.
no problem about the fix happy to pass along any changes we see along the way as we set up our server.

cheers Kris

5

Re: MySQL User Creation Script Update

I'm a newbie in iredmail. Just installed a new server with iredmail. The problem is how to modify the Create_mail_user_mysql.sh script in order to use an input text file (csv format) which contain list of username and password from previous linux mail server (/etc/passwd).

6

Re: MySQL User Creation Script Update

Hi @wole,

First of all, please do not hijack other's topic, especially do not hijack multiple times for the same question. You should always create a new forum topic for your own question.

Check this tutorial about how to use or migrate password hashes:
http://iredmail.org/wiki/index.php?titl … ord.hashes