File: pam_shield_lib.c

package info (click to toggle)
pam-shield 0.9.2-3.3~squeeze1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 216 kB
  • ctags: 113
  • sloc: ansic: 988; sh: 461; makefile: 102
file content (783 lines) | stat: -rw-r--r-- 18,281 bytes parent folder | download | duplicates (3)
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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
/*
	pam_shield_lib.c	WJ106

    pam_shield 0.9.2 WJ107
    Copyright (C) 2007  Walter de Jong <walter@heiho.net>

    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; either version 2 of the License, or
    (at your option) any later version.

    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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
/*
	This file is not a true library; this code is #include'd into
	the pam_shield sources. The reason for this is that I had problems
	with having lots of duplicate code, while the symbols should be 'static'
	in the resulting pam_shield.so shared library
*/

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <syslog.h>
#include <pwd.h>
#include <time.h>
#include <netdb.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <gdbm.h>

#define PAM_SHIELD_VERSION		"0.9.2"

#define DEFAULT_CONFFILE		"/etc/security/shield.conf"
#define DEFAULT_DBFILE			"/var/lib/pam_shield/db"
#define DEFAULT_TRIGGER_CMD		"/usr/sbin/pam_shield-trigger"
#define DEFAULT_MAX_CONNS		10
#define DEFAULT_INTERVAL		60L
#define DEFAULT_RETENTION		(3600L * 24L)

#define MAX_LINE				1024

#define OPT_DEBUG				1
#define OPT_BLOCK_ALL			2		/* block all, including known users */
#define OPT_DRYRUN				4
#define OPT_LISTDB				8
#define OPT_MISSING_DNS			0x10	/* allow missing DNS */
#define OPT_MISSING_REVERSE		0x20	/* allow missing reverse DNS */

static int options = 0;
static GDBM_FILE dbf;

static char *conffile = NULL;
static char *dbfile = NULL;
static char *trigger_cmd = NULL;

/* white lists of addresses */
static ip_list *allow_ipv4_list = NULL;
static ip_list *allow_ipv6_list = NULL;
static name_list *allow_names = NULL;

static int max_conns = DEFAULT_MAX_CONNS;
static long interval = DEFAULT_INTERVAL;
static long retention = DEFAULT_RETENTION;

static time_t this_time;


static void logmsg(int level, const char *fmt, ...);


static ip_list *new_ip_list(void) {
ip_list *ip;

	if ((ip = (ip_list *)malloc(sizeof(ip_list))) == NULL)
		return NULL;

	memset(ip, 0, sizeof(ip_list));
	return ip;
}

static void destroy_ip_list(ip_list *list) {
ip_list *p;

	while(list != NULL) {
		p = list;
		list = list->next;
		free(p);
	}
}

static void add_ip_list(ip_list **root, ip_list *ip) {
	if (root == NULL || ip == NULL)
		return;

	if (options & OPT_DEBUG) {
		char addr[INET6_ADDRSTRLEN], mask[INET6_ADDRSTRLEN];

		if (*root == allow_ipv4_list)					/* (butt ugly check, just to get nice debug output) */
			logmsg(LOG_DEBUG, "allowing from %s/%s", inet_ntop(AF_INET, &ip->ip.in, addr, sizeof(addr)),
				inet_ntop(AF_INET, &ip->mask.in, mask, sizeof(mask)));
		else
			logmsg(LOG_DEBUG, "allowing from %s/%s", inet_ntop(AF_INET6, &ip->ip.in6, addr, sizeof(addr)),
				inet_ntop(AF_INET6, &ip->mask.in6, mask, sizeof(mask)));
	}
	ip->prev = ip->next = NULL;

	if (*root == NULL) {
		*root = ip;
		return;
	}
/* prepend it */
	(*root)->prev = ip;
	ip->next = *root;
	*root = ip;
}

