create_optional_types_and_functions.pgsql
author Pascal Volk <neverseen@users.sourceforge.net>
Wed, 26 Nov 2008 23:50:04 +0000
changeset 93 bc41dfcef0ad
parent 82 6c85915f3815
child 102 485d3f7d6981
permissions -rw-r--r--
* 'VirtualMailManager/constants/VERSION.py' - Updated version from 0.5-dev to 0.5 * 'NEWS' - Added to repository
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
72
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     1
-- $Id$
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     2
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     3
-- --- Information:
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     4
-- This file contains some data types and functions these should speed up some
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     5
-- operations. Read the comment on each data type/functions for more details.
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     6
-- ---
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     7
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     8
-- ---
73
11da3d9298b4 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 72
diff changeset
     9
-- Data type for function postfix_smtpd_sender_login_map(varchar, varchar)
72
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    10
-- ---
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    11
CREATE TYPE sender_login AS (
73
11da3d9298b4 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 72
diff changeset
    12
    sender  varchar(320),
11da3d9298b4 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 72
diff changeset
    13
    login   text
72
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    14
);
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    15
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    16
-- ---
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    17
-- Parameters (from _sender_ address (MAIL FROM) [localpart@the_domain]):
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    18
--      varchar localpart
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    19
--      varchar the_domain
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    20
-- Returns: SASL _login_ names that own _sender_ addresses (MAIL FROM):
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    21
--      set of sender_login records.
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    22
--
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    23
-- Required access privileges for your postfix database user:
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    24
--      GRANT SELECT ON domain_name, users, alias TO postfix;
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    25
--
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    26
-- For more details see postconf(5) section smtpd_sender_login_maps
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    27
-- ---
73
11da3d9298b4 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 72
diff changeset
    28
CREATE OR REPLACE FUNCTION postfix_smtpd_sender_login_map(
72
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    29
    IN localpart varchar, IN the_domain varchar) RETURNS SETOF sender_login
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    30
AS $$
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    31
    DECLARE
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    32
        rec sender_login;
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    33
        did bigint := (SELECT gid FROM domain_name WHERE domainname=the_domain);
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    34
        sender varchar(320) := localpart || '@' || the_domain;
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    35
    BEGIN
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    36
        -- Get all addresses for 'localpart' in the primary and aliased domains
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    37
        FOR rec IN
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    38
            SELECT sender, local_part || '@' || domainname
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    39
              FROM domain_name, users
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    40
             WHERE domain_name.gid = did
75
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
    41
               AND users.gid = did
72
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    42
               AND users.local_part = localpart
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    43
            LOOP
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    44
                RETURN NEXT rec;
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    45
            END LOOP;
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    46
        IF NOT FOUND THEN
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    47
            -- Loop over the alias addresses for localpart@the_domain
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    48
            FOR rec IN
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    49
                SELECT DISTINCT sender, destination
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    50
                  FROM alias
82
6c85915f3815 „speedup commit“ ;-)
Pascal Volk <neverseen@users.sourceforge.net>
parents: 75
diff changeset
    51
                 WHERE alias.gid = did
6c85915f3815 „speedup commit“ ;-)
Pascal Volk <neverseen@users.sourceforge.net>
parents: 75
diff changeset
    52
                   AND alias.address = localpart
72
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    53
                LOOP
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    54
                    RETURN NEXT rec;
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    55
                END LOOP;
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    56
        END IF;
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    57
        RETURN;
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    58
    END;
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    59
$$ LANGUAGE plpgsql STABLE
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    60
RETURNS NULL ON NULL INPUT
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    61
EXTERNAL SECURITY INVOKER;
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    62
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    63
-- ########################################################################## --
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    64
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    65
-- ---
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    66
-- Data type for function postfix_virtual_mailbox(varchar, varchar)
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    67
-- ---
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    68
CREATE TYPE address_maildir AS (
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    69
    address varchar(320),
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    70
    maildir text
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    71
);
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    72
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    73
-- ---
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    74
-- Parameters (from recipients address (MAIL TO) [localpart@the_domain]):
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    75
--      varchar localpart
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    76
--      varchar the_domain
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    77
-- Returns: address_maildir records
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    78
--
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    79
-- Required access privileges for your postfix database user:
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    80
--      GRANT SELECT ON domain_data,domain_name,maillocation,users TO postfix;
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    81
--
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    82
-- For more details see postconf(5) section virtual_mailbox_maps
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    83
-- ---
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    84
CREATE OR REPLACE FUNCTION postfix_virtual_mailbox_map(
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    85
   IN localpart varchar, IN the_domain varchar) RETURNS SETOF address_maildir
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    86
AS $$
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    87
    DECLARE
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    88
        rec address_maildir;
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    89
        did bigint := (SELECT gid FROM domain_name WHERE domainname=the_domain);
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    90
        address varchar(320) := localpart || '@' || the_domain;
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    91
    BEGIN
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    92
        FOR rec IN
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    93
            SELECT address, domaindir||'/'||users.uid||'/'||maillocation||'/'
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    94
              FROM domain_data, users, maillocation
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    95
             WHERE domain_data.gid = did
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    96
               AND users.gid = did
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    97
               AND users.local_part = localpart
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    98
               AND maillocation.mid = users.mid
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    99
            LOOP
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   100
                RETURN NEXT rec;
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   101
            END LOOP;
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   102
        RETURN;
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   103
    END;
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   104
$$ LANGUAGE plpgsql STABLE
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   105
RETURNS NULL ON NULL INPUT
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   106
EXTERNAL SECURITY INVOKER;
ee0a0b5a8c2b * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   107
73
11da3d9298b4 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 72
diff changeset
   108
