File: tfgetent.c

package info (click to toggle)
syslog-ng-incubator 0.5.0-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 984 kB
  • ctags: 911
  • sloc: ansic: 4,707; makefile: 400; perl: 19; sh: 9
file content (190 lines) | stat: -rw-r--r-- 4,620 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
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
/*
 * Copyright (c) 2014 BalaBit IT Ltd, Budapest, Hungary
 * Copyright (c) 2014 Gergely Nagy <algernon@balabit.hu>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published
 * by the Free Software Foundation, 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
 *
 * 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 <syslog-ng.h>
#include <logmsg/logmsg.h>
#include <plugin.h>
#include <plugin-types.h>
#include <cfg.h>
#include <parse-number.h>
#include <template/simple-function.h>

#include <grp.h>
#include <pwd.h>
#include <netdb.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <stddef.h>


typedef gboolean (*lookup_method)(gchar *key, gchar *member_name, GString *result);
typedef gboolean (*format_member)(gchar *member_name, gpointer member, GString *result);

typedef struct
{
  gchar *member_name;
  format_member format;
  size_t offset;
} formatter_map_t;

static gboolean
_getent_format_array(gchar *member_name, gpointer member, GString *result)
{
  int i = 0;
  char *o = *(char **)member;
  char *p = *(char **)o;
  char *sep = "";

  do
    {
      g_string_append(result, sep);
      g_string_append(result, p);
      sep = ",";
      p = *((char **)o + ++i);
    }
  while (p != NULL && p != '\0');

  return TRUE;
}

static gboolean
_getent_format_string(gchar *member_name, gpointer member, GString *result)
{
  char *value = *(char **)member;

  g_string_append(result, value);
  return TRUE;
}

static gboolean
_getent_format_uid_gid(gchar *member_name, gpointer member, GString *result)
{
  if (strcmp(member_name, "uid") == 0)
    {
      uid_t u = *(uid_t *)member;

      g_string_append_printf(result, "%" G_GUINT64_FORMAT, (guint64)u);
    }
  else
    {
      gid_t g = *(gid_t *)member;

      g_string_append_printf(result, "%" G_GUINT64_FORMAT, (guint64)g);
    }

  return TRUE;
}

static int
_find_formatter(formatter_map_t *map, gchar *member_name)
{
  gint i = 0;

  while (map[i].member_name != NULL)
    {
      if (strcmp(map[i].member_name, member_name) == 0)
        return i;
      i++;
    }

  return -1;
}

#include "getent-protocols.c"
#include "getent-services.c"
#include "getent-group.c"
#include "getent-passwd.c"

static struct
{
  gchar *entity;
  lookup_method lookup;
} tf_getent_lookup_map[] =  {
  { "group", tf_getent_group },
  { "passwd", tf_getent_passwd },
  { "services", tf_getent_services },
  { "protocols", tf_getent_protocols },
  { NULL, NULL }
};

static lookup_method
tf_getent_find_lookup_method(gchar *entity)
{
  gint i = 0;

  while (tf_getent_lookup_map[i].entity != NULL)
    {
      if (strcmp(tf_getent_lookup_map[i].entity, entity) == 0)
        return tf_getent_lookup_map[i].lookup;
      i++;
    }
  return NULL;
}

static gboolean
tf_getent(LogMessage *msg, gint argc, GString *argv[], GString *result)
{
  lookup_method lookup;

  if (argc != 2 && argc != 3)
    {
      msg_error("$(getent) takes either two or three arguments",
                evt_tag_int("argc", argc),
                NULL);
      return FALSE;
    }

  lookup = tf_getent_find_lookup_method(argv[0]->str);
  if (!lookup)
    {
      msg_error("Unsupported $(getent) NSS service",
                evt_tag_str("service", argv[0]->str),
                NULL);
      return FALSE;
    }

  return lookup(argv[1]->str, (argc == 2) ? NULL : argv[2]->str, result);
}
TEMPLATE_FUNCTION_SIMPLE(tf_getent);

static Plugin getent_plugins[] =
{
  TEMPLATE_FUNCTION_PLUGIN(tf_getent, "getent"),
};

gboolean
getent_plugin_module_init(GlobalConfig *cfg, CfgArgs *args)
{
  plugin_register(cfg, getent_plugins, G_N_ELEMENTS(getent_plugins));
  return TRUE;
}

const ModuleInfo module_info =
{
  .canonical_name = "getent-plugin",
  .version = SYSLOG_NG_VERSION,
  .description = "The getent module provides getent template functions for syslog-ng.",
  .core_revision = VERSION_CURRENT_VER_ONLY,
  .plugins = getent_plugins,
  .plugins_len = G_N_ELEMENTS(getent_plugins),
};