File: rewrite-set-facility.c

package info (click to toggle)
syslog-ng 3.28.1-2%2Bdeb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 15,028 kB
  • sloc: ansic: 132,531; python: 5,838; makefile: 5,195; sh: 4,580; java: 3,555; xml: 3,344; yacc: 1,209; lex: 493; perl: 193; awk: 184
file content (144 lines) | stat: -rw-r--r-- 4,183 bytes parent folder | download
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
/*
 * Copyright (c) 2020 Balazs Scheidler <bazsi77@gmail.com>
 * Copyright (c) 2019 Balabit
 * Copyright (c) 2019 Kokan <kokaipeter@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * As an additional exemption you are allowed to compile & link against the
 * OpenSSL libraries as published by the OpenSSL project. See the file
 * COPYING for details.
 *
 */

#include "rewrite-set-facility.h"
#include "template/templates.h"
#include "syslog-names.h"
#include "scratch-buffers.h"
#include "parse-number.h"

#include <stdlib.h>

typedef struct _LogRewriteSetFacility LogRewriteSetFacility;

struct _LogRewriteSetFacility
{
  LogRewrite super;
  LogTemplate *facility;
};

static gint
_convert_facility_as_number(const gchar *facility_text)
{
  gint64 facility = 0;

  if (!parse_dec_number(facility_text, &facility))
    return -1;

  if (facility > 127)
    return -1;

  return FACILITY_CODE(facility);
}

static gint
_convert_facility_as_text(const gchar *facility_text)
{
  return syslog_name_lookup_facility_by_name(facility_text);
}

static gint
_convert_facility(const gchar *facility_text)
{
  gint facility = _convert_facility_as_number(facility_text);
  if (facility >= 0)
    return facility;

  facility = _convert_facility_as_text(facility_text);
  if (facility >= 0)
    return facility;

  return -1;
}

static void
_set_msg_facility(LogMessage *msg, const guint32 facility)
{
  msg->pri = (msg->pri & ~LOG_FACMASK) | facility;
}

static void
log_rewrite_set_facility_process(LogRewrite *s, LogMessage **pmsg, const LogPathOptions *path_options)
{
  ScratchBuffersMarker marker;
  GString *result = scratch_buffers_alloc_and_mark(&marker);
  LogRewriteSetFacility *self = (LogRewriteSetFacility *) s;

  log_msg_make_writable(pmsg, path_options);

  log_template_format(self->facility, *pmsg, NULL, LTZ_LOCAL, 0, NULL, result);

  const gint facility = _convert_facility(result->str);
  if (facility < 0)
    {
      msg_debug("Warning: invalid value passed to set-facility()",
                evt_tag_str("facility", result->str),
                log_pipe_location_tag(&s->super));
      goto error;
    }

  msg_trace("Setting syslog facility",
            evt_tag_int("old_facility", (*pmsg)->pri & LOG_FACMASK),
            evt_tag_int("new_facility", facility),
            evt_tag_printf("msg", "%p", *pmsg));
  _set_msg_facility(*pmsg, facility);

error:
  scratch_buffers_reclaim_marked(marker);
}

static LogPipe *
log_rewrite_set_facility_clone(LogPipe *s)
{
  LogRewriteSetFacility *self = (LogRewriteSetFacility *) s;
  LogRewriteSetFacility *cloned = (LogRewriteSetFacility *)log_rewrite_set_facility_new(log_template_ref(self->facility),
                                  s->cfg);

  if (self->super.condition)
    cloned->super.condition = filter_expr_ref(self->super.condition);

  return &cloned->super.super;
}

void
log_rewrite_set_facility_free(LogPipe *s)
{
  LogRewriteSetFacility *self = (LogRewriteSetFacility *) s;
  log_template_unref(self->facility);
  log_rewrite_free_method(s);
}

LogRewrite *
log_rewrite_set_facility_new(LogTemplate *facility, GlobalConfig *cfg)
{
  LogRewriteSetFacility *self = g_new0(LogRewriteSetFacility, 1);

  log_rewrite_init_instance(&self->super, cfg);
  self->super.super.free_fn = log_rewrite_set_facility_free;
  self->super.super.clone = log_rewrite_set_facility_clone;
  self->super.process = log_rewrite_set_facility_process;
  self->facility = log_template_ref(facility);
  return &self->super;
}