Source

iredmail / extra / clear_iredmail.sh

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#!/usr/bin/env bash

# Author:   Zhang Huangbin (michaelbibby <at> gmail.com)
# Purpose:  Remove main components which installed by iRedMail, so that
#           you can re-install iRedMail.
# Project:  iRedMail (http://www.iredmail.org/)

# ------------ USAGE --------
# Execute this file in the current directory.
#
#   # bash clear_iredmail.sh
#

export CONF_DIR='../conf'

# For Ubuntu & Debian.
export DEBIAN_FRONTEND='noninteractive'

# Source functions.
. ${CONF_DIR}/global
. ${CONF_DIR}/functions
. ${CONF_DIR}/core

# Source configurations.
[ -f ${CONF_DIR}/apache_php ] && . ${CONF_DIR}/apache_php
[ -f ${CONF_DIR}/openldap ] && . ${CONF_DIR}/openldap
[ -f ${CONF_DIR}/phpldapadmin ] && . ${CONF_DIR}/phpldapadmin
[ -f ${CONF_DIR}/mysql ] && . ${CONF_DIR}/mysql
[ -f ${CONF_DIR}/postfix ] && . ${CONF_DIR}/postfix
[ -f ${CONF_DIR}/policyd ] && . ${CONF_DIR}/policyd
[ -f ${CONF_DIR}/dovecot ] && . ${CONF_DIR}/dovecot
[ -f ${CONF_DIR}/managesieve ] && . ${CONF_DIR}/managesieve
[ -f ${CONF_DIR}/procmail ] && . ${CONF_DIR}/procmail
[ -f ${CONF_DIR}/amavisd ] && . ${CONF_DIR}/amavisd
[ -f ${CONF_DIR}/clamav ] && . ${CONF_DIR}/clamav
[ -f ${CONF_DIR}/spamassassin ] && . ${CONF_DIR}/spamassassin
[ -f ${CONF_DIR}/squirrelmail ] && . ${CONF_DIR}/squirrelmail
[ -f ${CONF_DIR}/roundcube ] && . ${CONF_DIR}/roundcube
[ -f ${CONF_DIR}/postfixadmin ] && . ${CONF_DIR}/postfixadmin
[ -f ${CONF_DIR}/phpmyadmin ] && . ${CONF_DIR}/phpmyadmin
[ -f ${CONF_DIR}/awstats ] && . ${CONF_DIR}/awstats
[ -f ${CONF_DIR}/iredadmin ] && . ${CONF_DIR}/iredadmin
[ -f ${CONF_DIR}/iredapd ] && . ${CONF_DIR}/iredapd

# Source user configurations of iRedMail.
. ../config

confirm_to_remove_account()
{
    # Usage: confirm_to_remove_account user USERNAME
    #        confirm_to_remove_account group GROUPNAME
    TYPE="${1}"
    NAME="${2}"

    if [ X"${TYPE}" == X"user" ]; then
        id -u ${NAME} >/dev/null 2>&1
        RETVAL="$?"
        remove_cmd="userdel -r"
    elif [ X"${TYPE}" == X"group" ]; then
        id -g ${NAME} >/dev/null 2>&1
        RETVAL="$?"
        remove_cmd="groupdel"
    fi

    if [ X"${RETVAL}" == X"0" ]; then
        ECHO_INFO -n "Remove ${TYPE} ${NAME}. [Y|n]"

        read ANSWER
        case $ANSWER in
            N|n ) : ;;
            Y|y|* )
                ECHO_INFO -n "Removing ${TYPE} ${NAME} ..."
                ${remove_cmd} ${NAME}
                echo -e "\t[ DONE ]\n--"
                ;;
        esac
    else
        # Account not exist.
        :
    fi
}

remove_pkg()
{
    ECHO_INFO "Removing package(s): $@"
    if [ X"${DISTRO}" == X"RHEL" ]; then
        yum remove $@
    elif [ X"${DISTRO}" == X"DEBIAN" -o X"${DISTRO}" == X"UBUNTU" ]; then
        dpkg -P postfix-policyd mailx bsd-mailx
        dpkg -P $@
    fi
}

