96e982
INSERT INTO databaseVersion VALUES (NULL, 1, 1);
96e982
96e982
-- ~ ************
96e982
-- ~ ** policy table
96e982
-- ~ **
96e982
-- ~ **
96e982
-- ~ **
96e982
-- ~ **
96e982
-- ~ ************
96e982
96e982
INSERT INTO policy 
96e982
SELECT id, 1, name, description,
96e982
0, 0, 0,
96e982
0, 0, 0, 0,
96e982
86400, 0, 0,
96e982
0, 0, 0,
96e982
0, 0, 0,
96e982
0, 0, 0,
96e982
0, 0, 0,
96e982
0, 0, 0,
96e982
0, 0, 0,
96e982
0, 0, 0,
96e982
0
96e982
FROM REMOTE.policies;
96e982
96e982
UPDATE policy
96e982
SET signaturesResign = (
96e982
	SELECT value
96e982
	FROM  REMOTE.parameters_policies
96e982
	INNER JOIN REMOTE.parameters
96e982
	ON REMOTE.parameters_policies.parameter_id = REMOTE.parameters.id 
96e982
	WHERE REMOTE.parameters_policies.policy_id = policy.id 
96e982
		AND REMOTE.parameters.category_id = 1
96e982
		AND REMOTE.parameters.name = 'resign');
96e982
96e982
UPDATE policy
96e982
SET signaturesRefresh = (
96e982
	SELECT value
96e982
	FROM  REMOTE.parameters_policies
96e982
	INNER JOIN REMOTE.parameters
96e982
	ON REMOTE.parameters_policies.parameter_id = REMOTE.parameters.id 
96e982
	WHERE REMOTE.parameters_policies.policy_id = policy.id 
96e982
		AND REMOTE.parameters.category_id = 1
96e982
		AND REMOTE.parameters.name = 'refresh') ;
96e982
96e982
UPDATE policy
96e982
SET signaturesJitter = (
96e982
	SELECT value
96e982
	FROM  REMOTE.parameters_policies
96e982
	INNER JOIN REMOTE.parameters
96e982
	ON REMOTE.parameters_policies.parameter_id = REMOTE.parameters.id 
96e982
	WHERE REMOTE.parameters_policies.policy_id = policy.id 
96e982
		AND REMOTE.parameters.category_id = 1
96e982
		AND REMOTE.parameters.name = 'jitter');
96e982
96e982
UPDATE policy
96e982
SET signaturesInceptionOffset = (
96e982
	SELECT value
96e982
	FROM  REMOTE.parameters_policies
96e982
	INNER JOIN REMOTE.parameters
96e982
	ON REMOTE.parameters_policies.parameter_id = REMOTE.parameters.id 
96e982
	WHERE REMOTE.parameters_policies.policy_id = policy.id 
96e982
		AND REMOTE.parameters.category_id = 1
96e982
		AND REMOTE.parameters.name = 'clockskew');
96e982
96e982
UPDATE policy
96e982
SET signaturesValidityDefault = (
96e982
	SELECT value
96e982
	FROM  REMOTE.parameters_policies
96e982
	INNER JOIN REMOTE.parameters
96e982
	ON REMOTE.parameters_policies.parameter_id = REMOTE.parameters.id 
96e982
	WHERE REMOTE.parameters_policies.policy_id = policy.id 
96e982
		AND REMOTE.parameters.category_id = 1
96e982
		AND REMOTE.parameters.name = 'valdefault');
96e982
96e982
UPDATE policy
96e982
SET signaturesValidityDenial = (
96e982
	SELECT value
96e982
	FROM  REMOTE.parameters_policies
96e982
	INNER JOIN REMOTE.parameters
96e982
	ON REMOTE.parameters_policies.parameter_id = REMOTE.parameters.id 
96e982
	WHERE REMOTE.parameters_policies.policy_id = policy.id 
96e982
		AND REMOTE.parameters.category_id = 1
96e982
		AND REMOTE.parameters.name = 'valdenial');
96e982
96e982
--MaxZoneTTL default 86400
96e982
96e982
-- We need the following mapping 1.4 -> 2.0 for denialType
96e982
-- 0 -> 1
96e982
-- 3 -> 0
96e982
96e982
UPDATE policy
96e982
SET denialType = (
96e982
	SELECT (~value)&1
96e982
	FROM  REMOTE.parameters_policies
96e982
	INNER JOIN REMOTE.parameters
96e982
	ON REMOTE.parameters_policies.parameter_id = REMOTE.parameters.id 
96e982
	WHERE REMOTE.parameters_policies.policy_id = policy.id 
96e982
		AND REMOTE.parameters.category_id = 2
96e982
		AND REMOTE.parameters.name = 'version');
96e982
96e982
-- I'm pretty sure this is not the correct way to do it. It is aweful but
96e982
-- I can't figure it out how it would work for sqlite.
96e982
UPDATE policy
96e982
SET denialOptout = (
96e982
	SELECT value
96e982
	FROM  REMOTE.parameters_policies
96e982
	INNER JOIN REMOTE.parameters
96e982
	ON REMOTE.parameters_policies.parameter_id = REMOTE.parameters.id 
96e982
	WHERE REMOTE.parameters_policies.policy_id = policy.id 
96e982
		AND REMOTE.parameters.category_id = 2
96e982
		AND REMOTE.parameters.name = 'optout')
