File: keydb_keyd.c

package info (click to toggle)
onak 0.4.5-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,744 kB
  • ctags: 777
  • sloc: ansic: 11,570; perl: 268; sh: 268; makefile: 169; sql: 21
file content (597 lines) | stat: -rw-r--r-- 15,988 bytes parent folder | download
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
/*
 * keydb_keyd.c - Routines to talk to keyd backend.
 *
 * Copyright 2002-2004,2011 Jonathan McDowell <noodles@earth.li>
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 51
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <unistd.h>

#include "charfuncs.h"
#include "keyd.h"
#include "keydb.h"
#include "keyid.h"
#include "keystructs.h"
#include "log.h"
#include "mem.h"
#include "onak-conf.h"
#include "parsekey.h"

/**
 *	starttrans - Start a transaction.
 *
 *	Start a transaction. Intended to be used if we're about to perform many
 *	operations on the database to help speed it all up, or if we want
 *	something to only succeed if all relevant operations are successful.
 */
static bool keyd_starttrans(struct onak_dbctx *dbctx)
{
	return true;
}

/**
 *	endtrans - End a transaction.
 *
 *	Ends a transaction.
 */
static void keyd_endtrans(struct onak_dbctx *dbctx)
{
	return;
}

static bool keyd_send_cmd(int fd, enum keyd_ops _cmd)
{
	uint32_t cmd = _cmd;
	ssize_t bytes;

	bytes = write(fd, &cmd, sizeof(cmd));
	if (bytes != sizeof(cmd)) {
		return false;
	}

	bytes = read(fd, &cmd, sizeof(cmd));
	if (bytes != sizeof(cmd)) {
		return false;
	}

	if (cmd != KEYD_REPLY_OK) {
		return false;
	}

	return true;
}

/**
 *	fetch_key - Given a keyid fetch the key from storage.
 *	@keyid: The keyid to fetch.
 *	@publickey: A pointer to a structure to return the key in.
 *	@intrans: If we're already in a transaction.
 *
 *	This function returns a public key from whatever storage mechanism we
 *	are using.
 *
 *      TODO: What about keyid collisions? Should we use fingerprint instead?
 */
static int keyd_fetch_key_id(struct onak_dbctx *dbctx,
		uint64_t keyid,
		struct openpgp_publickey **publickey,
		bool intrans)
{
	int keyd_fd = (intptr_t) dbctx->priv;
	struct buffer_ctx           keybuf;
	struct openpgp_packet_list *packets = NULL;
	ssize_t                     bytes = 0;
	ssize_t                     count = 0;

	if (keyd_send_cmd(keyd_fd, KEYD_CMD_GET_ID)) {
		write(keyd_fd, &keyid, sizeof(keyid));
		keybuf.offset = 0;
		read(keyd_fd, &keybuf.size, sizeof(keybuf.size));
		if (keybuf.size > 0) {
			keybuf.buffer = malloc(keybuf.size);
			bytes = count = 0;
			logthing(LOGTHING_TRACE,
					"Getting %d bytes of key data.",
					keybuf.size);
			while (bytes >= 0 && count < keybuf.size) {
				bytes = read(keyd_fd, &keybuf.buffer[count],
						keybuf.size - count);
				logthing(LOGTHING_TRACE,
						"Read %d bytes.", bytes);
				count += bytes;
			}
			read_openpgp_stream(buffer_fetchchar, &keybuf,
					&packets, 0);
			parse_keys(packets, publickey);
			free_packet_list(packets);
			packets = NULL;
			free(keybuf.buffer);
			keybuf.buffer = NULL;
			keybuf.size = 0;
		}
	}

	return (count > 0) ? 1 : 0;
}