/*
	try to match an IP number against the allow list
	returns 1 if it matches
*/
static int match_ipv4_list(unsigned char *saddr) {
ip_list *ip;
int i, match;

	for(ip = allow_ipv4_list; ip != NULL; ip = ip->next) {
		match = 1;
		for(i = 0; i < sizeof(ip->ip.in.s_addr); i++) {
			if ((ip->ip.any[i] & ip->mask.any[i]) != (saddr[i] & ip->mask.any[i])) {
				match = 0;
				break;
			}
		}
		if (match) {
			char addr1[INET_ADDRSTRLEN], addr2[INET_ADDRSTRLEN], mask[INET_ADDRSTRLEN];

			logmsg(LOG_DEBUG, "whitelist match: %s %s/%s", inet_ntop(AF_INET, saddr, addr1, sizeof(addr1)),
				inet_ntop(AF_INET, &ip->ip.in, addr2, sizeof(addr2)),
				inet_ntop(AF_INET, &ip->mask.in, mask, sizeof(mask)));
			return 1;
		}
	}
	return 0;
}

static int match_ipv6_list(unsigned char *saddr) {
ip_list *ip;
int i, match;

	for(ip = allow_ipv6_list; ip != NULL; ip = ip->next) {
		match = 1;
		for(i = 0; i < sizeof(ip->ip.in6.s6_addr); i++) {
			if ((ip->ip.any[i] & ip->mask.any[i]) != (saddr[i] & ip->mask.any[i])) {
				match = 0;
				break;
			}
		}
		if (match) {
			char addr1[INET6_ADDRSTRLEN], addr2[INET6_ADDRSTRLEN], mask[INET6_ADDRSTRLEN];

			logmsg(LOG_DEBUG, "whitelist match: %s %s/%s", inet_ntop(AF_INET6, saddr, addr1, sizeof(addr1)),
				inet_ntop(AF_INET6, &ip->ip.in6, addr2, sizeof(addr2)),
				inet_ntop(AF_INET6, &ip->mask.in6, mask, sizeof(mask)));
			return 1;
		}
	}
	return 0;
}

/*
	name_lists are hostnames and/or network names
*/
static name_list *new_name_list(char *name) {
name_list *n;

	if (name == NULL || !*name)
		return NULL;

	if ((n = (name_list *)malloc(sizeof(name_list) + strlen(name))) == NULL)
		return NULL;

	memset(n, 0, sizeof(name_list));
	strcpy(n->name, name);
	return n;
}

static void destroy_name_list(name_list *list) {
name_list *p;

	while(list != NULL) {
		p = list;
		list = list->next;
		free(p);
	}
}

static void add_name_list(name_list **root, name_list *n) {
	if (root == NULL || n == NULL)
		return;

	logmsg(LOG_DEBUG, "allowing from %s", n->name);

	n->prev = n->next = NULL;

	if (*root == NULL) {
		*root = n;
		return;
	}
/* prepend it */
	(*root)->prev = n;
	n->next = *root;
	*root = n;
}

/*
	see if 'name' matches our whitelist
	return 1 if it does
*/
static int match_name_list(char *name) {
name_list *n;

	if (name == NULL || !*name)
		return 0;

	for(n = allow_names; n != NULL; n = n->next) {
		if (n->name[0] == '.') {
			if ((strlen(name) > strlen(n->name)) && !strcasecmp(n->name, name + strlen(name) - strlen(n->name))) {
				logmsg(LOG_DEBUG, "whitelist match: host %s in domain %s", name, n->name);
				return 1;
			}
		} else {
			if (!strcasecmp(n->name, name)) {
				logmsg(LOG_DEBUG, "whitelist match: host %s", name);
				return 1;
			}
		}
	}
	return 0;
}


/*
	initialize variables
*/
static int init_module(void) {
	this_time = time(NULL);

	conffile = strdup(DEFAULT_CONFFILE);
	dbfile = strdup(DEFAULT_DBFILE);
	trigger_cmd = strdup(DEFAULT_TRIGGER_CMD);

	if (conffile == NULL || dbfile == NULL || trigger_cmd == NULL) {
		logmsg(LOG_CRIT, "out of memory");
		return -1;
	}
	return 0;
}

