# Courier MySQL authentication module

This module allows authentication information to be stored in a MySQL database.

- Supports both physical and virtual mail accounts.
- Can be configured to use either crypted or cleartext passwords (or both). Cleartext passwords allow CRAM-MD5 authentication to be implemented.

When `authmysql` is installed, a default configuration file, `authmysqlrc` will be installed too. You must edit this file to set up your MySQL authentication.

NOTE: this authentication module should NOT be used if you are using the vpopmail virtual mailing list manager. You should select the `authvchkpw` authentication module instead (which should happen automatically). It may be necessary to use the `--without-authmysql` flag to the `configure` script, because `configure` by default will include `authmysql` if it finds MySQL client libraries..

Edit `authmysqlrc`, and initialize the following variables:

- `MYSQL_SERVER` - MySQL server name (required).
- `MYSQL_PORT` - server port where MySQL accepts connections.
- `MYSQL_SOCKET` - If MySQL is running on the same machine and can accept connections from a filesystem socket, enter the path to the filesystem socket here, and do not initialize SERVER/PORT.
- `MYSQL_USERNAME` - MySQL username with log in with (required).
- `MYSQL_PASSWORD` - MySQL password with log in with (required).
- `MYSQL_DATABASE` - MySQL database to log in to (required).
- `MYSQL_USER_TABLE` - name of the MySQL with the authentication information (see below) (required).
- `MYSQL_LOGIN_FIELD` - field that contains the login id for this account.
- `MYSQL_CRYPT_PWFIELD` - name of the field containing the crypt-ed password (either `MYSQL_CRYPT_PWFIELD` or `MYSQL_CLEAR_PWFIELD` is required). NOTE: this password must be crypt-ed using the operating system's crypt function, NOT MySQL's crypt function. MySQL's crypt() function is non-standard and is not generally compatible with the operating system's crypt function.
- `MYSQL_CLEAR_PWFIELD` - name of the field containing the cleartext password (either `MYSQL_CRYPT_PWFIELD` or `MYSQL_CLEAR_PWFIELD` is required).
- `MYSQL_UID_FIELD` - field that contains the system userid for this account.
- `MYSQL_GID_FIELD` - field that contains the system groupid for this account.
- `MYSQL_MAILDIR_FIELD` - name of the field containing a non-default location of the account's system mailbox (optional).
- `MYSQL_DEFAULTDELIVERY` - default mail delivery instructions for the Courier mail server. If this field in the record is not empty, its contents supercede the DEFAULTDELIVERY setting.
- `MYSQL_QUOTA_FIELD` - name of the field containing a maildir quota (optional).
- `MYSQL_AUXOPTIONS` - auxiliary options. This field, if defined, should consist of a `VARCHAR` consisting of a comma-separate list of "`IDENTIFIER=VALUE`" pairs, that specify per-account options. See INSTALL's description of the "Account OPTIONS" setting for more information.
- `DEFAULT_DOMAIN` - if the user logs in without specifying `@domain`, use the following domain (in this case the id field must always contain `user@host`) (optional).
- `MYSQL_WHERE_CLAUSE` - optional freeform SQL that is appended to the SQL query string.

Here's a recommended definition of `MYSQL_USER_TABLE`:

    CREATE TABLE passwd (
            id                    char(128) DEFAULT '' NOT NULL,
            crypt                 char(128) DEFAULT '' NOT NULL,
            clear                 char(128) DEFAULT '' NOT NULL,
            name                  char(128) DEFAULT '' NOT NULL,
            uid                   int(10) unsigned DEFAULT '65534' NOT NULL,
            gid                   int(10) unsigned DEFAULT '65534' NOT NULL,
            home                  char(255) DEFAULT '' NOT NULL,
            maildir               char(255) DEFAULT '' NOT NULL,
            defaultdelivery       char(255) DEFAULT '' NOT NULL,
            quota                 char(255) DEFAULT '' NOT NULL,
            options               char(255) DEFAULT '' NOT NULL,
            KEY id (id(128))
    );

Observe that a valid SQL expression may be used in place of any field setting, since all that happens is that the contents of the settings are inserted into an SQL SELECT statement. Specifically, the `options` field may be replaced by several normal fields, such as "`allowpop3`", "`allowimap`", "`allowwebmail`", and "`sharedgroup`"; then `MYSQL_AUXOPTIONS` would be set to:

> `CONCAT("allowimap=",allowimap,",allowpop3=",allowpop3,",allowwebmail=",allowwebmail,",sharedgroup=",sharedgroup)`

This results in the expected comma-delimited options list, which is built up from individual table fields.