summaryrefslogtreecommitdiff
path: root/usr.sbin/snmpd/application_blocklist.c
blob: 354908d5068cbc0a62e40d4cc5f7a625f743dbda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*	$OpenBSD: application_blocklist.c,v 1.2 2023/12/21 12:43:31 martijn Exp $	*/

/*
 * Copyright (c) 2022 Martijn van Duren <martijn@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/types.h>

#include <stddef.h>

#include <ber.h>
#include <stdint.h>
#include <stdlib.h>

#include "application.h"
#include "log.h"
#include "snmpd.h"

struct appl_varbind *appl_blocklist_response(size_t);
void appl_blocklist_get(struct appl_backend *, int32_t, int32_t, const char *,
    struct appl_varbind *);
void appl_blocklist_getnext(struct appl_backend *, int32_t, int32_t,
    const char *, struct appl_varbind *);

struct appl_backend_functions appl_blocklist_functions = {
	.ab_get = appl_blocklist_get,
	.ab_getnext = appl_blocklist_getnext,
	.ab_getbulk = NULL,
};

struct appl_backend appl_blocklist = {
	.ab_name = "blocklist",
	.ab_cookie = NULL,
	.ab_retries = 0,
	.ab_fn = &appl_blocklist_functions
};

static struct appl_varbind *response = NULL;
static size_t responsesz = 0;

struct appl_varbind *
appl_blocklist_response(size_t nvarbind)
{
	struct appl_varbind *tmp;
	size_t i;

	if (responsesz < nvarbind) {
		if ((tmp = recallocarray(response, responsesz, nvarbind,
		    sizeof(*response))) == NULL) {
			log_warn(NULL);
			return NULL;
		}
		responsesz = nvarbind;
		response = tmp;
	}
	for (i = 0; i < nvarbind; i++)
		response[i].av_next = i + 1 == nvarbind ?
		    NULL : &(response[i + 1]);
	return response;
}

void
appl_blocklist_init(void)
{
	extern struct snmpd *snmpd_env;
	size_t i;

	for (i = 0; i < snmpd_env->sc_nblocklist; i++)
		appl_register(NULL, 150, 1, &(snmpd_env->sc_blocklist[i]),
		    0, 1, 0, 0, &appl_blocklist);
}

void
appl_blocklist_shutdown(void)
{
	appl_close(&appl_blocklist);
	free(response);
}

void
appl_blocklist_get(struct appl_backend *backend, __unused int32_t transactionid,
    int32_t requestid, __unused const char *ctx, struct appl_varbind *vblist)
{
	struct appl_varbind *vb, *rvb, *rvblist;
	size_t i;

	for (i = 0, vb = vblist; vb != NULL; vb = vb->av_next)
		i++;
	if ((rvblist = appl_blocklist_response(i)) == NULL)
		goto fail;
	rvb = rvblist;
	for (vb = vblist; vb != NULL; vb = vb->av_next, rvb = rvb->av_next) {
		rvb->av_oid = vb->av_oid;
		rvb->av_value = appl_exception(APPL_EXC_NOSUCHOBJECT);
		if (rvb->av_value == NULL)
			goto fail;
	}

	appl_response(backend, requestid, APPL_ERROR_NOERROR, 0, rvblist);
	return;
 fail:
	for (rvb = rvblist; rvb != NULL && rvb->av_value != NULL;
	    rvb = rvb->av_next)
		ober_free_elements(rvb->av_value);
	appl_response(backend, requestid, APPL_ERROR_GENERR, 1, vb);
}

void
appl_blocklist_getnext(struct appl_backend *backend,
    __unused int32_t transactionid, int32_t requestid, __unused const char *ctx,
    struct appl_varbind *vblist)
{
	struct appl_varbind *vb, *rvb, *rvblist;
	size_t i;

	for (i = 0, vb = vblist; vb != NULL; vb = vb->av_next)
		i++;
	if ((rvblist = appl_blocklist_response(i)) == NULL)
		goto fail;
	rvb = rvblist;
	for (vb = vblist; vb != NULL; vb = vb->av_next, rvb = rvb->av_next) {
		rvb->av_oid = vb->av_oid;
		rvb->av_value = appl_exception(APPL_EXC_ENDOFMIBVIEW);
		if (rvb->av_value == NULL)
			goto fail;
	}

	appl_response(backend, requestid, APPL_ERROR_NOERROR, 0, rvblist);
	return;
 fail:
	for (rvb = rvblist; rvb != NULL && rvb->av_value != NULL;
	    rvb = rvb->av_next)
		ober_free_elements(rvb->av_value);
	appl_response(backend, requestid, APPL_ERROR_GENERR, 1, vb);
}