Debian Codegraph @

contrib/mod_snmp/mod_snmp.c

raw symbols SCIP
1 /*
2 * ProFTPD - mod_snmp
3 * Copyright (c) 2008-2025 TJ Saunders
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 * $Archive: mod_snmp.a $
26 */
27
28 #include "mod_snmp.h"
29 #include "asn1.h"
30 #include "db.h"
31 #include "mib.h"
32 #include "packet.h"
33 #include "pdu.h"
34 #include "msg.h"
35 #include "notify.h"
36
37 /* Defaults */
38 #define SNMP_DEFAULT_AGENT_PORT 161
39 #define SNMP_DEFAULT_TRAP_PORT 162
40
41 /* Agent type/role */
42 #define SNMP_AGENT_TYPE_MASTER 1
43 #define SNMP_AGENT_TYPE_AGENTX 2
44
45 extern xaset_t *server_list;
46
47 module snmp_module;
48
49 int snmp_logfd = -1;
50 pool *snmp_pool = NULL;
51 conn_t *snmp_conn = NULL;
52 struct timeval snmp_start_tv;
53 int snmp_proto_udp = IPPROTO_UDP;
54
55 /* mod_snmp option flags */
56 #define SNMP_OPT_RESTART_CLEARS_COUNTERS 0x0001
57
58 static pid_t snmp_agent_pid = 0;
59 static int snmp_enabled = TRUE;
60 static int snmp_engine = FALSE;
61 static const char *snmp_logname = NULL;
62 static unsigned long snmp_opts = 0UL;
63
64 static const char *snmp_community = NULL;
65
66 /* The list of SNMPNotify receivers/managers to which to send notifications. */
67 static array_header *snmp_notifys = NULL;
68
69 /* This number defined as the maximum 'max-bindings' value in RFC19105; it's
70 * good enough for the default maximum number of variables in a bindings list
71 * to process.
72 */
73 static unsigned int snmp_max_variables = SNMP_PDU_MAX_BINDINGS;
74
75 /* Number of seconds to wait for the SNMP agent process to stop before
76 * we terminate it with extreme prejudice.
77 *
78 * Currently this has a granularity of seconds; needs to be in millsecs
79 * (e.g. for 500 ms timeout).
80 */
81 static time_t snmp_agent_timeout = 1;
82
83 static off_t snmp_retr_bytes = 0, snmp_stor_bytes = 0;
84
85 static const char *trace_channel = "snmp";
86
87 static int snmp_check_class_access(xaset_t *set, const char *name,
88 struct snmp_packet *pkt) {
89 config_rec *c;
90 int ok = FALSE;
91
92 /* If no class was found for this session, short-circuit the check.
93 * Note: this is an optimization that can/should be applied to the
94 * core engine as well, e.g. in the proftpd-1.3.5 devel cycle.
95 */
96 if (pkt->remote_class == NULL) {
97 return ok;
98 }
99
100 /* XXX Note: the pr_expr_eval_class_* functions assume the use of the
101 * global session variable. They should be refactored to take the
102 * class as an argument.
103 */
104
105 #if PROFTPD_VERSION_NUMBER >= 0x0001030501
106 session.conn_class = pkt->remote_class;
107 #else
108 session.class = pkt->remote_class;
109 #endif /* ProFTPD-1.3.5rc1 and later */
110
111 c = find_config(set, CONF_PARAM, name, FALSE);
112 while (c) {
113 pr_signals_handle();
114
115 #ifdef PR_USE_REGEX
116 if (*((unsigned char *) c->argv[0]) == PR_EXPR_EVAL_REGEX) {
117 pr_regex_t *pre = (pr_regex_t *) c->argv[1];
118
119 if (pkt->remote_class != NULL &&
120 pr_regexp_exec(pre, pkt->remote_class->cls_name, 0, NULL,
121 0, 0, 0) == 0) {
122 ok = TRUE;
123 break;
124 }
125
126 } else
127 #endif /* regex support */
128
129 if (*((unsigned char *) c->argv[0]) == PR_EXPR_EVAL_OR) {
130 ok = pr_expr_eval_class_or((char **) &c->argv[1]);
131 if (ok == TRUE)
132 break;
133
134 } else if (*((unsigned char *) c->argv[0]) == PR_EXPR_EVAL_AND) {
135 ok = pr_expr_eval_class_and((char **) &c->argv[1]);
136 if (ok == TRUE)
137 break;
138 }
139
140 c = find_config_next(c, c->next, CONF_PARAM, name, FALSE);
141 }
142
143 #if PROFTPD_VERSION_NUMBER >= 0x0001030501
144 session.conn_class = NULL;
145 #else
146 session.class = NULL;
147 #endif /* ProFTPD-1.3.5rc1 and later */
148
149 return ok;
150 }
151
152 static int snmp_check_ip_positive(const config_rec *c,
153 struct snmp_packet *pkt) {
154 int aclc;
155 pr_netacl_t **aclv;
156
157 for (aclc = c->argc, aclv = (pr_netacl_t **) c->argv; aclc; aclc--, aclv++) {
158 if (pr_netacl_get_negated(*aclv) == TRUE) {
159 continue;
160 }
161
162 switch (pr_netacl_match(*aclv, pkt->remote_addr)) {
163 case 1:
164 /* Found it! */
165 return TRUE;
166
167 case -1:
168 /* Special value "NONE", meaning nothing can match, so we can
169 * short-circuit on this as well.
170 */
171 return FALSE;
172
173 default:
174 /* No match, keep trying */
175 break;
176 }
177 }
178
179 return FALSE;
180 }
181
182 static int snmp_check_ip_negative(const config_rec *c,
183 struct snmp_packet *pkt) {
184 int aclc;
185 pr_netacl_t **aclv;
186
187 for (aclc = c->argc, aclv = (pr_netacl_t **) c->argv; aclc; aclc--, aclv++) {
188 if (pr_netacl_get_negated(*aclv) == FALSE) {
189 continue;
190 }
191
192 switch (pr_netacl_match(*aclv, pkt->remote_addr)) {
193 case 1:
194 /* This actually means we DID NOT match, and it's ok to short circuit
195 * everything (negative).
196 */
197 return FALSE;
198
199 case -1:
200 /* -1 signifies a NONE match, which isn't valid for negative
201 * conditions.
202 */
203 pr_log_pri(PR_LOG_NOTICE, MOD_SNMP_VERSION
204 ": ooops, it looks like !NONE was used in an ACL somehow");
205 return FALSE;
206
207 default:
208 /* This means our match is actually true and we can continue */
209 break;
210 }
211 }
212
213 /* If we got this far either all conditions were TRUE or there were no
214 * conditions.
215 */
216
217 return TRUE;
218 }
219
220 static int snmp_check_ip_access(xaset_t *set, const char *name,
221 struct snmp_packet *pkt) {
222 config_rec *c;
223 int ok = FALSE;
224
225 c = find_config(set, CONF_PARAM, name, FALSE);
226 while (c) {
227 pr_signals_handle();
228
229 if (snmp_check_ip_negative(c, pkt) != TRUE) {
230 ok = FALSE;
231 break;
232 }
233
234 if (snmp_check_ip_positive(c, pkt) == TRUE) {
235 ok = TRUE;
236 break;
237 }
238
239 c = find_config_next(c, c->next, CONF_PARAM, name, FALSE);
240 }
241
242 return ok;
243 }
244
245 static int snmp_check_allow_limit(config_rec *c, struct snmp_packet *pkt) {
246 unsigned char *allow_all = NULL;
247
248 if (pkt->remote_class != NULL) {
249 if (snmp_check_class_access(c->subset, "AllowClass", pkt)) {
250 return 1;
251 }
252 }
253
254 if (snmp_check_ip_access(c->subset, "Allow", pkt)) {
255 return 1;
256 }
257
258 allow_all = get_param_ptr(c->subset, "AllowAll", FALSE);
259 if (allow_all != NULL &&
260 *allow_all == TRUE) {
261 return 1;
262 }
263
264 return 0;
265 }
266
267 static int snmp_check_deny_limit(config_rec *c, struct snmp_packet *pkt) {
268 unsigned char *deny_all;
269
270 deny_all = get_param_ptr(c->subset, "DenyAll", FALSE);
271 if (deny_all != NULL &&
272 *deny_all == TRUE) {
273 return 1;
274 }
275
276 if (pkt->remote_class != NULL) {
277 if (snmp_check_class_access(c->subset, "DenyClass", pkt)) {
278 return 1;
279 }
280 }
281
282 if (snmp_check_ip_access(c->subset, "Deny", pkt)) {
283 return 1;
284 }
285
286 return 0;
287 }
288
289 static int snmp_check_limit(config_rec *c, struct snmp_packet *pkt) {
290 int *ptr = get_param_ptr(c->subset, "Order", FALSE);
291 int order = ptr ? *ptr : ORDER_ALLOWDENY;
292
293 if (order == ORDER_DENYALLOW) {
294 /* Check deny first */
295
296 if (snmp_check_deny_limit(c, pkt)) {
297 /* Explicit deny */
298 errno = EPERM;
299 return -2;
300 }
301
302 if (snmp_check_allow_limit(c, pkt)) {
303 /* Explicit allow */
304 return 1;
305 }
306
307 /* Implicit deny */
308 errno = EPERM;
309 return -1;
310 }
311
312 /* Check allow first */
313 if (snmp_check_allow_limit(c, pkt)) {
314 /* Explicit allow */
315 return 1;
316 }
317
318 if (snmp_check_deny_limit(c, pkt)) {
319 /* Explicit deny */
320 errno = EPERM;
321 return -2;
322 }
323
324 /* Implicit allow */
325 return 0;
326 }
327
328 /* Similar to the login_check_limits() function from src/dirtree.c, except
329 * that we assume some of the argument values (and thus don't need them
330 * from callers), we don't handle the per-user/group ACLs, and we look
331 * for <Limit SNMP> sections.
332 */
333 static int snmp_limits_allow(xaset_t *set, struct snmp_packet *pkt) {
334 config_rec *c = NULL;
335 int ok = FALSE;
336 int found = 0;
337
338 if (set == NULL ||
339 set->xas_list == NULL) {
340 /* Allow by default */
341 return TRUE;
342 }
343
344 for (c = (config_rec *) set->xas_list; c; c = c->next) {
345 int argc = -1;
346 char **argv = NULL;
347
348 if (c->config_type != CONF_LIMIT) {
349 continue;
350 }
351
352 argc = c->argc;
353 argv = (char **) c->argv;
354
355 for (; argc; argc--, argv++) {
356 if (strncasecmp(*argv, "SNMP", 5) == 0) {
357 break;
358 }
359 }
360
361 if (argc > 0) {
362 switch (snmp_check_limit(c, pkt)) {
363 case 1:
364 ok = TRUE;
365 found++;
366 break;
367
368 case -1:
369 case -2:
370 found++;
371 break;
372 }
373 }
374 }
375
376 if (found == 0) {
377 /* Allow by default. */
378 ok = TRUE;
379 }
380
381 return ok;
382 }
383
384 static int snmp_security_check(struct snmp_packet *pkt) {
385 int res = 0;
386
387 switch (pkt->snmp_version) {
388 case SNMP_PROTOCOL_VERSION_1:
389 case SNMP_PROTOCOL_VERSION_2:
390 /* Check the community string against the configured SNMPCommunity. */
391 if (strncmp(snmp_community, pkt->community, pkt->community_len) != 0) {
392 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
393 "%s message community '%s' does not match configured community, "
394 "ignoring message", snmp_msg_get_versionstr(pkt->snmp_version),
395 pkt->community);
396
397 /* XXX Send authenticationFailure trap to SNMPNotify address */
398
399 res = snmp_db_incr_value(pkt->pool,
400 SNMP_DB_SNMP_F_PKTS_AUTH_ERR_TOTAL, 1);
401 if (res < 0) {
402 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
403 "error incrementing snmp.packetsAuthFailedTotal: %s",
404 strerror(errno));
405 }
406
407 errno = EACCES;
408 return -1;
409 }
410 break;
411
412 case SNMP_PROTOCOL_VERSION_3:
413 /* XXX Not supported yet */
414 errno = ENOSYS;
415 return -1;
416 }
417
418 return res;
419 }
420
421 static int snmp_mkdir(const char *dir, uid_t uid, gid_t gid, mode_t mode) {
422 mode_t prev_mask;
423 struct stat st;
424 int res = -1;
425
426 pr_fs_clear_cache2(dir);
427 res = pr_fsio_stat(dir, &st);
428
429 if (res == -1 &&
430 errno != ENOENT) {
431 return -1;
432 }
433
434 /* The directory already exists. */
435 if (res == 0) {
436 return 0;
437 }
438
439 /* The given mode is absolute, not subject to any Umask setting. */
440 prev_mask = umask(0);
441
442 if (pr_fsio_mkdir(dir, mode) < 0) {
443 int xerrno = errno;
444
445 (void) umask(prev_mask);
446 errno = xerrno;
447 return -1;
448 }
449
450 umask(prev_mask);
451
452 if (pr_fsio_chown(dir, uid, gid) < 0) {
453 return -1;
454 }
455
456 return 0;
457 }
458
459 static int snmp_mkpath(pool *p, const char *path, uid_t uid, gid_t gid,
460 mode_t mode) {
461 char *currpath = NULL, *tmppath = NULL;
462 struct stat st;
463
464 pr_fs_clear_cache2(path);
465 if (pr_fsio_stat(path, &st) == 0) {
466 /* Path already exists, nothing to be done. */
467 errno = EEXIST;
468 return -1;
469 }
470
471 tmppath = pstrdup(p, path);
472
473 currpath = "/";
474 while (tmppath && *tmppath) {
475 char *currdir = strsep(&tmppath, "/");
476 currpath = pdircat(p, currpath, currdir, NULL);
477
478 if (snmp_mkdir(currpath, uid, gid, mode) < 0) {
479 return -1;
480 }
481
482 pr_signals_handle();
483 }
484
485 return 0;
486 }
487
488 static int snmp_openlog(void) {
489 int res = 0;
490 config_rec *c;
491
492 c = find_config(main_server->conf, CONF_PARAM, "SNMPLog", FALSE);
493 if (c) {
494 snmp_logname = c->argv[0];
495
496 if (strncasecmp(snmp_logname, "none", 5) != 0) {
497 int xerrno;
498
499 pr_signals_block();
500 PRIVS_ROOT
501 res = pr_log_openfile(snmp_logname, &snmp_logfd, 0600);
502 xerrno = errno;
503 PRIVS_RELINQUISH
504 pr_signals_unblock();
505
506 if (res < 0) {
507 if (res == -1) {
508 pr_log_pri(PR_LOG_NOTICE, MOD_SNMP_VERSION
509 ": notice: unable to open SNMPLog '%s': %s", snmp_logname,
510 strerror(xerrno));
511
512 } else if (res == PR_LOG_WRITABLE_DIR) {
513 pr_log_pri(PR_LOG_WARNING, MOD_SNMP_VERSION
514 ": notice: unable to open SNMPLog '%s': parent directory is "
515 "world-writable", snmp_logname);
516
517 } else if (res == PR_LOG_SYMLINK) {
518 pr_log_pri(PR_LOG_WARNING, MOD_SNMP_VERSION
519 ": notice: unable to open SNMPLog '%s': cannot log to a symlink",
520 snmp_logname);
521 }
522 }
523 }
524 }
525
526 return res;
527 }
528
529 /* We don't want to do the full daemonize() as provided in main.c; we
530 * already forked.
531 */
532 static void snmp_daemonize(const char *daemon_dir) {
533 #ifndef HAVE_SETSID
534 int tty_fd;
535 #endif
536
537 #ifdef HAVE_SETSID
538 /* setsid() is the preferred way to disassociate from the
539 * controlling terminal
540 */
541 setsid();
542 #else
543 /* Open /dev/tty to access our controlling tty (if any) */
544 tty_fd = open("/dev/tty", O_RDWR);
545 if (tty_fd != -1) {
546 if (ioctl(tty_fd, TIOCNOTTY, NULL) == -1) {
547 perror("ioctl");
548 exit(1);
549 }
550
551 close(tty_fd);
552 }
553 #endif /* HAVE_SETSID */
554
555 /* Close the three big boys. */
556 close(fileno(stdin));
557 close(fileno(stdout));
558 close(fileno(stderr));
559
560 /* Portable way to prevent re-acquiring a tty in the future */
561
562 #ifdef HAVE_SETPGID
563 setpgid(0, getpid());
564
565 #else
566 # ifdef SETPGRP_VOID
567 setpgrp();
568
569 # else
570 setpgrp(0, getpid());
571 # endif /* SETPGRP_VOID */
572 #endif /* HAVE_SETPGID */
573
574 pr_fsio_chdir(daemon_dir, 0);
575 }
576
577 static int snmp_agent_handle_get(struct snmp_packet *pkt) {
578 struct snmp_var *iter_var = NULL, *head_var = NULL, *tail_var = NULL;
579 unsigned int var_count = 0;
580 int res;
581
582 if (pkt->req_pdu->varlist == NULL) {
583 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
584 "missing request PDU variable bindings list, rejecting invalid request");
585 errno = EINVAL;
586 return -1;
587 }
588
589 pkt->resp_pdu = snmp_pdu_dup(pkt->pool, pkt->req_pdu);
590 pkt->resp_pdu->request_type = SNMP_PDU_RESPONSE;
591
592 if (pkt->req_pdu->varlistlen > snmp_max_variables) {
593 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
594 "%s %s of too many OIDs (%u, max %u)",
595 snmp_msg_get_versionstr(pkt->snmp_version),
596 snmp_pdu_get_request_type_desc(pkt->req_pdu->request_type),
597 pkt->req_pdu->varlistlen, snmp_max_variables);
598
599 pkt->resp_pdu->err_code = SNMP_ERR_TOO_BIG;
600 pkt->resp_pdu->err_idx = 0;
601
602 return 0;
603 }
604
605 for (iter_var = pkt->req_pdu->varlist; iter_var; iter_var = iter_var->next) {
606 struct snmp_mib *mib = NULL;
607 struct snmp_var *resp_var = NULL;
608 int32_t mib_int = -1;
609 char *mib_str = NULL;
610 size_t mib_strlen = 0;
611 int lacks_instance_id = FALSE;
612
613 pr_signals_handle();
614
615 mib = snmp_mib_get_by_oid(iter_var->name, iter_var->namelen,
616 &lacks_instance_id);
617 if (mib == NULL) {
618 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
619 "%s %s of unknown OID %s (lacks instance ID = %s)",
620 snmp_msg_get_versionstr(pkt->snmp_version),
621 snmp_pdu_get_request_type_desc(pkt->req_pdu->request_type),
622 snmp_asn1_get_oidstr(pkt->req_pdu->pool, iter_var->name,
623 iter_var->namelen), lacks_instance_id ? "true" : "false");
624
625 /* If SNMPv1, then set the err_code/err_idx values, and duplicate the
626 * varlist.
627 *
628 * If SNMPv2, then leave err_code/err_idex values set to zero, but
629 * create a var of exception 'noSuchObject' or 'noSuchInstance' as
630 * appropriate.
631 */
632
633 switch (pkt->snmp_version) {
634 case SNMP_PROTOCOL_VERSION_1:
635 pkt->resp_pdu->err_code = SNMP_ERR_NO_SUCH_NAME;
636 pkt->resp_pdu->err_idx = var_count + 1;
637 pkt->resp_pdu->varlist = snmp_smi_dup_var(pkt->pool,
638 pkt->req_pdu->varlist);
639 pkt->resp_pdu->varlistlen = pkt->req_pdu->varlistlen;
640 break;
641
642 case SNMP_PROTOCOL_VERSION_2:
643 case SNMP_PROTOCOL_VERSION_3:
644 resp_var = snmp_smi_create_exception(pkt->pool, iter_var->name,
645 iter_var->namelen, lacks_instance_id ? SNMP_SMI_NO_SUCH_INSTANCE :
646 SNMP_SMI_NO_SUCH_OBJECT);
647 break;
648 }
649
650 if (resp_var == NULL) {
651 return 0;
652 }
653 }
654
655 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
656 "%s %s of OID %s (%s)", snmp_msg_get_versionstr(pkt->snmp_version),
657 snmp_pdu_get_request_type_desc(pkt->req_pdu->request_type),
658 snmp_asn1_get_oidstr(iter_var->pool, iter_var->name, iter_var->namelen),
659 mib ? mib->instance_name : "unknown");
660
661 /* A response variable may be have generated above, e.g. when the MIB
662 * not known/supported.
663 */
664 if (resp_var == NULL) {
665 res = snmp_db_get_value(pkt->pool, mib->db_field, &mib_int, &mib_str,
666 &mib_strlen);
667
668 /* XXX Response with genErr instead? */
669 if (res < 0) {
670 int xerrno = errno;
671
672 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
673 "error retrieving database value for field %s: %s",
674 snmp_db_get_fieldstr(pkt->pool, mib->db_field), strerror(xerrno));
675 errno = xerrno;
676 return -1;
677 }
678
679 resp_var = snmp_smi_create_var(pkt->pool, mib->mib_oid, mib->mib_oidlen,
680 mib->smi_type, mib_int, mib_str, mib_strlen);
681 }
682
683 var_count = snmp_smi_util_add_list_var(&head_var, &tail_var, resp_var);
684 }
685
686 pkt->resp_pdu->varlist = head_var;
687 pkt->resp_pdu->varlistlen = var_count;
688
689 return 0;
690 }
691
692 static int snmp_agent_handle_getnext(struct snmp_packet *pkt) {
693 struct snmp_var *iter_var = NULL, *head_var = NULL, *tail_var = NULL;
694 unsigned int var_count = 0;
695 int max_idx, res;
696
697 if (pkt->req_pdu->varlist == NULL) {
698 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
699 "missing request PDU variable bindings list, rejecting invalid request");
700 errno = EINVAL;
701 return -1;
702 }
703
704 pkt->resp_pdu = snmp_pdu_dup(pkt->pool, pkt->req_pdu);
705 pkt->resp_pdu->request_type = SNMP_PDU_RESPONSE;
706
707 if (pkt->req_pdu->varlistlen > snmp_max_variables) {
708 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
709 "%s %s of too many OIDs (%u, max %u)",
710 snmp_msg_get_versionstr(pkt->snmp_version),
711 snmp_pdu_get_request_type_desc(pkt->req_pdu->request_type),
712 pkt->req_pdu->varlistlen, snmp_max_variables);
713
714 pkt->resp_pdu->err_code = SNMP_ERR_TOO_BIG;
715 pkt->resp_pdu->err_idx = 0;
716
717 return 0;
718 }
719
720 max_idx = snmp_mib_get_max_idx();
721
722 for (iter_var = pkt->req_pdu->varlist; iter_var; iter_var = iter_var->next) {
723 struct snmp_mib *mib = NULL;
724 struct snmp_var *resp_var = NULL;
725 int mib_idx = -1, next_idx = -1, lacks_instance_id = FALSE;
726 int32_t mib_int = -1;
727 char *mib_str = NULL;
728 size_t mib_strlen = 0;
729
730 pr_signals_handle();
731
732 mib_idx = snmp_mib_get_idx(iter_var->name, iter_var->namelen,
733 &lacks_instance_id);
734 if (mib_idx < 0) {
735 int unknown_oid = FALSE;
736
737 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
738 "%s %s of unknown OID %s (lacks instance ID = %s)",
739 snmp_msg_get_versionstr(pkt->snmp_version),
740 snmp_pdu_get_request_type_desc(pkt->req_pdu->request_type),
741 snmp_asn1_get_oidstr(pkt->req_pdu->pool, iter_var->name,
742 iter_var->namelen), lacks_instance_id ? "true" : "false");
743
744 if (lacks_instance_id) {
745 oid_t *oid;
746 unsigned int oidlen;
747
748 /* For GetNextRequest-PDUs, a request for "A", without instance
749 * identifier, gets the response of "A.0", since "A" comes before
750 * "A.0". (This does not hold true for GetRequest-PDUs.)
751 */
752
753 oidlen = iter_var->namelen + 1;
754 oid = pcalloc(pkt->pool, oidlen * sizeof(oid_t));
755 memmove(oid, iter_var->name, iter_var->namelen * sizeof(oid_t));
756
757 mib_idx = snmp_mib_get_idx(oid, oidlen, NULL);
758 if (mib_idx < 0) {
759 lacks_instance_id = FALSE;
760 unknown_oid = TRUE;
761
762 } else {
763 mib_idx--;
764 }
765
766 } else {
767 /* Try to find the "nearest" OID. */
768 mib_idx = snmp_mib_get_nearest_idx(iter_var->name, iter_var->namelen);
769 if (mib_idx < 0) {
770 unknown_oid = TRUE;
771
772 } else {
773 mib_idx--;
774 }
775 }
776
777 if (unknown_oid) {
778 /* If SNMPv1, then set the err_code/err_idx values, and duplicate the
779 * varlist.
780 *
781 * If SNMPv2/SNMPv3, then leave err_code/err_idex values set to zero,
782 * but create a var of exception 'noSuchObject' or 'noSuchInstance' as
783 * appropriate.
784 */
785
786 switch (pkt->snmp_version) {
787 case SNMP_PROTOCOL_VERSION_1:
788 pkt->resp_pdu->err_code = SNMP_ERR_NO_SUCH_NAME;
789 pkt->resp_pdu->err_idx = var_count + 1;
790 pkt->resp_pdu->varlist = snmp_smi_dup_var(pkt->pool,
791 pkt->req_pdu->varlist);
792 pkt->resp_pdu->varlistlen = pkt->req_pdu->varlistlen;
793 break;
794
795 case SNMP_PROTOCOL_VERSION_2:
796 case SNMP_PROTOCOL_VERSION_3:
797 resp_var = snmp_smi_create_exception(pkt->pool, iter_var->name,
798 iter_var->namelen, SNMP_SMI_NO_SUCH_OBJECT);
799 break;
800 }
801
802 if (resp_var == NULL) {
803 return 0;
804 }
805 }
806 }
807
808 pr_trace_msg(trace_channel, 19,
809 "%s %s for OID %s at MIB index %d (max index %d)",
810 snmp_msg_get_versionstr(pkt->snmp_version),
811 snmp_pdu_get_request_type_desc(pkt->req_pdu->request_type),
812 snmp_asn1_get_oidstr(pkt->req_pdu->pool, iter_var->name,
813 iter_var->namelen), mib_idx, max_idx);
814
815 next_idx = mib_idx + 1;
816
817 if (next_idx < max_idx) {
818 /* Get the next MIB in the list. Note that we may need to continue
819 * looking for a short while, as some arcs are for notifications only.
820 */
821 mib = snmp_mib_get_by_idx(next_idx);
822 while (mib != NULL &&
823 (mib->mib_enabled == FALSE ||
824 mib->notify_only == TRUE)) {
825 pr_signals_handle();
826
827 if (next_idx > max_idx) {
828 break;
829 }
830
831 mib = snmp_mib_get_by_idx(++next_idx);
832 }
833 }
834
835 if (mib_idx >= max_idx ||
836 next_idx > max_idx) {
837 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
838 "%s %s of last OID %s",
839 snmp_msg_get_versionstr(pkt->snmp_version),
840 snmp_pdu_get_request_type_desc(pkt->req_pdu->request_type),
841 snmp_asn1_get_oidstr(pkt->req_pdu->pool, iter_var->name,
842 iter_var->namelen));
843
844 /* If SNMPv1, then set the err_code/err_idx values, and duplicate the
845 * varlist.
846 *
847 * If SNMPv2/SNMPv3, then leave err_code/err_idex values set to zero, but
848 * create a var of value 'endOfMibView'.
849 */
850
851 switch (pkt->snmp_version) {
852 case SNMP_PROTOCOL_VERSION_1:
853 pkt->resp_pdu->err_code = SNMP_ERR_NO_SUCH_NAME;
854 pkt->resp_pdu->err_idx = var_count + 1;
855 pkt->resp_pdu->varlist = snmp_smi_dup_var(pkt->pool,
856 pkt->req_pdu->varlist);
857 pkt->resp_pdu->varlistlen = pkt->req_pdu->varlistlen;
858 break;
859
860 case SNMP_PROTOCOL_VERSION_2:
861 case SNMP_PROTOCOL_VERSION_3:
862 resp_var = snmp_smi_create_exception(pkt->pool, iter_var->name,
863 iter_var->namelen, SNMP_SMI_END_OF_MIB_VIEW);
864 break;
865 }
866
867 if (resp_var == NULL) {
868 return 0;
869 }
870 }
871
872 if (resp_var == NULL) {
873 /* Get the next MIB in the list. */
874 mib = snmp_mib_get_by_idx(next_idx);
875
876 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
877 "%s %s of OID %s (%s)", snmp_msg_get_versionstr(pkt->snmp_version),
878 snmp_pdu_get_request_type_desc(pkt->req_pdu->request_type),
879 snmp_asn1_get_oidstr(iter_var->pool, mib->mib_oid, mib->mib_oidlen),
880 mib->mib_name);
881
882 res = snmp_db_get_value(pkt->pool, mib->db_field, &mib_int, &mib_str,
883 &mib_strlen);
884
885 /* XXX Response with genErr instead? */
886 if (res < 0) {
887 int xerrno = errno;
888
889 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
890 "error retrieving database value for field %s: %s",
891 snmp_db_get_fieldstr(pkt->pool, mib->db_field), strerror(xerrno));
892 errno = xerrno;
893 return -1;
894 }
895
896 resp_var = snmp_smi_create_var(pkt->pool, mib->mib_oid, mib->mib_oidlen,
897 mib->smi_type, mib_int, mib_str, mib_strlen);
898 }
899
900 var_count = snmp_smi_util_add_list_var(&head_var, &tail_var, resp_var);
901 }
902
903 pkt->resp_pdu->varlist = head_var;
904 pkt->resp_pdu->varlistlen = var_count;
905
906 return 0;
907 }
908
909 static int snmp_agent_handle_getbulk(struct snmp_packet *pkt) {
910 register unsigned int i = 0;
911 struct snmp_var *iter_var = NULL, *head_var = NULL, *tail_var = NULL;
912 unsigned int var_count = 0;
913 int max_idx, res;
914
915 /* SNMPv1 does not support GetBulkRequest PDUs. */
916 if (pkt->snmp_version == SNMP_PROTOCOL_VERSION_1) {
917 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
918 "GetBulkRequest-PDU not supported for %s packets, rejecting "
919 "invalid request", snmp_msg_get_versionstr(pkt->snmp_version));
920 errno = EINVAL;
921 return -1;
922 }
923
924 if (pkt->req_pdu->varlist == NULL) {
925 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
926 "missing request PDU variable bindings list, rejecting invalid request");
927 errno = EINVAL;
928 return -1;
929 }
930
931 /* If non-repeaters is zero, and max-repetitions is zero, treat this as
932 * just another GetNextRequest PDU.
933 */
934 if (pkt->req_pdu->non_repeaters == 0 &&
935 pkt->req_pdu->max_repetitions == 0) {
936 return snmp_agent_handle_getnext(pkt);
937 }
938
939 pkt->resp_pdu = snmp_pdu_dup(pkt->pool, pkt->req_pdu);
940 pkt->resp_pdu->request_type = SNMP_PDU_RESPONSE;
941
942 if (pkt->req_pdu->varlistlen > snmp_max_variables) {
943 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
944 "%s %s of too many OIDs (%u, max %u)",
945 snmp_msg_get_versionstr(pkt->snmp_version),
946 snmp_pdu_get_request_type_desc(pkt->req_pdu->request_type),
947 pkt->req_pdu->varlistlen, snmp_max_variables);
948
949 pkt->resp_pdu->err_code = SNMP_ERR_TOO_BIG;
950 pkt->resp_pdu->err_idx = 0;
951
952 return 0;
953 }
954
955 max_idx = snmp_mib_get_max_idx();
956
957 /* First, deal with the non_repeaters count. This part is just like handling
958 * any other GetNextRequest PDU.
959 */
960 for (i = 0, iter_var = pkt->req_pdu->varlist;
961 i < pkt->req_pdu->non_repeaters && iter_var != NULL;
962 i++, iter_var = iter_var->next) {
963 struct snmp_mib *mib = NULL;
964 struct snmp_var *resp_var = NULL;
965 int mib_idx = -1, lacks_instance_id = FALSE;
966 int32_t mib_int = -1;
967 char *mib_str = NULL;
968 size_t mib_strlen = 0;
969
970 pr_signals_handle();
971
972 mib_idx = snmp_mib_get_idx(iter_var->name, iter_var->namelen,
973 &lacks_instance_id);
974 if (mib_idx < 0) {
975 int unknown_oid = FALSE;
976
977 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
978 "%s %s of unknown OID %s (lacks instance ID = %s)",
979 snmp_msg_get_versionstr(pkt->snmp_version),
980 snmp_pdu_get_request_type_desc(pkt->req_pdu->request_type),
981 snmp_asn1_get_oidstr(pkt->req_pdu->pool, iter_var->name,
982 iter_var->namelen), lacks_instance_id ? "true" : "false");
983
984 if (lacks_instance_id) {
985 oid_t *oid;
986 unsigned int oidlen;
987
988 /* For GetBulkRequest-PDUs, a request for "A", without instance
989 * identifier, gets the response of "A.0", since "A" comes before
990 * "A.0". (This does not hold true for GetRequest-PDUs.)
991 */
992
993 oidlen = iter_var->namelen + 1;
994 oid = pcalloc(pkt->pool, oidlen * sizeof(oid_t));
995 memmove(oid, iter_var->name, iter_var->namelen * sizeof(oid_t));
996
997 mib_idx = snmp_mib_get_idx(oid, oidlen, NULL);
998 if (mib_idx < 0) {
999 lacks_instance_id = FALSE;
1000 unknown_oid = TRUE;
1001
1002 } else {
1003 mib_idx--;
1004 }
1005
1006 } else {
1007 /* Try to find the "nearest" OID. */
1008 mib_idx = snmp_mib_get_nearest_idx(iter_var->name, iter_var->namelen);
1009 if (mib_idx < 0) {
1010 unknown_oid = TRUE;
1011
1012 } else {
1013 mib_idx--;
1014 }
1015 }
1016
1017 if (unknown_oid) {
1018 resp_var = snmp_smi_create_exception(pkt->pool, iter_var->name,
1019 iter_var->namelen, lacks_instance_id ? SNMP_SMI_NO_SUCH_INSTANCE :
1020 SNMP_SMI_NO_SUCH_OBJECT);
1021 }
1022 }
1023
1024 pr_trace_msg(trace_channel, 19,
1025 "%s %s for OID %s at MIB index %d (max index %d)",
1026 snmp_msg_get_versionstr(pkt->snmp_version),
1027 snmp_pdu_get_request_type_desc(pkt->req_pdu->request_type),
1028 snmp_asn1_get_oidstr(pkt->req_pdu->pool, iter_var->name,
1029 iter_var->namelen), mib_idx, max_idx);
1030
1031 if (mib_idx >= max_idx) {
1032 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
1033 "%s %s of last OID %s",
1034 snmp_msg_get_versionstr(pkt->snmp_version),
1035 snmp_pdu_get_request_type_desc(pkt->req_pdu->request_type),
1036 snmp_asn1_get_oidstr(pkt->req_pdu->pool, iter_var->name,
1037 iter_var->namelen));
1038
1039 resp_var = snmp_smi_create_exception(pkt->pool, iter_var->name,
1040 iter_var->namelen, SNMP_SMI_END_OF_MIB_VIEW);
1041 }
1042
1043 if (resp_var == NULL) {
1044 /* Get the next MIB in the list. */
1045 mib = snmp_mib_get_by_idx(mib_idx + 1);
1046
1047 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
1048 "%s %s of OID %s (%s)", snmp_msg_get_versionstr(pkt->snmp_version),
1049 snmp_pdu_get_request_type_desc(pkt->req_pdu->request_type),
1050 snmp_asn1_get_oidstr(iter_var->pool, mib->mib_oid, mib->mib_oidlen),
1051 mib->mib_name);
1052
1053 res = snmp_db_get_value(pkt->pool, mib->db_field, &mib_int, &mib_str,
1054 &mib_strlen);
1055
1056 /* XXX Response with genErr instead? */
1057 if (res < 0) {
1058 int xerrno = errno;
1059
1060 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
1061 "error retrieving database value for field %s: %s",
1062 snmp_db_get_fieldstr(pkt->pool, mib->db_field), strerror(xerrno));
1063 errno = xerrno;
1064 return -1;
1065 }
1066
1067 resp_var = snmp_smi_create_var(pkt->pool, mib->mib_oid, mib->mib_oidlen,
1068 mib->smi_type, mib_int, mib_str, mib_strlen);
1069 }
1070
1071 var_count = snmp_smi_util_add_list_var(&head_var, &tail_var, resp_var);
1072 }
1073
1074 /* Now, deal with the max_repetitions count. Keep in mind the max_variables
1075 * limits.
1076 *
1077 * The iter_var variable should (after the above non_repeaters loop) be
1078 * pointing at the starting variable for us to process in the max_repetitions
1079 * loop.
1080 */
1081 for (; iter_var; iter_var = iter_var->next) {
1082 register unsigned int j;
1083 struct snmp_mib *mib = NULL;
1084 struct snmp_var *resp_var = NULL;
1085 int mib_idx = -1, lacks_instance_id = FALSE;
1086 int32_t mib_int = -1;
1087 char *mib_str = NULL;
1088 size_t mib_strlen = 0;
1089
1090 mib_idx = snmp_mib_get_idx(iter_var->name, iter_var->namelen,
1091 &lacks_instance_id);
1092 if (mib_idx < 0) {
1093 int unknown_oid = FALSE;
1094
1095 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
1096 "%s %s of unknown OID %s (lacks instance ID = %s)",
1097 snmp_msg_get_versionstr(pkt->snmp_version),
1098 snmp_pdu_get_request_type_desc(pkt->req_pdu->request_type),
1099 snmp_asn1_get_oidstr(pkt->req_pdu->pool, iter_var->name,
1100 iter_var->namelen), lacks_instance_id ? "true" : "false");
1101
1102 if (lacks_instance_id) {
1103 oid_t *oid;
1104 unsigned int oidlen;
1105
1106 /* For GetBulkRequest-PDUs, a request for "A", without instance
1107 * identifier, gets the response of "A.0", since "A" comes before
1108 * "A.0". (This does not hold true for GetRequest-PDUs.)
1109 */
1110
1111 oidlen = iter_var->namelen + 1;
1112 oid = pcalloc(pkt->pool, oidlen * sizeof(oid_t));
1113 memmove(oid, iter_var->name, iter_var->namelen * sizeof(oid_t));
1114
1115 mib_idx = snmp_mib_get_idx(oid, oidlen, NULL);
1116 if (mib_idx < 0) {
1117 lacks_instance_id = FALSE;
1118 unknown_oid = TRUE;
1119
1120 } else {
1121 mib_idx--;
1122 }
1123
1124 } else {
1125 /* Try to find the "nearest" OID. */
1126 mib_idx = snmp_mib_get_nearest_idx(iter_var->name, iter_var->namelen);
1127 if (mib_idx < 0) {
1128 unknown_oid = TRUE;
1129
1130 } else {
1131 mib_idx--;
1132 }
1133 }
1134
1135 if (unknown_oid) {
1136 resp_var = snmp_smi_create_exception(pkt->pool, iter_var->name,
1137 iter_var->namelen, lacks_instance_id ? SNMP_SMI_NO_SUCH_INSTANCE :
1138 SNMP_SMI_NO_SUCH_OBJECT);
1139 }
1140 }
1141
1142 if (resp_var == NULL) {
1143 pr_trace_msg(trace_channel, 19,
1144 "%s %s for OID %s at MIB index %d (max index %d)",
1145 snmp_msg_get_versionstr(pkt->snmp_version),
1146 snmp_pdu_get_request_type_desc(pkt->req_pdu->request_type),
1147 snmp_asn1_get_oidstr(pkt->req_pdu->pool, iter_var->name,
1148 iter_var->namelen), mib_idx, max_idx);
1149
1150 if (mib_idx >= max_idx) {
1151 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
1152 "%s %s of last OID %s",
1153 snmp_msg_get_versionstr(pkt->snmp_version),
1154 snmp_pdu_get_request_type_desc(pkt->req_pdu->request_type),
1155 snmp_asn1_get_oidstr(pkt->req_pdu->pool, iter_var->name,
1156 iter_var->namelen));
1157
1158 resp_var = snmp_smi_create_exception(pkt->pool, iter_var->name,
1159 iter_var->namelen, SNMP_SMI_END_OF_MIB_VIEW);
1160 }
1161
1162 if (resp_var == NULL) {
1163 struct snmp_mib *prev_mib = NULL;
1164
1165 for (j = 1; j <= pkt->req_pdu->max_repetitions; j++) {
1166 int next_idx;
1167
1168 pr_signals_handle();
1169
1170 /* Get the next MIB in the list. */
1171 next_idx = mib_idx + j;
1172 if (next_idx < max_idx) {
1173 mib = snmp_mib_get_by_idx(next_idx);
1174 while (mib != NULL &&
1175 (mib->mib_enabled == FALSE ||
1176 mib->notify_only == TRUE)) {
1177 pr_signals_handle();
1178
1179 if (next_idx > max_idx) {
1180 break;
1181 }
1182
1183 mib = snmp_mib_get_by_idx(++next_idx);
1184 }
1185 }
1186
1187 mib = snmp_mib_get_by_idx(next_idx);
1188 if (mib != NULL) {
1189 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
1190 "%s %s of OID %s (%s)",
1191 snmp_msg_get_versionstr(pkt->snmp_version),
1192 snmp_pdu_get_request_type_desc(pkt->req_pdu->request_type),
1193 snmp_asn1_get_oidstr(iter_var->pool, mib->mib_oid,
1194 mib->mib_oidlen), mib->mib_name);
1195
1196 res = snmp_db_get_value(pkt->pool, mib->db_field, &mib_int,
1197 &mib_str, &mib_strlen);
1198
1199 /* XXX Response with genErr instead? */
1200 if (res < 0) {
1201 int xerrno = errno;
1202
1203 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
1204 "error retrieving database value for field %s: %s",
1205 snmp_db_get_fieldstr(pkt->pool, mib->db_field),
1206 strerror(xerrno));
1207 errno = xerrno;
1208 return -1;
1209 }
1210
1211 resp_var = snmp_smi_create_var(pkt->pool, mib->mib_oid,
1212 mib->mib_oidlen, mib->smi_type, mib_int, mib_str, mib_strlen);
1213 prev_mib = mib;
1214
1215 } else {
1216 oid_t *end_oid;
1217 unsigned int end_oidlen;
1218
1219 /* We want to use the OID of the last MIB we processed, or the
1220 * last OID in the request, whichever is present.
1221 */
1222 if (prev_mib != NULL) {
1223 end_oid = prev_mib->mib_oid;
1224 end_oidlen = prev_mib->mib_oidlen;
1225
1226 } else {
1227 end_oid = iter_var->name;
1228 end_oidlen = iter_var->namelen;
1229 }
1230
1231 resp_var = snmp_smi_create_exception(pkt->pool, end_oid,
1232 end_oidlen, SNMP_SMI_END_OF_MIB_VIEW);
1233 var_count = snmp_smi_util_add_list_var(&head_var, &tail_var,
1234 resp_var);
1235 break;
1236 }
1237
1238 var_count = snmp_smi_util_add_list_var(&head_var, &tail_var,
1239 resp_var);
1240 }
1241
1242 } else {
1243 var_count = snmp_smi_util_add_list_var(&head_var, &tail_var, resp_var);
1244 }
1245
1246 } else {
1247 var_count = snmp_smi_util_add_list_var(&head_var, &tail_var, resp_var);
1248 }
1249 }
1250
1251 pkt->resp_pdu->varlist = head_var;
1252 pkt->resp_pdu->varlistlen = var_count;
1253
1254 return 0;
1255 }
1256
1257 static int snmp_agent_handle_set(struct snmp_packet *pkt) {
1258
1259 /* We currently don't support any SET operations. */
1260
1261 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
1262 "%s %s not supported", snmp_msg_get_versionstr(pkt->snmp_version),
1263 snmp_pdu_get_request_type_desc(pkt->req_pdu->request_type));
1264
1265 /* Set the err_code/err_idx values, and duplicate the varlist. The only
1266 * difference is that SNMPv1 gets NO_SUCH_NAME, and SNMPv2/SNMPv3 get
1267 * NO_ACCESS.
1268 */
1269
1270 pkt->resp_pdu = snmp_pdu_dup(pkt->pool, pkt->req_pdu);
1271 pkt->resp_pdu->request_type = SNMP_PDU_RESPONSE;
1272
1273 switch (pkt->snmp_version) {
1274 case SNMP_PROTOCOL_VERSION_1:
1275 pkt->resp_pdu->err_code = SNMP_ERR_NO_SUCH_NAME;
1276 pkt->resp_pdu->err_idx = 1;
1277 pkt->resp_pdu->varlist = snmp_smi_dup_var(pkt->pool,
1278 pkt->req_pdu->varlist);
1279 pkt->resp_pdu->varlistlen = pkt->req_pdu->varlistlen;
1280 break;
1281
1282 case SNMP_PROTOCOL_VERSION_2:
1283 case SNMP_PROTOCOL_VERSION_3:
1284 pkt->resp_pdu->err_code = SNMP_ERR_NO_ACCESS;
1285 pkt->resp_pdu->err_idx = 1;
1286 pkt->resp_pdu->varlist = snmp_smi_dup_var(pkt->pool,
1287 pkt->req_pdu->varlist);
1288 pkt->resp_pdu->varlistlen = pkt->req_pdu->varlistlen;
1289 break;
1290 }
1291
1292 return 0;
1293 }
1294
1295 static int snmp_agent_handle_request(struct snmp_packet *pkt) {
1296 int res;
1297
1298 switch (pkt->req_pdu->request_type) {
1299 case SNMP_PDU_GET:
1300 res = snmp_agent_handle_get(pkt);
1301 break;
1302
1303 case SNMP_PDU_GETNEXT:
1304 res = snmp_agent_handle_getnext(pkt);
1305 break;
1306
1307 case SNMP_PDU_GETBULK:
1308 res = snmp_agent_handle_getbulk(pkt);
1309 break;
1310
1311 case SNMP_PDU_SET:
1312 res = snmp_agent_handle_set(pkt);
1313 break;
1314
1315 default:
1316 errno = EINVAL;
1317 res = -1;
1318 }
1319
1320 return res;
1321 }
1322
1323 static int snmp_agent_handle_packet(int sockfd, pr_netaddr_t *agent_addr) {
1324 int nbytes, res;
1325 struct sockaddr_in from_sockaddr;
1326 socklen_t from_sockaddrlen;
1327 pr_netaddr_t from_addr;
1328 struct snmp_packet *pkt = NULL;
1329
1330 pkt = snmp_packet_create(snmp_pool);
1331
1332 from_sockaddrlen = sizeof(struct sockaddr_in);
1333 nbytes = recvfrom(sockfd, pkt->req_data, pkt->req_datalen, 0,
1334 (struct sockaddr *) &from_sockaddr, &from_sockaddrlen);
1335 if (nbytes < 0) {
1336 int xerrno = errno;
1337
1338 pr_trace_msg(trace_channel, 3,
1339 "error receiving data from socket %d: %s", sockfd, strerror(xerrno));
1340
1341 destroy_pool(pkt->pool);
1342 errno = xerrno;
1343 return -1;
1344 }
1345
1346 pkt->req_datalen = nbytes;
1347
1348 /* XXX Support UDP/IPv6 in the future */
1349
1350 pr_netaddr_clear(&from_addr);
1351 pr_netaddr_set_family(&from_addr, AF_INET);
1352 pr_netaddr_set_sockaddr(&from_addr, (struct sockaddr *) &from_sockaddr);
1353
1354 pkt->remote_addr = &from_addr;
1355
1356 pr_trace_msg(trace_channel, 3,
1357 "read %d UDP bytes from %s#%u", nbytes,
1358 pr_netaddr_get_ipstr(pkt->remote_addr),
1359 ntohs(pr_netaddr_get_port(pkt->remote_addr)));
1360
1361 res = snmp_db_incr_value(pkt->pool, SNMP_DB_SNMP_F_PKTS_RECVD_TOTAL, 1);
1362 if (res < 0) {
1363 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
1364 "error incrementing SNMP database for "
1365 "snmp.packetsReceivedTotal: %s", strerror(errno));
1366 }
1367
1368 pkt->remote_class = pr_class_match_addr(&from_addr);
1369 if (pkt->remote_class != NULL) {
1370 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
1371 "received %d UDP bytes from client in '%s' class", nbytes,
1372 pkt->remote_class->cls_name);
1373
1374 } else {
1375 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
1376 "received %d UDP bytes from client in unknown class", nbytes);
1377 }
1378
1379 /* Check for malicious packets, which forge the from address/port to be
1380 * the same as our listening address/port, trying to induce us to talk
1381 * to ourselves.
1382 */
1383 if (pr_netaddr_cmp(&from_addr, agent_addr) == 0 &&
1384 pr_netaddr_get_port(&from_addr) == pr_netaddr_get_port(agent_addr)) {
1385 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
1386 "rejecting forged UDP packet from %s#%u (appears to be from "
1387 "SNMPAgent %s#%u)",
1388 pr_netaddr_get_ipstr(&from_addr), ntohs(pr_netaddr_get_port(&from_addr)),
1389 pr_netaddr_get_ipstr(agent_addr), ntohs(pr_netaddr_get_port(agent_addr)));
1390
1391 destroy_pool(pkt->pool);
1392 errno = EACCES;
1393 return -1;
1394 }
1395
1396 /* Note: mod_ifsession does NOT affect mod_snmp ACLs; use <Limit SNMP> */
1397
1398 if (snmp_limits_allow(main_server->conf, pkt) == FALSE) {
1399 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
1400 "UDP packet from %s#%u denied by <Limit SNMP> rules",
1401 pr_netaddr_get_ipstr(&from_addr), ntohs(pr_netaddr_get_port(&from_addr)));
1402
1403 destroy_pool(pkt->pool);
1404 errno = EACCES;
1405 return -1;
1406 }
1407
1408 res = snmp_msg_read(pkt->pool, &(pkt->req_data), &(pkt->req_datalen),
1409 &(pkt->community), &(pkt->community_len), &(pkt->snmp_version),
1410 &(pkt->req_pdu));
1411 if (res < 0) {
1412 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
1413 "error reading SNMP message from UDP packet: %s", strerror(errno));
1414
1415 destroy_pool(pkt->pool);
1416 errno = EINVAL;
1417 return -1;
1418 }
1419
1420 /* Check ACLs (community, SNMPv3, etc) */
1421 res = snmp_security_check(pkt);
1422 if (res < 0) {
1423 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
1424 "%s message does not contain correct authentication info, "
1425 "ignoring message", snmp_msg_get_versionstr(pkt->snmp_version));
1426
1427 destroy_pool(pkt->pool);
1428 errno = EINVAL;
1429 return -1;
1430 }
1431
1432 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
1433 "read SNMP message for %s, community = '%s', request ID %ld, "
1434 "request type '%s'", snmp_msg_get_versionstr(pkt->snmp_version),
1435 pkt->community, pkt->req_pdu->request_id,
1436 snmp_pdu_get_request_type_desc(pkt->req_pdu->request_type));
1437
1438 res = snmp_agent_handle_request(pkt);
1439 if (res < 0) {
1440 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
1441 "error handling SNMP message: %s", strerror(errno));
1442 destroy_pool(pkt->pool);
1443 errno = EINVAL;
1444 return -1;
1445 }
1446
1447 /* We're done with the request PDU here. */
1448 destroy_pool(pkt->req_pdu->pool);
1449 pkt->req_pdu = NULL;
1450
1451 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
1452 "writing SNMP message for %s, community = '%s', request ID %ld, "
1453 "request type '%s'", snmp_msg_get_versionstr(pkt->snmp_version),
1454 pkt->community, pkt->resp_pdu->request_id,
1455 snmp_pdu_get_request_type_desc(pkt->resp_pdu->request_type));
1456
1457 res = snmp_msg_write(pkt->pool, &(pkt->resp_data), &(pkt->resp_datalen),
1458 pkt->community, pkt->community_len, pkt->snmp_version, pkt->resp_pdu);
1459 if (res < 0) {
1460 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
1461 "error writing SNMP message to UDP packet: %s", strerror(errno));
1462
1463 destroy_pool(pkt->pool);
1464 errno = EINVAL;
1465 return -1;
1466 }
1467
1468 snmp_packet_write(snmp_pool, sockfd, pkt);
1469
1470 destroy_pool(pkt->pool);
1471 return 0;
1472 }
1473
1474 static int snmp_agent_listen(pr_netaddr_t *agent_addr) {
1475 int family, res, sockfd;
1476
1477 family = pr_netaddr_get_family(agent_addr);
1478 sockfd = socket(family, SOCK_DGRAM, snmp_proto_udp);
1479 if (sockfd < 0) {
1480 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
1481 "unable to create %s UDP socket: %s",
1482 family == AF_INET ? "IPv4" : "IPv6", strerror(errno));
1483 exit(1);
1484 }
1485
1486 res = bind(sockfd, pr_netaddr_get_sockaddr(agent_addr),
1487 pr_netaddr_get_sockaddr_len(agent_addr));
1488 if (res < 0) {
1489 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
1490 "unable to bind %s UDP socket to %s#%u: %s",
1491 family == AF_INET ? "IPv4" : "IPv6",
1492 pr_netaddr_get_ipstr(agent_addr),
1493 ntohs(pr_netaddr_get_port(agent_addr)), strerror(errno));
1494 (void) close(sockfd);
1495 exit(1);
1496
1497 } else {
1498 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
1499 "bound %s UDP socket to %s#%u", family == AF_INET ? "IPv4" : "IPv6",
1500 pr_netaddr_get_ipstr(agent_addr),
1501 ntohs(pr_netaddr_get_port(agent_addr)));
1502 }
1503
1504 return sockfd;
1505 }
1506
1507 static void snmp_agent_loop(array_header *sockfds, array_header *addrs) {
1508 fd_set listen_fds;
1509 struct timeval tv;
1510 int fd;
1511
1512 while (TRUE) {
1513 register unsigned int i;
1514 int maxfd = -1, *fds, res;
1515 pr_netaddr_t **agent_addrs;
1516
1517 /* XXX Is it necessary to even have a timeout? We could simply block
1518 * in select(2) indefinitely, until either an event arrives or we are
1519 * interrupted by a signal.
1520 *
1521 * Yes, we DO need a timeout here, specifically to poll the trap table
1522 * for any trap-generating state. Rather than using a timer and using
1523 * SIGALRM handling, we can reuse this event loop.
1524 */
1525 tv.tv_sec = 60;
1526 tv.tv_usec = 0L;
1527
1528 /* To implement notification criteria/thresholds, we poll for the
1529 * necessary conditions here.
1530 */
1531 snmp_notify_poll_cond();
1532
1533 FD_ZERO(&listen_fds);
1534
1535 fds = sockfds->elts;
1536 agent_addrs = addrs->elts;
1537
1538 for (i = 0; i < sockfds->nelts; i++) {
1539 fd = fds[i];
1540 FD_SET(fd, &listen_fds);
1541
1542 if (fd > maxfd) {
1543 maxfd = fd;
1544 }
1545 }
1546
1547 res = select(maxfd + 1, &listen_fds, NULL, NULL, &tv);
1548 if (res == 0) {
1549 /* Select timeout reached. Just try again. */
1550 continue;
1551 }
1552
1553 if (res < 0) {
1554 if (errno == EINTR) {
1555 pr_signals_handle();
1556 continue;
1557 }
1558
1559 } else {
1560 for (i = 0; i < sockfds->nelts; i++) {
1561 pr_netaddr_t *agent_addr;
1562
1563 fd = fds[i];
1564 agent_addr = agent_addrs[i];
1565
1566 if (FD_ISSET(fd, &listen_fds)) {
1567 res = snmp_agent_handle_packet(fd, agent_addr);
1568 if (res < 0) {
1569 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
1570 "error handling SNMP packet: %s", strerror(errno));
1571 }
1572 }
1573 }
1574 }
1575 }
1576 }
1577
1578 static pid_t snmp_agent_start(const char *tables_dir, int agent_type,
1579 array_header *agent_addrs) {
1580 register unsigned int i;
1581 pid_t agent_pid;
1582 char *agent_chroot = NULL;
1583 rlim_t curr_nproc, max_nproc;
1584 array_header *agent_fds = NULL;
1585
1586 agent_pid = fork();
1587 switch (agent_pid) {
1588 case -1:
1589 pr_log_pri(PR_LOG_ALERT,
1590 MOD_SNMP_VERSION ": unable to fork: %s", strerror(errno));
1591 return 0;
1592
1593 case 0:
1594 /* We're the child. */
1595 break;
1596
1597 default:
1598 /* We're the parent. */
1599 return agent_pid;
1600 }
1601
1602 /* Reset the cached PID, so that it is correctly reflected in the logs. */
1603 session.pid = getpid();
1604
1605 pr_trace_msg("snmp", 3, "forked SNMP agent PID %lu",
1606 (unsigned long) session.pid);
1607
1608 snmp_daemonize(tables_dir);
1609
1610 /* Install our own signal handlers (mostly to ignore signals) */
1611 (void) signal(SIGALRM, SIG_IGN);
1612 (void) signal(SIGHUP, SIG_IGN);
1613 (void) signal(SIGUSR1, SIG_IGN);
1614 (void) signal(SIGUSR2, SIG_IGN);
1615
1616 /* Remove our event listeners. */
1617 pr_event_unregister(&snmp_module, NULL, NULL);
1618
1619 /* XXX Check the agent_type variable, to see if we are a master agent or
1620 * an AgentX sub-agent.
1621 */
1622
1623 for (i = 0; i < agent_addrs->nelts; i++) {
1624 pr_netaddr_t *agent_addr, **addrs;
1625 int agent_fd;
1626
1627 addrs = agent_addrs->elts;
1628 agent_addr = addrs[i];
1629
1630 agent_fd = snmp_agent_listen(agent_addr);
1631 if (agent_fd < 0) {
1632 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
1633 "unable to create listening socket for SNMP agent process: %s",
1634 strerror(errno));
1635 exit(0);
1636 }
1637
1638 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
1639 "SNMP agent process listening on %s UDP %s#%u",
1640 pr_netaddr_get_family(agent_addr) == AF_INET ? "IPv4" : "IPv6",
1641 pr_netaddr_get_ipstr(agent_addr), ntohs(pr_netaddr_get_port(agent_addr)));
1642
1643 if (agent_fds == NULL) {
1644 agent_fds = make_array(snmp_pool, 1, sizeof(int));
1645 }
1646
1647 *((int *) push_array(agent_fds)) = agent_fd;
1648 }
1649
1650 PRIVS_ROOT
1651
1652 if (getuid() == PR_ROOT_UID) {
1653 int res;
1654
1655 /* Chroot to the SNMPTables/empty/ directory before dropping root privs. */
1656
1657 agent_chroot = pdircat(snmp_pool, tables_dir, "empty", NULL);
1658 res = chroot(agent_chroot);
1659 if (res < 0) {
1660 int xerrno = errno;
1661
1662 PRIVS_RELINQUISH
1663
1664 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
1665 "unable to chroot to SNMPTables/empty/ directory '%s': %s",
1666 agent_chroot, strerror(xerrno));
1667 exit(0);
1668 }
1669
1670 if (chdir("/") < 0) {
1671 int xerrno = errno;
1672
1673 PRIVS_RELINQUISH
1674
1675 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
1676 "unable to chdir to root directory within chroot: %s",
1677 strerror(xerrno));
1678 exit(0);
1679 }
1680 }
1681
1682 pr_proctitle_set("(listening for SNMP packets)");
1683
1684 /* Make the SNMP process have the identity of the configured daemon
1685 * User/Group.
1686 */
1687 session.uid = geteuid();
1688 session.gid = getegid();
1689 PRIVS_REVOKE
1690
1691 if (agent_chroot != NULL) {
1692 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
1693 "SNMP agent process running with UID %s, GID %s, restricted to '%s'",
1694 pr_uid2str(snmp_pool, getuid()), pr_gid2str(snmp_pool, getgid()),
1695 agent_chroot);
1696
1697 } else {
1698 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
1699 "SNMP agent process running with UID %s, GID %s, located in '%s'",
1700 pr_uid2str(snmp_pool, getuid()), pr_gid2str(snmp_pool, getgid()),
1701 getcwd(NULL, 0));
1702 }
1703
1704 /* Once we have chrooted, and dropped root privs completely, we can now
1705 * lower our nproc resource limit, so that we cannot fork any new
1706 * processed. We should not be doing so, and we want to mitigate any
1707 * possible exploitation.
1708 */
1709 if (pr_rlimit_get_nproc(&curr_nproc, NULL) == 0) {
1710 /* Override whatever the configured nproc is; we only want 1. */
1711 curr_nproc = 1;
1712
1713 max_nproc = curr_nproc;
1714
1715 if (pr_rlimit_set_nproc(curr_nproc, max_nproc) < 0) {
1716 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
1717 "error setting nproc resource limits to %lu: %s",
1718 (unsigned long) max_nproc, strerror(errno));
1719
1720 } else {
1721 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
1722 "set nproc resource limits to %lu", (unsigned long) max_nproc);
1723 }
1724
1725 } else {
1726 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
1727 "error getting nproc limits: %s", strerror(errno));
1728 }
1729
1730 snmp_agent_loop(agent_fds, agent_addrs);
1731
1732 /* When we are done, we simply exit. */;
1733 pr_trace_msg("snmp", 3, "SNMP agent PID %lu exiting",
1734 (unsigned long) session.pid);
1735 exit(0);
1736 }
1737
1738 static void snmp_agent_stop(pid_t agent_pid) {
1739 int res, status;
1740 time_t start_time = time(NULL);
1741
1742 if (agent_pid == 0) {
1743 /* Nothing to do. */
1744 return;
1745 }
1746
1747 pr_trace_msg("snmp", 3, "stopping agent PID %lu", (unsigned long) agent_pid);
1748
1749 /* Litmus test: is the SNMP agent process still around? If not, there's
1750 * nothing for us to do.
1751 */
1752 res = kill(agent_pid, 0);
1753 if (res < 0 &&
1754 errno == ESRCH) {
1755 return;
1756 }
1757
1758 res = kill(agent_pid, SIGTERM);
1759 if (res < 0) {
1760 int xerrno = errno;
1761
1762 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
1763 "error sending SIGTERM (signal %d) to SNMP agent process ID %lu: %s",
1764 SIGTERM, (unsigned long) agent_pid, strerror(xerrno));
1765 }
1766
1767 /* Poll every 500 millsecs. */
1768 pr_timer_usleep(500 * 1000);
1769
1770 res = waitpid(agent_pid, &status, WNOHANG);
1771 while (res <= 0) {
1772 if (res < 0) {
1773 if (errno == EINTR) {
1774 pr_signals_handle();
1775 continue;
1776 }
1777
1778 if (errno == ECHILD) {
1779 /* XXX Maybe we shouldn't be using waitpid(2) here, since the
1780 * main SIGCHLD handler may handle the termination of the SNMP
1781 * agent process?
1782 */
1783
1784 return;
1785 }
1786
1787 if (errno != EINTR) {
1788 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
1789 "error waiting for SNMP agent process ID %lu: %s",
1790 (unsigned long) agent_pid, strerror(errno));
1791 status = -1;
1792 break;
1793 }
1794 }
1795
1796 /* Check the time elapsed since we started. */
1797 if ((time(NULL) - start_time) > snmp_agent_timeout) {
1798 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
1799 "SNMP agent process ID %lu took longer than timeout (%lu secs) to "
1800 "stop, sending SIGKILL (signal %d)", (unsigned long) agent_pid,
1801 snmp_agent_timeout, SIGKILL);
1802 res = kill(agent_pid, SIGKILL);
1803 if (res < 0) {
1804 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
1805 "error sending SIGKILL (signal %d) to SNMP agent process ID %lu: %s",
1806 SIGKILL, (unsigned long) agent_pid, strerror(errno));
1807 }
1808
1809 break;
1810 }
1811
1812 /* Poll every 500 millsecs. */
1813 pr_timer_usleep(500 * 1000);
1814 }
1815
1816 if (WIFEXITED(status)) {
1817 int exit_status;
1818
1819 exit_status = WEXITSTATUS(status);
1820 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
1821 "SNMP agent process ID %lu terminated normally, with exit status %d",
1822 (unsigned long) agent_pid, exit_status);
1823 }
1824
1825 if (WIFSIGNALED(status)) {
1826 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
1827 "SNMP agent process ID %lu died from signal %d",
1828 (unsigned long) agent_pid, WTERMSIG(status));
1829
1830 if (WCOREDUMP(status)) {
1831 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
1832 "SNMP agent process ID %lu created a coredump",
1833 (unsigned long) agent_pid);
1834 }
1835 }
1836
1837 snmp_agent_pid = 0;
1838 return;
1839 }
1840
1841 /* Configuration handlers
1842 */
1843
1844 /* usage: SNMPAgent "master"|"agentx" address[:port] [...] */
1845 MODRET set_snmpagent(cmd_rec *cmd) {
1846 register unsigned int i;
1847 config_rec *c;
1848 array_header *agent_addrs;
1849 int agent_type;
1850
1851 if (cmd->argc < 2) {
1852 CONF_ERROR(cmd, "wrong number of parameters");
1853 }
1854 CHECK_CONF(cmd, CONF_ROOT);
1855
1856 if (strncasecmp(cmd->argv[1], "master", 7) == 0) {
1857 agent_type = SNMP_AGENT_TYPE_MASTER;
1858
1859 } else if (strncasecmp(cmd->argv[1], "agentx", 7) == 0) {
1860 agent_type = SNMP_AGENT_TYPE_AGENTX;
1861
1862 } else {
1863 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "unsupported SNMP agent type '",
1864 cmd->argv[1], "'", NULL));
1865 }
1866
1867 agent_addrs = make_array(snmp_pool, 1, sizeof(pr_netaddr_t *));
1868
1869 for (i = 2; i < cmd->argc; i++) {
1870 const pr_netaddr_t *agent_addr;
1871 int agent_port = SNMP_DEFAULT_AGENT_PORT;
1872 char *addr = NULL, *ptr;
1873 size_t addrlen;
1874
1875 /* Separate the port out from the address, if present. */
1876 ptr = strrchr(cmd->argv[i], ':');
1877
1878 if (ptr != NULL) {
1879 char *ptr2;
1880
1881 /* We need to handle the following possibilities:
1882 *
1883 * ipv4-addr
1884 * ipv4-addr:port
1885 * [ipv6-addr]
1886 * [ipv6-addr]:port
1887 *
1888 * Thus we check to see if the last ':' occurs before, or after,
1889 * a ']' for an IPv6 address.
1890 */
1891
1892 ptr2 = strrchr(cmd->argv[i], ']');
1893 if (ptr2 != NULL) {
1894 if (ptr2 > ptr) {
1895 /* The found ':' is part of an IPv6 address, not a port delimiter. */
1896 ptr = NULL;
1897 }
1898 }
1899
1900 if (ptr != NULL) {
1901 *ptr = '\0';
1902
1903 agent_port = atoi(ptr + 1);
1904 if (agent_port < 1 ||
1905 agent_port > 65535) {
1906 CONF_ERROR(cmd, "port must be between 1-65535");
1907 }
1908 }
1909 }
1910
1911 addr = cmd->argv[i];
1912 addrlen = strlen(addr);
1913
1914 /* Make sure we can handle an IPv6 address here, e.g.:
1915 *
1916 * [::1]:162
1917 */
1918 if (addrlen > 0 &&
1919 (addr[0] == '[' && addr[addrlen-1] == ']')) {
1920 addr = pstrndup(cmd->pool, addr + 1, addrlen - 2);
1921 }
1922
1923 agent_addr = pr_netaddr_get_addr(snmp_pool, addr, NULL);
1924 if (agent_addr == NULL) {
1925 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "unable to resolve \"", addr, "\"",
1926 NULL));
1927 }
1928
1929 pr_netaddr_set_port((pr_netaddr_t *) agent_addr, htons(agent_port));
1930 *((pr_netaddr_t **) push_array(agent_addrs)) = (pr_netaddr_t *) agent_addr;
1931 }
1932
1933 c = add_config_param(cmd->argv[0], 2, NULL, NULL);
1934 c->argv[0] = palloc(c->pool, sizeof(int));
1935 *((int *) c->argv[0]) = agent_type;
1936 c->argv[1] = agent_addrs;
1937
1938 return PR_HANDLED(cmd);
1939 }
1940
1941 /* usage: SNMPCommunity community */
1942 MODRET set_snmpcommunity(cmd_rec *cmd) {
1943 CHECK_ARGS(cmd, 1);
1944 CHECK_CONF(cmd, CONF_ROOT);
1945
1946 (void) add_config_param_str(cmd->argv[0], 1, cmd->argv[1]);
1947 return PR_HANDLED(cmd);
1948 }
1949
1950 /* usage: SNMPEnable on|off */
1951 MODRET set_snmpenable(cmd_rec *cmd) {
1952 int enabled = -1;
1953 config_rec *c;
1954
1955 CHECK_ARGS(cmd, 1);
1956 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
1957
1958 enabled = get_boolean(cmd, 1);
1959 if (enabled == -1) {
1960 CONF_ERROR(cmd, "expected Boolean parameter");
1961 }
1962
1963 c = add_config_param(cmd->argv[0], 1, NULL);
1964 c->argv[0] = palloc(c->pool, sizeof(int));
1965 *((int *) c->argv[0]) = enabled;
1966
1967 return PR_HANDLED(cmd);
1968 }
1969
1970 /* usage: SNMPEngine on|off */
1971 MODRET set_snmpengine(cmd_rec *cmd) {
1972 int engine = 1;
1973 config_rec *c;
1974
1975 CHECK_ARGS(cmd, 1);
1976 CHECK_CONF(cmd, CONF_ROOT);
1977
1978 engine = get_boolean(cmd, 1);
1979 if (engine == -1) {
1980 CONF_ERROR(cmd, "expected Boolean parameter");
1981 }
1982
1983 c = add_config_param(cmd->argv[0], 1, NULL);
1984 c->argv[0] = pcalloc(c->pool, sizeof(int));
1985 *((int *) c->argv[0]) = engine;
1986
1987 return PR_HANDLED(cmd);
1988 }
1989
1990 /* usage: SNMPLog path|"none" */
1991 MODRET set_snmplog(cmd_rec *cmd) {
1992 CHECK_ARGS(cmd, 1);
1993 CHECK_CONF(cmd, CONF_ROOT);
1994
1995 (void) add_config_param_str(cmd->argv[0], 1, cmd->argv[1]);
1996 return PR_HANDLED(cmd);
1997 }
1998
1999 /* usage: SNMPMaxVariables count */
2000 MODRET set_snmpmaxvariables(cmd_rec *cmd) {
2001 int count = 0;
2002 config_rec *c;
2003
2004 CHECK_ARGS(cmd, 1);
2005 CHECK_CONF(cmd, CONF_ROOT);
2006
2007 count = atoi(cmd->argv[1]);
2008 if (count < 0) {
2009 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "count '", cmd->argv[1],
2010 "' must be greater than zero", NULL));
2011 }
2012
2013 c = add_config_param(cmd->argv[0], 1, NULL);
2014 c->argv[0] = palloc(c->pool, sizeof(unsigned int));
2015 *((unsigned int *) c->argv[0]) = count;
2016
2017 return PR_HANDLED(cmd);
2018 }
2019
2020 /* usage: SNMPNotify address[:port]
2021 *
2022 * XXX In the future, allow specifying of notification types/thresholds
2023 */
2024 MODRET set_snmpnotify(cmd_rec *cmd) {
2025 config_rec *c;
2026 const pr_netaddr_t *notify_addr;
2027 int notify_port = SNMP_DEFAULT_TRAP_PORT;
2028 char *ptr;
2029
2030 if (cmd->argc != 2) {
2031 CONF_ERROR(cmd, "wrong number of parameters");
2032 }
2033
2034 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
2035
2036 /* Separate the port out from the address, if present.
2037 *
2038 * XXX Make sure we can handle an IPv6 address here, e.g.:
2039 *
2040 * [::1]:162
2041 */
2042 ptr = strrchr(cmd->argv[1], ':');
2043 if (ptr != NULL) {
2044 *ptr = '\0';
2045
2046 notify_port = atoi(ptr + 1);
2047 if (notify_port < 1 ||
2048 notify_port > 65535) {
2049 CONF_ERROR(cmd, "port must be between 1-65535");
2050 }
2051 }
2052
2053 c = add_config_param(cmd->argv[0], 1, NULL);
2054
2055 notify_addr = pr_netaddr_get_addr(c->pool, cmd->argv[1], NULL);
2056 if (notify_addr == NULL) {
2057 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "unable to resolve '", cmd->argv[1],
2058 "': ", strerror(errno), NULL));
2059 }
2060
2061 pr_netaddr_set_port((pr_netaddr_t *) notify_addr, htons(notify_port));
2062 c->argv[0] = (void *) notify_addr;
2063
2064 if (pr_module_exists("mod_ifsession.c")) {
2065 /* These are needed in case this directive is used with mod_ifsession
2066 * configuration.
2067 */
2068 c->flags |= CF_MULTI;
2069 }
2070
2071 return PR_HANDLED(cmd);
2072 }
2073
2074 /* usage: SNMPOptions opt1 ... optN */
2075 MODRET set_snmpoptions(cmd_rec *cmd) {
2076 config_rec *c = NULL;
2077 register unsigned int i;
2078 unsigned long opts = 0UL;
2079
2080 if (cmd->argc-1 == 0) {
2081 CONF_ERROR(cmd, "wrong number of parameters");
2082 }
2083
2084 CHECK_CONF(cmd, CONF_ROOT);
2085
2086 /* XXX Implement;
2087 *
2088 * NoSNMPv1
2089 * NoSNMPv2
2090 * NoSMPv3
2091 *
2092 * Describe how these relate to SNMPProtocol; or should these be folded
2093 * into SNMPProtocol, e.g.:
2094 *
2095 * SNMPProtocol 2-3
2096 *
2097 * Default SNMPProtocol would then be "1-3".
2098 */
2099
2100 c = add_config_param(cmd->argv[0], 1, NULL);
2101
2102 for (i = 1; i < cmd->argc; i++) {
2103 if (strcmp(cmd->argv[i], "RestartClearsCounters") == 0) {
2104 opts |= SNMP_OPT_RESTART_CLEARS_COUNTERS;
2105
2106 } else {
2107 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, ": unknown SNMPOption '",
2108 cmd->argv[i], "'", NULL));
2109 }
2110 }
2111
2112 c->argv[0] = pcalloc(c->pool, sizeof(unsigned long));
2113 *((unsigned long *) c->argv[0]) = opts;
2114
2115 if (pr_module_exists("mod_ifsession.c")) {
2116 /* These are needed in case this directive is used with mod_ifsession
2117 * configuration.
2118 */
2119 c->flags |= CF_MULTI;
2120 }
2121
2122 return PR_HANDLED(cmd);
2123 }
2124
2125 /* usage: SNMPTables path */
2126 MODRET set_snmptables(cmd_rec *cmd) {
2127 int res;
2128 struct stat st;
2129 char *path;
2130
2131 CHECK_ARGS(cmd, 1);
2132 CHECK_CONF(cmd, CONF_ROOT);
2133
2134 path = cmd->argv[1];
2135 if (*path != '/') {
2136 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "must be a full path: '", path, "'",
2137 NULL));
2138 }
2139
2140 res = stat(path, &st);
2141 if (res < 0) {
2142 char *agent_chroot;
2143
2144 if (errno != ENOENT) {
2145 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "unable to stat '", path, "': ",
2146 strerror(errno), NULL));
2147 }
2148
2149 pr_log_debug(DEBUG0, MOD_SNMP_VERSION
2150 ": SNMPTables directory '%s' does not exist, creating it", path);
2151
2152 /* Create the directory. */
2153 res = snmp_mkpath(cmd->tmp_pool, path, geteuid(), getegid(), 0755);
2154 if (res < 0) {
2155 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "unable to create directory '",
2156 path, "': ", strerror(errno), NULL));
2157 }
2158
2159 /* Also create the empty/ directory underneath, for the chroot. */
2160 agent_chroot = pdircat(cmd->tmp_pool, path, "empty", NULL);
2161
2162 res = snmp_mkpath(cmd->tmp_pool, agent_chroot, geteuid(), getegid(), 0111);
2163 if (res < 0) {
2164 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "unable to create directory '",
2165 agent_chroot, "': ", strerror(errno), NULL));
2166 }
2167
2168 pr_log_debug(DEBUG2, MOD_SNMP_VERSION
2169 ": created SNMPTables directory '%s'", path);
2170
2171 } else {
2172 char *agent_chroot;
2173
2174 if (!S_ISDIR(st.st_mode)) {
2175 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "unable to use '", path,
2176 ": Not a directory", NULL));
2177 }
2178
2179 /* See if the chroot directory empty/ already exists as well. And enforce
2180 * the permissions on that directory.
2181 */
2182 agent_chroot = pdircat(cmd->tmp_pool, path, "empty", NULL);
2183
2184 res = stat(agent_chroot, &st);
2185 if (res < 0) {
2186 if (errno != ENOENT) {
2187 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "unable to stat '", agent_chroot,
2188 "': ", strerror(errno), NULL));
2189 }
2190
2191 res = snmp_mkpath(cmd->tmp_pool, agent_chroot, geteuid(), getegid(),
2192 0111);
2193 if (res < 0) {
2194 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "unable to create directory '",
2195 agent_chroot, "': ", strerror(errno), NULL));
2196 }
2197
2198 } else {
2199 mode_t dir_mode, expected_mode;
2200
2201 dir_mode = st.st_mode;
2202 dir_mode &= ~S_IFMT;
2203 expected_mode = (S_IXUSR|S_IXGRP|S_IXOTH);
2204
2205 if (dir_mode != expected_mode) {
2206 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "directory '", agent_chroot,
2207 "' has incorrect permissions (not 0111 as required)", NULL));
2208 }
2209 }
2210 }
2211
2212 (void) add_config_param_str(cmd->argv[0], 1, path);
2213 return PR_HANDLED(cmd);
2214 }
2215
2216 /* Command handlers
2217 */
2218
2219 MODRET snmp_pre_list(cmd_rec *cmd) {
2220 const char *proto;
2221 int res;
2222
2223 if (snmp_engine == FALSE) {
2224 return PR_DECLINED(cmd);
2225 }
2226
2227 proto = pr_session_get_protocol(0);
2228 if (strcmp(proto, "ftp") == 0) {
2229 res = snmp_db_incr_value(cmd->tmp_pool, SNMP_DB_FTP_XFERS_F_DIR_LIST_COUNT,
2230 1);
2231 if (res < 0) {
2232 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2233 "error incrementing SNMP database for "
2234 "ftp.dataTransfers.dirListCount: %s", strerror(errno));
2235 }
2236
2237 } else if (strcmp(proto, "ftps") == 0) {
2238 res = snmp_db_incr_value(cmd->tmp_pool, SNMP_DB_FTPS_XFERS_F_DIR_LIST_COUNT,
2239 1);
2240 if (res < 0) {
2241 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2242 "error incrementing SNMP database for "
2243 "ftps.tlsDataTransfers.dirListCount: %s", strerror(errno));
2244 }
2245
2246 } else if (strcmp(proto, "sftp") == 0) {
2247 res = snmp_db_incr_value(cmd->tmp_pool, SNMP_DB_SFTP_XFERS_F_DIR_LIST_COUNT,
2248 1);
2249 if (res < 0) {
2250 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2251 "error incrementing SNMP database for "
2252 "sftp.sftpDataTransfers.dirListCount: %s", strerror(errno));
2253 }
2254 }
2255
2256 return PR_DECLINED(cmd);
2257 }
2258
2259 MODRET snmp_log_list(cmd_rec *cmd) {
2260 const char *proto;
2261 int res;
2262
2263 if (snmp_engine == FALSE) {
2264 return PR_DECLINED(cmd);
2265 }
2266
2267 proto = pr_session_get_protocol(0);
2268 if (strcmp(proto, "ftp") == 0) {
2269 res = snmp_db_incr_value(cmd->tmp_pool, SNMP_DB_FTP_XFERS_F_DIR_LIST_COUNT,
2270 -1);
2271 if (res < 0) {
2272 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2273 "error decrementing SNMP database for "
2274 "ftp.dataTransfers.dirListCount: %s", strerror(errno));
2275 }
2276
2277 res = snmp_db_incr_value(cmd->tmp_pool, SNMP_DB_FTP_XFERS_F_DIR_LIST_TOTAL,
2278 1);
2279 if (res < 0) {
2280 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2281 "error incrementing SNMP database for "
2282 "ftp.dataTransfers.dirListTotal: %s", strerror(errno));
2283 }
2284
2285 } else if (strcmp(proto, "ftps") == 0) {
2286 res = snmp_db_incr_value(cmd->tmp_pool, SNMP_DB_FTPS_XFERS_F_DIR_LIST_COUNT,
2287 -1);
2288 if (res < 0) {
2289 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2290 "error decrementing SNMP database for "
2291 "ftps.tlsDataTransfers.dirListCount: %s", strerror(errno));
2292 }
2293
2294 res = snmp_db_incr_value(cmd->tmp_pool, SNMP_DB_FTPS_XFERS_F_DIR_LIST_TOTAL,
2295 1);
2296 if (res < 0) {
2297 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2298 "error incrementing SNMP database for "
2299 "ftps.tlsDataTransfers.dirListTotal: %s", strerror(errno));
2300 }
2301
2302 } else if (strcmp(proto, "sftp") == 0) {
2303 res = snmp_db_incr_value(cmd->tmp_pool, SNMP_DB_SFTP_XFERS_F_DIR_LIST_COUNT,
2304 -1);
2305 if (res < 0) {
2306 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2307 "error decrementing SNMP database for "
2308 "sftp.sftpDataTransfers.dirListCount: %s", strerror(errno));
2309 }
2310
2311 res = snmp_db_incr_value(cmd->tmp_pool, SNMP_DB_SFTP_XFERS_F_DIR_LIST_TOTAL,
2312 1);
2313 if (res < 0) {
2314 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2315 "error incrementing SNMP database for "
2316 "sftp.sftpDataTransfers.dirListTotal: %s", strerror(errno));
2317 }
2318 }
2319
2320 return PR_DECLINED(cmd);
2321 }
2322
2323 MODRET snmp_err_list(cmd_rec *cmd) {
2324 const char *proto;
2325 int res;
2326
2327 if (snmp_engine == FALSE) {
2328 return PR_DECLINED(cmd);
2329 }
2330
2331 proto = pr_session_get_protocol(0);
2332
2333 if (strcmp(proto, "ftp") == 0) {
2334 res = snmp_db_incr_value(cmd->tmp_pool, SNMP_DB_FTP_XFERS_F_DIR_LIST_COUNT,
2335 -1);
2336 if (res < 0) {
2337 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2338 "error decrementing SNMP database for "
2339 "ftp.dataTransfers.dirListCount: %s", strerror(errno));
2340 }
2341
2342 res = snmp_db_incr_value(cmd->tmp_pool,
2343 SNMP_DB_FTP_XFERS_F_DIR_LIST_ERR_TOTAL, 1);
2344 if (res < 0) {
2345 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2346 "error incrementing SNMP database for "
2347 "ftp.dataTranfers.dirListFailedTotal: %s", strerror(errno));
2348 }
2349
2350 } else if (strcmp(proto, "ftps") == 0) {
2351 res = snmp_db_incr_value(cmd->tmp_pool, SNMP_DB_FTPS_XFERS_F_DIR_LIST_COUNT,
2352 -1);
2353 if (res < 0) {
2354 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2355 "error decrementing SNMP database for "
2356 "ftps.tlsDataTransfers.dirListCount: %s", strerror(errno));
2357 }
2358
2359 res = snmp_db_incr_value(cmd->tmp_pool,
2360 SNMP_DB_FTPS_XFERS_F_DIR_LIST_ERR_TOTAL, 1);
2361 if (res < 0) {
2362 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2363 "error incrementing SNMP database for "
2364 "ftps.tlsDataTranfers.dirListFailedTotal: %s", strerror(errno));
2365 }
2366
2367 } else if (strcmp(proto, "sftp") == 0) {
2368 res = snmp_db_incr_value(cmd->tmp_pool, SNMP_DB_SFTP_XFERS_F_DIR_LIST_COUNT,
2369 -1);
2370 if (res < 0) {
2371 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2372 "error decrementing SNMP database for "
2373 "sftp.sftpDataTransfers.dirListCount: %s", strerror(errno));
2374 }
2375
2376 res = snmp_db_incr_value(cmd->tmp_pool,
2377 SNMP_DB_SFTP_XFERS_F_DIR_LIST_ERR_TOTAL, 1);
2378 if (res < 0) {
2379 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2380 "error incrementing SNMP database for "
2381 "sftp.sftpDataTranfers.dirListFailedTotal: %s", strerror(errno));
2382 }
2383 }
2384
2385 return PR_DECLINED(cmd);
2386 }
2387
2388 MODRET snmp_log_pass(cmd_rec *cmd) {
2389 const char *proto;
2390 int res;
2391
2392 if (snmp_engine == FALSE) {
2393 return PR_DECLINED(cmd);
2394 }
2395
2396 proto = pr_session_get_protocol(0);
2397
2398 if (strcmp(proto, "ftp") == 0) {
2399 res = snmp_db_incr_value(cmd->tmp_pool, SNMP_DB_FTP_SESS_F_SESS_COUNT, 1);
2400 if (res < 0) {
2401 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2402 "error incrementing SNMP database for ftp.sessions.sessionCount: %s",
2403 strerror(errno));
2404 }
2405
2406 res = snmp_db_incr_value(cmd->tmp_pool, SNMP_DB_FTP_SESS_F_SESS_TOTAL, 1);
2407 if (res < 0) {
2408 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2409 "error incrementing SNMP database for ftp.sessions.sessionTotal: %s",
2410 strerror(errno));
2411 }
2412
2413 res = snmp_db_incr_value(cmd->tmp_pool, SNMP_DB_FTP_LOGINS_F_TOTAL, 1);
2414 if (res < 0) {
2415 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2416 "error incrementing SNMP database for ftp.logins.loginsTotal: %s",
2417 strerror(errno));
2418 }
2419
2420 if (session.anon_config != NULL) {
2421 res = snmp_db_incr_value(cmd->tmp_pool, SNMP_DB_FTP_LOGINS_F_ANON_COUNT,
2422 1);
2423 if (res < 0) {
2424 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2425 "error incrementing SNMP database for ftp.logins.anonLoginCount: %s",
2426 strerror(errno));
2427 }
2428
2429 res = snmp_db_incr_value(cmd->tmp_pool, SNMP_DB_FTP_LOGINS_F_ANON_TOTAL,
2430 1);
2431 if (res < 0) {
2432 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2433 "error incrementing SNMP database for ftp.logins.anonLoginTotal: %s",
2434 strerror(errno));
2435 }
2436 }
2437
2438 } else if (strcmp(proto, "ftps") == 0) {
2439 res = snmp_db_incr_value(cmd->tmp_pool, SNMP_DB_FTPS_LOGINS_F_TOTAL, 1);
2440 if (res < 0) {
2441 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2442 "error incrementing SNMP database for ftps.tlsLogins.loginsTotal: %s",
2443 strerror(errno));
2444 }
2445
2446 } else {
2447 /* SSH2 password logins are handled elsewhere. */
2448 }
2449
2450 return PR_DECLINED(cmd);
2451 }
2452
2453 MODRET snmp_err_pass(cmd_rec *cmd) {
2454 const char *proto;
2455 int res;
2456
2457 if (snmp_engine == FALSE) {
2458 return PR_DECLINED(cmd);
2459 }
2460
2461 proto = pr_session_get_protocol(0);
2462
2463 if (strcmp(proto, "ftp") == 0) {
2464 res = snmp_db_incr_value(cmd->tmp_pool, SNMP_DB_FTP_LOGINS_F_ERR_TOTAL, 1);
2465 if (res < 0) {
2466 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2467 "error incrementing SNMP database for ftp.logins.loginFailedTotal: %s",
2468 strerror(errno));
2469 }
2470
2471 } else if (strcmp(proto, "ftps") == 0) {
2472 res = snmp_db_incr_value(cmd->tmp_pool, SNMP_DB_FTPS_LOGINS_F_ERR_TOTAL, 1);
2473 if (res < 0) {
2474 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2475 "error incrementing SNMP database for "
2476 "ftps.tlsLogins.loginFailedTotal: %s", strerror(errno));
2477 }
2478
2479 } else {
2480 /* SSH2 password logins are handled elsewhere. */
2481 }
2482
2483 return PR_DECLINED(cmd);
2484 }
2485
2486 MODRET snmp_pre_retr(cmd_rec *cmd) {
2487 const char *proto;
2488 int res;
2489
2490 if (snmp_engine == FALSE) {
2491 return PR_DECLINED(cmd);
2492 }
2493
2494 proto = pr_session_get_protocol(0);
2495 if (strcmp(proto, "ftp") == 0) {
2496 res = snmp_db_incr_value(cmd->tmp_pool,
2497 SNMP_DB_FTP_XFERS_F_FILE_DOWNLOAD_COUNT, 1);
2498 if (res < 0) {
2499 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2500 "error incrementing SNMP database for "
2501 "ftp.dataTransfers.fileDownloadCount: %s", strerror(errno));
2502 }
2503
2504 } else if (strcmp(proto, "ftps") == 0) {
2505 res = snmp_db_incr_value(cmd->tmp_pool,
2506 SNMP_DB_FTPS_XFERS_F_FILE_DOWNLOAD_COUNT, 1);
2507 if (res < 0) {
2508 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2509 "error incrementing SNMP database for "
2510 "ftps.tlsDataTransfers.fileDownloadCount: %s", strerror(errno));
2511 }
2512
2513 } else if (strcmp(proto, "sftp") == 0) {
2514 res = snmp_db_incr_value(cmd->tmp_pool,
2515 SNMP_DB_SFTP_XFERS_F_FILE_DOWNLOAD_COUNT, 1);
2516 if (res < 0) {
2517 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2518 "error incrementing SNMP database for "
2519 "sftp.sftpDataTransfers.fileDownloadCount: %s", strerror(errno));
2520 }
2521
2522 } else if (strcmp(proto, "scp") == 0) {
2523 res = snmp_db_incr_value(cmd->tmp_pool,
2524 SNMP_DB_SCP_XFERS_F_FILE_DOWNLOAD_COUNT, 1);
2525 if (res < 0) {
2526 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2527 "error incrementing SNMP database for "
2528 "scp.scpDataTransfers.fileDownloadCount: %s", strerror(errno));
2529 }
2530 }
2531
2532 return PR_DECLINED(cmd);
2533 }
2534
2535 MODRET snmp_log_retr(cmd_rec *cmd) {
2536 const char *proto;
2537 uint32_t retr_kb;
2538 off_t rem_bytes;
2539 int res;
2540
2541 if (snmp_engine == FALSE) {
2542 return PR_DECLINED(cmd);
2543 }
2544
2545 proto = pr_session_get_protocol(0);
2546
2547 if (strcmp(proto, "ftp") == 0) {
2548 res = snmp_db_incr_value(cmd->tmp_pool,
2549 SNMP_DB_FTP_XFERS_F_FILE_DOWNLOAD_COUNT, -1);
2550 if (res < 0) {
2551 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2552 "error decrementing SNMP database for "
2553 "ftp.dataTransfers.fileDownloadCount: %s", strerror(errno));
2554 }
2555
2556 res = snmp_db_incr_value(cmd->tmp_pool,
2557 SNMP_DB_FTP_XFERS_F_FILE_DOWNLOAD_TOTAL, 1);
2558 if (res < 0) {
2559 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2560 "error incrementing SNMP database for "
2561 "ftp.dataTransfers.fileDownloadTotal: %s", strerror(errno));
2562 }
2563
2564 /* We also need to increment the KB download count. We know the number
2565 * of bytes downloaded as an off_t here, but we only store the number of KB
2566 * in the mod_snmp db tables.
2567 *
2568 * We could just increment by xfer_bytes / 1024, but that would mean that
2569 * several small files of say 999 bytes could be downloaded, and the KB
2570 * count would not be incremented.
2571 *
2572 * To deal with this situation, we use the snmp_retr_bytes static variable
2573 * as a "holding bucket" of bytes, from which we get the KB to add to the
2574 * db tables.
2575 */
2576 snmp_retr_bytes += session.xfer.total_bytes;
2577
2578 retr_kb = (snmp_retr_bytes / 1024);
2579 rem_bytes = (snmp_retr_bytes % 1024);
2580
2581 res = snmp_db_incr_value(cmd->tmp_pool,
2582 SNMP_DB_FTP_XFERS_F_KB_DOWNLOAD_TOTAL, retr_kb);
2583 if (res < 0) {
2584 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2585 "error incrementing SNMP database for "
2586 "ftp.dataTransfers.kbDownloadTotal: %s", strerror(errno));
2587 }
2588
2589 snmp_retr_bytes = rem_bytes;
2590
2591 } else if (strcmp(proto, "ftps") == 0) {
2592 res = snmp_db_incr_value(cmd->tmp_pool,
2593 SNMP_DB_FTPS_XFERS_F_FILE_DOWNLOAD_COUNT, -1);
2594 if (res < 0) {
2595 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2596 "error decrementing SNMP database for "
2597 "ftps.tlsDataTransfers.fileDownloadCount: %s", strerror(errno));
2598 }
2599
2600 res = snmp_db_incr_value(cmd->tmp_pool,
2601 SNMP_DB_FTPS_XFERS_F_FILE_DOWNLOAD_TOTAL, 1);
2602 if (res < 0) {
2603 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2604 "error incrementing SNMP database for "
2605 "ftps.tlsDataTransfers.fileDownloadTotal: %s", strerror(errno));
2606 }
2607
2608 /* We also need to increment the KB download count. We know the number
2609 * of bytes downloaded as an off_t here, but we only store the number of KB
2610 * in the mod_snmp db tables.
2611 *
2612 * We could just increment by xfer_bytes / 1024, but that would mean that
2613 * several small files of say 999 bytes could be downloaded, and the KB
2614 * count would not be incremented.
2615 *
2616 * To deal with this situation, we use the snmp_retr_bytes static variable
2617 * as a "holding bucket" of bytes, from which we get the KB to add to the
2618 * db tables.
2619 */
2620 snmp_retr_bytes += session.xfer.total_bytes;
2621
2622 retr_kb = (snmp_retr_bytes / 1024);
2623 rem_bytes = (snmp_retr_bytes % 1024);
2624
2625 res = snmp_db_incr_value(cmd->tmp_pool,
2626 SNMP_DB_FTPS_XFERS_F_KB_DOWNLOAD_TOTAL, retr_kb);
2627 if (res < 0) {
2628 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2629 "error incrementing SNMP database for "
2630 "ftps.tlsDataTransfers.kbDownloadTotal: %s", strerror(errno));
2631 }
2632
2633 snmp_retr_bytes = rem_bytes;
2634
2635 } else if (strcmp(proto, "sftp") == 0) {
2636 res = snmp_db_incr_value(cmd->tmp_pool,
2637 SNMP_DB_SFTP_XFERS_F_FILE_DOWNLOAD_COUNT, -1);
2638 if (res < 0) {
2639 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2640 "error decrementing SNMP database for "
2641 "sftp.sftpDataTransfers.fileDownloadCount: %s", strerror(errno));
2642 }
2643
2644 res = snmp_db_incr_value(cmd->tmp_pool,
2645 SNMP_DB_SFTP_XFERS_F_FILE_DOWNLOAD_TOTAL, 1);
2646 if (res < 0) {
2647 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2648 "error incrementing SNMP database for "
2649 "sftp.sftpDataTransfers.fileDownloadTotal: %s", strerror(errno));
2650 }
2651
2652 /* We also need to increment the KB download count. We know the number
2653 * of bytes downloaded as an off_t here, but we only store the number of KB
2654 * in the mod_snmp db tables.
2655 *
2656 * We could just increment by xfer_bytes / 1024, but that would mean that
2657 * several small files of say 999 bytes could be downloaded, and the KB
2658 * count would not be incremented.
2659 *
2660 * To deal with this situation, we use the snmp_retr_bytes static variable
2661 * as a "holding bucket" of bytes, from which we get the KB to add to the
2662 * db tables.
2663 */
2664 snmp_retr_bytes += session.xfer.total_bytes;
2665
2666 retr_kb = (snmp_retr_bytes / 1024);
2667 rem_bytes = (snmp_retr_bytes % 1024);
2668
2669 res = snmp_db_incr_value(cmd->tmp_pool,
2670 SNMP_DB_SFTP_XFERS_F_KB_DOWNLOAD_TOTAL, retr_kb);
2671 if (res < 0) {
2672 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2673 "error incrementing SNMP database for "
2674 "sftp.sftpDataTransfers.kbDownloadTotal: %s", strerror(errno));
2675 }
2676
2677 snmp_retr_bytes = rem_bytes;
2678
2679 } else if (strcmp(proto, "scp") == 0) {
2680 res = snmp_db_incr_value(cmd->tmp_pool,
2681 SNMP_DB_SCP_XFERS_F_FILE_DOWNLOAD_COUNT, -1);
2682 if (res < 0) {
2683 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2684 "error decrementing SNMP database for "
2685 "scp.scpDataTransfers.fileDownloadCount: %s", strerror(errno));
2686 }
2687
2688 res = snmp_db_incr_value(cmd->tmp_pool,
2689 SNMP_DB_SCP_XFERS_F_FILE_DOWNLOAD_TOTAL, 1);
2690 if (res < 0) {
2691 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2692 "error incrementing SNMP database for "
2693 "scp.scpDataTransfers.fileDownloadTotal: %s", strerror(errno));
2694 }
2695
2696 /* We also need to increment the KB download count. We know the number
2697 * of bytes downloaded as an off_t here, but we only store the number of KB
2698 * in the mod_snmp db tables.
2699 *
2700 * We could just increment by xfer_bytes / 1024, but that would mean that
2701 * several small files of say 999 bytes could be downloaded, and the KB
2702 * count would not be incremented.
2703 *
2704 * To deal with this situation, we use the snmp_retr_bytes static variable
2705 * as a "holding bucket" of bytes, from which we get the KB to add to the
2706 * db tables.
2707 */
2708 snmp_retr_bytes += session.xfer.total_bytes;
2709
2710 retr_kb = (snmp_retr_bytes / 1024);
2711 rem_bytes = (snmp_retr_bytes % 1024);
2712
2713 res = snmp_db_incr_value(cmd->tmp_pool,
2714 SNMP_DB_SCP_XFERS_F_KB_DOWNLOAD_TOTAL, retr_kb);
2715 if (res < 0) {
2716 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2717 "error incrementing SNMP database for "
2718 "scp.scpDataTransfers.kbDownloadTotal: %s", strerror(errno));
2719 }
2720
2721 snmp_retr_bytes = rem_bytes;
2722 }
2723
2724 return PR_DECLINED(cmd);
2725 }
2726
2727 MODRET snmp_err_retr(cmd_rec *cmd) {
2728 const char *proto;
2729 int res;
2730
2731 if (snmp_engine == FALSE) {
2732 return PR_DECLINED(cmd);
2733 }
2734
2735 proto = pr_session_get_protocol(0);
2736
2737 if (strcmp(proto, "ftp") == 0) {
2738 res = snmp_db_incr_value(cmd->tmp_pool,
2739 SNMP_DB_FTP_XFERS_F_FILE_DOWNLOAD_COUNT, -1);
2740 if (res < 0) {
2741 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2742 "error decrementing SNMP database for "
2743 "ftp.dataTransfers.fileDownloadCount: %s", strerror(errno));
2744 }
2745
2746 res = snmp_db_incr_value(cmd->tmp_pool,
2747 SNMP_DB_FTP_XFERS_F_FILE_DOWNLOAD_ERR_TOTAL, 1);
2748 if (res < 0) {
2749 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2750 "error incrementing SNMP database for "
2751 "ftp.dataTransfers.fileDownloadFailedTotal: %s", strerror(errno));
2752 }
2753
2754 } else if (strcmp(proto, "ftps") == 0) {
2755 res = snmp_db_incr_value(cmd->tmp_pool,
2756 SNMP_DB_FTPS_XFERS_F_FILE_DOWNLOAD_COUNT, -1);
2757 if (res < 0) {
2758 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2759 "error decrementing SNMP database for "
2760 "ftps.tlsDataTransfers.fileDownloadCount: %s", strerror(errno));
2761 }
2762
2763 res = snmp_db_incr_value(cmd->tmp_pool,
2764 SNMP_DB_FTPS_XFERS_F_FILE_DOWNLOAD_ERR_TOTAL, 1);
2765 if (res < 0) {
2766 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2767 "error incrementing SNMP database for "
2768 "ftps.tlsDataTransfers.fileDownloadFailedTotal: %s", strerror(errno));
2769 }
2770
2771 } else if (strcmp(proto, "sftp") == 0) {
2772 res = snmp_db_incr_value(cmd->tmp_pool,
2773 SNMP_DB_SFTP_XFERS_F_FILE_DOWNLOAD_COUNT, -1);
2774 if (res < 0) {
2775 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2776 "error decrementing SNMP database for "
2777 "sftp.sftpDataTransfers.fileDownloadCount: %s", strerror(errno));
2778 }
2779
2780 res = snmp_db_incr_value(cmd->tmp_pool,
2781 SNMP_DB_SFTP_XFERS_F_FILE_DOWNLOAD_ERR_TOTAL, 1);
2782 if (res < 0) {
2783 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2784 "error incrementing SNMP database for "
2785 "sftp.sftpDataTransfers.fileDownloadFailedTotal: %s", strerror(errno));
2786 }
2787
2788 } else if (strcmp(proto, "scp") == 0) {
2789 res = snmp_db_incr_value(cmd->tmp_pool,
2790 SNMP_DB_SCP_XFERS_F_FILE_DOWNLOAD_COUNT, -1);
2791 if (res < 0) {
2792 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2793 "error decrementing SNMP database for "
2794 "scp.scpDataTransfers.fileDownloadCount: %s", strerror(errno));
2795 }
2796
2797 res = snmp_db_incr_value(cmd->tmp_pool,
2798 SNMP_DB_SCP_XFERS_F_FILE_DOWNLOAD_ERR_TOTAL, 1);
2799 if (res < 0) {
2800 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2801 "error incrementing SNMP database for "
2802 "scp.scpDataTransfers.fileDownloadFailedTotal: %s", strerror(errno));
2803 }
2804 }
2805
2806 return PR_DECLINED(cmd);
2807 }
2808
2809 MODRET snmp_pre_stor(cmd_rec *cmd) {
2810 const char *proto;
2811 int res;
2812
2813 if (snmp_engine == FALSE) {
2814 return PR_DECLINED(cmd);
2815 }
2816
2817 proto = pr_session_get_protocol(0);
2818 if (strcmp(proto, "ftp") == 0) {
2819 res = snmp_db_incr_value(cmd->tmp_pool,
2820 SNMP_DB_FTP_XFERS_F_FILE_UPLOAD_COUNT, 1);
2821 if (res < 0) {
2822 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2823 "error incrementing SNMP database for "
2824 "ftp.dataTransfers.fileUploadCount: %s", strerror(errno));
2825 }
2826
2827 } else if (strcmp(proto, "ftps") == 0) {
2828 res = snmp_db_incr_value(cmd->tmp_pool,
2829 SNMP_DB_FTPS_XFERS_F_FILE_UPLOAD_COUNT, 1);
2830 if (res < 0) {
2831 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2832 "error incrementing SNMP database for "
2833 "ftps.tlsDataTransfers.fileUploadCount: %s", strerror(errno));
2834 }
2835
2836 } else if (strcmp(proto, "sftp") == 0) {
2837 res = snmp_db_incr_value(cmd->tmp_pool,
2838 SNMP_DB_SFTP_XFERS_F_FILE_UPLOAD_COUNT, 1);
2839 if (res < 0) {
2840 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2841 "error incrementing SNMP database for "
2842 "sftp.sftpDataTransfers.fileUploadCount: %s", strerror(errno));
2843 }
2844
2845 } else if (strcmp(proto, "scp") == 0) {
2846 res = snmp_db_incr_value(cmd->tmp_pool,
2847 SNMP_DB_SCP_XFERS_F_FILE_UPLOAD_COUNT, 1);
2848 if (res < 0) {
2849 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2850 "error incrementing SNMP database for "
2851 "scp.scpDataTransfers.fileUploadCount: %s", strerror(errno));
2852 }
2853 }
2854
2855 return PR_DECLINED(cmd);
2856 }
2857
2858 MODRET snmp_log_stor(cmd_rec *cmd) {
2859 const char *proto;
2860 uint32_t stor_kb;
2861 off_t rem_bytes;
2862 int res;
2863
2864 if (snmp_engine == FALSE) {
2865 return PR_DECLINED(cmd);
2866 }
2867
2868 proto = pr_session_get_protocol(0);
2869
2870 if (strcmp(proto, "ftp") == 0) {
2871 res = snmp_db_incr_value(cmd->tmp_pool,
2872 SNMP_DB_FTP_XFERS_F_FILE_UPLOAD_COUNT, -1);
2873 if (res < 0) {
2874 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2875 "error decrementing SNMP database for "
2876 "ftp.dataTransfers.fileUploadCount: %s", strerror(errno));
2877 }
2878
2879 res = snmp_db_incr_value(cmd->tmp_pool,
2880 SNMP_DB_FTP_XFERS_F_FILE_UPLOAD_TOTAL, 1);
2881 if (res < 0) {
2882 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2883 "error incrementing SNMP database for "
2884 "ftp.dataTransfers.fileUploadTotal: %s", strerror(errno));
2885 }
2886
2887 /* We also need to increment the KB upload count. We know the number
2888 * of bytes downloaded as an off_t here, but we only store the number of KB
2889 * in the mod_snmp db tables.
2890 *
2891 * We could just increment by xfer_bytes / 1024, but that would mean that
2892 * several small files of say 999 bytes could be uploaded, and the KB
2893 * count would not be incremented.
2894 *
2895 * To deal with this situation, we use the snmp_stor_bytes static variable
2896 * as a "holding bucket" of bytes, from which we get the KB to add to the
2897 * db tables.
2898 */
2899 snmp_stor_bytes += session.xfer.total_bytes;
2900
2901 stor_kb = (snmp_stor_bytes / 1024);
2902 rem_bytes = (snmp_stor_bytes % 1024);
2903
2904 res = snmp_db_incr_value(cmd->tmp_pool,
2905 SNMP_DB_FTP_XFERS_F_KB_UPLOAD_TOTAL, stor_kb);
2906 if (res < 0) {
2907 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2908 "error incrementing SNMP database for "
2909 "ftp.dataTransfers.kbUploadTotal: %s", strerror(errno));
2910 }
2911
2912 snmp_stor_bytes = rem_bytes;
2913
2914 } else if (strcmp(proto, "ftps") == 0) {
2915 res = snmp_db_incr_value(cmd->tmp_pool,
2916 SNMP_DB_FTPS_XFERS_F_FILE_UPLOAD_COUNT, -1);
2917 if (res < 0) {
2918 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2919 "error decrementing SNMP database for "
2920 "ftps.tlsDataTransfers.fileUploadCount: %s", strerror(errno));
2921 }
2922
2923 res = snmp_db_incr_value(cmd->tmp_pool,
2924 SNMP_DB_FTPS_XFERS_F_FILE_UPLOAD_TOTAL, 1);
2925 if (res < 0) {
2926 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2927 "error incrementing SNMP database for "
2928 "ftps.tlsDataTransfers.fileUploadTotal: %s", strerror(errno));
2929 }
2930
2931 /* We also need to increment the KB upload count. We know the number
2932 * of bytes downloaded as an off_t here, but we only store the number of KB
2933 * in the mod_snmp db tables.
2934 *
2935 * We could just increment by xfer_bytes / 1024, but that would mean that
2936 * several small files of say 999 bytes could be uploaded, and the KB
2937 * count would not be incremented.
2938 *
2939 * To deal with this situation, we use the snmp_stor_bytes static variable
2940 * as a "holding bucket" of bytes, from which we get the KB to add to the
2941 * db tables.
2942 */
2943 snmp_stor_bytes += session.xfer.total_bytes;
2944
2945 stor_kb = (snmp_stor_bytes / 1024);
2946 rem_bytes = (snmp_stor_bytes % 1024);
2947
2948 res = snmp_db_incr_value(cmd->tmp_pool,
2949 SNMP_DB_FTPS_XFERS_F_KB_UPLOAD_TOTAL, stor_kb);
2950 if (res < 0) {
2951 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2952 "error incrementing SNMP database for "
2953 "ftps.tlsDataTransfers.kbUploadTotal: %s", strerror(errno));
2954 }
2955
2956 snmp_stor_bytes = rem_bytes;
2957
2958 } else if (strcmp(proto, "sftp") == 0) {
2959 res = snmp_db_incr_value(cmd->tmp_pool,
2960 SNMP_DB_SFTP_XFERS_F_FILE_UPLOAD_COUNT, -1);
2961 if (res < 0) {
2962 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2963 "error decrementing SNMP database for "
2964 "sftp.sftpDataTransfers.fileUploadCount: %s", strerror(errno));
2965 }
2966
2967 res = snmp_db_incr_value(cmd->tmp_pool,
2968 SNMP_DB_SFTP_XFERS_F_FILE_UPLOAD_TOTAL, 1);
2969 if (res < 0) {
2970 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2971 "error incrementing SNMP database for "
2972 "sftp.sftpDataTransfers.fileUploadTotal: %s", strerror(errno));
2973 }
2974
2975 /* We also need to increment the KB upload count. We know the number
2976 * of bytes downloaded as an off_t here, but we only store the number of KB
2977 * in the mod_snmp db tables.
2978 *
2979 * We could just increment by xfer_bytes / 1024, but that would mean that
2980 * several small files of say 999 bytes could be uploaded, and the KB
2981 * count would not be incremented.
2982 *
2983 * To deal with this situation, we use the snmp_stor_bytes static variable
2984 * as a "holding bucket" of bytes, from which we get the KB to add to the
2985 * db tables.
2986 */
2987 snmp_stor_bytes += session.xfer.total_bytes;
2988
2989 stor_kb = (snmp_stor_bytes / 1024);
2990 rem_bytes = (snmp_stor_bytes % 1024);
2991
2992 res = snmp_db_incr_value(cmd->tmp_pool,
2993 SNMP_DB_SFTP_XFERS_F_KB_UPLOAD_TOTAL, stor_kb);
2994 if (res < 0) {
2995 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
2996 "error incrementing SNMP database for "
2997 "sftp.sftpDataTransfers.kbUploadTotal: %s", strerror(errno));
2998 }
2999
3000 snmp_stor_bytes = rem_bytes;
3001
3002 } else if (strcmp(proto, "scp") == 0) {
3003 res = snmp_db_incr_value(cmd->tmp_pool,
3004 SNMP_DB_SCP_XFERS_F_FILE_UPLOAD_COUNT, -1);
3005 if (res < 0) {
3006 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
3007 "error decrementing SNMP database for "
3008 "scp.scpDataTransfers.fileUploadCount: %s", strerror(errno));
3009 }
3010
3011 res = snmp_db_incr_value(cmd->tmp_pool,
3012 SNMP_DB_SCP_XFERS_F_FILE_UPLOAD_TOTAL, 1);
3013 if (res < 0) {
3014 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
3015 "error incrementing SNMP database for "
3016 "scp.scpDataTransfers.fileUploadTotal: %s", strerror(errno));
3017 }
3018
3019 /* We also need to increment the KB upload count. We know the number
3020 * of bytes downloaded as an off_t here, but we only store the number of KB
3021 * in the mod_snmp db tables.
3022 *
3023 * We could just increment by xfer_bytes / 1024, but that would mean that
3024 * several small files of say 999 bytes could be uploaded, and the KB
3025 * count would not be incremented.
3026 *
3027 * To deal with this situation, we use the snmp_stor_bytes static variable
3028 * as a "holding bucket" of bytes, from which we get the KB to add to the
3029 * db tables.
3030 */
3031 snmp_stor_bytes += session.xfer.total_bytes;
3032
3033 stor_kb = (snmp_stor_bytes / 1024);
3034 rem_bytes = (snmp_stor_bytes % 1024);
3035
3036 res = snmp_db_incr_value(cmd->tmp_pool,
3037 SNMP_DB_SCP_XFERS_F_KB_UPLOAD_TOTAL, stor_kb);
3038 if (res < 0) {
3039 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
3040 "error incrementing SNMP database for "
3041 "scp.scpDataTransfers.kbUploadTotal: %s", strerror(errno));
3042 }
3043
3044 snmp_stor_bytes = rem_bytes;
3045 }
3046
3047 return PR_DECLINED(cmd);
3048 }
3049
3050 MODRET snmp_err_stor(cmd_rec *cmd) {
3051 const char *proto;
3052 int res;
3053
3054 if (snmp_engine == FALSE) {
3055 return PR_DECLINED(cmd);
3056 }
3057
3058 proto = pr_session_get_protocol(0);
3059
3060 if (strcmp(proto, "ftp") == 0) {
3061 res = snmp_db_incr_value(cmd->tmp_pool,
3062 SNMP_DB_FTP_XFERS_F_FILE_UPLOAD_COUNT, -1);
3063 if (res < 0) {
3064 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
3065 "error decrementing SNMP database for "
3066 "ftp.dataTransfers.fileUploadCount: %s", strerror(errno));
3067 }
3068
3069 res = snmp_db_incr_value(cmd->tmp_pool,
3070 SNMP_DB_FTP_XFERS_F_FILE_UPLOAD_ERR_TOTAL, 1);
3071 if (res < 0) {
3072 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
3073 "error incrementing SNMP database for "
3074 "ftp.dataTransfers.fileUploadFailedTotal: %s", strerror(errno));
3075 }
3076
3077 } else if (strcmp(proto, "ftps") == 0) {
3078 res = snmp_db_incr_value(cmd->tmp_pool,
3079 SNMP_DB_FTPS_XFERS_F_FILE_UPLOAD_COUNT, -1);
3080 if (res < 0) {
3081 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
3082 "error decrementing SNMP database for "
3083 "ftps.tlsDataTransfers.fileUploadCount: %s", strerror(errno));
3084 }
3085
3086 res = snmp_db_incr_value(cmd->tmp_pool,
3087 SNMP_DB_FTPS_XFERS_F_FILE_UPLOAD_ERR_TOTAL, 1);
3088 if (res < 0) {
3089 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
3090 "error incrementing SNMP database for "
3091 "ftps.tlsDataTransfers.fileUploadFailedTotal: %s", strerror(errno));
3092 }
3093
3094 } else if (strcmp(proto, "sftp") == 0) {
3095 res = snmp_db_incr_value(cmd->tmp_pool,
3096 SNMP_DB_SFTP_XFERS_F_FILE_UPLOAD_COUNT, -1);
3097 if (res < 0) {
3098 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
3099 "error decrementing SNMP database for "
3100 "sftp.sftpDataTransfers.fileUploadCount: %s", strerror(errno));
3101 }
3102
3103 res = snmp_db_incr_value(cmd->tmp_pool,
3104 SNMP_DB_SFTP_XFERS_F_FILE_UPLOAD_ERR_TOTAL, 1);
3105 if (res < 0) {
3106 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
3107 "error incrementing SNMP database for "
3108 "sftp.sftpDataTransfers.fileUploadFailedTotal: %s", strerror(errno));
3109 }
3110
3111 } else if (strcmp(proto, "scp") == 0) {
3112 res = snmp_db_incr_value(cmd->tmp_pool,
3113 SNMP_DB_SCP_XFERS_F_FILE_UPLOAD_COUNT, -1);
3114 if (res < 0) {
3115 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
3116 "error decrementing SNMP database for "
3117 "scp.scpDataTransfers.fileUploadCount: %s", strerror(errno));
3118 }
3119
3120 res = snmp_db_incr_value(cmd->tmp_pool,
3121 SNMP_DB_SCP_XFERS_F_FILE_UPLOAD_ERR_TOTAL, 1);
3122 if (res < 0) {
3123 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
3124 "error incrementing SNMP database for "
3125 "scp.scpDataTransfers.fileUploadFailedTotal: %s", strerror(errno));
3126 }
3127 }
3128
3129 return PR_DECLINED(cmd);
3130 }
3131
3132 MODRET snmp_log_auth(cmd_rec *cmd) {
3133 const char *proto;
3134
3135 if (snmp_engine == FALSE) {
3136 return PR_DECLINED(cmd);
3137 }
3138
3139 /* Note: we are not currently properly incrementing
3140 * SNMP_DB_FTPS_SESS_F_SESS_COUNT and SNMP_DB_FTPS_SESS_F_SESS_TOTAL
3141 * for FTPS connections accepted using the UseImplicitSSL TLSOption.
3142 *
3143 * The issue is that for those connections, the protocol will be set to
3144 * "ftps" in mod_tls' sess_init callback. But here in mod_snmp, we
3145 * are not guaranteed to being called AFTER mod_tls, due to module load
3146 * ordering. Thus we do not have a good way of determining when to
3147 * increment those counts for implicit FTPS connections.
3148 */
3149
3150 proto = pr_session_get_protocol(0);
3151 if (strcmp(proto, "ftps") == 0) {
3152 int res;
3153
3154 res = snmp_db_incr_value(cmd->tmp_pool, SNMP_DB_FTPS_SESS_F_SESS_COUNT, 1);
3155 if (res < 0) {
3156 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
3157 "error incrementing SNMP database for "
3158 "ftps.tlsSessions.sessionCount: %s", strerror(errno));
3159 }
3160
3161 res = snmp_db_incr_value(cmd->tmp_pool, SNMP_DB_FTPS_SESS_F_SESS_TOTAL, 1);
3162 if (res < 0) {
3163 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
3164 "error incrementing SNMP database for "
3165 "ftps.tlsSessions.sessionTotal: %s", strerror(errno));
3166 }
3167
3168 } else {
3169 /* XXX Some other RFC2228 mechanism (e.g. mod_gss) */
3170 }
3171
3172 return PR_DECLINED(cmd);
3173 }
3174
3175 MODRET snmp_log_ccc(cmd_rec *cmd) {
3176 const char *proto;
3177 int res;
3178
3179 if (snmp_engine == FALSE) {
3180 return PR_DECLINED(cmd);
3181 }
3182
3183 proto = pr_session_get_protocol(0);
3184 if (strcmp(proto, "ftps") == 0) {
3185 res = snmp_db_incr_value(cmd->tmp_pool, SNMP_DB_FTPS_SESS_F_CCC_TOTAL, 1);
3186 if (res < 0) {
3187 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
3188 "error incrementing SNMP database for "
3189 "ftps.tlsSessions.clearCommandChannelTotal: %s", strerror(errno));
3190 }
3191 }
3192
3193 return PR_DECLINED(cmd);
3194 }
3195
3196 MODRET snmp_err_ccc(cmd_rec *cmd) {
3197 const char *proto;
3198 int res;
3199
3200 if (snmp_engine == FALSE) {
3201 return PR_DECLINED(cmd);
3202 }
3203
3204 proto = pr_session_get_protocol(0);
3205 if (strcmp(proto, "ftps") == 0) {
3206 res = snmp_db_incr_value(cmd->tmp_pool, SNMP_DB_FTPS_SESS_F_CCC_ERR_TOTAL,
3207 1);
3208 if (res < 0) {
3209 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
3210 "error incrementing SNMP database for "
3211 "ftps.tlsSessions.clearCommandChannelFailedTotal: %s", strerror(errno));
3212 }
3213 }
3214
3215 return PR_DECLINED(cmd);
3216 }
3217
3218 /* Event handlers
3219 */
3220
3221 static void ev_incr_value(unsigned int field_id, const char *field_str,
3222 int32_t incr) {
3223 int res;
3224 pool *p;
3225
3226 p = session.pool;
3227 if (p == NULL) {
3228 p = snmp_pool;
3229 }
3230
3231 res = snmp_db_incr_value(p, field_id, incr);
3232 if (res < 0) {
3233 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
3234 "error %s SNMP database for %s: %s",
3235 incr < 0 ? "decrementing" : "incrementing", field_str, strerror(errno));
3236 }
3237 }
3238
3239 static void snmp_auth_code_ev(const void *event_data, void *user_data) {
3240 int auth_code, res;
3241 unsigned int field_id = SNMP_DB_ID_UNKNOWN, is_ftps = FALSE, notify_id = 0;
3242 const char *notify_str = NULL, *proto;
3243
3244 if (snmp_engine == FALSE) {
3245 return;
3246 }
3247
3248 auth_code = *((int *) event_data);
3249
3250 /* Any notifications we generate here may depend on the protocol in use. */
3251 proto = pr_session_get_protocol(0);
3252
3253 if (strcmp(proto, "ftps") == 0) {
3254 is_ftps = TRUE;
3255 }
3256
3257 switch (auth_code) {
3258 case PR_AUTH_RFC2228_OK:
3259 if (is_ftps == TRUE) {
3260 field_id = SNMP_DB_FTPS_LOGINS_F_CERT_TOTAL;
3261 }
3262 break;
3263
3264 case PR_AUTH_NOPWD:
3265 if (is_ftps == FALSE) {
3266 field_id = SNMP_DB_FTP_LOGINS_F_ERR_BAD_USER_TOTAL;
3267
3268 } else {
3269 field_id = SNMP_DB_FTPS_LOGINS_F_ERR_BAD_USER_TOTAL;
3270 }
3271
3272 notify_id = SNMP_NOTIFY_FTP_BAD_USER;
3273 notify_str = "loginFailedBadUser";
3274 break;
3275
3276 case PR_AUTH_BADPWD:
3277 if (is_ftps == FALSE) {
3278 field_id = SNMP_DB_FTP_LOGINS_F_ERR_BAD_PASSWD_TOTAL;
3279
3280 } else {
3281 field_id = SNMP_DB_FTPS_LOGINS_F_ERR_BAD_PASSWD_TOTAL;
3282 }
3283
3284 notify_id = SNMP_NOTIFY_FTP_BAD_PASSWD;
3285 notify_str = "loginFailedBadPassword";
3286 break;
3287
3288 default:
3289 if (is_ftps == FALSE) {
3290 field_id = SNMP_DB_FTP_LOGINS_F_ERR_GENERAL_TOTAL;
3291
3292 } else {
3293 field_id = SNMP_DB_FTPS_LOGINS_F_ERR_GENERAL_TOTAL;
3294 }
3295
3296 break;
3297 }
3298
3299 if (auth_code >= 0) {
3300 ev_incr_value(field_id, "login total", 1);
3301
3302 /* We only send notifications for failed authentications. */
3303 return;
3304
3305 } else {
3306 ev_incr_value(field_id, "login failure total", 1);
3307 }
3308
3309 if (notify_id > 0 &&
3310 snmp_notifys != NULL) {
3311 register unsigned int i;
3312 pr_netaddr_t **dst_addrs;
3313
3314 dst_addrs = snmp_notifys->elts;
3315 for (i = 0; i < snmp_notifys->nelts; i++) {
3316 res = snmp_notify_generate(snmp_pool, -1, snmp_community,
3317 session.c->local_addr, dst_addrs[i], notify_id);
3318 if (res < 0) {
3319 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
3320 "unable to send %s notification to SNMPNotify %s:%d: %s", notify_str,
3321 pr_netaddr_get_ipstr(dst_addrs[i]),
3322 ntohs(pr_netaddr_get_port(dst_addrs[i])), strerror(errno));
3323 }
3324 }
3325 }
3326 }
3327
3328 static void snmp_cmd_invalid_ev(const void *event_data, void *user_data) {
3329 if (snmp_engine == FALSE) {
3330 return;
3331 }
3332
3333 ev_incr_value(SNMP_DB_FTP_SESS_F_CMD_INVALID_TOTAL,
3334 "ftp.connections.commandInvalidTotal", 1);
3335 }
3336
3337 static void snmp_exit_ev(const void *event_data, void *user_data) {
3338 if (snmp_engine == FALSE) {
3339 return;
3340 }
3341
3342 ev_incr_value(SNMP_DB_DAEMON_F_CONN_COUNT, "daemon.connectionCount", -1);
3343
3344 switch (session.disconnect_reason) {
3345 case PR_SESS_DISCONNECT_BANNED:
3346 case PR_SESS_DISCONNECT_CONFIG_ACL:
3347 case PR_SESS_DISCONNECT_MODULE_ACL:
3348 case PR_SESS_DISCONNECT_SESSION_INIT_FAILED:
3349 ev_incr_value(SNMP_DB_DAEMON_F_CONN_REFUSED_TOTAL,
3350 "daemon.connectionRefusedTotal", 1);
3351 break;
3352
3353 case PR_SESS_DISCONNECT_SEGFAULT:
3354 ev_incr_value(SNMP_DB_DAEMON_F_SEGFAULT_COUNT,
3355 "daemon.segfaultCount", 1);
3356 break;
3357
3358 default: {
3359 const char *proto;
3360
3361 proto = pr_session_get_protocol(0);
3362
3363 if (strcmp(proto, "ftp") == 0) {
3364 ev_incr_value(SNMP_DB_FTP_SESS_F_SESS_COUNT,
3365 "ftp.sessions.sessionCount", -1);
3366
3367 if (session.anon_config != NULL) {
3368 ev_incr_value(SNMP_DB_FTP_LOGINS_F_ANON_COUNT,
3369 "ftp.logins.anonLoginCount", -1);
3370 }
3371
3372 } else if (strcmp(proto, "ftps") == 0) {
3373 ev_incr_value(SNMP_DB_FTPS_SESS_F_SESS_COUNT,
3374 "ftps.tlsSessions.sessionCount", -1);
3375
3376 } else {
3377 /* XXX ssh2/sftp/scp session end */
3378 }
3379
3380 break;
3381 }
3382 }
3383
3384 if (snmp_logfd >= 0) {
3385 (void) close(snmp_logfd);
3386 snmp_logfd = -1;
3387 }
3388 }
3389
3390 static void snmp_max_inst_ev(const void *event_data, void *user_data) {
3391 if (snmp_engine == FALSE) {
3392 return;
3393 }
3394
3395 ev_incr_value(SNMP_DB_DAEMON_F_MAXINST_TOTAL,
3396 "daemon.maxInstancesLimitTotal", 1);
3397
3398 if (snmp_notifys != NULL) {
3399 register unsigned int i;
3400 pr_netaddr_t **dst_addrs;
3401 unsigned int notify_id = SNMP_NOTIFY_DAEMON_MAX_INSTANCES;
3402
3403 dst_addrs = snmp_notifys->elts;
3404 for (i = 0; i < snmp_notifys->nelts; i++) {
3405 int res;
3406
3407 res = snmp_notify_generate(snmp_pool, -1, snmp_community,
3408 session.c->local_addr, dst_addrs[i], notify_id);
3409 if (res < 0) {
3410 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
3411 "unable to send daemonMaxInstancesExceeded notification to "
3412 "SNMPNotify %s:%d: %s", pr_netaddr_get_ipstr(dst_addrs[i]),
3413 ntohs(pr_netaddr_get_port(dst_addrs[i])), strerror(errno));
3414 }
3415 }
3416 }
3417 }
3418
3419 #if defined(PR_SHARED_MODULE)
3420 static void snmp_mod_unload_ev(const void *event_data, void *user_data) {
3421 if (strcmp((const char *) event_data, "mod_snmp.c") == 0) {
3422 register unsigned int i;
3423
3424 /* Unregister ourselves from all events. */
3425 pr_event_unregister(&snmp_module, NULL, NULL);
3426
3427 for (i = 0; snmp_table_ids[i] > 0; i++) {
3428 snmp_db_close(snmp_pool, snmp_table_ids[i]);
3429 }
3430
3431 destroy_pool(snmp_pool);
3432 snmp_pool = NULL;
3433
3434 (void) close(snmp_logfd);
3435 snmp_logfd = -1;
3436 }
3437 }
3438 #endif
3439
3440 static void snmp_postparse_ev(const void *event_data, void *user_data) {
3441 register unsigned int i;
3442 config_rec *c;
3443 server_rec *s;
3444 unsigned int nvhosts = 0;
3445 const char *tables_dir;
3446 int agent_type, res;
3447 array_header *agent_addrs;
3448 unsigned char ban_loaded = FALSE, sftp_loaded = FALSE, tls_loaded = FALSE;
3449
3450 c = find_config(main_server->conf, CONF_PARAM, "SNMPEngine", FALSE);
3451 if (c) {
3452 snmp_engine = *((int *) c->argv[0]);
3453 }
3454
3455 if (snmp_engine == FALSE) {
3456 return;
3457 }
3458
3459 snmp_openlog();
3460
3461 c = find_config(main_server->conf, CONF_PARAM, "SNMPOptions", FALSE);
3462 while (c != NULL) {
3463 unsigned long opts = 0;
3464
3465 pr_signals_handle();
3466
3467 opts = *((unsigned long *) c->argv[0]);
3468 snmp_opts |= opts;
3469
3470 c = find_config_next(c, c->next, CONF_PARAM, "SNMPOptions", FALSE);
3471 }
3472
3473 c = find_config(main_server->conf, CONF_PARAM, "SNMPCommunity", FALSE);
3474 if (c == NULL) {
3475 /* No SNMPCommunity configured, mod_snmp cannot authenticate messages
3476 * properly.
3477 */
3478 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
3479 "no SNMPCommunity configured, disabling module");
3480
3481 snmp_engine = FALSE;
3482 return;
3483 }
3484
3485 snmp_community = c->argv[0];
3486
3487 c = find_config(main_server->conf, CONF_PARAM, "SNMPMaxVariables", FALSE);
3488 if (c != NULL) {
3489 snmp_max_variables = *((unsigned int *) c->argv[0]);
3490 }
3491
3492 c = find_config(main_server->conf, CONF_PARAM, "SNMPTables", FALSE);
3493 if (c == NULL) {
3494 /* No SNMPTables configured, mod_snmp cannot run. */
3495 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
3496 "no SNMPTables configured, disabling module");
3497
3498 snmp_engine = FALSE;
3499 return;
3500 }
3501
3502 tables_dir = c->argv[0];
3503
3504 if (snmp_db_set_root(tables_dir) < 0) {
3505 /* Unable to configure the SNMPTables root for some reason... */
3506
3507 snmp_engine = FALSE;
3508 return;
3509 }
3510
3511 /* Create the variable database table files, based on the configured
3512 * SNMPTables path.
3513 */
3514 tls_loaded = pr_module_exists("mod_tls.c");
3515 sftp_loaded = pr_module_exists("mod_sftp.c");
3516 ban_loaded = pr_module_exists("mod_ban.c");
3517
3518 for (i = 0; snmp_table_ids[i] > 0; i++) {
3519 int skip_table = FALSE;
3520
3521 switch (snmp_table_ids[i]) {
3522 case SNMP_DB_ID_TLS:
3523 if (tls_loaded == FALSE) {
3524 skip_table = TRUE;
3525 }
3526 break;
3527
3528 case SNMP_DB_ID_SSH:
3529 case SNMP_DB_ID_SFTP:
3530 case SNMP_DB_ID_SCP:
3531 if (sftp_loaded == FALSE) {
3532 skip_table = TRUE;
3533 }
3534 break;
3535
3536 case SNMP_DB_ID_BAN:
3537 if (ban_loaded == FALSE) {
3538 skip_table = TRUE;
3539 }
3540 break;
3541
3542 default:
3543 break;
3544 }
3545
3546 if (skip_table) {
3547 continue;
3548 }
3549
3550 res = snmp_db_open(snmp_pool, snmp_table_ids[i]);
3551 if (res < 0) {
3552 register unsigned int j;
3553
3554 /* If we fail to open this table, BUT have succeeded in opening previous
3555 * tables, AND we are just going to return here, then we need to make
3556 * sure to close the previously opened tables.
3557 */
3558 for (j = 0; snmp_table_ids[j] > 0 && j < i; j++) {
3559 (void) snmp_db_close(snmp_pool, snmp_table_ids[j]);
3560 }
3561
3562 snmp_engine = FALSE;
3563 return;
3564 }
3565 }
3566
3567 /* Initial the MIBs. */
3568 snmp_mib_init();
3569
3570 /* Iterate through the server_list, and count up the number of vhosts. */
3571 for (s = (server_rec *) server_list->xas_list; s; s = s->next) {
3572 nvhosts++;
3573 }
3574
3575 ev_incr_value(SNMP_DB_DAEMON_F_VHOST_COUNT, "daemon.vhostCount", nvhosts);
3576
3577 c = find_config(main_server->conf, CONF_PARAM, "SNMPAgent", FALSE);
3578 if (c == NULL) {
3579 snmp_engine = FALSE;
3580 pr_log_debug(DEBUG0, MOD_SNMP_VERSION
3581 ": missing required SNMPAgent directive, disabling module");
3582
3583 /* Need to close database tables here. */
3584 for (i = 0; snmp_table_ids[i] > 0; i++) {
3585 (void) snmp_db_close(snmp_pool, snmp_table_ids[i]);
3586 }
3587
3588 return;
3589 }
3590
3591 agent_type = *((int *) c->argv[0]);
3592 agent_addrs = c->argv[1];
3593
3594 snmp_agent_pid = snmp_agent_start(tables_dir, agent_type, agent_addrs);
3595 if (snmp_agent_pid == 0) {
3596 snmp_engine = FALSE;
3597 pr_log_debug(DEBUG0, MOD_SNMP_VERSION
3598 ": failed to start agent listening process, disabling module");
3599
3600 /* Need to close database tables here. */
3601 for (i = 0; snmp_table_ids[i] > 0; i++) {
3602 (void) snmp_db_close(snmp_pool, snmp_table_ids[i]);
3603 }
3604 }
3605
3606 return;
3607 }
3608
3609 static void snmp_restart_ev(const void *event_data, void *user_data) {
3610 if (snmp_engine == FALSE) {
3611 return;
3612 }
3613
3614 ev_incr_value(SNMP_DB_DAEMON_F_RESTART_COUNT, "daemon.restartCount", 1);
3615
3616 if (snmp_opts & SNMP_OPT_RESTART_CLEARS_COUNTERS) {
3617 int res;
3618
3619 pr_trace_msg(trace_channel, 17,
3620 "restart event received, resetting counters");
3621 res = snmp_mib_reset_counters();
3622 if (res < 0) {
3623 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
3624 "error resetting SNMP database counters: %s", strerror(errno));
3625 }
3626 }
3627
3628 snmp_agent_stop(snmp_agent_pid);
3629
3630 /* Close the SNMPLog file descriptor; it will be reopened in the
3631 * postparse event listener.
3632 */
3633 (void) close(snmp_logfd);
3634 snmp_logfd = -1;
3635 }
3636
3637 static void snmp_shutdown_ev(const void *event_data, void *user_data) {
3638 register unsigned int i;
3639
3640 snmp_agent_stop(snmp_agent_pid);
3641
3642 for (i = 0; snmp_table_ids[i] > 0; i++) {
3643 snmp_db_close(snmp_pool, snmp_table_ids[i]);
3644 }
3645
3646 destroy_pool(snmp_pool);
3647 snmp_pool = NULL;
3648
3649 (void) close(snmp_logfd);
3650 snmp_logfd = -1;
3651 }
3652
3653 static void snmp_startup_ev(const void *event_data, void *user_data) {
3654 if (snmp_engine == FALSE) {
3655 return;
3656 }
3657
3658 if (ServerType == SERVER_INETD) {
3659 snmp_engine = FALSE;
3660 pr_log_debug(DEBUG0, MOD_SNMP_VERSION
3661 ": cannot support SNMP for ServerType inetd, disabling module");
3662 return;
3663 }
3664
3665 gettimeofday(&snmp_start_tv, NULL);
3666 return;
3667 }
3668
3669 static void snmp_timeout_idle_ev(const void *event_data, void *user_data) {
3670 if (snmp_engine == FALSE) {
3671 return;
3672 }
3673
3674 ev_incr_value(SNMP_DB_TIMEOUTS_F_IDLE_TOTAL,
3675 "timeouts.idleTimeoutTotal", 1);
3676 }
3677
3678 static void snmp_timeout_login_ev(const void *event_data, void *user_data) {
3679 if (snmp_engine == FALSE) {
3680 return;
3681 }
3682
3683 ev_incr_value(SNMP_DB_TIMEOUTS_F_LOGIN_TOTAL,
3684 "timeouts.loginTimeoutTotal", 1);
3685 }
3686
3687 static void snmp_timeout_noxfer_ev(const void *event_data, void *user_data) {
3688 if (snmp_engine == FALSE) {
3689 return;
3690 }
3691
3692 ev_incr_value(SNMP_DB_TIMEOUTS_F_NOXFER_TOTAL,
3693 "timeouts.noTransferTimeoutTotal", 1);
3694 }
3695
3696 static void snmp_timeout_stalled_ev(const void *event_data, void *user_data) {
3697 if (snmp_engine == FALSE) {
3698 return;
3699 }
3700
3701 ev_incr_value(SNMP_DB_TIMEOUTS_F_STALLED_TOTAL,
3702 "timeouts.stalledTimeoutTotal", 1);
3703 }
3704
3705 /* mod_tls-generated events */
3706 static void snmp_tls_ctrl_handshake_err_ev(const void *event_data,
3707 void *user_data) {
3708 if (snmp_engine == FALSE) {
3709 return;
3710 }
3711
3712 ev_incr_value(SNMP_DB_FTPS_SESS_F_CTRL_HANDSHAKE_ERR_TOTAL,
3713 "ftps.tlsSessions.ctrlHandshakeFailedTotal", 1);
3714 }
3715
3716 static void snmp_tls_data_handshake_err_ev(const void *event_data,
3717 void *user_data) {
3718 if (snmp_engine == FALSE) {
3719 return;
3720 }
3721
3722 ev_incr_value(SNMP_DB_FTPS_SESS_F_DATA_HANDSHAKE_ERR_TOTAL,
3723 "ftps.tlsSessions.dataHandshakeFailedTotal", 1);
3724 }
3725
3726 static void snmp_tls_verify_client_ev(const void *event_data, void *user_data) {
3727 if (snmp_engine == FALSE) {
3728 return;
3729 }
3730
3731 ev_incr_value(SNMP_DB_FTPS_SESS_F_VERIFY_CLIENT_TOTAL,
3732 "ftps.tlsSessions.verifyClientTotal", 1);
3733 }
3734
3735 static void snmp_tls_verify_client_err_ev(const void *event_data,
3736 void *user_data) {
3737 if (snmp_engine == FALSE) {
3738 return;
3739 }
3740
3741 ev_incr_value(SNMP_DB_FTPS_SESS_F_VERIFY_CLIENT_ERR_TOTAL,
3742 "ftps.tlsSessions.verifyClientFailedTotal", 1);
3743 }
3744
3745 /* mod_sftp-generated events */
3746 static void snmp_ssh2_kex_err_ev(const void *event_data, void *user_data) {
3747 if (snmp_engine == FALSE) {
3748 return;
3749 }
3750
3751 ev_incr_value(SNMP_DB_SSH_SESS_F_KEX_ERR_TOTAL,
3752 "ssh.sshSessions.keyExchangeFailedTotal", 1);
3753 }
3754
3755 static void snmp_ssh2_c2s_compress_ev(const void *event_data, void *user_data) {
3756 if (snmp_engine == FALSE) {
3757 return;
3758 }
3759
3760 ev_incr_value(SNMP_DB_SSH_SESS_F_C2S_COMPRESS_TOTAL,
3761 "ssh.sshSessions.clientCompressionTotal", 1);
3762 }
3763
3764 static void snmp_ssh2_s2c_compress_ev(const void *event_data, void *user_data) {
3765 if (snmp_engine == FALSE) {
3766 return;
3767 }
3768
3769 ev_incr_value(SNMP_DB_SSH_SESS_F_S2C_COMPRESS_TOTAL,
3770 "ssh.sshSessions.serverCompressionTotal", 1);
3771 }
3772
3773 static void snmp_ssh2_auth_hostbased_ev(const void *event_data,
3774 void *user_data) {
3775 if (snmp_engine == FALSE) {
3776 return;
3777 }
3778
3779 ev_incr_value(SNMP_DB_SSH_LOGINS_F_HOSTBASED_TOTAL,
3780 "ssh.sshLogins.hostbasedAuthTotal", 1);
3781 }
3782
3783 static void snmp_ssh2_auth_hostbased_err_ev(const void *event_data,
3784 void *user_data) {
3785 if (snmp_engine == FALSE) {
3786 return;
3787 }
3788
3789 ev_incr_value(SNMP_DB_SSH_LOGINS_F_HOSTBASED_ERR_TOTAL,
3790 "ssh.sshLogins.hostbasedAuthFailedTotal", 1);
3791 }
3792
3793 static void snmp_ssh2_auth_kbdint_ev(const void *event_data,
3794 void *user_data) {
3795 if (snmp_engine == FALSE) {
3796 return;
3797 }
3798
3799 ev_incr_value(SNMP_DB_SSH_LOGINS_F_KBDINT_TOTAL,
3800 "ssh.sshLogins.keyboardInteractiveAuthTotal", 1);
3801 }
3802
3803 static void snmp_ssh2_auth_kbdint_err_ev(const void *event_data,
3804 void *user_data) {
3805 if (snmp_engine == FALSE) {
3806 return;
3807 }
3808
3809 ev_incr_value(SNMP_DB_SSH_LOGINS_F_KBDINT_ERR_TOTAL,
3810 "ssh.sshLogins.keyboardInteractiveAuthFailedTotal", 1);
3811 }
3812
3813 static void snmp_ssh2_auth_passwd_ev(const void *event_data,
3814 void *user_data) {
3815 if (snmp_engine == FALSE) {
3816 return;
3817 }
3818
3819 ev_incr_value(SNMP_DB_SSH_LOGINS_F_PASSWD_TOTAL,
3820 "ssh.sshLogins.passwordAuthTotal", 1);
3821 }
3822
3823 static void snmp_ssh2_auth_passwd_err_ev(const void *event_data,
3824 void *user_data) {
3825 if (snmp_engine == FALSE) {
3826 return;
3827 }
3828
3829 ev_incr_value(SNMP_DB_SSH_LOGINS_F_PASSWD_ERR_TOTAL,
3830 "ssh.sshLogins.passwordAuthFailedTotal", 1);
3831 }
3832
3833 static void snmp_ssh2_auth_publickey_ev(const void *event_data,
3834 void *user_data) {
3835 if (snmp_engine == FALSE) {
3836 return;
3837 }
3838
3839 ev_incr_value(SNMP_DB_SSH_LOGINS_F_PUBLICKEY_TOTAL,
3840 "ssh.sshLogins.publickeyAuthTotal", 1);
3841 }
3842
3843 static void snmp_ssh2_auth_publickey_err_ev(const void *event_data,
3844 void *user_data) {
3845 if (snmp_engine == FALSE) {
3846 return;
3847 }
3848
3849 ev_incr_value(SNMP_DB_SSH_LOGINS_F_PUBLICKEY_ERR_TOTAL,
3850 "ssh.sshLogins.publickeyAuthFailedTotal", 1);
3851 }
3852
3853 static void snmp_ssh2_sftp_proto_version_ev(const void *event_data,
3854 void *user_data) {
3855 unsigned long protocol_version;
3856 unsigned int field_id;
3857 const char *field_str;
3858
3859 if (snmp_engine == FALSE) {
3860 return;
3861 }
3862
3863 if (event_data == NULL) {
3864 /* Missing required data. */
3865 return;
3866 }
3867
3868 protocol_version = *((unsigned long *) event_data);
3869
3870 switch (protocol_version) {
3871 case 3:
3872 field_id = SNMP_DB_SFTP_SESS_F_SFTP_V3_TOTAL;
3873 field_str = "sftp.sftpSessions.protocolVersion3Total";
3874 break;
3875
3876 case 4:
3877 field_id = SNMP_DB_SFTP_SESS_F_SFTP_V4_TOTAL;
3878 field_str = "sftp.sftpSessions.protocolVersion4Total";
3879 break;
3880
3881 case 5:
3882 field_id = SNMP_DB_SFTP_SESS_F_SFTP_V5_TOTAL;
3883 field_str = "sftp.sftpSessions.protocolVersion5Total";
3884 break;
3885
3886 case 6:
3887 field_id = SNMP_DB_SFTP_SESS_F_SFTP_V6_TOTAL;
3888 field_str = "sftp.sftpSessions.protocolVersion6Total";
3889 break;
3890
3891 default:
3892 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
3893 "unknown SFTP protocol version %lu, ignoring", protocol_version);
3894 return;
3895 }
3896
3897 ev_incr_value(field_id, field_str, 1);
3898 }
3899
3900 static void snmp_ssh2_sftp_sess_opened_ev(const void *event_data,
3901 void *user_data) {
3902 if (snmp_engine == FALSE) {
3903 return;
3904 }
3905
3906 ev_incr_value(SNMP_DB_SFTP_SESS_F_SESS_COUNT,
3907 "sftp.sftpSessions.sessionCount", 1);
3908 ev_incr_value(SNMP_DB_SFTP_SESS_F_SESS_TOTAL,
3909 "sftp.sftpSessions.sessionTotal", 1);
3910 }
3911
3912 static void snmp_ssh2_sftp_sess_closed_ev(const void *event_data,
3913 void *user_data) {
3914 if (snmp_engine == FALSE) {
3915 return;
3916 }
3917
3918 ev_incr_value(SNMP_DB_SFTP_SESS_F_SESS_COUNT,
3919 "sftp.sftpSessions.sessionCount", -1);
3920 }
3921
3922 static void snmp_ssh2_scp_sess_opened_ev(const void *event_data,
3923 void *user_data) {
3924 if (snmp_engine == FALSE) {
3925 return;
3926 }
3927
3928 ev_incr_value(SNMP_DB_SCP_SESS_F_SESS_COUNT,
3929 "scp.scpSessions.sessionCount", 1);
3930 ev_incr_value(SNMP_DB_SCP_SESS_F_SESS_TOTAL,
3931 "scp.scpSessions.sessionTotal", 1);
3932 }
3933
3934 static void snmp_ssh2_scp_sess_closed_ev(const void *event_data,
3935 void *user_data) {
3936 if (snmp_engine == FALSE) {
3937 return;
3938 }
3939
3940 ev_incr_value(SNMP_DB_SCP_SESS_F_SESS_COUNT,
3941 "scp.scpSessions.sessionCount", -1);
3942 }
3943
3944 /* mod_ban-generated events */
3945 static void snmp_ban_ban_user_ev(const void *event_data, void *user_data) {
3946 ev_incr_value(SNMP_DB_BAN_BANS_F_USER_BAN_COUNT, "ban.bans.userBanCount", 1);
3947 ev_incr_value(SNMP_DB_BAN_BANS_F_USER_BAN_TOTAL, "ban.bans.userBanTotal", 1);
3948
3949 ev_incr_value(SNMP_DB_BAN_BANS_F_BAN_COUNT, "ban.bans.banCount", 1);
3950 ev_incr_value(SNMP_DB_BAN_BANS_F_BAN_TOTAL, "ban.bans.banTotal", 1);
3951 }
3952
3953 static void snmp_ban_ban_host_ev(const void *event_data, void *user_data) {
3954 ev_incr_value(SNMP_DB_BAN_BANS_F_HOST_BAN_COUNT, "ban.bans.hostBanCount", 1);
3955 ev_incr_value(SNMP_DB_BAN_BANS_F_HOST_BAN_TOTAL, "ban.bans.hostBanTotal", 1);
3956
3957 ev_incr_value(SNMP_DB_BAN_BANS_F_BAN_COUNT, "ban.bans.banCount", 1);
3958 ev_incr_value(SNMP_DB_BAN_BANS_F_BAN_TOTAL, "ban.bans.banTotal", 1);
3959 }
3960
3961 static void snmp_ban_ban_class_ev(const void *event_data, void *user_data) {
3962 ev_incr_value(SNMP_DB_BAN_BANS_F_CLASS_BAN_COUNT,
3963 "ban.bans.classBanCount", 1);
3964 ev_incr_value(SNMP_DB_BAN_BANS_F_CLASS_BAN_TOTAL,
3965 "ban.bans.classBanTotal", 1);
3966
3967 ev_incr_value(SNMP_DB_BAN_BANS_F_BAN_COUNT, "ban.bans.banCount", 1);
3968 ev_incr_value(SNMP_DB_BAN_BANS_F_BAN_TOTAL, "ban.bans.banTotal", 1);
3969 }
3970
3971 static void snmp_ban_expired_ban_ev(const void *event_data, void *user_data) {
3972 const char *ban_desc = NULL;
3973
3974 if (event_data != NULL) {
3975 char *ptr = NULL;
3976
3977 ban_desc = (const char *) event_data;
3978
3979 ptr = strchr(ban_desc, ':');
3980 if (ptr != NULL) {
3981 /* To get the specific ban criteria/name later, use ptr + 1. */
3982
3983 if (strncmp(ban_desc, "USER", 4) == 0) {
3984 ev_incr_value(SNMP_DB_BAN_BANS_F_USER_BAN_COUNT,
3985 "ban.bans.userBanCount", -1);
3986
3987 } else if (strncmp(ban_desc, "HOST", 4) == 0) {
3988 ev_incr_value(SNMP_DB_BAN_BANS_F_HOST_BAN_COUNT,
3989 "ban.bans.hostBanCount", -1);
3990
3991 } else if (strncmp(ban_desc, "CLASS", 5) == 0) {
3992 ev_incr_value(SNMP_DB_BAN_BANS_F_CLASS_BAN_COUNT,
3993 "ban.bans.classBanCount", -1);
3994 }
3995
3996 ev_incr_value(SNMP_DB_BAN_BANS_F_BAN_COUNT, "ban.bans.banCount", -1);
3997 }
3998 }
3999 }
4000
4001 static void snmp_ban_client_disconn_ev(const void *event_data,
4002 void *user_data) {
4003 const char *ban_desc = NULL;
4004
4005 if (event_data != NULL) {
4006 char *ptr = NULL;
4007
4008 ban_desc = (const char *) event_data;
4009
4010 ptr = strchr(ban_desc, ':');
4011 if (ptr != NULL) {
4012 /* To get the specific ban criteria/name later, use ptr + 1. */
4013
4014 if (strncmp(ban_desc, "USER", 4) == 0) {
4015 ev_incr_value(SNMP_DB_BAN_CONNS_F_USER_BAN_TOTAL,
4016 "ban.connections.userBannedTotal", 1);
4017
4018 } else if (strncmp(ban_desc, "HOST", 4) == 0) {
4019 ev_incr_value(SNMP_DB_BAN_CONNS_F_HOST_BAN_TOTAL,
4020 "ban.connections.hostBannedTotal", 1);
4021
4022 } else if (strncmp(ban_desc, "CLASS", 5) == 0) {
4023 ev_incr_value(SNMP_DB_BAN_CONNS_F_CLASS_BAN_TOTAL,
4024 "ban.connections.classBannedTotal", 1);
4025 }
4026
4027 ev_incr_value(SNMP_DB_BAN_CONNS_F_CONN_BAN_TOTAL,
4028 "ban.connections.connectionBannedTotal", 1);
4029 }
4030 }
4031 }
4032
4033 /* XXX Do we want to support any Controls/ftpctl actions? */
4034
4035 /* Initialization routines
4036 */
4037
4038 static int snmp_init(void) {
4039 struct protoent *pre = NULL;
4040
4041 snmp_pool = make_sub_pool(permanent_pool);
4042 pr_pool_tag(snmp_pool, MOD_SNMP_VERSION);
4043
4044 pr_event_register(&snmp_module, "core.max-instances",
4045 snmp_max_inst_ev, NULL);
4046 #if defined(PR_SHARED_MODULE)
4047 pr_event_register(&snmp_module, "core.module-unload", snmp_mod_unload_ev,
4048 NULL);
4049 #endif
4050 pr_event_register(&snmp_module, "core.postparse", snmp_postparse_ev, NULL);
4051 pr_event_register(&snmp_module, "core.restart", snmp_restart_ev, NULL);
4052 pr_event_register(&snmp_module, "core.shutdown", snmp_shutdown_ev, NULL);
4053 pr_event_register(&snmp_module, "core.startup", snmp_startup_ev, NULL);
4054
4055 /* Normally we should register the 'core.exit' event listener in the
4056 * sess_init callback. However, we use this listener to listen for
4057 * refused connections, e.g. connections refused by other modules'
4058 * sess_init callbacks. And depending on the module load order, another
4059 * module might refuse the connection before mod_snmp's sess_init callback
4060 * is invoked, which would prevent mod_snmp from registering its 'core.exit'
4061 * event listener.
4062 *
4063 * Thus to work around this timing issue, we register our 'core.exit' event
4064 * listener here, in the daemon process. It should not hurt anything.
4065 */
4066 pr_event_register(&snmp_module, "core.exit", snmp_exit_ev, NULL);
4067
4068 #ifdef HAVE_SETPROTOENT
4069 setprotoent(FALSE);
4070 #endif
4071
4072 pre = getprotobyname("udp");
4073 if (pre != NULL) {
4074 snmp_proto_udp = pre->p_proto;
4075 }
4076
4077 #ifdef HAVE_ENDPROTOENT
4078 endprotoent();
4079 #endif
4080
4081 #ifdef HAVE_RANDOM
4082 /* Seed the random(3) generator. */
4083 srandom((unsigned int) (time(NULL) * getpid()));
4084 #endif /* HAVE_RANDOM */
4085
4086 return 0;
4087 }
4088
4089 static int snmp_sess_init(void) {
4090 config_rec *c;
4091 int res;
4092
4093 c = find_config(main_server->conf, CONF_PARAM, "SNMPEnable", FALSE);
4094 if (c != NULL) {
4095 snmp_enabled = *((int *) c->argv[0]);
4096 }
4097
4098 if (snmp_enabled == FALSE) {
4099 snmp_engine = FALSE;
4100 return 0;
4101 }
4102
4103 pr_event_register(&snmp_module, "core.invalid-command",
4104 snmp_cmd_invalid_ev, NULL);
4105 pr_event_register(&snmp_module, "core.timeout-idle",
4106 snmp_timeout_idle_ev, NULL);
4107 pr_event_register(&snmp_module, "core.timeout-login",
4108 snmp_timeout_login_ev, NULL);
4109 pr_event_register(&snmp_module, "core.timeout-no-transfer",
4110 snmp_timeout_noxfer_ev, NULL);
4111 pr_event_register(&snmp_module, "core.timeout-stalled",
4112 snmp_timeout_stalled_ev, NULL);
4113 pr_event_register(&snmp_module, "core.unhandled-command",
4114 snmp_cmd_invalid_ev, NULL);
4115
4116 pr_event_register(&snmp_module, "mod_auth.authentication-code",
4117 snmp_auth_code_ev, NULL);
4118
4119 if (pr_module_exists("mod_tls.c") == TRUE) {
4120 /* mod_tls events */
4121 pr_event_register(&snmp_module, "mod_tls.ctrl-handshake-failed",
4122 snmp_tls_ctrl_handshake_err_ev, NULL);
4123 pr_event_register(&snmp_module, "mod_tls.data-handshake-failed",
4124 snmp_tls_data_handshake_err_ev, NULL);
4125
4126 pr_event_register(&snmp_module, "mod_tls.verify-client",
4127 snmp_tls_verify_client_ev, NULL);
4128 pr_event_register(&snmp_module, "mod_tls.verify-client-failed",
4129 snmp_tls_verify_client_err_ev, NULL);
4130 }
4131
4132 if (pr_module_exists("mod_sftp.c") == TRUE) {
4133 /* mod_sftp events */
4134
4135 pr_event_register(&snmp_module, "mod_sftp.ssh2.kex.failed",
4136 snmp_ssh2_kex_err_ev, NULL);
4137 pr_event_register(&snmp_module, "mod_sftp.ssh2.client-compression",
4138 snmp_ssh2_c2s_compress_ev, NULL);
4139 pr_event_register(&snmp_module, "mod_sftp.ssh2.server-compression",
4140 snmp_ssh2_s2c_compress_ev, NULL);
4141
4142 pr_event_register(&snmp_module, "mod_sftp.ssh2.auth-hostbased",
4143 snmp_ssh2_auth_hostbased_ev, NULL);
4144 pr_event_register(&snmp_module, "mod_sftp.ssh2.auth-hostbased.failed",
4145 snmp_ssh2_auth_hostbased_err_ev, NULL);
4146
4147 pr_event_register(&snmp_module, "mod_sftp.ssh2.auth-kbdint",
4148 snmp_ssh2_auth_kbdint_ev, NULL);
4149 pr_event_register(&snmp_module, "mod_sftp.ssh2.auth-kbdint.failed",
4150 snmp_ssh2_auth_kbdint_err_ev, NULL);
4151
4152 pr_event_register(&snmp_module, "mod_sftp.ssh2.auth-password",
4153 snmp_ssh2_auth_passwd_ev, NULL);
4154 pr_event_register(&snmp_module, "mod_sftp.ssh2.auth-password.failed",
4155 snmp_ssh2_auth_passwd_err_ev, NULL);
4156
4157 pr_event_register(&snmp_module, "mod_sftp.ssh2.auth-publickey",
4158 snmp_ssh2_auth_publickey_ev, NULL);
4159 pr_event_register(&snmp_module, "mod_sftp.ssh2.auth-publickey.failed",
4160 snmp_ssh2_auth_publickey_err_ev, NULL);
4161
4162 pr_event_register(&snmp_module, "mod_sftp.sftp.session-opened",
4163 snmp_ssh2_sftp_sess_opened_ev, NULL);
4164 pr_event_register(&snmp_module, "mod_sftp.sftp.session-closed",
4165 snmp_ssh2_sftp_sess_closed_ev, NULL);
4166 pr_event_register(&snmp_module, "mod_sftp.sftp.protocol-version",
4167 snmp_ssh2_sftp_proto_version_ev, NULL);
4168
4169 pr_event_register(&snmp_module, "mod_sftp.scp.session-opened",
4170 snmp_ssh2_scp_sess_opened_ev, NULL);
4171 pr_event_register(&snmp_module, "mod_sftp.scp.session-closed",
4172 snmp_ssh2_scp_sess_closed_ev, NULL);
4173 }
4174
4175 if (pr_module_exists("mod_ban.c") == TRUE) {
4176 /* mod_ban events */
4177
4178 pr_event_register(&snmp_module, "mod_ban.ban-user", snmp_ban_ban_user_ev,
4179 NULL);
4180 pr_event_register(&snmp_module, "mod_ban.ban-host", snmp_ban_ban_host_ev,
4181 NULL);
4182 pr_event_register(&snmp_module, "mod_ban.ban-class", snmp_ban_ban_class_ev,
4183 NULL);
4184
4185 /* Note: For these event listeners to work as expected, the mod_snmp
4186 * module needs to be loaded AFTER mod_ban, i.e.:
4187 *
4188 * --with-modules=....:mod_ban:mod_snmp:...
4189 *
4190 * or:
4191 *
4192 * LoadModule mod_ban.c
4193 * ...
4194 * LoadModule mod_snmp.c
4195 *
4196 * That we, we can have our event listeners registered by the time that
4197 * mod_ban's sess_init callback causes events to be generated for an
4198 * incoming connection (including ban expiration).
4199 */
4200 pr_event_register(&snmp_module, "mod_ban.ban.expired",
4201 snmp_ban_expired_ban_ev, NULL);
4202 pr_event_register(&snmp_module, "mod_ban.ban.client-disconnected",
4203 snmp_ban_client_disconn_ev, NULL);
4204 }
4205
4206 res = snmp_db_incr_value(session.pool, SNMP_DB_DAEMON_F_CONN_COUNT, 1);
4207 if (res < 0) {
4208 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
4209 "error incrementing daemon.connectionCount: %s",
4210 strerror(errno));
4211 }
4212
4213 res = snmp_db_incr_value(session.pool, SNMP_DB_DAEMON_F_CONN_TOTAL, 1);
4214 if (res < 0) {
4215 (void) pr_log_writefile(snmp_logfd, MOD_SNMP_VERSION,
4216 "error incrementing daemon.connectionTotal: %s",
4217 strerror(errno));
4218 }
4219
4220 #ifdef HAVE_RANDOM
4221 /* Reseed the random(3) generator. */
4222 srandom((unsigned int) (time(NULL) * getpid()));
4223 #endif /* HAVE_RANDOM */
4224
4225 c = find_config(main_server->conf, CONF_PARAM, "SNMPNotify", FALSE);
4226 while (c != NULL) {
4227 pr_signals_handle();
4228
4229 if (snmp_notifys == NULL) {
4230 snmp_notifys = make_array(session.pool, 1, sizeof(pr_netaddr_t *));
4231 }
4232
4233 *((pr_netaddr_t **) push_array(snmp_notifys)) = c->argv[0];
4234
4235 c = find_config_next(c, c->next, CONF_PARAM, "SNMPNotify", FALSE);
4236 }
4237
4238 return 0;
4239 }
4240
4241 /* Module API tables
4242 */
4243
4244 static conftable snmp_conftab[] = {
4245 { "SNMPAgent", set_snmpagent, NULL },
4246 { "SNMPCommunity", set_snmpcommunity, NULL },
4247 { "SNMPEnable", set_snmpenable, NULL },
4248 { "SNMPEngine", set_snmpengine, NULL },
4249 { "SNMPLog", set_snmplog, NULL },
4250 { "SNMPMaxVariables", set_snmpmaxvariables, NULL },
4251 { "SNMPNotify", set_snmpnotify, NULL },
4252 { "SNMPOptions", set_snmpoptions, NULL },
4253 { "SNMPTables", set_snmptables, NULL },
4254 { NULL }
4255 };
4256
4257 static cmdtable snmp_cmdtab[] = {
4258 { PRE_CMD, C_LIST, G_NONE, snmp_pre_list, FALSE, FALSE },
4259 { LOG_CMD, C_LIST, G_NONE, snmp_log_list, FALSE, FALSE },
4260 { LOG_CMD_ERR, C_LIST, G_NONE, snmp_err_list, FALSE, FALSE },
4261
4262 { PRE_CMD, C_MLSD, G_NONE, snmp_pre_list, FALSE, FALSE },
4263 { LOG_CMD, C_MLSD, G_NONE, snmp_log_list, FALSE, FALSE },
4264 { LOG_CMD_ERR, C_MLSD, G_NONE, snmp_err_list, FALSE, FALSE },
4265
4266 { PRE_CMD, C_NLST, G_NONE, snmp_pre_list, FALSE, FALSE },
4267 { LOG_CMD, C_NLST, G_NONE, snmp_log_list, FALSE, FALSE },
4268 { LOG_CMD_ERR, C_NLST, G_NONE, snmp_err_list, FALSE, FALSE },
4269
4270 { LOG_CMD, C_PASS, G_NONE, snmp_log_pass, FALSE, FALSE },
4271 { LOG_CMD_ERR, C_PASS, G_NONE, snmp_err_pass, FALSE, FALSE },
4272
4273 { PRE_CMD, C_RETR, G_NONE, snmp_pre_retr, FALSE, FALSE },
4274 { LOG_CMD, C_RETR, G_NONE, snmp_log_retr, FALSE, FALSE },
4275 { LOG_CMD_ERR, C_RETR, G_NONE, snmp_err_retr, FALSE, FALSE },
4276
4277 { PRE_CMD, C_STOR, G_NONE, snmp_pre_stor, FALSE, FALSE },
4278 { LOG_CMD, C_STOR, G_NONE, snmp_log_stor, FALSE, FALSE },
4279 { LOG_CMD_ERR, C_STOR, G_NONE, snmp_err_stor, FALSE, FALSE },
4280
4281 /* For mod_tls */
4282 { LOG_CMD, C_AUTH, G_NONE, snmp_log_auth, FALSE, FALSE },
4283 { LOG_CMD, C_CCC, G_NONE, snmp_log_ccc, FALSE, FALSE },
4284 { LOG_CMD_ERR, C_CCC, G_NONE, snmp_err_ccc, FALSE, FALSE },
4285
4286 { 0, NULL }
4287 };
4288
4289 module snmp_module = {
4290 /* Always NULL */
4291 NULL, NULL,
4292
4293 /* Module API version */
4294 0x20,
4295
4296 /* Module name */
4297 "snmp",
4298
4299 /* Module configuration handler table */
4300 snmp_conftab,
4301
4302 /* Module command handler table */
4303 snmp_cmdtab,
4304
4305 /* Module authentication handler table */
4306 NULL,
4307
4308 /* Module initialization */
4309 snmp_init,
4310
4311 /* Session initialization */
4312 snmp_sess_init,
4313
4314 /* Module version */
4315 MOD_SNMP_VERSION
4316 };