File: pam_shield.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 (339 lines) | stat: -rw-r--r-- 9,950 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
/*
	pam_shield.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
*/
/*
	pam_shield is a PAM module that uses route/iptables to lock out script kiddies
	that probe your machine for open logins and/or easy guessable passwords.

	You can run this module with

	auth optional	pam_shield.so

	But just make sure it's not the only auth module you run..!
	This module does not do any authentication, it just monitors access.


	(btw, if you don't like my indentation, try setting tabsize to 4)
*/

#include "pam_shield.h"

#define PAM_SM_AUTH		1

#include <security/pam_modules.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;

	openlog("PAM-shield", LOG_PID, LOG_AUTH);

	va_start(varargs, fmt);
	vsyslog(level, fmt, varargs);
	va_end(varargs);

	closelog();
}

/*
	Mind that argv[0] is an argument, not the name of the module
*/
static void get_options(int argc, char **argv) {
int i;

	for(i = 0; i < argc; i++) {
		if (!strcmp(argv[i], "debug")) {
			options |= OPT_DEBUG;
			logmsg(LOG_DEBUG, "logging debug info");
			continue;
		}
		if (!strcmp(argv[i], "use_first_pass"))			/* Thorsten Kukuk sez all modules should accept this argument */
			continue;

		if (!strncmp(argv[i], "conf=", 5)) {
			conffile = argv[i] + 5;
			continue;
		}
		logmsg(LOG_ERR, "unknown argument '%s', ignored", argv[i]);
	}
}

static _pam_shield_db_rec_t *new_db_record(int window_size) {
_pam_shield_db_rec_t *record;
int size;

	if (window_size <= 0) {
		window_size = 1;
		size = sizeof(_pam_shield_db_rec_t);
	} else
		size = sizeof(_pam_shield_db_rec_t) + (window_size-1) * sizeof(time_t);

	if ((record = (_pam_shield_db_rec_t *)malloc(size)) == NULL) {
		logmsg(LOG_CRIT, "new_db_record(): out of memory allocating %d bytes", size);
		return NULL;
	}
	memset(record, 0, size);
	record->max_entries = window_size;
	return record;
}

static void destroy_db_record(_pam_shield_db_rec_t *record) {
	if (record != NULL)
		free(record);
}

/*
	get remote IPs for the rhost

	the return value must be freed with freeaddrinfo()
*/
static struct addrinfo *get_addr_info(char *rhost) {
struct addrinfo hints, *res;
int err;

	memset(&hints, 0, sizeof(struct addrinfo));
	hints.ai_family = PF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;

	if ((err = getaddrinfo(rhost, NULL, &hints, &res)) != 0) {
		logmsg(LOG_ERR, "%s: %s\n", rhost, gai_strerror(err));
		return NULL;
	}
	return res;
}

