-
Notifications
You must be signed in to change notification settings - Fork 3
/
shield_purge.c
325 lines (274 loc) · 8.57 KB
/
shield_purge.c
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
/*
shield_purge.c
Copyright (C) 2007-2024
Walter de Jong <[email protected]>
Jonathan Niehof <[email protected]>
Jeffrey Clark <[email protected]>
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 <getopt.h>
#include <libgen.h>
#include "pam_shield_lib.h"
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"
" -f, --force Delete all entries, even if unexpired\n"
" -r, --remove=ip Remove IP from database\n"
" -s, --sync Trigger sync for active records\n"
" (rebuild/verify firewall rules)\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-2011 by Walter de Jong <[email protected]>\n"
"Copyright 2010 Jonathan Niehof <[email protected]>\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'}, {"force", 0, NULL, 'f'},
{"remove", 1, NULL, 'r'}, {"sync", 0, NULL, 's'}, {NULL, 0, NULL, 0},
};
while ((opt = getopt_long(argc, argv, "hdc:nlfr:s", 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;
case 'f':
options |= OPT_FORCE;
logmsg(LOG_DEBUG, "force purge");
break;
case 'r':
options |= OPT_REMOVEIP;
if (optarg == NULL || !*optarg) {
logmsg(LOG_ERR, "missing ip");
exit(1);
}
if ((removeip = strdup(optarg)) == NULL) {
logmsg(LOG_ERR, "out of memory");
exit(-1);
}
break;
case 's':
options |= OPT_SYNC;
logmsg(LOG_DEBUG, "sync");
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;
char *time = ctime(&record->trigger_active);
time[strlen(time) - 1] = '\0';
print_ip(record, ipbuf, INET6_ADDRSTRLEN);
printf("\n {\n"
" \"ip\": \"%s\",\n"
" \"max_entries\": %u,\n"
" \"count\": %u,\n"
" \"trigger_active\": \"%s\",\n"
" \"timestamps\": [\n",
ipbuf, record->max_entries, record->count, (record->trigger_active > 0) ? time : "");
for (i = 0; i < record->max_entries; i++)
if (record->timestamps[i] > 0) {
time = ctime(&record->timestamps[i]);
time[strlen(time) - 1] = '\0';
printf(" \"%s\"%s\n", time, (record->timestamps[(i + 1)] > 0) ? "," : "");
}
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");
return;
}
printf("{\n \"db\": [");
while (key.dptr != NULL) {
data = gdbm_fetch(dbf, key);
if (data.dptr != NULL) {
record = (_pam_shield_db_rec_t *)data.dptr;
print_record(record);
}
next_key = gdbm_nextkey(dbf, key);
free(key.dptr);
key = next_key;
if (data.dptr != NULL && key.dptr != NULL) {
printf(",");
}
}
printf("\n ]\n}\n");
}
/* expire old entries from the database */
static void purge_db(void) {
_pam_shield_db_rec_t *record;
datum key, next_key, data;
int deleted = 0; /*If any key deleted, order changes; must revisit all keys*/
key = gdbm_firstkey(dbf);
while (key.dptr != NULL) {
data = gdbm_fetch(dbf, key);
next_key = gdbm_nextkey(dbf, key);
if (options & OPT_FORCE) {
logmsg(LOG_DEBUG, "force-expiring entry");
if (!(options & OPT_DRYRUN)) {
gdbm_delete(dbf, key);
deleted = 1;
}
} else if (data.dptr == NULL) {
logmsg(LOG_DEBUG, "cleaning up empty key");
if (!(options & OPT_DRYRUN)) {
gdbm_delete(dbf, key);
deleted = 1;
}
} else {
record = (_pam_shield_db_rec_t *)data.dptr;
/* store any changes */
if (expire_record(record)) {
if (!record->count && !record->trigger_active) {
logmsg(LOG_DEBUG, "expiring entry");
if (!(options & OPT_DRYRUN)) {
gdbm_delete(dbf, key);
deleted = 1;
}
} else {
logmsg(LOG_DEBUG, "storing updated entry");
if (!(options & OPT_DRYRUN))
gdbm_store(dbf, key, data, GDBM_REPLACE);
}
}
free(data.dptr);
}
free(key.dptr);
key = next_key;
if (deleted && !key.dptr) {
deleted = 0;
key = gdbm_firstkey(dbf);
}
}
}
/* remove ip from the database */
static int remove_ip(void) {
_pam_shield_db_rec_t *record;
datum key, next_key, data;
int deleted = 0; /*If any key deleted, order changes; must revisit all keys*/
char ipbuf[INET6_ADDRSTRLEN];
key = gdbm_firstkey(dbf);
while (key.dptr != NULL) {
data = gdbm_fetch(dbf, key);
next_key = gdbm_nextkey(dbf, key);
if (data.dptr == NULL) {
logmsg(LOG_DEBUG, "cleaning up empty key");
if (!(options & OPT_DRYRUN)) {
gdbm_delete(dbf, key);
deleted = 1;
}
} else {
record = (_pam_shield_db_rec_t *)data.dptr;
print_ip(record, ipbuf, INET6_ADDRSTRLEN);
if (!strcmp(removeip, ipbuf)) {
logmsg(LOG_DEBUG, "remove entry: %s", ipbuf);
deleted = 1;
if (!(options & OPT_DRYRUN)) {
record->trigger_active = (time_t)0L;
run_trigger("del", record);
gdbm_delete(dbf, key);
}
}
free(data.dptr);
}
free(key.dptr);
key = next_key;
if (deleted && !key.dptr) {
if (!(options & OPT_DRYRUN)) {
key = gdbm_firstkey(dbf);
}
return 0;
}
}
logmsg(LOG_ERR, "not found: %s", removeip);
return 1;
}
int main(int argc, char **argv) {
int retval = 0;
init_module();
read_config();
get_options(argc, argv);
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 if (options & OPT_REMOVEIP)
retval = remove_ip();
else
purge_db();
gdbm_close(dbf);
deinit_module();
return retval;
}