summaryrefslogtreecommitdiffstats
path: root/src/modules/rlm_sql_map/rlm_sql_map.c
blob: b6a27e50db0537b57ea85b644c0441a0a938cd5b (plain)
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
/*
 *   This program is 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
 */

/**
 * $Id$
 * @file rlm_sql_map.c
 * @brief Tracks data usage and other counters using SQL.
 *
 * @copyright 2021  The FreeRADIUS server project
 * @copyright 2021  Alan DeKok <aland@ox.org>
 */
RCSID("$Id$")

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

#include <ctype.h>

#include <rlm_sql.h>

#define MAX_QUERY_LEN 2048

typedef struct rlm_sql_map_t {
	char const	*sql_instance_name;	//!< Instance of SQL module to use,
						//!< usually just 'sql'.
	bool		multiple_rows;		//!< Process all rows creating an attr[*] array

	char const	*query;			//!< SQL query to retrieve current

	rlm_sql_t	*sql_inst;

	CONF_SECTION	*cs;

	/*
	 *	SQL columns to RADIUS stuff
	 */
	vp_map_t	*user_map;
} rlm_sql_map_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[] = {
	{ "sql_module_instance", FR_CONF_OFFSET(PW_TYPE_STRING | PW_TYPE_REQUIRED, rlm_sql_map_t, sql_instance_name), NULL },
	{ "multiple_rows", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, rlm_sql_map_t, multiple_rows), "no" },
	{ "query", FR_CONF_OFFSET(PW_TYPE_STRING | PW_TYPE_XLAT | PW_TYPE_REQUIRED | PW_TYPE_NOT_EMPTY, rlm_sql_map_t, query), NULL },

	CONF_PARSER_TERMINATOR
};

#define SQL_MAX_ATTRMAP (128)

static int sql_map_verify(vp_map_t *map, UNUSED void *instance)
{
	/*
	 *	Destinations where we can put the VALUE_PAIRs we
	 *	create using SQL values.
	 */
	switch (map->lhs->type) {
	case TMPL_TYPE_ATTR:
		break;

	case TMPL_TYPE_ATTR_UNDEFINED:
		cf_log_err(map->ci, "Unknown attribute %s", map->lhs->tmpl_unknown_name);
		return -1;

	default:
		cf_log_err(map->ci, "Left hand side of map must be an attribute, not a %s",
			   fr_int2str(tmpl_names, map->lhs->type, "<INVALID>"));
		return -1;
	}

	/*
	 *	The RHS MUST be only a column number.
	 */
	switch (map->rhs->type) {
	case TMPL_TYPE_LITERAL:
	case TMPL_TYPE_DATA:
		if (tmpl_cast_in_place(map->rhs, PW_TYPE_INTEGER, NULL) < 0) {
			cf_log_err(map->ci, "Failed parsing right hand side of map as an integer.");
			return -1;
		}

		if (map->rhs->tmpl_data_value.integer > SQL_MAX_ATTRMAP) {
			cf_log_err(map->ci, "Column number %u is larger than allowed maximum %u",
				map->rhs->tmpl_data_value.integer, SQL_MAX_ATTRMAP);
			return -1;
		}
		break;

	default:
		cf_log_err(map->ci, "Right hand side of map must be a column number, not a %s",
			   fr_int2str(tmpl_names, map->rhs->type, "<INVALID>"));
		return -1;
	}

	/*
	 *	Only =, :=, += and -= operators are supported for SQL mappings.
	 */
	switch (map->op) {
	case T_OP_SET:
	case T_OP_EQ:
	case T_OP_SUB:
	case T_OP_ADD:
		break;

	default:
		cf_log_err(map->ci, "Operator \"%s\" not allowed for SQL mappings",
			   fr_int2str(fr_tokens, map->op, "<INVALID>"));
		return -1;
	}

	return 0;
}

typedef struct sql_map_row_s {
	int		num_columns;
	char		**row;
} sql_map_row_t;


/** Callback for map_to_request
 *
 * Performs exactly the same job as map_to_vp, but pulls attribute values from SQL entries
 *
 * @see map_to_vp
 */