96e982
WHERE null !=  (
96e982
	SELECT value
96e982
	FROM  REMOTE.parameters_policies
96e982
	INNER JOIN REMOTE.parameters
96e982
	ON REMOTE.parameters_policies.parameter_id = REMOTE.parameters.id 
96e982
	WHERE REMOTE.parameters_policies.policy_id = policy.id 
96e982
		AND REMOTE.parameters.category_id = 2
96e982
		AND REMOTE.parameters.name = 'optout');
96e982
96e982
UPDATE policy
96e982
SET denialTtl = (
96e982
	SELECT value
96e982
	FROM  REMOTE.parameters_policies
96e982
	INNER JOIN REMOTE.parameters
96e982
	ON REMOTE.parameters_policies.parameter_id = REMOTE.parameters.id 
96e982
	WHERE REMOTE.parameters_policies.policy_id = policy.id 
96e982
		AND REMOTE.parameters.category_id = 2
96e982
		AND REMOTE.parameters.name = 'ttl')
96e982
WHERE null != (
96e982
	SELECT value
96e982
	FROM  REMOTE.parameters_policies
96e982
	INNER JOIN REMOTE.parameters
96e982
	ON REMOTE.parameters_policies.parameter_id = REMOTE.parameters.id 
96e982
	WHERE REMOTE.parameters_policies.policy_id = policy.id 
96e982
		AND REMOTE.parameters.category_id = 2
96e982
		AND REMOTE.parameters.name = 'ttl');
96e982
96e982
UPDATE policy
96e982
SET denialResalt = (
96e982
	SELECT value
96e982
	FROM  REMOTE.parameters_policies
96e982
	INNER JOIN REMOTE.parameters
96e982
	ON REMOTE.parameters_policies.parameter_id = REMOTE.parameters.id 
96e982
	WHERE REMOTE.parameters_policies.policy_id = policy.id 
96e982
		AND REMOTE.parameters.category_id = 2
96e982
		AND REMOTE.parameters.name = 'resalt')
96e982
WHERE null != (
96e982
	SELECT value
96e982
	FROM  REMOTE.parameters_policies
96e982
	INNER JOIN REMOTE.parameters
96e982
	ON REMOTE.parameters_policies.parameter_id = REMOTE.parameters.id 
96e982
	WHERE REMOTE.parameters_policies.policy_id = policy.id 
96e982
		AND REMOTE.parameters.category_id = 2
96e982
		AND REMOTE.parameters.name = 'resalt');
96e982
96e982
UPDATE policy
96e982
SET denialAlgorithm = (
96e982
	SELECT value
96e982
	FROM  REMOTE.parameters_policies
96e982
	INNER JOIN REMOTE.parameters
96e982
	ON REMOTE.parameters_policies.parameter_id = REMOTE.parameters.id 
96e982
	WHERE REMOTE.parameters_policies.policy_id = policy.id 
96e982
		AND REMOTE.parameters.category_id = 2
96e982
		AND REMOTE.parameters.name = 'algorithm')
96e982
WHERE null != (
96e982
	SELECT value
96e982
	FROM  REMOTE.parameters_policies
96e982
	INNER JOIN REMOTE.parameters
96e982
	ON REMOTE.parameters_policies.parameter_id = REMOTE.parameters.id 
96e982
	WHERE REMOTE.parameters_policies.policy_id = policy.id 
96e982
		AND REMOTE.parameters.category_id = 2
96e982
		AND REMOTE.parameters.name = 'algorithm');
96e982
96e982
UPDATE policy
96e982
SET denialIterations = (
96e982
	SELECT value
96e982
	FROM  REMOTE.parameters_policies
96e982
	INNER JOIN REMOTE.parameters
96e982
	ON REMOTE.parameters_policies.parameter_id = REMOTE.parameters.id 
96e982
	WHERE REMOTE.parameters_policies.policy_id = policy.id 
96e982
		AND REMOTE.parameters.category_id = 2
96e982
		AND REMOTE.parameters.name = 'iterations')
96e982
WHERE null != (
96e982
	SELECT value
96e982
	FROM  REMOTE.parameters_policies
96e982
	INNER JOIN REMOTE.parameters
96e982
	ON REMOTE.parameters_policies.parameter_id = REMOTE.parameters.id 
96e982
	WHERE REMOTE.parameters_policies.policy_id = policy.id 
96e982
		AND REMOTE.parameters.category_id = 2
96e982
		AND REMOTE.parameters.name = 'iterations');
96e982
96e982
UPDATE policy
96e982
SET denialSaltLength = (
96e982
	SELECT value
96e982
	FROM  REMOTE.parameters_policies
96e982
	INNER JOIN REMOTE.parameters
96e982
	ON REMOTE.parameters_policies.parameter_id = REMOTE.parameters.id 
96e982
	WHERE REMOTE.parameters_policies.policy_id = policy.id 
96e982
		AND REMOTE.parameters.category_id = 2
96e982
		AND REMOTE.parameters.name = 'saltlength')
96e982
WHERE null != (
96e982
	SELECT value
96e982
	FROM  REMOTE.parameters_policies
96e982
	INNER JOIN REMOTE.parameters
96e982
	ON REMOTE.parameters_policies.parameter_id = REMOTE.parameters.id 
96e982
	WHERE REMOTE.parameters_policies.policy_id = policy.id 
96e982
		AND REMOTE.parameters.category_id = 2
96e982
		AND REMOTE.parameters.name = 'saltlength');
