@@ -76,7 +76,7 @@ extern netsnmp_log_handler *logh_head;
76
76
{ \
77
77
snmp_disable_log(); \
78
78
while(NULL != logh_head) \
79
- netsnmp_remove_loghandler ( logh_head ); \
79
+ remove_loghandler ( logh_head ); \
80
80
}
81
81
#endif
82
82
@@ -153,7 +153,7 @@ static PHP_GINIT_FUNCTION(snmp)
153
153
} \
154
154
}
155
155
156
- static void netsnmp_session_free (php_snmp_session * * session ) /* {{{ */
156
+ static void snmp_session_free (php_snmp_session * * session ) /* {{{ */
157
157
{
158
158
if (* session ) {
159
159
PHP_SNMP_SESSION_FREE (peername );
@@ -174,7 +174,7 @@ static void php_snmp_object_free_storage(zend_object *object) /* {{{ */
174
174
return ;
175
175
}
176
176
177
- netsnmp_session_free (& (intern -> session ));
177
+ snmp_session_free (& (intern -> session ));
178
178
179
179
zend_object_std_dtor (& intern -> zo );
180
180
}
@@ -829,19 +829,26 @@ static bool php_snmp_parse_oid(
829
829
}
830
830
/* }}} */
831
831
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()
834
834
*/
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 )
836
836
{
837
837
php_snmp_session * session ;
838
838
char * pptr , * host_ptr ;
839
839
bool force_ipv6 = false;
840
840
int n ;
841
841
struct sockaddr * * psal ;
842
842
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
+
843
849
// 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 ;
845
852
846
853
* session_p = (php_snmp_session * )emalloc (sizeof (php_snmp_session ));
847
854
session = * session_p ;
@@ -853,7 +860,7 @@ static bool netsnmp_session_init(php_snmp_session **session_p, int version, zend
853
860
854
861
session -> peername = emalloc (MAX_NAME_LEN );
855
862
/* 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 );
857
864
host_ptr = session -> peername ;
858
865
859
866
/* 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
862
869
host_ptr ++ ;
863
870
if ((pptr = strchr (host_ptr , ']' ))) {
864
871
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 ;
866
879
}
867
880
* pptr = '\0' ;
868
881
} else {
@@ -871,7 +884,13 @@ static bool netsnmp_session_init(php_snmp_session **session_p, int version, zend
871
884
}
872
885
} else { /* IPv4 address */
873
886
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 ;
875
894
* pptr = '\0' ;
876
895
}
877
896
}
@@ -920,7 +939,7 @@ static bool netsnmp_session_init(php_snmp_session **session_p, int version, zend
920
939
if (remote_port != SNMP_PORT ) {
921
940
size_t peername_length = strlen (session -> peername );
922
941
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 );
924
943
}
925
944
926
945
php_network_freeaddresses (psal );
@@ -942,7 +961,7 @@ static bool netsnmp_session_init(php_snmp_session **session_p, int version, zend
942
961
/* }}} */
943
962
944
963
/* {{{ 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 )
946
965
{
947
966
if (zend_string_equals_literal_ci (level , "noAuthNoPriv" ) || zend_string_equals_literal_ci (level , "nanp" )) {
948
967
s -> securityLevel = SNMP_SEC_LEVEL_NOAUTH ;
@@ -959,7 +978,7 @@ static bool netsnmp_session_set_sec_level(struct snmp_session *s, zend_string *l
959
978
/* }}} */
960
979
961
980
/* {{{ 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 )
963
982
{
964
983
#ifndef DISABLE_MD5
965
984
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
1011
1030
/* }}} */
1012
1031
1013
1032
/* {{{ 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 )
1015
1034
{
1016
1035
#ifndef NETSNMP_DISABLE_DES
1017
1036
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
1048
1067
/* }}} */
1049
1068
1050
1069
/* {{{ 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 )
1052
1071
{
1053
1072
int snmp_errno ;
1054
1073
s -> securityAuthKeyLen = USM_AUTH_KU_LEN ;
@@ -1063,7 +1082,7 @@ static bool netsnmp_session_gen_auth_key(struct snmp_session *s, zend_string *pa
1063
1082
/* }}} */
1064
1083
1065
1084
/* {{{ 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 )
1067
1086
{
1068
1087
int snmp_errno ;
1069
1088
@@ -1079,7 +1098,7 @@ static bool netsnmp_session_gen_sec_key(struct snmp_session *s, zend_string *pas
1079
1098
/* }}} */
1080
1099
1081
1100
/* {{{ 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 )
1083
1102
{
1084
1103
size_t ebuf_len = 32 , eout_len = 0 ;
1085
1104
uint8_t * ebuf = (uint8_t * ) emalloc (ebuf_len );
@@ -1102,40 +1121,40 @@ static bool netsnmp_session_set_contextEngineID(struct snmp_session *s, zend_str
1102
1121
/* }}} */
1103
1122
1104
1123
/* {{{ 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 ,
1106
1125
zend_string * auth_protocol , zend_string * auth_passphrase , zend_string * priv_protocol ,
1107
1126
zend_string * priv_passphrase , zend_string * contextName , zend_string * contextEngineID )
1108
1127
{
1109
1128
1110
1129
/* Setting the security level. */
1111
- if (!netsnmp_session_set_sec_level (session , sec_level )) {
1130
+ if (!snmp_session_set_sec_level (session , sec_level )) {
1112
1131
/* ValueError already generated, just bail out */
1113
1132
return false;
1114
1133
}
1115
1134
1116
1135
if (session -> securityLevel == SNMP_SEC_LEVEL_AUTHNOPRIV || session -> securityLevel == SNMP_SEC_LEVEL_AUTHPRIV ) {
1117
1136
1118
1137
/* Setting the authentication protocol. */
1119
- if (!netsnmp_session_set_auth_protocol (session , auth_protocol )) {
1138
+ if (!snmp_session_set_auth_protocol (session , auth_protocol )) {
1120
1139
/* ValueError already generated, just bail out */
1121
1140
return false;
1122
1141
}
1123
1142
1124
1143
/* Setting the authentication passphrase. */
1125
- if (!netsnmp_session_gen_auth_key (session , auth_passphrase )) {
1144
+ if (!snmp_session_gen_auth_key (session , auth_passphrase )) {
1126
1145
/* Warning message sent already, just bail out */
1127
1146
return false;
1128
1147
}
1129
1148
1130
1149
if (session -> securityLevel == SNMP_SEC_LEVEL_AUTHPRIV ) {
1131
1150
/* Setting the security protocol. */
1132
- if (!netsnmp_session_set_sec_protocol (session , priv_protocol )) {
1151
+ if (!snmp_session_set_sec_protocol (session , priv_protocol )) {
1133
1152
/* ValueError already generated, just bail out */
1134
1153
return false;
1135
1154
}
1136
1155
1137
1156
/* 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 )) {
1139
1158
/* Warning message sent already, just bail out */
1140
1159
return false;
1141
1160
}
@@ -1149,7 +1168,7 @@ static bool netsnmp_session_set_security(struct snmp_session *session, zend_stri
1149
1168
}
1150
1169
1151
1170
/* 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 )) {
1153
1172
/* Warning message sent already, just bail out */
1154
1173
return false;
1155
1174
}
@@ -1289,14 +1308,14 @@ static void php_snmp(INTERNAL_FUNCTION_PARAMETERS, int st, int version)
1289
1308
}
1290
1309
1291
1310
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 )) {
1293
1312
php_free_objid_query (& objid_query , oid_ht , value_ht , st );
1294
- netsnmp_session_free (& session );
1313
+ snmp_session_free (& session );
1295
1314
RETURN_FALSE ;
1296
1315
}
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 )) {
1298
1317
php_free_objid_query (& objid_query , oid_ht , value_ht , st );
1299
- netsnmp_session_free (& session );
1318
+ snmp_session_free (& session );
1300
1319
/* Warning message sent already, just bail out */
1301
1320
RETURN_FALSE ;
1302
1321
}
@@ -1335,7 +1354,7 @@ static void php_snmp(INTERNAL_FUNCTION_PARAMETERS, int st, int version)
1335
1354
php_free_objid_query (& objid_query , oid_ht , value_ht , st );
1336
1355
1337
1356
if (session_less_mode ) {
1338
- netsnmp_session_free (& session );
1357
+ snmp_session_free (& session );
1339
1358
} else {
1340
1359
netsnmp_ds_set_boolean (NETSNMP_DS_LIBRARY_ID , NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM , glob_snmp_object .enum_print );
1341
1360
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)
1590
1609
1591
1610
/* handle re-open of snmp session */
1592
1611
if (snmp_object -> session ) {
1593
- netsnmp_session_free (& (snmp_object -> session ));
1612
+ snmp_session_free (& (snmp_object -> session ));
1594
1613
}
1595
1614
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 )) {
1597
1616
return ;
1598
1617
}
1599
1618
snmp_object -> max_oids = 0 ;
@@ -1618,7 +1637,7 @@ PHP_METHOD(SNMP, close)
1618
1637
RETURN_THROWS ();
1619
1638
}
1620
1639
1621
- netsnmp_session_free (& (snmp_object -> session ));
1640
+ snmp_session_free (& (snmp_object -> session ));
1622
1641
1623
1642
RETURN_TRUE ;
1624
1643
}
@@ -1669,7 +1688,7 @@ PHP_METHOD(SNMP, setSecurity)
1669
1688
RETURN_THROWS ();
1670
1689
}
1671
1690
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 )) {
1673
1692
/* Warning message sent already, just bail out */
1674
1693
RETURN_FALSE ;
1675
1694
}
0 commit comments