Skip to content

Commit 42a05c7

Browse files
committed
ext/snmp: various internals rewrite.
1 parent e4ad271 commit 42a05c7

File tree

1 file changed

+52
-33
lines changed

1 file changed

+52
-33
lines changed

ext/snmp/snmp.c

Lines changed: 52 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ extern netsnmp_log_handler *logh_head;
7676
{ \
7777
snmp_disable_log(); \
7878
while(NULL != logh_head) \
79-
netsnmp_remove_loghandler( logh_head ); \
79+
remove_loghandler( logh_head ); \
8080
}
8181
#endif
8282

@@ -153,7 +153,7 @@ static PHP_GINIT_FUNCTION(snmp)
153153
} \
154154
}
155155

156-
static void netsnmp_session_free(php_snmp_session **session) /* {{{ */
156+
static void snmp_session_free(php_snmp_session **session) /* {{{ */
157157
{
158158
if (*session) {
159159
PHP_SNMP_SESSION_FREE(peername);
@@ -174,7 +174,7 @@ static void php_snmp_object_free_storage(zend_object *object) /* {{{ */
174174
return;
175175
}
176176

177-
netsnmp_session_free(&(intern->session));
177+
snmp_session_free(&(intern->session));
178178

179179
zend_object_std_dtor(&intern->zo);
180180
}
@@ -829,19 +829,26 @@ static bool php_snmp_parse_oid(
829829
}
830830
/* }}} */
831831