static void deinit_module(void) {
	if (conffile != NULL) {
		free(conffile);
		conffile = NULL;
	}
	if (dbfile != NULL) {
		free(dbfile);
		dbfile = NULL;
	}
	if (trigger_cmd != NULL) {
		free(trigger_cmd);
		trigger_cmd = NULL;
	}
	destroy_ip_list(allow_ipv4_list);
	allow_ipv4_list = NULL;

	destroy_ip_list(allow_ipv6_list);
	allow_ipv6_list = NULL;

	destroy_name_list(allow_names);
	allow_names = NULL;
}

/*
	strip leading and trailing whitespace from a string
*/
static void strip(char *str) {
char *p;
int i;

	if (str == NULL || !*str)
		return;

	p = str;

	if (*p == ' ' || *p == '\t') {
		while(*p && (*p == ' ' || *p == '\t'))
			p++;

		memmove(str, p, strlen(p)+1);
	}
	i = strlen(str)-1;
	while(i >= 0 && (str[i] == ' ' || str[i] == '\t' || str[i] == '\r' || str[i] == '\n'))
		str[i--] = 0;
}

/*
	multipliers:
		1s	second
		1m	minute
		1h	hour
		1d	day
		1w	week
		1M	month
		1y	year

	default is 1
	returns 0 on error
*/
static long get_multiplier(char *str) {
	if (str == NULL || !*str)
		return 1L;

	if (str[1])				/* we expect only a single character here */
		return 0L;

	switch(*str) {
		case 's':
			return 1L;

		case 'm':
			return 60L;

		case 'h':
			return 3600L;

		case 'd':
			return (3600L * 24L);

		case 'w':
			return (7L * 3600L * 24L);

		case 'M':
			return (30L * 3600L * 24L);

		case 'y':
		case 'Y':
			return (365L * 3600L * 24L);
	}
	return 0L;
}

/*
	generate bitmask from '/24' notation

	mask is struct in_addr.saddr, size is the size of the array
	(4 for IPv4, 16 for IPv6)
*/
static void ip_bitmask(int bits, unsigned char *mask, int size) {
int i, num, rest;

	if (mask == NULL)
		return;

	memset(mask, 0, size);

	if (bits < 0)
		bits = 0;

	if (bits > size*8)
		bits = size*8;

	num = bits / 8;
	rest = bits % 8;

	for(i = 0; i < num; i++)
		mask[i] = 0xff;

	if (rest)
		mask[i++] = ~(0xff >> rest);

	while(i < size)
		mask[i++] = 0;
}

