Merge pull request #1728 from jrouzierinverse/feature/mschap-cisco
[freeradius.git] / src / modules / rlm_linelog / rlm_linelog.c
1 /*
2  * rlm_linelog.c
3  *
4  * Version:     $Id$
5  *
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.
10  *
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.
15  *
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
19  *
20  * Copyright 2004,2006  The FreeRADIUS server project
21  * Copyright 2004  Alan DeKok <aland@freeradius.org>
22  */
23
24 RCSID("$Id$")
25
26 #include <freeradius-devel/radiusd.h>
27 #include <freeradius-devel/modules.h>
28 #include <freeradius-devel/rad_assert.h>
29 #include <freeradius-devel/exfile.h>
30
31 #ifdef HAVE_FCNTL_H
32 #include <fcntl.h>
33 #endif
34
35 #ifdef HAVE_UNISTD_H
36 #include <unistd.h>
37 #endif
38
39 #ifdef HAVE_GRP_H
40 #include <grp.h>
41 #endif
42
43 #ifdef HAVE_SYSLOG_H
44 #include <syslog.h>
45
46 #ifndef LOG_INFO
47 #define LOG_INFO (0)
48 #endif
49 #endif
50
51 /*
52  *      Define a structure for our module configuration.
53  */
54 typedef struct rlm_linelog_t {
55         CONF_SECTION    *cs;
56         char const      *filename;
57
58         bool            escape;                 //!< do filename escaping, yes / no
59
60         xlat_escape_t escape_func;      //!< escape function
61
62         char const      *syslog_facility;       //!< Syslog facility string.
63         char const      *syslog_severity;       //!< Syslog severity string.
64         int             syslog_priority;        //!< Bitwise | of severity and facility.
65
66         uint32_t        permissions;
67         char const      *group;
68         char const      *line;
69         char const      *reference;
70         exfile_t        *ef;
71 } rlm_linelog_t;
72
73 /*
74  *      A mapping of configuration file names to internal variables.
75  *
76  *      Note that the string is dynamically allocated, so it MUST
77  *      be freed.  When the configuration file parse re-reads the string,
78  *      it free's the old one, and strdup's the new one, placing the pointer
79  *      to the strdup'd string into 'config.string'.  This gets around
80  *      buffer over-flows.
81  */
82 static const CONF_PARSER module_config[] = {
83         { "filename", FR_CONF_OFFSET(PW_TYPE_FILE_OUTPUT | PW_TYPE_REQUIRED | PW_TYPE_XLAT, rlm_linelog_t, filename), NULL },
84         { "escape_filenames", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, rlm_linelog_t, escape), "no" },
85         { "syslog_facility", FR_CONF_OFFSET(PW_TYPE_STRING, rlm_linelog_t, syslog_facility), NULL },
86         { "syslog_severity", FR_CONF_OFFSET(PW_TYPE_STRING, rlm_linelog_t, syslog_severity), "info" },
87         { "permissions", FR_CONF_OFFSET(PW_TYPE_INTEGER, rlm_linelog_t, permissions), "0600" },
88         { "group", FR_CONF_OFFSET(PW_TYPE_STRING, rlm_linelog_t, group), NULL },
89         { "format", FR_CONF_OFFSET(PW_TYPE_STRING | PW_TYPE_XLAT, rlm_linelog_t, line), NULL },
90         { "reference", FR_CONF_OFFSET(PW_TYPE_STRING | PW_TYPE_XLAT, rlm_linelog_t, reference), NULL },
91         CONF_PARSER_TERMINATOR
92 };
93
94
95 /*
96  *      Instantiate the module.
97  */
98 static int mod_instantiate(CONF_SECTION *conf, void *instance)
99 {
100         rlm_linelog_t *inst = instance;
101         int num;
102
103         if (!inst->filename) {
104                 cf_log_err_cs(conf, "No value provided for 'filename'");
105                 return -1;
106         }
107
108         /*
109          *      Escape filenames only if asked.
110          */
111         if (inst->escape) {
112                 inst->escape_func = rad_filename_escape;
113         } else {
114                 inst->escape_func = rad_filename_make_safe;
115         }
116
117 #ifndef HAVE_SYSLOG_H
118         if (strcmp(inst->filename, "syslog") == 0) {
119                 cf_log_err_cs(conf, "Syslog output is not supported on this system");
120                 return -1;
121         }
122 #else
123
124         if (inst->syslog_facility) {
125                 num = fr_str2int(syslog_facility_table, inst->syslog_facility, -1);
126                 if (num < 0) {
127                         cf_log_err_cs(conf, "Invalid syslog facility \"%s\"", inst->syslog_facility);
128                         return -1;
129                 }
130
131                 inst->syslog_priority |= num;
132         }
133
134         num = fr_str2int(syslog_severity_table, inst->syslog_severity, -1);
135         if (num < 0) {
136                 cf_log_err_cs(conf, "Invalid syslog severity \"%s\"", inst->syslog_severity);
137                 return -1;
138         }
139         inst->syslog_priority |= num;
140 #endif
141
142         if (!inst->line && !inst->reference) {
143                 cf_log_err_cs(conf, "Must specify a log format, or reference");
144                 return -1;
145         }
146
147         inst->ef = exfile_init(inst, 256, 30, true);
148         if (!inst->ef) {
149                 cf_log_err_cs(conf, "Failed creating log file context");
150                 return -1;
151         }
152
153         inst->cs = conf;
154         return 0;
155 }
156
157
158 /*
159  *      Escape unprintable characters.
160  */
161 static size_t linelog_escape_func(UNUSED REQUEST *request,
162                 char *out, size_t outlen, char const *in,
163                 UNUSED void *arg)
164 {
165         if (outlen == 0) return 0;
166
167         if (outlen == 1) {
168                 *out = '\0';
169                 return 0;
170         }
171
172         return fr_prints(out, outlen, in, -1, 0);
173 }
174
175 static rlm_rcode_t CC_HINT(nonnull) mod_do_linelog(void *instance, REQUEST *request)
176 {
177         int fd = -1;
178         rlm_linelog_t *inst = (rlm_linelog_t*) instance;
179         char const *value = inst->line;
180
181 #ifdef HAVE_GRP_H
182         gid_t gid;
183         char *endptr;
184 #endif
185         char path[2048];
186         char line[4096];
187
188         line[0] = '\0';
189
190         if (inst->reference) {
191                 CONF_ITEM *ci;
192                 CONF_PAIR *cp;
193
194                 if (radius_xlat(line + 1, sizeof(line) - 1, request, inst->reference, linelog_escape_func, NULL) < 0) {
195                         return RLM_MODULE_FAIL;
196                 }
197
198                 line[0] = '.';  /* force to be in current section */
199
200                 /*
201                  *      Don't allow it to go back up
202                  */
203                 if (line[1] == '.') goto do_log;
204
205                 ci = cf_reference_item(NULL, inst->cs, line);
206                 if (!ci) {
207                         RDEBUG2("No such entry \"%s\"", line);
208                         return RLM_MODULE_NOOP;
209                 }
210
211                 if (!cf_item_is_pair(ci)) {
212                         RDEBUG2("Entry \"%s\" is not a variable assignment ", line);
213                         goto do_log;
214                 }
215
216                 cp = cf_item_to_pair(ci);
217                 value = cf_pair_value(cp);
218                 if (!value) {
219                         RWDEBUG2("Entry \"%s\" has no value", line);
220                         return RLM_MODULE_OK;
221                 }
222
223                 /*
224                  *      Value exists, but is empty.  Don't log anything.
225                  */
226                 if (!*value) return RLM_MODULE_OK;
227         }
228
229  do_log:
230         /*
231          *      FIXME: Check length.
232          */
233         if (radius_xlat(line, sizeof(line) - 1, request, value, linelog_escape_func, NULL) < 0) {
234                 return RLM_MODULE_FAIL;
235         }
236
237 #ifdef HAVE_SYSLOG_H
238         if (strcmp(inst->filename, "syslog") == 0) {
239                 syslog(inst->syslog_priority, "%s", line);
240                 return RLM_MODULE_OK;
241         }
242 #endif
243
244         /*
245          *      We're using a real filename now.
246          */
247         if (radius_xlat(path, sizeof(path), request, inst->filename, inst->escape_func, NULL) < 0) {
248                 return RLM_MODULE_FAIL;
249         }
250
251         fd = exfile_open(inst->ef, path, inst->permissions, true);
252         if (fd < 0) {
253                 ERROR("rlm_linelog: Failed to open %s: %s", path, fr_syserror(errno));
254                 return RLM_MODULE_FAIL;
255         }
256
257         if (inst->group != NULL) {
258                 gid = strtol(inst->group, &endptr, 10);
259                 if (*endptr != '\0') {
260                         if (rad_getgid(request, &gid, inst->group) < 0) {
261                                 RDEBUG2("Unable to find system group \"%s\"", inst->group);
262                                 goto skip_group;
263                         }
264                 }
265
266                 if (chown(path, -1, gid) == -1) {
267                         RDEBUG2("Unable to change system group of \"%s\"", path);
268                 }
269         }
270
271  skip_group:
272         strcat(line, "\n");
273
274         if (write(fd, line, strlen(line)) < 0) {
275                 exfile_close(inst->ef, fd);
276                 ERROR("rlm_linelog: Failed writing: %s", fr_syserror(errno));
277                 return RLM_MODULE_FAIL;
278         }
279
280         exfile_close(inst->ef, fd);
281         return RLM_MODULE_OK;
282 }
283
284
285 /*
286  *      Externally visible module definition.
287  */
288 extern module_t rlm_linelog;
289 module_t rlm_linelog = {
290         .magic          = RLM_MODULE_INIT,
291         .name           = "linelog",
292         .type           = RLM_TYPE_HUP_SAFE,
293         .inst_size      = sizeof(rlm_linelog_t),
294         .config         = module_config,
295         .instantiate    = mod_instantiate,
296         .methods = {
297                 [MOD_AUTHENTICATE]      = mod_do_linelog,
298                 [MOD_AUTHORIZE]         = mod_do_linelog,
299                 [MOD_PREACCT]           = mod_do_linelog,
300                 [MOD_ACCOUNTING]        = mod_do_linelog,
301                 [MOD_PRE_PROXY]         = mod_do_linelog,
302                 [MOD_POST_PROXY]        = mod_do_linelog,
303                 [MOD_POST_AUTH]         = mod_do_linelog,
304 #ifdef WITH_COA
305                 [MOD_RECV_COA]          = mod_do_linelog,
306                 [MOD_SEND_COA]          = mod_do_linelog
307 #endif
308         },
309 };