2012-05-31 18:53:42 +02:00
|
|
|
/*
|
|
|
|
* PROGRAM: Firebird samples.
|
|
|
|
* MODULE: CryptApplication.cpp
|
|
|
|
* DESCRIPTION: Sample of passing a key to crypt plugin
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Initial
|
|
|
|
* Developer's Public License Version 1.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
|
|
|
* http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed AS IS,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing rights
|
|
|
|
* and limitations under the License.
|
|
|
|
*
|
|
|
|
* The Original Code was created by Alex Peshkov
|
|
|
|
* for the Firebird Open Source RDBMS project.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2012 Alex Peshkov <peshkoff at mail.ru>
|
|
|
|
* and all contributors signed below.
|
|
|
|
*
|
|
|
|
* All Rights Reserved.
|
|
|
|
* Contributor(s): ______________________________________.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2014-10-16 12:36:23 +02:00
|
|
|
#include <stdint.h>
|
2012-05-31 18:53:42 +02:00
|
|
|
|
|
|
|
#include <ibase.h>
|
2014-09-29 13:03:47 +02:00
|
|
|
#include <firebird/Interface.h>
|
2012-05-31 18:53:42 +02:00
|
|
|
|
|
|
|
using namespace Firebird;
|
|
|
|
|
2014-12-19 16:24:44 +01:00
|
|
|
class CryptKey : public Api::ICryptKeyCallbackImpl<CryptKey>
|
2012-05-31 18:53:42 +02:00
|
|
|
{
|
|
|
|
public:
|
2014-09-29 13:03:47 +02:00
|
|
|
unsigned int callback(unsigned int, const void*, unsigned int length, void* buffer)
|
2012-05-31 18:53:42 +02:00
|
|
|
{
|
|
|
|
if (length > 0 && buffer)
|
|
|
|
{
|
|
|
|
char k = 0x5a;
|
|
|
|
memcpy(buffer, &k, 1);
|
|
|
|
fprintf(stderr, "\nTransfered key to server\n");
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-09-29 13:03:47 +02:00
|
|
|
IPluginModule* getModule()
|
2012-05-31 18:53:42 +02:00
|
|
|
{
|
|
|
|
return NULL; // OK for application, not for plugin
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class App
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
App() :
|
|
|
|
master(fb_get_master_interface()), status(master->getStatus()),
|
|
|
|
p(NULL), att(NULL), tra(NULL)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
~App()
|
|
|
|
{
|
|
|
|
if (tra)
|
|
|
|
{
|
|
|
|
tra->rollback(status);
|
2015-02-18 00:21:25 +01:00
|
|
|
if (status->getStatus() & IStatus::FB_HAS_ERRORS)
|
2012-05-31 18:53:42 +02:00
|
|
|
{
|
|
|
|
print("rollback");
|
|
|
|
tra->release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (att)
|
|
|
|
{
|
|
|
|
att->detach(status);
|
2015-02-18 00:21:25 +01:00
|
|
|
if (status->getStatus() & IStatus::FB_HAS_ERRORS)
|
2012-05-31 18:53:42 +02:00
|
|
|
{
|
|
|
|
print("detach");
|
|
|
|
att->release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (p)
|
|
|
|
{
|
|
|
|
p->release();
|
|
|
|
}
|
|
|
|
status->dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
enum Action {NONE, ENC, DEC};
|
|
|
|
|
2012-06-03 05:00:24 +02:00
|
|
|
void execute(const char* dbName, const Action a)
|
2012-05-31 18:53:42 +02:00
|
|
|
{
|
|
|
|
status->init();
|
|
|
|
|
|
|
|
p = master->getDispatcher();
|
|
|
|
|
|
|
|
p->setDbCryptCallback(status, &key);
|
2015-02-18 00:21:25 +01:00
|
|
|
if (status->getStatus() & IStatus::FB_HAS_ERRORS)
|
2012-05-31 18:53:42 +02:00
|
|
|
throw "setDbCryptCallback";
|
|
|
|
|
|
|
|
char s[256];
|
|
|
|
sprintf(s, "localhost:%s", dbName);
|
|
|
|
att = p->attachDatabase(status, s, 0, NULL);
|
2015-02-18 00:21:25 +01:00
|
|
|
if (status->getStatus() & IStatus::FB_HAS_ERRORS)
|
2012-05-31 18:53:42 +02:00
|
|
|
throw "attachDatabase";
|
|
|
|
|
|
|
|
if (a != NONE)
|
|
|
|
{
|
|
|
|
tra = att->startTransaction(status, 0, NULL);
|
2015-02-18 00:21:25 +01:00
|
|
|
if (status->getStatus() & IStatus::FB_HAS_ERRORS)
|
2012-05-31 18:53:42 +02:00
|
|
|
throw "startTransaction";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (a == ENC)
|
|
|
|
{
|
|
|
|
att->execute(status, tra, 0,
|
2013-09-09 11:14:38 +02:00
|
|
|
"ALTER DATABASE ENCRYPT WITH \"DbCrypt_example\"", 3, NULL, NULL, NULL, NULL);
|
2015-02-18 00:21:25 +01:00
|
|
|
if (status->getStatus() & IStatus::FB_HAS_ERRORS)
|
2012-05-31 18:53:42 +02:00
|
|
|
throw "execute";
|
|
|
|
}
|
|
|
|
if (a == DEC)
|
|
|
|
{
|
2013-09-09 11:14:38 +02:00
|
|
|
att->execute(status, tra, 0, "ALTER DATABASE DECRYPT", 3, NULL, NULL, NULL, NULL);
|
2015-02-18 00:21:25 +01:00
|
|
|
if (status->getStatus() & IStatus::FB_HAS_ERRORS)
|
2012-05-31 18:53:42 +02:00
|
|
|
throw "execute";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tra)
|
|
|
|
{
|
|
|
|
tra->commit(status);
|
2015-02-18 00:21:25 +01:00
|
|
|
if (status->getStatus() & IStatus::FB_HAS_ERRORS)
|
2012-05-31 18:53:42 +02:00
|
|
|
throw "commit";
|
|
|
|
tra = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Providing key for crypt plugin - press enter to continue ...");
|
|
|
|
getchar();
|
|
|
|
|
|
|
|
att->detach(status);
|
2015-02-18 00:21:25 +01:00
|
|
|
if (status->getStatus() & IStatus::FB_HAS_ERRORS)
|
2012-05-31 18:53:42 +02:00
|
|
|
throw "detach";
|
|
|
|
att = NULL;
|
|
|
|
|
|
|
|
p->release();
|
|
|
|
p = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void print(const char* where)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Error in %s: ", where);
|
2014-09-29 13:03:47 +02:00
|
|
|
isc_print_status(status->getErrors());
|
2012-05-31 18:53:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
IMaster* master;
|
|
|
|
IStatus* status;
|
|
|
|
IProvider* p;
|
|
|
|
IAttachment* att;
|
|
|
|
ITransaction* tra;
|
|
|
|
|
|
|
|
CryptKey key;
|
|
|
|
};
|
|
|
|
|
|
|
|
int usage()
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Usage: CryptApplication [ -e | -d ] { db-name }\n");
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int ac, char** av)
|
|
|
|
{
|
|
|
|
App::Action act = App::NONE;
|
|
|
|
|
|
|
|
if (ac < 2 || ac > 3)
|
|
|
|
return usage();
|
2012-06-03 05:00:24 +02:00
|
|
|
|
2012-05-31 18:53:42 +02:00
|
|
|
if (ac == 3)
|
|
|
|
{
|
|
|
|
if (av[1][0] != '-')
|
|
|
|
return usage();
|
2012-06-03 05:00:24 +02:00
|
|
|
|
2012-05-31 18:53:42 +02:00
|
|
|
switch(av[1][1])
|
|
|
|
{
|
|
|
|
case 'e':
|
|
|
|
act = App::ENC;
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
act = App::DEC;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return usage();
|
|
|
|
}
|
|
|
|
av++;
|
|
|
|
}
|
|
|
|
|
|
|
|
setenv("ISC_USER", "sysdba", 0);
|
|
|
|
setenv("ISC_PASSWORD", "masterkey", 0);
|
|
|
|
|
|
|
|
App app;
|
|
|
|
try
|
|
|
|
{
|
2012-06-03 05:00:24 +02:00
|
|
|
app.execute(av[1], act);
|
2012-05-31 18:53:42 +02:00
|
|
|
}
|
2012-06-03 05:00:24 +02:00
|
|
|
catch (const char* where)
|
2012-05-31 18:53:42 +02:00
|
|
|
{
|
|
|
|
app.print(where);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|