96e982
96e982
-- clumsy salt update. salt is optional in 1.4 but required in 2.0
96e982
-- sqlite is limited in what it can do in an update. I hope there is a
96e982
-- better way for this?
96e982
96e982
UPDATE policy
96e982
SET denialSalt = (
96e982
	SELECT salt
96e982
	FROM  REMOTE.policies
96e982
	WHERE REMOTE.policies.id = policy.id)
96e982
WHERE (
96e982
	SELECT salt
96e982
	FROM  REMOTE.policies
96e982
	WHERE REMOTE.policies.id = policy.id) != null;
96e982
96e982
UPDATE policy
96e982
SET denialSaltLastChange = (
96e982
	SELECT salt_stamp
96e982
	FROM  REMOTE.policies
96e982
	WHERE REMOTE.policies.id = policy.id)
96e982
WHERE (
96e982
	SELECT salt_stamp
96e982
	FROM  REMOTE.policies
96e982
	WHERE REMOTE.policies.id = policy.id) != null;
96e982
96e982
UPDATE policy
96e982
SET keysTtl = (
96e982
	SELECT value
96e982
	FROM  REMOTE.parameters_policies
96e982
	INNER JOIN REMOTE.parameters
96e982
	ON REMOTE.parameters_policies.parameter_id = REMOTE.parameters.id 
96e982
	WHERE REMOTE.parameters_policies.policy_id = policy.id 
96e982
		AND REMOTE.parameters.category_id = 5
96e982
		AND REMOTE.parameters.name = 'ttl');
96e982
96e982
UPDATE policy
96e982
SET keysRetireSafety = (
96e982
	SELECT value
96e982
	FROM  REMOTE.parameters_policies
96e982
	INNER JOIN REMOTE.parameters
96e982
	ON REMOTE.parameters_policies.parameter_id = REMOTE.parameters.id 
96e982
	WHERE REMOTE.parameters_policies.policy_id = policy.id 
96e982
		AND REMOTE.parameters.category_id = 5
96e982
		AND REMOTE.parameters.name = 'retiresafety');
96e982
96e982
UPDATE policy
96e982
SET keysPublishSafety = (
96e982
	SELECT value
96e982
	FROM  REMOTE.parameters_policies
96e982
	INNER JOIN REMOTE.parameters
96e982
	ON REMOTE.parameters_policies.parameter_id = REMOTE.parameters.id 
96e982
	WHERE REMOTE.parameters_policies.policy_id = policy.id 
96e982
		AND REMOTE.parameters.category_id = 5
96e982
		AND REMOTE.parameters.name = 'publishsafety');
96e982
96e982
UPDATE policy
96e982
SET keysShared = (
96e982
	SELECT value
96e982
	FROM  REMOTE.parameters_policies
96e982
	INNER JOIN REMOTE.parameters
96e982
	ON REMOTE.parameters_policies.parameter_id = REMOTE.parameters.id 
96e982
	WHERE REMOTE.parameters_policies.policy_id = policy.id 
96e982
		AND REMOTE.parameters.category_id = 5
96e982
		AND REMOTE.parameters.name = 'zones_share_keys');
96e982
96e982
UPDATE policy
96e982
SET keysPurgeAfter = COALESCE((
96e982
	SELECT value
96e982
	FROM  REMOTE.parameters_policies
96e982
	INNER JOIN REMOTE.parameters
96e982
	ON REMOTE.parameters_policies.parameter_id = REMOTE.parameters.id 
96e982
	WHERE REMOTE.parameters_policies.policy_id = policy.id 
96e982
		AND REMOTE.parameters.category_id = 5
96e982
		AND REMOTE.parameters.name = 'purge'), 0);
96e982
96e982
UPDATE policy
96e982
SET zonePropagationDelay = (
96e982
	SELECT value
96e982
	FROM  REMOTE.parameters_policies
96e982
	INNER JOIN REMOTE.parameters
96e982
	ON REMOTE.parameters_policies.parameter_id = REMOTE.parameters.id 
96e982
	WHERE REMOTE.parameters_policies.policy_id = policy.id 
96e982
		AND REMOTE.parameters.category_id = 7
96e982
		AND REMOTE.parameters.name = 'propagationdelay');
96e982
96e982
UPDATE policy
96e982
SET zoneSoaTtl = (
96e982
	SELECT value
96e982
	FROM  REMOTE.parameters_policies
96e982
	INNER JOIN REMOTE.parameters
96e982
	ON REMOTE.parameters_policies.parameter_id = REMOTE.parameters.id 
96e982
	WHERE REMOTE.parameters_policies.policy_id = policy.id 
96e982
		AND REMOTE.parameters.category_id = 7
96e982
		AND REMOTE.parameters.name = 'ttl');
96e982
96e982
UPDATE policy
96e982
SET zoneSoaMinimum = (
96e982
	SELECT value
96e982
	FROM  REMOTE.parameters_policies
96e982
	INNER JOIN REMOTE.parameters
96e982
	ON REMOTE.parameters_policies.parameter_id = REMOTE.parameters.id 
96e982
	WHERE REMOTE.parameters_policies.policy_id = policy.id 
96e982
		AND REMOTE.parameters.category_id = 7
96e982
		AND REMOTE.parameters.name = 'min');
