| 1 |
#include <net-snmp/net-snmp-config.h> |
| 2 |
#include <net-snmp/net-snmp-features.h> |
| 3 |
|
| 4 |
#ifdef HAVE_STDLIB_H |
| 5 |
#include <stdlib.h> |
| 6 |
#endif |
| 7 |
#include <stdio.h> |
| 8 |
#ifdef HAVE_STRING_H |
| 9 |
#include <string.h> |
| 10 |
#else |
| 11 |
#include <strings.h> |
| 12 |
#endif |
| 13 |
#ifdef HAVE_UNISTD_H |
| 14 |
#include <unistd.h> |
| 15 |
#endif |
| 16 |
#include <ctype.h> |
| 17 |
#include <sys/types.h> |
| 18 |
#ifdef HAVE_NETINET_IN_H |
| 19 |
#include <netinet/in.h> |
| 20 |
#endif |
| 21 |
#ifdef HAVE_SYS_WAIT_H |
| 22 |
# include <sys/wait.h> |
| 23 |
#endif |
| 24 |
#ifdef HAVE_LIMITS_H |
| 25 |
#include <limits.h> |
| 26 |
#endif |
| 27 |
#ifdef WIN32 |
| 28 |
#include <limits.h> |
| 29 |
#endif |
| 30 |
|
| 31 |
#include <net-snmp/net-snmp-includes.h> |
| 32 |
#include <net-snmp/agent/net-snmp-agent-includes.h> |
| 33 |
|
| 34 |
#include "struct.h" |
| 35 |
#include "pass.h" |
| 36 |
#include "pass_common.h" |
| 37 |
#include "extensible.h" |
| 38 |
#include "util_funcs.h" |
| 39 |
|
| 40 |
netsnmp_feature_require(get_exten_instance); |
| 41 |
netsnmp_feature_require(parse_miboid); |
| 42 |
|
| 43 |
struct extensible *passthrus = NULL; |
| 44 |
int numpassthrus = 0; |
| 45 |
|
| 46 |
/* |
| 47 |
* the relocatable extensible commands variables |
| 48 |
*/ |
| 49 |
struct variable2 extensible_passthru_variables[] = { |
| 50 |
/* |
| 51 |
* bogus entry. Only some of it is actually used. |
| 52 |
*/ |
| 53 |
{MIBINDEX, ASN_INTEGER, NETSNMP_OLDAPI_RWRITE, |
| 54 |
var_extensible_pass, 0, {MIBINDEX}}, |
| 55 |
}; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
void |
| 60 |
init_pass(void) |
| 61 |
{ |
| 62 |
snmpd_register_config_handler("pass", pass_parse_config, |
| 63 |
pass_free_config, "miboid command"); |
| 64 |
} |
| 65 |
|
| 66 |
void |
| 67 |
pass_parse_config(const char *token, char *cptr) |
| 68 |
{ |
| 69 |
struct extensible **ppass = &passthrus, **etmp, *ptmp; |
| 70 |
char *tcptr, *endopt; |
| 71 |
int i; |
| 72 |
unsigned long priority; |
| 73 |
|
| 74 |
/* |
| 75 |
* options |
| 76 |
*/ |
| 77 |
priority = DEFAULT_MIB_PRIORITY; |
| 78 |
while (*cptr == '-') { |
| 79 |
cptr++; |
| 80 |
switch (*cptr) { |
| 81 |
case 'p': |
| 82 |
/* change priority level */ |
| 83 |
cptr++; |
| 84 |
cptr = skip_white(cptr); |
| 85 |
if (! isdigit((unsigned char)(*cptr))) { |
| 86 |
config_perror("priority must be an integer"); |
| 87 |
return; |
| 88 |
} |
| 89 |
priority = strtol((const char*) cptr, &endopt, 0); |
| 90 |
if ((priority == LONG_MIN) || (priority == LONG_MAX)) { |
| 91 |
config_perror("priority under/overflow"); |
| 92 |
return; |
| 93 |
} |
| 94 |
cptr = endopt; |
| 95 |
cptr = skip_white(cptr); |
| 96 |
break; |
| 97 |
default: |
| 98 |
config_perror("unknown option for pass directive"); |
| 99 |
return; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
/* |
| 104 |
* MIB |
| 105 |
*/ |
| 106 |
if (*cptr == '.') |
| 107 |
cptr++; |
| 108 |
if (!isdigit((unsigned char)(*cptr))) { |
| 109 |
config_perror("second token is not a OID"); |
| 110 |
return; |
| 111 |
} |
| 112 |
numpassthrus++; |
| 113 |
|
| 114 |
while (*ppass != NULL) |
| 115 |
ppass = &((*ppass)->next); |
| 116 |
*ppass = calloc(1, sizeof(**ppass)); |
| 117 |
if (*ppass == NULL) |
| 118 |
return; |
| 119 |
(*ppass)->type = PASSTHRU; |
| 120 |
(*ppass)->mibpriority = priority; |
| 121 |
|
| 122 |
(*ppass)->miblen = parse_miboid(cptr, (*ppass)->miboid); |
| 123 |
while (isdigit((unsigned char)(*cptr)) || *cptr == '.') |
| 124 |
cptr++; |
| 125 |
/* |
| 126 |
* path |
| 127 |
*/ |
| 128 |
free((*ppass)->command); |
| 129 |
(*ppass)->command = NULL; |
| 130 |
cptr = skip_white(cptr); |
| 131 |
if (cptr == NULL) { |
| 132 |
config_perror("No command specified on pass line"); |
| 133 |
if (asprintf(&(*ppass)->command, "%s", "") < 0) { |
| 134 |
} |
| 135 |
} else { |
| 136 |
for (tcptr = cptr; *tcptr != 0 && *tcptr != '#' && *tcptr != ';'; |
| 137 |
tcptr++); |
| 138 |
if (asprintf(&(*ppass)->command, "%.*s", (int) (tcptr - cptr), cptr) |
| 139 |
< 0) { |
| 140 |
} |
| 141 |
} |
| 142 |
strlcpy((*ppass)->name, (*ppass)->command, sizeof((*ppass)->name)); |
| 143 |
(*ppass)->next = NULL; |
| 144 |
|
| 145 |
register_mib_priority("pass", |
| 146 |
(struct variable *) extensible_passthru_variables, |
| 147 |
sizeof(struct variable2), 1, (*ppass)->miboid, |
| 148 |
(*ppass)->miblen, (*ppass)->mibpriority); |
| 149 |
|
| 150 |
/* |
| 151 |
* argggg -- passthrus must be sorted |
| 152 |
*/ |
| 153 |
if (numpassthrus > 1) { |
| 154 |
etmp = (struct extensible **) |
| 155 |
malloc(((sizeof(struct extensible *)) * numpassthrus)); |
| 156 |
if (etmp == NULL) |
| 157 |
return; |
| 158 |
|
| 159 |
for (i = 0, ptmp = (struct extensible *) passthrus; |
| 160 |
i < numpassthrus && ptmp != NULL; i++, ptmp = ptmp->next) |
| 161 |
etmp[i] = ptmp; |
| 162 |
qsort(etmp, numpassthrus, sizeof(struct extensible *), |
| 163 |
pass_compare); |
| 164 |
passthrus = (struct extensible *) etmp[0]; |
| 165 |
ptmp = (struct extensible *) etmp[0]; |
| 166 |
|
| 167 |
for (i = 0; i < numpassthrus - 1; i++) { |
| 168 |
ptmp->next = etmp[i + 1]; |
| 169 |
ptmp = ptmp->next; |
| 170 |
} |
| 171 |
ptmp->next = NULL; |
| 172 |
free(etmp); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
void |
| 177 |
pass_free_config(void) |
| 178 |
{ |
| 179 |
struct extensible *etmp, *etmp2; |
| 180 |
|
| 181 |
for (etmp = passthrus; etmp != NULL;) { |
| 182 |
etmp2 = etmp; |
| 183 |
etmp = etmp->next; |
| 184 |
unregister_mib_priority(etmp2->miboid, etmp2->miblen, etmp2->mibpriority); |
| 185 |
free(etmp2->command); |
| 186 |
free(etmp2); |
| 187 |
} |
| 188 |
passthrus = NULL; |
| 189 |
numpassthrus = 0; |
| 190 |
} |
| 191 |
|
| 192 |
u_char * |
| 193 |
var_extensible_pass(struct variable *vp, |
| 194 |
oid * name, |
| 195 |
size_t * length, |
| 196 |
int exact, |
| 197 |
size_t * var_len, WriteMethod ** write_method) |
| 198 |
{ |
| 199 |
oid newname[MAX_OID_LEN]; |
| 200 |
int i, rtest, fd, newlen; |
| 201 |
char buf[SNMP_MAXBUF]; |
| 202 |
static char buf2[SNMP_MAXBUF]; |
| 203 |
struct extensible *passthru; |
| 204 |
FILE *file; |
| 205 |
|
| 206 |
for (i = 1; i <= numpassthrus; i++) { |
| 207 |
passthru = get_exten_instance(passthrus, i); |
| 208 |
rtest = snmp_oidtree_compare(name, *length, |
| 209 |
passthru->miboid, passthru->miblen); |
| 210 |
if ((exact && rtest == 0) || (!exact && rtest <= 0)) { |
| 211 |
/* |
| 212 |
* setup args |
| 213 |
*/ |
| 214 |
if (passthru->miblen >= *length || rtest < 0) |
| 215 |
sprint_mib_oid(buf, passthru->miboid, passthru->miblen); |
| 216 |
else |
| 217 |
sprint_mib_oid(buf, name, *length); |
| 218 |
free(passthru->command); |
| 219 |
passthru->command = NULL; |
| 220 |
if (asprintf(&passthru->command, "%s %s %s", passthru->name, |
| 221 |
exact ? "-g" : "-n", buf) < 0) { |
| 222 |
} |
| 223 |
DEBUGMSGTL(("ucd-snmp/pass", "pass-running: %s\n", |
| 224 |
passthru->command)); |
| 225 |
/* |
| 226 |
* valid call. Exec and get output |
| 227 |
*/ |
| 228 |
if ((fd = get_exec_output(passthru)) != -1) { |
| 229 |
file = fdopen(fd, "r"); |
| 230 |
if (fgets(buf, sizeof(buf), file) == NULL) { |
| 231 |
fclose(file); |
| 232 |
wait_on_exec(passthru); |
| 233 |
if (exact) { |
| 234 |
/* |
| 235 |
* to enable creation |
| 236 |
*/ |
| 237 |
*write_method = setPass; |
| 238 |
*var_len = 0; |
| 239 |
return (NULL); |
| 240 |
} |
| 241 |
continue; |
| 242 |
} |
| 243 |
newlen = parse_miboid(buf, newname); |
| 244 |
|
| 245 |
/* |
| 246 |
* its good, so copy onto name/length |
| 247 |
*/ |
| 248 |
memcpy((char *) name, (char *) newname, |
| 249 |
(int) newlen * sizeof(oid)); |
| 250 |
*length = newlen; |
| 251 |
|
| 252 |
/* |
| 253 |
* set up return pointer for setable stuff |
| 254 |
*/ |
| 255 |
*write_method = setPass; |
| 256 |
|
| 257 |
if (newlen == 0 || fgets(buf, sizeof(buf), file) == NULL |
| 258 |
|| fgets(buf2, sizeof(buf2), file) == NULL) { |
| 259 |
*var_len = 0; |
| 260 |
fclose(file); |
| 261 |
wait_on_exec(passthru); |
| 262 |
return (NULL); |
| 263 |
} |
| 264 |
fclose(file); |
| 265 |
wait_on_exec(passthru); |
| 266 |
|
| 267 |
return netsnmp_internal_pass_parse(buf, buf2, var_len, vp); |
| 268 |
} |
| 269 |
*var_len = 0; |
| 270 |
return (NULL); |
| 271 |
} |
| 272 |
} |
| 273 |
if (var_len) |
| 274 |
*var_len = 0; |
| 275 |
*write_method = NULL; |
| 276 |
return (NULL); |
| 277 |
} |
| 278 |
|
| 279 |
int |
| 280 |
setPass(int action, u_char * var_val, u_char var_val_type, |
| 281 |
size_t var_val_len, u_char * statP, oid * name, size_t name_len) |
| 282 |
{ |
| 283 |
int i, rtest; |
| 284 |
struct extensible *passthru; |
| 285 |
char buf[SNMP_MAXBUF], buf2[SNMP_MAXBUF]; |
| 286 |
|
| 287 |
for (i = 1; i <= numpassthrus; i++) { |
| 288 |
passthru = get_exten_instance(passthrus, i); |
| 289 |
rtest = snmp_oidtree_compare(name, name_len, |
| 290 |
passthru->miboid, passthru->miblen); |
| 291 |
if (rtest <= 0) { |
| 292 |
if (action != ACTION) |
| 293 |
return SNMP_ERR_NOERROR; |
| 294 |
/* |
| 295 |
* setup args |
| 296 |
*/ |
| 297 |
free(passthru->command); |
| 298 |
passthru->command = NULL; |
| 299 |
if (passthru->miblen >= name_len || rtest < 0) |
| 300 |
sprint_mib_oid(buf, passthru->miboid, passthru->miblen); |
| 301 |
else |
| 302 |
sprint_mib_oid(buf, name, name_len); |
| 303 |
netsnmp_internal_pass_set_format(buf2, var_val, var_val_type, |
| 304 |
var_val_len); |
| 305 |
if (asprintf(&passthru->command, "%s -s %s %s", passthru->name, buf, |
| 306 |
buf2) < 0) { |
| 307 |
} |
| 308 |
DEBUGMSGTL(("ucd-snmp/pass", "pass-running: %s", |
| 309 |
passthru->command)); |
| 310 |
exec_command(passthru); |
| 311 |
DEBUGMSGTL(("ucd-snmp/pass", "pass-running returned: %s", |
| 312 |
passthru->output)); |
| 313 |
return netsnmp_internal_pass_str_to_errno(passthru->output); |
| 314 |
} |
| 315 |
} |
| 316 |
if (snmp_get_do_debugging()) { |
| 317 |
sprint_mib_oid(buf2, name, name_len); |
| 318 |
DEBUGMSGTL(("ucd-snmp/pass", "pass-notfound: %s\n", buf2)); |
| 319 |
} |
| 320 |
return SNMP_ERR_NOSUCHNAME; |
| 321 |
} |
| 322 |
|
| 323 |
int |
| 324 |
pass_compare(const void *a, const void *b) |
| 325 |
{ |
| 326 |
const struct extensible *const *ap = a; |
| 327 |
const struct extensible *const *bp = b; |
| 328 |
|
| 329 |
return snmp_oid_compare((*ap)->miboid, (*ap)->miblen, (*bp)->miboid, |
| 330 |
(*bp)->miblen); |
| 331 |
} |