diff options
author | Rafael Zalamena <rzalamena@cvs.openbsd.org> | 2016-10-28 08:01:54 +0000 |
---|---|---|
committer | Rafael Zalamena <rzalamena@cvs.openbsd.org> | 2016-10-28 08:01:54 +0000 |
commit | d35485ba9ba0b979a305223c6cf140a4b3994967 (patch) | |
tree | e1f4179bf25233c2609032bd8f01f217aa19e9e2 /usr.sbin | |
parent | e21b9cdb6b4c709befe0cdee7f169dda63377365 (diff) |
Use snmpd_env as the only global variable for env to simplify the daemon
and avoid problems.
ok jca@
Diffstat (limited to 'usr.sbin')
-rw-r--r-- | usr.sbin/snmpd/kroute.c | 9 | ||||
-rw-r--r-- | usr.sbin/snmpd/mib.c | 24 | ||||
-rw-r--r-- | usr.sbin/snmpd/mps.c | 4 | ||||
-rw-r--r-- | usr.sbin/snmpd/smi.c | 8 | ||||
-rw-r--r-- | usr.sbin/snmpd/snmpd.h | 4 | ||||
-rw-r--r-- | usr.sbin/snmpd/snmpe.c | 24 | ||||
-rw-r--r-- | usr.sbin/snmpd/timer.c | 25 | ||||
-rw-r--r-- | usr.sbin/snmpd/trap.c | 12 | ||||
-rw-r--r-- | usr.sbin/snmpd/usm.c | 28 |
9 files changed, 66 insertions, 72 deletions
diff --git a/usr.sbin/snmpd/kroute.c b/usr.sbin/snmpd/kroute.c index a3e14740a63..2a3bb5b2a26 100644 --- a/usr.sbin/snmpd/kroute.c +++ b/usr.sbin/snmpd/kroute.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kroute.c,v 1.33 2016/09/03 15:45:02 jca Exp $ */ +/* $OpenBSD: kroute.c,v 1.34 2016/10/28 08:01:53 rzalamena Exp $ */ /* * Copyright (c) 2007, 2008 Reyk Floeter <reyk@openbsd.org> @@ -44,8 +44,6 @@ #include "snmpd.h" -extern struct snmpd *env; - struct ktable **krt; u_int krt_size; @@ -173,8 +171,9 @@ kr_init(void) &opt, sizeof(opt)) == -1) log_warn("%s: SO_USELOOPBACK", __func__); /* not fatal */ - if (env->sc_rtfilter && setsockopt(kr_state.ks_fd, PF_ROUTE, - ROUTE_MSGFILTER, &env->sc_rtfilter, sizeof(env->sc_rtfilter)) == -1) + if (snmpd_env->sc_rtfilter && setsockopt(kr_state.ks_fd, PF_ROUTE, + ROUTE_MSGFILTER, &snmpd_env->sc_rtfilter, + sizeof(snmpd_env->sc_rtfilter)) == -1) log_warn("%s: ROUTE_MSGFILTER", __func__); /* grow receive buffer, don't wanna miss messages */ diff --git a/usr.sbin/snmpd/mib.c b/usr.sbin/snmpd/mib.c index e8a4179fbc3..0dfd8c84f2b 100644 --- a/usr.sbin/snmpd/mib.c +++ b/usr.sbin/snmpd/mib.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mib.c,v 1.80 2015/11/17 12:30:23 gerhard Exp $ */ +/* $OpenBSD: mib.c,v 1.81 2016/10/28 08:01:53 rzalamena Exp $ */ /* * Copyright (c) 2012 Joel Knight <joel@openbsd.org> @@ -58,8 +58,6 @@ #include "snmpd.h" #include "mib.h" -extern struct snmpd *env; - /* * Defined in SNMPv2-MIB.txt (RFC 3418) */ @@ -255,7 +253,7 @@ mib_sysor(struct oid *oid, struct ber_oid *o, struct ber_element **elm) int mib_getsnmp(struct oid *oid, struct ber_oid *o, struct ber_element **elm) { - struct snmp_stats *stats = &env->sc_stats; + struct snmp_stats *stats = &snmpd_env->sc_stats; long long i; struct statsmap { u_int8_t m_id; @@ -316,7 +314,7 @@ mib_getsnmp(struct oid *oid, struct ber_oid *o, struct ber_element **elm) int mib_setsnmp(struct oid *oid, struct ber_oid *o, struct ber_element **elm) { - struct snmp_stats *stats = &env->sc_stats; + struct snmp_stats *stats = &snmpd_env->sc_stats; long long i; if (ber_get_integer(*elm, &i) == -1) @@ -354,11 +352,11 @@ mib_engine(struct oid *oid, struct ber_oid *o, struct ber_element **elm) { switch (oid->o_oid[OIDIDX_snmpEngine]) { case 1: - *elm = ber_add_nstring(*elm, env->sc_engineid, - env->sc_engineid_len); + *elm = ber_add_nstring(*elm, snmpd_env->sc_engineid, + snmpd_env->sc_engineid_len); break; case 2: - *elm = ber_add_integer(*elm, env->sc_engine_boots); + *elm = ber_add_integer(*elm, snmpd_env->sc_engine_boots); break; case 3: *elm = ber_add_integer(*elm, snmpd_engine_time()); @@ -375,7 +373,7 @@ mib_engine(struct oid *oid, struct ber_oid *o, struct ber_element **elm) int mib_usmstats(struct oid *oid, struct ber_oid *o, struct ber_element **elm) { - struct snmp_stats *stats = &env->sc_stats; + struct snmp_stats *stats = &snmpd_env->sc_stats; long long i; struct statsmap { u_int8_t m_id; @@ -697,7 +695,7 @@ mib_hrdevice(struct oid *oid, struct ber_oid *o, struct ber_element **elm) /* Get and verify the current row index */ idx = o->bo_id[OIDIDX_hrDeviceEntry]; - if (idx > (u_int)env->sc_ncpu) + if (idx > (u_int)snmpd_env->sc_ncpu) return (1); /* Tables need to prepend the OID on their own */ @@ -748,7 +746,7 @@ mib_hrprocessor(struct oid *oid, struct ber_oid *o, struct ber_element **elm) /* Get and verify the current row index */ idx = o->bo_id[OIDIDX_hrDeviceEntry]; - if (idx > (u_int)env->sc_ncpu) + if (idx > (u_int)snmpd_env->sc_ncpu) return (1); else if (idx < 1) idx = 1; @@ -766,9 +764,9 @@ mib_hrprocessor(struct oid *oid, struct ber_oid *o, struct ber_element **elm) * The percentage of time that the system was not * idle during the last minute. */ - if (env->sc_cpustates == NULL) + if (snmpd_env->sc_cpustates == NULL) return (-1); - cptime2 = env->sc_cpustates + (CPUSTATES * (idx - 1)); + cptime2 = snmpd_env->sc_cpustates + (CPUSTATES * (idx - 1)); val = 100 - (cptime2[CP_IDLE] > 1000 ? 1000 : (cptime2[CP_IDLE] / 10)); ber = ber_add_integer(ber, val); diff --git a/usr.sbin/snmpd/mps.c b/usr.sbin/snmpd/mps.c index c345cb59e62..2fcf03489b4 100644 --- a/usr.sbin/snmpd/mps.c +++ b/usr.sbin/snmpd/mps.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mps.c,v 1.23 2015/12/05 06:42:18 mmcc Exp $ */ +/* $OpenBSD: mps.c,v 1.24 2016/10/28 08:01:53 rzalamena Exp $ */ /* * Copyright (c) 2007, 2008, 2012 Reyk Floeter <reyk@openbsd.org> @@ -45,8 +45,6 @@ #include "snmpd.h" #include "mib.h" -extern struct snmpd *env; - struct ber_oid * mps_table(struct oid *, struct ber_oid *, struct ber_oid *); diff --git a/usr.sbin/snmpd/smi.c b/usr.sbin/snmpd/smi.c index 0777cc0e41d..fd11f806ac7 100644 --- a/usr.sbin/snmpd/smi.c +++ b/usr.sbin/snmpd/smi.c @@ -49,8 +49,6 @@ #define MINIMUM(a, b) (((a) < (b)) ? (a) : (b)) -extern struct snmpd *env; - RB_HEAD(oidtree, oid); RB_PROTOTYPE(oidtree, oid, o_element, smi_oid_cmp); struct oidtree smi_oidtree; @@ -66,9 +64,9 @@ smi_getticks(void) u_long ticks; gettimeofday(&now, NULL); - if (timercmp(&now, &env->sc_starttime, <=)) + if (timercmp(&now, &snmpd_env->sc_starttime, <=)) return (0); - timersub(&now, &env->sc_starttime, &run); + timersub(&now, &snmpd_env->sc_starttime, &run); ticks = run.tv_sec * 100; if (run.tv_usec) ticks += run.tv_usec / 10000; @@ -108,7 +106,7 @@ smi_oid2string(struct ber_oid *o, char *buf, size_t len, size_t skip) bcopy(o, &key.o_id, sizeof(struct ber_oid)); key.o_flags |= OID_KEY; /* do not match wildcards */ - if (env->sc_flags & SNMPD_F_NONAMES) + if (snmpd_env->sc_flags & SNMPD_F_NONAMES) lookup = 0; for (i = 0; i < o->bo_n; i++) { diff --git a/usr.sbin/snmpd/snmpd.h b/usr.sbin/snmpd/snmpd.h index ed54a5f1e77..352ffcf7f76 100644 --- a/usr.sbin/snmpd/snmpd.h +++ b/usr.sbin/snmpd/snmpd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: snmpd.h,v 1.69 2016/10/03 12:16:41 dlg Exp $ */ +/* $OpenBSD: snmpd.h,v 1.70 2016/10/28 08:01:53 rzalamena Exp $ */ /* * Copyright (c) 2007, 2008, 2012 Reyk Floeter <reyk@openbsd.org> @@ -580,6 +580,8 @@ struct trapcmd { RB_HEAD(trapcmd_tree, trapcmd); extern struct trapcmd_tree trapcmd_tree; +extern struct snmpd *snmpd_env; + /* control.c */ int control_init(struct privsep *, struct control_sock *); int control_listen(struct control_sock *); diff --git a/usr.sbin/snmpd/snmpe.c b/usr.sbin/snmpd/snmpe.c index 06c07212a3a..e9dc911852c 100644 --- a/usr.sbin/snmpd/snmpe.c +++ b/usr.sbin/snmpd/snmpe.c @@ -1,4 +1,4 @@ -/* $OpenBSD: snmpe.c,v 1.42 2016/08/16 18:41:57 tedu Exp $ */ +/* $OpenBSD: snmpe.c,v 1.43 2016/10/28 08:01:53 rzalamena Exp $ */ /* * Copyright (c) 2007, 2008, 2012 Reyk Floeter <reyk@openbsd.org> @@ -52,8 +52,6 @@ void snmpe_recvmsg(int fd, short, void *); int snmpe_encode(struct snmp_message *); void snmp_msgfree(struct snmp_message *); -struct snmpd *env = NULL; - struct imsgev *iev_parent; static struct privsep_proc procs[] = { @@ -63,13 +61,12 @@ static struct privsep_proc procs[] = { pid_t snmpe(struct privsep *ps, struct privsep_proc *p) { + struct snmpd *env = ps->ps_env; #ifdef DEBUG char buf[BUFSIZ]; struct oid *oid; #endif - env = ps->ps_env; - #ifdef DEBUG for (oid = NULL; (oid = smi_foreach(oid, 0)) != NULL;) { smi_oid2string(&oid->o_id, buf, sizeof(buf), 0); @@ -88,6 +85,8 @@ snmpe(struct privsep *ps, struct privsep_proc *p) void snmpe_init(struct privsep *ps, struct privsep_proc *p, void *arg) { + struct snmpd *env = ps->ps_env; + kr_init(); trap_init(); timer_init(); @@ -149,6 +148,7 @@ snmpe_bind(struct address *addr) int snmpe_parse(struct snmp_message *msg) { + struct snmpd *env = snmpd_env; struct snmp_stats *stats = &env->sc_stats; struct ber_element *a; long long ver, req; @@ -331,7 +331,7 @@ snmpe_parse(struct snmp_message *msg) int snmpe_parsevarbinds(struct snmp_message *msg) { - struct snmp_stats *stats = &env->sc_stats; + struct snmp_stats *stats = &snmpd_env->sc_stats; char buf[BUFSIZ]; struct ber_oid o; int ret = 0; @@ -398,7 +398,7 @@ snmpe_parsevarbinds(struct snmp_message *msg) goto varfail; case SNMP_C_SETREQ: - if (env->sc_readonly == 0) { + if (snmpd_env->sc_readonly == 0) { ret = mps_setreq(msg, msg->sm_b, &o); if (ret == 0) @@ -452,6 +452,7 @@ snmpe_parsevarbinds(struct snmp_message *msg) void snmpe_recvmsg(int fd, short sig, void *arg) { + struct snmpd *env = arg; struct snmp_stats *stats = &env->sc_stats; ssize_t len; struct snmp_message *msg; @@ -508,13 +509,13 @@ snmpe_dispatchmsg(struct snmp_message *msg) /* not dispatched to subagent; respond directly */ msg->sm_context = SNMP_C_GETRESP; - snmpe_response(env->sc_sock, msg); + snmpe_response(snmpd_env->sc_sock, msg); } void snmpe_response(int fd, struct snmp_message *msg) { - struct snmp_stats *stats = &env->sc_stats; + struct snmp_stats *stats = &snmpd_env->sc_stats; u_int8_t *ptr = NULL; ssize_t len; @@ -596,8 +597,9 @@ snmpe_encode(struct snmp_message *msg) pdu = epdu = ber_add_sequence(NULL); if (msg->sm_version == SNMP_V3) { - if ((epdu = ber_printf_elements(epdu, "xs{", env->sc_engineid, - env->sc_engineid_len, msg->sm_ctxname)) == NULL) { + if ((epdu = ber_printf_elements(epdu, "xs{", + snmpd_env->sc_engineid, snmpd_env->sc_engineid_len, + msg->sm_ctxname)) == NULL) { ber_free_elements(pdu); return -1; } diff --git a/usr.sbin/snmpd/timer.c b/usr.sbin/snmpd/timer.c index f7865d4cdbd..26d22b8d9f0 100644 --- a/usr.sbin/snmpd/timer.c +++ b/usr.sbin/snmpd/timer.c @@ -1,4 +1,4 @@ -/* $OpenBSD: timer.c,v 1.6 2016/09/26 14:00:05 rzalamena Exp $ */ +/* $OpenBSD: timer.c,v 1.7 2016/10/28 08:01:53 rzalamena Exp $ */ /* * Copyright (c) 2008 Reyk Floeter <reyk@openbsd.org> @@ -42,8 +42,6 @@ #include "snmpd.h" #include "mib.h" -extern struct snmpd *env; - void timer_cpu(int, short, void *); int percentages(int, int64_t *, int64_t *, int64_t *, int64_t *); @@ -62,9 +60,9 @@ timer_cpu(int fd, short event, void *arg) int64_t *cptime2; len = CPUSTATES * sizeof(int64_t); - for (n = 0; n < env->sc_ncpu; n++) { + for (n = 0; n < snmpd_env->sc_ncpu; n++) { mib[2] = n; - cptime2 = env->sc_cpustates + (CPUSTATES * n); + cptime2 = snmpd_env->sc_cpustates + (CPUSTATES * n); if (sysctl(mib, 3, cp_time[n], &len, NULL, 0) == -1) continue; (void)percentages(CPUSTATES, cptime2, cp_time[n], @@ -85,18 +83,19 @@ timer_init(void) int mib[] = { CTL_HW, HW_NCPU }, i; size_t len; - len = sizeof(env->sc_ncpu); - if (sysctl(mib, 2, &env->sc_ncpu, &len, NULL, 0) == -1) + len = sizeof(snmpd_env->sc_ncpu); + if (sysctl(mib, 2, &snmpd_env->sc_ncpu, &len, NULL, 0) == -1) fatal("sysctl"); - env->sc_cpustates = calloc(env->sc_ncpu, CPUSTATES * sizeof(int64_t)); - cp_time = calloc(env->sc_ncpu, sizeof(int64_t *)); - cp_old = calloc(env->sc_ncpu, sizeof(int64_t *)); - cp_diff = calloc(env->sc_ncpu, sizeof(int64_t *)); - if (env->sc_cpustates == NULL || + snmpd_env->sc_cpustates = calloc(snmpd_env->sc_ncpu, + CPUSTATES * sizeof(int64_t)); + cp_time = calloc(snmpd_env->sc_ncpu, sizeof(int64_t *)); + cp_old = calloc(snmpd_env->sc_ncpu, sizeof(int64_t *)); + cp_diff = calloc(snmpd_env->sc_ncpu, sizeof(int64_t *)); + if (snmpd_env->sc_cpustates == NULL || cp_time == NULL || cp_old == NULL || cp_diff == NULL) fatal("calloc"); - for (i = 0; i < env->sc_ncpu; i++) { + for (i = 0; i < snmpd_env->sc_ncpu; i++) { cp_time[i] = calloc(CPUSTATES, sizeof(int64_t)); cp_old[i] = calloc(CPUSTATES, sizeof(int64_t)); cp_diff[i] = calloc(CPUSTATES, sizeof(int64_t)); diff --git a/usr.sbin/snmpd/trap.c b/usr.sbin/snmpd/trap.c index 126bf27f5f5..b371e7fafe5 100644 --- a/usr.sbin/snmpd/trap.c +++ b/usr.sbin/snmpd/trap.c @@ -1,4 +1,4 @@ -/* $OpenBSD: trap.c,v 1.26 2015/12/05 06:42:18 mmcc Exp $ */ +/* $OpenBSD: trap.c,v 1.27 2016/10/28 08:01:53 rzalamena Exp $ */ /* * Copyright (c) 2008 Reyk Floeter <reyk@openbsd.org> @@ -39,8 +39,6 @@ #include "snmpd.h" #include "mib.h" -extern struct snmpd *env; - void trap_init(void) { @@ -158,7 +156,7 @@ trap_send(struct ber_oid *oid, struct ber_element *elm) char ostr[SNMP_MAX_OID_STRLEN]; struct oid oa, ob; - if (TAILQ_EMPTY(&env->sc_trapreceivers)) + if (TAILQ_EMPTY(&snmpd_env->sc_trapreceivers)) return (0); smi_scalar_oidlen(&uptime); @@ -187,7 +185,7 @@ trap_send(struct ber_oid *oid, struct ber_element *elm) bzero(&ber, sizeof(ber)); ber.fd = -1; - TAILQ_FOREACH(tr, &env->sc_trapreceivers, entry) { + TAILQ_FOREACH(tr, &snmpd_env->sc_trapreceivers, entry) { if (tr->sa_oid != NULL && tr->sa_oid->bo_n) { /* The trap receiver may want only a specified MIB */ bcopy(&tr->sa_oid->bo_id, &ob.o_oid, @@ -203,7 +201,7 @@ trap_send(struct ber_oid *oid, struct ber_element *elm) } cmn = tr->sa_community != NULL ? - tr->sa_community : env->sc_trcommunity; + tr->sa_community : snmpd_env->sc_trcommunity; /* SNMP header */ root = ber_add_sequence(NULL); @@ -219,7 +217,7 @@ trap_send(struct ber_oid *oid, struct ber_element *elm) if (ber_get_writebuf(&ber, (void *)&ptr) > 0 && sendto(s, ptr, len, 0, (struct sockaddr *)&tr->ss, tr->ss.ss_len) != -1) { - env->sc_stats.snmp_outpkts++; + snmpd_env->sc_stats.snmp_outpkts++; ret++; } diff --git a/usr.sbin/snmpd/usm.c b/usr.sbin/snmpd/usm.c index 0997542557c..c49854bb4cb 100644 --- a/usr.sbin/snmpd/usm.c +++ b/usr.sbin/snmpd/usm.c @@ -1,4 +1,4 @@ -/* $OpenBSD: usm.c,v 1.10 2016/10/03 12:19:59 dlg Exp $ */ +/* $OpenBSD: usm.c,v 1.11 2016/10/28 08:01:53 rzalamena Exp $ */ /* * Copyright (c) 2012 GeNUA mbH @@ -42,8 +42,6 @@ #include "snmpd.h" #include "mib.h" -extern struct snmpd *env; - SLIST_HEAD(, usmuser) usmuserlist; const EVP_MD *usm_get_md(enum usmauth); @@ -210,7 +208,7 @@ fail: struct ber_element * usm_decode(struct snmp_message *msg, struct ber_element *elm, const char **errp) { - struct snmp_stats *stats = &env->sc_stats; + struct snmp_stats *stats = &snmpd_env->sc_stats; off_t offs, offs2; char *usmparams; size_t len; @@ -263,8 +261,8 @@ usm_decode(struct snmp_message *msg, struct ber_element *elm, const char **errp) goto done; } - if (enginelen != env->sc_engineid_len || - memcmp(engineid, env->sc_engineid, enginelen) != 0) { + if (enginelen != snmpd_env->sc_engineid_len || + memcmp(engineid, snmpd_env->sc_engineid, enginelen) != 0) { *errp = "unknown engine id"; msg->sm_usmerr = OIDVAL_usmErrEngineId; stats->snmp_usmnosuchengine++; @@ -273,7 +271,7 @@ usm_decode(struct snmp_message *msg, struct ber_element *elm, const char **errp) if (engine_boots != 0LL && engine_time != 0LL) { now = snmpd_engine_time(); - if (engine_boots != env->sc_engine_boots || + if (engine_boots != snmpd_env->sc_engine_boots || engine_time < (long long)(now - SNMP_MAX_TIMEWINDOW) || engine_time > (long long)(now + SNMP_MAX_TIMEWINDOW)) { *errp = "out of time window"; @@ -372,11 +370,12 @@ usm_encode(struct snmp_message *msg, struct ber_element *e) } else saltlen = 0; - msg->sm_engine_boots = (u_int32_t)env->sc_engine_boots; + msg->sm_engine_boots = (u_int32_t)snmpd_env->sc_engine_boots; msg->sm_engine_time = (u_int32_t)snmpd_engine_time(); if ((a = ber_printf_elements(usm, "xdds", - env->sc_engineid, env->sc_engineid_len, msg->sm_engine_boots, - msg->sm_engine_time, msg->sm_username)) == NULL) + snmpd_env->sc_engineid, snmpd_env->sc_engineid_len, + msg->sm_engine_boots, msg->sm_engine_time, + msg->sm_username)) == NULL) goto done; if ((a = ber_add_nstring(a, digest, digestlen)) == NULL) @@ -644,14 +643,15 @@ usm_passwd2key(const EVP_MD *md, char *passwd, int *maxlen) /* Localize the key */ #ifdef DEBUG - assert(env->sc_engineid_len <= SNMPD_MAXENGINEIDLEN); + assert(snmpd_env->sc_engineid_len <= SNMPD_MAXENGINEIDLEN); #endif memcpy(pwbuf, keybuf, dlen); - memcpy(pwbuf + dlen, env->sc_engineid, env->sc_engineid_len); - memcpy(pwbuf + dlen + env->sc_engineid_len, keybuf, dlen); + memcpy(pwbuf + dlen, snmpd_env->sc_engineid, + snmpd_env->sc_engineid_len); + memcpy(pwbuf + dlen + snmpd_env->sc_engineid_len, keybuf, dlen); EVP_DigestInit(&ctx, md); - EVP_DigestUpdate(&ctx, pwbuf, 2 * dlen + env->sc_engineid_len); + EVP_DigestUpdate(&ctx, pwbuf, 2 * dlen + snmpd_env->sc_engineid_len); EVP_DigestFinal(&ctx, keybuf, &dlen); EVP_MD_CTX_cleanup(&ctx); |