96e982
96e982
-- Temporary mapping table between 1.4 and 2.0 SOA serial strategy
96e982
CREATE TABLE mapping (
96e982
	soa14 INTEGER,
96e982
	soa20 INTEGER
96e982
);
96e982
INSERT INTO mapping SELECT  1, 2;
96e982
INSERT INTO mapping SELECT  2, 0;
96e982
INSERT INTO mapping SELECT  3, 1;
96e982
INSERT INTO mapping SELECT  4, 3;
96e982
96e982
UPDATE policy
96e982
SET zoneSoaSerial = (
96e982
	SELECT mapping.soa20
96e982
	FROM  REMOTE.parameters_policies
96e982
	INNER JOIN REMOTE.parameters
96e982
	ON REMOTE.parameters_policies.parameter_id = REMOTE.parameters.id 
96e982
        INNER JOIN mapping
96e982
        ON REMOTE.parameters_policies.value = mapping.soa14
96e982
	WHERE REMOTE.parameters_policies.policy_id = policy.id 
96e982
		AND REMOTE.parameters.category_id = 7
96e982
		AND REMOTE.parameters.name = 'serial');
96e982
96e982
DROP TABLE mapping;
96e982
96e982
-- parentRegistrationDelay = 0 on 1.4
96e982
96e982
UPDATE policy
96e982
SET parentPropagationDelay = (
96e982
	SELECT value
96e982
	FROM  REMOTE.parameters_policies
96e982
	INNER JOIN REMOTE.parameters
96e982
	ON REMOTE.parameters_policies.parameter_id = REMOTE.parameters.id 
96e982
	WHERE REMOTE.parameters_policies.policy_id = policy.id 
96e982
		AND REMOTE.parameters.category_id = 8
96e982
		AND REMOTE.parameters.name = 'propagationdelay');
96e982
96e982
UPDATE policy
96e982
SET parentDsTtl = (
96e982
	SELECT value
96e982
	FROM  REMOTE.parameters_policies
96e982
	INNER JOIN REMOTE.parameters
96e982
	ON REMOTE.parameters_policies.parameter_id = REMOTE.parameters.id 
96e982
	WHERE REMOTE.parameters_policies.policy_id = policy.id 
96e982
		AND REMOTE.parameters.category_id = 8
96e982
		AND REMOTE.parameters.name = 'ttlds');
96e982
96e982
UPDATE policy
96e982
SET parentSoaTtl = (
96e982
	SELECT value
96e982
	FROM  REMOTE.parameters_policies
96e982
	INNER JOIN REMOTE.parameters
96e982
	ON REMOTE.parameters_policies.parameter_id = REMOTE.parameters.id 
96e982
	WHERE REMOTE.parameters_policies.policy_id = policy.id 
96e982
		AND REMOTE.parameters.category_id = 8
96e982
		AND REMOTE.parameters.name = 'ttl');
96e982
96e982
UPDATE policy
96e982
SET parentSoaMinimum = (
96e982
	SELECT value
96e982
	FROM  REMOTE.parameters_policies
96e982
	INNER JOIN REMOTE.parameters
96e982
	ON REMOTE.parameters_policies.parameter_id = REMOTE.parameters.id 
96e982
	WHERE REMOTE.parameters_policies.policy_id = policy.id 
96e982
		AND REMOTE.parameters.category_id = 8
96e982
		AND REMOTE.parameters.name = 'min');
96e982
96e982
-- passthrough = 0
96e982
96e982
-- ~ ************
96e982
-- ~ ** policyKey table
96e982
-- ~ **
96e982
-- ~ ** For each policy in 1.4 add two keys: KSK and ZSK
96e982
-- ~ **
96e982
-- ~ **
96e982
-- ~ ************
96e982
96e982
-- Insert each KSK
96e982
INSERT INTO policyKey
96e982
SELECT null, 1, id,
96e982
		1, 0, 0,
96e982
		0, 0, 0,
96e982
		0, 0, 4
96e982
FROM REMOTE.policies;
96e982
96e982
-- Insert each ZSK
96e982
INSERT INTO policyKey
96e982
SELECT null, 1, id,
96e982
		2, 0, 0,
96e982
		0, 0, 0,
96e982
		0, 0, 1
96e982
FROM REMOTE.policies;
96e982
96e982
UPDATE policyKey
96e982
SET algorithm = (
96e982
	SELECT value
96e982
	FROM  REMOTE.parameters_policies
96e982
	INNER JOIN REMOTE.parameters
96e982
	ON REMOTE.parameters_policies.parameter_id = REMOTE.parameters.id
96e982
	WHERE REMOTE.parameters_policies.policy_id = policyKey.policyId
96e982
		AND REMOTE.parameters.category_id = 3
96e982
		AND REMOTE.parameters.name = 'algorithm')
96e982
WHERE policyKey.role = 1;
96e982
96e982
UPDATE policyKey
96e982
SET algorithm = (
96e982
	SELECT value
96e982
	FROM  REMOTE.parameters_policies
96e982
	INNER JOIN REMOTE.parameters
96e982
	ON REMOTE.parameters_policies.parameter_id = REMOTE.parameters.id
96e982
	WHERE REMOTE.parameters_policies.policy_id = policyKey.policyId
96e982
		AND REMOTE.parameters.category_id = 4
96e982
		AND REMOTE.parameters.name = 'algorithm')
