diff --git a/email_account/email_account b/email_account/email_account index 6361eb1..0f9fdf4 100755 --- a/email_account/email_account +++ b/email_account/email_account @@ -31,7 +31,7 @@ usage () echo "$progname emailaccount [emailalias]" echo "$progname -h" 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 echo "Second form prints this help." @@ -145,21 +145,6 @@ try_sudo () 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_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 ; } } -# 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_and_create_password () { @@ -246,23 +212,15 @@ create_sasldb_password () update_virtual_domains () { local - - grep "defaultdomain:" /etc/imapd.conf | grep "$emaildomain" > /dev/null + grep "loginrealms:" /etc/imapd.conf | grep "$emaildomain" > /dev/null if [ ! $? -eq 0 ] then - grep "loginrealms:" /etc/imapd.conf | grep "$emaildomain" > /dev/null - if [ ! $? -eq 0 ] - then - sed -r -i "s/^([[:blank:]]*loginrealms): ?(.*)$/\1: $emaildomain \2/" /etc/imapd.conf - fi + sed -r -i "s/^([[:blank:]]*loginrealms): ?(.*)$/\1: $emaildomain \2/" /etc/imapd.conf 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 ] then - grep -E "virtual_mailbox_domains[[:blank:]]*=" /etc/postfix/main.cf | grep "$emaildomain" /dev/null - if [ ! $? -eq 0 ] - then - sed -r -i "s/^([[:blank:]]*virtual_mailbox_domains[[:blank:]]*)= ?(.*)$/\1= $emaildomain \2/" /etc/postfix/main.cf - fi + sed -r -i "s/^([[:blank:]]*virtual_mailbox_domains[[:blank:]]*)= ?(.*)$/\1= $emaildomain \2/" /etc/postfix/main.cf fi } @@ -282,26 +240,6 @@ echo_with_tabs () 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. update_email_aliases () { @@ -357,37 +295,20 @@ display_infos () echo "." } -# Restore cyrus state as it was before making it cyradm ready, that is -# restore allowplaintext, virtdomain and make it listen on *:imap -# instead of localhost:imap -restore_cyrus_state () +# Ask Postfix and Cyrus to reload their configuration +reload_config () { local - - sed -r -i "s/^(.*cmd=\"imapd.* listen=\")localhost:(imap\".*)$/\1\2/" /etc/cyrus.conf - # 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 + invoke-rc.d --quiet cyrus2.2 reload if [ ! $? -eq 0 ] then - echo "Error! Restarting cyrus failed." >&2 + echo "Error! Cyrus failed to reload its configuration." >&2 exit 1 fi invoke-rc.d --quiet postfix reload if [ ! $? -eq 0 ] then - echo "Error! Reloading postfix failed." >&2 + echo "Error! Postfix failed to reload its configuration." >&2 exit 1 fi } @@ -396,11 +317,11 @@ restore_cyrus_state () create_mail_account () { 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 ] then 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 } @@ -408,7 +329,6 @@ create_mail_account () main () { local - ret - ret=0 # This test should be useless if rights on this file are corrects # (that is 770 for root:gt owner) test_args "$@" @@ -416,28 +336,18 @@ main () then try_sudo "$@" fi - test_ready_for_restarts test_cyrus_admin - save_cyrus_state_and_become_cyradm_ready - if [ $? -eq 0 ] + create_sasldb_password + update_virtual_domains + aliasuser="${aliasuser:-""}" + if [ -n "$aliasuser" ] then - create_sasldb_password - 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 + update_email_aliases fi - restore_cyrus_state - return $ret + create_mail_account + display_infos + reload_config + return 0 } main "${@:-""}"