2 * rlm_sql_log.c Append the SQL queries in a log file which
3 * is read later by the radsqlrelay program
7 * Author: Nicolas Baradakis <nicolas.baradakis@cegetel.net>
9 * Copyright (C) 2005 Cegetel
10 * Copyright 2006 The FreeRADIUS server project
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include <freeradius-devel/ident.h>
30 #include <freeradius-devel/radiusd.h>
31 #include <freeradius-devel/modules.h>
36 static int sql_log_instantiate(CONF_SECTION *conf, void **instance);
37 static int sql_log_detach(void *instance);
38 static int sql_log_accounting(void *instance, REQUEST *request);
39 static int sql_log_postauth(void *instance, REQUEST *request);
41 #define MAX_QUERY_LEN 4096
44 * Define a structure for our module configuration.
46 typedef struct rlm_sql_log_t {
52 CONF_SECTION *conf_section;
56 * A mapping of configuration file names to internal variables.
58 static const CONF_PARSER module_config[] = {
59 {"path", PW_TYPE_STRING_PTR,
60 offsetof(rlm_sql_log_t,path), NULL, "${radacctdir}/sql-relay"},
61 {"Post-Auth", PW_TYPE_STRING_PTR,
62 offsetof(rlm_sql_log_t,postauth_query), NULL, ""},
63 {"sql_user_name", PW_TYPE_STRING_PTR,
64 offsetof(rlm_sql_log_t,sql_user_name), NULL, ""},
65 {"safe-characters", PW_TYPE_STRING_PTR,
66 offsetof(rlm_sql_log_t,allowed_chars), NULL,
67 "@abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_: /"},
69 { NULL, -1, 0, NULL, NULL } /* end the list */
72 static char *allowed_chars = NULL;
75 * Do any per-module initialization that is separate to each
76 * configured instance of the module. e.g. set up connections
77 * to external databases, read configuration files, set up
78 * dictionary entries, etc.
80 * If configuration information is given in the config section
81 * that must be referenced in later calls, store a handle to it
82 * in *instance otherwise put a null pointer there.
84 static int sql_log_instantiate(CONF_SECTION *conf, void **instance)
90 * Set up a storage area for instance data.
92 inst = calloc(1, sizeof(rlm_sql_log_t));
94 radlog(L_ERR, "rlm_sql_log: Not enough memory");
99 * Get the name of the current section in the conf file.
101 name = cf_section_name2(conf);
103 name = cf_section_name1(conf);
106 inst->name = strdup(name);
109 * If the configuration parameters can't be parsed,
112 if (cf_section_parse(conf, inst, module_config) < 0) {
113 radlog(L_ERR, "rlm_sql_log (%s): Unable to parse parameters",
115 sql_log_detach(inst);
119 inst->conf_section = conf;
120 allowed_chars = inst->allowed_chars;
126 * Say goodbye to the cruel world.
128 static int sql_log_detach(void *instance)
132 rlm_sql_log_t *inst = (rlm_sql_log_t *)instance;
140 * Free up dynamically allocated string pointers.
142 for (i = 0; module_config[i].name != NULL; i++) {
143 if (module_config[i].type != PW_TYPE_STRING_PTR) {
148 * Treat 'config' as an opaque array of bytes,
149 * and take the offset into it. There's a
150 * (char*) pointer at that offset, and we want
153 p = (char **) (((char *)inst) + module_config[i].offset);
154 if (!*p) { /* nothing allocated */
160 allowed_chars = NULL;
166 * Translate the SQL queries.
168 static int sql_escape_func(char *out, int outlen, const char *in)
174 * Non-printable characters get replaced with their
175 * mime-encoded equivalents.
178 strchr(allowed_chars, *in) == NULL) {
180 * Only 3 or less bytes available.
186 snprintf(out, outlen, "=%02X", (unsigned char) in[0]);
195 * Only one byte left.
215 * Add the 'SQL-User-Name' attribute to the packet.
217 static int sql_set_user(rlm_sql_log_t *inst, REQUEST *request, char *sqlusername, const char *username)
220 char tmpuser[MAX_STRING_LEN];
223 sqlusername[0] = '\0';
225 /* Remove any user attr we added previously */
226 pairdelete(&request->packet->vps, PW_SQL_USER_NAME);
228 if (username != NULL) {
229 strlcpy(tmpuser, username, MAX_STRING_LEN);
230 } else if (inst->sql_user_name[0] != '\0') {
231 radius_xlat(tmpuser, sizeof(tmpuser), inst->sql_user_name,
237 if (tmpuser[0] != '\0') {
238 strlcpy(sqlusername, tmpuser, sizeof(tmpuser));
239 DEBUG2("rlm_sql_log (%s): sql_set_user escaped user --> '%s'",
240 inst->name, sqlusername);
241 vp = pairmake("SQL-User-Name", sqlusername, 0);
243 radlog(L_ERR, "%s", librad_errstr);
247 pairadd(&request->packet->vps, vp);
254 * Replace %<whatever> in the query.
256 static int sql_xlat_query(rlm_sql_log_t *inst, REQUEST *request, const char *query, char *xlat_query, size_t len)
258 char sqlusername[MAX_STRING_LEN];
260 /* If query is not defined, we stop here */
261 if (query[0] == '\0')
262 return RLM_MODULE_NOOP;
264 /* Add attribute 'SQL-User-Name' */
265 if (sql_set_user(inst, request, sqlusername, NULL) <0) {
266 radlog(L_ERR, "rlm_sql_log (%s): Couldn't add SQL-User-Name attribute",
268 return RLM_MODULE_FAIL;
271 /* Expand variables in the query */
272 xlat_query[0] = '\0';
273 radius_xlat(xlat_query, len, query, request, sql_escape_func);
274 if (xlat_query[0] == '\0') {
275 radlog(L_ERR, "rlm_sql_log (%s): Couldn't xlat the query %s",
277 return RLM_MODULE_FAIL;
280 return RLM_MODULE_OK;
284 * The Perl version of radsqlrelay uses fcntl locks.
286 static int setlock(int fd)
289 memset(&fl, 0, sizeof(fl));
293 fl.l_whence = SEEK_SET;
294 return fcntl(fd, F_SETLKW, &fl);
298 * Write the line into file (with lock)
300 static int sql_log_write(rlm_sql_log_t *inst, REQUEST *request, const char *line)
306 char path[MAX_STRING_LEN];
309 radius_xlat(path, sizeof(path), inst->path, request, NULL);
311 return RLM_MODULE_FAIL;
314 if ((fd = open(path, O_WRONLY | O_APPEND | O_CREAT, 0666)) < 0) {
315 radlog(L_ERR, "rlm_sql_log (%s): Couldn't open file %s: %s",
316 inst->name, path, strerror(errno));
317 return RLM_MODULE_FAIL;
319 if (setlock(fd) != 0) {
320 radlog(L_ERR, "rlm_sql_log (%s): Couldn't lock file %s: %s",
321 inst->name, path, strerror(errno));
323 return RLM_MODULE_FAIL;
325 if (fstat(fd, &st) != 0) {
326 radlog(L_ERR, "rlm_sql_log (%s): Couldn't stat file %s: %s",
327 inst->name, path, strerror(errno));
329 return RLM_MODULE_FAIL;
331 if (st.st_nlink == 0) {
332 DEBUG("rlm_sql_log (%s): File %s removed by another program, retrying",
340 if ((fp = fdopen(fd, "a")) == NULL) {
341 radlog(L_ERR, "rlm_sql_log (%s): Couldn't associate a stream with file %s: %s",
342 inst->name, path, strerror(errno));
344 return RLM_MODULE_FAIL;
348 fclose(fp); /* and unlock */
349 return RLM_MODULE_OK;
353 * Write accounting information to this module's database.
355 static int sql_log_accounting(void *instance, REQUEST *request)
358 char querystr[MAX_QUERY_LEN];
360 rlm_sql_log_t *inst = (rlm_sql_log_t *)instance;
365 DEBUG("rlm_sql_log (%s): Processing sql_log_accounting", inst->name);
367 /* Find the Acct Status Type. */
368 if ((pair = pairfind(request->packet->vps, PW_ACCT_STATUS_TYPE)) == NULL) {
369 radlog(L_ERR, "rlm_sql_log (%s): Packet has no account status type",
371 return RLM_MODULE_INVALID;
374 /* Search the query in conf section of the module */
375 if ((dval = dict_valbyattr(PW_ACCT_STATUS_TYPE, pair->lvalue)) == NULL) {
376 radlog(L_ERR, "rlm_sql_log (%s): Unsupported Acct-Status-Type = %d",
377 inst->name, pair->lvalue);
378 return RLM_MODULE_NOOP;
380 if ((cp = cf_pair_find(inst->conf_section, dval->name)) == NULL) {
381 DEBUG("rlm_sql_log (%s): Couldn't find an entry %s in the config section",
382 inst->name, dval->name);
383 return RLM_MODULE_NOOP;
385 cfquery = cf_pair_value(cp);
388 ret = sql_xlat_query(inst, request, cfquery, querystr, sizeof(querystr));
389 if (ret != RLM_MODULE_OK)
392 /* Write query into sql-relay file */
393 return sql_log_write(inst, request, querystr);
397 * Write post-auth information to this module's database.
399 static int sql_log_postauth(void *instance, REQUEST *request)
402 char querystr[MAX_QUERY_LEN];
403 rlm_sql_log_t *inst = (rlm_sql_log_t *)instance;
405 DEBUG("rlm_sql_log (%s): Processing sql_log_postauth", inst->name);
408 ret = sql_xlat_query(inst, request, inst->postauth_query,
409 querystr, sizeof(querystr));
410 if (ret != RLM_MODULE_OK)
413 /* Write query into sql-relay file */
414 return sql_log_write(inst, request, querystr);
418 * The module name should be the only globally exported symbol.
419 * That is, everything else should be 'static'.
421 * If the module needs to temporarily modify it's instantiation
422 * data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
423 * The server will then take care of ensuring that the module
424 * is single-threaded.
426 module_t rlm_sql_log = {
429 RLM_TYPE_THREAD_SAFE, /* type */
430 sql_log_instantiate, /* instantiation */
431 sql_log_detach, /* detach */
433 NULL, /* authentication */
434 NULL, /* authorization */
435 NULL, /* preaccounting */
436 sql_log_accounting, /* accounting */
437 NULL, /* checksimul */
438 NULL, /* pre-proxy */
439 NULL, /* post-proxy */
440 sql_log_postauth /* post-auth */