File: CryptApplication.cpp

package info (click to toggle)
firebird3.0 3.0.13.ds7-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 42,632 kB
  • sloc: ansic: 374,403; cpp: 319,973; sql: 14,691; pascal: 14,532; yacc: 7,557; fortran: 5,645; sh: 5,336; makefile: 1,041; perl: 194; sed: 83; awk: 76; xml: 19; csh: 15
file content (256 lines) | stat: -rw-r--r-- 5,825 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
 *	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 "../interfaces/ifaceExamples.h"
#include <firebird/Message.h>

using namespace Firebird;

class CryptKey : public ICryptKeyCallbackImpl<CryptKey, CheckStatusWrapper>
{
public:
	unsigned int callback(unsigned int, const void*, unsigned int length, void* buffer)
	{
		if (length > 0 && buffer)
		{
			char k = 0x5a;
			memcpy(buffer, &k, 1);
			fprintf(stderr, "\nTransfered key to server\n");
		}
		return 1;
	}
};

class App
{
public:
	App() :
		master(fb_get_master_interface()),
		statusWrapper(master->getStatus()), status(&statusWrapper),
		p(NULL), att(NULL), tra(NULL)
	{ }

	~App()
	{
		if (tra)
		{
			tra->rollback(status);
			if (status->getState() & IStatus::STATE_ERRORS)
			{
				print("rollback");
				tra->release();
			}
		}
		if (att)
		{
			att->detach(status);
			if (status->getState() & IStatus::STATE_ERRORS)
			{
				print("detach");
				att->release();
			}
		}
		if (p)
		{
			p->release();
		}
		status->dispose();
	}

	enum Action {NONE, ENC, DEC, EX_LCL, EX_RMT};
	// Switches/actions have the following meanings:
	// ENC(-e) - encrypt database
	// DEC(-d) - decrypt database
	// EX_LCL(-l) - execute some predefined select command (demonstrates that database can respond to select request)
	// EX_RMT(-r) - execute select using execute statement in remote datasource (demonstrates that dbcrypt key is
	//				passed to target database when using execute statement)

	void execute(const char* dbName, const Action a)
	{
		status->init();

		p = master->getDispatcher();

		p->setDbCryptCallback(status, &key);
		if (status->getState() & IStatus::STATE_ERRORS)
			throw "setDbCryptCallback";

		char s[256];
		sprintf(s, "localhost:%s", dbName);
		att = p->attachDatabase(status, s, 0, NULL);
		if (status->getState() & IStatus::STATE_ERRORS)
			throw "attachDatabase";

		if (a != NONE)
		{
			tra = att->startTransaction(status, 0, NULL);
			if (status->getState() & IStatus::STATE_ERRORS)
				throw "startTransaction";
		}

		switch(a)
		{
		case ENC:
			att->execute(status, tra, 0,
				"ALTER DATABASE ENCRYPT WITH \"DbCrypt_example\"", 3, NULL, NULL, NULL, NULL);
			if (status->getState() & IStatus::STATE_ERRORS)
				throw "execute";
			break;

		case DEC:
			att->execute(status, tra, 0, "ALTER DATABASE DECRYPT", 3, NULL, NULL, NULL, NULL);
			if (status->getState() & IStatus::STATE_ERRORS)
				throw "execute";
			break;

		case EX_LCL:
		case EX_RMT:
		  {
			FB_MESSAGE(Output, CheckStatusWrapper,
				(FB_VARCHAR(31), logon)
			) output(status, master);

			const char* sqlL = "select current_user from rdb$database";
			const char* sqlR = "execute block returns(logon varchar(31)) as begin "
				"execute statement 'select current_user from rdb$database' "
				"on external 'localhost:employee' as user 'test' password 'test' into :logon; "
				"suspend; end";
			const char* sql = a == EX_LCL ? sqlL : sqlR;

			curs = att->openCursor(status, tra, 0, sql, 3, NULL, NULL, output.getMetadata(), NULL, 0);
			if (status->getState() & IStatus::STATE_ERRORS)
				throw "openCursor";

			printf("\nExec SQL: %s\nReturns:\n", sql);
			while (curs->fetchNext(status, output.getData()) == IStatus::RESULT_OK)
			{
				unsigned l = output->logonNull ? 0 : output->logon.length;
				printf("%*.*s\n", l, l, output->logon.str);
			}
			printf("done.\n");
			if (status->getState() & IStatus::STATE_ERRORS)
				throw "fetchNext";

			curs->close(status);
			if (status->getState() & IStatus::STATE_ERRORS)
				throw "close";
			curs = NULL;
			break;
		  }
		}

		if (tra)
		{
			tra->commit(status);
			if (status->getState() & IStatus::STATE_ERRORS)
				throw "commit";
			tra = NULL;
		}

		printf("\nProviding key for crypt plugin - press enter to continue ...");
		getchar();

		att->detach(status);
		if (status->getState() & IStatus::STATE_ERRORS)
			throw "detach";
		att = NULL;

		p->release();
		p = NULL;
	}

	void print(const char* where)
	{
		fprintf(stderr, "Error in %s: ", where);
		isc_print_status(status->getErrors());
	}

private:
	IMaster* master;
	CheckStatusWrapper statusWrapper;
	CheckStatusWrapper* status;
	IProvider* p;
	IAttachment* att;
	ITransaction* tra;
	IResultSet* curs;

	CryptKey key;
};

int usage()
{
	fprintf(stderr, "Usage: cryptAppSample [ -e | -d | -l | -r ] { db-name }\n");
	return 2;
}

int main(int ac, char** av)
{
	App::Action act = App::NONE;

	if (ac < 2 || ac > 3)
		return usage();

	if (ac == 3)
	{
		if (av[1][0] != '-')
			return usage();

		switch(av[1][1])
		{
		case 'e':
			act = App::ENC;
			break;
		case 'd':
			act = App::DEC;
			break;
		case 'l':
			act = App::EX_LCL;
			break;
		case 'r':
			act = App::EX_RMT;
			break;
		default:
			return usage();
		}
		av++;
	}

	setenv("ISC_USER", "sysdba", 0);
	setenv("ISC_PASSWORD", "masterkey", 0);

	App app;
	try
	{
		app.execute(av[1], act);
	}
	catch (const char* where)
	{
		app.print(where);
		return 1;
	}

	return 0;
}