Commits

Zhang Huangbin committed 14b3d35

It's now able to update password stored in PGSQL through Roundcube webmail.
Thanks Zou Zhicong <congli.cn@gmail> for his help. :)

  • Participants
  • Parent commits f7a73ea

Comments (0)

Files changed (5)

File iRedMail/dialog/config_via_dialog.sh

     . ${DIALOG_DIR}/pgsql_config.sh
 fi
 
+echo "export SQL_SERVER='${SQL_SERVER}'" >> ${CONFIG_FILE}
+echo "export SQL_SERVER_PORT='${SQL_SERVER_PORT}'" >> ${CONFIG_FILE}
+
 # Virtual domain configuration.
 . ${DIALOG_DIR}/virtual_domain_config.sh
 

File iRedMail/functions/packages.sh

             ALL_PKGS="${ALL_PKGS} postgresql-server"
 
         elif [ X"${DISTRO}" == X"DEBIAN" -o X"${DISTRO}" == X"UBUNTU" ]; then
-            ALL_PKGS="${ALL_PKGS} postgresql postgresql-client"
+            # postgresql-contrib provides extension 'dblink' used in Roundcube password plugin.
+            ALL_PKGS="${ALL_PKGS} postgresql postgresql-client postgresql-contrib"
         fi
     else
         :

File iRedMail/functions/postgresql.sh

 \c ${VMAIL_DB};
 \i ${PGSQL_SYS_USER_HOME}/vmail.sql;
 
+-- Create extension dblink.
+-- Used to change password through Roundcube webmail
+CREATE EXTENSION dblink;
+
 -- Crete roles:
 -- + vmail: read-only
 -- + vmailadmin: read, write

File iRedMail/functions/roundcubemail.sh

     perl -pi -e 's#(.*rcmail_config.*plugins.*=.*array\()(.*\).*)#${1}"password",${2}#' main.inc.php
 
     cd ${RCM_HTTPD_ROOT}/plugins/password/ && \
-    cp config.inc.php.dist config.inc.php
+        cp config.inc.php.dist config.inc.php
+
+    if [ X"${BACKEND}" == X"PGSQL" ]; then
+        # Patch to escape single quote while updating password
+        cd ${RCM_HTTPD_ROOT}
+        patch -p0 <${PATCH_DIR}/roundcubemail/password_driver_pgsql.patch >/dev/null
+
+        # Re-generate config.inc.php because it's hard to use perl to update
+        # 'password_query' setting.
+        cd ${RCM_HTTPD_ROOT}/plugins/password/
+        sed '/password_query/,$d' config.inc.php.dist > config.inc.php.tmp
+        # Update 'password_query' setting.
+        cat >> config.inc.php.tmp <<EOF
+\$rcmail_config['password_query'] = "SELECT * from dblink_exec(E'host=\'${PGSQL_SERVER}\' user=\'${RCM_DB_USER}\' password=\'${RCM_DB_PASSWD}\' dbname=\'${VMAIL_DB}\'', E'UPDATE mailbox SET password=%c,passwordlastchange=NOW() WHERE username=%u')";
+EOF
+
+        #perl -pi -e 's#(.*password_query.*)##' config.inc.php.tmp
+        sed '1,/password_query/d' config.inc.php.dist >> config.inc.php.tmp
+        rm -f config.inc.php &>/dev/null && \
+            mv config.inc.php.tmp config.inc.php
+    fi
 
     # Determine whether current password is required to change password
     perl -pi -e 's#(.*password_confirm_current.*=).*#${1} true;#' config.inc.php
 
         if [ X"${BACKEND}" == X"MYSQL" ]; then
             perl -pi -e 's#(.*password_query.*=).*#${1} "UPDATE $ENV{VMAIL_DB}.mailbox SET password=%c,passwordlastchange=NOW() WHERE username=%u LIMIT 1";#' config.inc.php
-        elif [ X"${BACKEND}" == X"MYSQL" ]; then
-            perl -pi -e 's#(.*password_query.*=).*#${1} "\c ${VMAIL_DB}; UPDATE mailbox SET password=%c,passwordlastchange=NOW() WHERE username=%u";#' config.inc.php
         fi
 
     elif [ X"${BACKEND}" == X"OPENLDAP" ]; then

File iRedMail/patches/roundcubemail/password_driver_pgsql.patch

+--- plugins/password/drivers/sql.php	2011-11-15 18:50:30.000000000 +0800
++++ plugins/password/drivers/sql.php	2012-01-21 16:23:31.000000000 +0800
+@@ -52,7 +52,8 @@
+     	    $salt .= $seedchars[rand(0, 63)];
+         }
+ 
+-        $sql = str_replace('%c',  $db->quote(crypt($passwd, CRYPT_MD5 ? '$1$'.$salt.'$' : $salt)), $sql);
++        //$sql = str_replace('%c',  $db->quote(crypt($passwd, CRYPT_MD5 ? '$1$'.$salt.'$' : $salt)), $sql);
++        $sql = str_replace('%c', str_replace("'", "\'", $db->quote(crypt($passwd, CRYPT_MD5 ? '$1$'.$salt.'$' : $salt))), $sql);
+     }
+ 
+     // dovecotpw
+@@ -150,7 +151,8 @@
+     // at least we should always have the local part
+     $sql = str_replace('%l', $db->quote($local_part, 'text'), $sql);
+     $sql = str_replace('%d', $db->quote($domain_part, 'text'), $sql);
+-    $sql = str_replace('%u', $db->quote($username, 'text'), $sql);
++    //$sql = str_replace('%u', $db->quote($username, 'text'), $sql);
++    $sql = str_replace('%u', str_replace("'", "\'", $db->quote($username, 'text')), $sql);
+     $sql = str_replace('%h', $db->quote($host, 'text'), $sql);
+ 
+     $res = $db->query($sql, $sql_vars);