Debian Codegraph @

agent/mibgroup/ucd-snmp/extensible.c

raw
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 #ifdef HAVE_UNISTD_H
8 #include <unistd.h>
9 #endif
10 #ifdef HAVE_FCNTL_H
11 #include <fcntl.h>
12 #endif
13 #ifdef TIME_WITH_SYS_TIME
14 # include <sys/time.h>
15 # include <time.h>
16 #else
17 # ifdef HAVE_SYS_TIME_H
18 # include <sys/time.h>
19 # else
20 # include <time.h>
21 # endif
22 #endif
23 #include <signal.h>
24 #ifdef HAVE_MACHINE_PARAM_H
25 #include <machine/param.h>
26 #endif
27 #ifdef HAVE_SYS_PARAM_H
28 #include <sys/param.h>
29 #endif
30 #ifdef HAVE_SYS_VMMETER_H
31 #if !(defined(bsdi2) || defined(netbsd1))
32 #include <sys/vmmeter.h>
33 #endif
34 #endif
35 #ifdef HAVE_SYS_CONF_H
36 #include <sys/conf.h>
37 #endif
38 #ifdef HAVE_ASM_PAGE_H
39 #include <asm/page.h>
40 #endif
41 #ifdef HAVE_SYS_SWAP_H
42 #include <sys/swap.h>
43 #endif
44 #ifdef HAVE_SYS_FS_H
45 #include <sys/fs.h>
46 #else
47 #ifdef HAVE_UFS_FS_H
48 #include <ufs/fs.h>
49 #else
50 #ifdef HAVE_UFS_UFS_DINODE_H
51 #include <ufs/ufs/dinode.h>
52 #endif
53 #ifdef HAVE_UFS_FFS_FS_H
54 #include <ufs/ffs/fs.h>
55 #endif
56 #endif
57 #endif
58 #ifdef HAVE_MTAB_H
59 #include <mtab.h>
60 #endif
61 #include <sys/stat.h>
62 #include <errno.h>
63 #ifdef HAVE_FSTAB_H
64 #include <fstab.h>
65 #endif
66 #ifdef HAVE_SYS_STATFS_H
67 #include <sys/statfs.h>
68 #endif
69 #ifdef HAVE_SYS_STATVFS_H
70 #include <sys/statvfs.h>
71 #endif
72 #ifdef HAVE_SYS_VFS_H
73 #include <sys/vfs.h>
74 #endif
75 #if (!defined(HAVE_STATVFS)) && defined(HAVE_STATFS)
76 #ifdef HAVE_SYS_MOUNT_H
77 #include <sys/mount.h>
78 #endif
79 #ifdef HAVE_SYS_SYSCTL_H
80 #include <sys/sysctl.h>
81 #endif
82 #define statvfs statfs
83 #endif
84 #ifdef HAVE_VM_VM_H
85 #include <vm/vm.h>
86 #endif
87 #ifdef HAVE_VM_SWAP_PAGER_H
88 #include <vm/swap_pager.h>
89 #endif
90 #ifdef HAVE_SYS_FIXPOINT_H
91 #include <sys/fixpoint.h>
92 #endif
93 #ifdef HAVE_MALLOC_H
94 #include <malloc.h>
95 #endif
96 #ifdef HAVE_STRING_H
97 #include <string.h>
98 #endif
99 #include <ctype.h>
100
101 #include <net-snmp/net-snmp-includes.h>
102 #include <net-snmp/agent/net-snmp-agent-includes.h>
103 #include <net-snmp/agent/auto_nlist.h>
104 #include <net-snmp/agent/agent_callbacks.h>
105 #include <net-snmp/library/system.h>
106
107 #include "struct.h"
108 #include "extensible.h"
109 #include "pass.h"
110 #include "mibgroup/util_funcs.h"
111 #include "utilities/execute.h"
112 #include "util_funcs/header_simple_table.h"
113
114 netsnmp_feature_require(get_exten_instance);
115 netsnmp_feature_require(parse_miboid);
116
117 /*
118 * the relocatable extensible commands variables
119 */
120 struct variable2 extensible_relocatable_variables[] = {
121 {MIBINDEX, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
122 var_extensible_relocatable, 1, {MIBINDEX}},
123 {ERRORNAME, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY,
124 var_extensible_relocatable, 1, {ERRORNAME}},
125 {SHELLCOMMAND, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY,
126 var_extensible_relocatable, 1, {SHELLCOMMAND}},
127 {ERRORFLAG, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
128 var_extensible_relocatable, 1, {ERRORFLAG}},
129 {ERRORMSG, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY,
130 var_extensible_relocatable, 1, {ERRORMSG}},
131 {ERRORFIX, ASN_INTEGER, NETSNMP_OLDAPI_RWRITE,
132 var_extensible_relocatable, 1, {ERRORFIX}},
133 {ERRORFIXCMD, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY,
134 var_extensible_relocatable, 1, {ERRORFIXCMD}}
135 };
136
137
138 void
139 init_extensible(void)
140 {
141
142 struct variable2 extensible_extensible_variables[] = {
143 {MIBINDEX, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
144 var_extensible_shell, 1, {MIBINDEX}},
145 {ERRORNAME, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY,
146 var_extensible_shell, 1, {ERRORNAME}},
147 {SHELLCOMMAND, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY,
148 var_extensible_shell, 1, {SHELLCOMMAND}},
149 {ERRORFLAG, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
150 var_extensible_shell, 1, {ERRORFLAG}},
151 {ERRORMSG, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY,
152 var_extensible_shell, 1, {ERRORMSG}},
153 {ERRORFIX, ASN_INTEGER, NETSNMP_OLDAPI_RWRITE,
154 var_extensible_shell, 1, {ERRORFIX}},
155 {ERRORFIXCMD, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY,
156 var_extensible_shell, 1, {ERRORFIXCMD}}
157 };
158
159 /*
160 * Define the OID pointer to the top of the mib tree that we're
161 * registering underneath
162 */
163 oid extensible_variables_oid[] =
164 { NETSNMP_UCDAVIS_MIB, NETSNMP_SHELLMIBNUM, 1 };
165
166 /*
167 * register ourselves with the agent to handle our mib tree
168 */
169 REGISTER_MIB("ucd-snmp/extensible", extensible_extensible_variables,
170 variable2, extensible_variables_oid);
171
172 snmpd_register_config_handler("exec", extensible_parse_config,
173 extensible_free_config,
174 "[miboid] name program arguments");
175 snmpd_register_config_handler("sh", extensible_parse_config,
176 extensible_free_config,
177 "[miboid] name program-or-script arguments");
178 snmpd_register_config_handler("execfix", execfix_parse_config, NULL,
179 "exec-or-sh-name program [arguments...]");
180 snmp_register_callback(SNMP_CALLBACK_APPLICATION,
181 SNMPD_CALLBACK_PRE_UPDATE_CONFIG,
182 extensible_unregister, NULL);
183 }
184
185 void
186 extensible_parse_config(const char *token, char *cptr)
187 {
188 struct extensible *ptmp, **pp;
189 char *tcptr;
190 int scount;
191
192 /*
193 * allocate and clear memory structure
194 */
195 ptmp = (struct extensible *) calloc(1, sizeof(struct extensible));
196 if (ptmp == NULL)
197 return; /* XXX memory alloc error */
198
199 if (*cptr == '.')
200 cptr++;
201 if (isdigit((unsigned char) *cptr)) {
202 /*
203 * its a relocatable extensible mib
204 */
205 config_perror("WARNING: This output format is not valid, and is only retained for backward compatibility - Please consider using the 'extend' directive instead" );
206 for (pp = &relocs, numrelocs++; *pp; pp = &((*pp)->next));
207 (*pp) = ptmp;
208 pp = &relocs; scount = numrelocs;
209
210 } else {
211 /*
212 * it goes in with the general extensible table
213 */
214 for (pp = &extens, numextens++; *pp; pp = &((*pp)->next));
215 (*pp) = ptmp;
216 pp = &extens; scount = numextens;
217 }
218
219 /*
220 * the rest is pretty much handled the same
221 */
222 if (!strncasecmp(token, "sh", 2))
223 ptmp->type = SHPROC;
224 else
225 ptmp->type = EXECPROC;
226 if (isdigit((unsigned char) *cptr)) {
227 ptmp->miblen = parse_miboid(cptr, ptmp->miboid);
228 while (isdigit((unsigned char) *cptr) || *cptr == '.')
229 cptr++;
230 }
231
232 /*
233 * name
234 */
235 cptr = skip_white(cptr);
236 copy_nword(cptr, ptmp->name, sizeof(ptmp->name));
237 cptr = skip_not_white(cptr);
238 cptr = skip_white(cptr);
239 /*
240 * command
241 */
242 if (cptr == NULL) {
243 config_perror("No command specified on line");
244 } else {
245 /*
246 * Support multi-element commands in shell configuration
247 * lines, but truncate after the first command for 'exec'
248 */
249 for (tcptr = cptr; *tcptr != 0 && *tcptr != '#'; tcptr++)
250 if (*tcptr == ';' && ptmp->type == EXECPROC)
251 break;
252 free(ptmp->command);
253 if (asprintf(&ptmp->command, "%.*s", (int) (tcptr - cptr), cptr) < 0)
254 ptmp->command = NULL;
255 }
256 #ifdef NETSNMP_EXECFIXCMD
257 sprintf(ptmp->fixcmd, NETSNMP_EXECFIXCMD, ptmp->name);
258 #endif
259 if (ptmp->miblen > 0) {
260 /*
261 * For relocatable "exec" entries,
262 * register the new (not-strictly-valid) MIB subtree...
263 */
264 register_mib(token,
265 (struct variable *) extensible_relocatable_variables,
266 sizeof(struct variable2),
267 sizeof(extensible_relocatable_variables) /
268 sizeof(*extensible_relocatable_variables),
269 ptmp->miboid, ptmp->miblen);
270
271 /*
272 * ... and ensure the entries are sorted by OID.
273 * This isn't needed for entries in the main extTable (which
274 * don't have MIB OIDs explicitly associated with them anyway)
275 */
276 if (scount > 1 && pp != &extens) {
277 int i;
278 struct extensible **etmp = (struct extensible **)
279 malloc(((sizeof(struct extensible *)) * scount));
280 if (etmp == NULL)
281 return; /* XXX memory alloc error */
282 for (i = 0, ptmp = *pp;
283 i < scount && ptmp != NULL; i++, ptmp = ptmp->next)
284 etmp[i] = ptmp;
285 qsort(etmp, scount, sizeof(struct extensible *),
286 pass_compare);
287 *pp = (struct extensible *) etmp[0];
288 ptmp = (struct extensible *) etmp[0];
289
290 for (i = 0; i < scount - 1; i++) {
291 ptmp->next = etmp[i + 1];
292 ptmp = ptmp->next;
293 }
294 ptmp->next = NULL;
295 free(etmp);
296 }
297 }
298 }
299
300 int
301 extensible_unregister(int major, int minor,
302 void *serverarg, void *clientarg)
303 {
304 extensible_free_config();
305 return 0;
306 }
307
308 void
309 extensible_free_config(void)
310 {
311 struct extensible *etmp, *etmp2;
312 oid tname[MAX_OID_LEN];
313 int i;
314
315 for (etmp = extens; etmp != NULL;) {
316 etmp2 = etmp;
317 etmp = etmp->next;
318 free(etmp2);
319 }
320
321 for (etmp = relocs; etmp != NULL;) {
322 etmp2 = etmp;
323 etmp = etmp->next;
324
325 /*
326 * The new modular API results in the column
327 * objects being registered individually, so
328 * they need to be unregistered individually too!
329 */
330 memset(tname, 0, MAX_OID_LEN*sizeof(oid));
331 memcpy(tname, etmp2->miboid, etmp2->miblen*sizeof(oid));
332 for (i=1; i<4; i++) {
333 tname[etmp2->miblen] = i;
334 unregister_mib(tname, etmp2->miblen+1);
335 }
336 for (i=100; i<=103; i++) {
337 tname[etmp2->miblen] = i;
338 unregister_mib(tname, etmp2->miblen+1);
339 }
340 free(etmp2);
341 }
342
343 relocs = NULL;
344 extens = NULL;
345 numextens = 0;
346 numrelocs = 0;
347 }
348
349
350 #define MAXMSGLINES 1000
351
352 struct extensible *extens = NULL; /* In exec.c */
353 struct extensible *relocs = NULL; /* In exec.c */
354 int numextens = 0, numrelocs = 0; /* ditto */
355
356
357 /*
358 * var_extensible_shell(...
359 * Arguments:
360 * vp IN - pointer to variable entry that points here
361 * name IN/OUT - IN/name requested, OUT/name found
362 * length IN/OUT - length of IN/OUT oid's
363 * exact IN - TRUE if an exact match was requested
364 * var_len OUT - length of variable or 0 if function returned
365 * write_method
366 *
367 */
368
369 /*
370 * find a give entry in the linked list associated with a proc name
371 */
372 struct extensible *
373 get_exec_by_name(char *name)
374 {
375 struct extensible *etmp;
376
377 if (name == NULL)
378 return NULL;
379
380 for (etmp = extens; etmp != NULL && strcmp(etmp->name, name) != 0;
381 etmp = etmp->next);
382
383 if(NULL == etmp)
384 for (etmp = relocs; etmp != NULL && strcmp(etmp->name, name) != 0;
385 etmp = etmp->next);
386
387 return etmp;
388 }
389
390 void
391 execfix_parse_config(const char *token, char *cptr)
392 {
393 char tmpname[STRMAX];
394 struct extensible *execp;
395
396 /*
397 * don't allow two entries with the same name
398 */
399 cptr = copy_nword(cptr, tmpname, sizeof(tmpname));
400 if ((execp = get_exec_by_name(tmpname)) == NULL) {
401 config_perror("No exec entry registered for this exec name yet.");
402 return;
403 }
404
405 if (strlen(cptr) > sizeof(execp->fixcmd)) {
406 config_perror("fix command too long.");
407 return;
408 }
409
410 strlcpy(execp->fixcmd, cptr, sizeof(execp->fixcmd));
411 }
412
413 u_char *
414 var_extensible_shell(struct variable * vp,
415 oid * name,
416 size_t * length,
417 int exact,
418 size_t * var_len, WriteMethod ** write_method)
419 {
420
421 static struct extensible *exten = NULL;
422 static long long_ret;
423 int len;
424
425 if (header_simple_table
426 (vp, name, length, exact, var_len, write_method, numextens))
427 return (NULL);
428
429 if ((exten = get_exten_instance(extens, name[*length - 1]))) {
430 switch (vp->magic) {
431 case MIBINDEX:
432 long_ret = name[*length - 1];
433 return ((u_char *) (&long_ret));
434 case ERRORNAME: /* name defined in config file */
435 *var_len = strlen(exten->name);
436 return ((u_char *) (exten->name));
437 case SHELLCOMMAND:
438 *var_len = strlen(exten->command);
439 return ((u_char *) (exten->command));
440 case ERRORFLAG: /* return code from the process */
441 len = sizeof(exten->output);
442 if (exten->type == EXECPROC) {
443 exten->result = run_exec_command( exten->command, NULL,
444 exten->output, &len);
445 } else {
446 exten->result = run_shell_command(exten->command, NULL,
447 exten->output, &len);
448 }
449 long_ret = exten->result;
450 return ((u_char *) (&long_ret));
451 case ERRORMSG: /* first line of text returned from the process */
452 len = sizeof(exten->output);
453 if (exten->type == EXECPROC) {
454 exten->result = run_exec_command( exten->command, NULL,
455 exten->output, &len);
456 } else {
457 exten->result = run_shell_command(exten->command, NULL,
458 exten->output, &len);
459 }
460 *var_len = strlen(exten->output);
461 if (*var_len && exten->output[*var_len - 1] == '\n')
462 exten->output[--(*var_len)] = '\0';
463 return ((u_char *) (exten->output));
464 case ERRORFIX:
465 *write_method = fixExecError;
466 long_return = 0;
467 return ((u_char *) & long_return);
468
469 case ERRORFIXCMD:
470 *var_len = strlen(exten->fixcmd);
471 return ((u_char *) exten->fixcmd);
472 }
473 return NULL;
474 }
475 return NULL;
476 }
477
478 int
479 fixExecError(int action,
480 u_char * var_val,
481 u_char var_val_type,
482 size_t var_val_len,
483 u_char * statP, oid * name, size_t name_len)
484 {
485
486 struct extensible *exten;
487 long tmp = 0;
488 int fd;
489 static struct extensible ex;
490 FILE *file;
491
492 if ((exten = get_exten_instance(extens, name[10]))) {
493 if (var_val_type != ASN_INTEGER) {
494 snmp_log(LOG_ERR, "Wrong type != int\n");
495 return SNMP_ERR_WRONGTYPE;
496 }
497 tmp = *((long *) var_val);
498 if ((tmp == 1) && (action == COMMIT) && (exten->fixcmd[0] != 0)) {
499 ex.command = strdup(exten->fixcmd);
500 if ((fd = get_exec_output(&ex)) != -1) {
501 file = fdopen(fd, "r");
502 while (fgets(ex.output, sizeof(ex.output), file) != NULL);
503 fclose(file);
504 wait_on_exec(&ex);
505 }
506 free(ex.command);
507 }
508 return SNMP_ERR_NOERROR;
509 }
510 return SNMP_ERR_WRONGTYPE;
511 }
512
513 u_char *
514 var_extensible_relocatable(struct variable *vp,
515 oid * name,
516 size_t * length,
517 int exact,
518 size_t * var_len, WriteMethod ** write_method)
519 {
520
521 int i;
522 int len;
523 struct extensible *exten = NULL;
524 static long long_ret;
525 static char errmsg[STRMAX];
526 char *cp, *cp1;
527 struct variable myvp;
528 oid tname[MAX_OID_LEN];
529
530 memcpy(&myvp, vp, sizeof(struct variable));
531
532 long_ret = *length;
533 for (i = 1; i <= (int) numrelocs; i++) {
534 exten = get_exten_instance(relocs, i);
535 if (!exten)
536 continue;
537 if ((int) exten->miblen == (int) vp->namelen - 1) {
538 memcpy(myvp.name, exten->miboid, exten->miblen * sizeof(oid));
539 myvp.namelen = exten->miblen;
540 *length = vp->namelen;
541 memcpy(tname, vp->name, vp->namelen * sizeof(oid));
542 if (!header_simple_table
543 (&myvp, tname, length, -1, var_len, write_method, -1))
544 break;
545 else
546 exten = NULL;
547 }
548 }
549 if (i > (int) numrelocs || exten == NULL) {
550 *length = long_ret;
551 *var_len = 0;
552 *write_method = NULL;
553 return (NULL);
554 }
555
556 *length = long_ret;
557 if (header_simple_table(vp, name, length, exact, var_len, write_method,
558 ((vp->magic == ERRORMSG) ? MAXMSGLINES : 1)))
559 return (NULL);
560
561 switch (vp->magic) {
562 case MIBINDEX:
563 long_ret = name[*length - 1];
564 return ((u_char *) (&long_ret));
565 case ERRORNAME: /* name defined in config file */
566 *var_len = strlen(exten->name);
567 return ((u_char *) (exten->name));
568 case SHELLCOMMAND:
569 *var_len = strlen(exten->command);
570 return ((u_char *) (exten->command));
571 case ERRORFLAG: /* return code from the process */
572 len = sizeof(exten->output);
573 if (exten->type == EXECPROC)
574 exten->result = run_exec_command( exten->command, NULL,
575 exten->output, &len);
576 else
577 exten->result = run_shell_command(exten->command, NULL,
578 exten->output, &len);
579 long_ret = exten->result;
580 return ((u_char *) (&long_ret));
581 case ERRORMSG: /* first line of text returned from the process */
582 len = sizeof(exten->output);
583 if (exten->type == EXECPROC)
584 exten->result = run_exec_command( exten->command, NULL,
585 exten->output, &len);
586 else
587 exten->result = run_shell_command(exten->command, NULL,
588 exten->output, &len);
589
590 /*
591 * Pick the output string apart into individual lines,
592 * and extract the one being asked for....
593 */
594 cp1 = exten->output;
595 for (i = 1; i != (int) name[*length - 1]; i++) {
596 cp = strchr(cp1, '\n');
597 if (!cp) {
598 *var_len = 0;
599 /* wait_on_exec(exten); ??? */
600 return NULL;
601 }
602 cp1 = ++cp;
603 }
604 /*
605 * ... and quit if we've run off the end of the output
606 */
607 if (!*cp1) {
608 *var_len = 0;
609 return NULL;
610 }
611 cp = strchr(cp1, '\n');
612 if (cp)
613 *cp = 0;
614 strlcpy(errmsg, cp1, sizeof(errmsg));
615 *var_len = strlen(errmsg);
616 if (*var_len && errmsg[*var_len - 1] == '\n')
617 errmsg[--(*var_len)] = '\0';
618 return ((u_char *) (errmsg));
619 case ERRORFIX:
620 *write_method = fixExecError;
621 long_return = 0;
622 return ((u_char *) & long_return);
623
624 case ERRORFIXCMD:
625 *var_len = strlen(exten->fixcmd);
626 return ((u_char *) exten->fixcmd);
627 }
628 return NULL;
629 }
630
631 netsnmp_subtree *
632 find_extensible(netsnmp_subtree *tp, oid *tname, size_t tnamelen, int exact)
633 {
634 size_t tmp;
635 int i;
636 struct extensible *exten = NULL;
637 struct variable myvp;
638 oid name[MAX_OID_LEN];
639 static netsnmp_subtree mysubtree[2] =
640 { { NULL, 0, NULL, 0, NULL, 0, NULL, 0, 0, NULL, NULL, 0, 0, 0,
641 NULL, NULL, NULL, 0, 0, NULL, 0, 0 },
642 { NULL, 0, NULL, 0, NULL, 0, NULL, 0, 0, NULL, NULL, 0, 0, 0,
643 NULL, NULL, NULL, 0, 0, NULL, 0, 0 } };
644
645 for (i = 1; i <= (int) numrelocs; i++) {
646 exten = get_exten_instance(relocs, i);
647 if (!exten)
648 continue;
649 if (exten->miblen != 0) {
650 memcpy(myvp.name, exten->miboid, exten->miblen * sizeof(oid));
651 memcpy(name, tname, tnamelen * sizeof(oid));
652 myvp.name[exten->miblen] = name[exten->miblen];
653 myvp.namelen = exten->miblen + 1;
654 tmp = exten->miblen + 1;
655 if (!header_simple_table(&myvp, name, &tmp, -1,
656 NULL, NULL, numrelocs)) {
657 break;
658 }
659 }
660 }
661 if (i > (int)numrelocs || exten == NULL) {
662 return (tp);
663 }
664
665 if (mysubtree[0].name_a != NULL) {
666 free(mysubtree[0].name_a);
667 mysubtree[0].name_a = NULL;
668 }
669 mysubtree[0].name_a = snmp_duplicate_objid(exten->miboid, exten->miblen);
670 mysubtree[0].namelen = exten->miblen;
671 mysubtree[0].variables = (struct variable *)extensible_relocatable_variables;
672 mysubtree[0].variables_len = sizeof(extensible_relocatable_variables) /
673 sizeof(*extensible_relocatable_variables);
674 mysubtree[0].variables_width = sizeof(*extensible_relocatable_variables);
675 mysubtree[1].namelen = 0;
676 return (mysubtree);
677 }