~ubuntu-branches/ubuntu/jaunty/freeradius/jaunty-updates

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
/*
 * rlm_protocol_filter.c
 *
 * Version:	$Id$
 *
 *   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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 *
 * Copyright 2004  Cladju Consulting, Inc. <aland@cladju.com>
 * Copyright 2006  The FreeRADIUS server project
 */

#include <freeradius-devel/ident.h>
RCSID("$Id$")

#include <freeradius-devel/radiusd.h>
#include <freeradius-devel/modules.h>

/*
 *	Define a structure for our module configuration.
 *
 */
typedef struct rlm_protocol_filter_t {
	char		*filename;
	char		*key;
	CONF_SECTION	*cs;
} rlm_protocol_filter_t;

/*
 *	A mapping of configuration file names to internal variables.
 *
 *	Note that the string is dynamically allocated, so it MUST
 *	be freed.  When the configuration file parse re-reads the string,
 *	it free's the old one, and strdup's the new one, placing the pointer
 *	to the strdup'd string into 'config.string'.  This gets around
 *	buffer over-flows.
 */
static const CONF_PARSER module_config[] = {
	{ "filename",  PW_TYPE_FILENAME,
	  offsetof(rlm_protocol_filter_t,filename), NULL,
	  "${raddbdir}/protocol_filter.conf"},

	{ "key",  PW_TYPE_STRING_PTR,
	  offsetof(rlm_protocol_filter_t,key), NULL, "%{Realm:-DEFAULT}"},

	{ NULL, -1, 0, NULL, NULL }		/* end the list */
};

static int filter_detach(void *instance)
{
	rlm_protocol_filter_t *inst = instance;

	if (inst->cs) cf_section_free(&(inst->cs));

	free(instance);
	return 0;
}


/*
 *	Do any per-module initialization that is separate to each
 *	configured instance of the module.  e.g. set up connections
 *	to external databases, read configuration files, set up
 *	dictionary entries, etc.
 *
 *	If configuration information is given in the config section
 *	that must be referenced in later calls, store a handle to it
 *	in *instance otherwise put a null pointer there.
 */
static int filter_instantiate(CONF_SECTION *conf, void **instance)
{
	rlm_protocol_filter_t *inst;

	/*
	 *	Set up a storage area for instance data
	 */
	inst = rad_malloc(sizeof(*inst));
	if (!inst) {
		return -1;
	}
	memset(inst, 0, sizeof(*inst));

	/*
	 *	If the configuration parameters can't be parsed, then
	 *	fail.
	 */
	if (cf_section_parse(conf, inst, module_config) < 0) {
		filter_detach(inst);
		return -1;
	}

	inst->cs = cf_file_read(inst->filename);
	if (!inst->cs) {
		filter_detach(inst);
		return -1;
	}

	*instance = inst;

	return 0;
}


/*
 *	Return permission.
 */
static int str2sense(const char *str)
{
	if (strcasecmp(str, "permit") == 0) return 1;
	if (strcasecmp(str, "deny") == 0) return 0;

	return -1;
}

/*
 *	Apply a subsection to a request.
 *	Returns permit/deny/error.
 */