-- ########################################################################## --
11da3d9298b4 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 72
diff changeset
   109
11da3d9298b4 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 72
diff changeset
   110
-- ---
74
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   111
-- Data type for functions: postfix_relocated_map(varchar, varchar)
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   112
--                          postfix_virtual_alias_map(varchar, varchar)
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   113
--                          
73
11da3d9298b4 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 72
diff changeset
   114
-- ---
11da3d9298b4 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 72
diff changeset
   115
CREATE TYPE recipient_destination AS (
11da3d9298b4 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 72
diff changeset
   116
    recipient   varchar(320),
11da3d9298b4 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 72
diff changeset
   117
    destination text
11da3d9298b4 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 72
diff changeset
   118
);
11da3d9298b4 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 72
diff changeset
   119
11da3d9298b4 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 72
diff changeset
   120
-- ---
11da3d9298b4 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 72
diff changeset
   121
-- Parameters (from recipients address (MAIL TO) [localpart@the_domain]):
11da3d9298b4 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 72
diff changeset
   122
--      varchar localpart
11da3d9298b4 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 72
diff changeset
   123
--      varchar the_domain
11da3d9298b4 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 72
diff changeset
   124
-- Returns: recipient_destination records
11da3d9298b4 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 72
diff changeset
   125
--
11da3d9298b4 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 72
diff changeset
   126
-- Required access privileges for your postfix database user:
74
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   127
--      GRANT SELECT ON alias, domain_name TO postfix;
73
11da3d9298b4 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 72
diff changeset
   128
--
11da3d9298b4 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 72
diff changeset
   129
-- For more details see postconf(5) section virtual_alias_maps and virtual(5)
11da3d9298b4 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 72
diff changeset
   130
-- ---
11da3d9298b4 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 72
diff changeset
   131
CREATE OR REPLACE FUNCTION postfix_virtual_alias_map(
11da3d9298b4 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 72
diff changeset
   132
    IN localpart varchar, IN the_domain varchar)
11da3d9298b4 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 72
diff changeset
   133
    RETURNS SETOF recipient_destination
11da3d9298b4 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 72
diff changeset
   134
AS $$
11da3d9298b4 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 72
diff changeset
   135
    DECLARE
11da3d9298b4 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 72
diff changeset
   136
        record recipient_destination;
11da3d9298b4 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 72
diff changeset
   137
        recipient varchar(320) := localpart || '@' || the_domain;
11da3d9298b4 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 72
diff changeset
   138
        did bigint := (SELECT gid FROM domain_name WHERE domainname=the_domain);
11da3d9298b4 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 72
diff changeset
   139
    BEGIN
11da3d9298b4 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 72
diff changeset
   140
        FOR record IN
11da3d9298b4 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 72
diff changeset
   141
            SELECT recipient, destination
11da3d9298b4 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 72
diff changeset
   142
              FROM alias
11da3d9298b4 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 72
diff changeset
   143
             WHERE gid = did
11da3d9298b4 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 72
diff changeset
   144
               AND address = localpart
11da3d9298b4 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 72
diff changeset
   145
            LOOP
11da3d9298b4 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 72
diff changeset
   146
                RETURN NEXT record;
11da3d9298b4 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 72
diff changeset
   147
            END LOOP;
11da3d9298b4 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 72
diff changeset
   148
        RETURN;
11da3d9298b4 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 72
diff changeset
   149
    END;
11da3d9298b4 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 72
diff changeset
   150
$$ LANGUAGE plpgsql STABLE
11da3d9298b4 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 72
diff changeset
   151
RETURNS NULL ON NULL INPUT
11da3d9298b4 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 72
diff changeset
   152