832-
/* {{{ netsnmp_session_init
833-
allocates memory for session and session->peername, caller should free it manually using netsnmp_session_free() and efree()
832+
/* {{{ snmp_session_init
833+
allocates memory for session and session->peername, caller should free it manually using session_free() and efree()
834834
*/
835-
static bool netsnmp_session_init(php_snmp_session **session_p, int version, zend_string *hostname, zend_string *community, int timeout, int retries)
835+
static bool snmp_session_init(php_snmp_session **session_p, int version, zend_string *hostname, zend_string *community, int timeout, int retries)
836836
{
837837
php_snmp_session *session;
838838
char *pptr, *host_ptr;
839839
bool force_ipv6 = false;
840840
int n;
841841
struct sockaddr **psal;
842842
struct sockaddr **res;
843+
844+
if (ZSTR_LEN(hostname) >= MAX_NAME_LEN) {
845+
php_error_docref(NULL, E_WARNING, "hostname length must be lower than %d", MAX_NAME_LEN);
846+
return false;
847+
}
848+
843849
// TODO: Do not strip and re-add the port in peername?
844-
unsigned remote_port = SNMP_PORT;
850+
unsigned short remote_port = SNMP_PORT;
851+
int tmp_port;
845852

846853
*session_p = (php_snmp_session *)emalloc(sizeof(php_snmp_session));
847854
session = *session_p;
@@ -853,7 +860,7 @@ static bool netsnmp_session_init(php_snmp_session **session_p, int version, zend
853860

854861
session->peername = emalloc(MAX_NAME_LEN);
855862
/* we copy original hostname for further processing */
856-
strlcpy(session->peername, ZSTR_VAL(hostname), MAX_NAME_LEN);
863+
memcpy(session->peername, ZSTR_VAL(hostname), ZSTR_LEN(hostname) + 1);
857864
host_ptr = session->peername;
858865

859866
/* Reading the hostname and its optional non-default port number */
@@ -862,7 +869,13 @@ static bool netsnmp_session_init(php_snmp_session **session_p, int version, zend
862869
host_ptr++;
863870
if ((pptr = strchr(host_ptr, ']'))) {
864871
if (pptr[1] == ':') {
865-
remote_port = atoi(pptr + 2);
872+
char *pport = pptr + 2;
873+
tmp_port = atoi(pport);
874+
if (tmp_port < 0 || tmp_port > USHRT_MAX) {
875+
php_error_docref(NULL, E_WARNING, "Remote port must be between 0 and %u", USHRT_MAX);
876+
return false;
877+
}
878+
remote_port = (unsigned short)tmp_port;
866879
}
867880
*pptr = '\0';
868881
} else {
@@ -871,7 +884,13 @@ static bool netsnmp_session_init(php_snmp_session **session_p, int version, zend
871884
}
872885
} else { /* IPv4 address */
873886
if ((pptr = strchr(host_ptr, ':'))) {
874-
remote_port = atoi(pptr + 1);
887+
char *pport = pptr + 1;
888+
tmp_port = atoi(pport);
889+
if (tmp_port < 0 || tmp_port > USHRT_MAX) {
890+
php_error_docref(NULL, E_WARNING, "Remote port must be between 0 and %u", USHRT_MAX);
891+
return false;
892+
}
893+
remote_port = (unsigned short)tmp_port;
875894
*pptr = '\0';
876895
}
877896
}
@@ -920,7 +939,7 @@ static bool netsnmp_session_init(php_snmp_session **session_p, int version, zend
920939
if (remote_port != SNMP_PORT) {
921940
size_t peername_length = strlen(session->peername);
922941
pptr = session->peername + peername_length;
923-
snprintf(pptr, MAX_NAME_LEN - peername_length, ":%d", remote_port);
942+
snprintf(pptr, MAX_NAME_LEN - peername_length, ":%u", remote_port);
924943
}
925944

926945
php_network_freeaddresses(psal);
@@ -942,7 +961,7 @@ static bool netsnmp_session_init(php_snmp_session **session_p, int version, zend
942961
/* }}} */
943962

944963
/* {{{ Set the security level in the snmpv3 session */
945-
static bool netsnmp_session_set_sec_level(struct snmp_session *s, zend_string *level)
964+
static bool snmp_session_set_sec_level(struct snmp_session *s, zend_string *level)
946965
{
947966
if (zend_string_equals_literal_ci(level, "noAuthNoPriv") || zend_string_equals_literal_ci(level, "nanp")) {
948967
s->securityLevel = SNMP_SEC_LEVEL_NOAUTH;
@@ -959,7 +978,7 @@ static bool netsnmp_session_set_sec_level(struct snmp_session *s, zend_string *l
959978
/* }}} */
960979

961980
/* {{{ Set the authentication protocol in the snmpv3 session */
962-
static bool netsnmp_session_set_auth_protocol(struct snmp_session *s, zend_string *prot)
981+
static bool snmp_session_set_auth_protocol(struct snmp_session *s, zend_string *prot)
963982
{
964983
#ifndef DISABLE_MD5
965984
if (zend_string_equals_literal_ci(prot, "MD5")) {
@@ -1011,7 +1030,7 @@ static bool netsnmp_session_set_auth_protocol(struct snmp_session *s, zend_strin
10111030
/* }}} */
10121031

10131032
/* {{{ Set the security protocol in the snmpv3 session */
1014-
static bool netsnmp_session_set_sec_protocol(struct snmp_session *s, zend_string *prot)
1033+
static bool snmp_session_set_sec_protocol(struct snmp_session *s, zend_string *prot)
10151034
{
10161035
#ifndef NETSNMP_DISABLE_DES
10171036
if (zend_string_equals_literal_ci(prot, "DES")) {
@@ -1048,7 +1067,7 @@ static bool netsnmp_session_set_sec_protocol(struct snmp_session *s, zend_string
10481067
/* }}} */
10491068

10501069
/* {{{ Make key from pass phrase in the snmpv3 session */
1051-
static bool netsnmp_session_gen_auth_key(struct snmp_session *s, zend_string *pass)
1070+
static bool snmp_session_gen_auth_key(struct snmp_session *s, zend_string *pass)
10521071
{
10531072
int snmp_errno;
10541073
s->securityAuthKeyLen = USM_AUTH_KU_LEN;
@@ -1063,7 +1082,7 @@ static bool netsnmp_session_gen_auth_key(struct snmp_session *s, zend_string *pa
10631082
/* }}} */
10641083

10651084
/* {{{ Make key from pass phrase in the snmpv3 session */
1066-
static bool netsnmp_session_gen_sec_key(struct snmp_session *s, zend_string *pass)
1085+
static bool snmp_session_gen_sec_key(struct snmp_session *s, zend_string *pass)
10671086
{
10681087
int snmp_errno;
10691088

@@ -1079,7 +1098,7 @@ static bool netsnmp_session_gen_sec_key(struct snmp_session *s, zend_string *pas
10791098
/* }}} */
10801099

10811100
/* {{{ Set context Engine Id in the snmpv3 session */
1082-
static bool netsnmp_session_set_contextEngineID(struct snmp_session *s, zend_string * contextEngineID)
1101+
static bool snmp_session_set_contextEngineID(struct snmp_session *s, zend_string * contextEngineID)
10831102
{
10841103
size_t ebuf_len = 32, eout_len = 0;
10851104
uint8_t *ebuf = (uint8_t *) emalloc(ebuf_len);
@@ -1102,40 +1121,40 @@ static bool netsnmp_session_set_contextEngineID(struct snmp_session *s, zend_str
11021121
/* }}} */
11031122

11041123
/* {{{ Set all snmpv3-related security options */
1105-
static bool netsnmp_session_set_security(struct snmp_session *session, zend_string *sec_level,
1124+
static bool snmp_session_set_security(struct snmp_session *session, zend_string *sec_level,
11061125
zend_string *auth_protocol, zend_string *auth_passphrase, zend_string *priv_protocol,
11071126
zend_string *priv_passphrase, zend_string *contextName, zend_string *contextEngineID)
11081127
{
11091128

11101129
/* Setting the security level. */
1111-
if (!netsnmp_session_set_sec_level(session, sec_level)) {
1130+
if (!snmp_session_set_sec_level(session, sec_level)) {
11121131
/* ValueError already generated, just bail out */
11131132
return false;
11141133
}
11151134

11161135
if (session->securityLevel == SNMP_SEC_LEVEL_AUTHNOPRIV || session->securityLevel == SNMP_SEC_LEVEL_AUTHPRIV) {
11171136

11181137
/* Setting the authentication protocol. */
1119-
if (!netsnmp_session_set_auth_protocol(session, auth_protocol)) {
1138+
if (!snmp_session_set_auth_protocol(session, auth_protocol)) {
11201139
/* ValueError already generated, just bail out */
11211140
return false;
11221141
}
11231142

11241143
/* Setting the authentication passphrase. */
1125-
if (!netsnmp_session_gen_auth_key(session, auth_passphrase)) {
1144+
if (!snmp_session_gen_auth_key(session, auth_passphrase)) {
11261145
/* Warning message sent already, just bail out */
11271146
return false;
11281147
}
11291148

11301149
if (session->securityLevel == SNMP_SEC_LEVEL_AUTHPRIV) {
11311150
/* Setting the security protocol. */
1132-
if (!netsnmp_session_set_sec_protocol(session, priv_protocol)) {
1151+
if (!snmp_session_set_sec_protocol(session, priv_protocol)) {
11331152
/* ValueError already generated, just bail out */
11341153
return false;
11351154
}
11361155

11371156
/* Setting the security protocol passphrase. */
1138-
if (!netsnmp_session_gen_sec_key(session, priv_passphrase)) {
1157+
if (!snmp_session_gen_sec_key(session, priv_passphrase)) {
11391158
/* Warning message sent already, just bail out */
11401159
return false;
11411160
}
@@ -1149,7 +1168,7 @@ static bool netsnmp_session_set_security(struct snmp_session *session, zend_stri
11491168
}
11501169

11511170
/* Setting contextEngineIS if specified */
1152-
if (contextEngineID && ZSTR_LEN(contextEngineID) && !netsnmp_session_set_contextEngineID(session, contextEngineID)) {
1171+
if (contextEngineID && ZSTR_LEN(contextEngineID) && !snmp_session_set_contextEngineID(session, contextEngineID)) {
11531172
/* Warning message sent already, just bail out */
11541173
return false;
11551174
}
@@ -1289,14 +1308,14 @@ static void php_snmp(INTERNAL_FUNCTION_PARAMETERS, int st, int version)
12891308
}
12901309

12911310
if (session_less_mode) {
1292-
if (!netsnmp_session_init(&session, version, a1, a2, timeout, retries)) {
1311+
if (!snmp_session_init(&session, version, a1, a2, timeout, retries)) {
12931312
php_free_objid_query(&objid_query, oid_ht, value_ht, st);
1294-
netsnmp_session_free(&session);
1313+
snmp_session_free(&session);
12951314
RETURN_FALSE;
12961315
}
1297-
if (version == SNMP_VERSION_3 && !netsnmp_session_set_security(session, a3, a4, a5, a6, a7, NULL, NULL)) {
1316+
if (version == SNMP_VERSION_3 && !snmp_session_set_security(session, a3, a4, a5, a6, a7, NULL, NULL)) {
12981317
php_free_objid_query(&objid_query, oid_ht, value_ht, st);
1299-
netsnmp_session_free(&session);
1318+
snmp_session_free(&session);
13001319
/* Warning message sent already, just bail out */
13011320
RETURN_FALSE;
13021321
}
@@ -1335,7 +1354,7 @@ static void php_snmp(INTERNAL_FUNCTION_PARAMETERS, int st, int version)
13351354
php_free_objid_query(&objid_query, oid_ht, value_ht, st);
13361355

13371356
if (session_less_mode) {
1338-
netsnmp_session_free(&session);
1357+
snmp_session_free(&session);
13391358
} else {
13401359
netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM, glob_snmp_object.enum_print);
13411360
netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT, glob_snmp_object.quick_print);
@@ -1590,10 +1609,10 @@ PHP_METHOD(SNMP, __construct)
15901609

15911610
/* handle re-open of snmp session */
15921611
if (snmp_object->session) {
1593-
netsnmp_session_free(&(snmp_object->session));
1612+
snmp_session_free(&(snmp_object->session));
15941613
}
15951614

1596-
if (!netsnmp_session_init(&(snmp_object->session), version, a1, a2, timeout, retries)) {
1615+
if (!snmp_session_init(&(snmp_object->session), version, a1, a2, timeout, retries)) {
15971616
return;
15981617
}
15991618
snmp_object->max_oids = 0;
@@ -1618,7 +1637,7 @@ PHP_METHOD(SNMP, close)
16181637
RETURN_THROWS();
16191638
}
16201639

1621-
netsnmp_session_free(&(snmp_object->session));
1640+
snmp_session_free(&(snmp_object->session));
16221641

16231642
RETURN_TRUE;
16241643
}
@@ -1669,7 +1688,7 @@ PHP_METHOD(SNMP, setSecurity)
16691688
RETURN_THROWS();
16701689
}
16711690

1672-
if (!netsnmp_session_set_security(snmp_object->session, a1, a2, a3, a4, a5, a6, a7)) {
1691+
if (!snmp_session_set_security(snmp_object->session, a1, a2, a3, a4, a5, a6, a7)) {
16731692
/* Warning message sent already, just bail out */
16741693
RETURN_FALSE;
16751694
}

0 commit comments

Comments
 (0)