96e982
WHERE policyKey.role = 2;
96e982
96e982
UPDATE policyKey
96e982
SET bits = (
96e982
	SELECT value
96e982
	FROM  REMOTE.parameters_policies
96e982
	INNER JOIN REMOTE.parameters
96e982
	ON REMOTE.parameters_policies.parameter_id = REMOTE.parameters.id
96e982
	WHERE REMOTE.parameters_policies.policy_id = policyKey.policyId
96e982
		AND REMOTE.parameters.category_id = 3
96e982
		AND REMOTE.parameters.name = 'bits')
96e982
WHERE policyKey.role = 1;
96e982
96e982
UPDATE policyKey
96e982
SET bits = (
96e982
	SELECT value
96e982
	FROM  REMOTE.parameters_policies
96e982
	INNER JOIN REMOTE.parameters
96e982
	ON REMOTE.parameters_policies.parameter_id = REMOTE.parameters.id
96e982
	WHERE REMOTE.parameters_policies.policy_id = policyKey.policyId
96e982
		AND REMOTE.parameters.category_id = 4
96e982
		AND REMOTE.parameters.name = 'bits')
96e982
WHERE policyKey.role = 2;
96e982
96e982
UPDATE policyKey
96e982
SET lifetime = (
96e982
	SELECT value
96e982
	FROM  REMOTE.parameters_policies
96e982
	INNER JOIN REMOTE.parameters
96e982
	ON REMOTE.parameters_policies.parameter_id = REMOTE.parameters.id
96e982
	WHERE REMOTE.parameters_policies.policy_id = policyKey.policyId
96e982
		AND REMOTE.parameters.category_id = 3
96e982
		AND REMOTE.parameters.name = 'lifetime')
96e982
WHERE policyKey.role = 1;
96e982
96e982
UPDATE policyKey
96e982
SET lifetime = (
96e982
	SELECT value
96e982
	FROM  REMOTE.parameters_policies
96e982
	INNER JOIN REMOTE.parameters
96e982
	ON REMOTE.parameters_policies.parameter_id = REMOTE.parameters.id
96e982
	WHERE REMOTE.parameters_policies.policy_id = policyKey.policyId
96e982
		AND REMOTE.parameters.category_id = 4
96e982
		AND REMOTE.parameters.name = 'lifetime')
96e982
WHERE policyKey.role = 2;
96e982
96e982
UPDATE policyKey
96e982
SET repository = (
96e982
	SELECT REMOTE.securitymodules.name
96e982
	FROM  REMOTE.parameters_policies
96e982
	INNER JOIN REMOTE.parameters
96e982
	ON REMOTE.parameters_policies.parameter_id = REMOTE.parameters.id
96e982
	INNER JOIN REMOTE.securitymodules
96e982
	ON REMOTE.parameters_policies.value = REMOTE.securitymodules.id
96e982
	WHERE REMOTE.parameters_policies.policy_id = policyKey.policyId
96e982
		AND REMOTE.parameters.category_id = 3
96e982
		AND REMOTE.parameters.name = 'repository')
96e982
WHERE policyKey.role = 1;
96e982
96e982
UPDATE policyKey
96e982
SET repository = (
96e982
	SELECT REMOTE.securitymodules.name
96e982
	FROM  REMOTE.parameters_policies
96e982
	INNER JOIN REMOTE.parameters
96e982
	ON REMOTE.parameters_policies.parameter_id = REMOTE.parameters.id
96e982
	INNER JOIN REMOTE.securitymodules
96e982
	ON REMOTE.parameters_policies.value = REMOTE.securitymodules.id
96e982
	WHERE REMOTE.parameters_policies.policy_id = policyKey.policyId
96e982
		AND REMOTE.parameters.category_id = 4
96e982
		AND REMOTE.parameters.name = 'repository')
96e982
WHERE policyKey.role = 2;
96e982
96e982
UPDATE policyKey
96e982
SET standby = (
96e982
	SELECT value
96e982
	FROM  REMOTE.parameters_policies
96e982
	INNER JOIN REMOTE.parameters
96e982
	ON REMOTE.parameters_policies.parameter_id = REMOTE.parameters.id
96e982
	WHERE REMOTE.parameters_policies.policy_id = policyKey.policyId
96e982
		AND REMOTE.parameters.category_id = 3
96e982
		AND REMOTE.parameters.name = 'standby')
96e982
WHERE policyKey.role = 1;
96e982
96e982
UPDATE policyKey
96e982
SET standby = (
96e982
	SELECT value
96e982
	FROM  REMOTE.parameters_policies
96e982
	INNER JOIN REMOTE.parameters
96e982
	ON REMOTE.parameters_policies.parameter_id = REMOTE.parameters.id
96e982
	WHERE REMOTE.parameters_policies.policy_id = policyKey.policyId
96e982
		AND REMOTE.parameters.category_id = 4
96e982
		AND REMOTE.parameters.name = 'standby')
96e982
WHERE policyKey.role = 2;
96e982
96e982
UPDATE policyKey
96e982
SET manualRollover = (
96e982
	SELECT value
96e982
	FROM  REMOTE.parameters_policies
96e982
	INNER JOIN REMOTE.parameters
96e982
	ON REMOTE.parameters_policies.parameter_id = REMOTE.parameters.id
96e982
	WHERE REMOTE.parameters_policies.policy_id = policyKey.policyId
96e982
		AND REMOTE.parameters.category_id = 3
96e982
		AND REMOTE.parameters.name = 'manual_rollover')
96e982
WHERE policyKey.role = 1;
96e982
96e982
UPDATE policyKey
96e982
SET manualRollover = (
96e982
	SELECT value
96e982
	FROM  REMOTE.parameters_policies
96e982
	INNER JOIN REMOTE.parameters
96e982
	ON REMOTE.parameters_policies.parameter_id = REMOTE.parameters.id
96e982
	WHERE REMOTE.parameters_policies.policy_id = policyKey.policyId
96e982
		AND REMOTE.parameters.category_id = 4
96e982
		AND REMOTE.parameters.name = 'manual_rollover')
96e982
WHERE policyKey.role = 2;
96e982
96e982
-- rfc5011 = 0. 2.0 has no support
96e982
-- minimize already set
96e982
96e982
-- ~ ************
96e982
-- ~ ** hsmKey table
96e982
-- ~ **
96e982
-- ~ ** get from keypairs and dnsseckeys
96e982
-- ~ **
96e982
-- ~ **
96e982
-- ~ ************
96e982
96e982
INSERT INTO hsmKey
96e982
SELECT DISTINCT REMOTE.keypairs.id, 1, REMOTE.keypairs.policy_id,
96e982
REMOTE.keypairs.HSMkey_id, 2, REMOTE.keypairs.size, 
96e982
REMOTE.keypairs.algorithm,  (~(REMOTE.dnsseckeys.keytype)&1)+1, 
96e982
CASE WHEN REMOTE.keypairs.generate IS NOT NULL THEN 
96e982
	strftime('%s', REMOTE.keypairs.generate) 
96e982
	ELSE strftime("%s", "now") END, 
96e982
0, 
96e982
1, --only RSA supported
96e982
 REMOTE.securitymodules.name, 
96e982
0 --assume no backup 
96e982
FROM REMOTE.keypairs
96e982
JOIN REMOTE.dnsseckeys 
96e982
	ON REMOTE.keypairs.id = REMOTE.dnsseckeys.keypair_id
96e982
JOIN REMOTE.securitymodules 
96e982
	ON REMOTE.securitymodules.id = REMOTE.keypairs.securitymodule_id;
96e982
96e982
-- For some policies put the keys in a shared state
96e982
UPDATE hsmKey 
96e982
SET state = 3
96e982
WHERE EXISTS 
96e982
	(SELECT * FROM hsmKey AS h 
96e982
	JOIN policy ON policy.id = h.policyId 
96e982
	WHERE policy.keysShared AND hsmKey.id = h.id);
96e982
96e982
-- ~ ************
96e982
-- ~ ** zone table
96e982
-- ~ **
96e982
-- ~ ** 
96e982
-- ~ **
96e982
-- ~ **
96e982
-- ~ ************
96e982
96e982
INSERT INTO zone
96e982
SELECT zones.id, 1, zones.policy_id, 
96e982
	zones.name, 1, zones.signconf, 0,  
96e982
	0,0,0, 
96e982
	0,0,0,
96e982
	zones.in_type, zones.input,
96e982
	zones.out_type, zones.output,
96e982
	0,0,0
96e982
	FROM REMOTE.zones;
96e982
96e982
-- ~ ************
96e982
-- ~ ** keyData table
96e982
-- ~ **
96e982
-- ~ ** 
96e982
-- ~ **
96e982
-- ~ **
96e982
-- ~ ************
96e982
96e982
-- Temporary mapping table between 1.4 states and 2.0 ds_at_parent states
96e982
-- We are ignoring the fact this may set a DS state for a ZSK; We don't care
96e982
CREATE TABLE mapping (
96e982
	state INTEGER,
96e982
	ds_state INTEGER
96e982
);
96e982
INSERT INTO mapping SELECT  1, 0;
96e982
INSERT INTO mapping SELECT  2, 0;
96e982
INSERT INTO mapping SELECT  3, 1;
96e982
INSERT INTO mapping SELECT  4, 3;
96e982
INSERT INTO mapping SELECT  5, 5;
96e982
INSERT INTO mapping SELECT  6, 5;
96e982
INSERT INTO mapping SELECT  7, 5;
96e982
INSERT INTO mapping SELECT  8, 5;
96e982
INSERT INTO mapping SELECT  9, 5;
96e982
INSERT INTO mapping SELECT 10, 5;
96e982
96e982
INSERT INTO keyData
96e982
SELECT 
96e982
	NULL, 1, REMOTE.dnsseckeys.zone_id,
96e982
	REMOTE.dnsseckeys.keypair_id, REMOTE.keypairs.algorithm,
96e982
	CASE WHEN REMOTE.dnsseckeys.publish IS NOT NULL THEN 
96e982
		strftime('%s', REMOTE.dnsseckeys.publish) 
96e982
		ELSE strftime("%s", "now") END, 
96e982
	(~REMOTE.dnsseckeys.keytype&1)+1,
96e982
	REMOTE.dnsseckeys.state <= 4, -- introducing
96e982
	0, -- should revoke, not used
96e982
	0, -- standby
96e982
	REMOTE.dnsseckeys.state  = 4 AND REMOTE.dnsseckeys.keytype = 256, --activeZSK: 
96e982
	REMOTE.dnsseckeys.state >= 2 AND REMOTE.dnsseckeys.state <= 5, --publish
96e982
	REMOTE.dnsseckeys.state  = 4 AND REMOTE.dnsseckeys.keytype = 257, --activeKSK: 
96e982
	mapping.ds_state, --dsatparent
96e982
	1<<16, --keytag (crap, will 2.0 regenerate this?)
96e982
	(REMOTE.dnsseckeys.keytype&1)*3+1 --minimize
96e982
FROM REMOTE.dnsseckeys
96e982
JOIN REMOTE.keypairs 
96e982
	ON REMOTE.dnsseckeys.keypair_id = REMOTE.keypairs.id
96e982
JOIN mapping 
96e982
	ON REMOTE.dnsseckeys.state = mapping.state
96e982
WHERE EXISTS(select REMOTE.zones.id FROM REMOTE.zones WHERE REMOTE.zones.id = REMOTE.dnsseckeys.zone_id);
96e982
96e982
-- Everything that is just a ZSK must not have dsatparent set.
96e982
UPDATE keyData
96e982
SET dsatparent = 0
96e982
WHERE role = 2;
96e982
96e982
DROP TABLE mapping;
96e982
96e982
-- If a active time is set for a ready KSK dsAtParent is submitted 
96e982
-- instead of submit
96e982
UPDATE keyData
96e982
SET dsatparent = 2
96e982
WHERE keyData.dsAtParent = 1 AND keyData.id IN (
96e982
	SELECT keyData.id
96e982
	FROM keyData
96e982
	JOIN REMOTE.dnsseckeys
96e982
	ON REMOTE.dnsseckeys.keypair_id = keyData.hsmkeyid
96e982
	WHERE REMOTE.dnsseckeys.active IS NOT NULL);
96e982
96e982
96e982
-- ~ ************
96e982
-- ~ ** Keystate table
96e982
-- ~ **
96e982
-- ~ ** 
96e982
-- ~ **
96e982
-- ~ **
96e982
-- ~ ************
96e982
96e982
CREATE TABLE mapping (
96e982
	state INTEGER,
96e982
	ds INTEGER,
96e982
	dk INTEGER,
96e982
	ks INTEGER,
96e982
	rs INTEGER
96e982
);
96e982
INSERT INTO mapping SELECT  1, 0, 0, 0, 0;
96e982
INSERT INTO mapping SELECT  2, 0, 1, 1, 1;
96e982
INSERT INTO mapping SELECT  3, 0, 2, 2, 1;
96e982
INSERT INTO mapping SELECT  4, 2, 2, 2, 1;
96e982
INSERT INTO mapping SELECT  5, 3, 2, 2, 3;
96e982
INSERT INTO mapping SELECT  6, 0, 3, 3, 0;
96e982
INSERT INTO mapping SELECT  7, 3, 0, 0, 0;
96e982
INSERT INTO mapping SELECT  8, 3, 0, 0, 0;
96e982
INSERT INTO mapping SELECT  9, 3, 0, 0, 0;
96e982
INSERT INTO mapping SELECT 10, 3, 0, 0, 0;
96e982
96e982
-- DS RECORDS
96e982
INSERT INTO keyState
96e982
SELECT NULL, 1, keyData.id, 0, mapping.ds, strftime("%s", "now"), (keyData.minimize>>2)&1, policy.parentDsTtl
96e982
FROM keyData
96e982
JOIN zone
96e982
	ON zone.id = keyData.zoneId
96e982
JOIN policy
96e982
	ON policy.id = zone.policyId
96e982
JOIN REMOTE.dnsseckeys
96e982
	ON REMOTE.dnsseckeys.keypair_id = keyData.hsmkeyid
96e982
JOIN mapping
96e982
	ON mapping.state = REMOTE.dnsseckeys.state;
96e982
96e982
UPDATE keyState
96e982
SET state = 1
96e982
WHERE keyState.state = 0 AND keyState.type = 0 AND keyState.id IN (
96e982
	SELECT keyState.id
96e982
	FROM keyState
96e982
	JOIN keyData
96e982
		ON keyData.id = keyState.keydataId
96e982
	JOIN REMOTE.dnsseckeys
96e982
		ON REMOTE.dnsseckeys.keypair_id = keyData.hsmkeyid
96e982
	WHERE REMOTE.dnsseckeys.active IS NOT NULL);
96e982
96e982
-- DNSKEY RECORDS
96e982
INSERT INTO keyState
96e982
SELECT NULL, 1, keyData.id, 2, mapping.dk, strftime("%s", "now"), (keyData.minimize>>1)&1, policy.keysTtl
96e982
FROM keyData
96e982
JOIN zone
96e982
	ON zone.id = keyData.zoneId
96e982
JOIN policy
96e982
	ON policy.id = zone.policyId
96e982
JOIN REMOTE.dnsseckeys
96e982
	ON REMOTE.dnsseckeys.keypair_id = keyData.hsmkeyid
96e982
JOIN mapping
96e982
	ON mapping.state = REMOTE.dnsseckeys.state;
96e982
96e982
-- RRSIG DNSKEY RECORDS
96e982
INSERT INTO keyState
96e982
SELECT NULL, 1, keyData.id, 3, mapping.ks, strftime("%s", "now"), (keyData.minimize>>1)&1, policy.keysTtl
96e982
FROM keyData
96e982
JOIN zone
96e982
	ON zone.id = keyData.zoneId