static int keyd_fetch_key_fp(struct onak_dbctx *dbctx,
		struct openpgp_fingerprint *fingerprint,
		struct openpgp_publickey **publickey,
		bool intrans)
{
	int keyd_fd = (intptr_t) dbctx->priv;
	struct buffer_ctx           keybuf;
	struct openpgp_packet_list *packets = NULL;
	ssize_t                     bytes = 0;
	ssize_t                     count = 0;
	uint8_t                     size;

	if (fingerprint->length > MAX_FINGERPRINT_LEN) {
		return 0;
	}

	if (keyd_send_cmd(keyd_fd, KEYD_CMD_GET_FP)) {
		size = fingerprint->length;
		write(keyd_fd, &size, sizeof(size));
		write(keyd_fd, fingerprint->fp, size);
		keybuf.offset = 0;
		read(keyd_fd, &keybuf.size, sizeof(keybuf.size));
		if (keybuf.size > 0) {
			keybuf.buffer = malloc(keybuf.size);
			bytes = count = 0;
			logthing(LOGTHING_TRACE,
					"Getting %d bytes of key data.",
					keybuf.size);
			while (bytes >= 0 && count < keybuf.size) {
				bytes = read(keyd_fd, &keybuf.buffer[count],
						keybuf.size - count);
				logthing(LOGTHING_TRACE,
						"Read %d bytes.", bytes);
				count += bytes;
			}
			read_openpgp_stream(buffer_fetchchar, &keybuf,
					&packets, 0);
			parse_keys(packets, publickey);
			free_packet_list(packets);
			packets = NULL;
			free(keybuf.buffer);
			keybuf.buffer = NULL;
			keybuf.size = 0;
		}
	}

	return (count > 0) ? 1 : 0;
}

/**
*	delete_key - Given a keyid delete the key from storage.
*	@keyid: The keyid to delete.
*	@intrans: If we're already in a transaction.
*
*	This function deletes a public key from whatever storage mechanism we
*	are using. Returns 0 if the key existed.
*/
static int keyd_delete_key(struct onak_dbctx *dbctx,
		uint64_t keyid, bool intrans)
{
	int keyd_fd = (intptr_t) dbctx->priv;

	if (keyd_send_cmd(keyd_fd, KEYD_CMD_DELETE)) {
		write(keyd_fd, &keyid, sizeof(keyid));
	}

	return 0;
}

/**
 *	store_key - Takes a key and stores it.
 *	@publickey: A pointer to the public key to store.
 *	@intrans: If we're already in a transaction.
 *	@update: If true the key exists and should be updated.
 *
 *	This function stores a public key in whatever storage mechanism we are
 *	using. intrans indicates if we're already in a transaction so don't
 *	need to start one. update indicates if the key already exists and is
 *	just being updated.
 *
 *	TODO: Do we store multiple keys of the same id? Or only one and replace
 *	it?
 */
static int keyd_store_key(struct onak_dbctx *dbctx,
		struct openpgp_publickey *publickey, bool intrans,
		bool update)
{
	int keyd_fd = (intptr_t) dbctx->priv;
	struct buffer_ctx           keybuf;
	struct openpgp_packet_list *packets = NULL;
	struct openpgp_packet_list *list_end = NULL;
	struct openpgp_publickey   *next = NULL;
	uint64_t                    keyid;

	if (get_keyid(publickey, &keyid) != ONAK_E_OK) {
		logthing(LOGTHING_ERROR, "Couldn't find key ID for key.");
		return 0;
	}

	if (update) {
		keyd_delete_key(dbctx, keyid, false);
	}

	if (keyd_send_cmd(keyd_fd, KEYD_CMD_STORE)) {
		keybuf.offset = 0;
		keybuf.size = 8192;
		keybuf.buffer = malloc(keybuf.size);

		next = publickey->next;
		publickey->next = NULL;
		flatten_publickey(publickey,
				&packets,
				&list_end);
		publickey->next = next;

		write_openpgp_stream(buffer_putchar, &keybuf, packets);
		logthing(LOGTHING_TRACE, "Sending %d bytes.", keybuf.offset);
		write(keyd_fd, &keybuf.offset, sizeof(keybuf.offset));
		write(keyd_fd, keybuf.buffer, keybuf.offset);

		free_packet_list(packets);
		packets = list_end = NULL;
		free(keybuf.buffer);
		keybuf.buffer = NULL;
		keybuf.size = keybuf.offset = 0;
	}

	return 0;
}

