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