~ubuntu-branches/ubuntu/gutsy/net-snmp/gutsy-security

« back to all changes in this revision

Viewing changes to agent/mibgroup/utilities/override.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2004-09-13 12:06:21 UTC
  • Revision ID: james.westby@ubuntu.com-20040913120621-g952ntonlleihcvm
Tags: upstream-5.1.1
Import upstream version 5.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/** allows overriding of a given oid with a new type and value */
 
2
 
 
3
/* Portions of this file are subject to the following copyright(s).  See
 
4
 * the Net-SNMP's COPYING file for more details and other copyrights
 
5
 * that may apply:
 
6
 */
 
7
/*
 
8
 * Portions of this file are copyrighted by:
 
9
 * Copyright � 2003 Sun Microsystems, Inc. All rights reserved.
 
10
 * Use is subject to license terms specified in the COPYING file
 
11
 * distributed with the Net-SNMP package.
 
12
 */
 
13
 
 
14
#include <net-snmp/net-snmp-config.h>
 
15
#include <net-snmp/net-snmp-includes.h>
 
16
#include <net-snmp/agent/net-snmp-agent-includes.h>
 
17
 
 
18
#include "util_funcs.h"
 
19
 
 
20
typedef struct override_data_s {
 
21
    int             type;
 
22
    void           *value;
 
23
    size_t          value_len;
 
24
} override_data;
 
25
 
 
26
/** @todo: (optionally) save values persistently when configured for
 
27
 *  read-write */
 
28
int
 
29
override_handler(netsnmp_mib_handler *handler,
 
30
                 netsnmp_handler_registration *reginfo,
 
31
                 netsnmp_agent_request_info *reqinfo,
 
32
                 netsnmp_request_info *requests)
 
33
{
 
34
 
 
35
    override_data  *data = handler->myvoid;
 
36
    if (!data) {
 
37
        netsnmp_set_request_error(reqinfo, requests, SNMP_ERR_GENERR);
 
38
        return SNMP_ERR_NOERROR;
 
39
    }
 
40
 
 
41
    switch (reqinfo->mode) {
 
42
    case MODE_GET:
 
43
        DEBUGMSGTL(("override", "overriding oid "));
 
44
        DEBUGMSGOID(("override", requests->requestvb->name,
 
45
                     requests->requestvb->name_length));
 
46
        DEBUGMSG(("override", "\n"));
 
47
        snmp_set_var_typed_value(requests->requestvb, (u_char)data->type,
 
48
                                 (u_char *) data->value, data->value_len);
 
49
        break;
 
50
 
 
51
    default:
 
52
        snmp_log(LOG_ERR, "unsupported mode in override handler\n");
 
53
        netsnmp_set_request_error(reqinfo, requests, SNMP_ERR_GENERR);
 
54
        return SNMP_ERR_GENERR;
 
55
    }
 
56
    return SNMP_ERR_NOERROR;
 
57
}
 
58
 
 
59
#define MALLOC_OR_DIE(x) \
 
60
  thedata->value = malloc(x); \
 
61
  thedata->value_len = x; \
 
62
  if (!thedata->value) { \
 
63
      free(thedata); \
 
64
      config_perror("memory allocation failure"); \
 
65
      return; \
 
66
  }
 
67
 
 
68
void
 
69
netsnmp_parse_override(const char *token, char *line)
 
70
{
 
71
    char           *cp;
 
72
    char            buf[SNMP_MAXBUF];
 
73
    int             readwrite = 0;
 
74
    oid             oidbuf[MAX_OID_LEN];
 
75
    size_t          oidbuf_len = sizeof(oidbuf);
 
76
    int             type;
 
77
    override_data  *thedata;
 
78
    netsnmp_handler_registration *the_reg;
 
79
 
 
80
    cp = copy_nword(line, buf, sizeof(buf) - 1);
 
81
    if (strcmp(buf, "-rw") == 0) {
 
82
        readwrite = 1;
 
83
        cp = copy_nword(cp, buf, sizeof(buf) - 1);
 
84
    }
 
85
 
 
86
    if (!cp) {
 
87
        config_perror("no oid specified");
 
88
        return;
 
89
    }
 
90
 
 
91
    if (!snmp_parse_oid(buf, oidbuf, &oidbuf_len)) {
 
92
        config_perror("illegal oid");
 
93
        return;
 
94
    }
 
95
    cp = copy_nword(cp, buf, sizeof(buf) - 1);
 
96
 
 
97
    if (!cp && strcmp(buf, "null") != 0) {
 
98
        config_perror("no variable value specified");
 
99
        return;
 
100
    }
 
101
 
 
102
    type = se_find_value_in_slist("asntypes", buf);
 
103
    if (!type) {
 
104
        config_perror("unknown type specified");
 
105
        return;
 
106
    }
 
107
 
 
108
    if (cp)
 
109
        copy_nword(cp, buf, sizeof(buf) - 1);
 
110
    else
 
111
        buf[0] = 0;
 
112
 
 
113
    thedata = SNMP_MALLOC_TYPEDEF(override_data);
 
114
    thedata->type = type;
 
115
    if (!thedata) {
 
116
        config_perror("memory allocation failure");
 
117
        return;
 
118
    }
 
119
 
 
120
    switch (type) {
 
121
    case ASN_INTEGER:
 
122
        MALLOC_OR_DIE(sizeof(long));
 
123
        *((long *) thedata->value) = strtol(buf, NULL, 0);
 
124
        break;
 
125
 
 
126
    case ASN_COUNTER:
 
127
    case ASN_UNSIGNED:
 
128
        MALLOC_OR_DIE(sizeof(u_long));
 
129
        *((u_long *) thedata->value) = strtoul(buf, NULL, 0);
 
130
        break;
 
131
 
 
132
    case ASN_OCTET_STR:
 
133
    case ASN_BIT_STR:
 
134
        if (buf[0] == '0' && buf[1] == 'x') {
 
135
            /*
 
136
             * hex 
 
137
             */
 
138
            thedata->value_len =
 
139
                hex_to_binary2(buf + 2, strlen(buf) - 2,
 
140
                               (char **) &thedata->value);
 
141
        } else {
 
142
            thedata->value = strdup(buf);
 
143
            thedata->value_len = strlen(buf);
 
144
        }
 
145
        break;
 
146
 
 
147
    case ASN_OBJECT_ID:
 
148
        read_config_read_objid(buf, (oid **) & thedata->value,
 
149
                               &thedata->value_len);
 
150
        break;
 
151
 
 
152
    case ASN_NULL:
 
153
        thedata->value_len = 0;
 
154
        break;
 
155
 
 
156
    default:
 
157
        config_perror("illegal/unsupported type specified");
 
158
        return;
 
159
    }
 
160
 
 
161
    if (!thedata->value && thedata->type != ASN_NULL) {
 
162
        config_perror("memory allocation failure");
 
163
        free(thedata);
 
164
        return;
 
165
    }
 
166
 
 
167
    the_reg = SNMP_MALLOC_TYPEDEF(netsnmp_handler_registration);
 
168
    if (!the_reg) {
 
169
        config_perror("memory allocation failure");
 
170
        free(thedata);
 
171
        return;
 
172
    }
 
173
 
 
174
    the_reg->priority = 255;
 
175
    the_reg->modes = (readwrite) ? HANDLER_CAN_RWRITE : HANDLER_CAN_RONLY;
 
176
    the_reg->handler =
 
177
        netsnmp_create_handler("override", override_handler);
 
178
    memdup((u_char **) & the_reg->rootoid, (const u_char *) oidbuf,
 
179
           oidbuf_len * sizeof(oid));
 
180
    the_reg->rootoid_len = oidbuf_len;
 
181
    if (!the_reg->rootoid) {
 
182
        config_perror("memory allocation failure");
 
183
        free(thedata);
 
184
        return;
 
185
    }
 
186
    the_reg->handler->myvoid = thedata;
 
187
 
 
188
    if (netsnmp_register_instance(the_reg)) {
 
189
        config_perror("oid registration failed within the agent");
 
190
        SNMP_FREE(thedata->value);
 
191
        free(thedata);
 
192
        return;
 
193
    }
 
194
}
 
195
 
 
196
 
 
197
void
 
198
init_override(void)
 
199
{
 
200
 
 
201
    snmpd_register_config_handler("override", netsnmp_parse_override, NULL,     /* XXX: free func */
 
202
                                  "[-rw] mibnode type value");
 
203
}