📄 extensible.c
字号:
#include <net-snmp/net-snmp-config.h>#if HAVE_STDLIB_H#include <stdlib.h>#endif#if HAVE_UNISTD_H#include <unistd.h>#endif#if HAVE_FCNTL_H#include <fcntl.h>#endif#if TIME_WITH_SYS_TIME# ifdef WIN32# include <sys/timeb.h># else# include <sys/time.h># endif# include <time.h>#else# if HAVE_SYS_TIME_H# include <sys/time.h># else# include <time.h># endif#endif#include <signal.h>#if HAVE_MACHINE_PARAM_H#include <machine/param.h>#endif#if HAVE_SYS_PARAM_H#include <sys/param.h>#endif#if HAVE_SYS_VMMETER_H#if !(defined(bsdi2) || defined(netbsd1))#include <sys/vmmeter.h>#endif#endif#if HAVE_SYS_CONF_H#include <sys/conf.h>#endif#if HAVE_ASM_PAGE_H#include <asm/page.h>#endif#if HAVE_SYS_SWAP_H#include <sys/swap.h>#endif#if HAVE_SYS_FS_H#include <sys/fs.h>#else#if HAVE_UFS_FS_H#include <ufs/fs.h>#else#if HAVE_UFS_UFS_DINODE_H#include <ufs/ufs/dinode.h>#endif#if HAVE_UFS_FFS_FS_H#include <ufs/ffs/fs.h>#endif#endif#endif#if HAVE_MTAB_H#include <mtab.h>#endif#include <sys/stat.h>#include <errno.h>#if HAVE_FSTAB_H#include <fstab.h>#endif#if HAVE_SYS_STATFS_H#include <sys/statfs.h>#endif#if HAVE_SYS_STATVFS_H#include <sys/statvfs.h>#endif#if HAVE_SYS_VFS_H#include <sys/vfs.h>#endif#if (!defined(HAVE_STATVFS)) && defined(HAVE_STATFS)#if HAVE_SYS_MOUNT_H#include <sys/mount.h>#endif#if HAVE_SYS_SYSCTL_H#include <sys/sysctl.h>#endif#define statvfs statfs#endif#if HAVE_VM_VM_H#include <vm/vm.h>#endif#if HAVE_VM_SWAP_PAGER_H#include <vm/swap_pager.h>#endif#if HAVE_SYS_FIXPOINT_H#include <sys/fixpoint.h>#endif#if HAVE_MALLOC_H#include <malloc.h>#endif#if HAVE_STRING_H#include <string.h>#endif#include <ctype.h>#if HAVE_WINSOCK_H#include <winsock.h>#endif#ifndef HAVE_STRNCASECMPint strncasecmp(const char *s1, const char *s2, size_t n);#endif#if HAVE_DMALLOC_H#include <dmalloc.h>#endif#include <net-snmp/net-snmp-includes.h>#include <net-snmp/agent/net-snmp-agent-includes.h>#include <net-snmp/agent/auto_nlist.h>#include <net-snmp/agent/agent_callbacks.h>#include "struct.h"#include "extensible.h"#include "utilities/execute.h"#include "util_funcs.h"extern struct myproc *procwatch; /* moved to proc.c */extern int numprocs; /* ditto */extern struct extensible *extens; /* In exec.c */extern struct extensible *relocs; /* In exec.c */extern int numextens; /* ditto */extern int numrelocs; /* ditto */extern struct extensible *passthrus; /* In pass.c */extern int numpassthrus; /* ditto */extern char sysName[];extern netsnmp_subtree *subtrees;extern struct variable2 extensible_relocatable_variables[];extern struct variable2 extensible_passthru_variables[];/* * the relocatable extensible commands variables */struct variable2 extensible_relocatable_variables[] = { {MIBINDEX, ASN_INTEGER, RONLY, var_extensible_relocatable, 1, {MIBINDEX}}, {ERRORNAME, ASN_OCTET_STR, RONLY, var_extensible_relocatable, 1, {ERRORNAME}}, {SHELLCOMMAND, ASN_OCTET_STR, RONLY, var_extensible_relocatable, 1, {SHELLCOMMAND}}, {ERRORFLAG, ASN_INTEGER, RONLY, var_extensible_relocatable, 1, {ERRORFLAG}}, {ERRORMSG, ASN_OCTET_STR, RONLY, var_extensible_relocatable, 1, {ERRORMSG}}, {ERRORFIX, ASN_INTEGER, RWRITE, var_extensible_relocatable, 1, {ERRORFIX}}, {ERRORFIXCMD, ASN_OCTET_STR, RONLY, var_extensible_relocatable, 1, {ERRORFIXCMD}}};voidinit_extensible(void){ struct variable2 extensible_extensible_variables[] = { {MIBINDEX, ASN_INTEGER, RONLY, var_extensible_shell, 1, {MIBINDEX}}, {ERRORNAME, ASN_OCTET_STR, RONLY, var_extensible_shell, 1, {ERRORNAME}}, {SHELLCOMMAND, ASN_OCTET_STR, RONLY, var_extensible_shell, 1, {SHELLCOMMAND}}, {ERRORFLAG, ASN_INTEGER, RONLY, var_extensible_shell, 1, {ERRORFLAG}}, {ERRORMSG, ASN_OCTET_STR, RONLY, var_extensible_shell, 1, {ERRORMSG}}, {ERRORFIX, ASN_INTEGER, RWRITE, var_extensible_shell, 1, {ERRORFIX}}, {ERRORFIXCMD, ASN_OCTET_STR, RONLY, var_extensible_shell, 1, {ERRORFIXCMD}} }; /* * Define the OID pointer to the top of the mib tree that we're * registering underneath */ oid extensible_variables_oid[] = { UCDAVIS_MIB, SHELLMIBNUM, 1 }; /* * register ourselves with the agent to handle our mib tree */ REGISTER_MIB("ucd-snmp/extensible", extensible_extensible_variables, variable2, extensible_variables_oid); snmpd_register_config_handler("exec", extensible_parse_config, extensible_free_config, "[miboid] name program arguments"); snmpd_register_config_handler("sh", extensible_parse_config, extensible_free_config, "[miboid] name program-or-script arguments"); snmpd_register_config_handler("execfix", execfix_parse_config, NULL, "exec-or-sh-name program [arguments...]"); snmp_register_callback(SNMP_CALLBACK_APPLICATION, SNMPD_CALLBACK_PRE_UPDATE_CONFIG, extensible_unregister, NULL);}extern int pass_compare(const void *a, const void *b);voidextensible_parse_config(const char *token, char *cptr){ struct extensible *ptmp, **pp; char *tcptr; int scount; /* * allocate and clear memory structure */ ptmp = (struct extensible *) calloc(1, sizeof(struct extensible)); if (ptmp == NULL) return; /* XXX memory alloc error */ if (*cptr == '.') cptr++; if (isdigit(*cptr)) { /* * its a relocatable extensible mib */ config_perror("Warning: relocatable 'exec' format will change in a future release - See 'NET-SNMP-EXTEND-MIB' for an alternative" ); for (pp = &relocs, numrelocs++; *pp; pp = &((*pp)->next)); (*pp) = ptmp; pp = &relocs; scount = numrelocs; } else { /* * it goes in with the general extensible table */ for (pp = &extens, numextens++; *pp; pp = &((*pp)->next)); (*pp) = ptmp; pp = &extens; scount = numextens; } /* * the rest is pretty much handled the same */ if (!strncasecmp(token, "sh", 2)) ptmp->type = SHPROC; else ptmp->type = EXECPROC; if (isdigit(*cptr)) { ptmp->miblen = parse_miboid(cptr, ptmp->miboid); while (isdigit(*cptr) || *cptr == '.') cptr++; } /* * name */ cptr = skip_white(cptr); copy_nword(cptr, ptmp->name, sizeof(ptmp->name)); cptr = skip_not_white(cptr); cptr = skip_white(cptr); /* * command */ if (cptr == NULL) { config_perror("No command specified on line"); } else { /* * Support multi-element commands in shell configuration * lines, but truncate after the first command for 'exec' */ for (tcptr = cptr; *tcptr != 0 && *tcptr != '#'; tcptr++) if (*tcptr == ';' && ptmp->type == EXECPROC) break; strncpy(ptmp->command, cptr, tcptr - cptr); ptmp->command[tcptr - cptr] = 0; }#ifdef EXECFIXCMD sprintf(ptmp->fixcmd, EXECFIXCMD, ptmp->name);#endif if (ptmp->miblen > 0) { register_mib(token, (struct variable *) extensible_relocatable_variables, sizeof(struct variable2), sizeof(extensible_relocatable_variables) / sizeof(*extensible_relocatable_variables), ptmp->miboid, ptmp->miblen); } if (scount > 1) { int i; struct extensible **etmp = (struct extensible **) malloc(((sizeof(struct extensible *)) * scount)); if (etmp == NULL) return; /* XXX memory alloc error */ for (i = 0, ptmp = *pp; i < scount && ptmp != 0; i++, ptmp = ptmp->next) etmp[i] = ptmp; qsort(etmp, scount, sizeof(struct extensible *), pass_compare); *pp = (struct extensible *) etmp[0]; ptmp = (struct extensible *) etmp[0]; for (i = 0; i < scount - 1; i++) { ptmp->next = etmp[i + 1]; ptmp = ptmp->next; } ptmp->next = NULL; free(etmp); }}intextensible_unregister(int major, int minor, void *serverarg, void *clientarg){ extensible_free_config(); return 0;}voidextensible_free_config(void){ struct extensible *etmp, *etmp2; oid tname[MAX_OID_LEN]; int i; for (etmp = extens; etmp != NULL;) { etmp2 = etmp; etmp = etmp->next; free(etmp2); } for (etmp = relocs; etmp != NULL;) { etmp2 = etmp; etmp = etmp->next; /* * The new modular API results in the column * objects being registered individually, so * they need to be unregistered individually too! */ memset(tname, 0, MAX_OID_LEN*sizeof(oid)); memcpy(tname, etmp2->miboid, etmp2->miblen*sizeof(oid)); for (i=1; i<4; i++) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -