# Upgrading to vmm 0.6.x

This document describes the procedure to upgrade a vmm-0.5.2 installation to version 0.6.x

If you still have installed vmm-0.4/vmm-0.4-r41 you have to proceed this step first:

> - Upgrading to vmm 0.5.x

## Upgrading from vmm-0.5.2

### Stop Postfix and Dovecot

Before updating your database layout you have to stop Postfix and Dovecot.

    root@host:~# /etc/init.d/postfix stop
    Stopping Postfix Mail Transport Agent: postfix.
    root@host:~# /etc/init.d/dovecot stop
    Stopping IMAP/POP3 mail server: dovecot.

### Create a database backup

It is always wise to back up your database from time to time. Particularly with regard to an impending structure modification.

Example: Backing up the mailsys database with pg_dump.

    user@host:~$ pg_dump -h localhost -U vmm -W mailsys > mailsys-0.5.2.sql

### Database update

Connect as your vmm database user to your database.

    user@host:~$ psql mailsys vmm -W -h localhost

In the PostgreSQL interactive terminal you have to run one of the following commands to perform the update.

#### Dovecot v1.2.x/v2.0.x/v2.1.x

    mailsys=> \i /path/to/vmm-0.6.2/pgsql/update_tables_0.5.x-0.6-dovecot-1.2.x.pgsql
    mailsys=> \q

#### Dovecot v1.0.x/v1.1.x

    mailsys=> \i /path/to/vmm-0.6.2/pgsql/update_tables_0.5.x-0.6.pgsql
    mailsys=> \q

### Set database permissions

There’s a python script which grants permissions to your Dovecot and Postfix database users.

    user@host:~$ python /path/to/vmm-0.6.2/pgsql/set-permissions.py -h
    Usage: set-permissions.py OPTIONS

    Set permissions for Dovecot and Postfix in the vmm database.

    Options:
      -h, --help            show this help message and exit
      -a, --askpass         Prompt for the database password.
      -H HOST, --host=HOST  Hostname or IP address of the database server. Leave
                            blank in order to use the default Unix-domain socket.
      -n NAME, --name=NAME  Specifies the name of the database to connect to.
                            Default: mailsys
      -p PASS, --pass=PASS  Password for the database connection.
      -P PORT, --port=PORT  Specifies the TCP port or the local Unix-domain socket
                            file extension on which the server is listening for
                            connections. Default: 5432
      -U USER, --user=USER  Connect to the database as the user USER instead of
                            the default: root
      -D USER, --dovecot=USER
                            Database user name of the Dovecot database user.
                            Default: dovecot
      -M USER, --postfix=USER
                            Database user name of the Postfix (MTA)  database
                            user. Default: postfix
    user@host:~$ python /path/to/vmm-0.6.2/pgsql/set-permissions.py -a -H 127.0.0.1 -U vmm
    Password:
    user@host:~$

### Start Dovecot and Postfix again

Warning

All the views (dovecot_… and postfix_…) have been replaced by database functions. So you have to adjust all your `postfix/pgsql-*.cf` files and also your `/etc/dovecot/dovecot-sql.conf` or `/etc/dovecot/dovecot-sql.conf.ext`.

See the vmm-0.6.2/postfix/pgsql-*.cf files and Dovecot configuration/dovecot-sql.conf.ext or Dovecot configuration/dovecot-sql-conf.

After a successfully update start Dovecot and Postfix again.

    root@host:~# /etc/init.d/dovecot start
    Starting IMAP/POP3 mail server: dovecot.
    root@host:~# /etc/init.d/postfix start
    Starting Postfix Mail Transport Agent: postfix.

### Upgrade vmm

Finally execute the `upgrade.sh` script. This will install the new code and also update your `vmm.cfg`.

    root@host:~# cd /path/to/vmm-0.6.2
    root@host:/path/to/vmm-0.6.2# ./upgrade.sh
    Please have a look at your configuration: /usr/local/etc/vmm.cfg
    This are your Modified/Renamed/New settings:
    R   account.delete_directory = false
    R   account.directory_mode = 448
    R   account.disk_usage = false
    R   domain.delete_directory = false
    R   domain.directory_mode = 504
    R   domain.force_deletion = false
    R   domain.imap = true
    R   domain.pop3 = true
    R   domain.sieve = true
    R   domain.smtp = true
    R   domain.transport = dovecot:
    R   mailbox.folders = Drafts:Sent:Templates:Trash
    R   mailbox.root = Maildir
    R   misc.base_directory = /srv/mail
    M   misc.dovecot_version = 2.1.7
    R   misc.dovecot_version = 2.1.7
    R   misc.password_scheme = PLAIN

    Removed section "config" with option "done" (obsolte)

    Removed option "gid_mail" from section "misc" (obsolte)

## Upgrading from vmm-0.6.0

### Database fixes

Due to an error in usertransport’s argument parsing, it is possible that some users’ transport-ID points to the erroneous transport domain. To fix that error in your database, execute the following SQL statement:

    UPDATE users SET tid = NULL
     WHERE tid = (SELECT tid FROM transport WHERE transport = 'domain');

If you are using Dovecot < v.1.2.0, you have to replace the database function dovecotpassword(). The service_set.ssid was selected unconditionally. This may cause an empty result, which will make logins impossible. To replace the function execute the following SQL statement:

    CREATE OR REPLACE FUNCTION dovecotpassword(
        IN localpart varchar, IN the_domain varchar) RETURNS SETOF dovecotpassword
    AS $$
        DECLARE
            record dovecotpassword;
            userid varchar(320) := localpart || '@' || the_domain;
        BEGIN
            FOR record IN
                SELECT userid, passwd, smtp, pop3, imap, managesieve
                  FROM users, service_set, domain_data
                 WHERE users.gid = (SELECT gid
                                      FROM domain_name
                                     WHERE domainname = the_domain)
                   AND local_part = localpart
                   AND users.gid = domain_data.gid
                   AND CASE WHEN
                      users.ssid IS NOT NULL
                      THEN
                        service_set.ssid = users.ssid
                      ELSE
                        service_set.ssid = domain_data.ssid
                      END
                LOOP
                    RETURN NEXT record;
                END LOOP;
            RETURN;
        END;
    $$ LANGUAGE plpgsql STABLE
    RETURNS NULL ON NULL INPUT
    EXTERNAL SECURITY INVOKER;

### Upgrade vmm

Execute the `upgrade.sh`, in order to install the updated code.

## Upgrading from vmm-0.6.1

If you have already installed vmm-6.0.1, simply execute the `upgrade.sh` script.

## Upgrade to Dovecot ≧ v1.2.beta2

When you are upgrading your old Dovecot (< v.1.2.beta2) to a newer version, ≧ v1.2.beta2, you also have to update the database layout. After you have backed up your database, you have to apply the changes from the file `dovecot_update_v1.2+.pgsql` to your database. (The file is currently only available from hg: dovecot_update_v1.2+.pgsql)

Connect as your vmm database user to your database.

    user@host:~$ psql mailsys vmm -W -h localhost

Then do the database update:

    mailsys=> \i /path/to/dovecot_update_v1.2+.pgsql
    mailsys=> \q

Finally Set database permissions.