# ---- Below is code snippet of functions/packages.sh ----
# Get all packages.
get_all_pkgs()
{
    export ALL_PKGS=''
    export ENABLED_SERVICES=''

    # Apache and PHP.
    if [ X"${USE_EXIST_AMP}" != X"YES" ]; then
        # Apache & PHP.
        if [ X"${DISTRO}" == X"RHEL" ]; then
            ALL_PKGS="${ALL_PKGS} httpd mod_ssl php php-common php-imap php-gd php-mbstring libmcrypt php-mcrypt php-pear php-xml php-pecl-fileinfo php-mysql php-ldap"
            ENABLED_SERVICES="${ENABLED_SERVICES} httpd"

        elif [ X"${DISTRO}" == X"DEBIAN" -o X"${DISTRO}" == X"UBUNTU" ]; then
            ALL_PKGS="${ALL_PKGS} apache2 apache2-mpm-prefork apache2.2-common libapache2-mod-php5 libapache2-mod-auth-mysql apache2-utils libaprutil1 php5-common php5-cli php5-imap php5-gd php5-mcrypt php5-mysql php5-ldap php-pear"
            ENABLED_SERVICES="${ENABLED_SERVICES} apache2"
        else
            :
        fi
    else
        :
    fi

    # Postfix.
    if [ X"${DISTRO}" == X"RHEL" ]; then
        ALL_PKGS="${ALL_PKGS} postfix"
    elif [ X"${DISTRO}" == X"DEBIAN" -o X"${DISTRO}" == X"UBUNTU" ]; then
        ALL_PKGS="${ALL_PKGS} postfix postfix-pcre"
    else
        :
    fi

    ENABLED_SERVICES="${ENABLED_SERVICES} postfix"

    # Awstats.
    if [ X"${USE_AWSTATS}" == X"YES" ]; then
        if [ X"${DISTRO}" == X"RHEL" ]; then
            ALL_PKGS="${ALL_PKGS} awstats"
        elif [ X"${DISTRO}" == X"DEBIAN" -o X"${DISTRO}" == X"UBUNTU" ]; then
            ALL_PKGS="${ALL_PKGS} awstats"
        else
            :
        fi
    else
        :
    fi

    # Note: mysql server is required, used to store extra data,
    #       such as policyd, roundcube webmail data.
    if [ X"${DISTRO}" == X"RHEL" ]; then
        ALL_PKGS="${ALL_PKGS} mysql-server mysql"
        ENABLED_SERVICES="${ENABLED_SERVICES} mysqld"
    elif [ X"${DISTRO}" == X"DEBIAN" -o X"${DISTRO}" == X"UBUNTU" ]; then
        [ X"${DISTRO_CODENAME}" == X"jaunty" ] && ALL_PKGS="${ALL_PKGS} mysql-server-core-5.0"
        ALL_PKGS="${ALL_PKGS} libdbd-mysql-perl libmysqlclient15off libmysqlclient15-dev mysql-common mysql-server-5.0 mysql-client mysql-client-5.0"
        ENABLED_SERVICES="${ENABLED_SERVICES} mysql"
    else
        :
    fi

    # Backend: OpenLDAP or MySQL.
    if [ X"${BACKEND}" == X"OpenLDAP" ]; then
        # OpenLDAP server & client.
        if [ X"${DISTRO}" == X"RHEL" ]; then
            ALL_PKGS="${ALL_PKGS} openldap-clients openldap-servers"
            ENABLED_SERVICES="${ENABLED_SERVICES} ldap"

        elif [ X"${DISTRO}" == X"DEBIAN" -o X"${DISTRO}" == X"UBUNTU" ]; then
            ALL_PKGS="${ALL_PKGS} postfix-ldap slapd ldap-utils"
            ENABLED_SERVICES="${ENABLED_SERVICES} slapd"
        else
            :
        fi
    elif [ X"${BACKEND}" == X"MySQL" ]; then
        # MySQL server & client.
        if [ X"${DISTRO}" == X"RHEL" ]; then
            # For Awstats.
            [ X"${USE_AWSTATS}" == X"YES" ] && ALL_PKGS="${ALL_PKGS} mod_auth_mysql"

        elif [ X"${DISTRO}" == X"DEBIAN" -o X"${DISTRO}" == X"UBUNTU" ]; then
            ALL_PKGS="${ALL_PKGS} postfix-mysql"

            # For Awstats.
            [ X"${USE_AWSTATS}" == X"YES" ] && ALL_PKGS="${ALL_PKGS} libapache2-mod-auth-mysql"
        else
            :
        fi
    else
        :
    fi

    # Policyd.
    if [ X"${DISTRO}" == X"RHEL" ]; then
        ALL_PKGS="${ALL_PKGS} policyd"
        ENABLED_SERVICES="${ENABLED_SERVICES} policyd"
    elif [ X"${DISTRO}" == X"DEBIAN" -o X"${DISTRO}" == X"UBUNTU" ]; then
        ALL_PKGS="${ALL_PKGS} postfix-policyd"
        ENABLED_SERVICES="${ENABLED_SERVICES} postfix-policyd"
    else
        :
    fi

    # Dovecot.
    if [ X"${ENABLE_DOVECOT}" == X"YES" ]; then
        if [ X"${DISTRO}" == X"RHEL" ]; then
            ALL_PKGS="${ALL_PKGS} dovecot dovecot-sieve"

        elif [ X"${DISTRO}" == X"DEBIAN" -o X"${DISTRO}" == X"UBUNTU" ]; then
            ALL_PKGS="${ALL_PKGS} dovecot-common dovecot-imapd dovecot-pop3d"
        else
            :
        fi

        ENABLED_SERVICES="${ENABLED_SERVICES} dovecot"
    else
        ALL_PKGS="procmail"
        [ X"${DISTRO}" == X"RHEL" ] && ENABLED_SERVICES="${ENABLED_SERVICES} saslauthd"
    fi

    # Amavisd-new & ClamAV & Altermime.
    if [ X"${DISTRO}" == X"RHEL" ]; then
        ALL_PKGS="${ALL_PKGS} amavisd-new clamd clamav clamav-db spamassassin altermime"
        ENABLED_SERVICES="${ENABLED_SERVICES} ${AMAVISD_RC_SCRIPT_NAME} clamd"
    elif [ X"${DISTRO}" == X"DEBIAN" -o X"${DISTRO}" == X"UBUNTU" ]; then
        ALL_PKGS="${ALL_PKGS} amavisd-new libcrypt-openssl-rsa-perl libmail-dkim-perl clamav clamav-base clamav-freshclam clamav-daemon spamassassin altermime"
        ENABLED_SERVICES="${ENABLED_SERVICES} ${AMAVISD_RC_SCRIPT_NAME} clamav-daemon clamav-freshclam"
    else
        :
    fi

    [ X"${DISTRO}" == X"DEBIAN" -o X"${DISTRO_CODENAME}" == X"hardy" ] && ALL_PKGS="${ALL_PKGS} libclamav5"

    # SPF.
    if [ X"${ENABLE_SPF}" == X"YES" ]; then
        if [ X"${DISTRO}" == X"RHEL" ]; then
            # SPF implemention via perl-Mail-SPF.
            ALL_PKGS="${ALL_PKGS} perl-Mail-SPF perl-Mail-SPF-Query"

        elif [ X"${DISTRO}" == X"DEBIAN" -o X"${DISTRO}" == X"UBUNTU" ]; then
            ALL_PKGS="${ALL_PKGS} libmail-spf-perl"
        else
            :
        fi
    else
        :
    fi

    # pysieved.
    # Warning: Do *NOT* add 'pysieved' service in 'ENABLED_SERVICES'.
    #          We don't have rc/init script under /etc/init.d/ till
    #          package is installed.
    if [ X"${USE_MANAGESIEVE}" == X"YES" ]; then
        # Note for Ubuntu & Debian:
        # Dovecot shipped in Debian/Ubuntu has managesieve plugin patched.
        [ X"${DISTRO}" == X"RHEL" ] && ALL_PKGS="${ALL_PKGS} pysieved"
    else
        :
    fi

    # SquirrelMail.
    if [ X"${USE_SM}" == X"YES" ]; then
        [ X"${DISTRO}" == X"RHEL" ] && ALL_PKGS="${ALL_PKGS} php-pear-db"
        [ X"${DISTRO}" == X"DEBIAN" -o X"${DISTRO}" == X"UBUNTU" ] && \
            ALL_PKGS="${ALL_PKGS} php-db"
    else
        :
    fi

    # iRedAdmin.
    if [ X"${USE_IREDADMIN}" == X"YES" ]; then
        [ X"${DISTRO}" == X"RHEL" ] && \
        ALL_PKGS="${ALL_PKGS} python-jinja2.${ARCH} python-webpy python-ldap.${ARCH} MySQL-python.${ARCH} mod_wsgi.${ARCH}"

        [ X"${DISTRO}" == X"DEBIAN" -o X"${DISTRO}" == X"UBUNTU" ] && \
            ALL_PKGS="${ALL_PKGS} libapache2-mod-wsgi python-mysqldb python-ldap python-jinja2 python-netifaces python-webpy"
    else
        :
    fi
}