/*
	allow network/netmask, for both IPv4 and IPv6
	netmask can be in canonical or decimal notation
*/
static int allow_ip(char *ipnum, int line_no) {
char *netmask;
ip_list *ip;
name_list *name;
int bits;

	if (ipnum == NULL || !*ipnum) {
		logmsg(LOG_ALERT, "%s:%d: missing argument to 'allow'", conffile, line_no);
		return -1;
	}
	if ((netmask = strchr(ipnum, '/')) != NULL) {
		*netmask = 0;
		netmask++;
		if (!*netmask) {
			logmsg(LOG_ALERT, "%s:%d: missing netmask, assuming it is a host", conffile, line_no);
			netmask = NULL;
		}
	}
	if ((ip = new_ip_list()) == NULL) {
		logmsg(LOG_ALERT, "%s:%d: out of memory adding 'allow' line", conffile, line_no);
		return -1;
	}
/* try network address as IPv4 */
	if (inet_pton(AF_INET, ipnum, &ip->ip.in) > 0) {
		if (netmask == NULL) {		/* no netmask given, treat as host */
			memset(&ip->mask.in.s_addr, 0xff, sizeof(ip->mask.in.s_addr));
			add_ip_list(&allow_ipv4_list, ip);
			return 0;
		}
/* netmask in '/24' like notation? */
		if (strspn(netmask, "0123456789") == strlen(netmask)) {
			bits = atoi(netmask);
			if (bits <= 0 || bits > 32) {
				logmsg(LOG_ALERT, "%s:%d: syntax error in netmask", conffile, line_no);
				destroy_ip_list(ip);
				return -1;
			}
			ip_bitmask(bits, (unsigned char *)&ip->mask.in.s_addr, sizeof(ip->mask.in.s_addr));

			add_ip_list(&allow_ipv4_list, ip);
			return 0;
		}
/* netmask in canonical notation? */
		if (inet_pton(AF_INET, netmask, &ip->mask.in) > 0) {
			add_ip_list(&allow_ipv4_list, ip);
			return 0;
		}
		logmsg(LOG_ALERT, "%s:%d: syntax error in netmask", conffile, line_no);
		destroy_ip_list(ip);
		return -1;
	}
/* try network address as IPv6 */
	if (inet_pton(AF_INET6, ipnum, &ip->ip.in6) > 0) {
		if (netmask == NULL) {		/* no netmask given, treat as host */
			memset(ip->mask.in6.s6_addr, 0xff, sizeof(ip->mask.in6.s6_addr));
			add_ip_list(&allow_ipv6_list, ip);
			return 0;
		}
/* netmask in '/24' like notation? */
		if (strspn(netmask, "0123456789") == strlen(netmask)) {
			bits = atoi(netmask);
			if (bits <= 0 || bits > 32) {
				logmsg(LOG_ALERT, "%s:%d: syntax error in netmask", conffile, line_no);
				destroy_ip_list(ip);
				return -1;
			}
			ip_bitmask(bits, (unsigned char *)ip->mask.in6.s6_addr, sizeof(ip->mask.in6.s6_addr));

			add_ip_list(&allow_ipv6_list, ip);
			return 0;
		}
/* netmask in canonical notation? */
		if (inet_pton(AF_INET6, netmask, &ip->mask.in6) > 0) {
			add_ip_list(&allow_ipv6_list, ip);
			return 0;
		}
		logmsg(LOG_ALERT, "%s:%d: syntax error in netmask", conffile, line_no);
		destroy_ip_list(ip);
		return -1;
	}
/*
	when we get here it's either a syntax error or a hostname or a network name
	with names, you can not specify a netmask
*/
	destroy_ip_list(ip);
	ip = NULL;

	if (netmask != NULL) {
		logmsg(LOG_ALERT, "%s:%d: syntax error in internet address", conffile, line_no);
		return -1;
	}
	if ((name = new_name_list(ipnum)) == NULL) {
		logmsg(LOG_ALERT, "%s:%d: out of memory while adding 'allow' line", conffile, line_no);
		return -1;
	}
	add_name_list(&allow_names, name);
	return 0;
}