96e982
JOIN policy
96e982
	ON policy.id = zone.policyId
96e982
JOIN REMOTE.dnsseckeys
96e982
	ON REMOTE.dnsseckeys.keypair_id = keyData.hsmkeyid
96e982
JOIN mapping
96e982
	ON mapping.state = REMOTE.dnsseckeys.state;
96e982
96e982
-- RRSIG RECORDS
96e982
INSERT INTO keyState
96e982
SELECT NULL, 1, keyData.id, 1, mapping.rs, strftime("%s", "now"), (keyData.minimize>>0)&1, policy.signaturesMaxZoneTtl
96e982
FROM keyData
96e982
JOIN zone
96e982
	ON zone.id = keyData.zoneId
96e982
JOIN policy
96e982
	ON policy.id = zone.policyId
96e982
JOIN REMOTE.dnsseckeys
96e982
	ON REMOTE.dnsseckeys.keypair_id = keyData.hsmkeyid
96e982
JOIN mapping
96e982
	ON mapping.state = REMOTE.dnsseckeys.state;
96e982
96e982
--Set to OMN if Tactive + Dttl < Tnow
96e982
UPDATE keyState
96e982
SET state = 2
96e982
WHERE keyState.state = 1 AND keyState.type = 1 AND keyState.id IN (
96e982
        SELECT keyState.id
96e982
        FROM keyState
96e982
        JOIN keyData
96e982
                ON keyData.id = keyState.keydataId
96e982
        JOIN REMOTE.dnsseckeys
96e982
                ON REMOTE.dnsseckeys.keypair_id = keyData.hsmkeyid
96e982
        JOIN zone
96e982
                ON keyData.zoneId = zone.id
96e982
        JOIN policy
96e982
                ON policy.id = zone.policyId
96e982
        WHERE CAST(strftime("%s", REMOTE.dnsseckeys.active) + policy.signaturesValidityDefault as INTEGER) < strftime("%s", "now"));
96e982
96e982
--Force the RRSIG state in omnipresent if rumoured and there is no old ZSK
96e982
-- unretentive
96e982
UPDATE keyState 
96e982
SET state = 2
96e982
WHERE keyState.id IN (
96e982
SELECT rs.id FROM keyState AS rs 
96e982
JOIN keystate AS dk ON dk.keyDataId == rs.keyDataId
96e982
WHERE rs.type == 1 AND dk.type == 2 AND rs.state == 1 AND dk.state == 2
96e982
AND NOT EXISTS(
96e982
	SELECT* FROM keystate AS rs2
96e982
	JOIN keystate AS dk2 ON dk2.keyDataId == rs2.keyDataId
96e982
	WHERE rs2.type == 1 AND dk2.type == 2 AND rs2.state == 3 AND dk2.state == 2
96e982
));
96e982
96e982
DROP TABLE mapping;
96e982
96e982
-- We need to create records in the keydependency table in case we are in a
96e982
-- rollover. Only done for ZSK. For every introducing ZSK with RRSIG rumoured
96e982
-- that has an outroducing ZSK with RRSIG unretentive, we add a record.
96e982
INSERT INTO keyDependency
96e982
SELECT NULL, 0, keyData.zoneID, SUB.IDout, keyData.id, 1
96e982
FROM keyData
96e982
JOIN keyState AS KS1 
96e982
	ON KS1.keyDataId == keyData.id
96e982
JOIN keyState AS KS2 
96e982
	ON KS2.keyDataId == keyData.id
96e982
JOIN (
96e982
	SELECT keyData.id AS IDout, keyData.zoneID 
96e982
	FROM keyData
96e982
	JOIN keyState AS KS1 
96e982
		ON KS1.keyDataId == keyData.id
96e982
	JOIN keyState AS KS2 
96e982
		ON KS2.keyDataId == keyData.id
96e982
	WHERE KS1.type == 2 
96e982
		AND ks1.state = 2 
96e982
		AND KS2.type == 1 
96e982
		AND KS2.state == 3
96e982
		AND keyData.introducing == 0 
96e982
		AND keyData.role == 2
96e982
) AS SUB
96e982
	ON SUB.zoneId == keyData.zoneId
96e982
WHERE 
96e982
	KS1.type == 2 
96e982
	AND ks1.state = 2 
96e982
	AND KS2.type == 1 
96e982
	AND KS2.state == 1
96e982
	AND keyData.introducing == 1 
96e982
	AND keyData.role == 2;
96e982
96e982
-- ZSK
96e982
UPDATE keyState
96e982
SET state = 4
96e982
WHERE (keyState.type = 0 OR keyState.type = 3) AND keyDataId IN (
96e982
	SELECT keyData.id
96e982
	FROM keyData
96e982
	WHERE keyData.role = 2);
96e982
96e982
--KSK
96e982
UPDATE keyState
96e982
SET state = 4
96e982
WHERE keyState.type = 1 AND keyDataId IN (
96e982
	SELECT keyData.id
96e982
	FROM keyData
96e982
	WHERE keyData.role = 1);
96e982
96e982
-- For rpm based systems to see if db was migrated already. store opendnssec major minor version
96e982
CREATE TABLE rpm_migration (
96e982
        major INTEGER,
96e982
        minor INTEGER
96e982
);
96e982
INSERT INTO rpm_migration VALUES(2, 1);
96e982