[email_account] Adapt to new mail config on adagio

Remove useless code from email_account considering the change of
configuration for the mail system on adagio:
* No need to twiddle cyrus and restart it to connect to cyradm, just
  stop using --auth
* No default domain anymore (thus no special casing for it)
* No file listing current valid emails
This commit is contained in:
Thomas Preud'homme 2013-02-10 18:48:33 +01:00
parent 580e240c9e
commit 3110ed1288
1 changed files with 21 additions and 111 deletions

View File

@ -31,7 +31,7 @@ usage ()
echo "$progname emailaccount [emailalias]" echo "$progname emailaccount [emailalias]"
echo "$progname -h" echo "$progname -h"
echo echo
echo "Fist form adds an email account named emailaccount if it doesn't already exist" echo "First form adds an email account named emailaccount if it doesn't already exist"
echo "and creates an alias named emailalias for this email account if specified." echo "and creates an alias named emailalias for this email account if specified."
echo echo
echo "Second form prints this help." echo "Second form prints this help."
@ -145,21 +145,6 @@ try_sudo ()
exit $ret exit $ret
} }
# Warn user that cyrus will need to restart twice and postfix will have
# to reload its configuration once and ask him if he is ok for that
test_ready_for_restarts ()
{
local - first_ask next_ask
first_ask="Be aware that you'll need to restart cyrus twice and to make postfix reload its configuration once. Are you ready?"
next_ask="Incorrect answer. Are you ready to restart cyrus and make postfix reload its configuration?"
ask_user_default_no "$first_ask" "$next_ask"
if [ $? -eq 1 ]
then
echo "Ok, exiting…"
exit 1
fi
}
# Test cyrus password is OK. # Test cyrus password is OK.
test_cyrus_admin () test_cyrus_admin ()
{ {
@ -169,25 +154,6 @@ test_cyrus_admin ()
testsaslauthd -u $cyruser -p $cyrpwd > /dev/null || { echo "Error! Password for main cyrus admin has changed but has not been modified in /root/" >&2 && exit 1 ; } testsaslauthd -u $cyruser -p $cyrpwd > /dev/null || { echo "Error! Password for main cyrus admin has changed but has not been modified in /root/" >&2 && exit 1 ; }
} }
# Save cyrus state and make it cyradm ready, that is save and change
# allowplaintext and virtdomain and make it listen on localhost:imap
# instead of *:imap
save_cyrus_state_and_become_cyradm_ready ()
{
local -
sed -r -i "s/^(.*cmd=\"imapd.* listen=\")(imap\".*)$/\1localhost:\2/" /etc/cyrus.conf
sed -r -i "s/^([[:blank:]]*allowplaintext:.*)$/#\1\nallowplaintext: 1/" /etc/imapd.conf
sed -r -i "s/^([[:blank:]]*virtdomains:.*)$/#\1\nvirtdomains: userid/" /etc/imapd.conf
invoke-rc.d --quiet cyrus2.2 restart
if [ ! $? -eq 0 ]
then
echo "Error! Restarting cyrus failed. The requested action coudn't be performed." >&2
echo "Look at the logs and restart Cyrus manually." >&2
return 1
fi
return 0
}
# Ask a password to the user and create the entry in sasldb database. # Ask a password to the user and create the entry in sasldb database.
ask_and_create_password () ask_and_create_password ()
{ {
@ -246,23 +212,15 @@ create_sasldb_password ()
update_virtual_domains () update_virtual_domains ()
{ {
local - local -
grep "defaultdomain:" /etc/imapd.conf | grep "$emaildomain" > /dev/null grep "loginrealms:" /etc/imapd.conf | grep "$emaildomain" > /dev/null
if [ ! $? -eq 0 ] if [ ! $? -eq 0 ]
then then
grep "loginrealms:" /etc/imapd.conf | grep "$emaildomain" > /dev/null sed -r -i "s/^([[:blank:]]*loginrealms): ?(.*)$/\1: $emaildomain \2/" /etc/imapd.conf
if [ ! $? -eq 0 ]
then
sed -r -i "s/^([[:blank:]]*loginrealms): ?(.*)$/\1: $emaildomain \2/" /etc/imapd.conf
fi
fi fi
grep -E "mydomain[[:blank:]]*=" /etc/postfix/main.cf | grep "$emaildomain" > /dev/null grep -E "virtual_mailbox_domains[[:blank:]]*=" /etc/postfix/main.cf | grep "$emaildomain" /dev/null
if [ ! $? -eq 0 ] if [ ! $? -eq 0 ]
then then
grep -E "virtual_mailbox_domains[[:blank:]]*=" /etc/postfix/main.cf | grep "$emaildomain" /dev/null sed -r -i "s/^([[:blank:]]*virtual_mailbox_domains[[:blank:]]*)= ?(.*)$/\1= $emaildomain \2/" /etc/postfix/main.cf
if [ ! $? -eq 0 ]
then
sed -r -i "s/^([[:blank:]]*virtual_mailbox_domains[[:blank:]]*)= ?(.*)$/\1= $emaildomain \2/" /etc/postfix/main.cf
fi
fi fi
} }
@ -282,26 +240,6 @@ echo_with_tabs ()
echo "$2" echo "$2"
} }
# Add requested email to postfix virtual_mailbox_maps.
update_postfix_email_account ()
{
local -
grep -E "^[[:blank:]]*$emailuser@$emaildomain" /etc/postfix/vmailbox > /dev/null
if [ ! $? -eq 0 ]
then
echo_with_tabs "$emailuser@$emaildomain" "whatever" 4 >> /etc/postfix/vmailbox
fi
if [ ! $# -eq 0 ]
then
grep -E "^[[:blank:]]*$aliasuser@$aliasdomain" /etc/postfix/vmailbox > /dev/null
if [ ! $? -eq 0 ]
then
echo_with_tabs "$aliasuser@$aliasdomain" "whatever" 4 >> /etc/postfix/vmailbox
fi
fi
postmap /etc/postfix/vmailbox
}
# Add requested alias to postfix virtual_alias_maps. # Add requested alias to postfix virtual_alias_maps.
update_email_aliases () update_email_aliases ()
{ {
@ -357,37 +295,20 @@ display_infos ()
echo "." echo "."
} }
# Restore cyrus state as it was before making it cyradm ready, that is # Ask Postfix and Cyrus to reload their configuration
# restore allowplaintext, virtdomain and make it listen on *:imap reload_config ()
# instead of localhost:imap
restore_cyrus_state ()
{ {
local - local -
sed -r -i "s/^(.*cmd=\"imapd.* listen=\")localhost:(imap\".*)$/\1\2/" /etc/cyrus.conf invoke-rc.d --quiet cyrus2.2 reload
# OK, let's explain the following sed line.
# First you must understand that for each line, all the commands inside
# the {} block and separated by ; are executed.
# When the allowplaintext commented line is found, the # is removed by
# a s/pattern/replacement/ command. Then, as this substitution is
# successful, we jump at success which copy the substituted line in
# the hold space, get the next line in the pattern space and replace
# it by the substituted line. We've just deleted the next line. Then,
# we print the line in the pattern space. If from the beginning we
# have a non allowplaintext line, then T makes us jump to fail and
# print the line in the pattern space (that is the current line).
sed -r -n -i "{s/^#([[:blank:]]*allowplaintext:.*)$/\1/;t success;T fail;: success;h;n;g;: fail;p}" /etc/imapd.conf
# Same as above for virtdomains
sed -r -n -i "{s/^#([[:blank:]]*virtdomains:.*)$/\1/;t success;T fail;: success;h;n;g;: fail;p}" /etc/imapd.conf
invoke-rc.d --quiet cyrus2.2 restart
if [ ! $? -eq 0 ] if [ ! $? -eq 0 ]
then then
echo "Error! Restarting cyrus failed." >&2 echo "Error! Cyrus failed to reload its configuration." >&2
exit 1 exit 1
fi fi
invoke-rc.d --quiet postfix reload invoke-rc.d --quiet postfix reload
if [ ! $? -eq 0 ] if [ ! $? -eq 0 ]
then then
echo "Error! Reloading postfix failed." >&2 echo "Error! Postfix failed to reload its configuration." >&2
exit 1 exit 1
fi fi
} }
@ -396,11 +317,11 @@ restore_cyrus_state ()
create_mail_account () create_mail_account ()
{ {
local - nblines local - nblines
nblines=$(echo "listmailbox user/$emailuser@$emaildomain" | cyradm --user $cyruser --auth login --pass $cyrpwd localhost | wc -l) nblines=$(echo "listmailbox user/$emailuser@$emaildomain" | cyradm --user $cyruser --pass $cyrpwd localhost | wc -l)
if [ $nblines -eq 0 ] if [ $nblines -eq 0 ]
then then
echo "Mailbox for $emailuser@$emaildomain doesn't exist, creating it…" echo "Mailbox for $emailuser@$emaildomain doesn't exist, creating it…"
echo "createmailbox user/$emailuser@$emaildomain" | cyradm --user $cyruser --auth login --pass $cyrpwd localhost echo "createmailbox user/$emailuser@$emaildomain" | cyradm --user $cyruser --pass $cyrpwd localhost
fi fi
} }
@ -408,7 +329,6 @@ create_mail_account ()
main () main ()
{ {
local - ret local - ret
ret=0
# This test should be useless if rights on this file are corrects # This test should be useless if rights on this file are corrects
# (that is 770 for root:gt owner) # (that is 770 for root:gt owner)
test_args "$@" test_args "$@"
@ -416,28 +336,18 @@ main ()
then then
try_sudo "$@" try_sudo "$@"
fi fi
test_ready_for_restarts
test_cyrus_admin test_cyrus_admin
save_cyrus_state_and_become_cyradm_ready create_sasldb_password
if [ $? -eq 0 ] update_virtual_domains
aliasuser="${aliasuser:-""}"
if [ -n "$aliasuser" ]
then then
create_sasldb_password update_email_aliases
update_virtual_domains
aliasuser="${aliasuser:-""}"
if [ -n "$aliasuser" ]
then
update_postfix_email_account 1
update_email_aliases
else
update_postfix_email_account
fi
create_mail_account
display_infos
else
ret=1
fi fi
restore_cyrus_state create_mail_account
return $ret display_infos
reload_config
return 0
} }
main "${@:-""}" main "${@:-""}"