/*
	read configuration file
*/
static int read_config(void) {
FILE *f;
char buf[MAX_LINE], *p, *endp;
int line_no, err;
long multiplier;

	logmsg(LOG_DEBUG, "reading config file '%s'", conffile);

	if ((f = fopen(conffile, "r")) == NULL) {
		logmsg(LOG_ALERT, "failed to read config file '%s'", conffile);
		return -1;
	}
	line_no = 0;
	err = 0;

	while(fgets(buf, MAX_LINE, f) != NULL) {
		line_no++;

		strip(buf);
		if (!*buf || buf[0] == '#')
			continue;

/* keyword <space> value */

		p = buf;
		while(*p && *p != ' ' && *p != '\t')
			p++;

		if (!*p) {
			logmsg(LOG_ALERT, "%s:%d: syntax error", conffile, line_no);
			err--;
			continue;
		}
		*p = 0;
		p++;

		strip(buf);
		if (!*buf) {
			logmsg(LOG_ALERT, "%s:%d: syntax error", conffile, line_no);
			err--;
			continue;
		}
		strip(p);
		if (!*p) {
			logmsg(LOG_ALERT, "%s:%d: syntax error", conffile, line_no);
			err--;
			continue;
		}

/* buf is the key, p is the value */

		if (!strcmp(buf, "debug")) {
			if (!strcmp(p, "on") || !strcmp(p, "yes")) {
				options |= OPT_DEBUG;
				logmsg(LOG_DEBUG, "logging debug info");
				continue;
			}
			if (!strcmp(p, "off") || !strcmp(p, "no")) {
				logmsg(LOG_DEBUG, "ignoring config option 'debug %s' (overruled by PAM command line argument 'debug')", p);
				continue;
			}
			logmsg(LOG_ALERT, "%s:%d: unknown argument '%s' to 'debug'", conffile, line_no, p);
			continue;
		}
		if (!strcmp(buf, "block")) {
			if (!strcmp(p, "all-users")) {
				options |= OPT_BLOCK_ALL;
				continue;
			}
			if (!strcmp(p, "unknown-users")) {
				options &= ~OPT_BLOCK_ALL;
				continue;
			}
			logmsg(LOG_ALERT, "%s:%d: unknown argument '%s' to 'block'", conffile, line_no, p);
			err--;
			continue;
		}
		if (!strcmp(buf, "allow_missing_dns")) {
			if (!strcasecmp(p, "yes") || !strcasecmp(p, "allow") || !strcasecmp(p, "on")) {
				options |= OPT_MISSING_DNS;
				continue;
			}
			if (!strcasecmp(p, "no") || !strcasecmp(p, "deny") || !strcasecmp(p, "off")) {
				options &= ~OPT_MISSING_DNS;
				continue;
			}
			logmsg(LOG_ALERT, "%s:%d: unknown argument '%s' to 'allow_missing_dns'", conffile, line_no, p);
			err--;
			continue;
		}
		if (!strcmp(buf, "allow_missing_reverse")) {
			if (!strcasecmp(p, "yes") || !strcasecmp(p, "allow") || !strcasecmp(p, "on")) {
				options |= OPT_MISSING_REVERSE;
				continue;
			}
			if (!strcasecmp(p, "no") || !strcasecmp(p, "deny") || !strcasecmp(p, "off")) {
				options &= ~OPT_MISSING_REVERSE;
				continue;
			}
			logmsg(LOG_ALERT, "%s:%d: unknown argument '%s' to 'allow_missing_reverse'", conffile, line_no, p);
			err--;
			continue;
		}
		if (!strcmp(buf, "allow")) {
			if (allow_ip(p, line_no))
				err--;
			continue;
		}
		if (!strcmp(buf, "db")) {
			free(dbfile);
			if ((dbfile = strdup(p)) == NULL) {
				logmsg(LOG_CRIT, "out of memory");
				err--;
			}
			continue;
		}
		if (!strcmp(buf, "trigger_cmd")) {
			free(trigger_cmd);
			if ((trigger_cmd = strdup(p)) == NULL) {
				logmsg(LOG_CRIT, "out of memory");
				err--;
			}
			continue;
		}
		if (!strcmp(buf, "max_conns")) {
			max_conns = (int)strtol(p, &endp, 10);
			if (*endp) {
				logmsg(LOG_ALERT, "%s:%d: syntax error", conffile, line_no);
				err--;
				max_conns = DEFAULT_MAX_CONNS;
			}
			continue;
		}
		if (!strcmp(buf, "interval")) {
			interval = (int)strtol(p, &endp, 10);
			if (!(multiplier = get_multiplier(endp))) {
				logmsg(LOG_ALERT, "%s:%d: syntax error", conffile, line_no);
				err--;
				interval = DEFAULT_INTERVAL;
			} else
				interval *= multiplier;
			continue;
		}
		if (!strcmp(buf, "retention")) {
			retention = (int)strtol(p, &endp, 10);
			if (!(multiplier = get_multiplier(endp))) {
				logmsg(LOG_ALERT, "%s:%d: syntax error", conffile, line_no);
				err--;
				retention = DEFAULT_RETENTION;
			} else
				retention *= multiplier;
			continue;
		}
		logmsg(LOG_ALERT, "%s:%d: unknown keyword '%s'", conffile, line_no, buf);
		err--;
	}
	fclose(f);

	logmsg(LOG_DEBUG, "done reading config file, %d errors", -err);

	return err;
}

