pgsql/create_tables.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     managesieve 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
   237 END;
   310 END;
   238 $$ LANGUAGE plpgsql;
   311 $$ LANGUAGE plpgsql;
   239 
   312 
   240 CREATE TRIGGER mergeuserquota_11 BEFORE INSERT ON userquota_11
   313 CREATE TRIGGER mergeuserquota_11 BEFORE INSERT ON userquota_11
   241     FOR EACH ROW EXECUTE PROCEDURE merge_userquota_11();
   314     FOR EACH ROW EXECUTE PROCEDURE merge_userquota_11();
       
   315 
       
   316 -- ######################## FUNCTIONs ####################################### --
       
   317 
       
   318 -- ---
       
   319 -- Parameters (from login name [localpart@the_domain]):
       
   320 --      varchar localpart
       
   321 --      varchar the_domain
       
   322 -- Returns: dovecotpassword records
       
   323 --
       
   324 -- Required access privileges for your dovecot database user:
       
   325 --      GRANT SELECT ON users, domain_name, service_set TO dovecot;
       
   326 --
       
   327 -- For more details see http://wiki.dovecot.org/AuthDatabase/SQL
       
   328 -- ---
       
   329 CREATE OR REPLACE FUNCTION dovecotpassword(
       
   330     IN localpart varchar, IN the_domain varchar) RETURNS SETOF dovecotpassword
       
   331 AS $$
       
   332     DECLARE
       
   333         record dovecotpassword;
       
   334         userid varchar(320) := localpart || '@' || the_domain;
       
   335     BEGIN
       
   336         FOR record IN
       
   337             SELECT userid, passwd, smtp, pop3, imap, managesieve
       
   338               FROM users, service_set
       
   339              WHERE gid = (SELECT gid
       
   340                             FROM domain_name
       
   341                            WHERE domainname = the_domain)
       
   342                AND local_part = localpart
       
   343                AND service_set.ssid = users.ssid
       
   344             LOOP
       
   345                 RETURN NEXT record;
       
   346             END LOOP;
       
   347         RETURN;
       
   348     END;
       
   349 $$ LANGUAGE plpgsql STABLE
       
   350 RETURNS NULL ON NULL INPUT
       
   351 EXTERNAL SECURITY INVOKER;
       
   352 -- ---
       
   353 -- Nearly the same as function dovecotuser below. It returns additionally the
       
   354 -- field quota_rule.
       
   355 --
       
   356 -- Required access privileges for your dovecot database user:
       
   357 --      GRANT SELECT
       
   358 --          ON users, domain_data, domain_name, maillocation, mailboxformat,
       
   359 --             quotalimit
       
   360 --          TO dovecot;
       
   361 -- ---
       
   362 CREATE OR REPLACE FUNCTION dovecotquotauser(
       
   363     IN localpart varchar, IN the_domain varchar) RETURNS SETOF dovecotquotauser
       
   364 AS $$
       
   365     DECLARE
       
   366         record dovecotquotauser;
       
   367         userid varchar(320) := localpart || '@' || the_domain;
       
   368         did bigint := (SELECT gid FROM domain_name WHERE domainname=the_domain);
       
   369     BEGIN
       
   370         FOR record IN
       
   371             SELECT userid, uid, did, domaindir || '/' || uid AS home,
       
   372                    format || ':~/' || directory AS mail, '*:bytes=' ||
       
   373                    bytes || ':messages=' || messages AS quota_rule
       
   374               FROM users, domain_data, mailboxformat, maillocation, quotalimit
       
   375              WHERE users.gid = did
       
   376                AND users.local_part = localpart
       
   377                AND maillocation.mid = users.mid
       
   378                AND mailboxformat.fid = maillocation.fid
       
   379                AND domain_data.gid = did
       
   380                AND quotalimit.qid = users.qid
       
   381             LOOP
       
   382                 RETURN NEXT record;
       
   383             END LOOP;
       
   384         RETURN;
       
   385     END;
       
   386 $$ LANGUAGE plpgsql STABLE
       
   387 RETURNS NULL ON NULL INPUT
       
   388 EXTERNAL SECURITY INVOKER;
       
   389 -- ---
       
   390 -- Parameters (from login name [localpart@the_domain]):
       
   391 --      varchar localpart
       
   392 --      varchar the_domain
       
   393 -- Returns: dovecotuser records
       
   394 --
       
   395 -- Required access privileges for your dovecot database user:
       
   396 --      GRANT SELECT
       
   397 --          ON users, domain_data, domain_name, maillocation, mailboxformat
       
   398 --          TO dovecot;
       
   399 --
       
   400 -- For more details see http://wiki.dovecot.org/UserDatabase
       
   401 -- ---
       
   402 CREATE OR REPLACE FUNCTION dovecotuser(
       
   403     IN localpart varchar, IN the_domain varchar) RETURNS SETOF dovecotuser
       
   404 AS $$
       
   405     DECLARE
       
   406         record dovecotuser;
       
   407         userid varchar(320) := localpart || '@' || the_domain;
       
   408         did bigint := (SELECT gid FROM domain_name WHERE domainname=the_domain);
       
   409     BEGIN
       
   410         FOR record IN
       
   411             SELECT userid, uid, did, domaindir || '/' || uid AS home,
       
   412                    format || ':~/' || directory AS mail
       
   413               FROM users, domain_data, mailboxformat, maillocation
       
   414              WHERE users.gid = did
       
   415                AND users.local_part = localpart
       
   416                AND maillocation.mid = users.mid
       
   417                AND mailboxformat.fid = maillocation.fid
       
   418                AND domain_data.gid = did
       
   419             LOOP
       
   420                 RETURN NEXT record;
       
   421             END LOOP;
       
   422         RETURN;
       
   423     END;
       
   424 $$ LANGUAGE plpgsql STABLE
       
   425 RETURNS NULL ON NULL INPUT
       
   426 EXTERNAL SECURITY INVOKER;
       
   427 -- ---
       
   428 -- Parameters (from recipients address (MAIL TO) [localpart@the_domain]):
       
   429 --      varchar localpart
       
   430 --      varchar the_domain
       
   431 -- Returns: recipient_destination records
       
   432 --
       
   433 -- Required access privileges for your postfix database user:
       
   434 --      GRANT SELECT ON domain_name, relocated TO postfix;
       
   435 --
       
   436 -- For more details see postconf(5) section relocated_maps and relocated(5)
       
   437 -- ---
       
   438 CREATE OR REPLACE FUNCTION postfix_relocated_map(
       
   439     IN localpart varchar, IN the_domain varchar)
       
   440     RETURNS SETOF recipient_destination
       
   441 AS $$
       
   442     DECLARE
       
   443         record recipient_destination;
       
   444         recipient varchar(320) := localpart || '@' || the_domain;
       
   445         did bigint := (SELECT gid FROM domain_name WHERE domainname=the_domain);
       
   446     BEGIN
       
   447         FOR record IN
       
   448             SELECT recipient, destination
       
   449               FROM relocated
       
   450              WHERE gid = did
       
   451                AND address = localpart
       
   452             LOOP
       
   453                 RETURN NEXT record;
       
   454             END LOOP;
       
   455         RETURN;
       
   456     END;
       
   457 $$ LANGUAGE plpgsql STABLE
       
   458 RETURNS NULL ON NULL INPUT
       
   459 EXTERNAL SECURITY INVOKER;
       
   460 -- ---
       
   461 -- Parameters (from _sender_ address (MAIL FROM) [localpart@the_domain]):
       
   462 --      varchar localpart
       
   463 --      varchar the_domain
       
   464 -- Returns: SASL _login_ names that own _sender_ addresses (MAIL FROM):
       
   465 --      set of sender_login records.
       
   466 --
       
   467 -- Required access privileges for your postfix database user:
       
   468 --      GRANT SELECT ON domain_name, users, alias TO postfix;
       
   469 --
       
   470 -- For more details see postconf(5) section smtpd_sender_login_maps
       
   471 -- ---
       
   472 CREATE OR REPLACE FUNCTION postfix_smtpd_sender_login_map(
       
   473     IN localpart varchar, IN the_domain varchar) RETURNS SETOF sender_login
       
   474 AS $$
       
   475     DECLARE
       
   476         rec sender_login;
       
   477         did bigint := (SELECT gid FROM domain_name WHERE domainname=the_domain);
       
   478         sender varchar(320) := localpart || '@' || the_domain;
       
   479     BEGIN
       
   480         -- Get all addresses for 'localpart' in the primary and aliased domains
       
   481         FOR rec IN
       
   482             SELECT sender, local_part || '@' || domainname
       
   483               FROM domain_name, users
       
   484              WHERE domain_name.gid = did
       
   485                AND users.gid = did
       
   486                AND users.local_part = localpart
       
   487             LOOP
       
   488                 RETURN NEXT rec;
       
   489             END LOOP;
       
   490         IF NOT FOUND THEN
       
   491             -- Loop over the alias addresses for localpart@the_domain
       
   492             FOR rec IN
       
   493                 SELECT DISTINCT sender, destination
       
   494                   FROM alias
       
   495                  WHERE alias.gid = did
       
   496                    AND alias.address = localpart
       
   497                 LOOP
       
   498                     RETURN NEXT rec;
       
   499                 END LOOP;
       
   500         END IF;
       
   501         RETURN;
       
   502     END;
       
   503 $$ LANGUAGE plpgsql STABLE
       
   504 RETURNS NULL ON NULL INPUT
       
   505 EXTERNAL SECURITY INVOKER;
       
   506 -- ---
       
   507 -- Parameters (from recipients address (MAIL TO) [localpart@the_domain]):
       
   508 --      varchar localpart
       
   509 --      varchar the_domain
       
   510 -- Returns: recipient_transport records
       
   511 --
       
   512 -- Required access privileges for your postfix database user:
       
   513 --      GRANT SELECT ON users, transport, domain_name TO postfix;
       
   514 --
       
   515 -- For more details see postconf(5) section transport_maps and transport(5)
       
   516 -- ---
       
   517 CREATE OR REPLACE FUNCTION postfix_transport_map(
       
   518     IN localpart varchar, IN the_domain varchar)
       
   519     RETURNS SETOF recipient_transport
       
   520 AS $$
       
   521     DECLARE
       
   522         record recipient_transport;
       
   523         recipient varchar(320) := localpart || '@' || the_domain;
       
   524     BEGIN
       
   525         FOR record IN
       
   526             SELECT recipient, transport
       
   527               FROM transport
       
   528              WHERE tid = (SELECT tid
       
   529                             FROM users
       
   530                            WHERE gid = (SELECT gid
       
   531                                           FROM domain_name
       
   532                                          WHERE domainname = the_domain)
       
   533                              AND local_part = localpart)
       
   534             LOOP
       
   535                 RETURN NEXT record;
       
   536             END LOOP;
       
   537         RETURN;
       
   538     END;
       
   539 $$ LANGUAGE plpgsql STABLE
       
   540 RETURNS NULL ON NULL INPUT
       
   541 EXTERNAL SECURITY INVOKER;
       
   542 -- ---
       
   543 -- Parameters (from recipients address (MAIL TO) [localpart@the_domain]):
       
   544 --      varchar localpart
       
   545 --      varchar the_domain
       
   546 -- Returns: recipient_destination records
       
   547 --
       
   548 -- Required access privileges for your postfix database user:
       
   549 --      GRANT SELECT ON alias, domain_name TO postfix;
       
   550 --
       
   551 -- For more details see postconf(5) section virtual_alias_maps and virtual(5)
       
   552 -- ---
       
   553 CREATE OR REPLACE FUNCTION postfix_virtual_alias_map(
       
   554     IN localpart varchar, IN the_domain varchar)
       
   555     RETURNS SETOF recipient_destination
       
   556 AS $$
       
   557     DECLARE
       
   558         record recipient_destination;
       
   559         recipient varchar(320) := localpart || '@' || the_domain;
       
   560         did bigint := (SELECT gid FROM domain_name WHERE domainname=the_domain);
       
   561     BEGIN
       
   562         FOR record IN
       
   563             SELECT recipient, destination
       
   564               FROM alias
       
   565              WHERE gid = did
       
   566                AND address = localpart
       
   567             LOOP
       
   568                 RETURN NEXT record;
       
   569             END LOOP;
       
   570         RETURN;
       
   571     END;
       
   572 $$ LANGUAGE plpgsql STABLE
       
   573 RETURNS NULL ON NULL INPUT
       
   574 EXTERNAL SECURITY INVOKER;
       
   575 -- ---
       
   576 -- Parameters (from recipients address (MAIL TO) [localpart@the_domain]):
       
   577 --      varchar localpart
       
   578 --      varchar the_domain
       
   579 -- Returns: address_maildir records
       
   580 --
       
   581 -- Required access privileges for your postfix database user:
       
   582 --      GRANT SELECT ON domain_data,domain_name,maillocation,users TO postfix;
       
   583 --
       
   584 -- For more details see postconf(5) section virtual_mailbox_maps
       
   585 -- ---
       
   586 CREATE OR REPLACE FUNCTION postfix_virtual_mailbox_map(
       
   587    IN localpart varchar, IN the_domain varchar) RETURNS SETOF address_maildir
       
   588 AS $$
       
   589     DECLARE
       
   590         rec address_maildir;
       
   591         did bigint := (SELECT gid FROM domain_name WHERE domainname=the_domain);
       
   592         address varchar(320) := localpart || '@' || the_domain;
       
   593     BEGIN
       
   594         FOR rec IN
       
   595             SELECT address, domaindir||'/'||users.uid||'/'||directory||'/'
       
   596               FROM domain_data, users, maillocation
       
   597              WHERE domain_data.gid = did
       
   598                AND users.gid = did
       
   599                AND users.local_part = localpart
       
   600                AND maillocation.mid = users.mid
       
   601             LOOP
       
   602                 RETURN NEXT rec;
       
   603             END LOOP;
       
   604         RETURN;
       
   605     END;
       
   606 $$ LANGUAGE plpgsql STABLE
       
   607 RETURNS NULL ON NULL INPUT
       
   608 EXTERNAL SECURITY INVOKER;
       
   609 -- ---
       
   610 -- Parameters (from recipients address (MAIL TO) [localpart@the_domain]):
       
   611 --      varchar localpart
       
   612 --      varchar the_domain
       
   613 -- Returns: recipient_uid records
       
   614 --
       
   615 -- Required access privileges for your postfix database user:
       
   616 --      GRANT SELECT ON users, domain_name TO postfix;
       
   617 --
       
   618 -- For more details see postconf(5) section virtual_uid_maps
       
   619 -- ---
       
   620 CREATE OR REPLACE FUNCTION postfix_virtual_uid_map(
       
   621     IN localpart varchar, IN the_domain varchar) RETURNS SETOF recipient_uid
       
   622 AS $$
       
   623     DECLARE
       
   624         record recipient_uid;
       
   625         recipient varchar(320) := localpart || '@' || the_domain;
       
   626     BEGIN
       
   627         FOR record IN
       
   628             SELECT recipient, uid
       
   629               FROM users
       
   630              WHERE gid = (SELECT gid
       
   631                             FROM domain_name
       
   632                            WHERE domainname = the_domain)
       
   633                AND local_part = localpart
       
   634             LOOP
       
   635                 RETURN NEXT record;
       
   636             END LOOP;
       
   637         RETURN;
       
   638     END;
       
   639 $$ LANGUAGE plpgsql STABLE
       
   640 RETURNS NULL ON NULL INPUT
       
   641 EXTERNAL SECURITY INVOKER;