Debian Codegraph @

contrib/mod_log_forensic.c

raw symbols SCIP
1 /*
2 * mod_log_forensic - a buffering log module for aiding in server behavior
3 * forensic analysis
4 * Copyright (c) 2011-2024 TJ Saunders
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 Street, Suite 500, Boston, MA 02110-1335, USA.
19 *
20 * As a special exemption, TJ Saunders and other respective copyright holders
21 * give permission to link this program with OpenSSL, and distribute the
22 * resulting executable, without including the source code for OpenSSL in the
23 * source distribution.
24 */
25
26 #include "conf.h"
27 #include "privs.h"
28
29 #include <sys/uio.h>
30
31 #define MOD_LOG_FORENSIC_VERSION "mod_log_forensic/0.2"
32
33 /* Make sure the version of proftpd is as necessary. */
34 #if PROFTPD_VERSION_NUMBER < 0x0001030403
35 # error "ProFTPD 1.3.4rc3 or later required"
36 #endif
37
38 module log_forensic_module;
39
40 static pool *forensic_pool = NULL;
41 static int forensic_engine = FALSE;
42 static int forensic_logfd = -1;
43
44 /* NOTE: We do NOT use trace logging within this module. Why not? Doing
45 * so creates a recursive loop, due to the default log listener for
46 * trace logging events.
47 */
48
49 /* Criteria for flushing out the "forensic" logs. */
50 #define FORENSIC_CRIT_FAILED_LOGIN 0x00001
51 #define FORENSIC_CRIT_MODULE_CONFIG 0x00002
52 #define FORENSIC_CRIT_UNTIMELY_DEATH 0x00004
53
54 #define FORENSIC_CRIT_DEFAULT \
55 (FORENSIC_CRIT_FAILED_LOGIN|FORENSIC_CRIT_UNTIMELY_DEATH)
56
57 static unsigned long forensic_criteria = FORENSIC_CRIT_DEFAULT;
58
59 /* Use a ring buffer for the cached/buffered log messages; the index pointing
60 * to where to stash the next message then moves around the ring.
61 *
62 * Messages in the ring will each have their own pool, allocated out of
63 * a module-specific ring pool. Whenever a message is overwritten/replaced
64 * in the ring, its message-specific pool is destroyed.
65 */
66 #define FORENSIC_DEFAULT_NMSGS 1024
67
68 struct forensic_msg {
69 pool *fm_pool;
70
71 unsigned int fm_log_type;
72 int fm_log_level;
73 const char *fm_msg;
74 size_t fm_msglen;
75 };
76
77 static struct forensic_msg **forensic_msgs = NULL;
78 static unsigned int forensic_nmsgs = FORENSIC_DEFAULT_NMSGS;
79 static unsigned int forensic_msg_idx = 0;
80
81 static pool *forensic_ring_pool = NULL;
82
83 #define FORENSIC_MAX_LEVELS 50
84 static const char *forensic_log_levels[] = {
85 "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
86 "10", "11", "12", "13", "14", "15", "16", "17", "18", "19",
87 "20", "21", "22", "23", "24", "25", "26", "27", "28", "29",
88 "30", "31", "32", "33", "34", "35", "36", "37", "38", "39",
89 "40", "41", "42", "43", "44", "45", "46", "47", "48", "49"
90 };
91
92 /* Necessary prototypes */
93 static int forensic_sess_init(void);
94
95 static void forensic_add_msg(unsigned int log_type, int log_level,
96 const char *log_msg, size_t log_msglen) {
97 struct forensic_msg *fm;
98 char *fm_msg;
99 pool *msg_pool;
100
101 /* Get the message that's currently in the ring where we want add our new
102 * one.
103 */
104 fm = forensic_msgs[forensic_msg_idx];
105 if (fm != NULL) {
106 destroy_pool(fm->fm_pool);
107 forensic_msgs[forensic_msg_idx] = NULL;
108 }
109
110 /* Add this message into the ring. */
111 msg_pool = pr_pool_create_sz(forensic_ring_pool, 128);
112 pr_pool_tag(msg_pool, "Forensic Msg Pool");
113 fm = pcalloc(msg_pool, sizeof(struct forensic_msg));
114 fm->fm_pool = msg_pool;
115 fm->fm_log_type = log_type;
116 fm->fm_log_level = log_level;
117
118 fm_msg = palloc(fm->fm_pool, log_msglen + 1);
119 memcpy(fm_msg, log_msg, log_msglen);
120 fm_msg[log_msglen] = '\0';
121
122 fm->fm_msg = fm_msg;
123 fm->fm_msglen = log_msglen;
124
125 forensic_msgs[forensic_msg_idx] = fm;
126
127 forensic_msg_idx += 1;
128 if (forensic_msg_idx == forensic_nmsgs) {
129 /* Wrap around */
130 forensic_msg_idx = 0;
131 }
132 }
133
134 static const char *forensic_get_begin_marker(unsigned int criterion,
135 size_t *markerlen) {
136 const char *marker = NULL;
137
138 switch (criterion) {
139 case FORENSIC_CRIT_FAILED_LOGIN:
140 marker = "-----BEGIN FAILED LOGIN FORENSICS-----\n";
141 break;
142
143 case FORENSIC_CRIT_MODULE_CONFIG:
144 marker = "-----BEGIN MODULE CONFIG FORENSICS-----\n";
145 break;
146
147 case FORENSIC_CRIT_UNTIMELY_DEATH:
148 marker = "-----BEGIN UNTIMELY DEATH FORENSICS-----\n";
149 break;
150 }
151
152 if (marker != NULL) {
153 *markerlen = strlen(marker);
154 }
155
156 return marker;
157 }
158
159 static const char *forensic_get_end_marker(unsigned int criterion,
160 size_t *markerlen) {
161 const char *marker = NULL;
162
163 switch (criterion) {
164 case FORENSIC_CRIT_FAILED_LOGIN:
165 marker = "-----END FAILED LOGIN FORENSICS-----\n";
166 break;
167
168 case FORENSIC_CRIT_MODULE_CONFIG:
169 marker = "-----END MODULE CONFIG FORENSICS-----\n";
170 break;
171
172 case FORENSIC_CRIT_UNTIMELY_DEATH:
173 marker = "-----END UNTIMELY DEATH FORENSICS-----\n";
174 break;
175 }
176
177 if (marker != NULL) {
178 *markerlen = strlen(marker);
179 }
180
181 return marker;
182 }
183
184 /* Rather than deal with some homegrown itoa() sort of thing for converting
185 * the log level number to an easily printable string, I'm using the level
186 * as an index in a precomputed array of strings.
187 */
188 static const char *forensic_get_level_str(int log_level) {
189 int i;
190
191 i = log_level;
192 if (i < 0) {
193 i = 0;
194 }
195
196 if (i >= FORENSIC_MAX_LEVELS) {
197 return "N";
198 }
199
200 return forensic_log_levels[i];
201 }
202
203 static void forensic_write_metadata(void) {
204 const char *client_ip, *server_ip, *proto, *unique_id;
205 int server_port;
206 char server_port_str[32], uid_str[32], gid_str[32], elapsed_str[64],
207 raw_bytes_in_str[64], raw_bytes_out_str[64],
208 total_bytes_in_str[64], total_bytes_out_str[64],
209 total_files_in_str[64], total_files_out_str[64];
210 size_t unique_idlen = 0;
211
212 /* 64 vectors is currently more than necessary, but it's better to have
213 * too many than too little.
214 */
215 struct iovec iov[64];
216 int niov = 0, res;
217 uint64_t now;
218 unsigned long elapsed_ms;
219
220 /* Write session metadata in key/value message headers:
221 *
222 * Client-Address:
223 * Server-Address:
224 * Elapsed:
225 * Protocol:
226 * User:
227 * UID:
228 * GID:
229 * [UNIQUE_ID:]
230 * Raw-Bytes-In:
231 * Raw-Bytes-Out:
232 * Total-Bytes-In:
233 * Total-Bytes-Out:
234 * Total-Files-In:
235 * Total-Files-Out:
236 */
237
238 client_ip = pr_netaddr_get_ipstr(pr_netaddr_get_sess_remote_addr());
239 server_ip = pr_netaddr_get_ipstr(pr_netaddr_get_sess_local_addr());
240 server_port = ntohs(pr_netaddr_get_port(pr_netaddr_get_sess_local_addr()));
241
242 /* Client address */
243 iov[niov].iov_base = "Client-Address: ";
244 iov[niov].iov_len = 16;
245 niov++;
246
247 iov[niov].iov_base = (void *) client_ip;
248 iov[niov].iov_len = strlen(client_ip);
249 niov++;
250
251 iov[niov].iov_base = "\n";
252 iov[niov].iov_len = 1;
253 niov++;
254
255 /* Server address */
256 iov[niov].iov_base = "Server-Address: ";
257 iov[niov].iov_len = 16;
258 niov++;
259
260 iov[niov].iov_base = (void *) server_ip;
261 iov[niov].iov_len = strlen(server_ip);
262 niov++;
263
264 memset(server_port_str, '\0', sizeof(server_port_str));
265 res = pr_snprintf(server_port_str, sizeof(server_port_str)-1, ":%d\n",
266 server_port);
267 iov[niov].iov_base = server_port_str;
268 iov[niov].iov_len = res;
269 niov++;
270
271 /* Elapsed (in ms) */
272 iov[niov].iov_base = "Elapsed: ";
273 iov[niov].iov_len = 9;
274 niov++;
275
276 pr_gettimeofday_millis(&now);
277 elapsed_ms = (unsigned long) (now - session.connect_time_ms);
278
279 memset(elapsed_str, '\0', sizeof(elapsed_str));
280 res = pr_snprintf(elapsed_str, sizeof(elapsed_str)-1, "%lu\n", elapsed_ms);
281 iov[niov].iov_base = (void *) elapsed_str;
282 iov[niov].iov_len = res;
283 niov++;
284
285 /* Protocol */
286 proto = pr_session_get_protocol(0);
287 iov[niov].iov_base = "Protocol: ";
288 iov[niov].iov_len = 10;
289 niov++;
290
291 iov[niov].iov_base = (char *) proto;
292 iov[niov].iov_len = strlen(proto);
293 niov++;
294
295 iov[niov].iov_base = "\n";
296 iov[niov].iov_len = 1;
297 niov++;
298
299 /* User */
300 if (session.user) {
301 iov[niov].iov_base = "User: ";
302 iov[niov].iov_len = 6;
303 niov++;
304
305 iov[niov].iov_base = (void *) session.user;
306 iov[niov].iov_len = strlen(session.user);
307 niov++;
308
309 iov[niov].iov_base = "\n";
310 iov[niov].iov_len = 1;
311 niov++;
312 }
313
314 /* UID */
315 iov[niov].iov_base = "UID: ";
316 iov[niov].iov_len = 5;
317 niov++;
318
319 memset(uid_str, '\0', sizeof(uid_str));
320 res = pr_snprintf(uid_str, sizeof(uid_str)-1, "%lu\n",
321 (unsigned long) geteuid());
322 iov[niov].iov_base = uid_str;
323 iov[niov].iov_len = res;
324 niov++;
325
326 /* GID */
327 iov[niov].iov_base = "GID: ";
328 iov[niov].iov_len = 5;
329 niov++;
330
331 memset(gid_str, '\0', sizeof(gid_str));
332 res = pr_snprintf(gid_str, sizeof(gid_str)-1, "%lu\n",
333 (unsigned long) getegid());
334 iov[niov].iov_base = gid_str;
335 iov[niov].iov_len = res;
336 niov++;
337
338 /* UNIQUE_ID (from mod_unique_id), if present. */
339 unique_id = pr_table_get(session.notes, "UNIQUE_ID", &unique_idlen);
340 if (unique_id != NULL) {
341 iov[niov].iov_base = "UNIQUE_ID: ";
342 iov[niov].iov_len = 11;
343 niov++;
344
345 iov[niov].iov_base = (char *) unique_id;
346 iov[niov].iov_len = unique_idlen;
347 niov++;
348
349 iov[niov].iov_base = "\n";
350 iov[niov].iov_len = 1;
351 niov++;
352 }
353
354 /* Raw bytes in */
355 iov[niov].iov_base = "Raw-Bytes-In: ";
356 iov[niov].iov_len = 14;
357 niov++;
358
359 if (session.total_raw_in == 0) {
360 iov[niov].iov_base = "0\n";
361 iov[niov].iov_len = 2;
362 niov++;
363
364 } else {
365 memset(raw_bytes_in_str, '\0', sizeof(raw_bytes_in_str));
366 res = pr_snprintf(raw_bytes_in_str, sizeof(raw_bytes_in_str)-1,
367 "%" PR_LU "\n", (pr_off_t) session.total_raw_in);
368 iov[niov].iov_base = raw_bytes_in_str;
369 iov[niov].iov_len = res;
370 niov++;
371 }
372
373 /* Raw bytes out */
374 iov[niov].iov_base = "Raw-Bytes-Out: ";
375 iov[niov].iov_len = 15;
376 niov++;
377
378 if (session.total_raw_out == 0) {
379 iov[niov].iov_base = "0\n";
380 iov[niov].iov_len = 2;
381 niov++;
382
383 } else {
384 memset(raw_bytes_out_str, '\0', sizeof(raw_bytes_out_str));
385 res = pr_snprintf(raw_bytes_out_str, sizeof(raw_bytes_out_str)-1,
386 "%" PR_LU "\n", (pr_off_t) session.total_raw_out);
387 iov[niov].iov_base = raw_bytes_out_str;
388 iov[niov].iov_len = res;
389 niov++;
390 }
391
392 /* Total bytes in */
393 iov[niov].iov_base = "Total-Bytes-In: ";
394 iov[niov].iov_len = 16;
395 niov++;
396
397 if (session.total_bytes_in == 0) {
398 iov[niov].iov_base = "0\n";
399 iov[niov].iov_len = 2;
400 niov++;
401
402 } else {
403 memset(total_bytes_in_str, '\0', sizeof(total_bytes_in_str));
404 res = pr_snprintf(total_bytes_in_str, sizeof(total_bytes_in_str)-1,
405 "%" PR_LU "\n", (pr_off_t) session.total_bytes_in);
406 iov[niov].iov_base = total_bytes_in_str;
407 iov[niov].iov_len = res;
408 niov++;
409 }
410
411 /* Total bytes out */
412 iov[niov].iov_base = "Total-Bytes-Out: ";
413 iov[niov].iov_len = 17;
414 niov++;
415
416 if (session.total_bytes_out == 0) {
417 iov[niov].iov_base = "0\n";
418 iov[niov].iov_len = 2;
419 niov++;
420
421 } else {
422 memset(total_bytes_out_str, '\0', sizeof(total_bytes_out_str));
423 res = pr_snprintf(total_bytes_out_str, sizeof(total_bytes_out_str)-1,
424 "%" PR_LU "\n", (pr_off_t) session.total_bytes_out);
425 iov[niov].iov_base = total_bytes_out_str;
426 iov[niov].iov_len = res;
427 niov++;
428 }
429
430 /* Total files in */
431 iov[niov].iov_base = "Total-Files-In: ";
432 iov[niov].iov_len = 16;
433 niov++;
434
435 if (session.total_files_in == 0) {
436 iov[niov].iov_base = "0\n";
437 iov[niov].iov_len = 2;
438 niov++;
439
440 } else {
441 memset(total_files_in_str, '\0', sizeof(total_files_in_str));
442 res = pr_snprintf(total_files_in_str, sizeof(total_files_in_str)-1,
443 "%u\n", session.total_files_in);
444 iov[niov].iov_base = total_files_in_str;
445 iov[niov].iov_len = res;
446 niov++;
447 }
448
449 /* Total files out */
450 iov[niov].iov_base = "Total-Files-Out: ";
451 iov[niov].iov_len = 17;
452 niov++;
453
454 if (session.total_files_out == 0) {
455 iov[niov].iov_base = "0\n";
456 iov[niov].iov_len = 2;
457 niov++;
458
459 } else {
460 memset(total_files_out_str, '\0', sizeof(total_files_out_str));
461 res = pr_snprintf(total_files_out_str, sizeof(total_files_out_str)-1,
462 "%u\n", session.total_files_out);
463 iov[niov].iov_base = total_files_out_str;
464 iov[niov].iov_len = res;
465 niov++;
466 }
467
468 iov[niov].iov_base = "\n";
469 iov[niov].iov_len = 1;
470 niov++;
471
472 res = writev(forensic_logfd, iov, niov);
473 }
474
475 static void forensic_write_msgs(unsigned int criterion) {
476 register unsigned int i;
477 unsigned int start_idx, end_idx;
478 int res;
479 const char *crit_marker = NULL;
480 size_t crit_markerlen = 0;
481
482 /* XXX An interesting optimization would be to rework this code so that
483 * we used writev(2) to write out the buffer as quickly as possible,
484 * taking IOV_MAX into account.
485 */
486
487 crit_marker = forensic_get_begin_marker(criterion, &crit_markerlen);
488 if (crit_marker != NULL) {
489 res = write(forensic_logfd, crit_marker, crit_markerlen);
490 }
491
492 forensic_write_metadata();
493
494 /* The head of the log messages (i.e. the oldest message) is always where we
495 * want to place the newest message.
496 */
497 start_idx = forensic_msg_idx;
498 end_idx = forensic_msg_idx - 1;
499 if (forensic_msg_idx == 0) {
500 end_idx = forensic_nmsgs - 1;
501 }
502
503 i = start_idx;
504 while (i != end_idx) {
505 struct forensic_msg *fm;
506
507 pr_signals_handle();
508
509 fm = forensic_msgs[i];
510 if (fm != NULL) {
511 const char *level;
512 size_t level_len;
513
514 level = forensic_get_level_str(fm->fm_log_level);
515 level_len = strlen(level);
516
517 switch (fm->fm_log_type) {
518 case PR_LOG_TYPE_UNSPEC:
519 res = write(forensic_logfd, "[Unspec:", 8);
520 res = write(forensic_logfd, level, level_len);
521 res = write(forensic_logfd, "] ", 2);
522 break;
523
524 case PR_LOG_TYPE_XFERLOG:
525 res = write(forensic_logfd, "[TransferLog:", 13);
526 res = write(forensic_logfd, level, level_len);
527 res = write(forensic_logfd, "] ", 2);
528 break;
529
530 case PR_LOG_TYPE_SYSLOG: {
531 char pid_str[32];
532
533 res = write(forensic_logfd, "[syslog:", 8);
534 res = write(forensic_logfd, level, level_len);
535
536 /* syslogd normally adds the PID; we thus need to add the PID in
537 * here as well, to aid in the correlation of these log lines
538 * with other tools/diagnostics.
539 */
540 res = write(forensic_logfd, ", PID ", 6);
541
542 memset(pid_str, '\0', sizeof(pid_str));
543 res = pr_snprintf(pid_str, sizeof(pid_str)-1, "%lu",
544 (unsigned long) (session.pid ? session.pid : getpid()));
545 res = write(forensic_logfd, pid_str, res);
546
547 res = write(forensic_logfd, "] ", 2);
548 break;
549 }
550
551 case PR_LOG_TYPE_SYSTEMLOG:
552 res = write(forensic_logfd, "[SystemLog:", 11);
553 res = write(forensic_logfd, level, level_len);
554 res = write(forensic_logfd, "] ", 2);
555 break;
556
557 case PR_LOG_TYPE_EXTLOG:
558 res = write(forensic_logfd, "[ExtendedLog:", 13);
559 res = write(forensic_logfd, level, level_len);
560 res = write(forensic_logfd, "] ", 2);
561 break;
562
563 case PR_LOG_TYPE_TRACELOG:
564 res = write(forensic_logfd, "[TraceLog:", 10);
565 res = write(forensic_logfd, level, level_len);
566 res = write(forensic_logfd, "] ", 2);
567 break;
568 }
569
570 res = write(forensic_logfd, fm->fm_msg, fm->fm_msglen);
571 while (res < 0) {
572 if (errno == EINTR) {
573 pr_signals_handle();
574 res = write(forensic_logfd, fm->fm_msg, fm->fm_msglen);
575 continue;
576 }
577 }
578
579 /* syslog-type messages don't have a newline appended to them, since
580 * syslogd handles that. So we then need to add our own newline here.
581 */
582 if (fm->fm_log_type == PR_LOG_TYPE_SYSLOG) {
583 res = write(forensic_logfd, "\n", 1);
584 }
585
586 destroy_pool(fm->fm_pool);
587 forensic_msgs[i] = NULL;
588 }
589
590 i++;
591 if (i == forensic_nmsgs) {
592 /* Wrap around */
593 i = 0;
594 }
595 }
596
597 crit_marker = forensic_get_end_marker(criterion, &crit_markerlen);
598 if (crit_marker != NULL) {
599 res = write(forensic_logfd, crit_marker, crit_markerlen);
600 }
601 }
602
603 /* Configuration handlers
604 */
605
606 /* usage: ForensicLogBufferSize count */
607 MODRET set_forensiclogbuffersize(cmd_rec *cmd) {
608 config_rec *c;
609 unsigned int count;
610 char *ptr = NULL;
611
612 CHECK_ARGS(cmd, 1);
613 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
614
615 count = strtoul(cmd->argv[1], &ptr, 10);
616 if (ptr && *ptr) {
617 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "badly formatted number: ",
618 cmd->argv[1], NULL));
619 }
620
621 if (count == 0) {
622 CONF_ERROR(cmd, "size must be greater than zero");
623 }
624
625 c = add_config_param(cmd->argv[0], 1, NULL);
626 c->argv[0] = palloc(c->pool, sizeof(unsigned int));
627 *((unsigned int *) c->argv[0]) = count;
628
629 return PR_HANDLED(cmd);
630 }
631
632 /* usage: ForensicLogCapture type1 ... typeN */
633 MODRET set_forensiclogcapture(cmd_rec *cmd) {
634 config_rec *c;
635 int unspec_listen = FALSE;
636 int xferlog_listen = FALSE;
637 int syslog_listen = FALSE;
638 int systemlog_listen = FALSE;
639 int extlog_listen = FALSE;
640 int tracelog_listen = FALSE;
641 register unsigned int i;
642
643 if (cmd->argc-1 < 1) {
644 CONF_ERROR(cmd, "wrong number of parameters");
645 }
646
647 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
648
649 for (i = 1; i < cmd->argc; i++) {
650 if (strncasecmp(cmd->argv[i], "Unspec", 7) == 0 ||
651 strncasecmp(cmd->argv[i], "Unknown", 8) == 0) {
652 unspec_listen = TRUE;
653
654 } else if (strncasecmp(cmd->argv[i], "TransferLog", 12) == 0) {
655 xferlog_listen = TRUE;
656
657 } else if (strncasecmp(cmd->argv[i], "Syslog", 7) == 0) {
658 syslog_listen = TRUE;
659
660 } else if (strncasecmp(cmd->argv[i], "SystemLog", 10) == 0) {
661 systemlog_listen = TRUE;
662
663 } else if (strncasecmp(cmd->argv[i], "ExtendedLog", 12) == 0) {
664 extlog_listen = TRUE;
665
666 } else if (strncasecmp(cmd->argv[i], "TraceLog", 9) == 0) {
667 tracelog_listen = TRUE;
668
669 } else {
670 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "unknown log type: ",
671 cmd->argv[i], NULL));
672 }
673 }
674
675 c = add_config_param(cmd->argv[0], 6, NULL, NULL, NULL, NULL, NULL, NULL);
676 c->argv[0] = palloc(c->pool, sizeof(int));
677 *((int *) c->argv[0]) = unspec_listen;
678 c->argv[1] = palloc(c->pool, sizeof(int));
679 *((int *) c->argv[1]) = xferlog_listen;
680 c->argv[2] = palloc(c->pool, sizeof(int));
681 *((int *) c->argv[2]) = syslog_listen;
682 c->argv[3] = palloc(c->pool, sizeof(int));
683 *((int *) c->argv[3]) = systemlog_listen;
684 c->argv[4] = palloc(c->pool, sizeof(int));
685 *((int *) c->argv[4]) = extlog_listen;
686 c->argv[5] = palloc(c->pool, sizeof(int));
687 *((int *) c->argv[5]) = tracelog_listen;
688
689 return PR_HANDLED(cmd);
690 }
691
692 /* usage: ForensicLogCriteria ... */
693 MODRET set_forensiclogcriteria(cmd_rec *cmd) {
694 config_rec *c;
695 unsigned long criteria = 0UL;
696 register unsigned int i;
697
698 if (cmd->argc-1 < 1) {
699 CONF_ERROR(cmd, "wrong number of parameters");
700 }
701
702 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
703
704 /* Possible criteria:
705 *
706 * FailedLogin
707 * ModuleConfig
708 * UntimelyDeath
709 */
710
711 for (i = 1; i < cmd->argc; i++) {
712 if (strncasecmp(cmd->argv[i], "FailedLogin", 12) == 0) {
713 criteria |= FORENSIC_CRIT_FAILED_LOGIN;
714
715 } else if (strncasecmp(cmd->argv[i], "ModuleConfig", 13) == 0) {
716 criteria |= FORENSIC_CRIT_MODULE_CONFIG;
717
718 } else if (strncasecmp(cmd->argv[i], "UntimelyDeath", 14) == 0) {
719 criteria |= FORENSIC_CRIT_UNTIMELY_DEATH;
720
721 } else {
722 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "unknown criterion: ",
723 cmd->argv[i], NULL));
724 }
725 }
726
727 c = add_config_param(cmd->argv[0], 1, NULL);
728 c->argv[0] = palloc(c->pool, sizeof(unsigned long));
729 *((unsigned long *) c->argv[0]) = criteria;
730
731 return PR_HANDLED(cmd);
732 }
733
734 /* usage: ForensicLogEngine on|off */
735 MODRET set_forensiclogengine(cmd_rec *cmd) {
736 config_rec *c;
737 int engine;
738
739 CHECK_ARGS(cmd, 1);
740 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
741
742 engine = get_boolean(cmd, 1);
743 if (engine == -1) {
744 CONF_ERROR(cmd, "expected Boolean parameter");
745 }
746
747 c = add_config_param(cmd->argv[0], 1, NULL);
748 c->argv[0] = palloc(c->pool, sizeof(int));
749 *((int *) c->argv[0]) = engine;
750
751 return PR_HANDLED(cmd);
752 }
753
754 /* usage: ForensicLogFile path */
755 MODRET set_forensiclogfile(cmd_rec *cmd) {
756 CHECK_ARGS(cmd, 1);
757 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
758
759 if (pr_fs_valid_path(cmd->argv[1]) < 0) {
760 CONF_ERROR(cmd, "must be an absolute path");
761 }
762
763 (void) add_config_param_str(cmd->argv[0], 1, cmd->argv[1]);
764 return PR_HANDLED(cmd);
765 }
766
767 /* Command handlers
768 */
769
770 MODRET forensic_pass_err(cmd_rec *cmd) {
771 if (!forensic_engine) {
772 return PR_DECLINED(cmd);
773 }
774
775 if (forensic_criteria & FORENSIC_CRIT_FAILED_LOGIN) {
776 forensic_write_msgs(FORENSIC_CRIT_FAILED_LOGIN);
777 }
778
779 return PR_DECLINED(cmd);
780 }
781
782 /* Event Listeners
783 */
784
785 static void forensic_exit_ev(const void *event_data, void *user_data) {
786
787 switch (session.disconnect_reason) {
788 case PR_SESS_DISCONNECT_SIGNAL:
789 if (forensic_criteria & FORENSIC_CRIT_UNTIMELY_DEATH) {
790 forensic_write_msgs(FORENSIC_CRIT_UNTIMELY_DEATH);
791 }
792 break;
793
794 case PR_SESS_DISCONNECT_MODULE_ACL:
795 if (forensic_criteria & FORENSIC_CRIT_MODULE_CONFIG) {
796 forensic_write_msgs(FORENSIC_CRIT_MODULE_CONFIG);
797 }
798 break;
799 }
800
801 return;
802 }
803
804 static void forensic_log_ev(const void *event_data, void *user_data) {
805 const pr_log_event_t *le;
806
807 le = event_data;
808 forensic_add_msg(le->log_type, le->log_level, le->log_msg, le->log_msglen);
809 }
810
811 #if defined(PR_SHARED_MODULE)
812 static void forensic_mod_unload_ev(const void *event_data, void *user_data) {
813 if (strcmp("mod_log_forensic.c", (const char *) event_data) == 0) {
814 pr_event_unregister(&log_forensic_module, NULL, NULL);
815 }
816 }
817 #endif /* PR_SHARED_MODULE */
818
819 static void forensic_sess_reinit_ev(const void *event_data, void *user_data) {
820 int res;
821
822 /* A HOST command changed the main_server pointer, reinitialize ourselves. */
823
824 pr_event_unregister(&log_forensic_module, "core.exit", forensic_exit_ev);
825 pr_event_unregister(&log_forensic_module, "core.log.unspec", forensic_log_ev);
826 pr_event_unregister(&log_forensic_module, "core.log.xferlog",
827 forensic_log_ev);
828 pr_event_unregister(&log_forensic_module, "core.log.syslog", forensic_log_ev);
829 pr_event_unregister(&log_forensic_module, "core.log.systemlog",
830 forensic_log_ev);
831 pr_event_unregister(&log_forensic_module, "core.log.extlog", forensic_log_ev);
832 pr_event_unregister(&log_forensic_module, "core.log.tracelog",
833 forensic_log_ev);
834 pr_event_unregister(&log_forensic_module, "core.session-reinit",
835 forensic_sess_reinit_ev);
836
837 forensic_engine = FALSE;
838 (void) close(forensic_logfd);
839 forensic_logfd = -1;
840 forensic_criteria = FORENSIC_CRIT_DEFAULT;
841 forensic_msgs = NULL;
842 forensic_nmsgs = FORENSIC_DEFAULT_NMSGS;
843 forensic_msg_idx = 0;
844
845 if (forensic_ring_pool != NULL) {
846 destroy_pool(forensic_ring_pool);
847 forensic_ring_pool = NULL;
848 }
849
850 res = forensic_sess_init();
851 if (res < 0) {
852 pr_session_disconnect(&log_forensic_module,
853 PR_SESS_DISCONNECT_SESSION_INIT_FAILED, NULL);
854 }
855 }
856
857 /* Module Initialization
858 */
859
860 static int forensic_init(void) {
861 #if defined(PR_SHARED_MODULE)
862 pr_event_register(&log_forensic_module, "core.module-unload",
863 forensic_mod_unload_ev, NULL);
864 #endif /* PR_SHARED_MODULE */
865
866 return 0;
867 }
868
869 static int forensic_sess_init(void) {
870 config_rec *c;
871 int unspec_listen = TRUE;
872 int xferlog_listen = TRUE;
873 int syslog_listen = TRUE;
874 int systemlog_listen = TRUE;
875 int extlog_listen = TRUE;
876 int tracelog_listen = TRUE;
877 int res, xerrno;
878
879 pr_event_register(&log_forensic_module, "core.session-reinit",
880 forensic_sess_reinit_ev, NULL);
881
882 /* Is this module enabled? */
883 c = find_config(main_server->conf, CONF_PARAM, "ForensicLogEngine", FALSE);
884 if (c != NULL) {
885 forensic_engine = *((int *) c->argv[0]);
886 }
887
888 if (forensic_engine != TRUE) {
889 return 0;
890 }
891
892 /* Do we have the required path for our logging? */
893 c = find_config(main_server->conf, CONF_PARAM, "ForensicLogFile", FALSE);
894 if (c == NULL) {
895 pr_log_debug(DEBUG1, MOD_LOG_FORENSIC_VERSION
896 ": missing required ForensicLogFile setting, disabling module");
897 forensic_engine = FALSE;
898 return 0;
899 }
900
901 pr_signals_block();
902 PRIVS_ROOT
903 res = pr_log_openfile((const char *) c->argv[0], &forensic_logfd, 0640);
904 xerrno = errno;
905 PRIVS_RELINQUISH
906 pr_signals_unblock();
907
908 if (res < 0) {
909 const char *path;
910
911 path = c->argv[0];
912
913 if (res == -1) {
914 pr_log_pri(PR_LOG_NOTICE, MOD_LOG_FORENSIC_VERSION
915 ": notice: unable to open ForensicLogFile '%s': %s", path,
916 strerror(xerrno));
917
918 } else if (res == PR_LOG_WRITABLE_DIR) {
919 pr_log_pri(PR_LOG_WARNING, MOD_LOG_FORENSIC_VERSION
920 ": notice: unable to open ForensicLogFile '%s': parent directory is "
921 "world-writable", path);
922
923 } else if (res == PR_LOG_SYMLINK) {
924 pr_log_pri(PR_LOG_WARNING, MOD_LOG_FORENSIC_VERSION
925 ": notice: unable to open ForensicLogFile '%s': "
926 "cannot log to a symlink", path);
927 }
928
929 pr_log_debug(DEBUG0, MOD_LOG_FORENSIC_VERSION
930 ": unable to ForensicLogFile '%s', disabling module", path);
931 forensic_engine = FALSE;
932 return 0;
933 }
934
935 /* Are there any log types for which we shouldn't be listening? */
936 c = find_config(main_server->conf, CONF_PARAM, "ForensicLogCapture", FALSE);
937 if (c != NULL) {
938 unspec_listen = *((int *) c->argv[0]);
939 xferlog_listen = *((int *) c->argv[1]);
940 syslog_listen = *((int *) c->argv[2]);
941 systemlog_listen = *((int *) c->argv[3]);
942 extlog_listen = *((int *) c->argv[4]);
943 tracelog_listen = *((int *) c->argv[5]);
944 }
945
946 /* What criteria are we to use for logging our captured log messages */
947 c = find_config(main_server->conf, CONF_PARAM, "ForensicLogCriteria", FALSE);
948 if (c != NULL) {
949 forensic_criteria = *((unsigned long *) c->argv[0]);
950 }
951
952 if (forensic_pool == NULL) {
953 forensic_pool = make_sub_pool(session.pool);
954 pr_pool_tag(forensic_pool, MOD_LOG_FORENSIC_VERSION);
955 }
956
957 c = find_config(main_server->conf, CONF_PARAM, "ForensicLogBufferSize",
958 FALSE);
959 if (c != NULL) {
960 forensic_nmsgs = *((unsigned int *) c->argv[0]);
961 }
962
963 forensic_msgs = pcalloc(forensic_pool,
964 sizeof(struct forensic_msg) * forensic_nmsgs);
965 forensic_ring_pool = pr_pool_create_sz(forensic_pool, 256);
966 pr_pool_tag(forensic_ring_pool, "Forensic Ring Pool");
967
968 /* We register our event listeners as the last thing we do. */
969
970 if ((forensic_criteria & FORENSIC_CRIT_MODULE_CONFIG) ||
971 (forensic_criteria & FORENSIC_CRIT_UNTIMELY_DEATH)) {
972 pr_event_register(&log_forensic_module, "core.exit", forensic_exit_ev,
973 NULL);
974 }
975
976 if (unspec_listen == TRUE) {
977 pr_event_register(&log_forensic_module, "core.log.unspec", forensic_log_ev,
978 NULL);
979 }
980
981 if (xferlog_listen == TRUE) {
982 pr_event_register(&log_forensic_module, "core.log.xferlog", forensic_log_ev,
983 NULL);
984 }
985
986 if (syslog_listen == TRUE) {
987 pr_event_register(&log_forensic_module, "core.log.syslog", forensic_log_ev,
988 NULL);
989 }
990
991 if (systemlog_listen == TRUE) {
992 pr_event_register(&log_forensic_module, "core.log.systemlog",
993 forensic_log_ev, NULL);
994 }
995
996 if (extlog_listen == TRUE) {
997 pr_event_register(&log_forensic_module, "core.log.extlog", forensic_log_ev,
998 NULL);
999 }
1000
1001 if (tracelog_listen == TRUE) {
1002 pr_event_register(&log_forensic_module, "core.log.tracelog",
1003 forensic_log_ev, NULL);
1004 }
1005
1006 return 0;
1007 }
1008
1009 /* Module API tables
1010 */
1011
1012 static conftable forensic_conftab[] = {
1013 { "ForensicLogBufferSize", set_forensiclogbuffersize, NULL },
1014 { "ForensicLogCapture", set_forensiclogcapture, NULL },
1015 { "ForensicLogCriteria", set_forensiclogcriteria, NULL },
1016 { "ForensicLogEngine", set_forensiclogengine, NULL },
1017 { "ForensicLogFile", set_forensiclogfile, NULL },
1018
1019 { NULL, NULL, NULL }
1020 };
1021
1022 static cmdtable forensic_cmdtab[] = {
1023 { LOG_CMD_ERR, C_PASS, G_NONE, forensic_pass_err, FALSE, FALSE },
1024
1025 { 0, NULL }
1026 };
1027
1028 module log_forensic_module = {
1029 /* Always NULL */
1030 NULL, NULL,
1031
1032 /* Module API version */
1033 0x20,
1034
1035 /* Module name */
1036 "log_forensic",
1037
1038 /* Module configuration directive table */
1039 forensic_conftab,
1040
1041 /* Module command handler table */
1042 forensic_cmdtab,
1043
1044 /* Module auth handler table */
1045 NULL,
1046
1047 /* Module initialization */
1048 forensic_init,
1049
1050 /* Session initialization */
1051 forensic_sess_init,
1052
1053 /* Module version */
1054 MOD_LOG_FORENSIC_VERSION
1055 };
1056