1 /*
2 * Copyright (c) 2018 Balabit
3 * Copyright (c) 2018 László Várady <laszlo.varady@balabit.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published
7 * by the Free Software Foundation, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 *
18 * As an additional exemption you are allowed to compile & link against the
19 * OpenSSL libraries as published by the OpenSSL project. See the file
20 * COPYING for details.
21 *
22 */
23
24 #include "threaded-random-generator.h"
25 #include "logthrsource/logthrsourcedrv.h"
26 #include "atomic.h"
27 #include "messages.h"
28 #include "str-format.h"
29
30 #include <strings.h>
31 #include <sys/random.h>
32 #include <unistd.h>
33
34 struct ThreadedRandomGeneratorSourceDriver
35 {
36 LogThreadedSourceDriver super;
37 GAtomicCounter exit_requested;
38
39 guint freq;
40 guint bytes;
41 guint flags;
42 };
43
44 static gboolean
_generate_random_bytes(guint8 * random,guint size,guint flags)45 _generate_random_bytes(guint8 *random, guint size, guint flags)
46 {
47 guint length = 0;
48
49 while (length < size)
50 {
51 gssize rc = getrandom(random + length, size - length, flags);
52
53 if (rc < 0)
54 {
55 msg_error("Could not generate random bytes", evt_tag_error("error"));
56 return FALSE;
57 }
58
59 length += rc;
60 }
61
62 return TRUE;
63 }
64
65
66 /* runs in a dedicated thread */
67 static void
_run(LogThreadedSourceDriver * s)68 _run(LogThreadedSourceDriver *s)
69 {
70 ThreadedRandomGeneratorSourceDriver *self = (ThreadedRandomGeneratorSourceDriver *) s;
71
72 guint8 *random_bytes = g_malloc(self->bytes);
73
74 const gsize random_hex_str_size = self->bytes * 2 + 1;
75 gchar *random_hex_str = g_malloc(random_hex_str_size);
76
77 while (!g_atomic_counter_get(&self->exit_requested))
78 {
79 if (_generate_random_bytes(random_bytes, self->bytes, self->flags))
80 {
81 format_hex_string(random_bytes, self->bytes, random_hex_str, random_hex_str_size);
82
83 LogMessage *msg = log_msg_new_empty();
84 log_msg_set_value(msg, LM_V_MESSAGE, random_hex_str, -1);
85
86 log_threaded_source_blocking_post(s, msg);
87 }
88
89 usleep(self->freq * 1000);
90 }
91
92 g_free(random_hex_str);
93 g_free(random_bytes);
94 }
95
96 static void
_request_exit(LogThreadedSourceDriver * s)97 _request_exit(LogThreadedSourceDriver *s)
98 {
99 ThreadedRandomGeneratorSourceDriver *self = (ThreadedRandomGeneratorSourceDriver *) s;
100
101 g_atomic_counter_set(&self->exit_requested, TRUE);
102 }
103
104 static gboolean
_init(LogPipe * s)105 _init(LogPipe *s)
106 {
107 ThreadedRandomGeneratorSourceDriver *self = (ThreadedRandomGeneratorSourceDriver *) s;
108
109 if (!self->bytes)
110 {
111 msg_error("The bytes() option for random-generator() is mandatory", log_pipe_location_tag(s));
112 return FALSE;
113 }
114
115 if (!log_threaded_source_driver_init_method(s))
116 return FALSE;
117
118 log_threaded_source_driver_set_worker_run_func(&self->super, _run);
119 log_threaded_source_driver_set_worker_request_exit_func(&self->super, _request_exit);
120
121 return TRUE;
122 }
123
124 static const gchar *
_format_stats_instance(LogThreadedSourceDriver * s)125 _format_stats_instance(LogThreadedSourceDriver *s)
126 {
127 ThreadedRandomGeneratorSourceDriver *self = (ThreadedRandomGeneratorSourceDriver *) s;
128 static gchar persist_name[1024];
129
130 if (s->super.super.super.persist_name)
131 g_snprintf(persist_name, sizeof(persist_name), "random-generator,%s", s->super.super.super.persist_name);
132 else
133 g_snprintf(persist_name, sizeof(persist_name), "random-generator,%u,%u,%u", self->bytes, self->freq, self->flags);
134
135 return persist_name;
136 }
137
138 void
threaded_random_generator_sd_set_freq(LogDriver * s,gdouble freq)139 threaded_random_generator_sd_set_freq(LogDriver *s, gdouble freq)
140 {
141 ThreadedRandomGeneratorSourceDriver *self = (ThreadedRandomGeneratorSourceDriver *) s;
142 self->freq = (guint) (freq * 1000);
143 }
144
145 void
threaded_random_generator_sd_set_bytes(LogDriver * s,guint bytes)146 threaded_random_generator_sd_set_bytes(LogDriver *s, guint bytes)
147 {
148 ThreadedRandomGeneratorSourceDriver *self = (ThreadedRandomGeneratorSourceDriver *) s;
149 self->bytes = bytes;
150 }
151
152 gboolean
threaded_random_generator_sd_set_type(LogDriver * s,const gchar * type)153 threaded_random_generator_sd_set_type(LogDriver *s, const gchar *type)
154 {
155 ThreadedRandomGeneratorSourceDriver *self = (ThreadedRandomGeneratorSourceDriver *) s;
156
157 if (strcasecmp(type, "random") == 0)
158 self->flags = GRND_RANDOM;
159 else if (strcasecmp(type, "urandom") == 0)
160 self->flags = 0;
161 else
162 return FALSE;
163
164 return TRUE;
165 }
166
167 LogDriver *
threaded_random_generator_sd_new(GlobalConfig * cfg)168 threaded_random_generator_sd_new(GlobalConfig *cfg)
169 {
170 ThreadedRandomGeneratorSourceDriver *self = g_new0(ThreadedRandomGeneratorSourceDriver, 1);
171 log_threaded_source_driver_init_instance(&self->super, cfg);
172
173 self->freq = 1000;
174 self->flags = GRND_RANDOM;
175 g_atomic_counter_set(&self->exit_requested, FALSE);
176
177 self->super.super.super.super.init = _init;
178 self->super.format_stats_instance = _format_stats_instance;
179
180 return &self->super.super.super;
181 }
182