/**
 *	fetch_key_text - Trys to find the keys that contain the supplied text.
 *	@search: The text to search for.
 *	@publickey: A pointer to a structure to return the key in.
 *
 *	This function searches for the supplied text and returns the keys that
 *	contain it.
 */
static int keyd_fetch_key_text(struct onak_dbctx *dbctx,
		const char *search,
		struct openpgp_publickey **publickey)
{
	int keyd_fd = (intptr_t) dbctx->priv;
	struct buffer_ctx           keybuf;
	struct openpgp_packet_list *packets = NULL;
	ssize_t                     bytes = 0;
	ssize_t                     count = 0;

	if (keyd_send_cmd(keyd_fd, KEYD_CMD_GET_TEXT)) {
		bytes = strlen(search);
		write(keyd_fd, &bytes, sizeof(bytes));
		write(keyd_fd, search, bytes);
		keybuf.offset = 0;
		read(keyd_fd, &keybuf.size, sizeof(keybuf.size));
		if (keybuf.size > 0) {
			keybuf.buffer = malloc(keybuf.size);
			bytes = count = 0;
			logthing(LOGTHING_TRACE,
					"Getting %d bytes of key data.",
					keybuf.size);
			while (bytes >= 0 && count < keybuf.size) {
				bytes = read(keyd_fd, &keybuf.buffer[count],
						keybuf.size - count);
				logthing(LOGTHING_TRACE,
						"Read %d bytes.", bytes);
				count += bytes;
			}
			read_openpgp_stream(buffer_fetchchar, &keybuf,
					&packets, 0);
			parse_keys(packets, publickey);
			free_packet_list(packets);
			packets = NULL;
			free(keybuf.buffer);
			keybuf.buffer = NULL;
			keybuf.size = 0;
		}
	}

	return (count > 0) ? 1 : 0;

	return 0;
}

static int keyd_fetch_key_skshash(struct onak_dbctx *dbctx,
		const struct skshash *hash,
		struct openpgp_publickey **publickey)
{
	int keyd_fd = (intptr_t) dbctx->priv;
	struct buffer_ctx           keybuf;
	struct openpgp_packet_list *packets = NULL;
	ssize_t                     bytes = 0;
	ssize_t                     count = 0;

	if (keyd_send_cmd(keyd_fd, KEYD_CMD_GET_SKSHASH)) {
		write(keyd_fd, hash->hash, sizeof(hash->hash));
		keybuf.offset = 0;
		read(keyd_fd, &keybuf.size, sizeof(keybuf.size));
		if (keybuf.size > 0) {
			keybuf.buffer = malloc(keybuf.size);
			bytes = count = 0;
			logthing(LOGTHING_TRACE,
					"Getting %d bytes of key data.",
					keybuf.size);
			while (bytes >= 0 && count < keybuf.size) {
				bytes = read(keyd_fd, &keybuf.buffer[count],
						keybuf.size - count);
				logthing(LOGTHING_TRACE,
						"Read %d bytes.", bytes);
				count += bytes;
			}
			read_openpgp_stream(buffer_fetchchar, &keybuf,
					&packets, 0);
			parse_keys(packets, publickey);
			free_packet_list(packets);
			packets = NULL;
			free(keybuf.buffer);
			keybuf.buffer = NULL;
			keybuf.size = 0;
		}
	}

	return (count > 0) ? 1 : 0;
}


/**
 *	getfullkeyid - Maps a 32bit key id to a 64bit one.
 *	@keyid: The 32bit keyid.
 *
 *	This function maps a 32bit key id to the full 64bit one. It returns the
 *	full keyid. If the key isn't found a keyid of 0 is returned.
 */