static int apply_subsection(rlm_protocol_filter_t *inst, REQUEST *request,
			    CONF_SECTION *cs, const char *name)
{
	int sense;
	CONF_PAIR *cp;
	const char *value;
	char keybuf[256];

	DEBUG2("  rlm_protocol_filter: Found subsection %s", name);

	cp = cf_pair_find(cs, "key");
	if (!cp) {
		radlog(L_ERR, "rlm_protocol_filter: %s[%d]: No key defined in subsection %s",
		       inst->filename, cf_section_lineno(cs), name);
		return RLM_MODULE_FAIL;
	}

	radius_xlat(keybuf, sizeof(keybuf),
		    cf_pair_value(cp), request, NULL);
	if (!*keybuf) {
		DEBUG2("  rlm_protocol_filter: %s[%d]: subsection %s, key is empty, doing nothing.",
		       inst->filename, cf_section_lineno(cs), name);
		return RLM_MODULE_NOOP;
	}

	DEBUG2("  rlm_protocol_filter: %s[%d]: subsection %s, using key %s",
	       inst->filename, cf_section_lineno(cs), name, keybuf);

	/*
	 *	And repeat some of the above code.
	 */
	cp = cf_pair_find(cs, keybuf);
	if (!cp) {
		CONF_SECTION *subcs;

		/*
		 *	Maybe it has a subsection, too.
		 */
		subcs = cf_section_sub_find(cs, keybuf);
		if (subcs) {
			return apply_subsection(inst, request, subcs, keybuf);
		} /* it was a subsection */



		DEBUG2("  rlm_protocol_filter: %s[%d]: subsection %s, rule not found, doing nothing.",
		       inst->filename, cf_section_lineno(cs), name);
		return RLM_MODULE_NOOP;
	}

	value = cf_pair_value(cp);
	sense = str2sense(value);
	if (sense < 0) {
		radlog(L_ERR, "rlm_protocol_filter: %s[%d]: Unknwn directive %s",
		       inst->filename, cf_pair_lineno(cp), value);
		return RLM_MODULE_FAIL;
	}

	if (!sense) return RLM_MODULE_REJECT;

	return RLM_MODULE_OK;
}


/*
 *	Authorize the user.
 */
static int filter_authorize(void *instance, REQUEST *request)
{
	int sense;
	VALUE_PAIR *vp;
	CONF_SECTION *cs;
	CONF_PAIR *cp;
	char keybuf[1024];
	rlm_protocol_filter_t *inst = instance;

	radius_xlat(keybuf, sizeof(keybuf), inst->key, request, NULL);
	if (!*keybuf) {
		DEBUG2("  rlm_protocol_filter: key is empty");
		return RLM_MODULE_NOOP;
	}
	DEBUG2("  rlm_protocol_filter: Using key %s", keybuf);

	cs = cf_section_sub_find(inst->cs, keybuf);
	if (!cs) {
		DEBUG2("  rlm_protocol_filter: No such key in %s", inst->filename);
		return RLM_MODULE_NOTFOUND;
	}

	/*
	 *	Walk through the list of attributes, seeing if they're
	 *	permitted/denied.
	 */
	for (vp = request->packet->vps; vp != NULL; vp = vp->next) {
		const char *value;
		CONF_SECTION *subcs;

		cp = cf_pair_find(cs, vp->name);
		if (cp) {
			value = cf_pair_value(cp);

			sense = str2sense(value);
			if (sense < 0) {
				radlog(L_ERR, "rlm_protocol_filter %s[%d]: Unknown directive %s",
				       inst->filename,
				       cf_pair_lineno(cp),
				       value);
				return RLM_MODULE_FAIL;
			}

			if (!sense) return RLM_MODULE_REJECT;
			continue; /* was permitted */
		} /* else no pair was found */

		/*
		 *	Maybe it has a subsection
		 */
		subcs = cf_section_sub_find(cs, vp->name);
		if (subcs) {
			sense = apply_subsection(inst, request, subcs, vp->name);
			if ((sense == RLM_MODULE_OK) ||
			    (sense == RLM_MODULE_NOOP)) {
				continue;
			}

			return sense;
		} /* it was a subsection */

		/*
		 *	Not found, must be "permit"
		 */
	}

	return RLM_MODULE_OK;
}


/*
 *	The module name should be the only globally exported symbol.
 *	That is, everything else should be 'static'.
 *
 *	If the module needs to temporarily modify it's instantiation
 *	data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
 *	The server will then take care of ensuring that the module
 *	is single-threaded.
 */
module_t rlm_protocol_filter = {
	RLM_MODULE_INIT,
	"protocol_filter",
	RLM_TYPE_THREAD_SAFE,		/* type */
	filter_instantiate,		/* instantiation */
	filter_detach,			/* detach */
	{
		NULL,			/* authentication */
		filter_authorize,	/* authorization */
		NULL,			/* preaccounting */
		NULL,			/* accounting */
		NULL,			/* checksimul */
		NULL,			/* pre-proxy */
		NULL,			/* post-proxy */
		NULL			/* post-auth */
	},
};