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