EXTERNAL SECURITY INVOKER;
74
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   153
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   154
-- ########################################################################## --
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   155
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   156
-- ---
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   157
-- Parameters (from recipients address (MAIL TO) [localpart@the_domain]):
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   158
--      varchar localpart
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   159
--      varchar the_domain
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   160
-- Returns: recipient_destination records
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   161
--
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   162
-- Required access privileges for your postfix database user:
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   163
--      GRANT SELECT ON domain_name, relocated TO postfix;
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   164
--
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   165
-- For more details see postconf(5) section relocated_maps and relocated(5)
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   166
-- ---
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   167
CREATE OR REPLACE FUNCTION postfix_relocated_map(
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   168
    IN localpart varchar, IN the_domain varchar)
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   169
    RETURNS SETOF recipient_destination
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   170
AS $$
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   171
    DECLARE
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   172
        record recipient_destination;
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   173
        recipient varchar(320) := localpart || '@' || the_domain;
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   174
        did bigint := (SELECT gid FROM domain_name WHERE domainname=the_domain);
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   175
    BEGIN
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   176
        FOR record IN
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   177
            SELECT recipient, destination
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   178
              FROM relocated
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   179
             WHERE gid = did
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   180
               AND address = localpart
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   181
            LOOP
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   182
                RETURN NEXT record;
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   183
            END LOOP;
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   184
        RETURN;
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   185
    END;
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   186
$$ LANGUAGE plpgsql STABLE
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   187
RETURNS NULL ON NULL INPUT
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   188
EXTERNAL SECURITY INVOKER;
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   189
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   190
-- ########################################################################## --
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   191
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   192
-- ---
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   193
-- Data type for function postfix_transport_map(varchar, varchar)
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   194
-- ---
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   195
CREATE TYPE recipient_transport AS (
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   196
    recipient   varchar(320),
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   197
    transport   text
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   198
);
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   199
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   200
-- ---
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   201
-- Parameters (from recipients address (MAIL TO) [localpart@the_domain]):
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   202
--      varchar localpart
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   203
--      varchar the_domain
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   204
-- Returns: recipient_transport records
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   205
--
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   206
-- Required access privileges for your postfix database user:
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   207
--      GRANT SELECT ON users, transport, domain_name TO postfix;
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   208
--
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   209
-- For more details see postconf(5) section transport_maps and transport(5)
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   210
-- ---
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   211
CREATE OR REPLACE FUNCTION postfix_transport_map(
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   212
    IN localpart varchar, IN the_domain varchar)
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   213
    RETURNS SETOF recipient_transport
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   214
AS $$
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   215
    DECLARE
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   216
        record recipient_transport;
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   217
        recipient varchar(320) := localpart || '@' || the_domain;
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   218
    BEGIN
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   219
        FOR record IN
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   220
            SELECT recipient, transport
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   221
              FROM transport
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   222
             WHERE tid = (SELECT tid
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   223
                            FROM users
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   224
                           WHERE gid = (SELECT gid
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   225
                                          FROM domain_name
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   226
                                         WHERE domainname = the_domain)
75
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   227
                             AND local_part = localpart)
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   228
            LOOP
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   229
                RETURN NEXT record;
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   230
            END LOOP;
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   231
        RETURN;
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   232
    END;
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   233
$$ LANGUAGE plpgsql STABLE
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   234
RETURNS NULL ON NULL INPUT
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   235
EXTERNAL SECURITY INVOKER;
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   236
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   237
-- ########################################################################## --
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   238
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   239
-- ---
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   240
-- Data type for function postfix_virtual_uid_map(varchar, varchar)
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   241
-- ---
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   242
CREATE TYPE recipient_uid AS (
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   243
    recipient   varchar(320),
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   244
    uid         bigint
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   245
);
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   246
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   247
-- ---
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   248
-- Parameters (from recipients address (MAIL TO) [localpart@the_domain]):
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   249
--      varchar localpart
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   250
--      varchar the_domain
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   251
-- Returns: recipient_uid records
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   252
--
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   253
-- Required access privileges for your postfix database user:
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   254
--      GRANT SELECT ON users, domain_name TO postfix;
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   255
--
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   256
-- For more details see postconf(5) section virtual_uid_maps
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   257
-- ---
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   258
CREATE OR REPLACE FUNCTION postfix_virtual_uid_map(
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   259
    IN localpart varchar, IN the_domain varchar) RETURNS SETOF recipient_uid
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   260
AS $$
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   261
    DECLARE
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   262
        record recipient_uid;
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   263
        recipient varchar(320) := localpart || '@' || the_domain;
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   264
    BEGIN
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   265
        FOR record IN
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   266
            SELECT recipient, uid
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   267
              FROM users
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   268
             WHERE gid = (SELECT gid
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   269
                            FROM domain_name
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   270
                           WHERE domainname = the_domain)
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   271
               AND local_part = localpart
74
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   272
            LOOP
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   273
                RETURN NEXT record;
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   274
            END LOOP;
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   275
        RETURN;
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   276
    END;
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   277
$$ LANGUAGE plpgsql STABLE
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   278
RETURNS NULL ON NULL INPUT
67a454ea5472 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 73
diff changeset
   279
EXTERNAL SECURITY INVOKER;
75
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   280
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   281
-- ########################################################################## --
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   282
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   283
-- ---
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   284
-- Data type for function dovecotuser(varchar, varchar)
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   285
-- ---
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   286
CREATE TYPE dovecotuser AS (
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   287
    userid      varchar(320),
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   288
    uid         bigint,
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   289
    gid         bigint,
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   290
    home        text,
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   291
    mail        text
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   292
);
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   293
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   294
-- ---
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   295
-- Parameters (from login name [localpart@the_domain]):
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   296
--      varchar localpart
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   297
--      varchar the_domain
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   298
-- Returns: dovecotuser records
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   299
--
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   300
-- Required access privileges for your dovecot database user:
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   301
--      GRANT SELECT ON users,domain_data,domain_name,maillocation TO dovecot;
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   302
--
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   303
-- For more details see http://wiki.dovecot.org/UserDatabase
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   304
-- ---
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   305
CREATE OR REPLACE FUNCTION dovecotuser(
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   306
    IN localpart varchar, IN the_domain varchar) RETURNS SETOF dovecotuser
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   307
AS $$
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   308
    DECLARE
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   309
        record dovecotuser;
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   310
        userid varchar(320) := localpart || '@' || the_domain;
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   311
        did bigint := (SELECT gid FROM domain_name WHERE domainname=the_domain);
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   312
    BEGIN
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   313
        FOR record IN
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   314
            SELECT userid, uid, did, domaindir ||'/'|| uid AS home,
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   315
                   '~/'|| maillocation AS mail
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   316
              FROM users, domain_data, maillocation
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   317
             WHERE users.gid = did
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   318
               AND users.local_part = localpart
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   319
               AND maillocation.mid = users.mid
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   320
               AND domain_data.gid = did
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   321
            LOOP
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   322
                RETURN NEXT record;
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   323
            END LOOP;
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   324
        RETURN;
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   325
    END;
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   326
$$ LANGUAGE plpgsql STABLE
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   327
RETURNS NULL ON NULL INPUT
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   328
EXTERNAL SECURITY INVOKER;
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   329
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   330
-- ########################################################################## --
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   331
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   332
-- ---
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   333
-- Data type for function dovecotpassword(varchar, varchar)
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   334
-- ---
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   335
CREATE TYPE dovecotpassword AS (
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   336
    userid      varchar(320),
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   337
    password    varchar(74),
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   338
    smtp        boolean,
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   339
    pop3        boolean,
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   340
    imap        boolean,
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   341
    managesieve boolean
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   342
);
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   343
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   344
-- ---
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   345
-- Parameters (from login name [localpart@the_domain]):
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   346
--      varchar localpart
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   347
--      varchar the_domain
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   348
-- Returns: dovecotpassword records
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   349
--
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   350
-- Required access privileges for your dovecot database user:
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   351
--      GRANT SELECT ON users, domain_name TO dovecot;
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   352
--
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   353
-- For more details see http://wiki.dovecot.org/AuthDatabase/SQL
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   354
-- ---
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   355
CREATE OR REPLACE FUNCTION dovecotpassword(
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   356
    IN localpart varchar, IN the_domain varchar) RETURNS SETOF dovecotpassword
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   357
AS $$
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   358
    DECLARE
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   359
        record dovecotpassword;
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   360
        userid varchar(320) := localpart || '@' || the_domain;
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   361
    BEGIN
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   362
        FOR record IN
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   363
            SELECT userid, passwd, smtp, pop3, imap, managesieve
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   364
              FROM users
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   365
             WHERE gid = (SELECT gid
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   366
                            FROM domain_name
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   367
                           WHERE domainname = the_domain)
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   368
               AND local_part = localpart
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   369
            LOOP
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   370
                RETURN NEXT record;
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   371
            END LOOP;
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   372
        RETURN;
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   373
    END;
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   374
$$ LANGUAGE plpgsql STABLE
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   375
RETURNS NULL ON NULL INPUT
af813ede1e19 * 'create_optional_types_and_functions.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 74
diff changeset
   376
EXTERNAL SECURITY INVOKER;