/*
	the authenticate function always returns PAM_IGNORE, because this
	module does not really authenticate
*/
PAM_EXTERN int pam_sm_authenticate(pam_handle_t *pamh, int flags, int argc, const char **argv) {
char *user, *rhost;
struct passwd *pwd;

	if (init_module())
		return PAM_IGNORE;

	get_options(argc, (char **)argv);
	logmsg(LOG_DEBUG, "this is version " PAM_SHIELD_VERSION);

	if (read_config()) {
		deinit_module();
		return PAM_IGNORE;
	}

/* get the username */
	if (pam_get_item(pamh, PAM_USER, (const void **)(void *)&user) != PAM_SUCCESS)
		user = NULL;

	if (user != NULL && !*user)
		user = NULL;

	logmsg(LOG_DEBUG, "user %s", (user == NULL) ? "(unknown)" : user);

/* get the remotehost address */
	if (pam_get_item(pamh, PAM_RHOST, (const void **)(void *)&rhost) != PAM_SUCCESS)
		rhost = NULL;

	if (rhost != NULL && !*rhost)
		rhost = NULL;

	logmsg(LOG_DEBUG, "remotehost %s", (rhost == NULL) ? "(unknown)" : rhost);

/*
	if rhost is completely numeric, then it has no DNS entry
*/
	if (strspn(rhost, "0123456789.") == strlen(rhost)
		|| strspn(rhost, "0123456789:abcdefABCDEF") == strlen(rhost)) {
		if (options & OPT_MISSING_DNS)
			logmsg(LOG_DEBUG, "missing DNS entry for %s (allowed)", rhost);
		else {
			logmsg(LOG_DEBUG, "missing DNS entry for %s (denied)", rhost);
			deinit_module();
			return PAM_AUTH_ERR;
		}
	} else {
/*
	see if this rhost is whitelisted
*/
		if (match_name_list(rhost)) {
			deinit_module();
			return PAM_IGNORE;
		}
	}

/* if not blocking all and the user is known, let go */
	if (!(options & OPT_BLOCK_ALL) && user != NULL && (pwd = getpwnam(user)) != NULL) {
		logmsg(LOG_DEBUG, "ignoring known user %s", user);
		deinit_module();
		return PAM_IGNORE;
	}
	if (rhost != NULL) {
		struct addrinfo *addr_info, *addr_p;
		unsigned char addr_family;
		char ipbuf[INET6_ADDRSTRLEN], *saddr;
		_pam_shield_db_rec_t *record;
		datum key, data;
		int whitelisted;

		if ((addr_info = get_addr_info(rhost)) == NULL) {		/* missing reverse DNS entry */
			deinit_module();

			if (options & OPT_MISSING_REVERSE)
				logmsg(LOG_DEBUG, "missing reverse DNS entry for %s (allowed)", rhost);
			else {
				logmsg(LOG_DEBUG, "missing reverse DNS entry for %s (denied)", rhost);
				deinit_module();
				return PAM_AUTH_ERR;
			}
		}
/* for every address that this host is known for, check for whitelist entry */
		for(addr_p = addr_info; addr_p != NULL; addr_p = addr_p->ai_next) {
			whitelisted = 0;
			switch(addr_p->ai_family) {
				case PF_INET:
					saddr = (char *)&((struct sockaddr_in *)(addr_p->ai_addr))->sin_addr.s_addr;

					if (match_ipv4_list((unsigned char *)saddr)) {
						logmsg(LOG_DEBUG, "remoteip %s (whitelisted)", inet_ntop(AF_INET, (char *)&((struct sockaddr_in *)(addr_p->ai_addr))->sin_addr, ipbuf, sizeof(ipbuf)));
						whitelisted = 1;
					} else
						logmsg(LOG_DEBUG, "remoteip %s", inet_ntop(AF_INET, (char *)&((struct sockaddr_in *)(addr_p->ai_addr))->sin_addr, ipbuf, sizeof(ipbuf)));
					break;
				
				case PF_INET6:
					saddr = (char *)&((struct sockaddr_in6 *)(addr_p->ai_addr))->sin6_addr.s6_addr;

					if (match_ipv6_list((unsigned char *)saddr)) {
						logmsg(LOG_DEBUG, "remoteip %s (whitelisted)", inet_ntop(AF_INET6, (char *)&((struct sockaddr_in6 *)(addr_p->ai_addr))->sin6_addr, ipbuf, sizeof(ipbuf)));
						whitelisted = 1;
					} else
						logmsg(LOG_DEBUG, "remoteip %s", inet_ntop(AF_INET6, (char *)&((struct sockaddr_in6 *)(addr_p->ai_addr))->sin6_addr, ipbuf, sizeof(ipbuf)));
					break;

				default:
					logmsg(LOG_DEBUG, "remoteip unknown (not IP)");

					freeaddrinfo(addr_info);
					deinit_module();
					return PAM_IGNORE;
			}
/* host is whitelisted by an allow line in the config file, so exit */
			if (whitelisted) {
				freeaddrinfo(addr_info);
				deinit_module();
				return PAM_IGNORE;
			}
		}
/* open the database */
		if ((dbf = gdbm_open(dbfile, 512, GDBM_WRCREAT, (mode_t)0600, fatal_func)) == NULL) {
			logmsg(LOG_ERR, "failed to open gdbm file '%s' : %s", dbfile, gdbm_strerror(gdbm_errno));
			freeaddrinfo(addr_info);
			deinit_module();
			return PAM_IGNORE;
		}
/* for every address that this host is known for, check the database */
		for(addr_p = addr_info; addr_p != NULL; addr_p = addr_p->ai_next) {
			whitelisted = 0;
			switch(addr_p->ai_family) {
				case PF_INET:
					addr_family = PAM_SHIELD_ADDR_IPV4;
					key.dptr = saddr = (char *)&((struct sockaddr_in *)(addr_p->ai_addr))->sin_addr.s_addr;
					key.dsize = sizeof(struct in_addr);
					break;

				case PF_INET6:
					addr_family = PAM_SHIELD_ADDR_IPV6;
					key.dptr = saddr = (char *)&((struct sockaddr_in6 *)(addr_p->ai_addr))->sin6_addr.s6_addr;
					key.dsize = sizeof(struct in6_addr);
					break;

				default:
					addr_family = -1;
					key.dptr = saddr = NULL;
					key.dsize = 0;
			}
			if (key.dptr == NULL)
				continue;

			data = gdbm_fetch(dbf, key);			/* get db record */
			if (data.dptr != NULL) {
				record = (_pam_shield_db_rec_t *)data.dptr;
/*
	Although this code does some expiration, it only does so for "this ip";
	it is still necessary to run an external database cleanup process every
	now and then (eg, from cron.daily)
*/
				expire_record(record);

				if (record->count >= record->max_entries) {		/* shift, so we always log the most recent time */
					memmove(record->timestamps, &record->timestamps[1], (record->max_entries-1)*sizeof(time_t));
					record->count--;
				}
				record->timestamps[record->count++] = this_time;

				logmsg(LOG_DEBUG, "%u times from %s", record->count, rhost);

/* too many in the interval, so trigger */
				if (!record->trigger_active && record->count >= max_conns) {
					record->trigger_active = this_time;
					run_trigger("add", record);
				}
			} else {
				if ((record = new_db_record(max_conns)) != NULL) {
					record->addr_family = addr_family;
					memcpy(record->ip.any, saddr, key.dsize);
					record->timestamps[record->count++] = this_time;

					logmsg(LOG_DEBUG, "putting new record in db");

					if (max_conns <= 1) {		/* (maybe) stupid, but possible */
						record->trigger_active = this_time;
						run_trigger("add", record);
					}
				}
			}
			if (record != NULL) {
				data.dptr = (char *)record;
				data.dsize = sizeof(_pam_shield_db_rec_t) + (record->max_entries-1)*sizeof(time_t);

/* key.dptr and key.dsize are still set to saddr and addr_size */

				if (gdbm_store(dbf, key, data, GDBM_REPLACE))
					logmsg(LOG_ERR, "failed to write db record");
			}
			destroy_db_record(record);
		}
		freeaddrinfo(addr_info);
		gdbm_close(dbf);
	}
	deinit_module();
	return PAM_IGNORE;
}

PAM_EXTERN int pam_sm_setcred(pam_handle_t *pamh, int flags, int argc, const char **argv) {
	return PAM_SUCCESS;
}

/* EOB */