/
rlm_linelog.c
857 lines (728 loc) · 22.5 KB
/
rlm_linelog.c
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
/*
* rlm_linelog.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,2006 The FreeRADIUS server project
* Copyright 2004 Alan DeKok <aland@freeradius.org>
*/
RCSID("$Id$")
#include <freeradius-devel/radiusd.h>
#include <freeradius-devel/modules.h>
#include <freeradius-devel/rad_assert.h>
#include <freeradius-devel/exfile.h>
#ifdef HAVE_FCNTL_H
# include <fcntl.h>
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#ifdef HAVE_GRP_H
# include <grp.h>
#endif
#ifdef HAVE_SYSLOG_H
# include <syslog.h>
# ifndef LOG_INFO
# define LOG_INFO (0)
# endif
#endif
#include <sys/uio.h>
typedef enum {
LINELOG_DST_INVALID = 0,
LINELOG_DST_FILE, //!< Log to a file.
LINELOG_DST_SYSLOG, //!< Log to syslog.
LINELOG_DST_UNIX, //!< Log via Unix socket.
LINELOG_DST_UDP, //!< Log via UDP.
LINELOG_DST_TCP, //!< Log via TCP.
} linelog_dst_t;
static FR_NAME_NUMBER const linelog_dst_table[] = {
{ "file", LINELOG_DST_FILE },
{ "syslog", LINELOG_DST_SYSLOG },
{ "unix", LINELOG_DST_UNIX },
{ "udp", LINELOG_DST_UDP },
{ "tcp", LINELOG_DST_TCP },
{ NULL , -1 }
};
typedef struct linelog_net {
fr_ipaddr_t dst_ipaddr; //!< Network server.
fr_ipaddr_t src_ipaddr; //!< Send requests from a given src_ipaddr.
uint16_t port; //!< Network port.
struct timeval timeout; //!< How long to wait for read/write operations.
} linelog_net_t;
/** linelog module instance
*/
typedef struct linelog_instance_t {
char const *name; //!< Module instance name.
fr_connection_pool_t *pool; //!< Connection pool instance.
char const *delimiter; //!< Line termination string (usually \n).
size_t delimiter_len; //!< Length of line termination string.
vp_tmpl_t *log_src; //!< Source of log messages.
vp_tmpl_t *log_ref; //!< Path to a #CONF_PAIR (to use as the source of
///< log messages).
linelog_dst_t log_dst; //!< Logging destination.
char const *log_dst_str; //!< Logging destination string.
struct {
char const *facility; //!< Syslog facility string.
char const *severity; //!< Syslog severity string.
int priority; //!< Bitwise | of severity and facility.
} syslog;
struct {
char const *name; //!< File to write to.
uint32_t permissions; //!< Permissions to use when creating new files.
char const *group_str; //!< Group to set on new files.
gid_t group; //!< Resolved gid.
exfile_t *ef; //!< Exclusive file access handle.
bool escape; //!< Do filename escaping, yes / no.
xlat_escape_t escape_func; //!< Escape function.
} file;
struct {
char const *path; //!< Where the UNIX socket lives.
struct timeval timeout; //!< How long to wait for read/write operations.
} unix;
linelog_net_t tcp; //!< TCP server.
linelog_net_t udp; //!< UDP server.
CONF_SECTION *cs; //!< #CONF_SECTION to use as the root for #log_ref lookups.
} linelog_instance_t;
typedef struct linelog_conn {
int sockfd; //!< File descriptor associated with socket
} linelog_conn_t;
static const CONF_PARSER file_config[] = {
{ FR_CONF_OFFSET("filename", PW_TYPE_FILE_OUTPUT | PW_TYPE_XLAT, linelog_instance_t, file.name) },
{ FR_CONF_OFFSET("permissions", PW_TYPE_INTEGER, linelog_instance_t, file.permissions), .dflt = "0600" },
{ FR_CONF_OFFSET("group", PW_TYPE_STRING, linelog_instance_t, file.group_str) },
{ FR_CONF_OFFSET("escape_filenames", PW_TYPE_BOOLEAN, linelog_instance_t, file.escape), .dflt = "no" },
CONF_PARSER_TERMINATOR
};
static const CONF_PARSER syslog_config[] = {
{ FR_CONF_OFFSET("facility", PW_TYPE_STRING, linelog_instance_t, syslog.facility) },
{ FR_CONF_OFFSET("severity", PW_TYPE_STRING, linelog_instance_t, syslog.severity), .dflt = "info" },
CONF_PARSER_TERMINATOR
};
static const CONF_PARSER unix_config[] = {
{ FR_CONF_OFFSET("filename", PW_TYPE_FILE_INPUT, linelog_instance_t, unix.path) },
CONF_PARSER_TERMINATOR
};
static const CONF_PARSER udp_config[] = {
{ FR_CONF_OFFSET("server", PW_TYPE_COMBO_IP_ADDR, linelog_net_t, dst_ipaddr) },
{ FR_CONF_OFFSET("port", PW_TYPE_SHORT, linelog_net_t, port) },
{ FR_CONF_OFFSET("timeout", PW_TYPE_TIMEVAL, linelog_net_t, timeout), .dflt = "1000" },
CONF_PARSER_TERMINATOR
};
static const CONF_PARSER tcp_config[] = {
{ FR_CONF_OFFSET("server", PW_TYPE_COMBO_IP_ADDR, linelog_net_t, dst_ipaddr) },
{ FR_CONF_OFFSET("port", PW_TYPE_SHORT, linelog_net_t, port) },
{ FR_CONF_OFFSET("timeout", PW_TYPE_TIMEVAL, linelog_net_t, timeout), .dflt = "1000" },
CONF_PARSER_TERMINATOR
};
/*
* 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[] = {
{ FR_CONF_OFFSET("destination", PW_TYPE_STRING | PW_TYPE_REQUIRED, linelog_instance_t, log_dst_str) },
{ FR_CONF_OFFSET("delimiter", PW_TYPE_STRING, linelog_instance_t, delimiter), .dflt = "\n" },
{ FR_CONF_OFFSET("format", PW_TYPE_TMPL, linelog_instance_t, log_src) },
{ FR_CONF_OFFSET("reference", PW_TYPE_TMPL, linelog_instance_t, log_ref) },
/*
* Log destinations
*/
{ FR_CONF_POINTER("file", PW_TYPE_SUBSECTION, NULL), .dflt = (void const *) file_config },
{ FR_CONF_POINTER("syslog", PW_TYPE_SUBSECTION, NULL), .dflt = (void const *) syslog_config },
{ FR_CONF_POINTER("unix", PW_TYPE_SUBSECTION, NULL), .dflt = (void const *) unix_config },
{ FR_CONF_OFFSET("tcp", PW_TYPE_SUBSECTION, linelog_instance_t, tcp), .dflt = (void const *) tcp_config },
{ FR_CONF_OFFSET("udp", PW_TYPE_SUBSECTION, linelog_instance_t, udp), .dflt = (void const *) udp_config },
/*
* Deprecated config items
*/
{ FR_CONF_OFFSET("filename", PW_TYPE_FILE_OUTPUT | PW_TYPE_DEPRECATED, linelog_instance_t, file.name) },
{ FR_CONF_OFFSET("permissions", PW_TYPE_INTEGER | PW_TYPE_DEPRECATED, linelog_instance_t, file.permissions) },
{ FR_CONF_OFFSET("group", PW_TYPE_STRING | PW_TYPE_DEPRECATED, linelog_instance_t, file.group_str) },
{ FR_CONF_OFFSET("syslog_facility", PW_TYPE_STRING | PW_TYPE_DEPRECATED, linelog_instance_t, syslog.facility) },
{ FR_CONF_OFFSET("syslog_severity", PW_TYPE_STRING | PW_TYPE_DEPRECATED, linelog_instance_t, syslog.severity) },
CONF_PARSER_TERMINATOR
};
static int _mod_conn_free(linelog_conn_t *conn)
{
if (shutdown(conn->sockfd, SHUT_RDWR) < 0) DEBUG3("Shutdown failed: %s", fr_syserror(errno));
if (close(conn->sockfd) < 0) DEBUG3("Closing socket failed: %s", fr_syserror(errno));
return 0;
}
static void *mod_conn_create(TALLOC_CTX *ctx, void *instance, struct timeval const *timeout)
{
linelog_instance_t *inst = instance;
linelog_conn_t *conn;
int sockfd = -1;
switch (inst->log_dst) {
case LINELOG_DST_UNIX:
DEBUG2("rlm_linelog (%s): Opening UNIX socket at \"%s\"", inst->name, inst->unix.path);
sockfd = fr_socket_client_unix(inst->unix.path, true);
if (sockfd < 0) {
ERROR("rlm_linelog (%s): Failed opening UNIX socket: %s", inst->name, fr_strerror());
return NULL;
}
break;
case LINELOG_DST_TCP:
if (DEBUG_ENABLED2) {
char buff[INET6_ADDRSTRLEN + 4]; /* IPv6 + /<d><d><d> */
fr_ntop(buff, sizeof(buff), &inst->tcp.dst_ipaddr);
DEBUG2("rlm_linelog (%s): Opening TCP connection to %s:%u", inst->name, buff, inst->tcp.port);
}
sockfd = fr_socket_client_tcp(NULL, &inst->tcp.dst_ipaddr, inst->tcp.port, true);
if (sockfd < 0) {
ERROR("rlm_linelog (%s): Failed opening TCP socket: %s", inst->name, fr_strerror());
return NULL;
}
break;
case LINELOG_DST_UDP:
if (DEBUG_ENABLED2) {
char buff[INET6_ADDRSTRLEN + 4]; /* IPv6 + /<d><d><d> */
fr_ntop(buff, sizeof(buff), &inst->udp.dst_ipaddr);
DEBUG2("rlm_linelog (%s): Opening UDP connection to %s:%u", inst->name, buff, inst->udp.port);
}
sockfd = fr_socket_client_udp(NULL, &inst->udp.dst_ipaddr, inst->udp.port, true);
if (sockfd < 0) {
ERROR("rlm_linelog (%s): Failed opening UDP socket: %s", inst->name, fr_strerror());
return NULL;
}
break;
/*
* Are not connection oriented destinations
*/
case LINELOG_DST_INVALID:
case LINELOG_DST_FILE:
case LINELOG_DST_SYSLOG:
rad_assert(0);
return NULL;
}
if (errno == EINPROGRESS) {
if (FR_TIMEVAL_TO_MS(timeout)) {
DEBUG2("rlm_linelog (%s): Waiting for connection to complete...", inst->name);
} else {
DEBUG2("rlm_linelog (%s): Blocking until connection complete...", inst->name);
}
if (fr_socket_wait_for_connect(sockfd, timeout) < 0) {
ERROR("rlm_linelog (%s): %s", inst->name, fr_strerror());
close(sockfd);
return NULL;
}
}
DEBUG2("rlm_linelog (%s): Connection successful", inst->name);
/*
* Set blocking operation as we have no timeout set
*/
if (!FR_TIMEVAL_TO_MS(timeout) && (fr_blocking(sockfd) < 0)) {
ERROR("rlm_linelog (%s): Failed setting nonblock flag on fd", inst->name);
close(sockfd);
return NULL;
}
conn = talloc_zero(ctx, linelog_conn_t);
conn->sockfd = sockfd;
talloc_set_destructor(conn, _mod_conn_free);
return conn;
}
static int mod_detach(void *instance)
{
linelog_instance_t *inst = instance;
fr_connection_pool_free(inst->pool);
return 0;
}
/*
* Instantiate the module.
*/
static int mod_instantiate(CONF_SECTION *conf, void *instance)
{
linelog_instance_t *inst = instance;
char prefix[100];
/*
* Escape filenames only if asked.
*/
if (inst->file.escape) {
inst->file.escape_func = rad_filename_escape;
} else {
inst->file.escape_func = rad_filename_make_safe;
}
inst->log_dst = fr_str2int(linelog_dst_table, inst->log_dst_str, LINELOG_DST_INVALID);
if (inst->log_dst == LINELOG_DST_INVALID) {
cf_log_err_cs(conf, "Invalid log destination \"%s\"", inst->log_dst_str);
return -1;
}
if (!inst->log_src && !inst->log_ref) {
cf_log_err_cs(conf, "Must specify a log format, or reference");
return -1;
}
inst->name = cf_section_name2(conf);
if (!inst->name) inst->name = cf_section_name1(conf);
snprintf(prefix, sizeof(prefix), "rlm_linelog (%s)", inst->name);
/*
* Setup the logging destination
*/
switch (inst->log_dst) {
case LINELOG_DST_FILE:
{
if (!inst->file.name) {
cf_log_err_cs(conf, "No value provided for 'filename'");
return -1;
}
inst->file.ef = exfile_init(inst, 64, 30, true);
if (!inst->file.ef) {
cf_log_err_cs(conf, "Failed creating log file context");
return -1;
}
if (inst->file.group_str) {
char *endptr;
inst->file.group = strtol(inst->file.group_str, &endptr, 10);
if (*endptr != '\0') {
if (rad_getgid(inst, &(inst->file.group), inst->file.group_str) < 0) {
cf_log_err_cs(conf, "Unable to find system group \"%s\"",
inst->file.group_str);
return -1;
}
}
}
}
break;
case LINELOG_DST_SYSLOG:
{
int num;
#ifndef HAVE_SYSLOG_H
cf_log_err_cs(conf, "Syslog output is not supported on this system");
return -1;
#else
if (inst->syslog.facility) {
num = fr_str2int(syslog_facility_table, inst->syslog.facility, -1);
if (num < 0) {
cf_log_err_cs(conf, "Invalid syslog facility \"%s\"", inst->syslog.facility);
return -1;
}
inst->syslog.priority |= num;
}
num = fr_str2int(syslog_severity_table, inst->syslog.severity, -1);
if (num < 0) {
cf_log_err_cs(conf, "Invalid syslog severity \"%s\"", inst->syslog.severity);
return -1;
}
inst->syslog.priority |= num;
#endif
}
break;
case LINELOG_DST_UNIX:
#ifndef HAVE_SYS_UN_H
cf_log_err_cs(conf, "Unix sockets are not supported on this sytem");
return -1;
#else
inst->pool = module_connection_pool_init(cf_section_sub_find(conf, "unix"),
inst, mod_conn_create, NULL, prefix);
if (!inst->pool) return -1;
#endif
break;
case LINELOG_DST_UDP:
inst->pool = module_connection_pool_init(cf_section_sub_find(conf, "udp"),
inst, mod_conn_create, NULL, prefix);
if (!inst->pool) return -1;
break;
case LINELOG_DST_TCP:
inst->pool = module_connection_pool_init(cf_section_sub_find(conf, "tcp"),
inst, mod_conn_create, NULL, prefix);
if (!inst->pool) return -1;
break;
case LINELOG_DST_INVALID:
rad_assert(0);
break;
}
inst->delimiter_len = talloc_array_length(inst->delimiter) - 1;
inst->cs = conf;
return 0;
}
/** Escape unprintable characters
*
* - Newline is escaped as ``\\n``.
* - Return is escaped as ``\\r``.
* - All other unprintables are escaped as @verbatim \<oct><oct><oct> @endverbatim.
*
* @param request The current request.
* @param out Where to write the escaped string.
* @param outlen Length of the output buffer.
* @param in String to escape.
* @param arg unused.
*/
static size_t linelog_escape_func(UNUSED REQUEST *request, char *out, size_t outlen,
char const *in, UNUSED void *arg)
{
int len = 0;
if (outlen == 0) return 0;
if (outlen == 1) {
*out = '\0';
return 0;
}
while (in[0]) {
if (in[0] >= ' ') {
if (in[0] == '\\') {
if (outlen <= 2) break;
outlen--;
*out++ = '\\';
len++;
}
outlen--;
if (outlen == 1) break;
*out++ = *in++;
len++;
continue;
}
switch (in[0]) {
case '\n':
if (outlen <= 2) break;
*out++ = '\\';
*out++ = 'n';
in++;
len += 2;
break;
case '\r':
if (outlen <= 2) break;
*out++ = '\\';
*out++ = 'r';
in++;
len += 2;
break;
default:
if (outlen <= 4) break;
snprintf(out, outlen, "\\%03o", (uint8_t) *in);
in++;
out += 4;
outlen -= 4;
len += 4;
break;
}
}
*out = '\0';
return len;
}
/** Write a linelog message
*
* Write a log message to syslog or a flat file.
*
* @param instance of rlm_linelog.
* @param request The current request.
* @return
* - #RLM_MODULE_NOOP if no message to log.
* - #RLM_MODULE_FAIL if we failed writing the message.
* - #RLM_MODULE_OK on success.
*/
static rlm_rcode_t mod_do_linelog(void *instance, REQUEST *request) CC_HINT(nonnull);
static rlm_rcode_t mod_do_linelog(void *instance, REQUEST *request)
{
int fd = -1;
linelog_conn_t *conn;
struct timeval *timeout = NULL;
char buff[4096];
char *p = buff;
linelog_instance_t *inst = instance;
char const *value;
vp_tmpl_t empty, *vpt = NULL, *vpt_p = NULL;
rlm_rcode_t rcode = RLM_MODULE_OK;
ssize_t slen;
struct iovec vector_s[2];
struct iovec *vector = NULL, *vector_p;
size_t vector_len;
bool with_delim;
buff[0] = '.'; /* force to be in current section */
buff[1] = '\0';
buff[2] = '\0';
/*
* Expand log_ref to a config path, using the module
* configuration section as the root.
*/
if (inst->log_ref) {
CONF_ITEM *ci;
CONF_PAIR *cp;
char const *tmpl_str;
if (tmpl_expand(NULL, buff + 1, sizeof(buff) - 1,
request, inst->log_ref, linelog_escape_func, NULL) < 0) {
return RLM_MODULE_FAIL;
}
if (buff[1] == '.') p++;
/*
* Don't go back up.
*/
if (buff[2] == '.') {
REDEBUG("Invalid path \"%s\"", p);
return RLM_MODULE_FAIL;
}
ci = cf_reference_item(NULL, inst->cs, p);
if (!ci) {
RDEBUG2("Path \"%s\" doesn't exist", p);
goto default_msg;
}
if (!cf_item_is_pair(ci)) {
REDEBUG("Path \"%s\" resolves to a section (should be a pair)", p);
return RLM_MODULE_FAIL;
}
cp = cf_item_to_pair(ci);
tmpl_str = cf_pair_value(cp);
if (!tmpl_str || (tmpl_str[0] == '\0')) {
RDEBUG2("Path \"%s\" resolves to an empty config pair", p);
vpt_p = tmpl_init(&empty, TMPL_TYPE_UNPARSED, "", 0, T_DOUBLE_QUOTED_STRING);
goto build_vector;
}
/*
* Alloc a template from the value of the CONF_PAIR
* using request as the context (which will hopefully avoid a malloc).
*/
slen = tmpl_afrom_str(request, &vpt, tmpl_str, talloc_array_length(tmpl_str) - 1,
cf_pair_value_type(cp), REQUEST_CURRENT, PAIR_LIST_REQUEST, true);
if (slen <= 0) {
REMARKER(tmpl_str, -slen, fr_strerror());
return RLM_MODULE_FAIL;
}
vpt_p = vpt;
} else {
default_msg:
/*
* Use the default format string
*/
if (!inst->log_src) {
RDEBUG2("No default message configured");
return RLM_MODULE_NOOP;
}
/*
* Use the pre-parsed format template
*/
RDEBUG2("Using default message");
vpt_p = inst->log_src;
}
build_vector:
with_delim = (inst->log_dst != LINELOG_DST_SYSLOG) && (inst->delimiter_len > 0);
/*
* Log all the things!
*/
switch (vpt_p->type) {
case TMPL_TYPE_ATTR:
case TMPL_TYPE_LIST:
{
#define VECTOR_INCREMENT 20
vp_cursor_t cursor;
VALUE_PAIR *vp;
int alloced = VECTOR_INCREMENT, i;
MEM(vector = talloc_array(request, struct iovec, alloced));
for (vp = tmpl_cursor_init(NULL, &cursor, request, vpt_p), i = 0;
vp;
vp = tmpl_cursor_next(&cursor, vpt_p), i++) {
/* need extra for line terminator */
if ((with_delim && ((i + 1) >= alloced)) ||
(i >= alloced)) {
alloced += VECTOR_INCREMENT;
MEM(vector = talloc_realloc(request, vector, struct iovec, alloced));
}
switch (vp->da->type) {
case PW_TYPE_OCTETS:
case PW_TYPE_STRING:
vector[i].iov_base = vp->data.ptr;
vector[i].iov_len = vp->vp_length;
break;
default:
p = fr_pair_value_asprint(vector, vp, '\0');
vector[i].iov_base = p;
vector[i].iov_len = talloc_array_length(p) - 1;
break;
}
/*
* Add the line delimiter string
*/
if (with_delim) {
i++;
memcpy(&vector[i].iov_base, &(inst->delimiter), sizeof(vector[i].iov_base));
vector[i].iov_len = inst->delimiter_len;
}
}
vector_p = vector;
vector_len = i;
}
break;
/*
* Log a single thing.
*/
default:
slen = tmpl_expand(&value, buff, sizeof(buff), request, vpt_p, linelog_escape_func, NULL);
if (slen < 0) {
rcode = RLM_MODULE_FAIL;
goto finish;
}
/* iov_base is not declared as const *sigh* */
memcpy(&vector_s[0].iov_base, &value, sizeof(vector_s[0].iov_base));
vector_s[0].iov_len = slen;
if (!with_delim) {
vector_len = 1;
} else {
memcpy(&vector_s[1].iov_base, &(inst->delimiter), sizeof(vector_s[1].iov_base));
vector_s[1].iov_len = inst->delimiter_len;
vector_len = 2;
}
vector_p = &vector_s[0];
}
if (vector_len == 0) {
RDEBUG("No data to write");
rcode = RLM_MODULE_NOOP;
goto finish;
}
/*
* Reserve a handle, write out the data, close the handle
*/
switch (inst->log_dst) {
case LINELOG_DST_FILE:
{
char path[2048];
if (radius_xlat(path, sizeof(path), request, inst->file.name, inst->file.escape_func, NULL) < 0) {
return RLM_MODULE_FAIL;
}
/* check path and eventually create subdirs */
p = strrchr(path, '/');
if (p) {
*p = '\0';
if (rad_mkdir(path, 0700, -1, -1) < 0) {
RERROR("Failed to create directory %s: %s", path, fr_syserror(errno));
rcode = RLM_MODULE_FAIL;
goto finish;
}
*p = '/';
}
fd = exfile_open(inst->file.ef, path, inst->file.permissions, true);
if (fd < 0) {
RERROR("Failed to open %s: %s", path, fr_syserror(errno));
rcode = RLM_MODULE_FAIL;
goto finish;
}
if (inst->file.group_str && (chown(path, -1, inst->file.group) == -1)) {
RWARN("Unable to change system group of \"%s\": %s", path, fr_strerror());
}
if (writev(fd, vector_p, vector_len) < 0) {
RERROR("Failed writing to \"%s\": %s", path, fr_syserror(errno));
exfile_close(inst->file.ef, fd);
/* Assert on the extra fatal errors */
rad_assert((errno != EINVAL) && (errno != EFAULT));
return RLM_MODULE_FAIL;
}
exfile_close(inst->file.ef, fd);
}
break;
case LINELOG_DST_UNIX:
if (inst->unix.timeout.tv_sec || inst->unix.timeout.tv_usec) timeout = &inst->unix.timeout;
goto do_write;
case LINELOG_DST_UDP:
if (inst->udp.timeout.tv_sec || inst->udp.timeout.tv_usec) timeout = &inst->udp.timeout;
goto do_write;
case LINELOG_DST_TCP:
{
int i, num;
if (inst->tcp.timeout.tv_sec || inst->tcp.timeout.tv_usec) timeout = &inst->tcp.timeout;
do_write:
num = fr_connection_pool_state(inst->pool)->num;
conn = fr_connection_get(inst->pool);
if (!conn) {
rcode = RLM_MODULE_FAIL;
goto finish;
}
for (i = num; i >= 0; i--) {
ssize_t wrote;
char discard[64];
wrote = fr_writev(conn->sockfd, vector_p, vector_len, timeout);
if (wrote < 0) switch (errno) {
/* Errors that indicate we should reconnect */
case EDESTADDRREQ:
case EPIPE:
case EBADF:
case ECONNRESET:
case ENETDOWN:
case ENETUNREACH:
case EADDRNOTAVAIL: /* Which is OSX for outbound interface is down? */
RWARN("Failed writing to socket: %s. Will reconnect and try again...",
fr_syserror(errno));
conn = fr_connection_reconnect(inst->pool, conn);
if (!conn) {
rcode = RLM_MODULE_FAIL;
goto done;
}
continue;
/* Assert on the extra fatal errors */
case EINVAL:
case EFAULT:
rad_assert(0);
/* FALL-THROUGH */
/* Normal errors that just cause the module to fail */
default:
RERROR("Failed writing to socket: %s", fr_syserror(errno));
rcode = RLM_MODULE_FAIL;
goto done;
}
RDEBUG2("Wrote %zi bytes", wrote);
/* Drain the receive buffer */
while (read(conn->sockfd, discard, sizeof(discard)) > 0);
break;
}
done:
fr_connection_release(inst->pool, conn);
}
break;
#ifdef HAVE_SYSLOG_H
case LINELOG_DST_SYSLOG:
{
size_t i;
for (i = 0; i < vector_len; i++) {
syslog(inst->syslog.priority, "%.*s", (int)vector_p[i].iov_len, (char *)vector_p[i].iov_base);
}
}
break;
#endif
case LINELOG_DST_INVALID:
rad_assert(0);
rcode = RLM_MODULE_FAIL;
break;
}
finish:
talloc_free(vpt);
talloc_free(vector);
return rcode;
}
/*
* Externally visible module definition.
*/
extern module_t rlm_linelog;
module_t rlm_linelog = {
.magic = RLM_MODULE_INIT,
.name = "linelog",
.type = RLM_TYPE_HUP_SAFE,
.inst_size = sizeof(linelog_instance_t),
.config = module_config,
.instantiate = mod_instantiate,
.detach = mod_detach,
.methods = {
[MOD_AUTHENTICATE] = mod_do_linelog,
[MOD_AUTHORIZE] = mod_do_linelog,
[MOD_PREACCT] = mod_do_linelog,
[MOD_ACCOUNTING] = mod_do_linelog,
[MOD_PRE_PROXY] = mod_do_linelog,
[MOD_POST_PROXY] = mod_do_linelog,
[MOD_POST_AUTH] = mod_do_linelog,
#ifdef WITH_COA
[MOD_RECV_COA] = mod_do_linelog,
[MOD_SEND_COA] = mod_do_linelog
#endif
},
};