/*
  Setup script for server 2 database 2 for SSB sample communicating
  between two remote servers

  Run this script to set up a new database and dialog security on the db

  Any questions etc.: nielsb@develop.com

*/

use master;
go

--create the database
create database [rem_ssb2];
go

use [rem_ssb2];
go

--set master key
create master key
encryption by password = 'hellodb2';
go

--create certificate for transport security
--Here you create a self signed cert. Make sure you set the start date
--to a little while before now(), otherwise you may run into
--troubles due to UTC dates.
create certificate DlgCertServ2Db2
with subject = 'DlgCertServ2Db2',
start_date = '06/01/2005'
active for begin_dialog = on;
go

--make sure the cert exist
select * from sys.certificates;
go

--dump out the public key of the cert to a file
--this will then be exchanged with the other instance
--make sure that the path you define below can be accessed
--by sql server. The file needs to be copied over to server 2
BACKUP CERTIFICATE DlgCertServ2Db2 
TO FILE = 'C:\code\ServiceBrokerExamples\certs\DlgCertServ2Db2Pub.cer';
go

--create a user which you eventually will assign a public
--key from the cert in the remote db to
create user remdlguser2
without login;
go

--copy in the public cert from server 1 to somewhere on this server

--create a certificate from the public cert from server 1
create certificate DlgCertServ1Db1Pub
authorization remdlguser2
from file = 'C:\code\ServiceBrokerExamples\certs\DlgCertServ1Db1Pub.cer';

--go back to server 1 and step 2 in the script 2_setup_objects_server1_db.sql
--make sure the public cert 'DlgCertServ2Db2Pub.cer' is available
--from server 1 


