The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
rlm_linelog.c
Go to the documentation of this file.
1 /*
2  * rlm_linelog.c
3  *
4  * Version: $Id: d57de4da7aeaa95602e6b6754fdb696dbb181873 $
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: d57de4da7aeaa95602e6b6754fdb696dbb181873 $")
25 
26 #include <freeradius-devel/server/base.h>
27 #include <freeradius-devel/server/exfile.h>
28 #include <freeradius-devel/server/module_rlm.h>
29 #include <freeradius-devel/server/tmpl_dcursor.h>
30 #include <freeradius-devel/server/rcode.h>
31 #include <freeradius-devel/server/tmpl.h>
32 #include <freeradius-devel/unlang/call_env.h>
33 #include <freeradius-devel/unlang/tmpl.h>
34 #include <freeradius-devel/unlang/module.h>
35 
36 #include <freeradius-devel/util/debug.h>
37 #include <freeradius-devel/util/iovec.h>
38 #include <freeradius-devel/util/perm.h>
39 #include <freeradius-devel/util/print.h>
40 #include <freeradius-devel/util/value.h>
41 #include <freeradius-devel/util/types.h>
42 
43 #include <freeradius-devel/unlang/xlat_func.h>
44 
45 #ifdef HAVE_FCNTL_H
46 # include <fcntl.h>
47 #endif
48 
49 #ifdef HAVE_UNISTD_H
50 # include <unistd.h>
51 #endif
52 
53 #ifdef HAVE_GRP_H
54 # include <grp.h>
55 #endif
56 
57 #ifdef HAVE_SYSLOG_H
58 # include <syslog.h>
59 # ifndef LOG_INFO
60 # define LOG_INFO (0)
61 # endif
62 #endif
63 
64 #include <sys/uio.h>
65 
66 static int linelog_escape_func(fr_value_box_t *vb, UNUSED void *uctx);
67 static int call_env_filename_parse(TALLOC_CTX *ctx, void *out, tmpl_rules_t const *t_rules, CONF_ITEM *ci,
68  call_env_ctx_t const *cec, UNUSED call_env_parser_t const *rule);
69 typedef enum {
71  LINELOG_DST_FILE, //!< Log to a file.
72  LINELOG_DST_REQUEST, //!< Log to the request->log
73  LINELOG_DST_SYSLOG, //!< Log to syslog.
74  LINELOG_DST_UNIX, //!< Log via Unix socket.
75  LINELOG_DST_UDP, //!< Log via UDP.
76  LINELOG_DST_TCP, //!< Log via TCP.
77  LINELOG_DST_STDOUT, //!< Log to stdout.
78  LINELOG_DST_STDERR, //!< Log to stderr.
80 
82  { L("file"), LINELOG_DST_FILE },
83  { L("files"), LINELOG_DST_FILE },
84  { L("request"), LINELOG_DST_REQUEST },
85  { L("stderr"), LINELOG_DST_STDERR },
86  { L("stdout"), LINELOG_DST_STDOUT },
87  { L("syslog"), LINELOG_DST_SYSLOG },
88  { L("tcp"), LINELOG_DST_TCP },
89  { L("udp"), LINELOG_DST_UDP },
90  { L("unix"), LINELOG_DST_UNIX }
91 };
93 
94 typedef struct {
95  fr_ipaddr_t dst_ipaddr; //!< Network server.
96  fr_ipaddr_t src_ipaddr; //!< Send requests from a given src_ipaddr.
97  uint16_t port; //!< Network port.
98  fr_time_delta_t timeout; //!< How long to wait for read/write operations.
100 
101 /** linelog module instance
102  */
103 typedef struct {
104  fr_pool_t *pool; //!< Connection pool instance.
105 
106  char const *delimiter; //!< Line termination string (usually \n).
107  size_t delimiter_len; //!< Length of line termination string.
108 
109  linefr_log_dst_t log_dst; //!< Logging destination.
110  char const *log_dst_str; //!< Logging destination string.
111 
112  struct {
113  char const *facility; //!< Syslog facility string.
114  char const *severity; //!< Syslog severity string.
115  int priority; //!< Bitwise | of severity and facility.
116  } syslog;
117 
118  struct {
119  uint32_t permissions; //!< Permissions to use when creating new files.
120  char const *group_str; //!< Group to set on new files.
121  gid_t group; //!< Resolved gid.
122  exfile_t *ef; //!< Exclusive file access handle.
123  bool escape; //!< Do filename escaping, yes / no.
124  bool fsync; //!< fsync after each write.
125  } file;
126 
127  struct {
128  char const *path; //!< Where the UNIX socket lives.
129  fr_time_delta_t timeout; //!< How long to wait for read/write operations.
130  } unix_sock; // Lowercase unix is a macro on some systems?!
131 
132  linelog_net_t tcp; //!< TCP server.
133  linelog_net_t udp; //!< UDP server.
134 
135  CONF_SECTION *cs; //!< #CONF_SECTION to use as the root for #log_ref lookups.
136 } rlm_linelog_t;
137 
138 typedef struct {
139  int sockfd; //!< File descriptor associated with socket
141 
142 
143 static const conf_parser_t file_config[] = {
144  { FR_CONF_OFFSET("permissions", rlm_linelog_t, file.permissions), .dflt = "0600" },
145  { FR_CONF_OFFSET("group", rlm_linelog_t, file.group_str) },
146  { FR_CONF_OFFSET("escape_filenames", rlm_linelog_t, file.escape), .dflt = "no" },
147  { FR_CONF_OFFSET("fsync", rlm_linelog_t, file.fsync), .dflt = "no" },
149 };
150 
151 static const conf_parser_t syslog_config[] = {
152  { FR_CONF_OFFSET("facility", rlm_linelog_t, syslog.facility) },
153  { FR_CONF_OFFSET("severity", rlm_linelog_t, syslog.severity), .dflt = "info" },
155 };
156 
157 static const conf_parser_t unix_config[] = {
158  { FR_CONF_OFFSET_FLAGS("filename", CONF_FLAG_FILE_INPUT, rlm_linelog_t, unix_sock.path) },
160 };
161 
162 static const conf_parser_t udp_config[] = {
163  { FR_CONF_OFFSET_TYPE_FLAGS("server", FR_TYPE_COMBO_IP_ADDR, 0, linelog_net_t, dst_ipaddr) },
164  { FR_CONF_OFFSET("port", linelog_net_t, port) },
165  { FR_CONF_OFFSET("timeout", linelog_net_t, timeout), .dflt = "1000" },
167 };
168 
169 static const conf_parser_t tcp_config[] = {
170  { FR_CONF_OFFSET_TYPE_FLAGS("server", FR_TYPE_COMBO_IP_ADDR, 0, linelog_net_t, dst_ipaddr) },
171  { FR_CONF_OFFSET("port", linelog_net_t, port) },
172  { FR_CONF_OFFSET("timeout", linelog_net_t, timeout), .dflt = "1000" },
174 };
175 
176 static const conf_parser_t module_config[] = {
177  { FR_CONF_OFFSET_FLAGS("destination", CONF_FLAG_REQUIRED, rlm_linelog_t, log_dst_str) },
178 
179  { FR_CONF_OFFSET("delimiter", rlm_linelog_t, delimiter), .dflt = "\n" },
180 
181  /*
182  * Log destinations
183  */
184  { FR_CONF_POINTER("file", 0, CONF_FLAG_SUBSECTION, NULL), .subcs = (void const *) file_config },
185  { FR_CONF_POINTER("syslog", 0, CONF_FLAG_SUBSECTION, NULL), .subcs = (void const *) syslog_config },
186  { FR_CONF_POINTER("unix", 0, CONF_FLAG_SUBSECTION, NULL), .subcs = (void const *) unix_config },
189 
190  /*
191  * Deprecated config items
192  */
193  { FR_CONF_DEPRECATED("permissions", rlm_linelog_t, file.permissions) },
194  { FR_CONF_DEPRECATED("group", rlm_linelog_t, file.group_str) },
195 
196  { FR_CONF_DEPRECATED("syslog_facility", rlm_linelog_t, syslog.facility) },
197  { FR_CONF_DEPRECATED("syslog_severity", rlm_linelog_t, syslog.severity) },
199 };
200 
201 typedef struct {
202  tmpl_t *log_src; //!< Source of log messages.
203 
204  fr_value_box_t *log_ref; //!< Path to a #CONF_PAIR (to use as the source of
205  ///< log messages).
206 
207  fr_value_box_t *log_head; //!< Header to add to each new log file.
208 
209  fr_value_box_t *filename; //!< File name, if output is to a file.
211 
214  .env = (call_env_parser_t[]) {
216  { FR_CALL_ENV_OFFSET("reference",FR_TYPE_STRING, CALL_ENV_FLAG_CONCAT, linelog_call_env_t, log_ref), .pair.escape.func = linelog_escape_func },
217  { FR_CALL_ENV_OFFSET("header", FR_TYPE_STRING, CALL_ENV_FLAG_CONCAT, linelog_call_env_t, log_head), .pair.escape.func = linelog_escape_func },
219  ((call_env_parser_t[]) {
221  .pair.func = call_env_filename_parse },
223  })) },
225  }
226 };
227 
230  .env = (call_env_parser_t[]) {
231  { FR_CALL_ENV_OFFSET("header", FR_TYPE_STRING, CALL_ENV_FLAG_CONCAT, linelog_call_env_t, log_head), .pair.escape.func = linelog_escape_func },
233  ((call_env_parser_t[]) {
235  .pair.func = call_env_filename_parse },
237  })) },
239  }
240 };
241 
243 {
244  if (shutdown(conn->sockfd, SHUT_RDWR) < 0) DEBUG3("Shutdown failed: %s", fr_syserror(errno));
245  if (close(conn->sockfd) < 0) DEBUG3("Closing socket failed: %s", fr_syserror(errno));
246 
247  return 0;
248 }
249 
250 static void *mod_conn_create(TALLOC_CTX *ctx, void *instance, fr_time_delta_t timeout)
251 {
252  rlm_linelog_t const *inst = talloc_get_type_abort(instance, rlm_linelog_t);
253  linelog_conn_t *conn;
254  int sockfd = -1;
255 
256  switch (inst->log_dst) {
257  case LINELOG_DST_UNIX:
258  DEBUG2("Opening UNIX socket at \"%s\"", inst->unix_sock.path);
259  sockfd = fr_socket_client_unix(inst->unix_sock.path, true);
260  if (sockfd < 0) {
261  PERROR("Failed opening UNIX socket");
262  return NULL;
263  }
264  break;
265 
266  case LINELOG_DST_TCP:
267  DEBUG2("Opening TCP connection to %pV:%u", fr_box_ipaddr(inst->tcp.dst_ipaddr), inst->tcp.port);
268 
269  sockfd = fr_socket_client_tcp(NULL, NULL, &inst->tcp.dst_ipaddr, inst->tcp.port, true);
270  if (sockfd < 0) {
271  PERROR("Failed opening TCP socket");
272  return NULL;
273  }
274  break;
275 
276  case LINELOG_DST_UDP:
277  DEBUG2("Opening UDP connection to %pV:%u", fr_box_ipaddr(inst->udp.dst_ipaddr), inst->udp.port);
278 
279  sockfd = fr_socket_client_udp(NULL, NULL, NULL, &inst->udp.dst_ipaddr, inst->udp.port, true);
280  if (sockfd < 0) {
281  PERROR("Failed opening UDP socket");
282  return NULL;
283  }
284  break;
285 
286  /*
287  * Are not connection oriented destinations
288  */
289  case LINELOG_DST_INVALID:
290  case LINELOG_DST_FILE:
291  case LINELOG_DST_REQUEST:
292  case LINELOG_DST_SYSLOG:
293  case LINELOG_DST_STDOUT:
294  case LINELOG_DST_STDERR:
295  fr_assert(0);
296  return NULL;
297  }
298 
299  if (errno == EINPROGRESS) {
301  DEBUG2("Waiting for connection to complete...");
302  } else {
303  DEBUG2("Blocking until connection complete...");
304  }
306  PERROR("Failed connecting to log destination");
307  close(sockfd);
308  return NULL;
309  }
310  }
311  DEBUG2("Connection successful");
312 
313  /*
314  * Set blocking operation as we have no timeout set
315  */
316  if (!fr_time_delta_ispos(timeout) && (fr_blocking(sockfd) < 0)) {
317  ERROR("Failed setting nonblock flag on fd");
318  close(sockfd);
319  return NULL;
320  }
321 
322  conn = talloc_zero(ctx, linelog_conn_t);
323  conn->sockfd = sockfd;
324  talloc_set_destructor(conn, _mod_conn_free);
325 
326  return conn;
327 }
328 
329 /** Escape unprintable characters
330  *
331  * - Newline is escaped as ``\\n``.
332  * - Return is escaped as ``\\r``.
333  * - All other unprintables are escaped as @verbatim <oct><oct><oct> @endverbatim.
334  *
335  * @param vb Value box to escape.
336  * @param uctx unused.
337  */
338 /*
339  * Escape unprintable characters.
340  */
342 {
343  char *escaped;
344 
345  if (vb->vb_length == 0) return 0;
346 
347  MEM(escaped = fr_asprint(vb, vb->vb_strvalue, vb->vb_length, 0));
348  fr_value_box_strdup_shallow_replace(vb, escaped, talloc_strlen(escaped));
349 
350  return 0;
351 }
352 
353 static void linelog_hexdump(request_t *request, struct iovec *vector_p, size_t vector_len, char const *msg)
354 {
355  fr_dbuff_t *agg;
356 
357  FR_DBUFF_TALLOC_THREAD_LOCAL(&agg, 1024, SIZE_MAX);
358  fr_concatv(agg, vector_p, vector_len);
359 
360  RHEXDUMP3(fr_dbuff_start(agg), fr_dbuff_used(agg), "%s", msg);
361 }
362 
363 static int linelog_write(rlm_linelog_t const *inst, linelog_call_env_t const *call_env, request_t *request, struct iovec *vector_p, size_t vector_len, bool with_delim)
364 {
365  int ret = 0;
366  linelog_conn_t *conn;
368 
369  /*
370  * Reserve a handle, write out the data, close the handle
371  */
372  switch (inst->log_dst) {
373  case LINELOG_DST_FILE:
374  {
375  int fd = -1;
376  char const *path;
377  off_t offset;
378  char *p;
379 
380  if (!call_env->filename) {
381  RERROR("Missing filename");
382  return -1;
383  }
384 
385  path = call_env->filename->vb_strvalue;
386 
387  /* check path and eventually create subdirs */
388  p = strrchr(path, '/');
389  if (p) {
390  *p = '\0';
391  if (fr_mkdir(NULL, path, -1, 0700, NULL, NULL) < 0) {
392  RERROR("Failed to create directory %pV: %s", call_env->filename, fr_syserror(errno));
393  return -1;
394  }
395  *p = '/';
396  }
397 
398  fd = exfile_open(inst->file.ef, path, inst->file.permissions, &offset);
399  if (fd < 0) {
400  RERROR("Failed to open %pV: %s", call_env->filename, fr_syserror(errno));
401  return -1;
402  }
403 
404  if (inst->file.group_str && (chown(path, -1, inst->file.group) == -1)) {
405  RPWARN("Unable to change system group of \"%pV\": %s", call_env->filename, fr_strerror());
406  }
407 
408  /*
409  * If a header format is defined and we are at the beginning
410  * of the file then expand the format and write it out before
411  * writing the actual log entries.
412  */
413  if (call_env->log_head && (offset == 0)) {
414  struct iovec head_vector_s[2];
415  size_t head_vector_len;
416 
417  memcpy(&head_vector_s[0].iov_base, &call_env->log_head->vb_strvalue, sizeof(head_vector_s[0].iov_base));
418  head_vector_s[0].iov_len = call_env->log_head->vb_length;
419 
420  if (!with_delim) {
421  head_vector_len = 1;
422  } else {
423  memcpy(&head_vector_s[1].iov_base, &(inst->delimiter),
424  sizeof(head_vector_s[1].iov_base));
425  head_vector_s[1].iov_len = inst->delimiter_len;
426  head_vector_len = 2;
427  }
428 
429  if (RDEBUG_ENABLED3) linelog_hexdump(request, head_vector_s, head_vector_len, "linelog header");
430 
431  if (writev(fd, &head_vector_s[0], head_vector_len) < 0) {
432  write_fail:
433  RERROR("Failed writing to \"%pV\": %s", call_env->filename, fr_syserror(errno));
434  exfile_close(inst->file.ef, fd);
435 
436  /* Assert on the extra fatal errors */
437  fr_assert((errno != EINVAL) && (errno != EFAULT));
438 
439  return -1;
440  }
441  if (inst->file.fsync && (fsync(fd) < 0)) {
442  RERROR("Failed syncing \"%pV\" to persistent storage: %s", call_env->filename, fr_syserror(errno));
443  exfile_close(inst->file.ef, fd);
444  return -1;
445  }
446  }
447 
448  if (RDEBUG_ENABLED3) linelog_hexdump(request, vector_p, vector_len, "linelog data");
449 
450  ret = writev(fd, vector_p, vector_len);
451  if (ret < 0) goto write_fail;
452 
453  exfile_close(inst->file.ef, fd);
454  }
455  break;
456 
457  case LINELOG_DST_REQUEST:
458  {
459  size_t i;
460 
461  ret = 0;
462  for (i = 0; i < vector_len; i++) {
463  RINFO("%.*s", (int)vector_p[i].iov_len, (char *)vector_p[i].iov_base);
464  ret += vector_p[i].iov_len;
465  }
466  }
467  break;
468 
469  case LINELOG_DST_UNIX:
470  if (fr_time_delta_ispos(inst->unix_sock.timeout)) {
471  timeout = inst->unix_sock.timeout;
472  }
473  goto do_write;
474 
475  case LINELOG_DST_UDP:
476  if (fr_time_delta_ispos(inst->udp.timeout)) {
477  timeout = inst->udp.timeout;
478  }
479  goto do_write;
480 
481  case LINELOG_DST_TCP:
482  {
483  int i, num;
484  if (fr_time_delta_ispos(inst->tcp.timeout)) {
485  timeout = inst->tcp.timeout;
486  }
487 
488  do_write:
489  num = fr_pool_state(inst->pool)->num;
490  conn = fr_pool_connection_get(inst->pool, request);
491  if (!conn) return -1;
492 
493  for (i = num; i >= 0; i--) {
494  ssize_t wrote;
495  char discard[64];
496 
497  if (RDEBUG_ENABLED3) linelog_hexdump(request, vector_p, vector_len, "linelog data");
498  wrote = fr_writev(conn->sockfd, vector_p, vector_len, timeout);
499  if (wrote < 0) switch (errno) {
500  /* Errors that indicate we should reconnect */
501  case EDESTADDRREQ:
502  case EPIPE:
503  case EBADF:
504  case ECONNRESET:
505  case ENETDOWN:
506  case ENETUNREACH:
507  case EADDRNOTAVAIL: /* Which is OSX for outbound interface is down? */
508  RWARN("Failed writing to socket: %s. Will reconnect and try again...",
509  fr_syserror(errno));
510  conn = fr_pool_connection_reconnect(inst->pool, request, conn);
511  if (!conn) {
512  ret = -1;
513  goto done;
514  }
515  continue;
516 
517  /* Assert on the extra fatal errors */
518  case EINVAL:
519  case EFAULT:
520  fr_assert(0);
521  FALL_THROUGH;
522 
523  /* Normal errors that just cause the module to fail */
524  default:
525  RERROR("Failed writing to socket: %s", fr_syserror(errno));
526  ret = -1;
527  goto done;
528  }
529  RDEBUG2("Wrote %zi bytes", wrote);
530  ret = wrote;
531 
532  /* Drain the receive buffer */
533  while (read(conn->sockfd, discard, sizeof(discard)) > 0);
534  break;
535  }
536  done:
537  fr_pool_connection_release(inst->pool, request, conn);
538  }
539  break;
540 
541 #ifdef HAVE_SYSLOG_H
542  case LINELOG_DST_SYSLOG:
543  {
544  size_t i;
545 
546  ret = 0;
547  for (i = 0; i < vector_len; i++) {
548  syslog(inst->syslog.priority, "%.*s", (int)vector_p[i].iov_len, (char *)vector_p[i].iov_base);
549  ret += vector_p[i].iov_len;
550  }
551  }
552  break;
553 #endif
554 
555  case LINELOG_DST_STDOUT:
556  case LINELOG_DST_STDERR:
557  {
558  int fd = inst->log_dst == LINELOG_DST_STDOUT ? STDOUT_FILENO : STDERR_FILENO;
559  if ((ret = writev(fd, vector_p, vector_len)) < 0) {
560  RERROR("Failed writing to \"%s\": %s",
562  fr_syserror(errno));
563  }
564  }
565  break;
566 
567  case LINELOG_DST_INVALID:
568  fr_assert(0);
569  ret = -1;
570  break;
571  }
572 
573  return ret;
574 }
575 
576 static xlat_action_t linelog_xlat(TALLOC_CTX *ctx, fr_dcursor_t *out,
577  xlat_ctx_t const *xctx, request_t *request,
578  fr_value_box_list_t *args)
579 {
581  linelog_call_env_t const *call_env = talloc_get_type_abort(xctx->env_data, linelog_call_env_t);
582 
583  struct iovec vector[2];
584  size_t i = 0;
585  bool with_delim;
586  fr_value_box_t *msg, *wrote;
587  ssize_t slen;
588 
589  XLAT_ARGS(args, &msg);
590 
591  vector[i].iov_base = UNCONST(char *, msg->vb_strvalue);
592  vector[i].iov_len = msg->vb_length;
593  i++;
594 
595  with_delim = (inst->log_dst != LINELOG_DST_SYSLOG) && (inst->delimiter_len > 0);
596  if (with_delim) {
597  memcpy(&vector[i].iov_base, &(inst->delimiter), sizeof(vector[i].iov_base));
598  vector[i].iov_len = inst->delimiter_len;
599  i++;
600  }
601  slen = linelog_write(inst, call_env, request, vector, i, with_delim);
602  if (slen < 0) return XLAT_ACTION_FAIL;
603 
604  MEM(wrote = fr_value_box_alloc(ctx, FR_TYPE_SIZE, NULL));
605  wrote->vb_size = (size_t)slen;
606 
607  fr_dcursor_insert(out, wrote);
608 
609  return XLAT_ACTION_DONE;
610 }
611 
612 typedef struct {
613  fr_value_box_list_t expanded; //!< The result of expanding the fmt tmpl
614  bool with_delim; //!< Whether to add a delimiter
616 
617 static unlang_action_t CC_HINT(nonnull) mod_do_linelog_resume(rlm_rcode_t *p_result, module_ctx_t const *mctx, request_t *request)
618 {
620  linelog_call_env_t const *call_env = talloc_get_type_abort(mctx->env_data, linelog_call_env_t);
621  rlm_linelog_rctx_t *rctx = talloc_get_type_abort(mctx->rctx, rlm_linelog_rctx_t);
622  struct iovec *vector;
623  struct iovec *vector_p;
624  size_t vector_len;
625 
626  vector_len = fr_value_box_list_num_elements(&rctx->expanded);
627  if (vector_len == 0) {
628  RDEBUG2("No data to write");
630  }
631 
632  /*
633  * Add extra space for the delimiter
634  */
635  if (rctx->with_delim) vector_len *= 2;
636 
637  MEM(vector = vector_p = talloc_array(rctx, struct iovec, vector_len));
638  fr_value_box_list_foreach(&rctx->expanded, vb) {
639  switch(vb->type) {
640  default:
641  if (unlikely(fr_value_box_cast_in_place(rctx, vb, FR_TYPE_STRING, vb->enumv) < 0)) {
642  REDEBUG("Failed casting value to string");
644  }
645  FALL_THROUGH;
646 
647  case FR_TYPE_STRING:
648  vector_p->iov_base = UNCONST(char *, vb->vb_strvalue);
649  vector_p->iov_len = vb->vb_length;
650  vector_p++;
651  break;
652 
653  case FR_TYPE_OCTETS:
654  vector_p->iov_base = UNCONST(char *, vb->vb_octets);
655  vector_p->iov_len = vb->vb_length;
656  vector_p++;
657  break;
658  }
659 
660  /*
661  * Don't add the delim for the last element
662  */
663  if (rctx->with_delim) {
664  memcpy(&vector_p->iov_base, &(inst->delimiter), sizeof(vector_p->iov_base));
665  vector_p->iov_len = inst->delimiter_len;
666  vector_p++;
667  }
668  }
669 
670  RETURN_MODULE_RCODE(linelog_write(inst, call_env, request, vector, vector_len, rctx->with_delim) < 0 ? RLM_MODULE_FAIL : RLM_MODULE_OK);
671 }
672 
673 /** Write a linelog message
674  *
675  * Write a log message to syslog or a flat file.
676  *
677  * @param[in] p_result the result of the module call:
678  * - #RLM_MODULE_NOOP if no message to log.
679  * - #RLM_MODULE_FAIL if we failed writing the message.
680  * - #RLM_MODULE_OK on success.
681  * @param[in] mctx module calling context.
682  * @param[in] request The current request.
683  */
684 static unlang_action_t CC_HINT(nonnull) mod_do_linelog(rlm_rcode_t *p_result, module_ctx_t const *mctx, request_t *request)
685 {
687  linelog_call_env_t const *call_env = talloc_get_type_abort(mctx->env_data, linelog_call_env_t);
688  CONF_SECTION *conf = mctx->mi->conf;
689 
690  char buff[4096];
691  char *p = buff;
692  tmpl_t *empty, *vpt = NULL, *vpt_p = NULL;
693  ssize_t slen;
694  bool with_delim;
695 
696  TALLOC_CTX *frame_ctx = unlang_interpret_frame_talloc_ctx(request);
697 
698  if (!call_env->log_src && !call_env->log_ref) {
699  cf_log_err(conf, "A 'format', or 'reference' configuration item must be set to call this module");
701  }
702 
703  buff[0] = '.'; /* force to be in current section (by default) */
704  buff[1] = '\0';
705  buff[2] = '\0';
706 
707  /*
708  * Expand log_ref to a config path, using the module
709  * configuration section as the root.
710  */
711  if (call_env->log_ref) {
712  CONF_ITEM *ci;
713  CONF_PAIR *cp;
714  char const *tmpl_str;
715 
716  memcpy(buff + 1, call_env->log_ref->vb_strvalue, call_env->log_ref->vb_length);
717  buff[call_env->log_ref->vb_length + 1] = '\0';
718 
719  if (buff[1] == '.') p++;
720 
721  /*
722  * Don't go back up.
723  */
724  if (buff[2] == '.') {
725  REDEBUG("Invalid path \"%s\"", p);
727  }
728 
729  ci = cf_reference_item(NULL, inst->cs, p);
730  if (!ci) {
731  RDEBUG2("Path \"%s\" doesn't exist", p);
732  goto default_msg;
733  }
734 
735  if (!cf_item_is_pair(ci)) {
736  REDEBUG("Path \"%s\" resolves to a section (should be a pair)", p);
738  }
739 
740  cp = cf_item_to_pair(ci);
741  tmpl_str = cf_pair_value(cp);
742  if (!tmpl_str || (tmpl_str[0] == '\0')) {
743  RDEBUG2("Path \"%s\" resolves to an empty config pair", p);
744  empty = talloc(frame_ctx, tmpl_t);
745  vpt_p = tmpl_init_shallow(empty, TMPL_TYPE_DATA, T_DOUBLE_QUOTED_STRING, "", 0, NULL);
746  fr_value_box_init_null(&empty->data.literal);
747  fr_value_box_strdup_shallow(&empty->data.literal, NULL, "", false);
748  goto build_vector;
749  }
750 
751  /*
752  * Alloc a template from the value of the CONF_PAIR
753  * using request as the context (which will hopefully avoid an alloc).
754  */
755  slen = tmpl_afrom_substr(frame_ctx, &vpt,
756  &FR_SBUFF_IN(tmpl_str, talloc_array_length(tmpl_str) - 1),
758  NULL,
759  &(tmpl_rules_t){
760  .attr = {
761  .list_def = request_attr_request,
762  .dict_def = request->dict,
763  .allow_unknown = true,
764  .allow_unresolved = false,
765  },
766  .xlat = {
767  .runtime_el = unlang_interpret_event_list(request),
768  },
769  .at_runtime = true
770  });
771  if (!vpt) {
772  REMARKER(tmpl_str, -slen, "%s", fr_strerror());
774  }
775  if (tmpl_resolve(vpt, NULL) < 0) {
776  RPERROR("Runtime resolution of tmpl failed");
777  talloc_free(vpt);
779  }
780  vpt_p = vpt;
781  } else {
782  default_msg:
783  /*
784  * Use the default format string
785  */
786  if (!call_env->log_src) {
787  RDEBUG2("No default message configured");
789  }
790  /*
791  * Use the pre-parsed format template
792  */
793  RDEBUG2("Using default message");
794  vpt_p = call_env->log_src;
795  }
796 
797 build_vector:
798  with_delim = (inst->log_dst != LINELOG_DST_SYSLOG) && (inst->delimiter_len > 0);
799 
800  /*
801  * Log all the things!
802  */
803  switch (vpt_p->type) {
804  case TMPL_TYPE_ATTR:
805  {
806  #define VECTOR_INCREMENT 20
807  fr_dcursor_t cursor;
809  fr_pair_t *vp;
810  int alloced = VECTOR_INCREMENT, i;
811  struct iovec *vector = NULL, *vector_p;
812  size_t vector_len;
813  rlm_rcode_t rcode = RLM_MODULE_OK;
814 
815  MEM(vector = talloc_array(frame_ctx, struct iovec, alloced));
816  for (vp = tmpl_dcursor_init(NULL, NULL, &cc, &cursor, request, vpt_p), i = 0;
817  vp;
818  vp = fr_dcursor_next(&cursor), i++) {
819  /* need extra for line terminator */
820  if ((with_delim && ((i + 1) >= alloced)) ||
821  (i >= alloced)) {
822  alloced += VECTOR_INCREMENT;
823  MEM(vector = talloc_realloc(frame_ctx, vector, struct iovec, alloced));
824  }
825 
826  switch (vp->vp_type) {
827  case FR_TYPE_OCTETS:
828  case FR_TYPE_STRING:
829  vector[i].iov_len = vp->vp_length;
830  vector[i].iov_base = vp->vp_ptr;
831  break;
832 
833  default:
834  vector[i].iov_len = fr_value_box_aprint(vector, &p, &vp->data, NULL);
835  vector[i].iov_base = p;
836  break;
837  }
838 
839  /*
840  * Add the line delimiter string
841  */
842  if (with_delim) {
843  i++;
844  memcpy(&vector[i].iov_base, &(inst->delimiter), sizeof(vector[i].iov_base));
845  vector[i].iov_len = inst->delimiter_len;
846  }
847  }
848  tmpl_dcursor_clear(&cc);
849  vector_p = vector;
850  vector_len = i;
851 
852  if (vector_len == 0) {
853  RDEBUG2("No data to write");
854  rcode = RLM_MODULE_NOOP;
855  } else {
856  rcode = linelog_write(inst, call_env, request, vector_p, vector_len, with_delim) < 0 ? RLM_MODULE_FAIL : RLM_MODULE_OK;
857  }
858 
859  talloc_free(vpt);
860  talloc_free(vector);
861 
862  RETURN_MODULE_RCODE(rcode);
863  }
864 
865  /*
866  * Log a format string. We need to yield as this might contain asynchronous expansions.
867  */
868  default:
869  {
870  rlm_linelog_rctx_t *rctx;
871 
872  MEM(rctx = talloc(frame_ctx, rlm_linelog_rctx_t));
873  fr_value_box_list_init(&rctx->expanded);
874  rctx->with_delim = with_delim;
875 
876  return unlang_module_yield_to_tmpl(rctx, &rctx->expanded, request, vpt_p, NULL, mod_do_linelog_resume, NULL, 0, rctx);
877  }
878  }
879 }
880 
881 /*
882  * Custom call env parser for filenames - sets the correct escaping function
883  */
884 static int call_env_filename_parse(TALLOC_CTX *ctx, void *out, tmpl_rules_t const *t_rules, CONF_ITEM *ci,
885  call_env_ctx_t const *cec, UNUSED call_env_parser_t const *rule)
886 {
888  tmpl_t *parsed;
889  CONF_PAIR const *to_parse = cf_item_to_pair(ci);
890  tmpl_rules_t our_rules;
891 
892  /*
893  * If we're not logging to a file destination, do nothing
894  */
896 
897  our_rules = *t_rules;
898  our_rules.escape.func = (inst->file.escape) ? rad_filename_box_escape : rad_filename_box_make_safe;
899  our_rules.escape.safe_for = (inst->file.escape) ? (fr_value_box_safe_for_t)rad_filename_box_escape :
901  our_rules.escape.mode = TMPL_ESCAPE_PRE_CONCAT;
902  our_rules.literals_safe_for = our_rules.escape.safe_for;
903 
904  if (tmpl_afrom_substr(ctx, &parsed,
905  &FR_SBUFF_IN(cf_pair_value(to_parse), talloc_array_length(cf_pair_value(to_parse)) - 1),
907  &our_rules) < 0) return -1;
908 
909  *(void **)out = parsed;
910  return 0;
911 }
912 
913 static int mod_detach(module_detach_ctx_t const *mctx)
914 {
915  rlm_linelog_t *inst = talloc_get_type_abort(mctx->mi->data, rlm_linelog_t);
916 
917  fr_pool_free(inst->pool);
918 
919  return 0;
920 }
921 
922 /*
923  * Instantiate the module.
924  */
925 static int mod_instantiate(module_inst_ctx_t const *mctx)
926 {
927  rlm_linelog_t *inst = talloc_get_type_abort(mctx->mi->data, rlm_linelog_t);
928  CONF_SECTION *cs, *conf = mctx->mi->conf;
929  char prefix[100];
930 
932  if (inst->log_dst == LINELOG_DST_INVALID) {
933  cf_log_err(conf, "Invalid log destination \"%s\"", inst->log_dst_str);
934  return -1;
935  }
936 
937  snprintf(prefix, sizeof(prefix), "rlm_linelog (%s)", mctx->mi->name);
938 
939  /*
940  * Setup the logging destination
941  */
942  switch (inst->log_dst) {
943  case LINELOG_DST_FILE:
944  {
945  cs = cf_section_find(conf, "file", CF_IDENT_ANY);
946  if (!cs) {
947  no_filename:
948  cf_log_err(conf, "No value provided for 'file.filename'");
949  return -1;
950  }
951  if (!cf_pair_find(cs, "filename")) goto no_filename;
952 
953  inst->file.ef = module_rlm_exfile_init(inst, conf, 256, fr_time_delta_from_sec(30), true, NULL, NULL);
954  if (!inst->file.ef) {
955  cf_log_err(conf, "Failed creating log file context");
956  return -1;
957  }
958 
959  if (inst->file.group_str) {
960  char *endptr;
961 
962  inst->file.group = strtol(inst->file.group_str, &endptr, 10);
963  if (*endptr != '\0') {
964  if (fr_perm_gid_from_str(inst, &(inst->file.group), inst->file.group_str) < 0) {
965  cf_log_err(conf, "Unable to find system group \"%s\"",
966  inst->file.group_str);
967  return -1;
968  }
969  }
970  }
971  }
972  break;
973 
974  case LINELOG_DST_SYSLOG:
975  {
976  int num;
977 
978 #ifndef HAVE_SYSLOG_H
979  cf_log_err(conf, "Syslog output is not supported on this system");
980  return -1;
981 #else
982  if (inst->syslog.facility) {
983  num = fr_table_value_by_str(syslog_facility_table, inst->syslog.facility, -1);
984  if (num < 0) {
985  cf_log_err(conf, "Invalid syslog facility \"%s\"", inst->syslog.facility);
986  return -1;
987  }
988  inst->syslog.priority |= num;
989  }
990 
991  num = fr_table_value_by_str(syslog_severity_table, inst->syslog.severity, -1);
992  if (num < 0) {
993  cf_log_err(conf, "Invalid syslog severity \"%s\"", inst->syslog.severity);
994  return -1;
995  }
996  inst->syslog.priority |= num;
997 #endif
998  }
999  break;
1000 
1001  case LINELOG_DST_UNIX:
1002 #ifndef HAVE_SYS_UN_H
1003  cf_log_err(conf, "Unix sockets are not supported on this system");
1004  return -1;
1005 #else
1007  inst, mod_conn_create, NULL, prefix, NULL, NULL);
1008  if (!inst->pool) return -1;
1009 #endif
1010  break;
1011 
1012  case LINELOG_DST_UDP:
1014  inst, mod_conn_create, NULL, prefix, NULL, NULL);
1015  if (!inst->pool) return -1;
1016  break;
1017 
1018  case LINELOG_DST_TCP:
1020  inst, mod_conn_create, NULL, prefix, NULL, NULL);
1021  if (!inst->pool) return -1;
1022  break;
1023 
1024  case LINELOG_DST_REQUEST:
1025  case LINELOG_DST_STDOUT:
1026  case LINELOG_DST_STDERR:
1027  break;
1028 
1029  case LINELOG_DST_INVALID:
1030  fr_assert(0);
1031  break;
1032  }
1033 
1034  inst->delimiter_len = talloc_array_length(inst->delimiter) - 1;
1035  inst->cs = conf;
1036 
1037  return 0;
1038 }
1039 
1040 static int mod_bootstrap(module_inst_ctx_t const *mctx)
1041 {
1042  xlat_t *xlat;
1043 
1044  static xlat_arg_parser_t const linelog_xlat_args[] = {
1045  { .required = true, .concat = true, .type = FR_TYPE_STRING },
1047  };
1048 
1049  xlat = module_rlm_xlat_register(mctx->mi->boot, mctx, NULL, linelog_xlat, FR_TYPE_SIZE);
1050  xlat_func_args_set(xlat, linelog_xlat_args);
1052 
1053  return 0;
1054 }
1055 
1056 /*
1057  * Externally visible module definition.
1058  */
1059 extern module_rlm_t rlm_linelog;
1061  .common = {
1062  .magic = MODULE_MAGIC_INIT,
1063  .name = "linelog",
1064  .inst_size = sizeof(rlm_linelog_t),
1065  .config = module_config,
1066  .bootstrap = mod_bootstrap,
1068  .detach = mod_detach
1069  },
1070  .method_group = {
1071  .bindings = (module_method_binding_t[]){
1072  { .section = SECTION_NAME(CF_IDENT_ANY, CF_IDENT_ANY), .method = mod_do_linelog, .method_env = &linelog_method_env },
1074  }
1075  }
1076 };
unlang_action_t
Returned by unlang_op_t calls, determine the next action of the interpreter.
Definition: action.h:35
int const char * file
Definition: acutest.h:702
log_entry msg
Definition: acutest.h:794
va_list args
Definition: acutest.h:770
#define UNCONST(_type, _ptr)
Remove const qualification from a pointer.
Definition: build.h:165
#define RCSID(id)
Definition: build.h:481
#define L(_str)
Helper for initialising arrays of string literals.
Definition: build.h:207
#define FALL_THROUGH
clang 10 doesn't recognised the FALL-THROUGH comment anymore
Definition: build.h:320
#define unlikely(_x)
Definition: build.h:379
#define UNUSED
Definition: build.h:313
#define NUM_ELEMENTS(_t)
Definition: build.h:335
#define CALL_ENV_TERMINATOR
Definition: call_env.h:231
#define FR_CALL_ENV_METHOD_OUT(_inst)
Helper macro for populating the size/type fields of a call_env_method_t from the output structure typ...
Definition: call_env.h:235
#define FR_CALL_ENV_SUBSECTION(_name, _name2, _flags, _subcs)
Specify a call_env_parser_t which defines a nested subsection.
Definition: call_env.h:397
@ CALL_ENV_FLAG_CONCAT
If the tmpl produced multiple boxes they should be concatenated.
Definition: call_env.h:76
@ CALL_ENV_FLAG_PARSE_ONLY
The result of parsing will not be evaluated at runtime.
Definition: call_env.h:85
@ CALL_ENV_FLAG_NONE
Definition: call_env.h:74
module_instance_t const * mi
Module instance that the callenv is registered to.
Definition: call_env.h:224
#define FR_CALL_ENV_OFFSET(_name, _cast_type, _flags, _struct, _field)
Specify a call_env_parser_t which writes out runtime results to the specified field.
Definition: call_env.h:335
#define FR_CALL_ENV_PARSE_ONLY_OFFSET(_name, _cast_type, _flags, _struct, _parse_field)
Specify a call_env_parser_t which writes out the result of the parsing phase to the field specified.
Definition: call_env.h:384
Per method call config.
Definition: call_env.h:175
CONF_ITEM * cf_reference_item(CONF_SECTION const *parent_cs, CONF_SECTION const *outer_cs, char const *ptr)
Definition: cf_file.c:3611
#define CONF_PARSER_TERMINATOR
Definition: cf_parse.h:627
#define FR_CONF_DEPRECATED(_name, _struct, _field)
conf_parser_t entry which raises an error if a matching CONF_PAIR is found
Definition: cf_parse.h:385
#define FR_CONF_OFFSET(_name, _struct, _field)
conf_parser_t which parses a single CONF_PAIR, writing the result to a field in a struct
Definition: cf_parse.h:268
#define FR_CONF_POINTER(_name, _type, _flags, _res_p)
conf_parser_t which parses a single CONF_PAIR producing a single global result
Definition: cf_parse.h:310
#define FR_CONF_OFFSET_FLAGS(_name, _flags, _struct, _field)
conf_parser_t which parses a single CONF_PAIR, writing the result to a field in a struct
Definition: cf_parse.h:256
#define FR_CONF_OFFSET_SUBSECTION(_name, _flags, _struct, _field, _subcs)
conf_parser_t which populates a sub-struct using a CONF_SECTION
Definition: cf_parse.h:297
@ CONF_FLAG_REQUIRED
Error out if no matching CONF_PAIR is found, and no dflt value is set.
Definition: cf_parse.h:405
@ CONF_FLAG_FILE_INPUT
File matching value must exist, and must be readable.
Definition: cf_parse.h:411
@ CONF_FLAG_SUBSECTION
Instead of putting the information into a configuration structure, the configuration file routines MA...
Definition: cf_parse.h:399
#define FR_CONF_OFFSET_TYPE_FLAGS(_name, _type, _flags, _struct, _field)
conf_parser_t which parses a single CONF_PAIR, writing the result to a field in a struct
Definition: cf_parse.h:241
Defines a CONF_PAIR to C data type mapping.
Definition: cf_parse.h:564
Common header for all CONF_* types.
Definition: cf_priv.h:49
Configuration AVP similar to a fr_pair_t.
Definition: cf_priv.h:70
A section grouping multiple CONF_PAIR.
Definition: cf_priv.h:101
bool cf_item_is_pair(CONF_ITEM const *ci)
Determine if CONF_ITEM is a CONF_PAIR.
Definition: cf_util.c:632
CONF_PAIR * cf_item_to_pair(CONF_ITEM const *ci)
Cast a CONF_ITEM to a CONF_PAIR.
Definition: cf_util.c:664
CONF_SECTION * cf_section_find(CONF_SECTION const *cs, char const *name1, char const *name2)
Find a CONF_SECTION with name1 and optionally name2.
Definition: cf_util.c:1028
CONF_PAIR * cf_pair_find(CONF_SECTION const *cs, char const *attr)
Search for a CONF_PAIR with a specific name.
Definition: cf_util.c:1439
char const * cf_pair_value(CONF_PAIR const *pair)
Return the value of a CONF_PAIR.
Definition: cf_util.c:1594
fr_token_t cf_pair_value_quote(CONF_PAIR const *pair)
Return the value (rhs) quoting of a pair.
Definition: cf_util.c:1638
#define cf_log_err(_cf, _fmt,...)
Definition: cf_util.h:289
#define CF_IDENT_ANY
Definition: cf_util.h:78
#define fr_dbuff_used(_dbuff_or_marker)
Return the number of bytes remaining between the start of the dbuff or marker and the current positio...
Definition: dbuff.h:767
#define fr_dbuff_start(_dbuff_or_marker)
Return the 'start' position of a dbuff or marker.
Definition: dbuff.h:898
#define FR_DBUFF_TALLOC_THREAD_LOCAL(_out, _init, _max)
Create a function local and thread local extensible dbuff.
Definition: dbuff.h:556
static int fr_dcursor_insert(fr_dcursor_t *cursor, void *v)
Insert directly after the current item.
Definition: dcursor.h:435
fr_dcursor_eval_t void const * uctx
Definition: dcursor.h:546
static void * fr_dcursor_next(fr_dcursor_t *cursor)
Advanced the cursor to the next item.
Definition: dcursor.h:288
#define ERROR(fmt,...)
Definition: dhcpclient.c:41
static int sockfd
Definition: dhcpclient.c:56
static fr_time_delta_t timeout
Definition: dhcpclient.c:54
#define MODULE_MAGIC_INIT
Stop people using different module/library/server versions together.
Definition: dl_module.h:63
int exfile_open(exfile_t *ef, char const *filename, mode_t permissions, off_t *offset)
Open a new log file, or maybe an existing one.
Definition: exfile.c:506
int exfile_close(exfile_t *ef, int fd)
Close the log file.
Definition: exfile.c:562
ssize_t fr_mkdir(int *fd_out, char const *path, ssize_t len, mode_t mode, fr_mkdir_func_t func, void *uctx)
Create directories that are missing in the specified path.
Definition: file.c:219
IPv4/6 prefix.
Definition: merged_model.c:272
fr_event_list_t * unlang_interpret_event_list(request_t *request)
Get the event list for the current interpreter.
Definition: interpret.c:1764
TALLOC_CTX * unlang_interpret_frame_talloc_ctx(request_t *request)
Get a talloc_ctx which is valid only for this frame.
Definition: interpret.c:1403
ssize_t fr_writev(int fd, struct iovec vector[], int iovcnt, fr_time_delta_t timeout)
Write out a vector to a file descriptor.
Definition: iovec.c:68
fr_slen_t fr_concatv(fr_dbuff_t *out, struct iovec vector[], int iovcnt)
Concatenate an iovec into a dbuff.
Definition: iovec.c:38
fr_table_num_sorted_t const syslog_severity_table[]
Syslog severity table.
Definition: log.c:151
fr_table_num_sorted_t const syslog_facility_table[]
Syslog facility table.
Definition: log.c:66
#define PERROR(_fmt,...)
Definition: log.h:228
#define DEBUG3(_fmt,...)
Definition: log.h:266
#define RDEBUG_ENABLED3
True if request debug level 1-3 messages are enabled.
Definition: log.h:335
#define RWARN(fmt,...)
Definition: log.h:297
#define RERROR(fmt,...)
Definition: log.h:298
#define RPWARN(fmt,...)
Definition: log.h:301
#define RPERROR(fmt,...)
Definition: log.h:302
#define REMARKER(_str, _marker_idx, _marker,...)
Output string with error marker, showing where format error occurred.
Definition: log.h:498
#define RINFO(fmt,...)
Definition: log.h:296
#define RHEXDUMP3(_data, _len, _fmt,...)
Definition: log.h:705
int rad_filename_box_escape(fr_value_box_t *vb, UNUSED void *uxtc)
Definition: util.c:292
int rad_filename_box_make_safe(fr_value_box_t *vb, UNUSED void *uxtc)
Definition: util.c:167
talloc_free(reap)
unsigned short uint16_t
Definition: merged_model.c:31
@ FR_TYPE_STRING
String of printable characters.
Definition: merged_model.c:83
@ FR_TYPE_SIZE
Unsigned integer capable of representing any memory address on the local system.
Definition: merged_model.c:115
@ FR_TYPE_COMBO_IP_ADDR
IPv4 or IPv6 address depending on length.
Definition: merged_model.c:91
@ FR_TYPE_OCTETS
Raw octets.
Definition: merged_model.c:84
unsigned int uint32_t
Definition: merged_model.c:33
long int ssize_t
Definition: merged_model.c:24
long long int off_t
Definition: merged_model.c:22
unsigned long int size_t
Definition: merged_model.c:25
int fr_blocking(UNUSED int fd)
Definition: misc.c:298
void * env_data
Per call environment data.
Definition: module_ctx.h:44
module_instance_t const * mi
Instance of the module being instantiated.
Definition: module_ctx.h:42
void * rctx
Resume ctx that a module previously set.
Definition: module_ctx.h:45
module_instance_t * mi
Module instance to detach.
Definition: module_ctx.h:57
module_instance_t * mi
Instance of the module being instantiated.
Definition: module_ctx.h:51
Temporary structure to hold arguments for module calls.
Definition: module_ctx.h:41
Temporary structure to hold arguments for detach calls.
Definition: module_ctx.h:56
Temporary structure to hold arguments for instantiation calls.
Definition: module_ctx.h:50
exfile_t * module_rlm_exfile_init(TALLOC_CTX *ctx, CONF_SECTION *module, uint32_t max_entries, fr_time_delta_t max_idle, bool locking, char const *trigger_prefix, fr_pair_list_t *trigger_args)
Initialise a module specific exfile handle.
Definition: module_rlm.c:116
fr_pool_t * module_rlm_connection_pool_init(CONF_SECTION *module, void *opaque, fr_pool_connection_create_t c, fr_pool_connection_alive_t a, char const *log_prefix, char const *trigger_prefix, fr_pair_list_t *trigger_args)
Initialise a module specific connection pool.
Definition: module_rlm.c:308
xlat_t * module_rlm_xlat_register(TALLOC_CTX *ctx, module_inst_ctx_t const *mctx, char const *name, xlat_func_t func, fr_type_t return_type)
Definition: module_rlm.c:257
module_t common
Common fields presented by all modules.
Definition: module_rlm.h:39
int fr_perm_gid_from_str(TALLOC_CTX *ctx, gid_t *out, char const *name)
Resolve a group name to a GID.
Definition: perm.c:345
void fr_pool_connection_release(fr_pool_t *pool, request_t *request, void *conn)
Release a connection.
Definition: pool.c:1407
void fr_pool_free(fr_pool_t *pool)
Delete a connection pool.
Definition: pool.c:1329
fr_pool_state_t const * fr_pool_state(fr_pool_t *pool)
Get the number of connections currently in the pool.
Definition: pool.c:1173
void * fr_pool_connection_get(fr_pool_t *pool, request_t *request)
Reserve a connection in the connection pool.
Definition: pool.c:1392
void * fr_pool_connection_reconnect(fr_pool_t *pool, request_t *request, void *conn)
Reconnect a suspected inviable connection.
Definition: pool.c:1500
A connection pool.
Definition: pool.c:87
uint32_t num
Number of connections in the pool.
Definition: pool.h:67
char * fr_asprint(TALLOC_CTX *ctx, char const *in, ssize_t inlen, char quote)
Escape string that may contain binary data, and write it to a new buffer.
Definition: print.c:428
static const conf_parser_t config[]
Definition: base.c:183
static bool done
Definition: radclient.c:80
#define REDEBUG(fmt,...)
Definition: radclient.h:52
#define RDEBUG2(fmt,...)
Definition: radclient.h:54
#define DEBUG2(fmt,...)
Definition: radclient.h:43
static rs_t * conf
Definition: radsniff.c:53
#define RETURN_MODULE_NOOP
Definition: rcode.h:62
#define RETURN_MODULE_RCODE(_rcode)
Definition: rcode.h:64
rlm_rcode_t
Return codes indicating the result of the module call.
Definition: rcode.h:40
@ RLM_MODULE_OK
The module is OK, continue.
Definition: rcode.h:43
@ RLM_MODULE_FAIL
Module failed, don't reply.
Definition: rcode.h:42
@ RLM_MODULE_NOOP
Module succeeded without doing anything.
Definition: rcode.h:48
fr_dict_attr_t const * request_attr_request
Definition: request.c:45
static int mod_detach(module_detach_ctx_t const *mctx)
Definition: rlm_linelog.c:913
static int call_env_filename_parse(TALLOC_CTX *ctx, void *out, tmpl_rules_t const *t_rules, CONF_ITEM *ci, call_env_ctx_t const *cec, UNUSED call_env_parser_t const *rule)
Definition: rlm_linelog.c:884
module_rlm_t rlm_linelog
Definition: rlm_linelog.c:1060
tmpl_t * log_src
Source of log messages.
Definition: rlm_linelog.c:202
static const conf_parser_t file_config[]
Definition: rlm_linelog.c:143
static const call_env_method_t linelog_xlat_method_env
Definition: rlm_linelog.c:228
linelog_net_t tcp
TCP server.
Definition: rlm_linelog.c:132
linelog_net_t udp
UDP server.
Definition: rlm_linelog.c:133
static const conf_parser_t syslog_config[]
Definition: rlm_linelog.c:151
fr_ipaddr_t src_ipaddr
Send requests from a given src_ipaddr.
Definition: rlm_linelog.c:96
size_t delimiter_len
Length of line termination string.
Definition: rlm_linelog.c:107
static xlat_action_t linelog_xlat(TALLOC_CTX *ctx, fr_dcursor_t *out, xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *args)
Definition: rlm_linelog.c:576
static int _mod_conn_free(linelog_conn_t *conn)
Definition: rlm_linelog.c:242
static unlang_action_t mod_do_linelog(rlm_rcode_t *p_result, module_ctx_t const *mctx, request_t *request)
Write a linelog message.
Definition: rlm_linelog.c:684
static int mod_bootstrap(module_inst_ctx_t const *mctx)
Definition: rlm_linelog.c:1040
fr_value_box_list_t expanded
The result of expanding the fmt tmpl.
Definition: rlm_linelog.c:613
#define VECTOR_INCREMENT
fr_ipaddr_t dst_ipaddr
Network server.
Definition: rlm_linelog.c:95
static const conf_parser_t udp_config[]
Definition: rlm_linelog.c:162
static void linelog_hexdump(request_t *request, struct iovec *vector_p, size_t vector_len, char const *msg)
Definition: rlm_linelog.c:353
fr_pool_t * pool
Connection pool instance.
Definition: rlm_linelog.c:104
static int linelog_write(rlm_linelog_t const *inst, linelog_call_env_t const *call_env, request_t *request, struct iovec *vector_p, size_t vector_len, bool with_delim)
Definition: rlm_linelog.c:363
fr_time_delta_t timeout
How long to wait for read/write operations.
Definition: rlm_linelog.c:98
int sockfd
File descriptor associated with socket.
Definition: rlm_linelog.c:139
linefr_log_dst_t
Definition: rlm_linelog.c:69
@ LINELOG_DST_FILE
Log to a file.
Definition: rlm_linelog.c:71
@ LINELOG_DST_STDERR
Log to stderr.
Definition: rlm_linelog.c:78
@ LINELOG_DST_UNIX
Log via Unix socket.
Definition: rlm_linelog.c:74
@ LINELOG_DST_STDOUT
Log to stdout.
Definition: rlm_linelog.c:77
@ LINELOG_DST_TCP
Log via TCP.
Definition: rlm_linelog.c:76
@ LINELOG_DST_INVALID
Definition: rlm_linelog.c:70
@ LINELOG_DST_REQUEST
Log to the request->log.
Definition: rlm_linelog.c:72
@ LINELOG_DST_UDP
Log via UDP.
Definition: rlm_linelog.c:75
@ LINELOG_DST_SYSLOG
Log to syslog.
Definition: rlm_linelog.c:73
fr_value_box_t * log_head
Header to add to each new log file.
Definition: rlm_linelog.c:207
static void * mod_conn_create(TALLOC_CTX *ctx, void *instance, fr_time_delta_t timeout)
Definition: rlm_linelog.c:250
static unlang_action_t mod_do_linelog_resume(rlm_rcode_t *p_result, module_ctx_t const *mctx, request_t *request)
Definition: rlm_linelog.c:617
uint16_t port
Network port.
Definition: rlm_linelog.c:97
static fr_table_num_sorted_t const linefr_log_dst_table[]
Definition: rlm_linelog.c:81
static const call_env_method_t linelog_method_env
Definition: rlm_linelog.c:212
CONF_SECTION * cs
CONF_SECTION to use as the root for #log_ref lookups.
Definition: rlm_linelog.c:135
static const conf_parser_t module_config[]
Definition: rlm_linelog.c:176
static int linelog_escape_func(fr_value_box_t *vb, UNUSED void *uctx)
Escape unprintable characters.
Definition: rlm_linelog.c:341
fr_value_box_t * log_ref
Path to a CONF_PAIR (to use as the source of log messages).
Definition: rlm_linelog.c:204
static const conf_parser_t unix_config[]
Definition: rlm_linelog.c:157
fr_value_box_t * filename
File name, if output is to a file.
Definition: rlm_linelog.c:209
static size_t linefr_log_dst_table_len
Definition: rlm_linelog.c:92
static int mod_instantiate(module_inst_ctx_t const *mctx)
Definition: rlm_linelog.c:925
char const * delimiter
Line termination string (usually ).
Definition: rlm_linelog.c:106
bool with_delim
Whether to add a delimiter.
Definition: rlm_linelog.c:614
static const conf_parser_t tcp_config[]
Definition: rlm_linelog.c:169
linefr_log_dst_t log_dst
Logging destination.
Definition: rlm_linelog.c:109
char const * log_dst_str
Logging destination string.
Definition: rlm_linelog.c:110
linelog module instance
Definition: rlm_linelog.c:103
static int instantiate(module_inst_ctx_t const *mctx)
Definition: rlm_rest.c:1302
#define FR_SBUFF_IN(_start, _len_or_end)
#define SECTION_NAME(_name1, _name2)
Define a section name consisting of a verb and a noun.
Definition: section.h:40
char const * name
Instance name e.g. user_database.
Definition: module.h:335
CONF_SECTION * conf
Module's instance configuration.
Definition: module.h:329
void * data
Module's instance data.
Definition: module.h:271
void * boot
Data allocated during the boostrap phase.
Definition: module.h:274
#define MODULE_BINDING_TERMINATOR
Terminate a module binding list.
Definition: module.h:151
Named methods exported by a module.
Definition: module.h:173
static fr_slen_t vpt
Definition: tmpl.h:1272
tmpl_escape_t escape
How escaping should be handled during evaluation.
Definition: tmpl.h:358
int tmpl_resolve(tmpl_t *vpt, tmpl_res_rules_t const *tr_rules))
Attempt to resolve functions and attributes in xlats and attribute references.
fr_value_box_safe_for_t literals_safe_for
safe_for value assigned to literal values in xlats, execs, and data.
Definition: tmpl.h:356
@ TMPL_TYPE_ATTR
Reference to one or more attributes.
Definition: tmpl.h:146
@ TMPL_TYPE_DATA
Value in native boxed format.
Definition: tmpl.h:142
ssize_t tmpl_afrom_substr(TALLOC_CTX *ctx, tmpl_t **out, fr_sbuff_t *in, fr_token_t quote, fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules))
Convert an arbitrary string into a tmpl_t.
tmpl_t * tmpl_init_shallow(tmpl_t *vpt, tmpl_type_t type, fr_token_t quote, char const *name, ssize_t len, tmpl_rules_t const *t_rules))
Initialise a tmpl without copying the input name string.
Optional arguments passed to vp_tmpl functions.
Definition: tmpl.h:341
static char buff[sizeof("18446744073709551615")+3]
Definition: size_tests.c:41
PUBLIC int snprintf(char *string, size_t length, char *format, va_alist)
Definition: snprintf.c:689
int fr_socket_client_udp(char const *ifname, fr_ipaddr_t *src_ipaddr, uint16_t *src_port, fr_ipaddr_t const *dst_ipaddr, uint16_t dst_port, bool async)
Establish a connected UDP socket.
Definition: socket.c:634
int fr_socket_client_tcp(char const *ifname, fr_ipaddr_t *src_ipaddr, fr_ipaddr_t const *dst_ipaddr, uint16_t dst_port, bool async)
Establish a connected TCP socket.
Definition: socket.c:729
int fr_socket_wait_for_connect(int sockfd, fr_time_delta_t timeout)
Wait for a socket to be connected, with an optional timeout.
Definition: socket.c:803
int fr_socket_client_unix(UNUSED char const *path, UNUSED bool async)
Definition: socket.c:564
unlang_action_t unlang_module_yield_to_tmpl(TALLOC_CTX *ctx, fr_value_box_list_t *out, request_t *request, tmpl_t const *vpt, unlang_tmpl_args_t *args, module_method_t resume, unlang_module_signal_t signal, fr_signal_t sigmask, void *rctx)
Push a pre-compiled tmpl and resumption state onto the stack for evaluation.
Definition: module.c:228
RETURN_MODULE_FAIL
fr_assert(0)
MEM(pair_append_request(&vp, attr_eap_aka_sim_identity) >=0)
eap_aka_sim_process_conf_t * inst
fr_pair_t * vp
Stores an attribute, a value and various bits of other data.
Definition: pair.h:68
char const * fr_syserror(int num)
Guaranteed to be thread-safe version of strerror.
Definition: syserror.c:243
#define fr_table_value_by_str(_table, _name, _def)
Convert a string to a value using a sorted or ordered table.
Definition: table.h:653
#define fr_table_str_by_value(_table, _number, _def)
Convert an integer to a string.
Definition: table.h:772
An element in a lexicographically sorted array of name to num mappings.
Definition: table.h:49
#define talloc_get_type_abort_const
Definition: talloc.h:282
static size_t talloc_strlen(char const *s)
Returns the length of a talloc array containing a string.
Definition: talloc.h:289
static fr_time_delta_t fr_time_delta_from_sec(int64_t sec)
Definition: time.h:590
#define fr_time_delta_wrap(_time)
Definition: time.h:152
#define fr_time_delta_ispos(_a)
Definition: time.h:290
A time delta, a difference in time measured in nanoseconds.
Definition: time.h:80
void tmpl_dcursor_clear(tmpl_dcursor_ctx_t *cc)
Clear any temporary state allocations.
Definition: tmpl_dcursor.c:495
#define tmpl_dcursor_init(_err, _ctx, _cc, _cursor, _request, _vpt)
Definition: tmpl_dcursor.h:100
Maintains state between cursor calls.
Definition: tmpl_dcursor.h:62
fr_value_box_escape_t func
How to escape when returned from evaluation.
Definition: tmpl_escape.h:81
@ TMPL_ESCAPE_PRE_CONCAT
Pre-concatenation escaping is useful for DSLs where elements of the expansion are static,...
Definition: tmpl_escape.h:61
fr_value_box_safe_for_t safe_for
Value to set on boxes which have been escaped by the fr_value_box_escape_t function.
Definition: tmpl_escape.h:83
tmpl_escape_mode_t mode
Whether to apply escape function after concatenation, i.e.
Definition: tmpl_escape.h:86
@ T_DOUBLE_QUOTED_STRING
Definition: token.h:121
close(uq->fd)
bool required
Argument must be present, and non-empty.
Definition: xlat.h:146
#define XLAT_ARGS(_list,...)
Populate local variables with value boxes from the input list.
Definition: xlat.h:365
#define XLAT_ARG_PARSER_TERMINATOR
Definition: xlat.h:166
xlat_action_t
Definition: xlat.h:35
@ XLAT_ACTION_FAIL
An xlat function failed.
Definition: xlat.h:42
@ XLAT_ACTION_DONE
We're done evaluating this level of nesting.
Definition: xlat.h:41
Definition for a single argument consumend by an xlat function.
Definition: xlat.h:145
char const * fr_strerror(void)
Get the last library error.
Definition: strerror.c:554
fr_sbuff_parse_rules_t const * value_parse_rules_quoted[T_TOKEN_LAST]
Parse rules for quoted strings.
Definition: value.c:606
int fr_value_box_cast_in_place(TALLOC_CTX *ctx, fr_value_box_t *vb, fr_type_t dst_type, fr_dict_attr_t const *dst_enumv)
Convert one type of fr_value_box_t to another in place.
Definition: value.c:3572
void fr_value_box_strdup_shallow_replace(fr_value_box_t *vb, char const *src, ssize_t len)
Free the existing buffer (if talloced) associated with the valuebox, and replace it with a new one.
Definition: value.c:4052
void fr_value_box_strdup_shallow(fr_value_box_t *dst, fr_dict_attr_t const *enumv, char const *src, bool tainted)
Assign a buffer containing a nul terminated string to a box, but don't copy it.
Definition: value.c:4036
#define fr_value_box_alloc(_ctx, _type, _enumv)
Allocate a value box of a specific type.
Definition: value.h:621
static fr_slen_t fr_value_box_aprint(TALLOC_CTX *ctx, char **out, fr_value_box_t const *data, fr_sbuff_escape_rules_t const *e_rules) 1(fr_value_box_print
#define fr_box_ipaddr(_val)
Definition: value.h:294
#define fr_value_box_init_null(_vb)
Initialise an empty/null box that will be filled later.
Definition: value.h:593
uintptr_t fr_value_box_safe_for_t
Escaping that's been applied to a value box.
Definition: value.h:155
int nonnull(2, 5))
#define fr_value_box_list_foreach(_list_head, _iter)
Definition: value.h:206
static size_t char ** out
Definition: value.h:997
void * env_data
Expanded call env data.
Definition: xlat_ctx.h:53
module_ctx_t const * mctx
Synthesised module calling ctx.
Definition: xlat_ctx.h:52
An xlat calling ctx.
Definition: xlat_ctx.h:49
int xlat_func_args_set(xlat_t *x, xlat_arg_parser_t const args[])
Register the arguments of an xlat.
Definition: xlat_func.c:365
void xlat_func_call_env_set(xlat_t *x, call_env_method_t const *env_method)
Register call environment of an xlat.
Definition: xlat_func.c:392