/*
	print the IP number of a db_record
	return NULL on error, or buf on success
*/
static const char *print_ip(_pam_shield_db_rec_t *record, char *buf, int buflen) {
	if (buf == NULL || buflen <= 1)
		return NULL;

	buflen--;
	if (!buflen) {
		*buf = 0;
		return buf;
	}
	if (record == NULL) {
		strncpy(buf, "(null)", buflen);
		buf[buflen] = 0;
		return buf;
	}
	switch(record->addr_family) {
		case PAM_SHIELD_ADDR_IPV4:
			return inet_ntop(AF_INET, &record->ip.in, buf, buflen);

		case PAM_SHIELD_ADDR_IPV6:
			return inet_ntop(AF_INET6, &record->ip.in6, buf, buflen);
	}
	return NULL;
}

/*
	run external command
*/
static int run_trigger(char *cmd, _pam_shield_db_rec_t *record) {
char ipbuf[INET6_ADDRSTRLEN];
pid_t pid;

	if (cmd == NULL || record == NULL)
		return -1;

	if (print_ip(record, ipbuf, sizeof(ipbuf)) == NULL)
		return -1;

	logmsg(LOG_DEBUG, "running command '%s %s'", cmd, ipbuf);

	if (options & OPT_DRYRUN)
		return 0;

	pid = fork();
	if (pid == (pid_t)-1) {
		logmsg(LOG_CRIT, "can not fork, failed to run trigger");
		return -1;
	}
	if (!pid) {
		char *argv[4];

		argv[0] = trigger_cmd;
		argv[1] = cmd;
		argv[2] = ipbuf;
		argv[3] = NULL;

		execvp(argv[0], argv);

		logmsg(LOG_CRIT, "failed to execute command '%s %s %s'", trigger_cmd, cmd, ipbuf);
		exit(-1);
	} else {
		pid_t err;

		while((err = waitpid(pid, NULL, 0)) > 0);
	}
	return 0;
}

static int expire_record(_pam_shield_db_rec_t *record) {
int updated;
char ipbuf[INET6_ADDRSTRLEN];

	if (record == NULL)
		return 0;

	updated = 0;
/*
	expire entries that are no longer in this interval (sliding window)
*/
	while(record->count > 0 && difftime(this_time, record->timestamps[0]) >= (double)interval) {
		memmove(record->timestamps, &record->timestamps[1], (record->max_entries-1)*sizeof(time_t));
		record->count--;
		updated++;
	}
	if (record->trigger_active) {
		if (difftime(this_time, record->trigger_active) >= (double)retention) {
/*
	expire old trigger, but only do this if the sliding window is clean
*/
			if (!record->count) {
				logmsg(LOG_DEBUG, "expiring old trigger for %s", print_ip(record, ipbuf, sizeof(ipbuf)));
				record->trigger_active = (time_t)0L;
				run_trigger("del", record);
				updated++;
			}
		}
	}
	return updated;
}


/*
	gdbm has encountered a fatal error
*/
static void fatal_func(char *str) {
	logmsg(LOG_ERR, "gdbm encountered a fatal error : %s; resetting the database", str);

	gdbm_close(dbf);
	if ((dbf = gdbm_open(dbfile, GDBM_NEWDB, 512, (mode_t)0600, fatal_func)) == NULL)
		logmsg(LOG_ERR, "failed to create new gdbm file '%s' : %s", dbfile, gdbm_strerror(gdbm_errno));
}

/* EOB */