Debian Codegraph @

modules/mod_redis.c

raw symbols SCIP
1 /*
2 * ProFTPD: mod_redis -- a module for managing Redis data
3 * Copyright (c) 2017-2025 The ProFTPD Project
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18 *
19 * As a special exemption, TJ Saunders and other respective copyright holders
20 * give permission to link this program with OpenSSL, and distribute the
21 * resulting executable, without including the source code for OpenSSL in the
22 * source distribution.
23 *
24 * -----DO NOT EDIT BELOW THIS LINE-----
25 * $Libraries: -lhiredis$
26 */
27
28 #include "conf.h"
29 #include "privs.h"
30 #include "logfmt.h"
31 #include "json.h"
32 #include "jot.h"
33
34 #define MOD_REDIS_VERSION "mod_redis/0.2.3"
35
36 #if PROFTPD_VERSION_NUMBER < 0x0001030605
37 # error "ProFTPD 1.3.6rc5 or later required"
38 #endif
39
40 #include <hiredis/hiredis.h>
41
42 extern xaset_t *server_list;
43
44 module redis_module;
45
46 #define REDIS_SERVER_DEFAULT_PORT 6379
47 #define REDIS_SENTINEL_DEFAULT_PORT 26379
48
49 static int redis_engine = FALSE;
50 static int redis_logfd = -1;
51 static unsigned long redis_opts = 0UL;
52 static pool *redis_pool = NULL;
53
54 static int redis_sess_init(void);
55
56 static pr_table_t *jot_logfmt2json = NULL;
57 static const char *trace_channel = "redis";
58
59 /* TODO: Refactor this into a pr_jot_resolved_t structure, with shared/common
60 * resolver callbacks, similar to the shared parser callbacks.
61 */
62
63 struct redis_buffer {
64 char *ptr, *buf;
65 size_t bufsz, buflen;
66 };
67
68 static void redis_buffer_append_text(struct redis_buffer *log, const char *text,
69 size_t text_len) {
70 if (text == NULL ||
71 text_len == 0) {
72 return;
73 }
74
75 if (text_len > log->buflen) {
76 text_len = log->buflen;
77 }
78
79 pr_trace_msg(trace_channel, 19, "appending text '%.*s' (%lu) to buffer",
80 (int) text_len, text, (unsigned long) text_len);
81 memcpy(log->buf, text, text_len);
82 log->buf += text_len;
83 log->buflen -= text_len;
84 }
85
86 static int resolve_on_meta(pool *p, pr_jot_ctx_t *jot_ctx,
87 unsigned char logfmt_id, const char *jot_hint, const void *val) {
88 struct redis_buffer *log;
89
90 log = jot_ctx->log;
91 if (log->buflen > 0) {
92 const char *text = NULL;
93 size_t text_len = 0;
94 char buf[1024];
95
96 switch (logfmt_id) {
97 case LOGFMT_META_MICROSECS: {
98 unsigned long num;
99
100 num = *((double *) val);
101 text_len = pr_snprintf(buf, sizeof(buf)-1, "%06lu", num);
102 buf[text_len] = '\0';
103 text = buf;
104 break;
105 }
106
107 case LOGFMT_META_MILLISECS: {
108 unsigned long num;
109
110 num = *((double *) val);
111 text_len = pr_snprintf(buf, sizeof(buf)-1, "%03lu", num);
112 buf[text_len] = '\0';
113 text = buf;
114 break;
115 }
116
117 case LOGFMT_META_LOCAL_PORT:
118 case LOGFMT_META_REMOTE_PORT:
119 case LOGFMT_META_RESPONSE_CODE:
120 case LOGFMT_META_XFER_PORT: {
121 int num;
122
123 num = *((double *) val);
124 text_len = pr_snprintf(buf, sizeof(buf)-1, "%d", num);
125 buf[text_len] = '\0';
126 text = buf;
127 break;
128 }
129
130 case LOGFMT_META_UID: {
131 uid_t uid;
132
133 uid = *((double *) val);
134 text = pr_uid2str(p, uid);
135 break;
136 }
137
138 case LOGFMT_META_GID: {
139 gid_t gid;
140
141 gid = *((double *) val);
142 text = pr_gid2str(p, gid);
143 break;
144 }
145
146 case LOGFMT_META_BYTES_SENT:
147 case LOGFMT_META_FILE_OFFSET:
148 case LOGFMT_META_FILE_SIZE:
149 case LOGFMT_META_RAW_BYTES_IN:
150 case LOGFMT_META_RAW_BYTES_OUT:
151 case LOGFMT_META_RESPONSE_MS:
152 case LOGFMT_META_XFER_MS: {
153 off_t num;
154
155 num = *((double *) val);
156 text_len = pr_snprintf(buf, sizeof(buf)-1, "%" PR_LU, (pr_off_t) num);
157 buf[text_len] = '\0';
158 text = buf;
159 break;
160 }
161
162 case LOGFMT_META_EPOCH:
163 case LOGFMT_META_PID: {
164 unsigned long num;
165
166 num = *((double *) val);
167 text_len = pr_snprintf(buf, sizeof(buf)-1, "%lu", num);
168 buf[text_len] = '\0';
169 text = buf;
170 break;
171 }
172
173 case LOGFMT_META_FILE_MODIFIED: {
174 int truth;
175
176 truth = *((int *) val);
177 text = truth ? "true" : "false";
178 break;
179 }
180
181 case LOGFMT_META_SECONDS: {
182 float num;
183
184 num = *((double *) val);
185 text_len = pr_snprintf(buf, sizeof(buf)-1, "%0.3f", num);
186 buf[text_len] = '\0';
187 text = buf;
188 break;
189 }
190
191 case LOGFMT_META_ANON_PASS:
192 case LOGFMT_META_BASENAME:
193 case LOGFMT_META_CLASS:
194 case LOGFMT_META_CMD_PARAMS:
195 case LOGFMT_META_COMMAND:
196 case LOGFMT_META_DIR_NAME:
197 case LOGFMT_META_DIR_PATH:
198 case LOGFMT_META_ENV_VAR:
199 case LOGFMT_META_EOS_REASON:
200 case LOGFMT_META_FILENAME:
201 case LOGFMT_META_GROUP:
202 case LOGFMT_META_IDENT_USER:
203 case LOGFMT_META_ISO8601:
204 case LOGFMT_META_LOCAL_FQDN:
205 case LOGFMT_META_LOCAL_IP:
206 case LOGFMT_META_LOCAL_NAME:
207 case LOGFMT_META_METHOD:
208 case LOGFMT_META_NOTE_VAR:
209 case LOGFMT_META_ORIGINAL_USER:
210 case LOGFMT_META_PROTOCOL:
211 case LOGFMT_META_REMOTE_HOST:
212 case LOGFMT_META_REMOTE_IP:
213 case LOGFMT_META_RENAME_FROM:
214 case LOGFMT_META_RESPONSE_STR:
215 case LOGFMT_META_TIME:
216 case LOGFMT_META_USER:
217 case LOGFMT_META_VERSION:
218 case LOGFMT_META_VHOST_IP:
219 case LOGFMT_META_XFER_FAILURE:
220 case LOGFMT_META_XFER_PATH:
221 case LOGFMT_META_XFER_SPEED:
222 case LOGFMT_META_XFER_STATUS:
223 case LOGFMT_META_XFER_TYPE:
224 default:
225 text = val;
226 }
227
228 if (text != NULL &&
229 text_len == 0) {
230 text_len = strlen(text);
231 }
232
233 redis_buffer_append_text(log, text, text_len);
234 }
235
236 return 0;
237 }
238
239 static int resolve_on_other(pool *p, pr_jot_ctx_t *jot_ctx, unsigned char *text,
240 size_t text_len) {
241 struct redis_buffer *log;
242
243 log = jot_ctx->log;
244 redis_buffer_append_text(log, (const char *) text, text_len);
245 return 0;
246 }
247
248 struct redis_log_fmt_extra_ctx {
249 pool *pool;
250 cmd_rec *cmd;
251 pr_jot_ctx_t *jot_ctx;
252 pr_json_object_t *json;
253 struct redis_buffer *rb;
254 };
255
256 static const char *log_fmt_extra_resolve_val(pool *p, cmd_rec *cmd,
257 pr_jot_ctx_t *jot_ctx, struct redis_buffer *rb, const char *text) {
258 int res;
259 pr_jot_parsed_t *jot_parsed;
260 unsigned char parsed_buf[1024], *parsed_val;
261 size_t parsed_valsz;
262 char val_buf[1024];
263 const char *val_text = NULL;
264
265 jot_parsed = pcalloc(p, sizeof(pr_jot_parsed_t));
266 jot_parsed->bufsz = jot_parsed->buflen = sizeof(parsed_buf);
267 jot_parsed->ptr = jot_parsed->buf = parsed_buf;
268
269 jot_ctx->log = jot_parsed;
270
271 res = pr_jot_parse_logfmt(p, text, jot_ctx, pr_jot_parse_on_meta,
272 pr_jot_parse_on_unknown, pr_jot_parse_on_other, 0);
273 if (res < 0) {
274 pr_trace_msg(trace_channel, 3,
275 "error parsing RedisLogFormatExtra value '%s': %s", text,
276 strerror(errno));
277 return text;
278 }
279
280 parsed_valsz = jot_parsed->bufsz - jot_parsed->buflen;
281 parsed_val = palloc(p, parsed_valsz + 1);
282 memcpy(parsed_val, parsed_buf, parsed_valsz);
283 parsed_val[parsed_valsz] = '\0';
284
285 rb->bufsz = rb->buflen = sizeof(val_buf)-1;
286 rb->ptr = rb->buf = val_buf;
287 jot_ctx->log = rb;
288
289 res = pr_jot_resolve_logfmt(p, cmd, NULL, parsed_val, jot_ctx,
290 resolve_on_meta, NULL, resolve_on_other);
291 if (res == 0) {
292 size_t val_buflen;
293
294 val_buflen = rb->bufsz - rb->buflen;
295 val_text = pstrndup(p, val_buf, val_buflen);
296
297 } else {
298 pr_trace_msg(trace_channel, 3,
299 "error resolving RedisLogFormatExtra value '%s': %s", text,
300 strerror(errno));
301
302 val_text = text;
303 }
304
305 return val_text;
306 }
307
308 static int log_fmt_extra_iter_cb(const char *key, int val_type, const void *val,
309 size_t valsz, void *user_data) {
310 int res = 0;
311 pool *tmp_pool;
312 cmd_rec *cmd;
313 pr_jot_ctx_t *jot_ctx;
314 pr_json_object_t *json;
315 struct redis_log_fmt_extra_ctx *extra_ctx;
316
317 extra_ctx = user_data;
318 tmp_pool = extra_ctx->pool;
319 cmd = extra_ctx->cmd;
320 jot_ctx = extra_ctx->jot_ctx;
321 json = extra_ctx->json;
322
323 /* Skip any keys that already exist in the destination object. */
324 if (pr_json_object_exists(json, key) == TRUE) {
325 return 0;
326 }
327
328 switch (val_type) {
329 case PR_JSON_TYPE_BOOL:
330 res = pr_json_object_set_bool(tmp_pool, json, key, *((int *) val));
331 break;
332
333 case PR_JSON_TYPE_NUMBER:
334 res = pr_json_object_set_number(tmp_pool, json, key, *((double *) val));
335 break;
336
337 case PR_JSON_TYPE_NULL:
338 res = pr_json_object_set_null(tmp_pool, json, key);
339 break;
340
341 case PR_JSON_TYPE_ARRAY:
342 res = pr_json_object_set_array(tmp_pool, json, key, val);
343 break;
344
345 case PR_JSON_TYPE_OBJECT:
346 res = pr_json_object_set_object(tmp_pool, json, key, val);
347 break;
348
349 case PR_JSON_TYPE_STRING: {
350 struct redis_buffer *rb;
351 const char *val_text;
352
353 rb = extra_ctx->rb;
354
355 val_text = log_fmt_extra_resolve_val(tmp_pool, cmd, jot_ctx, rb, val);
356 res = pr_json_object_set_string(tmp_pool, json, key, val_text);
357 }
358 }
359
360 return res;
361 }
362
363 static pr_json_object_t *add_log_fmt_extra(pool *p, pr_json_object_t *json,
364 cmd_rec *cmd, pr_jot_ctx_t *jot_ctx, pr_json_object_t *extra_json) {
365 int res;
366 struct redis_log_fmt_extra_ctx extra_ctx;
367 struct redis_buffer *rb;
368
369 rb = pcalloc(p, sizeof(struct redis_buffer));
370
371 extra_ctx.pool = p;
372 extra_ctx.cmd = cmd;
373 extra_ctx.jot_ctx = jot_ctx;
374 extra_ctx.json = json;
375 extra_ctx.rb = rb;
376
377 res = pr_json_object_foreach(p, extra_json, log_fmt_extra_iter_cb,
378 &extra_ctx);
379 if (res < 0) {
380 pr_trace_msg(trace_channel, 3, "error adding extra log data: %s",
381 strerror(errno));
382 }
383
384 return json;
385 }
386
387 static pr_json_object_t *add_log_fmt_extras(pool *p, pr_json_object_t *json,
388 const char *fmt_name, cmd_rec *cmd, pr_jot_ctx_t *jot_ctx) {
389 config_rec *c;
390
391 c = find_config(CURRENT_CONF, CONF_PARAM, "RedisLogFormatExtra", FALSE);
392 while (c != NULL) {
393 const char *extra_fmt_name;
394
395 pr_signals_handle();
396
397 extra_fmt_name = c->argv[0];
398 if (strcmp(fmt_name, extra_fmt_name) == 0) {
399 pr_json_object_t *extra_json;
400
401 extra_json = c->argv[1];
402 json = add_log_fmt_extra(p, json, cmd, jot_ctx, extra_json);
403 }
404
405 c = find_config_next(c, c->next, CONF_PARAM, "RedisLogFormatExtra", FALSE);
406 }
407
408 return json;
409 }
410
411 static void log_event(pr_redis_t *redis, config_rec *c, cmd_rec *cmd) {
412 pool *tmp_pool;
413 int res;
414 pr_jot_ctx_t *jot_ctx;
415 pr_jot_filters_t *jot_filters;
416 pr_json_object_t *json;
417 const char *fmt_name = NULL;
418 char *payload = NULL;
419 size_t payload_len = 0;
420 unsigned char *log_fmt, *key_fmt;
421
422 jot_filters = c->argv[0];
423 fmt_name = c->argv[1];
424 log_fmt = c->argv[2];
425 key_fmt = c->argv[3];
426
427 if (jot_filters == NULL ||
428 fmt_name == NULL ||
429 log_fmt == NULL) {
430 return;
431 }
432
433 tmp_pool = make_sub_pool(cmd->tmp_pool);
434 jot_ctx = pcalloc(tmp_pool, sizeof(pr_jot_ctx_t));
435 json = pr_json_object_alloc(tmp_pool);
436 jot_ctx->log = json;
437 jot_ctx->user_data = jot_logfmt2json;
438
439 res = pr_jot_resolve_logfmt(tmp_pool, cmd, jot_filters, log_fmt, jot_ctx,
440 pr_jot_on_json, NULL, NULL);
441 if (res == 0) {
442 json = add_log_fmt_extras(tmp_pool, json, fmt_name, cmd, jot_ctx);
443
444 payload = pr_json_object_to_text(tmp_pool, json, "");
445 payload_len = strlen(payload);
446 pr_trace_msg(trace_channel, 8, "generated JSON payload for %s: %.*s",
447 (char *) cmd->argv[0], (int) payload_len, payload);
448
449 } else {
450 /* EPERM indicates that the message was filtered. */
451 if (errno != EPERM) {
452 (void) pr_log_writefile(redis_logfd, MOD_REDIS_VERSION,
453 "error generating JSON formatted log message: %s", strerror(errno));
454 }
455
456 payload = NULL;
457 payload_len = 0;
458 }
459
460 pr_json_object_free(json);
461
462 if (payload_len > 0) {
463 const char *key;
464
465 if (key_fmt != NULL) {
466 struct redis_buffer *rb;
467 char key_buf[1024];
468
469 rb = pcalloc(tmp_pool, sizeof(struct redis_buffer));
470 rb->bufsz = rb->buflen = sizeof(key_buf)-1;
471 rb->ptr = rb->buf = key_buf;
472
473 jot_ctx->log = rb;
474
475 res = pr_jot_resolve_logfmt(tmp_pool, cmd, NULL, key_fmt, jot_ctx,
476 resolve_on_meta, NULL, resolve_on_other);
477 if (res == 0) {
478 size_t key_buflen;
479
480 key_buflen = rb->bufsz - rb->buflen;
481 key = pstrndup(tmp_pool, key_buf, key_buflen);
482
483 } else {
484 (void) pr_log_writefile(redis_logfd, MOD_REDIS_VERSION,
485 "error resolving Redis key format: %s", strerror(errno));
486 key = fmt_name;
487 }
488
489 } else {
490 key = fmt_name;
491 }
492
493 res = pr_redis_list_append(redis, &redis_module, key, payload,
494 payload_len);
495 if (res < 0) {
496 (void) pr_log_writefile(redis_logfd, MOD_REDIS_VERSION,
497 "error appending log message to '%s': %s", key, strerror(errno));
498
499 } else {
500 pr_trace_msg(trace_channel, 17, "appended log message to '%s'", key);
501 }
502 }
503
504 destroy_pool(tmp_pool);
505 }
506
507 static void log_events(cmd_rec *cmd) {
508 pr_redis_t *redis;
509 config_rec *c;
510
511 redis = pr_redis_conn_get(session.pool, redis_opts);
512 if (redis == NULL) {
513 (void) pr_log_writefile(redis_logfd, MOD_REDIS_VERSION,
514 "error connecting to Redis: %s", strerror(errno));
515 return;
516 }
517
518 c = find_config(CURRENT_CONF, CONF_PARAM, "RedisLogOnCommand", FALSE);
519 while (c != NULL) {
520 pr_signals_handle();
521
522 log_event(redis, c, cmd);
523 c = find_config_next(c, c->next, CONF_PARAM, "RedisLogOnCommand", FALSE);
524 }
525
526 c = find_config(CURRENT_CONF, CONF_PARAM, "RedisLogOnEvent", FALSE);
527 while (c != NULL) {
528 pr_signals_handle();
529
530 log_event(redis, c, cmd);
531 c = find_config_next(c, c->next, CONF_PARAM, "RedisLogOnEvent", FALSE);
532 }
533 }
534
535 static unsigned char *find_log_fmt(server_rec *s, const char *fmt_name) {
536 config_rec *c;
537 unsigned char *log_fmt = NULL;
538
539 c = find_config(s->conf, CONF_PARAM, "LogFormat", FALSE);
540 while (c != NULL) {
541 pr_signals_handle();
542
543 if (strcmp(fmt_name, c->argv[0]) == 0) {
544 log_fmt = c->argv[1];
545 break;
546 }
547
548 c = find_config_next(c, c->next, CONF_PARAM, "LogFormat", FALSE);
549 }
550
551 return log_fmt;
552 }
553
554 /* Configuration handlers
555 */
556
557 /* usage: RedisEngine on|off */
558 MODRET set_redisengine(cmd_rec *cmd) {
559 int engine = -1;
560 config_rec *c = NULL;
561
562 CHECK_ARGS(cmd, 1);
563 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
564
565 engine = get_boolean(cmd, 1);
566 if (engine == -1) {
567 CONF_ERROR(cmd, "expected Boolean parameter");
568 }
569
570 c = add_config_param(cmd->argv[0], 1, NULL);
571 c->argv[0] = pcalloc(c->pool, sizeof(int));
572 *((int *) c->argv[0]) = engine;
573
574 return PR_HANDLED(cmd);
575 }
576
577 /* usage: RedisLog path|"none" */
578 MODRET set_redislog(cmd_rec *cmd) {
579 CHECK_ARGS(cmd, 1);
580 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
581
582 if (strcasecmp(cmd->argv[1], "none") != 0 &&
583 pr_fs_valid_path(cmd->argv[1]) < 0) {
584 CONF_ERROR(cmd, "must be an absolute path");
585 }
586
587 add_config_param_str(cmd->argv[0], 1, cmd->argv[1]);
588 return PR_HANDLED(cmd);
589 }
590
591 /* usage: RedisLogFormatExtra log-fmt json-object */
592 MODRET set_redislogfmtextra(cmd_rec *cmd) {
593 config_rec *c;
594 char *fmt_name, *text;
595 unsigned char *log_fmt;
596 pr_json_object_t *json = NULL;
597
598 CHECK_ARGS(cmd, 2);
599 CHECK_CONF(cmd, CONF_ROOT|CONF_GLOBAL|CONF_VIRTUAL|CONF_ANON|CONF_DIR);
600
601 c = add_config_param(cmd->argv[0], 2, NULL, NULL);
602
603 fmt_name = cmd->argv[1];
604
605 /* Make sure that the given LogFormat name is known. */
606 log_fmt = find_log_fmt(cmd->server, fmt_name);
607 if (log_fmt == NULL) {
608 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "no LogFormat '", fmt_name,
609 "' configured", NULL));
610 }
611
612 text = pstrdup(c->pool, cmd->argv[2]);
613 json = pr_json_object_from_text(c->pool, text);
614 if (json == NULL) {
615 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "error parsing '", cmd->argv[2],
616 "' as JSON object: invalid JSON", NULL));
617 }
618
619 c->argv[0] = pstrdup(c->pool, fmt_name);
620 c->argv[1] = json;
621
622 c->flags |= CF_MERGEDOWN_MULTI;
623 return PR_HANDLED(cmd);
624 }
625
626 /* usage: RedisLogOnCommand "none"|commands log-fmt [key] */
627 MODRET set_redislogoncommand(cmd_rec *cmd) {
628 config_rec *c;
629 const char *fmt_name, *rules;
630 unsigned char *log_fmt = NULL, *key_fmt = NULL;
631 pr_jot_filters_t *jot_filters;
632
633 CHECK_CONF(cmd, CONF_ROOT|CONF_GLOBAL|CONF_VIRTUAL|CONF_ANON|CONF_DIR);
634
635 if (cmd->argc < 3 ||
636 cmd->argc > 4) {
637
638 if (cmd->argc == 2 &&
639 strcasecmp(cmd->argv[1], "none") == 0) {
640 c = add_config_param(cmd->argv[0], 4, NULL, NULL, NULL, NULL);
641 c->flags |= CF_MERGEDOWN;
642 return PR_HANDLED(cmd);
643 }
644
645 CONF_ERROR(cmd, "wrong number of parameters");
646 }
647
648 c = add_config_param(cmd->argv[0], 4, NULL, NULL, NULL, NULL);
649
650 rules = cmd->argv[1];
651 jot_filters = pr_jot_filters_create(c->pool, rules,
652 PR_JOT_FILTER_TYPE_COMMANDS, 0);
653 if (jot_filters == NULL) {
654 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "unable to use commands '", rules,
655 "': ", strerror(errno), NULL));
656 }
657
658 fmt_name = cmd->argv[2];
659
660 /* Make sure that the given LogFormat name is known. */
661 log_fmt = find_log_fmt(cmd->server, fmt_name);
662 if (log_fmt == NULL) {
663 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "no LogFormat '", fmt_name,
664 "' configured", NULL));
665 }
666
667 if (cmd->argc == 4) {
668 int res;
669 pool *tmp_pool;
670 pr_jot_ctx_t *jot_ctx;
671 pr_jot_parsed_t *jot_parsed;
672 char *key_text;
673 unsigned char key_fmtbuf[1024];
674 size_t key_fmtlen;
675
676 tmp_pool = make_sub_pool(cmd->tmp_pool);
677 jot_ctx = pcalloc(tmp_pool, sizeof(pr_jot_ctx_t));
678 jot_parsed = pcalloc(tmp_pool, sizeof(pr_jot_parsed_t));
679 jot_parsed->bufsz = jot_parsed->buflen = sizeof(key_fmtbuf)-1;
680 jot_parsed->ptr = jot_parsed->buf = key_fmtbuf;
681
682 jot_ctx->log = jot_parsed;
683
684 key_text = cmd->argv[3];
685 res = pr_jot_parse_logfmt(tmp_pool, key_text, jot_ctx,
686 pr_jot_parse_on_meta, pr_jot_parse_on_unknown, pr_jot_parse_on_other, 0);
687 if (res < 0) {
688 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "error parsing Redis key '",
689 key_text, "': ", strerror(errno), NULL));
690 }
691
692 destroy_pool(tmp_pool);
693
694 key_fmtlen = jot_parsed->bufsz - jot_parsed->buflen;
695 key_fmtbuf[key_fmtlen] = '\0';
696 key_fmt = (unsigned char *) pstrndup(c->pool, (char *) key_fmtbuf,
697 key_fmtlen);
698 }
699
700 c->argv[0] = jot_filters;
701 c->argv[1] = pstrdup(c->pool, fmt_name);
702 c->argv[2] = log_fmt;
703 c->argv[3] = key_fmt;
704
705 c->flags |= CF_MERGEDOWN_MULTI;
706 return PR_HANDLED(cmd);
707 }
708
709 /* usage: RedisLogOnEvent "none"|events log-fmt [key] */
710 MODRET set_redislogonevent(cmd_rec *cmd) {
711 config_rec *c;
712 const char *fmt_name, *rules;
713 unsigned char *log_fmt = NULL, *key_fmt = NULL;
714 pr_jot_filters_t *jot_filters;
715
716 CHECK_CONF(cmd, CONF_ROOT|CONF_GLOBAL|CONF_VIRTUAL|CONF_ANON|CONF_DIR);
717
718 if (cmd->argc < 3 ||
719 cmd->argc > 4) {
720
721 if (cmd->argc == 2 &&
722 strcasecmp(cmd->argv[1], "none") == 0) {
723 c = add_config_param(cmd->argv[0], 4, NULL, NULL, NULL, NULL);
724 c->flags |= CF_MERGEDOWN;
725 return PR_HANDLED(cmd);
726 }
727
728 CONF_ERROR(cmd, "wrong number of parameters");
729 }
730
731 c = add_config_param(cmd->argv[0], 4, NULL, NULL, NULL, NULL);
732
733 rules = cmd->argv[1];
734 jot_filters = pr_jot_filters_create(c->pool, rules,
735 PR_JOT_FILTER_TYPE_COMMANDS_WITH_CLASSES,
736 PR_JOT_FILTER_FL_ALL_INCL_ALL);
737 if (jot_filters == NULL) {
738 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "unable to use events '", rules,
739 "': ", strerror(errno), NULL));
740 }
741
742 fmt_name = cmd->argv[2];
743
744 /* Make sure that the given LogFormat name is known. */
745 log_fmt = find_log_fmt(cmd->server, fmt_name);
746 if (log_fmt == NULL) {
747 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "no LogFormat '", fmt_name,
748 "' configured", NULL));
749 }
750
751 if (cmd->argc == 4) {
752 int res;
753 pool *tmp_pool;
754 pr_jot_ctx_t *jot_ctx;
755 pr_jot_parsed_t *jot_parsed;
756 char *key_text;
757 unsigned char key_fmtbuf[1024];
758 size_t key_fmtlen;
759
760 tmp_pool = make_sub_pool(cmd->tmp_pool);
761 jot_ctx = pcalloc(tmp_pool, sizeof(pr_jot_ctx_t));
762 jot_parsed = pcalloc(tmp_pool, sizeof(pr_jot_parsed_t));
763 jot_parsed->bufsz = jot_parsed->buflen = sizeof(key_fmtbuf)-1;
764 jot_parsed->ptr = jot_parsed->buf = key_fmtbuf;
765
766 jot_ctx->log = jot_parsed;
767
768 key_text = cmd->argv[3];
769 res = pr_jot_parse_logfmt(tmp_pool, key_text, jot_ctx,
770 pr_jot_parse_on_meta, pr_jot_parse_on_unknown, pr_jot_parse_on_other, 0);
771 if (res < 0) {
772 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "error parsing Redis key '",
773 key_text, "': ", strerror(errno), NULL));
774 }
775
776 destroy_pool(tmp_pool);
777
778 key_fmtlen = jot_parsed->bufsz - jot_parsed->buflen;
779 key_fmtbuf[key_fmtlen] = '\0';
780 key_fmt = (unsigned char *) pstrndup(c->pool, (char *) key_fmtbuf,
781 key_fmtlen);
782 }
783
784 c->argv[0] = jot_filters;
785 c->argv[1] = pstrdup(c->pool, fmt_name);
786 c->argv[2] = log_fmt;
787 c->argv[3] = key_fmt;
788
789 c->flags |= CF_MERGEDOWN_MULTI;
790 return PR_HANDLED(cmd);
791 }
792
793 /* usage: RedisOptions opt1 opt2 ... */
794 MODRET set_redisoptions(cmd_rec *cmd) {
795 config_rec *c = NULL;
796 register unsigned int i = 0;
797 unsigned long opts = 0UL;
798
799 if (cmd->argc-1 == 0) {
800 CONF_ERROR(cmd, "wrong number of parameters");
801 }
802
803 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
804
805 c = add_config_param(cmd->argv[0], 1, NULL);
806
807 for (i = 1; i < cmd->argc; i++) {
808 if (strcmp(cmd->argv[i], "NoReconnect") == 0) {
809 opts |= PR_REDIS_CONN_FL_NO_RECONNECT;
810
811 } else {
812 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, ": unknown RedisOption '",
813 cmd->argv[i], "'", NULL));
814 }
815 }
816
817 c->argv[0] = pcalloc(c->pool, sizeof(unsigned long));
818 *((unsigned long *) c->argv[0]) = opts;
819
820 if (pr_module_exists("mod_ifsession.c")) {
821 /* These are needed in case this directive is used with mod_ifsession
822 * configuration.
823 */
824 c->flags |= CF_MULTI;
825 }
826
827 return PR_HANDLED(cmd);
828 }
829
830 /* usage: RedisSentinel host[:port] ... [master name] \
831 * [ssl:true] [ssl-ca:/path] [ssl-cert:/path] [ssl-key:/path]
832 */
833 MODRET set_redissentinel(cmd_rec *cmd) {
834 register unsigned int i;
835 config_rec *c;
836 array_header *sentinels;
837 char *master_name = NULL, *ssl_cacert = NULL, *ssl_cert = NULL,
838 *ssl_key = NULL;
839 int use_ssl = FALSE;
840
841 CHECK_ARGS(cmd, 1);
842 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
843
844 if (cmd->argc >= 4) {
845 /* Has the name of a master been explicitly provided? */
846 if (strcasecmp(cmd->argv[cmd->argc-2], "master") == 0) {
847 master_name = cmd->argv[cmd->argc-1];
848
849 cmd->argc -= 2;
850 }
851 }
852
853 c = add_config_param(cmd->argv[0], 6, NULL, NULL, NULL, NULL, NULL, NULL);
854 sentinels = make_array(c->pool, 0, sizeof(pr_netaddr_t *));
855
856 for (i = 1; i < cmd->argc; i++) {
857 char *sentinel, *ptr;
858 size_t sentinel_len;
859 int port = REDIS_SENTINEL_DEFAULT_PORT;
860 pr_netaddr_t *sentinel_addr;
861
862 /* Handle the optional SSL/TLS settings, too. */
863 if (strncmp(cmd->argv[i], "ssl:", 4) == 0) {
864 const char *text;
865
866 text = cmd->argv[i];
867
868 /* Advance past the "ssl:" prefix. */
869 text += 4;
870
871 use_ssl = pr_str_is_boolean(text);
872 if (use_ssl < 0) {
873 pr_log_pri(PR_LOG_NOTICE, MOD_REDIS_VERSION
874 ": %s: use SSL '%s': %s", (char *) cmd->argv[0], text,
875 strerror(EINVAL));
876 }
877
878 continue;
879 }
880
881 if (strncmp(cmd->argv[i], "ssl-ca:", 7) == 0) {
882 char *path;
883
884 path = cmd->argv[i];
885
886 /* Advance past the "ssl-ca:" prefix. */
887 path += 7;
888
889 /* Check the file exists! */
890 if (file_exists2(cmd->tmp_pool, path) == TRUE) {
891 ssl_cacert = path;
892
893 } else {
894 pr_log_pri(PR_LOG_NOTICE, MOD_REDIS_VERSION
895 ": %s: SSL CA '%s': %s", (char *) cmd->argv[0], path,
896 strerror(ENOENT));
897 }
898
899 continue;
900 }
901
902 if (strncmp(cmd->argv[i], "ssl-cert:", 9) == 0) {
903 char *path;
904
905 path = cmd->argv[i];
906
907 /* Advance past the "ssl-cert:" prefix. */
908 path += 9;
909
910 /* Check the file exists! */
911 if (file_exists2(cmd->tmp_pool, path) == TRUE) {
912 ssl_cert = path;
913
914 } else {
915 pr_log_pri(PR_LOG_NOTICE, MOD_REDIS_VERSION
916 ": %s: SSL certificate '%s': %s", (char *) cmd->argv[0], path,
917 strerror(ENOENT));
918 }
919
920 continue;
921 }
922
923 if (strncmp(cmd->argv[i], "ssl-key:", 8) == 0) {
924 char *path;
925
926 path = cmd->argv[i];
927
928 /* Advance past the "ssl-key:" prefix. */
929 path += 8;
930
931 /* Check the file exists! */
932 if (file_exists2(cmd->tmp_pool, path) == TRUE) {
933 ssl_key = path;
934
935 } else {
936 pr_log_pri(PR_LOG_NOTICE, MOD_REDIS_VERSION
937 ": %s: SSL certificate key '%s': %s", (char *) cmd->argv[0], path,
938 strerror(ENOENT));
939 }
940
941 continue;
942 }
943
944 sentinel = pstrdup(cmd->tmp_pool, cmd->argv[i]);
945 sentinel_len = strlen(sentinel);
946
947 ptr = strrchr(sentinel, ':');
948 if (ptr != NULL) {
949 /* We also need to check for IPv6 addresses, e.g. "[::1]" or
950 * "[::1]:26379", before assuming that the text following our discovered
951 * ':' is indeed a port number.
952 */
953 if (*sentinel == '[') {
954 if (*(ptr-1) == ']') {
955 /* We have an IPv6 address with an explicit port number. */
956 sentinel = pstrndup(cmd->tmp_pool, sentinel + 1,
957 (ptr - 1) - (sentinel + 1));
958 *ptr = '\0';
959 port = atoi(ptr + 1);
960
961 } else if (sentinel[sentinel_len-1] == ']') {
962 /* We have an IPv6 address without an explicit port number. */
963 sentinel = pstrndup(cmd->tmp_pool, sentinel + 1, sentinel_len - 2);
964 port = REDIS_SENTINEL_DEFAULT_PORT;
965 }
966
967 } else {
968 *ptr = '\0';
969 port = atoi(ptr + 1);
970 }
971 }
972
973 sentinel_addr = (pr_netaddr_t *) pr_netaddr_get_addr(c->pool, sentinel,
974 NULL);
975 if (sentinel_addr != NULL) {
976 pr_netaddr_set_port2(sentinel_addr, port);
977 *((pr_netaddr_t **) push_array(sentinels)) = sentinel_addr;
978
979 } else {
980 pr_log_debug(DEBUG0, "%s: unable to resolve '%s' (%s), ignoring",
981 (char *) cmd->argv[0], sentinel, strerror(errno));
982 }
983 }
984
985 c->argv[0] = sentinels;
986 c->argv[1] = pstrdup(c->pool, master_name);
987 c->argv[2] = pcalloc(c->pool, sizeof(int));
988 *((int *) c->argv[2]) = use_ssl;
989 c->argv[3] = pstrdup(c->pool, ssl_cacert);
990 c->argv[4] = pstrdup(c->pool, ssl_cert);
991 c->argv[5] = pstrdup(c->pool, ssl_key);
992
993 return PR_HANDLED(cmd);
994 }
995
996 /* usage: RedisServer host[:port] [username] [password] [db-index] \
997 * [ssl:true] [ssl-ca:/path] [ssl-cert:/path] [ssl-key:/path]
998 */
999 MODRET set_redisserver(cmd_rec *cmd) {
1000 register unsigned int i;
1001 config_rec *c;
1002 char *server, *username = NULL, *password = NULL, *db_idx = NULL, *ptr;
1003 const char *ssl_cacert = NULL, *ssl_cert = NULL, *ssl_key = NULL;
1004 size_t server_len;
1005 int ctx, opts_idx = 0, port = REDIS_SERVER_DEFAULT_PORT, use_ssl = FALSE;
1006
1007 CHECK_ARGS(cmd, 1);
1008 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
1009
1010 server = pstrdup(cmd->tmp_pool, cmd->argv[1]);
1011 server_len = strlen(server);
1012
1013 ptr = strrchr(server, ':');
1014 if (ptr != NULL) {
1015 /* We also need to check for IPv6 addresses, e.g. "[::1]" or "[::1]:6379",
1016 * before assuming that the text following our discovered ':' is indeed
1017 * a port number.
1018 */
1019
1020 if (*server == '[') {
1021 if (*(ptr-1) == ']') {
1022 /* We have an IPv6 address with an explicit port number. */
1023 server = pstrndup(cmd->tmp_pool, server + 1, (ptr - 1) - (server + 1));
1024 *ptr = '\0';
1025 port = atoi(ptr + 1);
1026
1027 } else if (server[server_len-1] == ']') {
1028 /* We have an IPv6 address without an explicit port number. */
1029 server = pstrndup(cmd->tmp_pool, server + 1, server_len - 2);
1030 port = REDIS_SERVER_DEFAULT_PORT;
1031 }
1032
1033 } else {
1034 *ptr = '\0';
1035 port = atoi(ptr + 1);
1036 }
1037 }
1038
1039 /* This index tells us where to start looking for optional settings, like
1040 * the SSL/TLS settings.
1041 */
1042 opts_idx = 2;
1043
1044 if (cmd->argc >= 3) {
1045 char *text;
1046
1047 text = cmd->argv[2];
1048
1049 if (strchr(text, ':') == NULL &&
1050 strncmp(text, "ssl", 3) != 0) {
1051 username = text;
1052
1053 if (strcmp(username, "") == 0) {
1054 username = NULL;
1055 }
1056
1057 opts_idx = 3;
1058 }
1059 }
1060
1061 if (cmd->argc >= 4) {
1062 char *text;
1063
1064 text = cmd->argv[3];
1065
1066 if (strchr(text, ':') == NULL &&
1067 strncmp(text, "ssl", 3) != 0) {
1068 password = text;
1069
1070 if (strcmp(password, "") == 0) {
1071 password = NULL;
1072 }
1073
1074 opts_idx = 4;
1075 }
1076 }
1077
1078 if (cmd->argc >= 5) {
1079 char *text;
1080
1081 text = cmd->argv[4];
1082
1083 if (strchr(text, ':') == NULL &&
1084 strncmp(text, "ssl", 3) != 0) {
1085 db_idx = text;
1086
1087 if (strcmp(db_idx, "") == 0) {
1088 db_idx = NULL;
1089 }
1090
1091 opts_idx = 5;
1092 }
1093 }
1094
1095 /* Handle the optional SSL/TLS settings, too. */
1096 for (i = opts_idx; i < cmd->argc; i++) {
1097 if (strncmp(cmd->argv[i], "ssl:", 4) == 0) {
1098 const char *text;
1099
1100 text = cmd->argv[i];
1101
1102 /* Advance past the "ssl:" prefix. */
1103 text += 4;
1104
1105 use_ssl = pr_str_is_boolean(text);
1106 if (use_ssl < 0) {
1107 pr_log_pri(PR_LOG_NOTICE, MOD_REDIS_VERSION
1108 ": %s: use SSL '%s': %s", (char *) cmd->argv[0], text,
1109 strerror(EINVAL));
1110 }
1111
1112 continue;
1113 }
1114
1115 if (strncmp(cmd->argv[i], "ssl-ca:", 7) == 0) {
1116 const char *path;
1117
1118 path = cmd->argv[i];
1119
1120 /* Advance past the "ssl-ca:" prefix. */
1121 path += 7;
1122
1123 /* Check the file exists! */
1124 if (file_exists2(cmd->tmp_pool, path) == TRUE) {
1125 ssl_cacert = path;
1126
1127 } else {
1128 pr_log_pri(PR_LOG_NOTICE, MOD_REDIS_VERSION
1129 ": %s: SSL CA '%s': %s", (char *) cmd->argv[0], path,
1130 strerror(ENOENT));
1131 }
1132
1133 continue;
1134 }
1135
1136 if (strncmp(cmd->argv[i], "ssl-cert:", 9) == 0) {
1137 char *path;
1138
1139 path = cmd->argv[i];
1140
1141 /* Advance past the "ssl-cert:" prefix. */
1142 path += 9;
1143
1144 /* Check the file exists! */
1145 if (file_exists2(cmd->tmp_pool, path) == TRUE) {
1146 ssl_cert = path;
1147
1148 } else {
1149 pr_log_pri(PR_LOG_NOTICE, MOD_REDIS_VERSION
1150 ": %s: SSL certificate '%s': %s", (char *) cmd->argv[0], path,
1151 strerror(ENOENT));
1152 }
1153
1154 continue;
1155 }
1156
1157 if (strncmp(cmd->argv[i], "ssl-key:", 8) == 0) {
1158 char *path;
1159
1160 path = cmd->argv[i];
1161
1162 /* Advance past the "ssl-key:" prefix. */
1163 path += 8;
1164
1165 /* Check the file exists! */
1166 if (file_exists2(cmd->tmp_pool, path) == TRUE) {
1167 ssl_key = path;
1168
1169 } else {
1170 pr_log_pri(PR_LOG_NOTICE, MOD_REDIS_VERSION
1171 ": %s: SSL certificate key '%s': %s", (char *) cmd->argv[0], path,
1172 strerror(ENOENT));
1173 }
1174
1175 continue;
1176 }
1177 }
1178
1179 c = add_config_param(cmd->argv[0], 9, NULL, NULL, NULL, NULL, NULL, NULL,
1180 NULL, NULL, NULL);
1181 c->argv[0] = pstrdup(c->pool, server);
1182 c->argv[1] = palloc(c->pool, sizeof(int));
1183 *((int *) c->argv[1]) = port;
1184 c->argv[2] = pstrdup(c->pool, username);
1185 c->argv[3] = pstrdup(c->pool, password);
1186 c->argv[4] = pstrdup(c->pool, db_idx);
1187 c->argv[5] = palloc(c->pool, sizeof(int));
1188 *((int *) c->argv[5]) = use_ssl;
1189 c->argv[6] = pstrdup(c->pool, ssl_cacert);
1190 c->argv[7] = pstrdup(c->pool, ssl_cert);
1191 c->argv[8] = pstrdup(c->pool, ssl_key);
1192
1193 ctx = (cmd->config && cmd->config->config_type != CONF_PARAM ?
1194 cmd->config->config_type : cmd->server->config_type ?
1195 cmd->server->config_type : CONF_ROOT);
1196
1197 if (ctx == CONF_ROOT) {
1198 /* If we're the "server config" context, set the server now. This
1199 * would let mod_redis talk to those servers for e.g. ftpdctl actions.
1200 */
1201 (void) redis_set_server3(c->argv[0], port, 0UL, c->argv[2], c->argv[3],
1202 c->argv[4], use_ssl, ssl_cacert, ssl_cert, ssl_key);
1203 }
1204
1205 return PR_HANDLED(cmd);
1206 }
1207
1208 /* usage: RedisTimeouts conn-timeout io-timeout */
1209 MODRET set_redistimeouts(cmd_rec *cmd) {
1210 config_rec *c;
1211 unsigned long connect_millis, io_millis;
1212 char *ptr = NULL;
1213
1214 CHECK_ARGS(cmd, 2);
1215 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
1216
1217 connect_millis = strtoul(cmd->argv[1], &ptr, 10);
1218 if (ptr && *ptr) {
1219 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
1220 "badly formatted connect timeout value: ", cmd->argv[1], NULL));
1221 }
1222
1223 ptr = NULL;
1224 io_millis = strtoul(cmd->argv[2], &ptr, 10);
1225 if (ptr && *ptr) {
1226 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
1227 "badly formatted IO timeout value: ", cmd->argv[2], NULL));
1228 }
1229
1230 #if 0
1231 /* XXX If we're the "server config" context, set the timeouts now.
1232 * This would let mod_redis talk to those servers for e.g. ftpdctl
1233 * actions.
1234 */
1235 redis_set_timeouts(conn_timeout, io_timeout);
1236 #endif
1237
1238 c = add_config_param(cmd->argv[0], 2, NULL, NULL);
1239 c->argv[0] = palloc(c->pool, sizeof(unsigned long));
1240 *((unsigned long *) c->argv[0]) = connect_millis;
1241 c->argv[1] = palloc(c->pool, sizeof(unsigned long));
1242 *((unsigned long *) c->argv[1]) = io_millis;
1243
1244 return PR_HANDLED(cmd);
1245 }
1246
1247 /* Command handlers
1248 */
1249
1250 MODRET redis_log_any(cmd_rec *cmd) {
1251 if (redis_engine == FALSE) {
1252 return PR_DECLINED(cmd);
1253 }
1254
1255 log_events(cmd);
1256 return PR_DECLINED(cmd);
1257 }
1258
1259 static int is_redis_log_config(config_rec *c, const char *name, size_t namelen,
1260 unsigned int config_id) {
1261 int matched = FALSE;
1262
1263 if (c->config_type == CONF_PARAM &&
1264 c->argc == 4 &&
1265 c->config_id == config_id &&
1266 strncmp(name, c->name, namelen + 1) == 0) {
1267 matched = TRUE;
1268 }
1269
1270 return matched;
1271 }
1272
1273 static int prune_redis_log_config(pool *p, xaset_t *set, const char *name,
1274 size_t namelen, unsigned int config_id) {
1275 config_rec *c;
1276 int needs_pruning = FALSE, pruned = FALSE;
1277
1278 /* Bottom up pruning; handle subsets first. */
1279 for (c = (config_rec *) set->xas_list; c; c = c->next) {
1280 pr_signals_handle();
1281
1282 if (c->subset == NULL) {
1283 continue;
1284 }
1285
1286 if (prune_redis_log_config(p, c->subset, name, namelen,
1287 config_id) == TRUE) {
1288 pruned = TRUE;
1289 }
1290 }
1291
1292 /* Look for the named directive, specifically looking for any "none"
1293 * configurations.
1294 */
1295 for (c = (config_rec *) set->xas_list; c; c = c->next) {
1296 pr_signals_handle();
1297
1298 if (is_redis_log_config(c, name, namelen, config_id) == TRUE) {
1299 if (c->argv[0] == NULL ||
1300 c->argv[1] == NULL ||
1301 c->argv[2] == NULL) {
1302 needs_pruning = TRUE;
1303 break;
1304 }
1305 }
1306 }
1307
1308 if (needs_pruning) {
1309 pr_config_remove(set, name, 0, FALSE);
1310 pruned = TRUE;
1311 }
1312
1313 return pruned;
1314 }
1315
1316 MODRET redis_post_pass(cmd_rec *cmd) {
1317 const char *name;
1318 int pruned = FALSE;
1319
1320 if (redis_engine == FALSE) {
1321 return PR_DECLINED(cmd);
1322 }
1323
1324 if (main_server->conf == NULL ||
1325 main_server->conf->xas_list == NULL) {
1326 return PR_DECLINED(cmd);
1327 }
1328
1329 /* Process the config tree for RedisLogOnCommand/Event "none" directives,
1330 * so that they work as expected.
1331 *
1332 * Since RedisLogOnCommand/Event allow for multiple simultaneous occurrences
1333 * (via CF_MERGEDOWN_MULTI), due to multiple possible filters/formats, we
1334 * cannot simply assume that the CF_MERGEDOWN flag for the "none" case will
1335 * do as we want. Instead, we have manually prune any competing
1336 * RedisLogOnCommand/Event directives, for a given directory level, whenever
1337 * we encounter a "none" directive.
1338 */
1339
1340 name = "RedisLogOnCommand";
1341 if (find_config(main_server->conf, CONF_PARAM, name, TRUE) != NULL) {
1342 size_t namelen;
1343 unsigned int config_id;
1344
1345 namelen = strlen(name);
1346 config_id = pr_config_get_id(name);
1347 if (prune_redis_log_config(cmd->tmp_pool, main_server->conf, name, namelen,
1348 config_id) == TRUE) {
1349 pruned = TRUE;
1350 }
1351 }
1352
1353 name = "RedisLogOnEvent";
1354 if (find_config(main_server->conf, CONF_PARAM, name, TRUE) != NULL) {
1355 size_t namelen;
1356 unsigned int config_id;
1357
1358 namelen = strlen(name);
1359 config_id = pr_config_get_id(name);
1360 if (prune_redis_log_config(cmd->tmp_pool, main_server->conf, name, namelen,
1361 config_id) == TRUE) {
1362 pruned = TRUE;
1363 }
1364 }
1365
1366 if (pruned) {
1367 pr_log_debug(DEBUG9, MOD_REDIS_VERSION
1368 ": Pruned configuration for Redis logging");
1369 pr_config_dump(NULL, main_server->conf, NULL);
1370 }
1371
1372 return PR_DECLINED(cmd);
1373 }
1374
1375 /* Event handlers
1376 */
1377
1378 static void redis_restart_ev(const void *event_data, void *user_data) {
1379 destroy_pool(redis_pool);
1380 redis_pool = make_sub_pool(permanent_pool);
1381 pr_pool_tag(redis_pool, MOD_REDIS_VERSION);
1382
1383 jot_logfmt2json = pr_jot_get_logfmt2json(redis_pool);
1384 }
1385
1386 static void redis_sess_reinit_ev(const void *event_data, void *user_data) {
1387 int res;
1388
1389 /* A HOST command changed the main_server pointer, reinitialize ourselves. */
1390
1391 pr_event_unregister(&redis_module, "core.session-reinit",
1392 redis_sess_reinit_ev);
1393
1394 (void) close(redis_logfd);
1395 redis_logfd = -1;
1396
1397 /* XXX Restore other Redis settings? */
1398 /* reset RedisTimeouts */
1399
1400 res = redis_sess_init();
1401 if (res < 0) {
1402 pr_session_disconnect(&redis_module,
1403 PR_SESS_DISCONNECT_SESSION_INIT_FAILED, NULL);
1404 }
1405 }
1406
1407 static void redis_shutdown_ev(const void *event_data, void *user_data) {
1408 destroy_pool(redis_pool);
1409 jot_logfmt2json = NULL;
1410 }
1411
1412 /* Initialization functions
1413 */
1414
1415 static int redis_module_init(void) {
1416 redis_pool = make_sub_pool(permanent_pool);
1417 pr_pool_tag(redis_pool, MOD_REDIS_VERSION);
1418
1419 redis_init();
1420 pr_event_register(&redis_module, "core.restart", redis_restart_ev, NULL);
1421 pr_event_register(&redis_module, "core.shutdown", redis_shutdown_ev, NULL);
1422
1423 pr_log_debug(DEBUG2, MOD_REDIS_VERSION ": using hiredis-%d.%d.%d",
1424 HIREDIS_MAJOR, HIREDIS_MINOR, HIREDIS_PATCH);
1425
1426 jot_logfmt2json = pr_jot_get_logfmt2json(redis_pool);
1427 if (jot_logfmt2json == NULL) {
1428 return -1;
1429 }
1430
1431 return 0;
1432 }
1433
1434 static int redis_sess_init(void) {
1435 config_rec *c;
1436
1437 pr_event_register(&redis_module, "core.session-reinit",
1438 redis_sess_reinit_ev, NULL);
1439
1440 c = find_config(main_server->conf, CONF_PARAM, "RedisEngine", FALSE);
1441 if (c != NULL) {
1442 int engine;
1443
1444 engine = *((int *) c->argv[0]);
1445 if (engine == FALSE) {
1446 return 0;
1447 }
1448
1449 redis_engine = engine;
1450 }
1451
1452 c = find_config(main_server->conf, CONF_PARAM, "RedisLog", FALSE);
1453 if (c != NULL) {
1454 const char *path;
1455
1456 path = c->argv[0];
1457 if (strcasecmp(path, "none") != 0) {
1458 int res, xerrno;
1459
1460 pr_signals_block();
1461 PRIVS_ROOT
1462 res = pr_log_openfile(path, &redis_logfd, PR_LOG_SYSTEM_MODE);
1463 xerrno = errno;
1464 PRIVS_RELINQUISH
1465 pr_signals_unblock();
1466
1467 switch (res) {
1468 case 0:
1469 break;
1470
1471 case -1:
1472 pr_log_pri(PR_LOG_NOTICE, MOD_REDIS_VERSION
1473 ": notice: unable to open RedisLog '%s': %s", path,
1474 strerror(xerrno));
1475 break;
1476
1477 case PR_LOG_WRITABLE_DIR:
1478 pr_log_pri(PR_LOG_WARNING, MOD_REDIS_VERSION
1479 ": notice: unable to use RedisLog '%s': parent directory is "
1480 "world-writable", path);
1481 break;
1482
1483 case PR_LOG_SYMLINK:
1484 pr_log_pri(PR_LOG_WARNING, MOD_REDIS_VERSION
1485 ": notice: unable to use RedisLog '%s': cannot log to a symlink",
1486 path);
1487 break;
1488 }
1489 }
1490 }
1491
1492 c = find_config(main_server->conf, CONF_PARAM, "RedisOptions", FALSE);
1493 while (c != NULL) {
1494 unsigned long opts = 0;
1495
1496 pr_signals_handle();
1497
1498 opts = *((unsigned long *) c->argv[0]);
1499 redis_opts |= opts;
1500
1501 c = find_config_next(c, c->next, CONF_PARAM, "RedisOptions", FALSE);
1502 }
1503
1504 c = find_config(main_server->conf, CONF_PARAM, "RedisSentinel", FALSE);
1505 if (c != NULL) {
1506 array_header *sentinels;
1507 const char *master, *ssl_cacert, *ssl_cert, *ssl_key;
1508 int use_ssl;
1509
1510 sentinels = c->argv[0];
1511 master = c->argv[1];
1512 use_ssl = *((int *) c->argv[2]);
1513 ssl_cacert = c->argv[3];
1514 ssl_cert = c->argv[4];
1515 ssl_key = c->argv[5];
1516
1517 (void) redis_set_sentinels2(sentinels, master, use_ssl, ssl_cacert,
1518 ssl_cert, ssl_key);
1519 }
1520
1521 c = find_config(main_server->conf, CONF_PARAM, "RedisServer", FALSE);
1522 if (c != NULL) {
1523 const char *server, *username, *password, *db_idx, *ssl_cacert,
1524 *ssl_cert, *ssl_key;
1525 int port, use_ssl;
1526
1527 server = c->argv[0];
1528 port = *((int *) c->argv[1]);
1529 username = c->argv[2];
1530 password = c->argv[3];
1531 db_idx = c->argv[4];
1532 use_ssl = *((int *) c->argv[5]);
1533 ssl_cacert = c->argv[6];
1534 ssl_cert = c->argv[7];
1535 ssl_key = c->argv[8];
1536
1537 (void) redis_set_server3(server, port, redis_opts, username, password,
1538 db_idx, use_ssl, ssl_cacert, ssl_cert, ssl_key);
1539 }
1540
1541 c = find_config(main_server->conf, CONF_PARAM, "RedisTimeouts", FALSE);
1542 if (c != NULL) {
1543 unsigned long connect_millis, io_millis;
1544
1545 connect_millis = *((unsigned long *) c->argv[0]);
1546 io_millis = *((unsigned long *) c->argv[1]);
1547
1548 if (redis_set_timeouts(connect_millis, io_millis) < 0) {
1549 (void) pr_log_writefile(redis_logfd, MOD_REDIS_VERSION,
1550 "error setting Redis timeouts: %s", strerror(errno));
1551 }
1552 }
1553
1554 return 0;
1555 }
1556
1557 /* Module API tables
1558 */
1559
1560 static conftable redis_conftab[] = {
1561 { "RedisEngine", set_redisengine, NULL },
1562 { "RedisLog", set_redislog, NULL },
1563 { "RedisLogFormatExtra", set_redislogfmtextra, NULL },
1564 { "RedisLogOnCommand", set_redislogoncommand, NULL },
1565 { "RedisLogOnEvent", set_redislogonevent, NULL },
1566 { "RedisOptions", set_redisoptions, NULL },
1567 { "RedisSentinel", set_redissentinel, NULL },
1568 { "RedisServer", set_redisserver, NULL },
1569 { "RedisTimeouts", set_redistimeouts, NULL },
1570
1571 { NULL }
1572 };
1573
1574 static cmdtable redis_cmdtab[] = {
1575 { POST_CMD, C_PASS, G_NONE, redis_post_pass, FALSE, FALSE },
1576 { LOG_CMD, C_ANY, G_NONE, redis_log_any, FALSE, FALSE },
1577 { LOG_CMD_ERR, C_ANY, G_NONE, redis_log_any, FALSE, FALSE },
1578
1579 { 0, NULL }
1580 };
1581
1582 module redis_module = {
1583 NULL, NULL,
1584
1585 /* Module API version 2.0 */
1586 0x20,
1587
1588 /* Module name */
1589 "redis",
1590
1591 /* Module configuration handler table */
1592 redis_conftab,
1593
1594 /* Module command handler table */
1595 redis_cmdtab,
1596
1597 /* Module authentication handler table */
1598 NULL,
1599
1600 /* Module initialization function */
1601 redis_module_init,
1602
1603 /* Session initialization function */
1604 redis_sess_init,
1605
1606 /* Module version */
1607 MOD_REDIS_VERSION
1608 };