📄 pass.c
字号:
static long long_ret; char buf[SNMP_MAXBUF]; static char buf2[SNMP_MAXBUF]; static oid objid[MAX_OID_LEN]; struct extensible *passthru; FILE *file; long_ret = *length; for (i = 1; i <= numpassthrus; i++) { passthru = get_exten_instance(passthrus, i); rtest = snmp_oid_min_compare(name, *length, passthru->miboid, passthru->miblen); if ((exact && rtest == 0) || (!exact && rtest <= 0)) { /* * setup args */ if (passthru->miblen >= *length || rtest < 0) sprint_mib_oid(buf, passthru->miboid, passthru->miblen); else sprint_mib_oid(buf, name, *length); if (exact) snprintf(passthru->command, sizeof(passthru->command), "%s -g %s", passthru->name, buf); else snprintf(passthru->command, sizeof(passthru->command), "%s -n %s", passthru->name, buf); passthru->command[ sizeof(passthru->command)-1 ] = 0; DEBUGMSGTL(("ucd-snmp/pass", "pass-running: %s\n", passthru->command)); /* * valid call. Exec and get output */ if ((fd = get_exec_output(passthru)) != -1) { file = fdopen(fd, "r"); if (fgets(buf, sizeof(buf), file) == NULL) { fclose(file); wait_on_exec(passthru); if (exact) { /* * to enable creation */ *write_method = setPass; *var_len = 0; return (NULL); } continue; } newlen = parse_miboid(buf, newname); /* * its good, so copy onto name/length */ memcpy((char *) name, (char *) newname, (int) newlen * sizeof(oid)); *length = newlen; /* * set up return pointer for setable stuff */ *write_method = setPass; if (newlen == 0 || fgets(buf, sizeof(buf), file) == NULL || fgets(buf2, sizeof(buf2), file) == NULL) { *var_len = 0; fclose(file); wait_on_exec(passthru); return (NULL); } fclose(file); wait_on_exec(passthru); /* * buf contains the return type, and buf2 contains the data */ if (!strncasecmp(buf, "string", 6)) { buf2[strlen(buf2) - 1] = 0; /* zap the linefeed */ *var_len = strlen(buf2); vp->type = ASN_OCTET_STR; return ((unsigned char *) buf2); } else if (!strncasecmp(buf, "integer", 7)) { *var_len = sizeof(long_ret); long_ret = strtol(buf2, NULL, 10); vp->type = ASN_INTEGER; return ((unsigned char *) &long_ret); } else if (!strncasecmp(buf, "unsigned", 8)) { *var_len = sizeof(long_ret); long_ret = strtoul(buf2, NULL, 10); vp->type = ASN_UNSIGNED; return ((unsigned char *) &long_ret); } else if (!strncasecmp(buf, "counter", 7)) { *var_len = sizeof(long_ret); long_ret = strtoul(buf2, NULL, 10); vp->type = ASN_COUNTER; return ((unsigned char *) &long_ret); } else if (!strncasecmp(buf, "octet", 5)) { *var_len = asc2bin(buf2); vp->type = ASN_OCTET_STR; return ((unsigned char *) buf2); } else if (!strncasecmp(buf, "opaque", 6)) { *var_len = asc2bin(buf2); vp->type = ASN_OPAQUE; return ((unsigned char *) buf2); } else if (!strncasecmp(buf, "gauge", 5)) { *var_len = sizeof(long_ret); long_ret = strtoul(buf2, NULL, 10); vp->type = ASN_GAUGE; return ((unsigned char *) &long_ret); } else if (!strncasecmp(buf, "objectid", 8)) { newlen = parse_miboid(buf2, objid); *var_len = newlen * sizeof(oid); vp->type = ASN_OBJECT_ID; return ((unsigned char *) objid); } else if (!strncasecmp(buf, "timetick", 8)) { *var_len = sizeof(long_ret); long_ret = strtoul(buf2, NULL, 10); vp->type = ASN_TIMETICKS; return ((unsigned char *) &long_ret); } else if (!strncasecmp(buf, "ipaddress", 9)) { newlen = parse_miboid(buf2, objid); if (newlen != 4) { snmp_log(LOG_ERR, "invalid ipaddress returned: %s\n", buf2); *var_len = 0; return (NULL); } long_ret = (objid[0] << (8 * 3)) + (objid[1] << (8 * 2)) + (objid[2] << 8) + objid[3]; long_ret = htonl(long_ret); *var_len = sizeof(long_ret); vp->type = ASN_IPADDRESS; return ((unsigned char *) &long_ret); } } *var_len = 0; return (NULL); } } if (var_len) *var_len = 0; *write_method = NULL; return (NULL);}intsetPass(int action, u_char * var_val, u_char var_val_type, size_t var_val_len, u_char * statP, oid * name, size_t name_len){ int i, rtest; struct extensible *passthru; char buf[SNMP_MAXBUF], buf2[SNMP_MAXBUF]; long tmp; unsigned long utmp; for (i = 1; i <= numpassthrus; i++) { passthru = get_exten_instance(passthrus, i); rtest = snmp_oid_min_compare(name, name_len, passthru->miboid, passthru->miblen); if (rtest <= 0) { if (action != ACTION) return SNMP_ERR_NOERROR; /* * setup args */ if (passthru->miblen >= name_len || rtest < 0) sprint_mib_oid(buf, passthru->miboid, passthru->miblen); else sprint_mib_oid(buf, name, name_len); snprintf(passthru->command, sizeof(passthru->command), "%s -s %s ", passthru->name, buf); passthru->command[ sizeof(passthru->command)-1 ] = 0; switch (var_val_type) { case ASN_INTEGER: case ASN_COUNTER: case ASN_GAUGE: case ASN_TIMETICKS: tmp = *((long *) var_val); switch (var_val_type) { case ASN_INTEGER: sprintf(buf, "integer %d\n", (int) tmp); break; case ASN_COUNTER: sprintf(buf, "counter %d\n", (int) tmp); break; case ASN_GAUGE: sprintf(buf, "gauge %d\n", (int) tmp); break; case ASN_TIMETICKS: sprintf(buf, "timeticks %d\n", (int) tmp); break; } break; case ASN_IPADDRESS: utmp = *((u_long *) var_val); utmp = ntohl(utmp); sprintf(buf, "ipaddress %d.%d.%d.%d\n", (int) ((utmp & 0xff000000) >> (8 * 3)), (int) ((utmp & 0xff0000) >> (8 * 2)), (int) ((utmp & 0xff00) >> (8)), (int) ((utmp & 0xff))); break; case ASN_OCTET_STR: memcpy(buf2, var_val, var_val_len); if (var_val_len == 0) sprintf(buf, "string \"\"\n"); else if (bin2asc(buf2, var_val_len) == (int) var_val_len) snprintf(buf, sizeof(buf), "string \"%s\"\n", buf2); else snprintf(buf, sizeof(buf), "octet \"%s\"\n", buf2); buf[ sizeof(buf)-1 ] = 0; break; case ASN_OBJECT_ID: sprint_mib_oid(buf2, (oid *) var_val, var_val_len); snprintf(buf, sizeof(buf), "objectid \"%s\"\n", buf2); buf[ sizeof(buf)-1 ] = 0; break; } strncat(passthru->command, buf, sizeof(passthru->command)); passthru->command[ sizeof(passthru->command)-1 ] = 0; DEBUGMSGTL(("ucd-snmp/pass", "pass-running: %s", passthru->command)); exec_command(passthru); DEBUGMSGTL(("ucd-snmp/pass", "pass-running returned: %s", passthru->output)); if (!strncasecmp(passthru->output, "not-writable", 12)) { return SNMP_ERR_NOTWRITABLE; } else if (!strncasecmp(passthru->output, "wrong-type", 10)) { return SNMP_ERR_WRONGTYPE; } return SNMP_ERR_NOERROR; } } if (snmp_get_do_debugging()) { sprint_mib_oid(buf2, name, name_len); DEBUGMSGTL(("ucd-snmp/pass", "pass-notfound: %s\n", buf2)); } return SNMP_ERR_NOSUCHNAME;}intpass_compare(const void *a, const void *b){ const struct extensible *const *ap, *const *bp; ap = (const struct extensible * const *) a; bp = (const struct extensible * const *) b; return snmp_oid_compare((*ap)->miboid, (*ap)->miblen, (*bp)->miboid, (*bp)->miblen);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -