📄 snmp.c
字号:
s->securityLevel = SNMP_SEC_LEVEL_AUTHNOPRIV; return (0); } else if (!strcasecmp(level, "authPriv") || !strcasecmp(level, "ap")) { s->securityLevel = SNMP_SEC_LEVEL_AUTHPRIV; return (0); } php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid security level: %s", level); } return (-1);}/* }}} *//* {{{ int netsnmp_session_set_auth_protocol(struct snmp_session *s, char *prot) Set the authentication protocol in the snmpv3 session */static int netsnmp_session_set_auth_protocol(struct snmp_session *s, char *prot TSRMLS_DC){ if ((s) && (prot)) { if (!strcasecmp(prot, "MD5")) { s->securityAuthProto = usmHMACMD5AuthProtocol; s->securityAuthProtoLen = OIDSIZE(usmHMACMD5AuthProtocol); return (0); } else if (!strcasecmp(prot, "SHA")) { s->securityAuthProto = usmHMACSHA1AuthProtocol; s->securityAuthProtoLen = OIDSIZE(usmHMACSHA1AuthProtocol); return (0); } else if (strlen(prot)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid authentication protocol: %s", prot); } } return (-1);}/* }}} *//* {{{ int netsnmp_session_set_sec_protocol(struct snmp_session *s, char *prot) Set the security protocol in the snmpv3 session */static int netsnmp_session_set_sec_protocol(struct snmp_session *s, char *prot TSRMLS_DC){ if ((s) && (prot)) { if (!strcasecmp(prot, "DES")) { s->securityPrivProto = usmDESPrivProtocol; s->securityPrivProtoLen = OIDSIZE(usmDESPrivProtocol); return (0);#ifdef HAVE_AES } else if (!strcasecmp(prot, "AES128")#ifdef SNMP_VALIDATE_ERR/* * In Net-SNMP before 5.2, the following symbols exist:* usmAES128PrivProtocol, usmAES192PrivProtocol, usmAES256PrivProtocol* In an effort to be more standards-compliant, 5.2 removed the last two.* As of 5.2, the symbols are:* usmAESPrivProtocol, usmAES128PrivProtocol* * As we want this extension to compile on both versions, we use the latter* symbol on purpose, as it's defined to be the same as the former.*/ || !strcasecmp(prot, "AES")) { s->securityPrivProto = usmAES128PrivProtocol; s->securityPrivProtoLen = OIDSIZE(usmAES128PrivProtocol); return (0);#else ) { s->securityPrivProto = usmAES128PrivProtocol; s->securityPrivProtoLen = OIDSIZE(usmAES128PrivProtocol); return (0); } else if (!strcasecmp(prot, "AES192")) { s->securityPrivProto = usmAES192PrivProtocol; s->securityPrivProtoLen = OIDSIZE(usmAES192PrivProtocol); return (0); } else if (!strcasecmp(prot, "AES256")) { s->securityPrivProto = usmAES256PrivProtocol; s->securityPrivProtoLen = OIDSIZE(usmAES256PrivProtocol); return (0);#endif#endif } else if (strlen(prot)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid privacy protocol: %s", prot); } } return (-1);}/* }}} *//* {{{ int netsnmp_session_gen_auth_key(struct snmp_session *s, char *pass) Make key from pass phrase in the snmpv3 session */static int netsnmp_session_gen_auth_key(struct snmp_session *s, char *pass TSRMLS_DC){ /* * make master key from pass phrases */ if ((s) && (pass) && strlen(pass)) { s->securityAuthKeyLen = USM_AUTH_KU_LEN; if (s->securityAuthProto == NULL) { /* get .conf set default */ const oid *def = get_default_authtype(&(s->securityAuthProtoLen)); s->securityAuthProto = snmp_duplicate_objid(def, s->securityAuthProtoLen); } if (s->securityAuthProto == NULL) { /* assume MD5 */ s->securityAuthProto = snmp_duplicate_objid(usmHMACMD5AuthProtocol, OIDSIZE(usmHMACMD5AuthProtocol)); s->securityAuthProtoLen = OIDSIZE(usmHMACMD5AuthProtocol); } if (generate_Ku(s->securityAuthProto, s->securityAuthProtoLen, (u_char *) pass, strlen(pass), s->securityAuthKey, &(s->securityAuthKeyLen)) != SNMPERR_SUCCESS) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error generating a key for authentication pass phrase"); return (-2); } return (0); } return (-1);}/* }}} *//* {{{ int netsnmp_session_gen_sec_key(struct snmp_session *s, u_char *pass) Make key from pass phrase in the snmpv3 session */static int netsnmp_session_gen_sec_key(struct snmp_session *s, u_char *pass TSRMLS_DC){ if ((s) && (pass) && strlen(pass)) { s->securityPrivKeyLen = USM_PRIV_KU_LEN; if (s->securityPrivProto == NULL) { /* get .conf set default */ const oid *def = get_default_privtype(&(s->securityPrivProtoLen)); s->securityPrivProto = snmp_duplicate_objid(def, s->securityPrivProtoLen); } if (s->securityPrivProto == NULL) { /* assume DES */ s->securityPrivProto = snmp_duplicate_objid(usmDESPrivProtocol, OIDSIZE(usmDESPrivProtocol)); s->securityPrivProtoLen = OIDSIZE(usmDESPrivProtocol); } if (generate_Ku(s->securityAuthProto, s->securityAuthProtoLen, pass, strlen(pass), s->securityPrivKey, &(s->securityPrivKeyLen)) != SNMPERR_SUCCESS) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error generating a key for privacy pass phrase"); return (-2); } return (0); } return (-1);}/* }}} *//* {{{ proto string snmp2_get(string host, string community, string object_id [, int timeout [, int retries]]) Fetch a SNMP object */PHP_FUNCTION(snmp2_get){ php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU,1, SNMP_VERSION_2c);}/* }}} *//* {{{ proto array snmp2_walk(string host, string community, string object_id [, int timeout [, int retries]]) Return all objects under the specified object id */PHP_FUNCTION(snmp2_walk){ php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU,2, SNMP_VERSION_2c);}/* }}} *//* {{{ proto array snmp2_real_walk(string host, string community, string object_id [, int timeout [, int retries]]) Return all objects including their respective object id withing the specified one */PHP_FUNCTION(snmp2_real_walk){ php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU,3, SNMP_VERSION_2c);}/* }}} *//* {{{ proto int snmp2_set(string host, string community, string object_id, string type, mixed value [, int timeout [, int retries]]) Set the value of a SNMP object */PHP_FUNCTION(snmp2_set){ php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU,11, SNMP_VERSION_2c);}/* }}} *//* {{{ proto void php_snmpv3(INTERNAL_FUNCTION_PARAMETERS, int st)** Generic SNMPv3 object fetcher* From here is passed on the the common internal object fetcher.** st=1 snmp3_get() - query an agent and return a single value.* st=2 snmp3_walk() - walk the mib and return a single dimensional array * containing the values.* st=3 snmp3_real_walk() - walk the mib and return an * array of oid,value pairs.* st=11 snmp3_set() - query an agent and set a single value**/static void php_snmpv3(INTERNAL_FUNCTION_PARAMETERS, int st){ zval **a1, **a2, **a3, **a4, **a5, **a6, **a7, **a8, **a9, **a10, **a11, **a12; struct snmp_session session; long timeout=SNMP_DEFAULT_TIMEOUT; long retries=SNMP_DEFAULT_RETRIES; int myargc = ZEND_NUM_ARGS(); char type = (char) 0; char *value = (char *) 0; char hostname[MAX_NAME_LEN]; int remote_port = 161; char *pptr; if (myargc < 8 || myargc > 12 || zend_get_parameters_ex(myargc, &a1, &a2, &a3, &a4, &a5, &a6, &a7, &a8, &a9, &a10, &a11, &a12) == FAILURE) { WRONG_PARAM_COUNT; } snmp_sess_init(&session); /* This is all SNMPv3 */ session.version = SNMP_VERSION_3; /* Reading the hostname and its optional non-default port number */ convert_to_string_ex(a1); strlcpy(hostname, Z_STRVAL_PP(a1), sizeof(hostname)); if ((pptr = strchr (hostname, ':'))) { remote_port = strtol (pptr + 1, NULL, 0); } session.peername = hostname; session.remote_port = remote_port; /* Setting the security name. */ convert_to_string_ex(a2); if (netsnmp_session_set_sec_name(&session, Z_STRVAL_PP(a2))) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could net set security name: %s", Z_STRVAL_PP(a2)); RETURN_FALSE; } /* Setting the security level. */ convert_to_string_ex(a3); if (netsnmp_session_set_sec_level(&session, Z_STRVAL_PP(a3) TSRMLS_CC)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid security level: %s", Z_STRVAL_PP(a3)); RETURN_FALSE; } /* Setting the authentication protocol. */ convert_to_string_ex(a4); if (netsnmp_session_set_auth_protocol(&session, Z_STRVAL_PP(a4) TSRMLS_CC)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid authentication protocol: %s", Z_STRVAL_PP(a4)); RETURN_FALSE; } /* Setting the authentication passphrase. */ convert_to_string_ex(a5); if (netsnmp_session_gen_auth_key(&session, Z_STRVAL_PP(a5) TSRMLS_CC)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not generate key for authentication pass phrase: %s", Z_STRVAL_PP(a4)); RETURN_FALSE; } /* Setting the security protocol. */ convert_to_string_ex(a6); if (netsnmp_session_set_sec_protocol(&session, Z_STRVAL_PP(a6) TSRMLS_CC) && (0 != strlen(Z_STRVAL_PP(a6)))) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid security protocol: %s", Z_STRVAL_PP(a6)); RETURN_FALSE; } /* Setting the security protocol passphrase. */ convert_to_string_ex(a7); if (netsnmp_session_gen_sec_key(&session, Z_STRVAL_PP(a7) TSRMLS_CC) && (0 != strlen(Z_STRVAL_PP(a7)))) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not generate key for security pass phrase: %s", Z_STRVAL_PP(a7)); RETURN_FALSE; } if (st == 11) { if (myargc < 10) { WRONG_PARAM_COUNT; } if (myargc > 10) { convert_to_long_ex(a11); timeout = Z_LVAL_PP(a11); } if (myargc > 11) { convert_to_long_ex(a12); retries = Z_LVAL_PP(a12); } convert_to_string_ex(a9); convert_to_string_ex(a10); type = Z_STRVAL_PP(a9)[0]; value = Z_STRVAL_PP(a10); } else { if (myargc > 8) { convert_to_long_ex(a9); timeout = Z_LVAL_PP(a9); } if (myargc > 9) { convert_to_long_ex(a10); retries = Z_LVAL_PP(a10); } } session.retries = retries; session.timeout = timeout; php_snmp_internal(INTERNAL_FUNCTION_PARAM_PASSTHRU, st, &session, Z_STRVAL_PP(a8), type, value);}/* }}} *//* {{{ proto int snmp3_get(string host, string sec_name, string sec_level, string auth_protocol, string auth_passphrase, string priv_protocol, string priv_passphrase, string object_id [, int timeout [, int retries]]) Fetch the value of a SNMP object */PHP_FUNCTION(snmp3_get){ php_snmpv3(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);}/* }}} *//* {{{ proto int snmp3_walk(string host, string sec_name, string sec_level, string auth_protocol, string auth_passphrase, string priv_protocol, string priv_passphrase, string object_id [, int timeout [, int retries]]) Fetch the value of a SNMP object */PHP_FUNCTION(snmp3_walk){ php_snmpv3(INTERNAL_FUNCTION_PARAM_PASSTHRU, 2);}/* }}} *//* {{{ proto int snmp3_real_walk(string host, string sec_name, string sec_level, string auth_protocol, string auth_passphrase, string priv_protocol, string priv_passphrase, string object_id [, int timeout [, int retries]]) Fetch the value of a SNMP object */PHP_FUNCTION(snmp3_real_walk){ php_snmpv3(INTERNAL_FUNCTION_PARAM_PASSTHRU, 3);}/* }}} *//* {{{ proto int snmp3_set(string host, string sec_name, string sec_level, string auth_protocol, string auth_passphrase, string priv_protocol, string priv_passphrase, string object_id, string type, mixed value [, int timeout [, int retries]]) Fetch the value of a SNMP object */PHP_FUNCTION(snmp3_set){ php_snmpv3(INTERNAL_FUNCTION_PARAM_PASSTHRU, 11);}/* }}} *//* {{{ proto int snmp_set_valueretrieval(int method) Specify the method how the SNMP values will be returned */PHP_FUNCTION(snmp_set_valueretrieval){ zval **method; if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(ZEND_NUM_ARGS(), &method) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_long_ex(method); if ((Z_LVAL_PP(method) == SNMP_VALUE_LIBRARY) || (Z_LVAL_PP(method) == SNMP_VALUE_PLAIN) || (Z_LVAL_PP(method) == SNMP_VALUE_OBJECT)) { SNMP_G(valueretrieval) = Z_LVAL_PP(method); }}/* }}} *//* {{{ proto int snmp_get_valueretrieval() Return the method how the SNMP values will be returned */PHP_FUNCTION(snmp_get_valueretrieval){ RETURN_LONG(SNMP_G(valueretrieval));}/* }}} */#endif/* * Local variables: * tab-width: 4 * c-basic-offset: 4 * End: * vim600: sw=4 ts=4 fdm=marker * vim<600: sw=4 ts=4 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -