File: shield_purge.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 (228 lines) | stat: -rw-r--r-- 5,255 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
/*
	shield_purge.c	WJ107

    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
*/

#include "pam_shield.h"

#include <libgen.h>
#include <getopt.h>

#include "pam_shield_lib.c"


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

	if (level == LOG_DEBUG && !(options & OPT_DEBUG))
		return;

	va_start(varargs, fmt);
	vfprintf(stderr, fmt, varargs);
	fprintf(stderr, "\n");
	va_end(varargs);
}


static void usage(char *progname) {
	printf(
"shield-purge " PAM_SHIELD_VERSION "\n"
"usage: %s <options>\n"
"options:\n"
"  -h, --help        Display this information\n"
"  -c, --conf=file   Specify config file (default: " DEFAULT_CONFFILE ")\n"
"  -d, --debug       Verbose output for debugging purposes\n"
"  -n, --dry-run     Do not perform any updates\n"
"  -l, --list        List all database entries\n"
, basename(progname));

	printf("\n"
"This program is part of the PAM-shield package.\n"
"PAM-shield comes with ABSOLUTELY NO WARRANTY.  This is free software, and you\n"
"are welcome to redistribute it under certain conditions.  See the GNU\n"
"General Public Licence for details.\n"
"\n"
"Copyright (C) 2007 by Walter de Jong <walter@heiho.net>\n");
	exit(1);
}

static void get_options(int argc, char **argv) {
int opt;
struct option long_options[] = {
	{ "help",		0, NULL, 'h' },
	{ "debug",		0, NULL, 'd' },
	{ "conf",		1, NULL, 'c' },
	{ "dry-run",	0, NULL, 'n' },
	{ "list",		0, NULL, 'l' },
};

	if (argc <= 1)
		usage(argv[0]);

	while((opt = getopt_long(argc, argv, "hdc:nl", long_options, NULL)) != -1) {
		switch(opt) {
			case 'h':
			case '?':
				usage(argv[0]);

			case 'd':
				options |= OPT_DEBUG;
				logmsg(LOG_DEBUG, "logging debug info");
				break;

			case 'c':
				if (optarg == NULL || !*optarg) {
					logmsg(LOG_ERR, "missing filename");
					exit(1);
				}
				if ((conffile = strdup(optarg)) == NULL) {
					logmsg(LOG_ERR, "out of memory");
					exit(-1);
				}
				break;

			case 'n':
				options |= OPT_DRYRUN;
				logmsg(LOG_DEBUG, "performing dry-run");
				break;

			case 'l':
				options |= OPT_LISTDB;
				logmsg(LOG_DEBUG, "list database");
				break;

			default:
				logmsg(LOG_ERR, "bad command line option");
				exit(1);
		}
	}
}

/*
	lists one record from the DB
*/
static void print_record(_pam_shield_db_rec_t *record) {
char ipbuf[INET6_ADDRSTRLEN];
unsigned int i;

	print_ip(record, ipbuf, INET6_ADDRSTRLEN);

	printf("{\n"
		"  ip %s\n"
		"  max_entries %u\n"
		"  count %u\n"
		"  trigger_active %s\n",
		ipbuf, record->max_entries, record->count, ctime(&record->trigger_active)
	);
	for(i = 0; i < record->max_entries; i++)
		printf("  timestamp %s", ctime(&record->timestamps[i]));

	printf("}\n");
}

/*
	list database entries
	this is also mostly meant for debugging and looking at what's in the DB
*/
static void list_db(void) {
_pam_shield_db_rec_t *record;
datum key, next_key, data;

	key = gdbm_firstkey(dbf);

	if (key.dptr == NULL)
		printf("database is empty\n");

	while(key.dptr != NULL) {
		data = gdbm_fetch(dbf, key);

		if (data.dptr == NULL) {
			printf("NULL data\n");
		} else {
			record = (_pam_shield_db_rec_t *)data.dptr;
			print_record(record);
		}
		next_key = gdbm_nextkey(dbf, key);
		free(key.dptr);
		key = next_key;
	}
}

/*
	expire old entries from the database
*/
static void purge_db(void) {
_pam_shield_db_rec_t *record;
datum key, next_key, data;

	key = gdbm_firstkey(dbf);

	while(key.dptr != NULL) {
		data = gdbm_fetch(dbf, key);

		if (data.dptr == NULL) {
			logmsg(LOG_DEBUG, "cleaning up empty key");
			if (!(options & OPT_DRYRUN))
				gdbm_delete(dbf, key);
		} else {
			record = (_pam_shield_db_rec_t *)data.dptr;

/* store any changes */
			if (expire_record(record)) {
				if (!record->count) {
					logmsg(LOG_DEBUG, "expiring entry");
					if (!(options & OPT_DRYRUN))
						gdbm_delete(dbf, key);
				} else {
					logmsg(LOG_DEBUG, "storing updated entry");
					if (!(options & OPT_DRYRUN))
						gdbm_store(dbf, key, data, GDBM_REPLACE);
				}
			}
		}
		next_key = gdbm_nextkey(dbf, key);
		free(key.dptr);
		key = next_key;
	}
}

int main(int argc, char **argv) {
	init_module();

	get_options(argc, argv);
	read_config();

	this_time = time(NULL);

	if ((dbf = gdbm_open(dbfile, 512, GDBM_WRITER, (mode_t)0600, fatal_func)) == NULL) {
		logmsg(LOG_ERR, "failed to open db '%s' : %s", dbfile, gdbm_strerror(gdbm_errno));
		return -1;
	}
	if (options & OPT_LISTDB)
		list_db();
	else
		purge_db();

	gdbm_close(dbf);

	deinit_module();
	return 0;
}

/* EOB */