📄 extend.c
字号:
extend_free_cache, NULL, 0 ); if (extension->cache) extension->cache->magic = extension; row = netsnmp_create_table_data_row(); if (!row || !extension->cache) { _free_extension( extension, ereg ); SNMP_FREE( row ); return NULL; } row->data = (void *)extension; extension->row = row; netsnmp_table_row_add_index( row, ASN_OCTET_STR, exec_name, strlen(exec_name)); if ( netsnmp_table_data_add_row( dinfo, row) != SNMPERR_SUCCESS ) { /* _free_extension( extension, ereg ); */ SNMP_FREE( extension ); /* Probably not sufficient */ SNMP_FREE( row ); return NULL; } ereg->num_entries++; /* * Now add this structure to a private linked list. * We don't need this for the main tables - the * 'table_data' helper will take care of those. * But it's probably easier to handle the multi-line * output table ourselves, for which we need access * to the underlying data. * So we'll keep a list internally as well. */ for ( eptr1 = ereg->ehead, eptr2 = NULL; eptr1; eptr2 = eptr1, eptr1 = eptr1->next ) { if (strlen( eptr1->token ) > strlen( exec_name )) break; if (strlen( eptr1->token ) == strlen( exec_name ) && strcmp( eptr1->token, exec_name ) > 0 ) break; } if ( eptr2 ) eptr2->next = extension; else ereg->ehead = extension; extension->next = eptr1; return extension;}voidextend_parse_config(const char *token, char *cptr){ netsnmp_extend *extension; char exec_name[STRMAX]; char exec_command[STRMAX]; oid oid_buf[MAX_OID_LEN]; size_t oid_len; extend_registration_block *eptr; int flags; cptr = copy_nword(cptr, exec_name, sizeof(exec_name)); if ( *exec_name == '.' ) { oid_len = MAX_OID_LEN - 2; read_objid( exec_name, oid_buf, &oid_len ); cptr = copy_nword(cptr, exec_name, sizeof(exec_name)); if (!strcmp( token, "sh" ) || !strcmp( token, "exec" )) { config_perror("ERROR: This output format has been deprecated - Please use the 'extend' directive instead" ); return; } } else { memcpy( oid_buf, ns_extend_oid, sizeof(ns_extend_oid)); oid_len = OID_LENGTH(ns_extend_oid); } cptr = copy_nword(cptr, exec_command, sizeof(exec_command)); /* XXX - check 'exec_command' exists & is executable */ flags = (NS_EXTEND_FLAGS_ACTIVE | NS_EXTEND_FLAGS_CONFIG); if (!strcmp( token, "sh" ) || !strcmp( token, "extend-sh" ) || !strcmp( token, "sh2" )) flags |= NS_EXTEND_FLAGS_SHELL; if (!strcmp( token, "execFix" ) || !strcmp( token, "extendfix" ) || !strcmp( token, "execFix2" )) { strcat( exec_name, "Fix" ); flags |= NS_EXTEND_FLAGS_WRITEABLE; /* XXX - Check for shell... */ } eptr = _register_extend( oid_buf, oid_len ); extension = _new_extension( exec_name, flags, eptr ); if (extension) { extension->command = strdup( exec_command ); if (cptr) extension->args = strdup( cptr ); }#ifndef USING_UCD_SNMP_EXTENSIBLE_MODULE /* * Compatability with the UCD extTable */ if (!strcmp( token, "execFix" )) { int i; for ( i=0; i < num_compatability_entries; i++ ) { if (!strcmp( exec_name, compatability_entries[i].exec_entry->token)) break; } if ( i == num_compatability_entries ) config_perror("No matching exec entry" ); else compatability_entries[ i ].efix_entry = extension; } else if (!strcmp( token, "sh" ) || !strcmp( token, "exec" )) { if ( num_compatability_entries == max_compatability_entries ) /* XXX - should really use dynamic allocation */ config_perror("No further UCD-compatible entries" ); else compatability_entries[ num_compatability_entries++ ].exec_entry = extension; }#endif} /************************* * * Main table handlers * Most of the work is handled * by the 'table_data' helper. * *************************/inthandle_nsExtendConfigTable(netsnmp_mib_handler *handler, netsnmp_handler_registration *reginfo, netsnmp_agent_request_info *reqinfo, netsnmp_request_info *requests){ netsnmp_request_info *request; netsnmp_table_request_info *table_info; netsnmp_extend *extension; extend_registration_block *eptr; int i; int need_to_validate = 0; for ( request=requests; request; request=request->next ) { if (request->processed) continue; table_info = netsnmp_extract_table_info( request ); extension = (netsnmp_extend*)netsnmp_extract_table_row_data( request ); DEBUGMSGTL(( "nsExtendTable:config", "varbind: ")); DEBUGMSGOID(("nsExtendTable:config", request->requestvb->name, request->requestvb->name_length)); DEBUGMSG(( "nsExtendTable:config", " (%s)\n", se_find_label_in_slist("agent_mode", reqinfo->mode))); switch (reqinfo->mode) { case MODE_GET: switch (table_info->colnum) { case COLUMN_EXTCFG_COMMAND: snmp_set_var_typed_value( request->requestvb, ASN_OCTET_STR, extension->command, (extension->command)?strlen(extension->command):0); break; case COLUMN_EXTCFG_ARGS: snmp_set_var_typed_value( request->requestvb, ASN_OCTET_STR, extension->args, (extension->args)?strlen(extension->args):0); break; case COLUMN_EXTCFG_INPUT: snmp_set_var_typed_value( request->requestvb, ASN_OCTET_STR, extension->input, (extension->input)?strlen(extension->input):0); break; case COLUMN_EXTCFG_CACHETIME: snmp_set_var_typed_value( request->requestvb, ASN_INTEGER, (u_char*)&extension->cache->timeout, sizeof(int)); break; case COLUMN_EXTCFG_EXECTYPE: i = ((extension->flags & NS_EXTEND_FLAGS_SHELL) ? NS_EXTEND_ETYPE_SHELL : NS_EXTEND_ETYPE_EXEC); snmp_set_var_typed_value( request->requestvb, ASN_INTEGER, (u_char*)&i, sizeof(i)); break; case COLUMN_EXTCFG_RUNTYPE: i = ((extension->flags & NS_EXTEND_FLAGS_WRITEABLE) ? NS_EXTEND_RTYPE_RWRITE : NS_EXTEND_RTYPE_RONLY); snmp_set_var_typed_value( request->requestvb, ASN_INTEGER, (u_char*)&i, sizeof(i)); break; case COLUMN_EXTCFG_STORAGE: i = ((extension->flags & NS_EXTEND_FLAGS_CONFIG) ? ST_PERMANENT : ST_VOLATILE); snmp_set_var_typed_value( request->requestvb, ASN_INTEGER, (u_char*)&i, sizeof(i)); break; case COLUMN_EXTCFG_STATUS: i = ((extension->flags & NS_EXTEND_FLAGS_ACTIVE) ? RS_ACTIVE : RS_NOTINSERVICE); snmp_set_var_typed_value( request->requestvb, ASN_INTEGER, (u_char*)&i, sizeof(i)); break; default: netsnmp_set_request_error(reqinfo, request, SNMP_NOSUCHOBJECT); continue; } break; /********** * * Start of SET handling * * All config objects are potentially writable except * nsExtendStorage which is fixed as either 'permanent' * (if read from a config file) or 'volatile' (if set via SNMP) * The string-based settings of a 'permanent' entry cannot * be changed - neither can the execution or run type. * Such entries can be (temporarily) marked as inactive, * and the cache timeout adjusted, but these changes are * not persistent. * **********/ case MODE_SET_RESERVE1: /* * Validate the new assignments */ switch (table_info->colnum) { case COLUMN_EXTCFG_COMMAND: if (request->requestvb->type != ASN_OCTET_STR) { netsnmp_set_request_error(reqinfo, request, SNMP_ERR_WRONGTYPE); return SNMP_ERR_WRONGTYPE; } /* * Must have a full path to the command * XXX - Assumes Unix-style paths */ if (request->requestvb->val_len == 0 || request->requestvb->val.string[0] != '/') { netsnmp_set_request_error(reqinfo, request, SNMP_ERR_WRONGVALUE); return SNMP_ERR_WRONGVALUE; } /* * XXX - need to check this file exists * (and is executable) */ if (extension && extension->flags & NS_EXTEND_FLAGS_CONFIG) { /* * config entries are "permanent" so can't be changed */ netsnmp_set_request_error(reqinfo, request, SNMP_ERR_NOTWRITABLE); return SNMP_ERR_NOTWRITABLE; } break; case COLUMN_EXTCFG_ARGS: case COLUMN_EXTCFG_INPUT: if (request->requestvb->type != ASN_OCTET_STR) { netsnmp_set_request_error(reqinfo, request, SNMP_ERR_WRONGTYPE); return SNMP_ERR_WRONGTYPE; } if (extension && extension->flags & NS_EXTEND_FLAGS_CONFIG) { /* * config entries are "permanent" so can't be changed */ netsnmp_set_request_error(reqinfo, request, SNMP_ERR_NOTWRITABLE); return SNMP_ERR_NOTWRITABLE; } break; case COLUMN_EXTCFG_CACHETIME: if (request->requestvb->type != ASN_INTEGER) { netsnmp_set_request_error(reqinfo, request, SNMP_ERR_WRONGTYPE); return SNMP_ERR_WRONGTYPE; } i = *request->requestvb->val.integer; /* * -1 is a special value indicating "don't cache" * [[ XXX - should this be 0 ?? ]] * Otherwise, cache times must be non-negative */ if (i < -1 ) { netsnmp_set_request_error(reqinfo, request, SNMP_ERR_WRONGVALUE); return SNMP_ERR_WRONGVALUE; } break; case COLUMN_EXTCFG_EXECTYPE: if (request->requestvb->type != ASN_INTEGER) { netsnmp_set_request_error(reqinfo, request, SNMP_ERR_WRONGTYPE); return SNMP_ERR_WRONGTYPE; } i = *request->requestvb->val.integer; if (i<1 || i>2) { /* 'exec(1)' or 'shell(2)' only */ netsnmp_set_request_error(reqinfo, request, SNMP_ERR_WRONGVALUE); return SNMP_ERR_WRONGVALUE; } if (extension && extension->flags & NS_EXTEND_FLAGS_CONFIG) { /* * config entries are "permanent" so can't be changed */ netsnmp_set_request_error(reqinfo, request, SNMP_ERR_NOTWRITABLE); return SNMP_ERR_NOTWRITABLE; } break; case COLUMN_EXTCFG_RUNTYPE: if (request->requestvb->type != ASN_INTEGER) { netsnmp_set_request_error(reqinfo, request, SNMP_ERR_WRONGTYPE); return SNMP_ERR_WRONGTYPE; } /* * 'run-on-read(1)', 'run-on-set(2)' * or 'run-command(3)' only */ i = *request->requestvb->val.integer; if (i<1 || i>3) { netsnmp_set_request_error(reqinfo, request, SNMP_ERR_WRONGVALUE); return SNMP_ERR_WRONGVALUE; } /* * 'run-command(3)' can only be used with * a pre-existing 'run-on-set(2)' entry. */ if (i==3 && !(extension && (extension->flags & NS_EXTEND_FLAGS_WRITEABLE))) { netsnmp_set_request_error(reqinfo, request, SNMP_ERR_INCONSISTENTVALUE); return SNMP_ERR_INCONSISTENTVALUE;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -