pgsql/create_tables-dovecot-1.2.x.pgsql
branchv0.6.x
changeset 502 e1b32377032f
parent 500 5ccc9c6e5193
child 503 492c179094c9
equal deleted inserted replaced
501:f2387d60624b 502:e1b32377032f
   193            LEFT JOIN domain_name USING (gid)
   193            LEFT JOIN domain_name USING (gid)
   194            LEFT JOIN users USING (gid)
   194            LEFT JOIN users USING (gid)
   195      WHERE domain_name.is_primary
   195      WHERE domain_name.is_primary
   196   GROUP BY gid;
   196   GROUP BY gid;
   197 
   197 
       
   198 -- ########################################################################## --
   198 
   199 
   199 CREATE LANGUAGE plpgsql;
   200 CREATE LANGUAGE plpgsql;
   200 
   201 
       
   202 -- ######################## TYPEs ########################################### --
       
   203 
       
   204 -- ---
       
   205 -- Data type for function postfix_virtual_mailbox(varchar, varchar)
       
   206 -- ---
       
   207 CREATE TYPE address_maildir AS (
       
   208     address varchar(320),
       
   209     maildir text
       
   210 );
       
   211 -- ---
       
   212 -- Data type for function dovecotpassword(varchar, varchar)
       
   213 -- ---
       
   214 CREATE TYPE dovecotpassword AS (
       
   215     userid    varchar(320),
       
   216     password  varchar(270),
       
   217     smtp      boolean,
       
   218     pop3      boolean,
       
   219     imap      boolean,
       
   220     sieve     boolean
       
   221 );
       
   222 -- ---
       
   223 -- Data type for function dovecotquotauser(varchar, varchar)
       
   224 -- ---
       
   225 CREATE TYPE dovecotquotauser AS (
       
   226     userid      varchar(320),
       
   227     uid         bigint,
       
   228     gid         bigint,
       
   229     home        text,
       
   230     mail        text,
       
   231     quota_rule  text
       
   232 );
       
   233 -- ---
       
   234 -- Data type for function dovecotuser(varchar, varchar)
       
   235 -- ---
       
   236 CREATE TYPE dovecotuser AS (
       
   237     userid      varchar(320),
       
   238     uid         bigint,
       
   239     gid         bigint,
       
   240     home        text,
       
   241     mail        text
       
   242 );
       
   243 -- ---
       
   244 -- Data type for functions: postfix_relocated_map(varchar, varchar)
       
   245 --                          postfix_virtual_alias_map(varchar, varchar)
       
   246 -- ---
       
   247 CREATE TYPE recipient_destination AS (
       
   248     recipient   varchar(320),
       
   249     destination text
       
   250 );
       
   251 -- ---
       
   252 -- Data type for function postfix_transport_map(varchar, varchar)
       
   253 -- ---
       
   254 CREATE TYPE recipient_transport AS (
       
   255     recipient   varchar(320),
       
   256     transport   text
       
   257 );
       
   258 -- ---
       
   259 -- Data type for function postfix_virtual_uid_map(varchar, varchar)
       
   260 -- ---
       
   261 CREATE TYPE recipient_uid AS (
       
   262     recipient   varchar(320),
       
   263     uid         bigint
       
   264 );
       
   265 -- ---
       
   266 -- Data type for function postfix_smtpd_sender_login_map(varchar, varchar)
       
   267 -- ---
       
   268 CREATE TYPE sender_login AS (
       
   269     sender  varchar(320),
       
   270     login   text
       
   271 );
       
   272 
       
   273 -- ######################## TRIGGERs ######################################## --
   201 
   274 
   202 CREATE OR REPLACE FUNCTION domain_primary_trigger() RETURNS TRIGGER AS $$
   275 CREATE OR REPLACE FUNCTION domain_primary_trigger() RETURNS TRIGGER AS $$
   203 DECLARE
   276 DECLARE
   204     primary_count bigint;
   277     primary_count bigint;
   205 BEGIN
   278 BEGIN
   260 $$ LANGUAGE plpgsql;
   333 $$ LANGUAGE plpgsql;
   261 
   334 
   262 
   335 
   263 CREATE TRIGGER mergeuserquota BEFORE INSERT ON userquota
   336 CREATE TRIGGER mergeuserquota BEFORE INSERT ON userquota
   264     FOR EACH ROW EXECUTE PROCEDURE merge_userquota();
   337     FOR EACH ROW EXECUTE PROCEDURE merge_userquota();
       
   338 
       
   339 -- ######################## FUNCTIONs ####################################### --
       
   340 
       
   341 -- ---
       
   342 -- Parameters (from login name [localpart@the_domain]):
       
   343 --      varchar localpart
       
   344 --      varchar the_domain
       
   345 -- Returns: dovecotpassword records
       
   346 --
       
   347 -- Required access privileges for your dovecot database user:
       
   348 --      GRANT SELECT ON users, domain_name, service_set TO dovecot;
       
   349 --
       
   350 -- For more details see http://wiki.dovecot.org/AuthDatabase/SQL
       
   351 -- ---
       
   352 CREATE OR REPLACE FUNCTION dovecotpassword(
       
   353     IN localpart varchar, IN the_domain varchar) RETURNS SETOF dovecotpassword
       
   354 AS $$
       
   355     DECLARE
       
   356         record dovecotpassword;
       
   357         userid varchar(320) := localpart || '@' || the_domain;
       
   358     BEGIN
       
   359         FOR record IN
       
   360             SELECT userid, passwd, smtp, pop3, imap, sieve
       
   361               FROM users, service_set
       
   362              WHERE gid = (SELECT gid
       
   363                             FROM domain_name
       
   364                            WHERE domainname = the_domain)
       
   365                AND local_part = localpart
       
   366                AND service_set.ssid = users.ssid
       
   367             LOOP
       
   368                 RETURN NEXT record;
       
   369             END LOOP;
       
   370         RETURN;
       
   371     END;
       
   372 $$ LANGUAGE plpgsql STABLE
       
   373 RETURNS NULL ON NULL INPUT
       
   374 EXTERNAL SECURITY INVOKER;
       
   375 -- ---
       
   376 -- Nearly the same as function dovecotuser below. It returns additionally the
       
   377 -- field quota_rule.
       
   378 --
       
   379 -- Required access privileges for your dovecot database user:
       
   380 --      GRANT SELECT
       
   381 --          ON users, domain_data, domain_name, maillocation, mailboxformat,
       
   382 --             quotalimit
       
   383 --          TO dovecot;
       
   384 -- ---
       
   385 CREATE OR REPLACE FUNCTION dovecotquotauser(
       
   386     IN localpart varchar, IN the_domain varchar) RETURNS SETOF dovecotquotauser
       
   387 AS $$
       
   388     DECLARE
       
   389         record dovecotquotauser;
       
   390         userid varchar(320) := localpart || '@' || the_domain;
       
   391         did bigint := (SELECT gid FROM domain_name WHERE domainname=the_domain);
       
   392     BEGIN
       
   393         FOR record IN
       
   394             SELECT userid, uid, did, domaindir || '/' || uid AS home,
       
   395                    format || ':~/' || directory AS mail, '*:bytes=' ||
       
   396                    bytes || ':messages=' || messages AS quota_rule
       
   397               FROM users, domain_data, mailboxformat, maillocation, quotalimit
       
   398              WHERE users.gid = did
       
   399                AND users.local_part = localpart
       
   400                AND maillocation.mid = users.mid
       
   401                AND mailboxformat.fid = maillocation.fid
       
   402                AND domain_data.gid = did
       
   403                AND quotalimit.qid = users.qid
       
   404             LOOP
       
   405                 RETURN NEXT record;
       
   406             END LOOP;
       
   407         RETURN;
       
   408     END;
       
   409 $$ LANGUAGE plpgsql STABLE
       
   410 RETURNS NULL ON NULL INPUT
       
   411 EXTERNAL SECURITY INVOKER;
       
   412 -- ---
       
   413 -- Parameters (from login name [localpart@the_domain]):
       
   414 --      varchar localpart
       
   415 --      varchar the_domain
       
   416 -- Returns: dovecotuser records
       
   417 --
       
   418 -- Required access privileges for your dovecot database user:
       
   419 --      GRANT SELECT
       
   420 --          ON users, domain_data, domain_name, maillocation, mailboxformat
       
   421 --          TO dovecot;
       
   422 --
       
   423 -- For more details see http://wiki.dovecot.org/UserDatabase
       
   424 -- ---
       
   425 CREATE OR REPLACE FUNCTION dovecotuser(
       
   426     IN localpart varchar, IN the_domain varchar) RETURNS SETOF dovecotuser
       
   427 AS $$
       
   428     DECLARE
       
   429         record dovecotuser;
       
   430         userid varchar(320) := localpart || '@' || the_domain;
       
   431         did bigint := (SELECT gid FROM domain_name WHERE domainname=the_domain);
       
   432     BEGIN
       
   433         FOR record IN
       
   434             SELECT userid, uid, did, domaindir || '/' || uid AS home,
       
   435                    format || ':~/' || directory AS mail
       
   436               FROM users, domain_data, mailboxformat, maillocation
       
   437              WHERE users.gid = did
       
   438                AND users.local_part = localpart
       
   439                AND maillocation.mid = users.mid
       
   440                AND mailboxformat.fid = maillocation.fid
       
   441                AND domain_data.gid = did
       
   442             LOOP
       
   443                 RETURN NEXT record;
       
   444             END LOOP;
       
   445         RETURN;
       
   446     END;
       
   447 $$ LANGUAGE plpgsql STABLE
       
   448 RETURNS NULL ON NULL INPUT
       
   449 EXTERNAL SECURITY INVOKER;
       
   450 -- ---
       
   451 -- Parameters (from recipients address (MAIL TO) [localpart@the_domain]):
       
   452 --      varchar localpart
       
   453 --      varchar the_domain
       
   454 -- Returns: recipient_destination records
       
   455 --
       
   456 -- Required access privileges for your postfix database user:
       
   457 --      GRANT SELECT ON domain_name, relocated TO postfix;
       
   458 --
       
   459 -- For more details see postconf(5) section relocated_maps and relocated(5)
       
   460 -- ---
       
   461 CREATE OR REPLACE FUNCTION postfix_relocated_map(
       
   462     IN localpart varchar, IN the_domain varchar)
       
   463     RETURNS SETOF recipient_destination
       
   464 AS $$
       
   465     DECLARE
       
   466         record recipient_destination;
       
   467         recipient varchar(320) := localpart || '@' || the_domain;
       
   468         did bigint := (SELECT gid FROM domain_name WHERE domainname=the_domain);
       
   469     BEGIN
       
   470         FOR record IN
       
   471             SELECT recipient, destination
       
   472               FROM relocated
       
   473              WHERE gid = did
       
   474                AND address = localpart
       
   475             LOOP
       
   476                 RETURN NEXT record;
       
   477             END LOOP;
       
   478         RETURN;
       
   479     END;
       
   480 $$ LANGUAGE plpgsql STABLE
       
   481 RETURNS NULL ON NULL INPUT
       
   482 EXTERNAL SECURITY INVOKER;
       
   483 -- ---
       
   484 -- Parameters (from _sender_ address (MAIL FROM) [localpart@the_domain]):
       
   485 --      varchar localpart
       
   486 --      varchar the_domain
       
   487 -- Returns: SASL _login_ names that own _sender_ addresses (MAIL FROM):
       
   488 --      set of sender_login records.
       
   489 --
       
   490 -- Required access privileges for your postfix database user:
       
   491 --      GRANT SELECT ON domain_name, users, alias TO postfix;
       
   492 --
       
   493 -- For more details see postconf(5) section smtpd_sender_login_maps
       
   494 -- ---
       
   495 CREATE OR REPLACE FUNCTION postfix_smtpd_sender_login_map(
       
   496     IN localpart varchar, IN the_domain varchar) RETURNS SETOF sender_login
       
   497 AS $$
       
   498     DECLARE
       
   499         rec sender_login;
       
   500         did bigint := (SELECT gid FROM domain_name WHERE domainname=the_domain);
       
   501         sender varchar(320) := localpart || '@' || the_domain;
       
   502     BEGIN
       
   503         -- Get all addresses for 'localpart' in the primary and aliased domains
       
   504         FOR rec IN
       
   505             SELECT sender, local_part || '@' || domainname
       
   506               FROM domain_name, users
       
   507              WHERE domain_name.gid = did
       
   508                AND users.gid = did
       
   509                AND users.local_part = localpart
       
   510             LOOP
       
   511                 RETURN NEXT rec;
       
   512             END LOOP;
       
   513         IF NOT FOUND THEN
       
   514             -- Loop over the alias addresses for localpart@the_domain
       
   515             FOR rec IN
       
   516                 SELECT DISTINCT sender, destination
       
   517                   FROM alias
       
   518                  WHERE alias.gid = did
       
   519                    AND alias.address = localpart
       
   520                 LOOP
       
   521                     RETURN NEXT rec;
       
   522                 END LOOP;
       
   523         END IF;
       
   524         RETURN;
       
   525     END;
       
   526 $$ LANGUAGE plpgsql STABLE
       
   527 RETURNS NULL ON NULL INPUT
       
   528 EXTERNAL SECURITY INVOKER;
       
   529 -- ---
       
   530 -- Parameters (from recipients address (MAIL TO) [localpart@the_domain]):
       
   531 --      varchar localpart
       
   532 --      varchar the_domain
       
   533 -- Returns: recipient_transport records
       
   534 --
       
   535 -- Required access privileges for your postfix database user:
       
   536 --      GRANT SELECT ON users, transport, domain_name TO postfix;
       
   537 --
       
   538 -- For more details see postconf(5) section transport_maps and transport(5)
       
   539 -- ---
       
   540 CREATE OR REPLACE FUNCTION postfix_transport_map(
       
   541     IN localpart varchar, IN the_domain varchar)
       
   542     RETURNS SETOF recipient_transport
       
   543 AS $$
       
   544     DECLARE
       
   545         record recipient_transport;
       
   546         recipient varchar(320) := localpart || '@' || the_domain;
       
   547     BEGIN
       
   548         FOR record IN
       
   549             SELECT recipient, transport
       
   550               FROM transport
       
   551              WHERE tid = (SELECT tid
       
   552                             FROM users
       
   553                            WHERE gid = (SELECT gid
       
   554                                           FROM domain_name
       
   555                                          WHERE domainname = the_domain)
       
   556                              AND local_part = localpart)
       
   557             LOOP
       
   558                 RETURN NEXT record;
       
   559             END LOOP;
       
   560         RETURN;
       
   561     END;
       
   562 $$ LANGUAGE plpgsql STABLE
       
   563 RETURNS NULL ON NULL INPUT
       
   564 EXTERNAL SECURITY INVOKER;
       
   565 -- ---
       
   566 -- Parameters (from recipients address (MAIL TO) [localpart@the_domain]):
       
   567 --      varchar localpart
       
   568 --      varchar the_domain
       
   569 -- Returns: recipient_destination records
       
   570 --
       
   571 -- Required access privileges for your postfix database user:
       
   572 --      GRANT SELECT ON alias, domain_name TO postfix;
       
   573 --
       
   574 -- For more details see postconf(5) section virtual_alias_maps and virtual(5)
       
   575 -- ---
       
   576 CREATE OR REPLACE FUNCTION postfix_virtual_alias_map(
       
   577     IN localpart varchar, IN the_domain varchar)
       
   578     RETURNS SETOF recipient_destination
       
   579 AS $$
       
   580     DECLARE
       
   581         record recipient_destination;
       
   582         recipient varchar(320) := localpart || '@' || the_domain;
       
   583         did bigint := (SELECT gid FROM domain_name WHERE domainname=the_domain);
       
   584     BEGIN
       
   585         FOR record IN
       
   586             SELECT recipient, destination
       
   587               FROM alias
       
   588              WHERE gid = did
       
   589                AND address = localpart
       
   590             LOOP
       
   591                 RETURN NEXT record;
       
   592             END LOOP;
       
   593         RETURN;
       
   594     END;
       
   595 $$ LANGUAGE plpgsql STABLE
       
   596 RETURNS NULL ON NULL INPUT
       
   597 EXTERNAL SECURITY INVOKER;
       
   598 -- ---
       
   599 -- Parameters (from recipients address (MAIL TO) [localpart@the_domain]):
       
   600 --      varchar localpart
       
   601 --      varchar the_domain
       
   602 -- Returns: address_maildir records
       
   603 --
       
   604 -- Required access privileges for your postfix database user:
       
   605 --      GRANT SELECT ON domain_data,domain_name,maillocation,users TO postfix;
       
   606 --
       
   607 -- For more details see postconf(5) section virtual_mailbox_maps
       
   608 -- ---
       
   609 CREATE OR REPLACE FUNCTION postfix_virtual_mailbox_map(
       
   610    IN localpart varchar, IN the_domain varchar) RETURNS SETOF address_maildir
       
   611 AS $$
       
   612     DECLARE
       
   613         rec address_maildir;
       
   614         did bigint := (SELECT gid FROM domain_name WHERE domainname=the_domain);
       
   615         address varchar(320) := localpart || '@' || the_domain;
       
   616     BEGIN
       
   617         FOR rec IN
       
   618             SELECT address, domaindir||'/'||users.uid||'/'||directory||'/'
       
   619               FROM domain_data, users, maillocation
       
   620              WHERE domain_data.gid = did
       
   621                AND users.gid = did
       
   622                AND users.local_part = localpart
       
   623                AND maillocation.mid = users.mid
       
   624             LOOP
       
   625                 RETURN NEXT rec;
       
   626             END LOOP;
       
   627         RETURN;
       
   628     END;
       
   629 $$ LANGUAGE plpgsql STABLE
       
   630 RETURNS NULL ON NULL INPUT
       
   631 EXTERNAL SECURITY INVOKER;
       
   632 -- ---
       
   633 -- Parameters (from recipients address (MAIL TO) [localpart@the_domain]):
       
   634 --      varchar localpart
       
   635 --      varchar the_domain
       
   636 -- Returns: recipient_uid records
       
   637 --
       
   638 -- Required access privileges for your postfix database user:
       
   639 --      GRANT SELECT ON users, domain_name TO postfix;
       
   640 --
       
   641 -- For more details see postconf(5) section virtual_uid_maps
       
   642 -- ---
       
   643 CREATE OR REPLACE FUNCTION postfix_virtual_uid_map(
       
   644     IN localpart varchar, IN the_domain varchar) RETURNS SETOF recipient_uid
       
   645 AS $$
       
   646     DECLARE
       
   647         record recipient_uid;
       
   648         recipient varchar(320) := localpart || '@' || the_domain;
       
   649     BEGIN
       
   650         FOR record IN
       
   651             SELECT recipient, uid
       
   652               FROM users
       
   653              WHERE gid = (SELECT gid
       
   654                             FROM domain_name
       
   655                            WHERE domainname = the_domain)
       
   656                AND local_part = localpart
       
   657             LOOP
       
   658                 RETURN NEXT record;
       
   659             END LOOP;
       
   660         RETURN;
       
   661     END;
       
   662 $$ LANGUAGE plpgsql STABLE
       
   663 RETURNS NULL ON NULL INPUT
       
   664 EXTERNAL SECURITY INVOKER;