static int sql_map_getvalue(TALLOC_CTX *ctx, VALUE_PAIR **out, REQUEST *request, vp_map_t const *map, void *uctx)
{
	VALUE_PAIR	*head = NULL, *vp;
	int		column;
	sql_map_row_t	*data = uctx;
	char		*value;
	vp_cursor_t	cursor;

	*out = NULL;
	fr_cursor_init(&cursor, &head);

	switch (map->lhs->type) {
	/*
	 *	Iterate over all the retrieved values,
	 *	don't try and be clever about changing operators
	 *	just use whatever was set in the attribute map.
	 */
	case TMPL_TYPE_ATTR:
		fr_assert(map->rhs->type == TMPL_TYPE_DATA);
		fr_assert(map->rhs->tmpl_data_type == PW_TYPE_INTEGER);

		column = map->rhs->tmpl_data_value.integer;
		if (column >= data->num_columns) {
			RWDEBUG("Ignoring source column number %u, as it is larger than the number of returned columns %d",
				column, data->num_columns);
			return 0;
		}

		if (!data->row[column]) {
			RWDEBUG("Ignoring source column number %u - it is empty", column);
			return 0;
		}
		
		value = data->row[column];

		vp = fr_pair_afrom_da(ctx, map->lhs->tmpl_da);
		rad_assert(vp);

		if (fr_pair_value_from_str(vp, value, -1) < 0) {
			char *escaped;

			escaped = fr_aprints(vp, value, -1, '"');
			RWDEBUG("Failed parsing value \"%s\" for attribute %s: %s", escaped,
				map->lhs->tmpl_da->name, fr_strerror());
			talloc_free(vp); /* also frees escaped */
			break;
		}

		vp->op = map->op;
		fr_cursor_insert(&cursor, vp);
		break;

	default:
		rad_assert(0);
	}

	*out = head;

	return 0;
}


/** Convert attribute map into valuepairs
 *
 * Use the attribute map built earlier to convert SQL values into valuepairs and insert them into whichever
 * list they need to go into.
 *
 * This is *NOT* atomic, but there's no condition for which we should error out...
 *
 * @param[in] inst module configuration.
 * @param[in] request Current request.
 * @param[in] handle associated with entry.
 * @return
 *	- Number of maps successfully applied.
 *	- -1 on failure.
 */
static int sql_map_do(const rlm_sql_map_t *inst, REQUEST *request, rlm_sql_handle_t **handle)
{
	vp_map_t const		*map;
	int			applied = 0;	/* How many maps have been applied to the current request */
	sql_map_row_t		ctx;

	/*
	 *	Cache all of the rows in a simple array.
	 */
	while ((inst->sql_inst->module->sql_fetch_row)(*handle, inst->sql_inst->config) == RLM_SQL_OK) {
#ifdef __clang_analyzer__
		if (!*handle) return -1; /* only true when return code is not RLM_SQL_OK */
#endif

		ctx.row = (*handle)->row;
		ctx.num_columns = (inst->sql_inst->module->sql_num_fields)(*handle, inst->sql_inst->config);

		if (applied >= 1 && !inst->multiple_rows) {
			RWDEBUG("Ignoring multiple rows. Enable the option 'multiple_rows' if you need multiple rows.");
			break;
		}

		for (map = inst->user_map; map != NULL; map = map->next) {
			/*
			 *	If something bad happened, just skip, this is probably
			 *	a case of the dst being incorrect for the current
			 *	request context
			 */
			if (map_to_request(request, map, sql_map_getvalue, &ctx) < 0) {
				return -1;	/* Fail */
			}
		}

		applied++;
	}

	return applied;
}

/*
 *	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 mod_instantiate(CONF_SECTION *conf, void *instance)
{
	rlm_sql_map_t *inst = instance;
	module_instance_t *sql_inst;
	CONF_SECTION *update;

	sql_inst = module_instantiate(cf_section_find("modules"),
					inst->sql_instance_name);
	if (!sql_inst) {
		cf_log_err_cs(conf, "Failed to find sql instance named %s",
			   inst->sql_instance_name);
		return -1;
	}
	inst->sql_inst = (rlm_sql_t *)sql_inst->insthandle;

	inst->cs = conf;

	/*
	 *	Build the attribute map
	 */
	update = cf_section_sub_find(inst->cs, "update");
	if (!update) {
		cf_log_err_cs(conf, "Failed to find 'update' section");
		return -1;
	}

	if (map_afrom_cs(&inst->user_map, update,
			 PAIR_LIST_REPLY, PAIR_LIST_REQUEST, sql_map_verify, inst,
			 SQL_MAX_ATTRMAP) < 0) {
		return -1;
	}

	return 0;
}

