--
-- Database Schema for gnuviechadmin-tools
-- (c) 2006 Jan Dittberner <jan@dittberner.info>
-- $Id$
--

SET client_encoding = 'SQL_ASCII';
SET check_function_bodies = false;

--
-- Country names
--

CREATE TABLE country (
	countryid serial NOT NULL,
	name varchar(40) NOT NULL,
	PRIMARY KEY (countryid)
);
REVOKE ALL ON TABLE country FROM PUBLIC;

--
-- Salutations
--
CREATE TABLE salutation (
	salutationid serial NOT NULL,
	name varchar(20) NOT NULL,
	primary key (salutationid),
	unique (name)
);
REVOKE ALL ON TABLE salutation FROM PUBLIC;

--
-- Client data
--
CREATE TABLE client (
	clientid serial NOT NULL,
	clientnr varchar(10) NOT NULL,
	salutationid integer NOT NULL REFERENCES salutation(salutationid),
	firstname varchar(40) NOT NULL,
	lastname varchar(40) NOT NULL,
	address1 varchar(40) NOT NULL,
	address2 varchar(40),
	countryid integer NOT NULL REFERENCES country(countryid),
	town varchar(50) NOT NULL,
	zipcode character(5) NOT NULL,
	state varchar(40),
	active boolean DEFAULT false NOT NULL,
	phone varchar(20),
	mobile varchar(20),
	reseller_id integer REFERENCES client(clientid),
	organisation varchar(200),
	primary key (clientid),
	unique (clientnr)
);
REVOKE ALL ON TABLE client FROM PUBLIC;

--
-- Email addresses
--
CREATE TABLE email (
	emailid serial NOT NULL,
	email varchar(128) NOT NULL,
	clientid integer NOT NULL REFERENCES client(clientid),
	verified boolean DEFAULT false NOT NULL,
	sortorder integer NOT NULL,
	primary key (emailid),
	unique (email),
	unique (clientid, sortorder)
);
REVOKE ALL ON TABLE email FROM PUBLIC;

--
-- System user to client mapping
--
CREATE TABLE sysuser (
	sysuserid serial NOT NULL,
	name varchar(12) NOT NULL,
	"type" integer DEFAULT 0 NOT NULL,
	home varchar(128),
	shell boolean,
	"password" varchar(64),
	clientid integer NOT NULL REFERENCES client(clientid),
	toupdate boolean DEFAULT false NOT NULL,
	md5pass varchar(32),
	sysuid integer,
	primary key (sysuserid),
	unique (name),
	unique (sysuid)
);
CREATE INDEX sysuser_type_idx ON sysuser ("type");
REVOKE ALL ON TABLE sysuser FROM PUBLIC;
GRANT SELECT ON TABLE sysuser TO GROUP services;

--
-- Domains
--
CREATE TABLE "domain" (
	domainid serial NOT NULL,
	domainname varchar(64) NOT NULL,
	clientid integer NOT NULL REFERENCES client(clientid),
	status integer DEFAULT 0 NOT NULL,
	sysuserid integer NOT NULL REFERENCES sysuser(sysuserid),
	primary key (domainid),
	unique (domainname)
);
REVOKE ALL ON TABLE "domain" FROM PUBLIC;
GRANT SELECT ON TABLE "domain" TO GROUP services;

--
-- DNS Zones
--
CREATE TABLE "zone" (
	zoneid serial NOT NULL,
	"domain" integer NOT NULL REFERENCES "domain"(domainid),
	"type" varchar(5) NOT NULL,
	ttl integer NOT NULL,
	mxprio integer,
	resource varchar(256),
	toupdate boolean DEFAULT false NOT NULL,
	primary key (zoneid)
);
REVOKE ALL ON TABLE "zone" FROM PUBLIC;

--
-- Mail passwords for pop3/imap accounts
--
CREATE TABLE mailpasswd (
	id varchar(128) DEFAULT ''::varchar NOT NULL,
	crypt varchar(128) DEFAULT ''::varchar NOT NULL,
	clear varchar(128) DEFAULT ''::varchar NOT NULL,
	name varchar(128) DEFAULT ''::varchar NOT NULL,
	uid integer DEFAULT 65534 NOT NULL,
	gid integer DEFAULT 65534 NOT NULL,
	home varchar(255) DEFAULT ''::varchar NOT NULL,
	maildir varchar(255) DEFAULT ''::varchar NOT NULL,
	defaultdelivery varchar(255) DEFAULT ''::varchar NOT NULL,
	quota varchar(255) DEFAULT ''::varchar NOT NULL,
	spamcheck boolean DEFAULT false NOT NULL,
	sajunkscore integer,
	primary key (id)
);
REVOKE ALL ON TABLE mailpasswd FROM PUBLIC;
GRANT SELECT ON TABLE mailpasswd TO GROUP services;

--
-- Mail aliases for forwarding configuration and pop3/imap inbox mapping
--
CREATE TABLE mailalias (
	email varchar(255) NOT NULL,
	"domain" varchar(255) NOT NULL,
	target varchar(255) NOT NULL,
	primary key (email, "domain")
);
REVOKE ALL ON TABLE mailalias FROM PUBLIC;
GRANT SELECT ON TABLE mailalias TO GROUP services;