static uint64_t keyd_getfullkeyid(struct onak_dbctx *dbctx, uint64_t keyid)
{
	int keyd_fd = (intptr_t) dbctx->priv;
	uint32_t cmd = KEYD_CMD_GETFULLKEYID;

	if (keyd_send_cmd(keyd_fd, KEYD_CMD_GETFULLKEYID)) {
		write(keyd_fd, &keyid, sizeof(keyid));
		read(keyd_fd, &cmd, sizeof(cmd));
		if (cmd != sizeof(keyid)) {
			return 0;
		}
		read(keyd_fd, &keyid, sizeof(keyid));
	}

	return keyid;
}

/**
 *	iterate_keys - call a function once for each key in the db.
 *	@iterfunc: The function to call.
 *	@ctx: A context pointer
 *
 *	Calls iterfunc once for each key in the database. ctx is passed
 *	unaltered to iterfunc. This function is intended to aid database dumps
 *	and statistic calculations.
 *
 *	Returns the number of keys we iterated over.
 */
static int keyd_iterate_keys(struct onak_dbctx *dbctx,
		void (*iterfunc)(void *ctx,
		struct openpgp_publickey *key),	void *ctx)
{
	int keyd_fd = (intptr_t) dbctx->priv;
	struct buffer_ctx           keybuf;
	struct openpgp_packet_list *packets = NULL;
	struct openpgp_publickey   *key = NULL;
	ssize_t                     bytes = 0;
	ssize_t                     count = 0;
	int                         numkeys = 0;

	if (keyd_send_cmd(keyd_fd, KEYD_CMD_KEYITER)) {
		keybuf.offset = 0;
		read(keyd_fd, &keybuf.size, sizeof(keybuf.size));
		while (keybuf.size > 0) {
			keybuf.buffer = malloc(keybuf.size);
			bytes = count = 0;
			logthing(LOGTHING_TRACE,
					"Getting %d bytes of key data.",
					keybuf.size);
			while (bytes >= 0 && count < keybuf.size) {
				bytes = read(keyd_fd, &keybuf.buffer[count],
						keybuf.size - count);
				logthing(LOGTHING_TRACE,
						"Read %d bytes.", bytes);
				count += bytes;
			}
			read_openpgp_stream(buffer_fetchchar, &keybuf,
					&packets, 0);
			parse_keys(packets, &key);

			if (iterfunc != NULL && key != NULL) {
				iterfunc(ctx, key);
			}

			free_publickey(key);
			key = NULL;
			free_packet_list(packets);
			packets = NULL;
			free(keybuf.buffer);
			keybuf.buffer = NULL;
			keybuf.size = keybuf.offset = 0;

			numkeys++;

			read(keyd_fd, &keybuf.size, sizeof(keybuf.size));
		}
	}

	return numkeys;
}

#define NEED_KEYID2UID 1
#define NEED_GETKEYSIGS 1
#define NEED_UPDATEKEYS 1
#include "keydb.c"

/**
 *	cleanupdb - De-initialize the key database.
 *
 *	This function should be called upon program exit to allow the DB to
 *	cleanup after itself.
 */
static void keyd_cleanupdb(struct onak_dbctx *dbctx)
{
	int keyd_fd = (intptr_t) dbctx->priv;
	uint32_t cmd = KEYD_CMD_CLOSE;

	if (write(keyd_fd, &cmd, sizeof(cmd)) != sizeof(cmd)) {
		logthing(LOGTHING_CRITICAL,
				"Couldn't send close cmd: %s (%d)",
				strerror(errno),
				errno);
	}

	if (read(keyd_fd, &cmd, sizeof(cmd)) != sizeof(cmd)) {
		logthing(LOGTHING_CRITICAL,
			"Couldn't read close cmd reply: %s (%d)",
			strerror(errno),
			errno);
	} else if (cmd != KEYD_REPLY_OK) {
		logthing(LOGTHING_CRITICAL,
			"Got bad reply to KEYD_CMD_CLOSE: %d", cmd);
	}

	if (shutdown(keyd_fd, SHUT_RDWR) < 0) {
		logthing(LOGTHING_NOTICE, "Error shutting down socket: %d",
				errno);
	}
	if (close(keyd_fd) < 0) {
		logthing(LOGTHING_NOTICE, "Error closing down socket: %d",
				errno);
	}

	free(dbctx);

	return;
}

