6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Copyright 2004,2006 The FreeRADIUS server project
21 * Copyright 2004 Alan DeKok <aland@freeradius.org>
24 #include <freeradius-devel/ident.h>
27 #include <freeradius-devel/radiusd.h>
28 #include <freeradius-devel/modules.h>
35 * Define a structure for our module configuration.
37 * These variables do not need to be in a structure, but it's
38 * a lot cleaner to do so, and a pointer to the structure can
39 * be used as the instance handle.
41 typedef struct rlm_linelog_t {
47 * A mapping of configuration file names to internal variables.
49 * Note that the string is dynamically allocated, so it MUST
50 * be freed. When the configuration file parse re-reads the string,
51 * it free's the old one, and strdup's the new one, placing the pointer
52 * to the strdup'd string into 'config.string'. This gets around
55 static const CONF_PARSER module_config[] = {
56 { "filename", PW_TYPE_STRING_PTR,
57 offsetof(rlm_linelog_t,filename), NULL, NULL},
58 { "format", PW_TYPE_STRING_PTR,
59 offsetof(rlm_linelog_t,line), NULL, NULL},
60 { NULL, -1, 0, NULL, NULL } /* end the list */
64 static int linelog_detach(void *instance)
66 rlm_linelog_t *inst = instance;
74 * Instantiate the module.
76 static int linelog_instantiate(CONF_SECTION *conf, void **instance)
81 * Set up a storage area for instance data
83 inst = rad_malloc(sizeof(*inst));
84 memset(inst, 0, sizeof(*inst));
87 * If the configuration parameters can't be parsed, then
90 if (cf_section_parse(conf, inst, module_config) < 0) {
102 * Escape unprintable characters.
104 static int linelog_escape_func(char *out, int outlen, const char *in)
108 if (outlen == 0) return 0;
117 if (outlen <= 2) break;
124 if (outlen == 1) break;
132 if (outlen <= 2) break;
140 if (outlen <= 2) break;
148 if (outlen <= 4) break;
149 snprintf(out, outlen, "\\%03o", *in);
162 static int do_linelog(void *instance, REQUEST *request)
169 inst = (rlm_linelog_t*) instance;
172 * FIXME: Check length.
174 radius_xlat(buffer, sizeof(buffer), inst->filename, request, NULL);
176 fd = open(buffer, O_WRONLY | O_APPEND | O_CREAT, 0600);
178 radlog(L_ERR, "rlm_linelog: Failed to open %s: %s",
179 buffer, strerror(errno));
180 return RLM_MODULE_FAIL;
184 * FIXME: Check length.
186 radius_xlat(line, sizeof(line) - 1, inst->line, request,
187 linelog_escape_func);
190 write(fd, line, strlen(line));
193 return RLM_MODULE_OK;
198 * Externally visible module definition.
200 module_t rlm_linelog = {
203 RLM_TYPE_THREAD_SAFE, /* type */
204 linelog_instantiate, /* instantiation */
205 linelog_detach, /* detach */
207 do_linelog, /* authentication */
208 do_linelog, /* authorization */
209 do_linelog, /* preaccounting */
210 do_linelog, /* accounting */
211 NULL, /* checksimul */
212 do_linelog, /* pre-proxy */
213 do_linelog, /* post-proxy */
214 do_linelog /* post-auth */