static int mod_bootstrap(CONF_SECTION *conf, void *instance)
{
	rlm_sql_map_t *inst = instance;
	char const *p = inst->query;

	if (!p || !*p) {
		cf_log_err_cs(conf, "'query' cannot be empty");
		return -1;
	}

	while (isspace((uint8_t) *p)) p++;

	if ((strncasecmp(p, "insert", 6) == 0) ||
	    (strncasecmp(p, "update", 6) == 0) ||
	    (strncasecmp(p, "delete", 6) == 0)) {
		cf_log_err_cs(conf, "'query' MUST be 'SELECT ...', not 'INSERT', 'UPDATE', or 'DELETE'");
		return -1;
	}

	return 0;
}


/** Detach from the SQL server and cleanup internal state.
 *
 */
static int mod_detach(void *instance)
{
	rlm_sql_map_t *inst = instance;

	talloc_free(inst->user_map);

	return 0;
}


/*
 *	Find the named user in this modules database.  Create the set
 *	of attribute-value pairs to check and reply with for this user
 *	from the database. The authentication code only needs to check
 *	the password, the rest is done here.
 */
static rlm_rcode_t CC_HINT(nonnull) mod_map(void *instance, REQUEST *request)
{
	int res;
	rlm_rcode_t rcode = RLM_MODULE_NOOP;
	char *query;
	rlm_sql_map_t *inst = instance;
	rlm_sql_handle_t *handle;

	handle = fr_connection_get(inst->sql_inst->pool);
	if (!handle) {
		REDEBUG("Failed reserving SQL connection");
		return RLM_MODULE_FAIL;
	}

	if (inst->sql_inst->sql_set_user(inst->sql_inst, request, NULL) < 0) {
		return RLM_MODULE_FAIL;
	}

	if (radius_axlat(&query, request, inst->query, inst->sql_inst->sql_escape_func, handle) < 0) {
		return RLM_MODULE_FAIL;
	}

	res = inst->sql_inst->sql_select_query(inst->sql_inst, request, &handle, query);
	talloc_free(query);
	if (res != RLM_SQL_OK) {
		if (handle) fr_connection_release(inst->sql_inst->pool, handle);

		return RLM_MODULE_FAIL;
	}

	fr_assert(handle != NULL);

	if (sql_map_do(inst, request, &handle) > 0) rcode = RLM_MODULE_UPDATED;

	if (handle) {
		(inst->sql_inst->module->sql_finish_query)(handle, inst->sql_inst->config);

		fr_connection_release(inst->sql_inst->pool, handle);
	}

	return rcode;
}

/*
 *	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.
 */
extern module_t rlm_sql_map;
module_t rlm_sql_map = {
	.magic		= RLM_MODULE_INIT,
	.name		= "sqlcounter",
	.type		= RLM_TYPE_THREAD_SAFE,
	.inst_size	= sizeof(rlm_sql_map_t),
	.config		= module_config,
	.bootstrap	= mod_bootstrap,
	.instantiate	= mod_instantiate,
	.detach		= mod_detach,
	.methods = {
		[MOD_AUTHENTICATE]	= mod_map,
		[MOD_AUTHORIZE]		= mod_map,
		[MOD_PREACCT]		= mod_map,
		[MOD_ACCOUNTING]	= mod_map,
		[MOD_PRE_PROXY]		= mod_map,
		[MOD_POST_PROXY]	= mod_map,
		[MOD_POST_AUTH]		= mod_map,
#ifdef WITH_COA
		[MOD_RECV_COA]		= mod_map,
		[MOD_SEND_COA]		= mod_map
#endif
	},
};