/**
 *	initdb - Initialize the key database.
 *	@readonly: If we'll only be reading the DB, not writing to it.
 *
 *	This function should be called before any of the other functions in
 *	this file are called in order to allow the DB to be initialized ready
 *	for access.
 */
struct onak_dbctx *keydb_keyd_init(bool readonly)
{
	struct sockaddr_un sock;
	uint32_t	   cmd = KEYD_CMD_UNKNOWN;
	uint32_t	   reply = KEYD_REPLY_UNKNOWN_CMD;
	ssize_t		   count;
	int keyd_fd;
	struct onak_dbctx *dbctx;

	dbctx = malloc(sizeof(*dbctx));
	if (dbctx == NULL) {
		return NULL;
	}

	keyd_fd = socket(PF_UNIX, SOCK_STREAM, 0);
	if (keyd_fd < 0) {
		logthing(LOGTHING_CRITICAL,
				"Couldn't open socket: %s (%d)",
				strerror(errno),
				errno);
		exit(EXIT_FAILURE);
	}

	sock.sun_family = AF_UNIX;
	snprintf(sock.sun_path, sizeof(sock.sun_path) - 1, "%s/%s",
			config.db_dir,
			KEYD_SOCKET);
	if (connect(keyd_fd, (struct sockaddr *) &sock, sizeof(sock)) < 0) {
		logthing(LOGTHING_CRITICAL,
				"Couldn't connect to socket %s: %s (%d)",
				sock.sun_path,
				strerror(errno),
				errno);
		exit(EXIT_FAILURE);
	}

	cmd = KEYD_CMD_VERSION;
	if (write(keyd_fd, &cmd, sizeof(cmd)) != sizeof(cmd)) {
		logthing(LOGTHING_CRITICAL,
				"Couldn't write version cmd: %s (%d)",
				strerror(errno),
				errno);
	} else {
		count = read(keyd_fd, &reply, sizeof(reply));
		if (count == sizeof(reply) && reply == KEYD_REPLY_OK) {
			count = read(keyd_fd, &reply, sizeof(reply));
			if (count != sizeof(reply) || reply != sizeof(reply)) {
				logthing(LOGTHING_CRITICAL,
					"Error! Unexpected keyd version "
					"length: %d != %d",
					reply, sizeof(reply));
				exit(EXIT_FAILURE);
			}

			count = read(keyd_fd, &reply, sizeof(reply));
			if (count != sizeof(reply)) {
				logthing(LOGTHING_CRITICAL,
					"Error! Unexpected keyd version "
					"length: %d != %d",
					count, sizeof(reply));
				exit(EXIT_FAILURE);
			}
			logthing(LOGTHING_DEBUG,
					"keyd protocol version %d",
					reply);
			if (reply != keyd_version) {
				logthing(LOGTHING_CRITICAL,
					"Error! keyd protocol version "
					"mismatch. (us = %d, it = %d)",
						keyd_version, reply);
			}
		}
	}

	dbctx->priv			= (void *) (intptr_t) keyd_fd;
	dbctx->cleanupdb		= keyd_cleanupdb;
	dbctx->starttrans		= keyd_starttrans;
	dbctx->endtrans			= keyd_endtrans;
	dbctx->fetch_key_id		= keyd_fetch_key_id;
	dbctx->fetch_key_fp		= keyd_fetch_key_fp;
	dbctx->fetch_key_text		= keyd_fetch_key_text;
	dbctx->fetch_key_skshash	= keyd_fetch_key_skshash;
	dbctx->store_key		= keyd_store_key;
	dbctx->update_keys		= generic_update_keys;
	dbctx->delete_key		= keyd_delete_key;
	dbctx->getkeysigs		= generic_getkeysigs;
	dbctx->cached_getkeysigs	= generic_cached_getkeysigs;
	dbctx->keyid2uid		= generic_keyid2uid;
	dbctx->getfullkeyid		= keyd_getfullkeyid;
	dbctx->iterate_keys		= keyd_iterate_keys;

	return dbctx;
}