xref: /PHP-8.4/ext/snmp/snmp.c (revision 6f5610cc)
1 /*
2    +----------------------------------------------------------------------+
3    | Copyright (c) The PHP Group                                          |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | https://www.php.net/license/3_01.txt                                 |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Authors: Rasmus Lerdorf <rasmus@php.net>                             |
14    |          Mike Jackson <mhjack@tscnet.com>                            |
15    |          Steven Lawrance <slawrance@technologist.com>                |
16    |          Harrie Hazewinkel <harrie@lisanza.net>                      |
17    |          Johann Hanne <jonny@nurfuerspam.de>                         |
18    |          Boris Lytockin <lytboris@gmail.com>                         |
19    +----------------------------------------------------------------------+
20  */
21 
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25 
26 #include "php.h"
27 #include "main/php_network.h"
28 #include "ext/standard/info.h"
29 
30 #ifdef PHP_WIN32
31 // avoid conflicting declarations of (v)asprintf()
32 # define HAVE_ASPRINTF
33 #endif
34 #include "php_snmp.h"
35 
36 #include "zend_exceptions.h"
37 #include "zend_smart_string.h"
38 #include "ext/spl/spl_exceptions.h"
39 
40 #ifdef HAVE_SNMP
41 
42 #include <sys/types.h>
43 #include <errno.h>
44 #ifdef PHP_WIN32
45 #include <winsock2.h>
46 #include <process.h>
47 #include "win32/time.h"
48 #else
49 #include <sys/socket.h>
50 #include <netinet/in.h>
51 #include <arpa/inet.h>
52 #include <netdb.h>
53 #endif
54 #ifdef HAVE_UNISTD_H
55 #include <unistd.h>
56 #endif
57 #include <locale.h>
58 
59 #ifndef __P
60 #ifdef __GNUC__
61 #define __P(args) args
62 #else
63 #define __P(args) ()
64 #endif
65 #endif
66 
67 #include <net-snmp/net-snmp-config.h>
68 #include <net-snmp/net-snmp-includes.h>
69 
70 #include "snmp_arginfo.h"
71 
72 /* For net-snmp prior to 5.4 */
73 #ifndef HAVE_SHUTDOWN_SNMP_LOGGING
74 extern netsnmp_log_handler *logh_head;
75 #define shutdown_snmp_logging() \
76 	{ \
77 		snmp_disable_log(); \
78 		while(NULL != logh_head) \
79 			netsnmp_remove_loghandler( logh_head ); \
80 	}
81 #endif
82 
83 typedef struct snmp_session php_snmp_session;
84 
85 #define PHP_SNMP_ADD_PROPERTIES(a, b) \
86 { \
87 	int i = 0; \
88 	while (b[i].name != NULL) { \
89 		php_snmp_add_property((a), (b)[i].name, (b)[i].name_length, \
90 							(php_snmp_read_t)(b)[i].read_func, (php_snmp_write_t)(b)[i].write_func); \
91 		i++; \
92 	} \
93 }
94 
95 ZEND_DECLARE_MODULE_GLOBALS(snmp)
96 static PHP_GINIT_FUNCTION(snmp);
97 
98 /* constant - can be shared among threads */
99 static const oid objid_mib[] = {1, 3, 6, 1, 2, 1};
100 
101 /* Handlers */
102 static zend_object_handlers php_snmp_object_handlers;
103 
104 /* Class entries */
105 zend_class_entry *php_snmp_ce;
106 zend_class_entry *php_snmp_exception_ce;
107 
108 /* Class object properties */
109 static HashTable php_snmp_properties;
110 
111 struct objid_query {
112 	int count;
113 	int offset;
114 	int step;
115 	zend_long non_repeaters;
116 	zend_long max_repetitions;
117 	int valueretrieval;
118 	bool array_output;
119 	bool oid_increasing_check;
120 	snmpobjarg *vars;
121 };
122 
123 /* query an agent with GET method */
124 #define SNMP_CMD_GET		(1<<0)
125 /* query an agent with GETNEXT method */
126 #define SNMP_CMD_GETNEXT	(1<<1)
127 /* query an agent with SET method */
128 #define SNMP_CMD_SET		(1<<2)
129 /* walk the mib */
130 #define SNMP_CMD_WALK		(1<<3)
131 /* force values-only output */
132 #define SNMP_NUMERIC_KEYS	(1<<7)
133 /* use user-supplied OID names for keys in array output mode in GET method */
134 #define SNMP_ORIGINAL_NAMES_AS_KEYS	(1<<8)
135 /* use OID suffix (`index') for keys in array output mode in WALK  method */
136 #define SNMP_USE_SUFFIX_AS_KEYS	(1<<9)
137 
138 #ifdef COMPILE_DL_SNMP
139 ZEND_GET_MODULE(snmp)
140 #endif
141 
142 /* {{{ PHP_GINIT_FUNCTION */
PHP_GINIT_FUNCTION(snmp)143 static PHP_GINIT_FUNCTION(snmp)
144 {
145 	snmp_globals->valueretrieval = SNMP_VALUE_LIBRARY;
146 }
147 /* }}} */
148 
149 #define PHP_SNMP_SESSION_FREE(a) { \
150 	if ((*session)->a) { \
151 		efree((*session)->a); \
152 		(*session)->a = NULL; \
153 	} \
154 }
155 
netsnmp_session_free(php_snmp_session ** session)156 static void netsnmp_session_free(php_snmp_session **session) /* {{{ */
157 {
158 	if (*session) {
159 		PHP_SNMP_SESSION_FREE(peername);
160 		PHP_SNMP_SESSION_FREE(community);
161 		PHP_SNMP_SESSION_FREE(securityName);
162 		PHP_SNMP_SESSION_FREE(contextEngineID);
163 		efree(*session);
164 		*session = NULL;
165 	}
166 }
167 /* }}} */
168 
php_snmp_object_free_storage(zend_object * object)169 static void php_snmp_object_free_storage(zend_object *object) /* {{{ */
170 {
171 	php_snmp_object *intern = php_snmp_fetch_object(object);
172 
173 	if (!intern) {
174 		return;
175 	}
176 
177 	netsnmp_session_free(&(intern->session));
178 
179 	zend_object_std_dtor(&intern->zo);
180 }
181 /* }}} */
182 
php_snmp_object_new(zend_class_entry * class_type)183 static zend_object *php_snmp_object_new(zend_class_entry *class_type) /* {{{ */
184 {
185 	php_snmp_object *intern;
186 
187 	/* Allocate memory for it */
188 	intern = zend_object_alloc(sizeof(php_snmp_object), class_type);
189 
190 	zend_object_std_init(&intern->zo, class_type);
191 	object_properties_init(&intern->zo, class_type);
192 
193 	return &intern->zo;
194 
195 }
196 /* }}} */
197 
198 /* {{{ php_snmp_error
199  *
200  * Record last SNMP-related error in object
201  *
202  */
php_snmp_error(zval * object,int type,const char * format,...)203 static void php_snmp_error(zval *object, int type, const char *format, ...)
204 {
205 	va_list args;
206 	php_snmp_object *snmp_object = NULL;
207 
208 	if (object) {
209 		snmp_object = Z_SNMP_P(object);
210 		if (type == PHP_SNMP_ERRNO_NOERROR) {
211 			memset(snmp_object->snmp_errstr, 0, sizeof(snmp_object->snmp_errstr));
212 		} else {
213 			va_start(args, format);
214 			vsnprintf(snmp_object->snmp_errstr, sizeof(snmp_object->snmp_errstr) - 1, format, args);
215 			va_end(args);
216 		}
217 		snmp_object->snmp_errno = type;
218 	}
219 
220 	if (type == PHP_SNMP_ERRNO_NOERROR) {
221 		return;
222 	}
223 
224 	if (object && (snmp_object->exceptions_enabled & type)) {
225 		zend_throw_exception_ex(php_snmp_exception_ce, type, "%s", snmp_object->snmp_errstr);
226 	} else {
227 		va_start(args, format);
228 		php_verror(NULL, "", E_WARNING, format, args);
229 		va_end(args);
230 	}
231 }
232 
233 /* }}} */
234 
235 /* {{{ php_snmp_getvalue
236 *
237 * SNMP value to zval converter
238 *
239 */
php_snmp_getvalue(struct variable_list * vars,zval * snmpval,int valueretrieval)240 static void php_snmp_getvalue(struct variable_list *vars, zval *snmpval, int valueretrieval)
241 {
242 	zval val;
243 	char sbuf[512];
244 	char *buf = &(sbuf[0]);
245 	char *dbuf = (char *)NULL;
246 	int buflen = sizeof(sbuf) - 1;
247 	int val_len = vars->val_len;
248 
249 	/* use emalloc() for large values, use static array otherwise */
250 
251 	/* There is no way to know the size of buffer snprint_value() needs in order to print a value there.
252 	 * So we are forced to probe it
253 	 */
254 	while ((valueretrieval & SNMP_VALUE_PLAIN) == 0) {
255 		*buf = '\0';
256 		if (snprint_value(buf, buflen, vars->name, vars->name_length, vars) == -1) {
257 			if (val_len > 512*1024) {
258 				php_error_docref(NULL, E_WARNING, "snprint_value() asks for a buffer more than 512k, Net-SNMP bug?");
259 				break;
260 			}
261 			 /* buffer is not long enough to hold full output, double it */
262 			val_len *= 2;
263 		} else {
264 			break;
265 		}
266 
267 		if (buf == dbuf) {
268 			dbuf = (char *)erealloc(dbuf, val_len + 1);
269 		} else {
270 			dbuf = (char *)emalloc(val_len + 1);
271 		}
272 
273 		buf = dbuf;
274 		buflen = val_len;
275 	}
276 
277 	if((valueretrieval & SNMP_VALUE_PLAIN) && val_len > buflen){
278 		dbuf = (char *)emalloc(val_len + 1);
279 		buf = dbuf;
280 		buflen = val_len;
281 	}
282 
283 	if (valueretrieval & SNMP_VALUE_PLAIN) {
284 		*buf = 0;
285 		switch (vars->type) {
286 		case ASN_BIT_STR:		/* 0x03, asn1.h */
287 			ZVAL_STRINGL(&val, (char *)vars->val.bitstring, vars->val_len);
288 			break;
289 
290 		case ASN_OCTET_STR:		/* 0x04, asn1.h */
291 		case ASN_OPAQUE:		/* 0x44, snmp_impl.h */
292 			ZVAL_STRINGL(&val, (char *)vars->val.string, vars->val_len);
293 			break;
294 
295 		case ASN_NULL:			/* 0x05, asn1.h */
296 			ZVAL_NULL(&val);
297 			break;
298 
299 		case ASN_OBJECT_ID:		/* 0x06, asn1.h */
300 			snprint_objid(buf, buflen, vars->val.objid, vars->val_len / sizeof(oid));
301 			ZVAL_STRING(&val, buf);
302 			break;
303 
304 		case ASN_IPADDRESS:		/* 0x40, snmp_impl.h */
305 			snprintf(buf, buflen, "%d.%d.%d.%d",
306 				 (vars->val.string)[0], (vars->val.string)[1],
307 				 (vars->val.string)[2], (vars->val.string)[3]);
308 			buf[buflen]=0;
309 			ZVAL_STRING(&val, buf);
310 			break;
311 
312 		case ASN_COUNTER:		/* 0x41, snmp_impl.h */
313 		case ASN_GAUGE:			/* 0x42, snmp_impl.h */
314 		/* ASN_UNSIGNED is the same as ASN_GAUGE */
315 		case ASN_TIMETICKS:		/* 0x43, snmp_impl.h */
316 		case ASN_UINTEGER:		/* 0x47, snmp_impl.h */
317 			snprintf(buf, buflen, "%lu", *vars->val.integer);
318 			buf[buflen]=0;
319 			ZVAL_STRING(&val, buf);
320 			break;
321 
322 		case ASN_INTEGER:		/* 0x02, asn1.h */
323 			snprintf(buf, buflen, "%ld", *vars->val.integer);
324 			buf[buflen]=0;
325 			ZVAL_STRING(&val, buf);
326 			break;
327 
328 #if defined(NETSNMP_WITH_OPAQUE_SPECIAL_TYPES) || defined(OPAQUE_SPECIAL_TYPES)
329 		case ASN_OPAQUE_FLOAT:		/* 0x78, asn1.h */
330 			snprintf(buf, buflen, "%f", *vars->val.floatVal);
331 			ZVAL_STRING(&val, buf);
332 			break;
333 
334 		case ASN_OPAQUE_DOUBLE:		/* 0x79, asn1.h */
335 			snprintf(buf, buflen, "%f", *vars->val.doubleVal);
336 			ZVAL_STRING(&val, buf);
337 			break;
338 
339 		case ASN_OPAQUE_I64:		/* 0x80, asn1.h */
340 			printI64(buf, vars->val.counter64);
341 			ZVAL_STRING(&val, buf);
342 			break;
343 
344 		case ASN_OPAQUE_U64:		/* 0x81, asn1.h */
345 #endif
346 		case ASN_COUNTER64:		/* 0x46, snmp_impl.h */
347 			printU64(buf, vars->val.counter64);
348 			ZVAL_STRING(&val, buf);
349 			break;
350 
351 		default:
352 			ZVAL_STRING(&val, "Unknown value type");
353 			php_error_docref(NULL, E_WARNING, "Unknown value type: %u", vars->type);
354 			break;
355 		}
356 	} else /* use Net-SNMP value translation */ {
357 		/* we have desired string in buffer, just use it */
358 		ZVAL_STRING(&val, buf);
359 	}
360 
361 	if (valueretrieval & SNMP_VALUE_OBJECT) {
362 		object_init(snmpval);
363 		add_property_long(snmpval, "type", vars->type);
364 		add_property_zval(snmpval, "value", &val);
365 	} else  {
366 		ZVAL_COPY(snmpval, &val);
367 	}
368 	zval_ptr_dtor(&val);
369 
370 	if (dbuf){ /* malloc was used to store value */
371 		efree(dbuf);
372 	}
373 }
374 /* }}} */
375 
376 /* {{{ php_snmp_internal
377 *
378 * SNMP object fetcher/setter for all SNMP versions
379 *
380 */
php_snmp_internal(INTERNAL_FUNCTION_PARAMETERS,int st,struct snmp_session * session,struct objid_query * objid_query)381 static void php_snmp_internal(INTERNAL_FUNCTION_PARAMETERS, int st,
382 							struct snmp_session *session,
383 							struct objid_query *objid_query)
384 {
385 	struct snmp_session *ss;
386 	struct snmp_pdu *pdu=NULL, *response;
387 	struct variable_list *vars;
388 	oid root[MAX_NAME_LEN];
389 	size_t rootlen = 0;
390 	int status, count, found;
391 	char buf[2048];
392 	char buf2[2048];
393 	bool keepwalking = true;
394 	char *err;
395 	zval snmpval;
396 	int snmp_errno;
397 
398 	/* we start with retval=FALSE. If any actual data is acquired, retval will be set to appropriate type */
399 	RETVAL_FALSE;
400 
401 	/* reset errno and errstr */
402 	php_snmp_error(getThis(), PHP_SNMP_ERRNO_NOERROR, "");
403 
404 	if (st & SNMP_CMD_WALK) { /* remember root OID */
405 		memcpy((char *)root, (char *)(objid_query->vars[0].name), (objid_query->vars[0].name_length) * sizeof(oid));
406 		rootlen = objid_query->vars[0].name_length;
407 		objid_query->offset = objid_query->count;
408 	}
409 
410 	if ((ss = snmp_open(session)) == NULL) {
411 		snmp_error(session, NULL, NULL, &err);
412 		php_error_docref(NULL, E_WARNING, "Could not open snmp connection: %s", err);
413 		free(err);
414 		RETURN_FALSE;
415 	}
416 
417 	if ((st & SNMP_CMD_SET) && objid_query->count > objid_query->step) {
418 		php_snmp_error(getThis(), PHP_SNMP_ERRNO_MULTIPLE_SET_QUERIES, "Cannot fit all OIDs for SET query into one packet, using multiple queries");
419 	}
420 
421 	while (keepwalking) {
422 		keepwalking = false;
423 		if (st & SNMP_CMD_WALK) {
424 			if (session->version == SNMP_VERSION_1) {
425 				pdu = snmp_pdu_create(SNMP_MSG_GETNEXT);
426 			} else {
427 				pdu = snmp_pdu_create(SNMP_MSG_GETBULK);
428 				pdu->non_repeaters = objid_query->non_repeaters;
429 				pdu->max_repetitions = objid_query->max_repetitions;
430 			}
431 			snmp_add_null_var(pdu, objid_query->vars[0].name, objid_query->vars[0].name_length);
432 		} else {
433 			if (st & SNMP_CMD_GET) {
434 				pdu = snmp_pdu_create(SNMP_MSG_GET);
435 			} else if (st & SNMP_CMD_GETNEXT) {
436 				pdu = snmp_pdu_create(SNMP_MSG_GETNEXT);
437 			} else if (st & SNMP_CMD_SET) {
438 				pdu = snmp_pdu_create(SNMP_MSG_SET);
439 			} else {
440 				snmp_close(ss);
441 				php_error_docref(NULL, E_ERROR, "Unknown SNMP command (internals)");
442 				RETURN_FALSE;
443 			}
444 			for (count = 0; objid_query->offset < objid_query->count && count < objid_query->step; objid_query->offset++, count++){
445 				if (st & SNMP_CMD_SET) {
446 					if ((snmp_errno = snmp_add_var(pdu, objid_query->vars[objid_query->offset].name, objid_query->vars[objid_query->offset].name_length, objid_query->vars[objid_query->offset].type, objid_query->vars[objid_query->offset].value))) {
447 						snprint_objid(buf, sizeof(buf), objid_query->vars[objid_query->offset].name, objid_query->vars[objid_query->offset].name_length);
448 						php_snmp_error(getThis(), PHP_SNMP_ERRNO_OID_PARSING_ERROR, "Could not add variable: OID='%s' type='%c' value='%s': %s", buf, objid_query->vars[objid_query->offset].type, objid_query->vars[objid_query->offset].value, snmp_api_errstring(snmp_errno));
449 						snmp_free_pdu(pdu);
450 						snmp_close(ss);
451 						RETURN_FALSE;
452 					}
453 				} else {
454 					snmp_add_null_var(pdu, objid_query->vars[objid_query->offset].name, objid_query->vars[objid_query->offset].name_length);
455 				}
456 			}
457 			if(pdu->variables == NULL){
458 				snmp_free_pdu(pdu);
459 				snmp_close(ss);
460 				RETURN_FALSE;
461 			}
462 		}
463 
464 retry:
465 		status = snmp_synch_response(ss, pdu, &response);
466 		if (status == STAT_SUCCESS) {
467 			if (response->errstat == SNMP_ERR_NOERROR) {
468 				if (st & SNMP_CMD_SET) {
469 					if (objid_query->offset < objid_query->count) { /* we have unprocessed OIDs */
470 						keepwalking = true;
471 						snmp_free_pdu(response);
472 						continue;
473 					}
474 					snmp_free_pdu(response);
475 					snmp_close(ss);
476 					RETURN_TRUE;
477 				}
478 				for (vars = response->variables; vars; vars = vars->next_variable) {
479 					/* do not output errors as values */
480 					if ( 	vars->type == SNMP_ENDOFMIBVIEW ||
481 						vars->type == SNMP_NOSUCHOBJECT ||
482 						vars->type == SNMP_NOSUCHINSTANCE ) {
483 						if ((st & SNMP_CMD_WALK) && Z_TYPE_P(return_value) == IS_ARRAY) {
484 							break;
485 						}
486 						snprint_objid(buf, sizeof(buf), vars->name, vars->name_length);
487 						snprint_value(buf2, sizeof(buf2), vars->name, vars->name_length, vars);
488 						php_snmp_error(getThis(), PHP_SNMP_ERRNO_ERROR_IN_REPLY, "Error in packet at '%s': %s", buf, buf2);
489 						continue;
490 					}
491 
492 					if ((st & SNMP_CMD_WALK) &&
493 						(vars->name_length < rootlen || memcmp(root, vars->name, rootlen * sizeof(oid)))) { /* not part of this subtree */
494 						if (Z_TYPE_P(return_value) == IS_ARRAY) { /* some records are fetched already, shut down further lookup */
495 							keepwalking = false;
496 						} else {
497 							/* first fetched OID is out of subtree, fallback to GET query */
498 							st |= SNMP_CMD_GET;
499 							st ^= SNMP_CMD_WALK;
500 							objid_query->offset = 0;
501 							keepwalking = true;
502 						}
503 						break;
504 					}
505 
506 					ZVAL_NULL(&snmpval);
507 					php_snmp_getvalue(vars, &snmpval, objid_query->valueretrieval);
508 
509 					if (objid_query->array_output) {
510 						if (Z_TYPE_P(return_value) == IS_TRUE || Z_TYPE_P(return_value) == IS_FALSE) {
511 							array_init(return_value);
512 						}
513 						if (st & SNMP_NUMERIC_KEYS) {
514 							add_next_index_zval(return_value, &snmpval);
515 						} else if (st & SNMP_ORIGINAL_NAMES_AS_KEYS && st & SNMP_CMD_GET) {
516 							found = 0;
517 							for (count = 0; count < objid_query->count; count++) {
518 								if (objid_query->vars[count].name_length == vars->name_length && snmp_oid_compare(objid_query->vars[count].name, objid_query->vars[count].name_length, vars->name, vars->name_length) == 0) {
519 									found = 1;
520 									objid_query->vars[count].name_length = 0; /* mark this name as used */
521 									break;
522 								}
523 							}
524 							if (found) {
525 								add_assoc_zval(return_value, objid_query->vars[count].oid, &snmpval);
526 							} else {
527 								snprint_objid(buf2, sizeof(buf2), vars->name, vars->name_length);
528 								php_error_docref(NULL, E_WARNING, "Could not find original OID name for '%s'", buf2);
529 							}
530 						} else if (st & SNMP_USE_SUFFIX_AS_KEYS && st & SNMP_CMD_WALK) {
531 							snprint_objid(buf2, sizeof(buf2), vars->name, vars->name_length);
532 							if (rootlen <= vars->name_length && snmp_oid_compare(root, rootlen, vars->name, rootlen) == 0) {
533 								buf2[0] = '\0';
534 								count = rootlen;
535 								while(count < vars->name_length){
536 									snprintf(buf, sizeof(buf), "%lu.", vars->name[count]);
537 									strcat(buf2, buf);
538 									count++;
539 								}
540 								buf2[strlen(buf2) - 1] = '\0'; /* remove trailing '.' */
541 							}
542 							add_assoc_zval(return_value, buf2, &snmpval);
543 						} else {
544 							snprint_objid(buf2, sizeof(buf2), vars->name, vars->name_length);
545 							add_assoc_zval(return_value, buf2, &snmpval);
546 						}
547 					} else {
548 						ZVAL_COPY_VALUE(return_value, &snmpval);
549 						break;
550 					}
551 
552 					/* OID increase check */
553 					if (st & SNMP_CMD_WALK) {
554 						if (objid_query->oid_increasing_check && snmp_oid_compare(objid_query->vars[0].name, objid_query->vars[0].name_length, vars->name, vars->name_length) >= 0) {
555 							snprint_objid(buf2, sizeof(buf2), vars->name, vars->name_length);
556 							php_snmp_error(getThis(), PHP_SNMP_ERRNO_OID_NOT_INCREASING, "Error: OID not increasing: %s", buf2);
557 							keepwalking = false;
558 						} else {
559 							memcpy((char *)(objid_query->vars[0].name), (char *)vars->name, vars->name_length * sizeof(oid));
560 							objid_query->vars[0].name_length = vars->name_length;
561 							keepwalking = true;
562 						}
563 					}
564 				}
565 				if (objid_query->offset < objid_query->count) { /* we have unprocessed OIDs */
566 					keepwalking = true;
567 				}
568 			} else {
569 				if (st & SNMP_CMD_WALK && response->errstat == SNMP_ERR_TOOBIG && objid_query->max_repetitions > 1) { /* Answer will not fit into single packet */
570 					objid_query->max_repetitions /= 2;
571 					snmp_free_pdu(response);
572 					keepwalking = true;
573 					continue;
574 				}
575 				if (!(st & SNMP_CMD_WALK) || response->errstat != SNMP_ERR_NOSUCHNAME || Z_TYPE_P(return_value) == IS_TRUE || Z_TYPE_P(return_value) == IS_FALSE) {
576 					for (count=1, vars = response->variables;
577 						vars && count != response->errindex;
578 						vars = vars->next_variable, count++);
579 
580 					if (st & (SNMP_CMD_GET | SNMP_CMD_GETNEXT) && response->errstat == SNMP_ERR_TOOBIG && objid_query->step > 1) { /* Answer will not fit into single packet */
581 						objid_query->offset = ((objid_query->offset > objid_query->step) ? (objid_query->offset - objid_query->step) : 0 );
582 						objid_query->step /= 2;
583 						snmp_free_pdu(response);
584 						keepwalking = true;
585 						continue;
586 					}
587 					if (vars) {
588 						snprint_objid(buf, sizeof(buf), vars->name, vars->name_length);
589 						php_snmp_error(getThis(), PHP_SNMP_ERRNO_ERROR_IN_REPLY, "Error in packet at '%s': %s", buf, snmp_errstring(response->errstat));
590 					} else {
591 						php_snmp_error(getThis(), PHP_SNMP_ERRNO_ERROR_IN_REPLY, "Error in packet at %u object_id: %s", response->errindex, snmp_errstring(response->errstat));
592 					}
593 					if (st & (SNMP_CMD_GET | SNMP_CMD_GETNEXT)) { /* cut out bogus OID and retry */
594 						if ((pdu = snmp_fix_pdu(response, ((st & SNMP_CMD_GET) ? SNMP_MSG_GET : SNMP_MSG_GETNEXT) )) != NULL) {
595 							snmp_free_pdu(response);
596 							goto retry;
597 						}
598 					}
599 					snmp_free_pdu(response);
600 					snmp_close(ss);
601 					if (objid_query->array_output) {
602 						zval_ptr_dtor(return_value);
603 					}
604 					RETURN_FALSE;
605 				}
606 			}
607 		} else if (status == STAT_TIMEOUT) {
608 			php_snmp_error(getThis(), PHP_SNMP_ERRNO_TIMEOUT, "No response from %s", session->peername);
609 			if (objid_query->array_output) {
610 				zval_ptr_dtor(return_value);
611 			}
612 			snmp_close(ss);
613 			RETURN_FALSE;
614 		} else {    /* status == STAT_ERROR */
615 			snmp_error(ss, NULL, NULL, &err);
616 			php_snmp_error(getThis(), PHP_SNMP_ERRNO_GENERIC, "Fatal error: %s", err);
617 			free(err);
618 			if (objid_query->array_output) {
619 				zval_ptr_dtor(return_value);
620 			}
621 			snmp_close(ss);
622 			RETURN_FALSE;
623 		}
624 		if (response) {
625 			snmp_free_pdu(response);
626 		}
627 	} /* keepwalking */
628 	snmp_close(ss);
629 }
630 /* }}} */
631 
632 /* {{{ php_snmp_parse_oid
633 *
634 * OID parser (and type, value for SNMP_SET command)
635 */
php_snmp_parse_oid(zval * object,int st,struct objid_query * objid_query,zend_string * oid_str,HashTable * oid_ht,zend_string * type_str,HashTable * type_ht,zend_string * value_str,HashTable * value_ht)636 static bool php_snmp_parse_oid(
637 	zval *object, int st, struct objid_query *objid_query, zend_string *oid_str, HashTable *oid_ht,
638 	zend_string *type_str, HashTable *type_ht, zend_string *value_str, HashTable *value_ht
639 ) {
640 	char *pptr;
641 	uint32_t idx_type = 0, idx_value = 0;
642 	zval *tmp_oid, *tmp_type, *tmp_value;
643 
644 	objid_query->count = 0;
645 	objid_query->array_output = (st & SNMP_CMD_WALK) != 0;
646 	if (oid_str) {
647 		objid_query->vars = (snmpobjarg *)emalloc(sizeof(snmpobjarg));
648 		objid_query->vars[objid_query->count].oid = ZSTR_VAL(oid_str);
649 		if (st & SNMP_CMD_SET) {
650 			if (type_ht) {
651 				zend_type_error("Type must be of type string when object ID is a string");
652 				efree(objid_query->vars);
653 				return false;
654 			}
655 			if (value_ht) {
656 				zend_type_error("Value must be of type string when object ID is a string");
657 				efree(objid_query->vars);
658 				return false;
659 			}
660 
661 			/* Both type and value must be valid strings */
662 			ZEND_ASSERT(type_str && value_str);
663 
664 			if (ZSTR_LEN(type_str) != 1) {
665 				zend_value_error("Type must be a single character");
666 				efree(objid_query->vars);
667 				return false;
668 			}
669 			pptr = ZSTR_VAL(type_str);
670 			objid_query->vars[objid_query->count].type = *pptr;
671 			objid_query->vars[objid_query->count].value = ZSTR_VAL(value_str);
672 		}
673 		objid_query->count++;
674 	} else if (oid_ht) { /* we got objid array */
675 		if (zend_hash_num_elements(oid_ht) == 0) {
676 			zend_value_error("Array of object IDs must not be empty");
677 			return false;
678 		}
679 		objid_query->vars = (snmpobjarg *)safe_emalloc(sizeof(snmpobjarg), zend_hash_num_elements(oid_ht), 0);
680 		objid_query->array_output = (st & SNMP_CMD_SET) == 0;
681 		ZEND_HASH_FOREACH_VAL(oid_ht, tmp_oid) {
682 			convert_to_string(tmp_oid);
683 			objid_query->vars[objid_query->count].oid = Z_STRVAL_P(tmp_oid);
684 			if (st & SNMP_CMD_SET) {
685 				if (type_str) {
686 					pptr = ZSTR_VAL(type_str);
687 					objid_query->vars[objid_query->count].type = *pptr;
688 				} else if (type_ht) {
689 					if (HT_IS_PACKED(type_ht)) {
690 						while (idx_type < type_ht->nNumUsed) {
691 							tmp_type = &type_ht->arPacked[idx_type];
692 							if (Z_TYPE_P(tmp_type) != IS_UNDEF) {
693 								break;
694 							}
695 							idx_type++;
696 						}
697 					} else {
698 						while (idx_type < type_ht->nNumUsed) {
699 							tmp_type = &type_ht->arData[idx_type].val;
700 							if (Z_TYPE_P(tmp_type) != IS_UNDEF) {
701 								break;
702 							}
703 							idx_type++;
704 						}
705 					}
706 					if (idx_type < type_ht->nNumUsed) {
707 						convert_to_string(tmp_type);
708 						if (Z_STRLEN_P(tmp_type) != 1) {
709 							zend_value_error("Type must be a single character");
710 							efree(objid_query->vars);
711 							return false;
712 						}
713 						pptr = Z_STRVAL_P(tmp_type);
714 						objid_query->vars[objid_query->count].type = *pptr;
715 						idx_type++;
716 					} else {
717 						php_error_docref(NULL, E_WARNING, "'%s': no type set", Z_STRVAL_P(tmp_oid));
718 						efree(objid_query->vars);
719 						return false;
720 					}
721 				}
722 
723 				if (value_str) {
724 					objid_query->vars[objid_query->count].value = ZSTR_VAL(value_str);
725 				} else if (value_ht) {
726 					if (HT_IS_PACKED(value_ht)) {
727 						while (idx_value < value_ht->nNumUsed) {
728 							tmp_value = &value_ht->arPacked[idx_value];
729 							if (Z_TYPE_P(tmp_value) != IS_UNDEF) {
730 								break;
731 							}
732 							idx_value++;
733 						}
734 					} else {
735 						while (idx_value < value_ht->nNumUsed) {
736 							tmp_value = &value_ht->arData[idx_value].val;
737 							if (Z_TYPE_P(tmp_value) != IS_UNDEF) {
738 								break;
739 							}
740 							idx_value++;
741 						}
742 					}
743 					if (idx_value < value_ht->nNumUsed) {
744 						convert_to_string(tmp_value);
745 						objid_query->vars[objid_query->count].value = Z_STRVAL_P(tmp_value);
746 						idx_value++;
747 					} else {
748 						php_error_docref(NULL, E_WARNING, "'%s': no value set", Z_STRVAL_P(tmp_oid));
749 						efree(objid_query->vars);
750 						return false;
751 					}
752 				}
753 			}
754 			objid_query->count++;
755 		} ZEND_HASH_FOREACH_END();
756 	}
757 
758 	/* now parse all OIDs */
759 	if (st & SNMP_CMD_WALK) {
760 		if (objid_query->count > 1) {
761 			php_snmp_error(object, PHP_SNMP_ERRNO_OID_PARSING_ERROR, "Multi OID walks are not supported!");
762 			efree(objid_query->vars);
763 			return false;
764 		}
765 		objid_query->vars[0].name_length = MAX_NAME_LEN;
766 		if (strlen(objid_query->vars[0].oid)) { /* on a walk, an empty string means top of tree - no error */
767 			if (!snmp_parse_oid(objid_query->vars[0].oid, objid_query->vars[0].name, &(objid_query->vars[0].name_length))) {
768 				php_snmp_error(object, PHP_SNMP_ERRNO_OID_PARSING_ERROR, "Invalid object identifier: %s", objid_query->vars[0].oid);
769 				efree(objid_query->vars);
770 				return false;
771 			}
772 		} else {
773 			memmove((char *)objid_query->vars[0].name, (const char *)objid_mib, sizeof(objid_mib));
774 			objid_query->vars[0].name_length = sizeof(objid_mib) / sizeof(oid);
775 		}
776 	} else {
777 		for (objid_query->offset = 0; objid_query->offset < objid_query->count; objid_query->offset++) {
778 			objid_query->vars[objid_query->offset].name_length = MAX_OID_LEN;
779 			if (!snmp_parse_oid(objid_query->vars[objid_query->offset].oid, objid_query->vars[objid_query->offset].name, &(objid_query->vars[objid_query->offset].name_length))) {
780 				php_snmp_error(object, PHP_SNMP_ERRNO_OID_PARSING_ERROR, "Invalid object identifier: %s", objid_query->vars[objid_query->offset].oid);
781 				efree(objid_query->vars);
782 				return false;
783 			}
784 		}
785 	}
786 	objid_query->offset = 0;
787 	objid_query->step = objid_query->count;
788 	return (objid_query->count > 0);
789 }
790 /* }}} */
791 
792 /* {{{ netsnmp_session_init
793 	allocates memory for session and session->peername, caller should free it manually using netsnmp_session_free() and efree()
794 */
netsnmp_session_init(php_snmp_session ** session_p,int version,zend_string * hostname,zend_string * community,int timeout,int retries)795 static bool netsnmp_session_init(php_snmp_session **session_p, int version, zend_string *hostname, zend_string *community, int timeout, int retries)
796 {
797 	php_snmp_session *session;
798 	char *pptr, *host_ptr;
799 	bool force_ipv6 = false;
800 	int n;
801 	struct sockaddr **psal;
802 	struct sockaddr **res;
803 	// TODO: Do not strip and re-add the port in peername?
804 	unsigned remote_port = SNMP_PORT;
805 
806 	*session_p = (php_snmp_session *)emalloc(sizeof(php_snmp_session));
807 	session = *session_p;
808 	memset(session, 0, sizeof(php_snmp_session));
809 
810 	snmp_sess_init(session);
811 
812 	session->version = version;
813 
814 	session->peername = emalloc(MAX_NAME_LEN);
815 	/* we copy original hostname for further processing */
816 	strlcpy(session->peername, ZSTR_VAL(hostname), MAX_NAME_LEN);
817 	host_ptr = session->peername;
818 
819 	/* Reading the hostname and its optional non-default port number */
820 	if (*host_ptr == '[') { /* IPv6 address */
821 		force_ipv6 = true;
822 		host_ptr++;
823 		if ((pptr = strchr(host_ptr, ']'))) {
824 			if (pptr[1] == ':') {
825 				remote_port = atoi(pptr + 2);
826 			}
827 			*pptr = '\0';
828 		} else {
829 			php_error_docref(NULL, E_WARNING, "Malformed IPv6 address, closing square bracket missing");
830 			return false;
831 		}
832 	} else { /* IPv4 address */
833 		if ((pptr = strchr(host_ptr, ':'))) {
834 			remote_port = atoi(pptr + 1);
835 			*pptr = '\0';
836 		}
837 	}
838 
839 	/* since Net-SNMP library requires 'udp6:' prefix for all IPv6 addresses (in FQDN form too) we need to
840 	   perform possible name resolution before running any SNMP queries */
841 	if ((n = php_network_getaddresses(host_ptr, SOCK_DGRAM, &psal, NULL)) == 0) { /* some resolver error */
842 		/* warnings sent, bailing out */
843 		return false;
844 	}
845 
846 	/* we have everything we need in psal, flush peername and fill it properly */
847 	*(session->peername) = '\0';
848 	res = psal;
849 	while (n-- > 0) {
850 		pptr = session->peername;
851 #if defined(HAVE_GETADDRINFO) && defined(HAVE_IPV6)
852 		if (force_ipv6 && (*res)->sa_family != AF_INET6) {
853 			res++;
854 			continue;
855 		}
856 		if ((*res)->sa_family == AF_INET6) {
857 			strcpy(session->peername, "udp6:[");
858 			pptr = session->peername + strlen(session->peername);
859 			inet_ntop((*res)->sa_family, &(((struct sockaddr_in6*)(*res))->sin6_addr), pptr, MAX_NAME_LEN);
860 			strcat(pptr, "]");
861 		} else if ((*res)->sa_family == AF_INET) {
862 			inet_ntop((*res)->sa_family, &(((struct sockaddr_in*)(*res))->sin_addr), pptr, MAX_NAME_LEN);
863 		} else {
864 			res++;
865 			continue;
866 		}
867 #endif
868 		break;
869 	}
870 
871 	if (strlen(session->peername) == 0) {
872 		php_error_docref(NULL, E_WARNING, "Unknown failure while resolving '%s'", ZSTR_VAL(hostname));
873 		return false;
874 	}
875 	/* XXX FIXME
876 		There should be check for non-empty session->peername!
877 	*/
878 
879 	/* put back non-standard SNMP port */
880 	if (remote_port != SNMP_PORT) {
881 		size_t peername_length = strlen(session->peername);
882 		pptr = session->peername + peername_length;
883 		snprintf(pptr, MAX_NAME_LEN - peername_length, ":%d", remote_port);
884 	}
885 
886 	php_network_freeaddresses(psal);
887 
888 	if (version == SNMP_VERSION_3) {
889 		/* Setting the security name. */
890 		session->securityName = estrdup(ZSTR_VAL(community));
891 		session->securityNameLen = ZSTR_LEN(community);
892 	} else {
893 		session->authenticator = NULL;
894 		session->community = (uint8_t *)estrdup(ZSTR_VAL(community));
895 		session->community_len = ZSTR_LEN(community);
896 	}
897 
898 	session->retries = retries;
899 	session->timeout = timeout;
900 	return true;
901 }
902 /* }}} */
903 
904 /* {{{ Set the security level in the snmpv3 session */
netsnmp_session_set_sec_level(struct snmp_session * s,zend_string * level)905 static bool netsnmp_session_set_sec_level(struct snmp_session *s, zend_string *level)
906 {
907 	if (zend_string_equals_literal_ci(level, "noAuthNoPriv") || zend_string_equals_literal_ci(level, "nanp")) {
908 		s->securityLevel = SNMP_SEC_LEVEL_NOAUTH;
909 	} else if (zend_string_equals_literal_ci(level, "authNoPriv") || zend_string_equals_literal_ci(level, "anp")) {
910 		s->securityLevel = SNMP_SEC_LEVEL_AUTHNOPRIV;
911 	} else if (zend_string_equals_literal_ci(level, "authPriv") || zend_string_equals_literal_ci(level, "ap")) {
912 		s->securityLevel = SNMP_SEC_LEVEL_AUTHPRIV;
913 	} else {
914 		zend_value_error("Security level must be one of \"noAuthNoPriv\", \"authNoPriv\", or \"authPriv\"");
915 		return false;
916 	}
917 	return true;
918 }
919 /* }}} */
920 
921 /* {{{ Set the authentication protocol in the snmpv3 session */
netsnmp_session_set_auth_protocol(struct snmp_session * s,zend_string * prot)922 static bool netsnmp_session_set_auth_protocol(struct snmp_session *s, zend_string *prot)
923 {
924 #ifndef DISABLE_MD5
925 	if (zend_string_equals_literal_ci(prot, "MD5")) {
926 		s->securityAuthProto = usmHMACMD5AuthProtocol;
927 		s->securityAuthProtoLen = USM_AUTH_PROTO_MD5_LEN;
928 		return true;
929 	}
930 #endif
931 
932 	if (zend_string_equals_literal_ci(prot, "SHA")) {
933 		s->securityAuthProto = usmHMACSHA1AuthProtocol;
934 		s->securityAuthProtoLen = USM_AUTH_PROTO_SHA_LEN;
935 		return true;
936 	}
937 
938 #ifdef HAVE_SNMP_SHA256
939 	if (zend_string_equals_literal_ci(prot, "SHA256")) {
940 		s->securityAuthProto = usmHMAC192SHA256AuthProtocol;
941 		s->securityAuthProtoLen = sizeof(usmHMAC192SHA256AuthProtocol) / sizeof(oid);
942 		return true;
943 	}
944 #endif
945 
946 #ifdef HAVE_SNMP_SHA512
947 	if (zend_string_equals_literal_ci(prot, "SHA512")) {
948 		s->securityAuthProto = usmHMAC384SHA512AuthProtocol;
949 		s->securityAuthProtoLen = sizeof(usmHMAC384SHA512AuthProtocol) / sizeof(oid);
950 		return true;
951 	}
952 #endif
953 
954 	smart_string err = {0};
955 
956 	smart_string_appends(&err, "Authentication protocol must be \"SHA\"");
957 #ifdef HAVE_SNMP_SHA256
958 	smart_string_appends(&err, " or \"SHA256\"");
959 #endif
960 #ifdef HAVE_SNMP_SHA512
961 	smart_string_appends(&err, " or \"SHA512\"");
962 #endif
963 #ifndef DISABLE_MD5
964 	smart_string_appends(&err, " or \"MD5\"");
965 #endif
966 	smart_string_0(&err);
967 	zend_value_error("%s", err.c);
968 	smart_string_free(&err);
969 	return false;
970 }
971 /* }}} */
972 
973 /* {{{ Set the security protocol in the snmpv3 session */
netsnmp_session_set_sec_protocol(struct snmp_session * s,zend_string * prot)974 static bool netsnmp_session_set_sec_protocol(struct snmp_session *s, zend_string *prot)
975 {
976 #ifndef NETSNMP_DISABLE_DES
977 	if (zend_string_equals_literal_ci(prot, "DES")) {
978 		s->securityPrivProto = usmDESPrivProtocol;
979 		s->securityPrivProtoLen = USM_PRIV_PROTO_DES_LEN;
980 		return true;
981 	}
982 #endif
983 
984 #ifdef HAVE_AES
985 	if (zend_string_equals_literal_ci(prot, "AES128")
986 			|| zend_string_equals_literal_ci(prot, "AES")) {
987 		s->securityPrivProto = usmAESPrivProtocol;
988 		s->securityPrivProtoLen = USM_PRIV_PROTO_AES_LEN;
989 		return true;
990 	}
991 #endif
992 
993 #ifdef HAVE_AES
994 # ifndef NETSNMP_DISABLE_DES
995 	zend_value_error("Security protocol must be one of \"DES\", \"AES128\", or \"AES\"");
996 # else
997 	zend_value_error("Security protocol must be one of \"AES128\", or \"AES\"");
998 # endif
999 #else
1000 # ifndef NETSNMP_DISABLE_DES
1001 	zend_value_error("Security protocol must be \"DES\"");
1002 # else
1003 	zend_value_error("No security protocol supported");
1004 # endif
1005 #endif
1006 	return false;
1007 }
1008 /* }}} */
1009 
1010 /* {{{ Make key from pass phrase in the snmpv3 session */
netsnmp_session_gen_auth_key(struct snmp_session * s,zend_string * pass)1011 static bool netsnmp_session_gen_auth_key(struct snmp_session *s, zend_string *pass)
1012 {
1013 	int snmp_errno;
1014 	s->securityAuthKeyLen = USM_AUTH_KU_LEN;
1015 	if ((snmp_errno = generate_Ku(s->securityAuthProto, s->securityAuthProtoLen,
1016 			(uint8_t *) ZSTR_VAL(pass), ZSTR_LEN(pass),
1017 			s->securityAuthKey, &(s->securityAuthKeyLen)))) {
1018 		php_error_docref(NULL, E_WARNING, "Error generating a key for authentication pass phrase '%s': %s", ZSTR_VAL(pass), snmp_api_errstring(snmp_errno));
1019 		return false;
1020 	}
1021 	return true;
1022 }
1023 /* }}} */
1024 
1025 /* {{{ Make key from pass phrase in the snmpv3 session */
netsnmp_session_gen_sec_key(struct snmp_session * s,zend_string * pass)1026 static bool netsnmp_session_gen_sec_key(struct snmp_session *s, zend_string *pass)
1027 {
1028 	int snmp_errno;
1029 
1030 	s->securityPrivKeyLen = USM_PRIV_KU_LEN;
1031 	if ((snmp_errno = generate_Ku(s->securityAuthProto, s->securityAuthProtoLen,
1032 			(uint8_t *)ZSTR_VAL(pass), ZSTR_LEN(pass),
1033 			s->securityPrivKey, &(s->securityPrivKeyLen)))) {
1034 		php_error_docref(NULL, E_WARNING, "Error generating a key for privacy pass phrase '%s': %s", ZSTR_VAL(pass), snmp_api_errstring(snmp_errno));
1035 		return false;
1036 	}
1037 	return true;
1038 }
1039 /* }}} */
1040 
1041 /* {{{ Set context Engine Id in the snmpv3 session */
netsnmp_session_set_contextEngineID(struct snmp_session * s,zend_string * contextEngineID)1042 static bool netsnmp_session_set_contextEngineID(struct snmp_session *s, zend_string * contextEngineID)
1043 {
1044 	size_t	ebuf_len = 32, eout_len = 0;
1045 	uint8_t	*ebuf = (uint8_t *) emalloc(ebuf_len);
1046 
1047 	if (!snmp_hex_to_binary(&ebuf, &ebuf_len, &eout_len, 1, ZSTR_VAL(contextEngineID))) {
1048 		// TODO Promote to Error?
1049 		php_error_docref(NULL, E_WARNING, "Bad engine ID value '%s'", ZSTR_VAL(contextEngineID));
1050 		efree(ebuf);
1051 		return false;
1052 	}
1053 
1054 	if (s->contextEngineID) {
1055 		efree(s->contextEngineID);
1056 	}
1057 
1058 	s->contextEngineID = ebuf;
1059 	s->contextEngineIDLen = eout_len;
1060 	return true;
1061 }
1062 /* }}} */
1063 
1064 /* {{{ Set all snmpv3-related security options */
netsnmp_session_set_security(struct snmp_session * session,zend_string * sec_level,zend_string * auth_protocol,zend_string * auth_passphrase,zend_string * priv_protocol,zend_string * priv_passphrase,zend_string * contextName,zend_string * contextEngineID)1065 static bool netsnmp_session_set_security(struct snmp_session *session, zend_string *sec_level,
1066 	zend_string *auth_protocol, zend_string *auth_passphrase, zend_string *priv_protocol,
1067 	zend_string *priv_passphrase, zend_string *contextName, zend_string *contextEngineID)
1068 {
1069 
1070 	/* Setting the security level. */
1071 	if (!netsnmp_session_set_sec_level(session, sec_level)) {
1072 		/* ValueError already generated, just bail out */
1073 		return false;
1074 	}
1075 
1076 	if (session->securityLevel == SNMP_SEC_LEVEL_AUTHNOPRIV || session->securityLevel == SNMP_SEC_LEVEL_AUTHPRIV) {
1077 
1078 		/* Setting the authentication protocol. */
1079 		if (!netsnmp_session_set_auth_protocol(session, auth_protocol)) {
1080 			/* ValueError already generated, just bail out */
1081 			return false;
1082 		}
1083 
1084 		/* Setting the authentication passphrase. */
1085 		if (!netsnmp_session_gen_auth_key(session, auth_passphrase)) {
1086 			/* Warning message sent already, just bail out */
1087 			return false;
1088 		}
1089 
1090 		if (session->securityLevel == SNMP_SEC_LEVEL_AUTHPRIV) {
1091 			/* Setting the security protocol. */
1092 			if (!netsnmp_session_set_sec_protocol(session, priv_protocol)) {
1093 				/* ValueError already generated, just bail out */
1094 				return false;
1095 			}
1096 
1097 			/* Setting the security protocol passphrase. */
1098 			if (!netsnmp_session_gen_sec_key(session, priv_passphrase)) {
1099 				/* Warning message sent already, just bail out */
1100 				return false;
1101 			}
1102 		}
1103 	}
1104 
1105 	/* Setting contextName if specified */
1106 	if (contextName) {
1107 		session->contextName = ZSTR_VAL(contextName);
1108 		session->contextNameLen = ZSTR_LEN(contextName);
1109 	}
1110 
1111 	/* Setting contextEngineIS if specified */
1112 	if (contextEngineID && ZSTR_LEN(contextEngineID) && !netsnmp_session_set_contextEngineID(session, contextEngineID)) {
1113 		/* Warning message sent already, just bail out */
1114 		return false;
1115 	}
1116 
1117 	return true;
1118 }
1119 /* }}} */
1120 
1121 /* {{{ php_snmp
1122 *
1123 * Generic SNMP handler for all versions.
1124 * This function makes use of the internal SNMP object fetcher.
1125 * Used both in old (non-OO) and OO API
1126 *
1127 */
php_snmp(INTERNAL_FUNCTION_PARAMETERS,int st,int version)1128 static void php_snmp(INTERNAL_FUNCTION_PARAMETERS, int st, int version)
1129 {
1130 	zend_string *oid_str, *type_str = NULL, *value_str = NULL;
1131 	HashTable *oid_ht, *type_ht = NULL, *value_ht = NULL;
1132 	zend_string *a1 = NULL, *a2 = NULL, *a3 = NULL, *a4 = NULL, *a5 = NULL, *a6 = NULL, *a7 = NULL;
1133 	bool use_orignames = 0, suffix_keys = 0;
1134 	zend_long timeout = SNMP_DEFAULT_TIMEOUT;
1135 	zend_long retries = SNMP_DEFAULT_RETRIES;
1136 	struct objid_query objid_query;
1137 	php_snmp_session *session;
1138 	int session_less_mode = (getThis() == NULL);
1139 	php_snmp_object *snmp_object;
1140 	php_snmp_object glob_snmp_object;
1141 
1142 	objid_query.max_repetitions = -1;
1143 	objid_query.non_repeaters = 0;
1144 	objid_query.valueretrieval = SNMP_G(valueretrieval);
1145 	objid_query.oid_increasing_check = true;
1146 
1147 	if (session_less_mode) {
1148 		if (version == SNMP_VERSION_3) {
1149 			if (st & SNMP_CMD_SET) {
1150 				ZEND_PARSE_PARAMETERS_START(10, 12)
1151 					Z_PARAM_STR(a1)
1152 					Z_PARAM_STR(a2)
1153 					Z_PARAM_STR(a3)
1154 					Z_PARAM_STR(a4)
1155 					Z_PARAM_STR(a5)
1156 					Z_PARAM_STR(a6)
1157 					Z_PARAM_STR(a7)
1158 					Z_PARAM_ARRAY_HT_OR_STR(oid_ht, oid_str)
1159 					Z_PARAM_ARRAY_HT_OR_STR(type_ht, type_str)
1160 					Z_PARAM_ARRAY_HT_OR_STR(value_ht, value_str)
1161 					Z_PARAM_OPTIONAL
1162 					Z_PARAM_LONG(timeout)
1163 					Z_PARAM_LONG(retries)
1164 				ZEND_PARSE_PARAMETERS_END();
1165 			} else {
1166 				/* SNMP_CMD_GET
1167 				 * SNMP_CMD_GETNEXT
1168 				 * SNMP_CMD_WALK
1169 				 */
1170 				ZEND_PARSE_PARAMETERS_START(8, 10)
1171 					Z_PARAM_STR(a1)
1172 					Z_PARAM_STR(a2)
1173 					Z_PARAM_STR(a3)
1174 					Z_PARAM_STR(a4)
1175 					Z_PARAM_STR(a5)
1176 					Z_PARAM_STR(a6)
1177 					Z_PARAM_STR(a7)
1178 					Z_PARAM_ARRAY_HT_OR_STR(oid_ht, oid_str)
1179 					Z_PARAM_OPTIONAL
1180 					Z_PARAM_LONG(timeout)
1181 					Z_PARAM_LONG(retries)
1182 				ZEND_PARSE_PARAMETERS_END();
1183 			}
1184 		} else {
1185 			if (st & SNMP_CMD_SET) {
1186 				ZEND_PARSE_PARAMETERS_START(5, 7)
1187 					Z_PARAM_STR(a1)
1188 					Z_PARAM_STR(a2)
1189 					Z_PARAM_ARRAY_HT_OR_STR(oid_ht, oid_str)
1190 					Z_PARAM_ARRAY_HT_OR_STR(type_ht, type_str)
1191 					Z_PARAM_ARRAY_HT_OR_STR(value_ht, value_str)
1192 					Z_PARAM_OPTIONAL
1193 					Z_PARAM_LONG(timeout)
1194 					Z_PARAM_LONG(retries)
1195 				ZEND_PARSE_PARAMETERS_END();
1196 			} else {
1197 				/* SNMP_CMD_GET
1198 				 * SNMP_CMD_GETNEXT
1199 				 * SNMP_CMD_WALK
1200 				 */
1201 				ZEND_PARSE_PARAMETERS_START(3, 5)
1202 					Z_PARAM_STR(a1)
1203 					Z_PARAM_STR(a2)
1204 					Z_PARAM_ARRAY_HT_OR_STR(oid_ht, oid_str)
1205 					Z_PARAM_OPTIONAL
1206 					Z_PARAM_LONG(timeout)
1207 					Z_PARAM_LONG(retries)
1208 				ZEND_PARSE_PARAMETERS_END();
1209 			}
1210 		}
1211 	} else {
1212 		if (st & SNMP_CMD_SET) {
1213 			ZEND_PARSE_PARAMETERS_START(3, 3)
1214 				Z_PARAM_ARRAY_HT_OR_STR(oid_ht, oid_str)
1215 				Z_PARAM_ARRAY_HT_OR_STR(type_ht, type_str)
1216 				Z_PARAM_ARRAY_HT_OR_STR(value_ht, value_str)
1217 			ZEND_PARSE_PARAMETERS_END();
1218 		} else if (st & SNMP_CMD_WALK) {
1219 			ZEND_PARSE_PARAMETERS_START(1, 4)
1220 				Z_PARAM_ARRAY_HT_OR_STR(oid_ht, oid_str)
1221 				Z_PARAM_OPTIONAL
1222 				Z_PARAM_BOOL(suffix_keys)
1223 				Z_PARAM_LONG(objid_query.max_repetitions)
1224 				Z_PARAM_LONG(objid_query.non_repeaters)
1225 			ZEND_PARSE_PARAMETERS_END();
1226 			if (suffix_keys) {
1227 				st |= SNMP_USE_SUFFIX_AS_KEYS;
1228 			}
1229 		} else if (st & SNMP_CMD_GET) {
1230 			ZEND_PARSE_PARAMETERS_START(1, 2)
1231 				Z_PARAM_ARRAY_HT_OR_STR(oid_ht, oid_str)
1232 				Z_PARAM_OPTIONAL
1233 				Z_PARAM_BOOL(use_orignames)
1234 			ZEND_PARSE_PARAMETERS_END();
1235 			if (use_orignames) {
1236 				st |= SNMP_ORIGINAL_NAMES_AS_KEYS;
1237 			}
1238 		} else {
1239 			/* SNMP_CMD_GETNEXT
1240 			 */
1241 			ZEND_PARSE_PARAMETERS_START(1, 1)
1242 				Z_PARAM_ARRAY_HT_OR_STR(oid_ht, oid_str)
1243 			ZEND_PARSE_PARAMETERS_END();
1244 		}
1245 	}
1246 
1247 	if (!php_snmp_parse_oid(getThis(), st, &objid_query, oid_str, oid_ht, type_str, type_ht, value_str, value_ht)) {
1248 		RETURN_FALSE;
1249 	}
1250 
1251 	if (session_less_mode) {
1252 		if (!netsnmp_session_init(&session, version, a1, a2, timeout, retries)) {
1253 			efree(objid_query.vars);
1254 			netsnmp_session_free(&session);
1255 			RETURN_FALSE;
1256 		}
1257 		if (version == SNMP_VERSION_3 && !netsnmp_session_set_security(session, a3, a4, a5, a6, a7, NULL, NULL)) {
1258 			efree(objid_query.vars);
1259 			netsnmp_session_free(&session);
1260 			/* Warning message sent already, just bail out */
1261 			RETURN_FALSE;
1262 		}
1263 	} else {
1264 		zval *object = getThis();
1265 		snmp_object = Z_SNMP_P(object);
1266 		session = snmp_object->session;
1267 		if (!session) {
1268 			zend_throw_error(NULL, "Invalid or uninitialized SNMP object");
1269 			efree(objid_query.vars);
1270 			RETURN_THROWS();
1271 		}
1272 
1273 		if (snmp_object->max_oids > 0) {
1274 			objid_query.step = snmp_object->max_oids;
1275 			if (objid_query.max_repetitions < 0) { /* unspecified in function call, use session-wise */
1276 				objid_query.max_repetitions = snmp_object->max_oids;
1277 			}
1278 		}
1279 		objid_query.oid_increasing_check = snmp_object->oid_increasing_check;
1280 		objid_query.valueretrieval = snmp_object->valueretrieval;
1281 		glob_snmp_object.enum_print = netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM);
1282 		netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM, snmp_object->enum_print);
1283 		glob_snmp_object.quick_print = netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT);
1284 		netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT, snmp_object->quick_print);
1285 		glob_snmp_object.oid_output_format = netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT);
1286 		netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT, snmp_object->oid_output_format);
1287 	}
1288 
1289 	if (objid_query.max_repetitions < 0) {
1290 		objid_query.max_repetitions = 20; /* provide correct default value */
1291 	}
1292 
1293 	php_snmp_internal(INTERNAL_FUNCTION_PARAM_PASSTHRU, st, session, &objid_query);
1294 
1295 	efree(objid_query.vars);
1296 
1297 	if (session_less_mode) {
1298 		netsnmp_session_free(&session);
1299 	} else {
1300 		netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM, glob_snmp_object.enum_print);
1301 		netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT, glob_snmp_object.quick_print);
1302 		netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT, glob_snmp_object.oid_output_format);
1303 	}
1304 }
1305 /* }}} */
1306 
1307 /* {{{ Fetch a SNMP object */
PHP_FUNCTION(snmpget)1308 PHP_FUNCTION(snmpget)
1309 {
1310 	php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_GET, SNMP_VERSION_1);
1311 }
1312 /* }}} */
1313 
1314 /* {{{ Fetch a SNMP object */
PHP_FUNCTION(snmpgetnext)1315 PHP_FUNCTION(snmpgetnext)
1316 {
1317 	php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_GETNEXT, SNMP_VERSION_1);
1318 }
1319 /* }}} */
1320 
1321 /* {{{ Return all objects under the specified object id */
PHP_FUNCTION(snmpwalk)1322 PHP_FUNCTION(snmpwalk)
1323 {
1324 	php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, (SNMP_CMD_WALK | SNMP_NUMERIC_KEYS), SNMP_VERSION_1);
1325 }
1326 /* }}} */
1327 
1328 /* {{{ Return all objects including their respective object id within the specified one */
PHP_FUNCTION(snmprealwalk)1329 PHP_FUNCTION(snmprealwalk)
1330 {
1331 	php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_WALK, SNMP_VERSION_1);
1332 }
1333 /* }}} */
1334 
1335 /* {{{ Set the value of a SNMP object */
PHP_FUNCTION(snmpset)1336 PHP_FUNCTION(snmpset)
1337 {
1338 	php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_SET, SNMP_VERSION_1);
1339 }
1340 /* }}} */
1341 
1342 /* {{{ Return the current status of quick_print */
PHP_FUNCTION(snmp_get_quick_print)1343 PHP_FUNCTION(snmp_get_quick_print)
1344 {
1345 	if (zend_parse_parameters_none() == FAILURE) {
1346 		RETURN_THROWS();
1347 	}
1348 
1349 	RETURN_BOOL(netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT));
1350 }
1351 /* }}} */
1352 
1353 /* {{{ Return all objects including their respective object id within the specified one */
PHP_FUNCTION(snmp_set_quick_print)1354 PHP_FUNCTION(snmp_set_quick_print)
1355 {
1356 	bool a1;
1357 
1358 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "b", &a1) == FAILURE) {
1359 		RETURN_THROWS();
1360 	}
1361 
1362 	netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT, (int)a1);
1363 	RETURN_TRUE;
1364 }
1365 /* }}} */
1366 
1367 /* {{{ Return all values that are enums with their enum value instead of the raw integer */
PHP_FUNCTION(snmp_set_enum_print)1368 PHP_FUNCTION(snmp_set_enum_print)
1369 {
1370 	bool a1;
1371 
1372 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "b", &a1) == FAILURE) {
1373 		RETURN_THROWS();
1374 	}
1375 
1376 	netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM, (int) a1);
1377 	RETURN_TRUE;
1378 }
1379 /* }}} */
1380 
1381 /* {{{ Set the OID output format. */
PHP_FUNCTION(snmp_set_oid_output_format)1382 PHP_FUNCTION(snmp_set_oid_output_format)
1383 {
1384 	zend_long a1;
1385 
1386 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &a1) == FAILURE) {
1387 		RETURN_THROWS();
1388 	}
1389 
1390 	switch (a1) {
1391 		case NETSNMP_OID_OUTPUT_SUFFIX:
1392 		case NETSNMP_OID_OUTPUT_MODULE:
1393 		case NETSNMP_OID_OUTPUT_FULL:
1394 		case NETSNMP_OID_OUTPUT_NUMERIC:
1395 		case NETSNMP_OID_OUTPUT_UCD:
1396 		case NETSNMP_OID_OUTPUT_NONE:
1397 			netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT, a1);
1398 			RETURN_TRUE;
1399 		default:
1400 			zend_argument_value_error(1, "must be an SNMP_OID_OUTPUT_* constant");
1401 			RETURN_THROWS();
1402 	}
1403 }
1404 /* }}} */
1405 
1406 /* {{{ Fetch a SNMP object */
PHP_FUNCTION(snmp2_get)1407 PHP_FUNCTION(snmp2_get)
1408 {
1409 	php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_GET, SNMP_VERSION_2c);
1410 }
1411 /* }}} */
1412 
1413 /* {{{ Fetch a SNMP object */
PHP_FUNCTION(snmp2_getnext)1414 PHP_FUNCTION(snmp2_getnext)
1415 {
1416 	php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_GETNEXT, SNMP_VERSION_2c);
1417 }
1418 /* }}} */
1419 
1420 /* {{{ Return all objects under the specified object id */
PHP_FUNCTION(snmp2_walk)1421 PHP_FUNCTION(snmp2_walk)
1422 {
1423 	php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, (SNMP_CMD_WALK | SNMP_NUMERIC_KEYS), SNMP_VERSION_2c);
1424 }
1425 /* }}} */
1426 
1427 /* {{{ Return all objects including their respective object id within the specified one */
PHP_FUNCTION(snmp2_real_walk)1428 PHP_FUNCTION(snmp2_real_walk)
1429 {
1430 	php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_WALK, SNMP_VERSION_2c);
1431 }
1432 /* }}} */
1433 
1434 /* {{{ Set the value of a SNMP object */
PHP_FUNCTION(snmp2_set)1435 PHP_FUNCTION(snmp2_set)
1436 {
1437 	php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_SET, SNMP_VERSION_2c);
1438 }
1439 /* }}} */
1440 
1441 /* {{{ Fetch the value of a SNMP object */
PHP_FUNCTION(snmp3_get)1442 PHP_FUNCTION(snmp3_get)
1443 {
1444 	php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_GET, SNMP_VERSION_3);
1445 }
1446 /* }}} */
1447 
1448 /* {{{ Fetch the value of a SNMP object */
PHP_FUNCTION(snmp3_getnext)1449 PHP_FUNCTION(snmp3_getnext)
1450 {
1451 	php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_GETNEXT, SNMP_VERSION_3);
1452 }
1453 /* }}} */
1454 
1455 /* {{{ Fetch the value of a SNMP object */
PHP_FUNCTION(snmp3_walk)1456 PHP_FUNCTION(snmp3_walk)
1457 {
1458 	php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, (SNMP_CMD_WALK | SNMP_NUMERIC_KEYS), SNMP_VERSION_3);
1459 }
1460 /* }}} */
1461 
1462 /* {{{ Fetch the value of a SNMP object */
PHP_FUNCTION(snmp3_real_walk)1463 PHP_FUNCTION(snmp3_real_walk)
1464 {
1465 	php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_WALK, SNMP_VERSION_3);
1466 }
1467 /* }}} */
1468 
1469 /* {{{ Fetch the value of a SNMP object */
PHP_FUNCTION(snmp3_set)1470 PHP_FUNCTION(snmp3_set)
1471 {
1472 	php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_SET, SNMP_VERSION_3);
1473 }
1474 /* }}} */
1475 
1476 /* {{{ Specify the method how the SNMP values will be returned */
PHP_FUNCTION(snmp_set_valueretrieval)1477 PHP_FUNCTION(snmp_set_valueretrieval)
1478 {
1479 	zend_long method;
1480 
1481 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &method) == FAILURE) {
1482 		RETURN_THROWS();
1483 	}
1484 
1485 	if (method >= 0 && method <= (SNMP_VALUE_LIBRARY|SNMP_VALUE_PLAIN|SNMP_VALUE_OBJECT)) {
1486 			SNMP_G(valueretrieval) = method;
1487 			RETURN_TRUE;
1488 	} else {
1489 		zend_argument_value_error(1, "must be a bitmask of SNMP_VALUE_LIBRARY, SNMP_VALUE_PLAIN, and SNMP_VALUE_OBJECT");
1490 		RETURN_THROWS();
1491 	}
1492 }
1493 /* }}} */
1494 
1495 /* {{{ Return the method how the SNMP values will be returned */
PHP_FUNCTION(snmp_get_valueretrieval)1496 PHP_FUNCTION(snmp_get_valueretrieval)
1497 {
1498 	if (zend_parse_parameters_none() == FAILURE) {
1499 		RETURN_THROWS();
1500 	}
1501 
1502 	RETURN_LONG(SNMP_G(valueretrieval));
1503 }
1504 /* }}} */
1505 
1506 /* {{{ Reads and parses a MIB file into the active MIB tree. */
PHP_FUNCTION(snmp_read_mib)1507 PHP_FUNCTION(snmp_read_mib)
1508 {
1509 	char *filename;
1510 	size_t filename_len;
1511 
1512 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &filename, &filename_len) == FAILURE) {
1513 		RETURN_THROWS();
1514 	}
1515 
1516 	if (!read_mib(filename)) {
1517 		char *error = strerror(errno);
1518 		php_error_docref(NULL, E_WARNING, "Error while reading MIB file '%s': %s", filename, error);
1519 		RETURN_FALSE;
1520 	}
1521 	RETURN_TRUE;
1522 }
1523 /* }}} */
1524 
1525 /* {{{ Creates a new SNMP session to specified host. */
PHP_METHOD(SNMP,__construct)1526 PHP_METHOD(SNMP, __construct)
1527 {
1528 	php_snmp_object *snmp_object;
1529 	zval *object = ZEND_THIS;
1530 	zend_string *a1, *a2;
1531 	zend_long timeout = SNMP_DEFAULT_TIMEOUT;
1532 	zend_long retries = SNMP_DEFAULT_RETRIES;
1533 	zend_long version = SNMP_DEFAULT_VERSION;
1534 
1535 	snmp_object = Z_SNMP_P(object);
1536 
1537 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "lSS|ll", &version, &a1, &a2, &timeout, &retries) == FAILURE) {
1538 		RETURN_THROWS();
1539 	}
1540 
1541 	switch (version) {
1542 		case SNMP_VERSION_1:
1543 		case SNMP_VERSION_2c:
1544 		case SNMP_VERSION_3:
1545 			break;
1546 		default:
1547 			zend_argument_value_error(1, "must be a valid SNMP protocol version");
1548 			RETURN_THROWS();
1549 	}
1550 
1551 	/* handle re-open of snmp session */
1552 	if (snmp_object->session) {
1553 		netsnmp_session_free(&(snmp_object->session));
1554 	}
1555 
1556 	if (!netsnmp_session_init(&(snmp_object->session), version, a1, a2, timeout, retries)) {
1557 		return;
1558 	}
1559 	snmp_object->max_oids = 0;
1560 	snmp_object->valueretrieval = SNMP_G(valueretrieval);
1561 	snmp_object->enum_print = netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM);
1562 	snmp_object->oid_output_format = netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT);
1563 	snmp_object->quick_print = netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT);
1564 	snmp_object->oid_increasing_check = true;
1565 	snmp_object->exceptions_enabled = 0;
1566 }
1567 /* }}} */
1568 
1569 /* {{{ Close SNMP session */
PHP_METHOD(SNMP,close)1570 PHP_METHOD(SNMP, close)
1571 {
1572 	php_snmp_object *snmp_object;
1573 	zval *object = ZEND_THIS;
1574 
1575 	snmp_object = Z_SNMP_P(object);
1576 
1577 	if (zend_parse_parameters_none() == FAILURE) {
1578 		RETURN_THROWS();
1579 	}
1580 
1581 	netsnmp_session_free(&(snmp_object->session));
1582 
1583 	RETURN_TRUE;
1584 }
1585 /* }}} */
1586 
1587 /* {{{ Fetch a SNMP object returning scalar for single OID and array of oid->value pairs for multi OID request */
PHP_METHOD(SNMP,get)1588 PHP_METHOD(SNMP, get)
1589 {
1590 	php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_GET, (-1));
1591 }
1592 /* }}} */
1593 
1594 /* {{{ Fetch a SNMP object returning scalar for single OID and array of oid->value pairs for multi OID request */
PHP_METHOD(SNMP,getnext)1595 PHP_METHOD(SNMP, getnext)
1596 {
1597 	php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_GETNEXT, (-1));
1598 }
1599 /* }}} */
1600 
1601 /* {{{ Return all objects including their respective object id within the specified one as array of oid->value pairs */
PHP_METHOD(SNMP,walk)1602 PHP_METHOD(SNMP, walk)
1603 {
1604 	php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_WALK, (-1));
1605 }
1606 /* }}} */
1607 
1608 /* {{{ Set the value of a SNMP object */
PHP_METHOD(SNMP,set)1609 PHP_METHOD(SNMP, set)
1610 {
1611 	php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_SET, (-1));
1612 }
1613 /* }}} */
1614 
1615 /* {{{ Set SNMPv3 security-related session parameters */
PHP_METHOD(SNMP,setSecurity)1616 PHP_METHOD(SNMP, setSecurity)
1617 {
1618 	php_snmp_object *snmp_object;
1619 	zval *object = ZEND_THIS;
1620 	zend_string *a1 = NULL, *a2 = NULL, *a3 = NULL, *a4 = NULL, *a5 = NULL, *a6 = NULL, *a7 = NULL;
1621 
1622 	snmp_object = Z_SNMP_P(object);
1623 
1624 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|SSSSSS", &a1, &a2, &a3, &a4,&a5, &a6, &a7) == FAILURE) {
1625 		RETURN_THROWS();
1626 	}
1627 
1628 	if (!netsnmp_session_set_security(snmp_object->session, a1, a2, a3, a4, a5, a6, a7)) {
1629 		/* Warning message sent already, just bail out */
1630 		RETURN_FALSE;
1631 	}
1632 	RETURN_TRUE;
1633 }
1634 /* }}} */
1635 
1636 /* {{{ Get last error code number */
PHP_METHOD(SNMP,getErrno)1637 PHP_METHOD(SNMP, getErrno)
1638 {
1639 	php_snmp_object *snmp_object;
1640 	zval *object = ZEND_THIS;
1641 
1642 	snmp_object = Z_SNMP_P(object);
1643 
1644 	if (zend_parse_parameters_none() == FAILURE) {
1645 		RETURN_THROWS();
1646 	}
1647 
1648 	RETURN_LONG(snmp_object->snmp_errno);
1649 }
1650 /* }}} */
1651 
1652 /* {{{ Get last error message */
PHP_METHOD(SNMP,getError)1653 PHP_METHOD(SNMP, getError)
1654 {
1655 	php_snmp_object *snmp_object;
1656 	zval *object = ZEND_THIS;
1657 
1658 	snmp_object = Z_SNMP_P(object);
1659 
1660 	if (zend_parse_parameters_none() == FAILURE) {
1661 		RETURN_THROWS();
1662 	}
1663 
1664 	RETURN_STRING(snmp_object->snmp_errstr);
1665 }
1666 /* }}} */
1667 
1668 /* {{{ */
php_snmp_add_property(HashTable * h,const char * name,size_t name_length,php_snmp_read_t read_func,php_snmp_write_t write_func)1669 void php_snmp_add_property(HashTable *h, const char *name, size_t name_length, php_snmp_read_t read_func, php_snmp_write_t write_func)
1670 {
1671 	php_snmp_prop_handler p;
1672 	zend_string *str;
1673 
1674 	p.name = (char*) name;
1675 	p.name_length = name_length;
1676 	p.read_func = (read_func) ? read_func : NULL;
1677 	p.write_func = (write_func) ? write_func : NULL;
1678 	str = zend_string_init_interned(name, name_length, 1);
1679 	zend_hash_add_mem(h, str, &p, sizeof(php_snmp_prop_handler));
1680 	zend_string_release_ex(str, 1);
1681 }
1682 /* }}} */
1683 
1684 /* {{{ php_snmp_read_property(zval *object, zval *member, int type[, const zend_literal *key])
1685    Generic object property reader */
php_snmp_read_property(zend_object * object,zend_string * name,int type,void ** cache_slot,zval * rv)1686 zval *php_snmp_read_property(zend_object *object, zend_string *name, int type, void **cache_slot, zval *rv)
1687 {
1688 	zval *retval;
1689 	php_snmp_object *obj;
1690 	php_snmp_prop_handler *hnd;
1691 	int ret;
1692 
1693 	obj = php_snmp_fetch_object(object);
1694 	hnd = zend_hash_find_ptr(&php_snmp_properties, name);
1695 
1696 	if (hnd && hnd->read_func) {
1697 		ret = hnd->read_func(obj, rv);
1698 		if (ret == SUCCESS) {
1699 			retval = rv;
1700 		} else {
1701 			retval = &EG(uninitialized_zval);
1702 		}
1703 	} else {
1704 		retval = zend_std_read_property(object, name, type, cache_slot, rv);
1705 	}
1706 
1707 	return retval;
1708 }
1709 /* }}} */
1710 
1711 /* {{{ Generic object property writer */
php_snmp_write_property(zend_object * object,zend_string * name,zval * value,void ** cache_slot)1712 zval *php_snmp_write_property(zend_object *object, zend_string *name, zval *value, void **cache_slot)
1713 {
1714 	php_snmp_object *obj = php_snmp_fetch_object(object);
1715 	php_snmp_prop_handler *hnd = zend_hash_find_ptr(&php_snmp_properties, name);
1716 
1717 	if (hnd) {
1718 		if (!hnd->write_func) {
1719 			zend_throw_error(NULL, "Cannot write read-only property %s::$%s", ZSTR_VAL(object->ce->name), ZSTR_VAL(name));
1720 			return &EG(error_zval);
1721 		}
1722 
1723 		zend_property_info *prop = zend_get_property_info(object->ce, name, /* silent */ true);
1724 		if (prop && ZEND_TYPE_IS_SET(prop->type)) {
1725 			zval tmp;
1726 			ZVAL_COPY(&tmp, value);
1727 			if (!zend_verify_property_type(prop, &tmp,
1728 						ZEND_CALL_USES_STRICT_TYPES(EG(current_execute_data)))) {
1729 				zval_ptr_dtor(&tmp);
1730 				return &EG(error_zval);
1731 			}
1732 			hnd->write_func(obj, &tmp);
1733 			zval_ptr_dtor(&tmp);
1734 		} else {
1735 			hnd->write_func(obj, value);
1736 		}
1737 		return value;
1738 	}
1739 
1740 	return zend_std_write_property(object, name, value, cache_slot);
1741 }
1742 /* }}} */
1743 
1744 /* {{{ php_snmp_has_property(zval *object, zval *member, int has_set_exists[, const zend_literal *key])
1745    Generic object property checker */
php_snmp_has_property(zend_object * object,zend_string * name,int has_set_exists,void ** cache_slot)1746 static int php_snmp_has_property(zend_object *object, zend_string *name, int has_set_exists, void **cache_slot)
1747 {
1748 	zval rv;
1749 	php_snmp_prop_handler *hnd;
1750 	int ret = 0;
1751 
1752 	if ((hnd = zend_hash_find_ptr(&php_snmp_properties, name)) != NULL) {
1753 		switch (has_set_exists) {
1754 			case ZEND_PROPERTY_EXISTS:
1755 				ret = 1;
1756 				break;
1757 			case ZEND_PROPERTY_ISSET: {
1758 				zval *value = php_snmp_read_property(object, name, BP_VAR_IS, cache_slot, &rv);
1759 				if (value != &EG(uninitialized_zval)) {
1760 					ret = Z_TYPE_P(value) != IS_NULL? 1 : 0;
1761 					zval_ptr_dtor(value);
1762 				}
1763 				break;
1764 			}
1765 			default: {
1766 				zval *value = php_snmp_read_property(object, name, BP_VAR_IS, cache_slot, &rv);
1767 				if (value != &EG(uninitialized_zval)) {
1768 					convert_to_boolean(value);
1769 					ret = Z_TYPE_P(value) == IS_TRUE? 1:0;
1770 				}
1771 				break;
1772 			}
1773 		}
1774 	} else {
1775 		ret = zend_std_has_property(object, name, has_set_exists, cache_slot);
1776 	}
1777 	return ret;
1778 }
1779 /* }}} */
1780 
php_snmp_get_gc(zend_object * object,zval ** gc_data,int * gc_data_count)1781 static HashTable *php_snmp_get_gc(zend_object *object, zval **gc_data, int *gc_data_count) /* {{{ */
1782 {
1783 	*gc_data = NULL;
1784 	*gc_data_count = 0;
1785 	return zend_std_get_properties(object);
1786 }
1787 /* }}} */
1788 
1789 /* {{{ php_snmp_get_properties(zval *object)
1790    Returns all object properties. Injects SNMP properties into object on first call */
php_snmp_get_properties(zend_object * object)1791 static HashTable *php_snmp_get_properties(zend_object *object)
1792 {
1793 	php_snmp_object *obj;
1794 	php_snmp_prop_handler *hnd;
1795 	HashTable *props;
1796 	zval rv;
1797 	zend_string *key;
1798 
1799 	obj = php_snmp_fetch_object(object);
1800 	props = zend_std_get_properties(object);
1801 
1802 	ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(&php_snmp_properties, key, hnd) {
1803 		if (!hnd->read_func || hnd->read_func(obj, &rv) != SUCCESS) {
1804 			ZVAL_NULL(&rv);
1805 		}
1806 		zend_hash_update(props, key, &rv);
1807 	} ZEND_HASH_FOREACH_END();
1808 
1809 	return obj->zo.properties;
1810 }
1811 /* }}} */
1812 
php_snmp_get_property_ptr_ptr(zend_object * object,zend_string * name,int type,void ** cache_slot)1813 static zval *php_snmp_get_property_ptr_ptr(zend_object *object, zend_string *name, int type, void **cache_slot)
1814 {
1815 	php_snmp_prop_handler *hnd = zend_hash_find_ptr(&php_snmp_properties, name);
1816 	if (hnd == NULL) {
1817 		return zend_std_get_property_ptr_ptr(object, name, type, cache_slot);
1818 	}
1819 
1820 	return NULL;
1821 }
1822 
1823 /* {{{ */
php_snmp_read_info(php_snmp_object * snmp_object,zval * retval)1824 static int php_snmp_read_info(php_snmp_object *snmp_object, zval *retval)
1825 {
1826 	zval val;
1827 
1828 	array_init(retval);
1829 
1830 	if (snmp_object->session == NULL) {
1831 		return SUCCESS;
1832 	}
1833 
1834 	ZVAL_STRINGL(&val, snmp_object->session->peername, strlen(snmp_object->session->peername));
1835 	add_assoc_zval(retval, "hostname", &val);
1836 
1837 	ZVAL_LONG(&val, snmp_object->session->timeout);
1838 	add_assoc_zval(retval, "timeout", &val);
1839 
1840 	ZVAL_LONG(&val, snmp_object->session->retries);
1841 	add_assoc_zval(retval, "retries", &val);
1842 
1843 	return SUCCESS;
1844 }
1845 /* }}} */
1846 
1847 /* {{{ */
php_snmp_read_max_oids(php_snmp_object * snmp_object,zval * retval)1848 static int php_snmp_read_max_oids(php_snmp_object *snmp_object, zval *retval)
1849 {
1850 	if (snmp_object->max_oids > 0) {
1851 		ZVAL_LONG(retval, snmp_object->max_oids);
1852 	} else {
1853 		ZVAL_NULL(retval);
1854 	}
1855 	return SUCCESS;
1856 }
1857 /* }}} */
1858 
1859 #define PHP_SNMP_BOOL_PROPERTY_READER_FUNCTION(name) \
1860 	static int php_snmp_read_##name(php_snmp_object *snmp_object, zval *retval) \
1861 	{ \
1862 		ZVAL_BOOL(retval, snmp_object->name); \
1863 		return SUCCESS; \
1864 	}
1865 
1866 PHP_SNMP_BOOL_PROPERTY_READER_FUNCTION(oid_increasing_check)
PHP_SNMP_BOOL_PROPERTY_READER_FUNCTION(quick_print)1867 PHP_SNMP_BOOL_PROPERTY_READER_FUNCTION(quick_print)
1868 PHP_SNMP_BOOL_PROPERTY_READER_FUNCTION(enum_print)
1869 
1870 #define PHP_SNMP_LONG_PROPERTY_READER_FUNCTION(name) \
1871 	static int php_snmp_read_##name(php_snmp_object *snmp_object, zval *retval) \
1872 	{ \
1873 		ZVAL_LONG(retval, snmp_object->name); \
1874 		return SUCCESS; \
1875 	}
1876 
1877 PHP_SNMP_LONG_PROPERTY_READER_FUNCTION(valueretrieval)
1878 PHP_SNMP_LONG_PROPERTY_READER_FUNCTION(oid_output_format)
1879 PHP_SNMP_LONG_PROPERTY_READER_FUNCTION(exceptions_enabled)
1880 
1881 /* {{{ */
1882 static int php_snmp_write_max_oids(php_snmp_object *snmp_object, zval *newval)
1883 {
1884 	zend_long lval;
1885 
1886 	if (Z_TYPE_P(newval) == IS_NULL) {
1887 		snmp_object->max_oids = 0;
1888 		return SUCCESS;
1889 	}
1890 
1891 	lval = zval_get_long(newval);
1892 
1893 	if (lval <= 0) {
1894 		zend_value_error("SNMP::$max_oids must be greater than 0 or null");
1895 		return FAILURE;
1896 	}
1897 	snmp_object->max_oids = lval;
1898 
1899 	return SUCCESS;
1900 }
1901 /* }}} */
1902 
1903 /* {{{ */
php_snmp_write_valueretrieval(php_snmp_object * snmp_object,zval * newval)1904 static int php_snmp_write_valueretrieval(php_snmp_object *snmp_object, zval *newval)
1905 {
1906 	zend_long lval = zval_get_long(newval);
1907 
1908 	if (lval >= 0 && lval <= (SNMP_VALUE_LIBRARY|SNMP_VALUE_PLAIN|SNMP_VALUE_OBJECT)) {
1909 		snmp_object->valueretrieval = lval;
1910 	} else {
1911 		zend_value_error("SNMP retrieval method must be a bitmask of SNMP_VALUE_LIBRARY, SNMP_VALUE_PLAIN, and SNMP_VALUE_OBJECT");
1912 		return FAILURE;
1913 	}
1914 
1915 	return SUCCESS;
1916 }
1917 /* }}} */
1918 
1919 #define PHP_SNMP_BOOL_PROPERTY_WRITER_FUNCTION(name) \
1920 static int php_snmp_write_##name(php_snmp_object *snmp_object, zval *newval) \
1921 { \
1922 	zval ztmp; \
1923 	ZVAL_COPY(&ztmp, newval); \
1924 	convert_to_boolean(&ztmp); \
1925 	newval = &ztmp; \
1926 \
1927 	snmp_object->name = Z_TYPE_P(newval) == IS_TRUE? 1 : 0; \
1928 \
1929 	return SUCCESS; \
1930 }
1931 
1932 PHP_SNMP_BOOL_PROPERTY_WRITER_FUNCTION(quick_print)
PHP_SNMP_BOOL_PROPERTY_WRITER_FUNCTION(enum_print)1933 PHP_SNMP_BOOL_PROPERTY_WRITER_FUNCTION(enum_print)
1934 PHP_SNMP_BOOL_PROPERTY_WRITER_FUNCTION(oid_increasing_check)
1935 
1936 /* {{{ */
1937 static int php_snmp_write_oid_output_format(php_snmp_object *snmp_object, zval *newval)
1938 {
1939 	zend_long lval = zval_get_long(newval);
1940 
1941 	switch(lval) {
1942 		case NETSNMP_OID_OUTPUT_SUFFIX:
1943 		case NETSNMP_OID_OUTPUT_MODULE:
1944 		case NETSNMP_OID_OUTPUT_FULL:
1945 		case NETSNMP_OID_OUTPUT_NUMERIC:
1946 		case NETSNMP_OID_OUTPUT_UCD:
1947 		case NETSNMP_OID_OUTPUT_NONE:
1948 			snmp_object->oid_output_format = lval;
1949 			return SUCCESS;
1950 		default:
1951 			zend_value_error("SNMP output print format must be an SNMP_OID_OUTPUT_* constant");
1952 			return FAILURE;
1953 	}
1954 }
1955 /* }}} */
1956 
1957 /* {{{ */
php_snmp_write_exceptions_enabled(php_snmp_object * snmp_object,zval * newval)1958 static int php_snmp_write_exceptions_enabled(php_snmp_object *snmp_object, zval *newval)
1959 {
1960 	int ret = SUCCESS;
1961 
1962 	snmp_object->exceptions_enabled = zval_get_long(newval);
1963 
1964 	return ret;
1965 }
1966 /* }}} */
1967 
free_php_snmp_properties(zval * el)1968 static void free_php_snmp_properties(zval *el)  /* {{{ */
1969 {
1970 	pefree(Z_PTR_P(el), 1);
1971 }
1972 /* }}} */
1973 
1974 #define PHP_SNMP_PROPERTY_ENTRY_RECORD(name) \
1975 	{ "" #name "",		sizeof("" #name "") - 1,	php_snmp_read_##name,	php_snmp_write_##name }
1976 
1977 #define PHP_SNMP_READONLY_PROPERTY_ENTRY_RECORD(name) \
1978 	{ "" #name "",		sizeof("" #name "") - 1,	php_snmp_read_##name,	NULL }
1979 
1980 const php_snmp_prop_handler php_snmp_property_entries[] = {
1981 	PHP_SNMP_READONLY_PROPERTY_ENTRY_RECORD(info),
1982 	PHP_SNMP_PROPERTY_ENTRY_RECORD(max_oids),
1983 	PHP_SNMP_PROPERTY_ENTRY_RECORD(valueretrieval),
1984 	PHP_SNMP_PROPERTY_ENTRY_RECORD(quick_print),
1985 	PHP_SNMP_PROPERTY_ENTRY_RECORD(enum_print),
1986 	PHP_SNMP_PROPERTY_ENTRY_RECORD(oid_output_format),
1987 	PHP_SNMP_PROPERTY_ENTRY_RECORD(oid_increasing_check),
1988 	PHP_SNMP_PROPERTY_ENTRY_RECORD(exceptions_enabled),
1989 	{ NULL, 0, NULL, NULL}
1990 };
1991 /* }}} */
1992 
1993 /* {{{ PHP_MINIT_FUNCTION */
PHP_MINIT_FUNCTION(snmp)1994 PHP_MINIT_FUNCTION(snmp)
1995 {
1996 	netsnmp_log_handler *logh;
1997 
1998 	init_snmp("snmpapp");
1999 	/* net-snmp corrupts the CTYPE locale during initialization. */
2000 	zend_reset_lc_ctype_locale();
2001 
2002 #ifdef NETSNMP_DS_LIB_DONT_PERSIST_STATE
2003 	/* Prevent update of the snmpapp.conf file */
2004 	netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_DONT_PERSIST_STATE, 1);
2005 #endif
2006 
2007 	/* Disable logging, use exit status'es and related variabled to detect errors */
2008 	shutdown_snmp_logging();
2009 	logh = netsnmp_register_loghandler(NETSNMP_LOGHANDLER_NONE, LOG_ERR);
2010 	if (logh) {
2011 		logh->pri_max = LOG_ERR;
2012 	}
2013 
2014 	memcpy(&php_snmp_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
2015 	php_snmp_object_handlers.read_property = php_snmp_read_property;
2016 	php_snmp_object_handlers.write_property = php_snmp_write_property;
2017 	php_snmp_object_handlers.get_property_ptr_ptr = php_snmp_get_property_ptr_ptr;
2018 	php_snmp_object_handlers.has_property = php_snmp_has_property;
2019 	php_snmp_object_handlers.get_properties = php_snmp_get_properties;
2020 	php_snmp_object_handlers.get_gc = php_snmp_get_gc;
2021 
2022 	/* Register SNMP Class */
2023 	php_snmp_ce = register_class_SNMP();
2024 	php_snmp_ce->create_object = php_snmp_object_new;
2025 	php_snmp_ce->default_object_handlers = &php_snmp_object_handlers;
2026 	php_snmp_object_handlers.offset = XtOffsetOf(php_snmp_object, zo);
2027 	php_snmp_object_handlers.clone_obj = NULL;
2028 	php_snmp_object_handlers.free_obj = php_snmp_object_free_storage;
2029 
2030 	/* Register SNMP Class properties */
2031 	zend_hash_init(&php_snmp_properties, 0, NULL, free_php_snmp_properties, 1);
2032 	PHP_SNMP_ADD_PROPERTIES(&php_snmp_properties, php_snmp_property_entries);
2033 
2034 	/* Register SNMPException class */
2035 	php_snmp_exception_ce = register_class_SNMPException(spl_ce_RuntimeException);
2036 
2037 	register_snmp_symbols(module_number);
2038 
2039 	return SUCCESS;
2040 }
2041 /* }}} */
2042 
2043 /* {{{ PHP_MSHUTDOWN_FUNCTION */
PHP_MSHUTDOWN_FUNCTION(snmp)2044 PHP_MSHUTDOWN_FUNCTION(snmp)
2045 {
2046 	snmp_shutdown("snmpapp");
2047 
2048 	zend_hash_destroy(&php_snmp_properties);
2049 
2050 	return SUCCESS;
2051 }
2052 /* }}} */
2053 
2054 /* {{{ PHP_MINFO_FUNCTION */
PHP_MINFO_FUNCTION(snmp)2055 PHP_MINFO_FUNCTION(snmp)
2056 {
2057 	php_info_print_table_start();
2058 	php_info_print_table_row(2, "NET-SNMP Support", "enabled");
2059 	php_info_print_table_row(2, "NET-SNMP Version", netsnmp_get_version());
2060 	php_info_print_table_end();
2061 }
2062 /* }}} */
2063 
2064 /* {{{ snmp_module_deps[] */
2065 static const zend_module_dep snmp_module_deps[] = {
2066 	ZEND_MOD_REQUIRED("spl")
2067 	ZEND_MOD_END
2068 };
2069 /* }}} */
2070 
2071 /* {{{ snmp_module_entry */
2072 zend_module_entry snmp_module_entry = {
2073 	STANDARD_MODULE_HEADER_EX,
2074 	NULL,
2075 	snmp_module_deps,
2076 	"snmp",
2077 	ext_functions,
2078 	PHP_MINIT(snmp),
2079 	PHP_MSHUTDOWN(snmp),
2080 	NULL,
2081 	NULL,
2082 	PHP_MINFO(snmp),
2083 	PHP_SNMP_VERSION,
2084 	PHP_MODULE_GLOBALS(snmp),
2085 	PHP_GINIT(snmp),
2086 	NULL,
2087 	NULL,
2088 	STANDARD_MODULE_PROPERTIES_EX
2089 };
2090 /* }}} */
2091 
2092 #endif
2093