get_all_misc()
{
    export EXTRA_FILES=''

    # SSL keys.
    EXTRA_FILES="${EXTRA_FILES} ${SSL_CERT_FILE} ${SSL_KEY_FILE}"

    # Apache & PHP.
    EXTRA_FILES="${EXTRA_FILES} ${HTTPD_CONF_ROOT} ${PHP_INI} /etc/php.d"

    # MySQL.
    EXTRA_FILES="${EXTRA_FILES} ${MYSQL_MY_CNF} /var/lib/mysql /var/log/mysqld.log /var/log/mysql.log /etc/mysql"

    # OpenLDAP.
    EXTRA_FILES="${EXTRA_FILES} ${OPENLDAP_CONF_ROOT} ${OPENLDAP_DATA_DIR} ${OPENLDAP_LOGFILE} ${OPENLDAP_LOGROTATE_FILE}"

    # Postfix.
    EXTRA_FILES="${EXTRA_FILES} ${POSTFIX_ROOTDIR}"

    # Dovecot.
    EXTRA_FILES="${EXTRA_FILES} ${DOVECOT_CONF} ${DOVECOT_LDAP_CONF} ${DOVECOT_MYSQL_CONF} ${DOVECOT_LOG_FILE} ${SIEVE_LOG_FILE} ${DOVECOT_LOGROTATE_FILE} ${SIEVE_LOGROTATE_FILE} ${DOVECOT_EXPIRE_DICT_BDB} ${GLOBAL_SIEVE_FILE} ${DOVECOT_QUOTA_WARNING_BIN}"

    # Procmail.
    EXTRA_FILES="${EXTRA_FILES} ${PROCMAILRC} ${PROCMAIL_LOGFILE} ${PROCMAIL_LOGROTATE_FILE}"

    # Policyd.
    EXTRA_FILES="${EXTRA_FILES} ${POLICYD_CONF} ${POLICYD_SENDER_THROTTLE_CONF} ${POLICYD_LOGFILE} ${POLICYD_LOGROTATE_FILE}"

    # Pysieved.
    EXTRA_FILES="${EXTRA_FILES} ${PYSIEVED_INI} /etc/init.d/pysieved"

    # Amavisd.
    EXTRA_FILES="${EXTRA_FILES} ${AMAVISD_CONF} ${AMAVISD_DKIM_CONF} ${AMAVISD_DKIM_DIR} ${AMAVISD_LOGFILE} ${AMAVISD_LOGROTATE_FILE} ${DISCLAIMER_DIR}"

    # ClamAV.
    EXTRA_FILES="${EXTRA_FILES} ${CLAMD_CONF} ${FRESHCLAM_CONF} ${CLAMD_LOGFILE} ${FRESHCLAM_LOGFILE}"

    # Awstats.
    EXTRA_FILES="${EXTRA_FILES} ${AWSTATS_CONF_DIR} ${AWSTATS_HTTPD_ROOT} ${AWSTATS_CGI_DIR}"

    # phpLDAPadmin.
    EXTRA_FILES="${EXTRA_FILES} ${PLA_HTTPD_ROOT} ${HTTPD_SERVERROOT}/phpldapadmin"

    # phpMyAdmin.
    EXTRA_FILES="${EXTRA_FILES} ${PHPMYADMIN_HTTPD_ROOT} ${HTTPD_SERVERROOT}/phpmyadmin"

    # Roundcube webmail.
    EXTRA_FILES="${EXTRA_FILES} ${RCM_HTTPD_ROOT} ${HTTPD_SERVERROOT}/roundcubemail ${RCM_LOGFILE} ${RCM_LOGROTATE_FILE}"

    # PostfixAdmin.
    EXTRA_FILES="${EXTRA_FILES} ${POSTFIXADMIN_HTTPD_ROOT} ${HTTPD_SERVERROOT}/postfixadmin"

    # SquirrelMail.
    EXTRA_FILES="${EXTRA_FILES} ${SM_HTTPD_ROOT} ${HTTPD_SERVERROOT}/squirrelmail"

    # iRedAdmin.
    EXTRA_FILES="${EXTRA_FILES} ${IREDADMIN_HTTPD_ROOT} ${HTTPD_SERVERROOT}/iredadmin"

    # Misc.
    EXTRA_FILES="${EXTRA_FILES} ${LOCAL_REPO_FILE}"
}

# Ge all users & groups
get_all_accounts()
{
    export ALL_USERS=''
    export ALL_GROUPS=''

    # Vmail.
    ALL_USERS="${ALL_USERS} ${VMAIL_USER_NAME}"
    ALL_GROUPS="${ALL_GROUPS} ${VMAIL_GROUP_NAME}"

    # Apache.
    #ALL_USERS="${ALL_USERS} ${HTTPD_USER}"
    #ALL_GROUPS="${ALL_GROUPS} ${HTTPD_GROUP}"

    # OpenLDAP.
    #ALL_USERS="${ALL_USERS} ${LDAP_USER}"
    #ALL_GROUPS="${ALL_GROUPS} ${LDAP_GROUP}"

    # Dovecot.
    #ALL_USERS="${ALL_USERS} ${DOVECOT_USER}"
    #ALL_GROUPS="${ALL_GROUPS} ${DOVECOT_GROUP}"

    # Policyd.
    ALL_USERS="${ALL_USERS} ${POLICYD_USER}"
    ALL_GROUPS="${ALL_GROUPS} ${POLICYD_GROUP}"

    # Amavisd.
    if [ X"${DISTRO}" == X"DEBIAN" -o X"${DISTRO}" == X"UBUNTU" ]; then
        ALL_USERS="${ALL_USERS} ${AMAVISD_SYS_USER}"
        ALL_GROUPS="${ALL_GROUPS} ${AMAVISD_SYS_GROUP}"
    fi

    # ClamAV.
    #ALL_USERS="${ALL_USERS} ${CLAMAV_USER}"
    #ALL_GROUPS="${ALL_GROUPS} ${CLAMAV_GROUP}"

    # iRedAPD.
    ALL_USERS="${ALL_USERS} ${IREDAPD_DAEMON_USER}"
    ALL_GROUPS="${ALL_GROUPS} ${IREDAPD_DAEMON_GROUP}"

    # iRedAdmin.
    ALL_USERS="${ALL_USERS} ${IREDADMIN_HTTPD_USER}"
    ALL_GROUPS="${ALL_GROUPS} ${IREDADMIN_HTTPD_GROUP}"
}

get_all_pkgs
get_all_misc
get_all_accounts

ECHO_INFO "=================== Stop services ================"
for i in ${ENABLED_SERVICES}; do
    [ -x /etc/init.d/$i ] && /etc/init.d/$i stop
done

if [ X"${DISTRO}" == X"RHEL" ]; then
    /usr/bin/killall -u ${IREDAPD_DAEMON_USER} python
fi

ECHO_INFO "=================== Remove binary packages ================"
remove_pkg ${ALL_PKGS}

ECHO_INFO "=================== Remove users ================"
for user in ${ALL_USERS}; do
    confirm_to_remove_account user ${user}
done

ECHO_INFO "=================== Remove groups ================"
for group in ${ALL_GROUPS}; do
    confirm_to_remove_account group ${group}
done

ECHO_INFO "=================== Remove configuration files ================"
for i in ${EXTRA_FILES}; do
    [ -e ${i} -o -L ${i} ] && rm -rfi ${i}
done

if [ X"${DISTRO}" == X"DEBIAN" -o X"${DISTRO}" == X"UBUNTU" ]; then
    for i in $(dpkg-statoverride --list); do
        file="$(echo $i | awk '{print $NF}')"
        #rm -rf $file 2>/dev/null
        [ ! -z ${file} ] && dpkg-statoverride --remove $file
    done
fi
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.