xref: /PHP-8.3/ext/snmp/snmp.c (revision d3abcae4)
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) && defined(HAVE_INET_NTOP)
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 #else
863 		if ((*res)->sa_family != AF_INET) {
864 			res++;
865 			continue;
866 		}
867 		strcat(pptr, inet_ntoa(((struct sockaddr_in*)(*res))->sin_addr));
868 #endif
869 		break;
870 	}
871 
872 	if (strlen(session->peername) == 0) {
873 		php_error_docref(NULL, E_WARNING, "Unknown failure while resolving '%s'", ZSTR_VAL(hostname));
874 		return false;
875 	}
876 	/* XXX FIXME
877 		There should be check for non-empty session->peername!
878 	*/
879 
880 	/* put back non-standard SNMP port */
881 	if (remote_port != SNMP_PORT) {
882 		pptr = session->peername + strlen(session->peername);
883 		sprintf(pptr, ":%d", remote_port);
884 	}
885 
886 	php_network_freeaddresses(psal);
887 
888 	if (version == SNMP_VERSION_3) {
889 		/* Setting the security name. */
890 		session->securityName = estrdup(ZSTR_VAL(community));
891 		session->securityNameLen = ZSTR_LEN(community);
892 	} else {
893 		session->authenticator = NULL;
894 		session->community = (uint8_t *)estrdup(ZSTR_VAL(community));
895 		session->community_len = ZSTR_LEN(community);
896 	}
897 
898 	session->retries = retries;
899 	session->timeout = timeout;
900 	return true;
901 }
902 /* }}} */
903 
904 /* {{{ Set the security level in the snmpv3 session */
netsnmp_session_set_sec_level(struct snmp_session * s,zend_string * level)905 static bool netsnmp_session_set_sec_level(struct snmp_session *s, zend_string *level)
906 {
907 	if (zend_string_equals_literal_ci(level, "noAuthNoPriv") || zend_string_equals_literal_ci(level, "nanp")) {
908 		s->securityLevel = SNMP_SEC_LEVEL_NOAUTH;
909 	} else if (zend_string_equals_literal_ci(level, "authNoPriv") || zend_string_equals_literal_ci(level, "anp")) {
910 		s->securityLevel = SNMP_SEC_LEVEL_AUTHNOPRIV;
911 	} else if (zend_string_equals_literal_ci(level, "authPriv") || zend_string_equals_literal_ci(level, "ap")) {
912 		s->securityLevel = SNMP_SEC_LEVEL_AUTHPRIV;
913 	} else {
914 		zend_value_error("Security level must be one of \"noAuthNoPriv\", \"authNoPriv\", or \"authPriv\"");
915 		return false;
916 	}
917 	return true;
918 }
919 /* }}} */
920 
921 /* {{{ Set the authentication protocol in the snmpv3 session */
netsnmp_session_set_auth_protocol(struct snmp_session * s,zend_string * prot)922 static bool netsnmp_session_set_auth_protocol(struct snmp_session *s, zend_string *prot)
923 {
924 #ifndef DISABLE_MD5
925 	if (zend_string_equals_literal_ci(prot, "MD5")) {
926 		s->securityAuthProto = usmHMACMD5AuthProtocol;
927 		s->securityAuthProtoLen = USM_AUTH_PROTO_MD5_LEN;
928 		return true;
929 	}
930 #endif
931 
932 	if (zend_string_equals_literal_ci(prot, "SHA")) {
933 		s->securityAuthProto = usmHMACSHA1AuthProtocol;
934 		s->securityAuthProtoLen = USM_AUTH_PROTO_SHA_LEN;
935 		return true;
936 	}
937 
938 #ifdef HAVE_SNMP_SHA256
939 	if (zend_string_equals_literal_ci(prot, "SHA256")) {
940 		s->securityAuthProto = usmHMAC192SHA256AuthProtocol;
941 		s->securityAuthProtoLen = sizeof(usmHMAC192SHA256AuthProtocol) / sizeof(oid);
942 		return true;
943 	}
944 #endif
945 
946 #ifdef HAVE_SNMP_SHA512
947 	if (zend_string_equals_literal_ci(prot, "SHA512")) {
948 		s->securityAuthProto = usmHMAC384SHA512AuthProtocol;
949 		s->securityAuthProtoLen = sizeof(usmHMAC384SHA512AuthProtocol) / sizeof(oid);
950 		return true;
951 	}
952 #endif
953 
954 	smart_string err = {0};
955 
956 	smart_string_appends(&err, "Authentication protocol must be \"SHA\"");
957 #ifdef HAVE_SNMP_SHA256
958 	smart_string_appends(&err, " or \"SHA256\"");
959 #endif
960 #ifdef HAVE_SNMP_SHA512
961 	smart_string_appends(&err, " or \"SHA512\"");
962 #endif
963 #ifndef DISABLE_MD5
964 	smart_string_appends(&err, " or \"MD5\"");
965 #endif
966 	smart_string_0(&err);
967 	zend_value_error("%s", err.c);
968 	smart_string_free(&err);
969 	return false;
970 }
971 /* }}} */
972 
973 /* {{{ Set the security protocol in the snmpv3 session */
netsnmp_session_set_sec_protocol(struct snmp_session * s,zend_string * prot)974 static bool netsnmp_session_set_sec_protocol(struct snmp_session *s, zend_string *prot)
975 {
976 #ifndef NETSNMP_DISABLE_DES
977 	if (zend_string_equals_literal_ci(prot, "DES")) {
978 		s->securityPrivProto = usmDESPrivProtocol;
979 		s->securityPrivProtoLen = USM_PRIV_PROTO_DES_LEN;
980 		return true;
981 	}
982 #endif
983 
984 #ifdef HAVE_AES
985 	if (zend_string_equals_literal_ci(prot, "AES128")
986 			|| zend_string_equals_literal_ci(prot, "AES")) {
987 		s->securityPrivProto = usmAESPrivProtocol;
988 		s->securityPrivProtoLen = USM_PRIV_PROTO_AES_LEN;
989 		return true;
990 	}
991 #endif
992 
993 #ifdef HAVE_AES
994 # ifndef NETSNMP_DISABLE_DES
995 	zend_value_error("Security protocol must be one of \"DES\", \"AES128\", or \"AES\"");
996 # else
997 	zend_value_error("Security protocol must be one of \"AES128\", or \"AES\"");
998 # endif
999 #else
1000 # ifndef NETSNMP_DISABLE_DES
1001 	zend_value_error("Security protocol must be \"DES\"");
1002 # else
1003 	zend_value_error("No security protocol supported");
1004 # endif
1005 #endif
1006 	return false;
1007 }
1008 /* }}} */
1009 
1010 /* {{{ Make key from pass phrase in the snmpv3 session */
netsnmp_session_gen_auth_key(struct snmp_session * s,zend_string * pass)1011 static bool netsnmp_session_gen_auth_key(struct snmp_session *s, zend_string *pass)
1012 {
1013 	int snmp_errno;
1014 	s->securityAuthKeyLen = USM_AUTH_KU_LEN;
1015 	if ((snmp_errno = generate_Ku(s->securityAuthProto, s->securityAuthProtoLen,
1016 			(uint8_t *) ZSTR_VAL(pass), ZSTR_LEN(pass),
1017 			s->securityAuthKey, &(s->securityAuthKeyLen)))) {
1018 		php_error_docref(NULL, E_WARNING, "Error generating a key for authentication pass phrase '%s': %s", ZSTR_VAL(pass), snmp_api_errstring(snmp_errno));
1019 		return false;
1020 	}
1021 	return true;
1022 }
1023 /* }}} */
1024 
1025 /* {{{ Make key from pass phrase in the snmpv3 session */
netsnmp_session_gen_sec_key(struct snmp_session * s,zend_string * pass)1026 static bool netsnmp_session_gen_sec_key(struct snmp_session *s, zend_string *pass)
1027 {
1028 	int snmp_errno;
1029 
1030 	s->securityPrivKeyLen = USM_PRIV_KU_LEN;
1031 	if ((snmp_errno = generate_Ku(s->securityAuthProto, s->securityAuthProtoLen,
1032 			(uint8_t *)ZSTR_VAL(pass), ZSTR_LEN(pass),
1033 			s->securityPrivKey, &(s->securityPrivKeyLen)))) {
1034 		php_error_docref(NULL, E_WARNING, "Error generating a key for privacy pass phrase '%s': %s", ZSTR_VAL(pass), snmp_api_errstring(snmp_errno));
1035 		return false;
1036 	}
1037 	return true;
1038 }
1039 /* }}} */
1040 
1041 /* {{{ Set context Engine Id in the snmpv3 session */
netsnmp_session_set_contextEngineID(struct snmp_session * s,zend_string * contextEngineID)1042 static bool netsnmp_session_set_contextEngineID(struct snmp_session *s, zend_string * contextEngineID)
1043 {
1044 	size_t	ebuf_len = 32, eout_len = 0;
1045 	uint8_t	*ebuf = (uint8_t *) emalloc(ebuf_len);
1046 
1047 	if (!snmp_hex_to_binary(&ebuf, &ebuf_len, &eout_len, 1, ZSTR_VAL(contextEngineID))) {
1048 		// TODO Promote to Error?
1049 		php_error_docref(NULL, E_WARNING, "Bad engine ID value '%s'", ZSTR_VAL(contextEngineID));
1050 		efree(ebuf);
1051 		return false;
1052 	}
1053 
1054 	if (s->contextEngineID) {
1055 		efree(s->contextEngineID);
1056 	}
1057 
1058 	s->contextEngineID = ebuf;
1059 	s->contextEngineIDLen = eout_len;
1060 	return true;
1061 }
1062 /* }}} */
1063 
1064 /* {{{ Set all snmpv3-related security options */
netsnmp_session_set_security(struct snmp_session * session,zend_string * sec_level,zend_string * auth_protocol,zend_string * auth_passphrase,zend_string * priv_protocol,zend_string * priv_passphrase,zend_string * contextName,zend_string * contextEngineID)1065 static bool netsnmp_session_set_security(struct snmp_session *session, zend_string *sec_level,
1066 	zend_string *auth_protocol, zend_string *auth_passphrase, zend_string *priv_protocol,
1067 	zend_string *priv_passphrase, zend_string *contextName, zend_string *contextEngineID)
1068 {
1069 
1070 	/* Setting the security level. */
1071 	if (!netsnmp_session_set_sec_level(session, sec_level)) {
1072 		/* ValueError already generated, just bail out */
1073 		return false;
1074 	}
1075 
1076 	if (session->securityLevel == SNMP_SEC_LEVEL_AUTHNOPRIV || session->securityLevel == SNMP_SEC_LEVEL_AUTHPRIV) {
1077 
1078 		/* Setting the authentication protocol. */
1079 		if (!netsnmp_session_set_auth_protocol(session, auth_protocol)) {
1080 			/* ValueError already generated, just bail out */
1081 			return false;
1082 		}
1083 
1084 		/* Setting the authentication passphrase. */
1085 		if (!netsnmp_session_gen_auth_key(session, auth_passphrase)) {
1086 			/* Warning message sent already, just bail out */
1087 			return false;
1088 		}
1089 
1090 		if (session->securityLevel == SNMP_SEC_LEVEL_AUTHPRIV) {
1091 			/* Setting the security protocol. */
1092 			if (!netsnmp_session_set_sec_protocol(session, priv_protocol)) {
1093 				/* ValueError already generated, just bail out */
1094 				return false;
1095 			}
1096 
1097 			/* Setting the security protocol passphrase. */
1098 			if (!netsnmp_session_gen_sec_key(session, priv_passphrase)) {
1099 				/* Warning message sent already, just bail out */
1100 				return false;
1101 			}
1102 		}
1103 	}
1104 
1105 	/* Setting contextName if specified */
1106 	if (contextName) {
1107 		session->contextName = ZSTR_VAL(contextName);
1108 		session->contextNameLen = ZSTR_LEN(contextName);
1109 	}
1110 
1111 	/* Setting contextEngineIS if specified */
1112 	if (contextEngineID && ZSTR_LEN(contextEngineID) && !netsnmp_session_set_contextEngineID(session, contextEngineID)) {
1113 		/* Warning message sent already, just bail out */
1114 		return false;
1115 	}
1116 
1117 	return true;
1118 }
1119 /* }}} */
1120 
1121 /* {{{ php_snmp
1122 *
1123 * Generic SNMP handler for all versions.
1124 * This function makes use of the internal SNMP object fetcher.
1125 * Used both in old (non-OO) and OO API
1126 *
1127 */
php_snmp(INTERNAL_FUNCTION_PARAMETERS,int st,int version)1128 static void php_snmp(INTERNAL_FUNCTION_PARAMETERS, int st, int version)
1129 {
1130 	zend_string *oid_str, *type_str = NULL, *value_str = NULL;
1131 	HashTable *oid_ht, *type_ht = NULL, *value_ht = NULL;
1132 	zend_string *a1 = NULL, *a2 = NULL, *a3 = NULL, *a4 = NULL, *a5 = NULL, *a6 = NULL, *a7 = NULL;
1133 	bool use_orignames = 0, suffix_keys = 0;
1134 	zend_long timeout = SNMP_DEFAULT_TIMEOUT;
1135 	zend_long retries = SNMP_DEFAULT_RETRIES;
1136 	struct objid_query objid_query;
1137 	php_snmp_session *session;
1138 	int session_less_mode = (getThis() == NULL);
1139 	php_snmp_object *snmp_object;
1140 	php_snmp_object glob_snmp_object;
1141 
1142 	objid_query.max_repetitions = -1;
1143 	objid_query.non_repeaters = 0;
1144 	objid_query.valueretrieval = SNMP_G(valueretrieval);
1145 	objid_query.oid_increasing_check = true;
1146 
1147 	if (session_less_mode) {
1148 		if (version == SNMP_VERSION_3) {
1149 			if (st & SNMP_CMD_SET) {
1150 				ZEND_PARSE_PARAMETERS_START(10, 12)
1151 					Z_PARAM_STR(a1)
1152 					Z_PARAM_STR(a2)
1153 					Z_PARAM_STR(a3)
1154 					Z_PARAM_STR(a4)
1155 					Z_PARAM_STR(a5)
1156 					Z_PARAM_STR(a6)
1157 					Z_PARAM_STR(a7)
1158 					Z_PARAM_ARRAY_HT_OR_STR(oid_ht, oid_str)
1159 					Z_PARAM_ARRAY_HT_OR_STR(type_ht, type_str)
1160 					Z_PARAM_ARRAY_HT_OR_STR(value_ht, value_str)
1161 					Z_PARAM_OPTIONAL
1162 					Z_PARAM_LONG(timeout)
1163 					Z_PARAM_LONG(retries)
1164 				ZEND_PARSE_PARAMETERS_END();
1165 			} else {
1166 				/* SNMP_CMD_GET
1167 				 * SNMP_CMD_GETNEXT
1168 				 * SNMP_CMD_WALK
1169 				 */
1170 				ZEND_PARSE_PARAMETERS_START(8, 10)
1171 					Z_PARAM_STR(a1)
1172 					Z_PARAM_STR(a2)
1173 					Z_PARAM_STR(a3)
1174 					Z_PARAM_STR(a4)
1175 					Z_PARAM_STR(a5)
1176 					Z_PARAM_STR(a6)
1177 					Z_PARAM_STR(a7)
1178 					Z_PARAM_ARRAY_HT_OR_STR(oid_ht, oid_str)
1179 					Z_PARAM_OPTIONAL
1180 					Z_PARAM_LONG(timeout)
1181 					Z_PARAM_LONG(retries)
1182 				ZEND_PARSE_PARAMETERS_END();
1183 			}
1184 		} else {
1185 			if (st & SNMP_CMD_SET) {
1186 				ZEND_PARSE_PARAMETERS_START(5, 7)
1187 					Z_PARAM_STR(a1)
1188 					Z_PARAM_STR(a2)
1189 					Z_PARAM_ARRAY_HT_OR_STR(oid_ht, oid_str)
1190 					Z_PARAM_ARRAY_HT_OR_STR(type_ht, type_str)
1191 					Z_PARAM_ARRAY_HT_OR_STR(value_ht, value_str)
1192 					Z_PARAM_OPTIONAL
1193 					Z_PARAM_LONG(timeout)
1194 					Z_PARAM_LONG(retries)
1195 				ZEND_PARSE_PARAMETERS_END();
1196 			} else {
1197 				/* SNMP_CMD_GET
1198 				 * SNMP_CMD_GETNEXT
1199 				 * SNMP_CMD_WALK
1200 				 */
1201 				ZEND_PARSE_PARAMETERS_START(3, 5)
1202 					Z_PARAM_STR(a1)
1203 					Z_PARAM_STR(a2)
1204 					Z_PARAM_ARRAY_HT_OR_STR(oid_ht, oid_str)
1205 					Z_PARAM_OPTIONAL
1206 					Z_PARAM_LONG(timeout)
1207 					Z_PARAM_LONG(retries)
1208 				ZEND_PARSE_PARAMETERS_END();
1209 			}
1210 		}
1211 	} else {
1212 		if (st & SNMP_CMD_SET) {
1213 			ZEND_PARSE_PARAMETERS_START(3, 3)
1214 				Z_PARAM_ARRAY_HT_OR_STR(oid_ht, oid_str)
1215 				Z_PARAM_ARRAY_HT_OR_STR(type_ht, type_str)
1216 				Z_PARAM_ARRAY_HT_OR_STR(value_ht, value_str)
1217 			ZEND_PARSE_PARAMETERS_END();
1218 		} else if (st & SNMP_CMD_WALK) {
1219 			ZEND_PARSE_PARAMETERS_START(1, 4)
1220 				Z_PARAM_ARRAY_HT_OR_STR(oid_ht, oid_str)
1221 				Z_PARAM_OPTIONAL
1222 				Z_PARAM_BOOL(suffix_keys)
1223 				Z_PARAM_LONG(objid_query.max_repetitions)
1224 				Z_PARAM_LONG(objid_query.non_repeaters)
1225 			ZEND_PARSE_PARAMETERS_END();
1226 			if (suffix_keys) {
1227 				st |= SNMP_USE_SUFFIX_AS_KEYS;
1228 			}
1229 		} else if (st & SNMP_CMD_GET) {
1230 			ZEND_PARSE_PARAMETERS_START(1, 2)
1231 				Z_PARAM_ARRAY_HT_OR_STR(oid_ht, oid_str)
1232 				Z_PARAM_OPTIONAL
1233 				Z_PARAM_BOOL(use_orignames)
1234 			ZEND_PARSE_PARAMETERS_END();
1235 			if (use_orignames) {
1236 				st |= SNMP_ORIGINAL_NAMES_AS_KEYS;
1237 			}
1238 		} else {
1239 			/* SNMP_CMD_GETNEXT
1240 			 */
1241 			ZEND_PARSE_PARAMETERS_START(1, 1)
1242 				Z_PARAM_ARRAY_HT_OR_STR(oid_ht, oid_str)
1243 			ZEND_PARSE_PARAMETERS_END();
1244 		}
1245 	}
1246 
1247 	if (!php_snmp_parse_oid(getThis(), st, &objid_query, oid_str, oid_ht, type_str, type_ht, value_str, value_ht)) {
1248 		RETURN_FALSE;
1249 	}
1250 
1251 	if (session_less_mode) {
1252 		if (!netsnmp_session_init(&session, version, a1, a2, timeout, retries)) {
1253 			efree(objid_query.vars);
1254 			netsnmp_session_free(&session);
1255 			RETURN_FALSE;
1256 		}
1257 		if (version == SNMP_VERSION_3 && !netsnmp_session_set_security(session, a3, a4, a5, a6, a7, NULL, NULL)) {
1258 			efree(objid_query.vars);
1259 			netsnmp_session_free(&session);
1260 			/* Warning message sent already, just bail out */
1261 			RETURN_FALSE;
1262 		}
1263 	} else {
1264 		zval *object = getThis();
1265 		snmp_object = Z_SNMP_P(object);
1266 		session = snmp_object->session;
1267 		if (!session) {
1268 			zend_throw_error(NULL, "Invalid or uninitialized SNMP object");
1269 			efree(objid_query.vars);
1270 			RETURN_THROWS();
1271 		}
1272 
1273 		if (snmp_object->max_oids > 0) {
1274 			objid_query.step = snmp_object->max_oids;
1275 			if (objid_query.max_repetitions < 0) { /* unspecified in function call, use session-wise */
1276 				objid_query.max_repetitions = snmp_object->max_oids;
1277 			}
1278 		}
1279 		objid_query.oid_increasing_check = snmp_object->oid_increasing_check;
1280 		objid_query.valueretrieval = snmp_object->valueretrieval;
1281 		glob_snmp_object.enum_print = netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM);
1282 		netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM, snmp_object->enum_print);
1283 		glob_snmp_object.quick_print = netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT);
1284 		netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT, snmp_object->quick_print);
1285 		glob_snmp_object.oid_output_format = netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT);
1286 		netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT, snmp_object->oid_output_format);
1287 	}
1288 
1289 	if (objid_query.max_repetitions < 0) {
1290 		objid_query.max_repetitions = 20; /* provide correct default value */
1291 	}
1292 
1293 	php_snmp_internal(INTERNAL_FUNCTION_PARAM_PASSTHRU, st, session, &objid_query);
1294 
1295 	efree(objid_query.vars);
1296 
1297 	if (session_less_mode) {
1298 		netsnmp_session_free(&session);
1299 	} else {
1300 		netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM, glob_snmp_object.enum_print);
1301 		netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT, glob_snmp_object.quick_print);
1302 		netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT, glob_snmp_object.oid_output_format);
1303 	}
1304 }
1305 /* }}} */
1306 
1307 /* {{{ Fetch a SNMP object */
PHP_FUNCTION(snmpget)1308 PHP_FUNCTION(snmpget)
1309 {
1310 	php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_GET, SNMP_VERSION_1);
1311 }
1312 /* }}} */
1313 
1314 /* {{{ Fetch a SNMP object */
PHP_FUNCTION(snmpgetnext)1315 PHP_FUNCTION(snmpgetnext)
1316 {
1317 	php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_GETNEXT, SNMP_VERSION_1);
1318 }
1319 /* }}} */
1320 
1321 /* {{{ Return all objects under the specified object id */
PHP_FUNCTION(snmpwalk)1322 PHP_FUNCTION(snmpwalk)
1323 {
1324 	php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, (SNMP_CMD_WALK | SNMP_NUMERIC_KEYS), SNMP_VERSION_1);
1325 }
1326 /* }}} */
1327 
1328 /* {{{ Return all objects including their respective object id within the specified one */
PHP_FUNCTION(snmprealwalk)1329 PHP_FUNCTION(snmprealwalk)
1330 {
1331 	php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_WALK, SNMP_VERSION_1);
1332 }
1333 /* }}} */
1334 
1335 /* {{{ Set the value of a SNMP object */
PHP_FUNCTION(snmpset)1336 PHP_FUNCTION(snmpset)
1337 {
1338 	php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_SET, SNMP_VERSION_1);
1339 }
1340 /* }}} */
1341 
1342 /* {{{ Return the current status of quick_print */
PHP_FUNCTION(snmp_get_quick_print)1343 PHP_FUNCTION(snmp_get_quick_print)
1344 {
1345 	if (zend_parse_parameters_none() == FAILURE) {
1346 		RETURN_THROWS();
1347 	}
1348 
1349 	RETURN_BOOL(netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT));
1350 }
1351 /* }}} */
1352 
1353 /* {{{ Return all objects including their respective object id within the specified one */
PHP_FUNCTION(snmp_set_quick_print)1354 PHP_FUNCTION(snmp_set_quick_print)
1355 {
1356 	bool a1;
1357 
1358 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "b", &a1) == FAILURE) {
1359 		RETURN_THROWS();
1360 	}
1361 
1362 	netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT, (int)a1);
1363 	RETURN_TRUE;
1364 }
1365 /* }}} */
1366 
1367 /* {{{ Return all values that are enums with their enum value instead of the raw integer */
PHP_FUNCTION(snmp_set_enum_print)1368 PHP_FUNCTION(snmp_set_enum_print)
1369 {
1370 	bool a1;
1371 
1372 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "b", &a1) == FAILURE) {
1373 		RETURN_THROWS();
1374 	}
1375 
1376 	netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM, (int) a1);
1377 	RETURN_TRUE;
1378 }
1379 /* }}} */
1380 
1381 /* {{{ Set the OID output format. */
PHP_FUNCTION(snmp_set_oid_output_format)1382 PHP_FUNCTION(snmp_set_oid_output_format)
1383 {
1384 	zend_long a1;
1385 
1386 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &a1) == FAILURE) {
1387 		RETURN_THROWS();
1388 	}
1389 
1390 	switch (a1) {
1391 		case NETSNMP_OID_OUTPUT_SUFFIX:
1392 		case NETSNMP_OID_OUTPUT_MODULE:
1393 		case NETSNMP_OID_OUTPUT_FULL:
1394 		case NETSNMP_OID_OUTPUT_NUMERIC:
1395 		case NETSNMP_OID_OUTPUT_UCD:
1396 		case NETSNMP_OID_OUTPUT_NONE:
1397 			netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT, a1);
1398 			RETURN_TRUE;
1399 		default:
1400 			zend_argument_value_error(1, "must be an SNMP_OID_OUTPUT_* constant");
1401 			RETURN_THROWS();
1402 	}
1403 }
1404 /* }}} */
1405 
1406 /* {{{ Fetch a SNMP object */
PHP_FUNCTION(snmp2_get)1407 PHP_FUNCTION(snmp2_get)
1408 {
1409 	php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_GET, SNMP_VERSION_2c);
1410 }
1411 /* }}} */
1412 
1413 /* {{{ Fetch a SNMP object */
PHP_FUNCTION(snmp2_getnext)1414 PHP_FUNCTION(snmp2_getnext)
1415 {
1416 	php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_GETNEXT, SNMP_VERSION_2c);
1417 }
1418 /* }}} */
1419 
1420 /* {{{ Return all objects under the specified object id */
PHP_FUNCTION(snmp2_walk)1421 PHP_FUNCTION(snmp2_walk)
1422 {
1423 	php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, (SNMP_CMD_WALK | SNMP_NUMERIC_KEYS), SNMP_VERSION_2c);
1424 }
1425 /* }}} */
1426 
1427 /* {{{ Return all objects including their respective object id within the specified one */
PHP_FUNCTION(snmp2_real_walk)1428 PHP_FUNCTION(snmp2_real_walk)
1429 {
1430 	php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_WALK, SNMP_VERSION_2c);
1431 }
1432 /* }}} */
1433 
1434 /* {{{ Set the value of a SNMP object */
PHP_FUNCTION(snmp2_set)1435 PHP_FUNCTION(snmp2_set)
1436 {
1437 	php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_SET, SNMP_VERSION_2c);
1438 }
1439 /* }}} */
1440 
1441 /* {{{ Fetch the value of a SNMP object */
PHP_FUNCTION(snmp3_get)1442 PHP_FUNCTION(snmp3_get)
1443 {
1444 	php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_GET, SNMP_VERSION_3);
1445 }
1446 /* }}} */
1447 
1448 /* {{{ Fetch the value of a SNMP object */
PHP_FUNCTION(snmp3_getnext)1449 PHP_FUNCTION(snmp3_getnext)
1450 {
1451 	php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_GETNEXT, SNMP_VERSION_3);
1452 }
1453 /* }}} */
1454 
1455 /* {{{ Fetch the value of a SNMP object */
PHP_FUNCTION(snmp3_walk)1456 PHP_FUNCTION(snmp3_walk)
1457 {
1458 	php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, (SNMP_CMD_WALK | SNMP_NUMERIC_KEYS), SNMP_VERSION_3);
1459 }
1460 /* }}} */
1461 
1462 /* {{{ Fetch the value of a SNMP object */
PHP_FUNCTION(snmp3_real_walk)1463 PHP_FUNCTION(snmp3_real_walk)
1464 {
1465 	php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_WALK, SNMP_VERSION_3);
1466 }
1467 /* }}} */
1468 
1469 /* {{{ Fetch the value of a SNMP object */
PHP_FUNCTION(snmp3_set)1470 PHP_FUNCTION(snmp3_set)
1471 {
1472 	php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_SET, SNMP_VERSION_3);
1473 }
1474 /* }}} */
1475 
1476 /* {{{ Specify the method how the SNMP values will be returned */
PHP_FUNCTION(snmp_set_valueretrieval)1477 PHP_FUNCTION(snmp_set_valueretrieval)
1478 {
1479 	zend_long method;
1480 
1481 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &method) == FAILURE) {
1482 		RETURN_THROWS();
1483 	}
1484 
1485 	if (method >= 0 && method <= (SNMP_VALUE_LIBRARY|SNMP_VALUE_PLAIN|SNMP_VALUE_OBJECT)) {
1486 			SNMP_G(valueretrieval) = method;
1487 			RETURN_TRUE;
1488 	} else {
1489 		zend_argument_value_error(1, "must be a bitmask of SNMP_VALUE_LIBRARY, SNMP_VALUE_PLAIN, and SNMP_VALUE_OBJECT");
1490 		RETURN_THROWS();
1491 	}
1492 }
1493 /* }}} */
1494 
1495 /* {{{ Return the method how the SNMP values will be returned */
PHP_FUNCTION(snmp_get_valueretrieval)1496 PHP_FUNCTION(snmp_get_valueretrieval)
1497 {
1498 	if (zend_parse_parameters_none() == FAILURE) {
1499 		RETURN_THROWS();
1500 	}
1501 
1502 	RETURN_LONG(SNMP_G(valueretrieval));
1503 }
1504 /* }}} */
1505 
1506 /* {{{ Reads and parses a MIB file into the active MIB tree. */
PHP_FUNCTION(snmp_read_mib)1507 PHP_FUNCTION(snmp_read_mib)
1508 {
1509 	char *filename;
1510 	size_t filename_len;
1511 
1512 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &filename, &filename_len) == FAILURE) {
1513 		RETURN_THROWS();
1514 	}
1515 
1516 	if (!read_mib(filename)) {
1517 		char *error = strerror(errno);
1518 		php_error_docref(NULL, E_WARNING, "Error while reading MIB file '%s': %s", filename, error);
1519 		RETURN_FALSE;
1520 	}
1521 	RETURN_TRUE;
1522 }
1523 /* }}} */
1524 
1525 /* {{{ Creates a new SNMP session to specified host. */
PHP_METHOD(SNMP,__construct)1526 PHP_METHOD(SNMP, __construct)
1527 {
1528 	php_snmp_object *snmp_object;
1529 	zval *object = ZEND_THIS;
1530 	zend_string *a1, *a2;
1531 	zend_long timeout = SNMP_DEFAULT_TIMEOUT;
1532 	zend_long retries = SNMP_DEFAULT_RETRIES;
1533 	zend_long version = SNMP_DEFAULT_VERSION;
1534 
1535 	snmp_object = Z_SNMP_P(object);
1536 
1537 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "lSS|ll", &version, &a1, &a2, &timeout, &retries) == FAILURE) {
1538 		RETURN_THROWS();
1539 	}
1540 
1541 	switch (version) {
1542 		case SNMP_VERSION_1:
1543 		case SNMP_VERSION_2c:
1544 		case SNMP_VERSION_3:
1545 			break;
1546 		default:
1547 			zend_argument_value_error(1, "must be a valid SNMP protocol version");
1548 			RETURN_THROWS();
1549 	}
1550 
1551 	/* handle re-open of snmp session */
1552 	if (snmp_object->session) {
1553 		netsnmp_session_free(&(snmp_object->session));
1554 	}
1555 
1556 	if (!netsnmp_session_init(&(snmp_object->session), version, a1, a2, timeout, retries)) {
1557 		return;
1558 	}
1559 	snmp_object->max_oids = 0;
1560 	snmp_object->valueretrieval = SNMP_G(valueretrieval);
1561 	snmp_object->enum_print = netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM);
1562 	snmp_object->oid_output_format = netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT);
1563 	snmp_object->quick_print = netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT);
1564 	snmp_object->oid_increasing_check = true;
1565 	snmp_object->exceptions_enabled = 0;
1566 }
1567 /* }}} */
1568 
1569 /* {{{ Close SNMP session */
PHP_METHOD(SNMP,close)1570 PHP_METHOD(SNMP, close)
1571 {
1572 	php_snmp_object *snmp_object;
1573 	zval *object = ZEND_THIS;
1574 
1575 	snmp_object = Z_SNMP_P(object);
1576 
1577 	if (zend_parse_parameters_none() == FAILURE) {
1578 		RETURN_THROWS();
1579 	}
1580 
1581 	netsnmp_session_free(&(snmp_object->session));
1582 
1583 	RETURN_TRUE;
1584 }
1585 /* }}} */
1586 
1587 /* {{{ Fetch a SNMP object returning scalar for single OID and array of oid->value pairs for multi OID request */
PHP_METHOD(SNMP,get)1588 PHP_METHOD(SNMP, get)
1589 {
1590 	php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_GET, (-1));
1591 }
1592 /* }}} */
1593 
1594 /* {{{ Fetch a SNMP object returning scalar for single OID and array of oid->value pairs for multi OID request */
PHP_METHOD(SNMP,getnext)1595 PHP_METHOD(SNMP, getnext)
1596 {
1597 	php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_GETNEXT, (-1));
1598 }
1599 /* }}} */
1600 
1601 /* {{{ Return all objects including their respective object id within the specified one as array of oid->value pairs */
PHP_METHOD(SNMP,walk)1602 PHP_METHOD(SNMP, walk)
1603 {
1604 	php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_WALK, (-1));
1605 }
1606 /* }}} */
1607 
1608 /* {{{ Set the value of a SNMP object */
PHP_METHOD(SNMP,set)1609 PHP_METHOD(SNMP, set)
1610 {
1611 	php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_SET, (-1));
1612 }
1613 /* }}} */
1614 
1615 /* {{{ Set SNMPv3 security-related session parameters */
PHP_METHOD(SNMP,setSecurity)1616 PHP_METHOD(SNMP, setSecurity)
1617 {
1618 	php_snmp_object *snmp_object;
1619 	zval *object = ZEND_THIS;
1620 	zend_string *a1 = NULL, *a2 = NULL, *a3 = NULL, *a4 = NULL, *a5 = NULL, *a6 = NULL, *a7 = NULL;
1621 
1622 	snmp_object = Z_SNMP_P(object);
1623 
1624 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|SSSSSS", &a1, &a2, &a3, &a4,&a5, &a6, &a7) == FAILURE) {
1625 		RETURN_THROWS();
1626 	}
1627 
1628 	if (!netsnmp_session_set_security(snmp_object->session, a1, a2, a3, a4, a5, a6, a7)) {
1629 		/* Warning message sent already, just bail out */
1630 		RETURN_FALSE;
1631 	}
1632 	RETURN_TRUE;
1633 }
1634 /* }}} */
1635 
1636 /* {{{ Get last error code number */
PHP_METHOD(SNMP,getErrno)1637 PHP_METHOD(SNMP, getErrno)
1638 {
1639 	php_snmp_object *snmp_object;
1640 	zval *object = ZEND_THIS;
1641 
1642 	snmp_object = Z_SNMP_P(object);
1643 
1644 	if (zend_parse_parameters_none() == FAILURE) {
1645 		RETURN_THROWS();
1646 	}
1647 
1648 	RETURN_LONG(snmp_object->snmp_errno);
1649 }
1650 /* }}} */
1651 
1652 /* {{{ Get last error message */
PHP_METHOD(SNMP,getError)1653 PHP_METHOD(SNMP, getError)
1654 {
1655 	php_snmp_object *snmp_object;
1656 	zval *object = ZEND_THIS;
1657 
1658 	snmp_object = Z_SNMP_P(object);
1659 
1660 	if (zend_parse_parameters_none() == FAILURE) {
1661 		RETURN_THROWS();
1662 	}
1663 
1664 	RETURN_STRING(snmp_object->snmp_errstr);
1665 }
1666 /* }}} */
1667 
1668 /* {{{ */
php_snmp_add_property(HashTable * h,const char * name,size_t name_length,php_snmp_read_t read_func,php_snmp_write_t write_func)1669 void php_snmp_add_property(HashTable *h, const char *name, size_t name_length, php_snmp_read_t read_func, php_snmp_write_t write_func)
1670 {
1671 	php_snmp_prop_handler p;
1672 	zend_string *str;
1673 
1674 	p.name = (char*) name;
1675 	p.name_length = name_length;
1676 	p.read_func = (read_func) ? read_func : NULL;
1677 	p.write_func = (write_func) ? write_func : NULL;
1678 	str = zend_string_init_interned(name, name_length, 1);
1679 	zend_hash_add_mem(h, str, &p, sizeof(php_snmp_prop_handler));
1680 	zend_string_release_ex(str, 1);
1681 }
1682 /* }}} */
1683 
1684 /* {{{ php_snmp_read_property(zval *object, zval *member, int type[, const zend_literal *key])
1685    Generic object property reader */
php_snmp_read_property(zend_object * object,zend_string * name,int type,void ** cache_slot,zval * rv)1686 zval *php_snmp_read_property(zend_object *object, zend_string *name, int type, void **cache_slot, zval *rv)
1687 {
1688 	zval *retval;
1689 	php_snmp_object *obj;
1690 	php_snmp_prop_handler *hnd;
1691 	int ret;
1692 
1693 	obj = php_snmp_fetch_object(object);
1694 	hnd = zend_hash_find_ptr(&php_snmp_properties, name);
1695 
1696 	if (hnd && hnd->read_func) {
1697 		ret = hnd->read_func(obj, rv);
1698 		if (ret == SUCCESS) {
1699 			retval = rv;
1700 		} else {
1701 			retval = &EG(uninitialized_zval);
1702 		}
1703 	} else {
1704 		retval = zend_std_read_property(object, name, type, cache_slot, rv);
1705 	}
1706 
1707 	return retval;
1708 }
1709 /* }}} */
1710 
1711 /* {{{ Generic object property writer */
php_snmp_write_property(zend_object * object,zend_string * name,zval * value,void ** cache_slot)1712 zval *php_snmp_write_property(zend_object *object, zend_string *name, zval *value, void **cache_slot)
1713 {
1714 	php_snmp_object *obj = php_snmp_fetch_object(object);
1715 	php_snmp_prop_handler *hnd = zend_hash_find_ptr(&php_snmp_properties, name);
1716 
1717 	if (hnd) {
1718 		if (!hnd->write_func) {
1719 			zend_throw_error(NULL, "Cannot write read-only property %s::$%s", ZSTR_VAL(object->ce->name), ZSTR_VAL(name));
1720 			return &EG(error_zval);
1721 		}
1722 
1723 		zend_property_info *prop = zend_get_property_info(object->ce, name, /* silent */ true);
1724 		if (prop && ZEND_TYPE_IS_SET(prop->type)) {
1725 			zval tmp;
1726 			ZVAL_COPY(&tmp, value);
1727 			if (!zend_verify_property_type(prop, &tmp,
1728 						ZEND_CALL_USES_STRICT_TYPES(EG(current_execute_data)))) {
1729 				zval_ptr_dtor(&tmp);
1730 				return &EG(error_zval);
1731 			}
1732 			hnd->write_func(obj, &tmp);
1733 			zval_ptr_dtor(&tmp);
1734 		} else {
1735 			hnd->write_func(obj, value);
1736 		}
1737 		return value;
1738 	}
1739 
1740 	return zend_std_write_property(object, name, value, cache_slot);
1741 }
1742 /* }}} */
1743 
1744 /* {{{ php_snmp_has_property(zval *object, zval *member, int has_set_exists[, const zend_literal *key])
1745    Generic object property checker */
php_snmp_has_property(zend_object * object,zend_string * name,int has_set_exists,void ** cache_slot)1746 static int php_snmp_has_property(zend_object *object, zend_string *name, int has_set_exists, void **cache_slot)
1747 {
1748 	zval rv;
1749 	php_snmp_prop_handler *hnd;
1750 	int ret = 0;
1751 
1752 	if ((hnd = zend_hash_find_ptr(&php_snmp_properties, name)) != NULL) {
1753 		switch (has_set_exists) {
1754 			case ZEND_PROPERTY_EXISTS:
1755 				ret = 1;
1756 				break;
1757 			case ZEND_PROPERTY_ISSET: {
1758 				zval *value = php_snmp_read_property(object, name, BP_VAR_IS, cache_slot, &rv);
1759 				if (value != &EG(uninitialized_zval)) {
1760 					ret = Z_TYPE_P(value) != IS_NULL? 1 : 0;
1761 					zval_ptr_dtor(value);
1762 				}
1763 				break;
1764 			}
1765 			default: {
1766 				zval *value = php_snmp_read_property(object, name, BP_VAR_IS, cache_slot, &rv);
1767 				if (value != &EG(uninitialized_zval)) {
1768 					convert_to_boolean(value);
1769 					ret = Z_TYPE_P(value) == IS_TRUE? 1:0;
1770 				}
1771 				break;
1772 			}
1773 		}
1774 	} else {
1775 		ret = zend_std_has_property(object, name, has_set_exists, cache_slot);
1776 	}
1777 	return ret;
1778 }
1779 /* }}} */
1780 
php_snmp_get_gc(zend_object * object,zval ** gc_data,int * gc_data_count)1781 static HashTable *php_snmp_get_gc(zend_object *object, zval **gc_data, int *gc_data_count) /* {{{ */
1782 {
1783 	*gc_data = NULL;
1784 	*gc_data_count = 0;
1785 	return zend_std_get_properties(object);
1786 }
1787 /* }}} */
1788 
1789 /* {{{ php_snmp_get_properties(zval *object)
1790    Returns all object properties. Injects SNMP properties into object on first call */
php_snmp_get_properties(zend_object * object)1791 static HashTable *php_snmp_get_properties(zend_object *object)
1792 {
1793 	php_snmp_object *obj;
1794 	php_snmp_prop_handler *hnd;
1795 	HashTable *props;
1796 	zval rv;
1797 	zend_string *key;
1798 
1799 	obj = php_snmp_fetch_object(object);
1800 	props = zend_std_get_properties(object);
1801 
1802 	ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(&php_snmp_properties, key, hnd) {
1803 		if (!hnd->read_func || hnd->read_func(obj, &rv) != SUCCESS) {
1804 			ZVAL_NULL(&rv);
1805 		}
1806 		zend_hash_update(props, key, &rv);
1807 	} ZEND_HASH_FOREACH_END();
1808 
1809 	return obj->zo.properties;
1810 }
1811 /* }}} */
1812 
php_snmp_get_property_ptr_ptr(zend_object * object,zend_string * name,int type,void ** cache_slot)1813 static zval *php_snmp_get_property_ptr_ptr(zend_object *object, zend_string *name, int type, void **cache_slot)
1814 {
1815 	php_snmp_prop_handler *hnd = zend_hash_find_ptr(&php_snmp_properties, name);
1816 	if (hnd == NULL) {
1817 		return zend_std_get_property_ptr_ptr(object, name, type, cache_slot);
1818 	}
1819 
1820 	return NULL;
1821 }
1822 
1823 /* {{{ */
php_snmp_read_info(php_snmp_object * snmp_object,zval * retval)1824 static int php_snmp_read_info(php_snmp_object *snmp_object, zval *retval)
1825 {
1826 	zval val;
1827 
1828 	array_init(retval);
1829 
1830 	if (snmp_object->session == NULL) {
1831 		return SUCCESS;
1832 	}
1833 
1834 	ZVAL_STRINGL(&val, snmp_object->session->peername, strlen(snmp_object->session->peername));
1835 	add_assoc_zval(retval, "hostname", &val);
1836 
1837 	ZVAL_LONG(&val, snmp_object->session->timeout);
1838 	add_assoc_zval(retval, "timeout", &val);
1839 
1840 	ZVAL_LONG(&val, snmp_object->session->retries);
1841 	add_assoc_zval(retval, "retries", &val);
1842 
1843 	return SUCCESS;
1844 }
1845 /* }}} */
1846 
1847 /* {{{ */
php_snmp_read_max_oids(php_snmp_object * snmp_object,zval * retval)1848 static int php_snmp_read_max_oids(php_snmp_object *snmp_object, zval *retval)
1849 {
1850 	if (snmp_object->max_oids > 0) {
1851 		ZVAL_LONG(retval, snmp_object->max_oids);
1852 	} else {
1853 		ZVAL_NULL(retval);
1854 	}
1855 	return SUCCESS;
1856 }
1857 /* }}} */
1858 
1859 #define PHP_SNMP_BOOL_PROPERTY_READER_FUNCTION(name) \
1860 	static int php_snmp_read_##name(php_snmp_object *snmp_object, zval *retval) \
1861 	{ \
1862 		ZVAL_BOOL(retval, snmp_object->name); \
1863 		return SUCCESS; \
1864 	}
1865 
1866 PHP_SNMP_BOOL_PROPERTY_READER_FUNCTION(oid_increasing_check)
PHP_SNMP_BOOL_PROPERTY_READER_FUNCTION(quick_print)1867 PHP_SNMP_BOOL_PROPERTY_READER_FUNCTION(quick_print)
1868 PHP_SNMP_BOOL_PROPERTY_READER_FUNCTION(enum_print)
1869 
1870 #define PHP_SNMP_LONG_PROPERTY_READER_FUNCTION(name) \
1871 	static int php_snmp_read_##name(php_snmp_object *snmp_object, zval *retval) \
1872 	{ \
1873 		ZVAL_LONG(retval, snmp_object->name); \
1874 		return SUCCESS; \
1875 	}
1876 
1877 PHP_SNMP_LONG_PROPERTY_READER_FUNCTION(valueretrieval)
1878 PHP_SNMP_LONG_PROPERTY_READER_FUNCTION(oid_output_format)
1879 PHP_SNMP_LONG_PROPERTY_READER_FUNCTION(exceptions_enabled)
1880 
1881 /* {{{ */
1882 static int php_snmp_write_max_oids(php_snmp_object *snmp_object, zval *newval)
1883 {
1884 	zend_long lval;
1885 
1886 	if (Z_TYPE_P(newval) == IS_NULL) {
1887 		snmp_object->max_oids = 0;
1888 		return SUCCESS;
1889 	}
1890 
1891 	lval = zval_get_long(newval);
1892 
1893 	if (lval <= 0) {
1894 		zend_value_error("SNMP::$max_oids must be greater than 0 or null");
1895 		return FAILURE;
1896 	}
1897 	snmp_object->max_oids = lval;
1898 
1899 	return SUCCESS;
1900 }
1901 /* }}} */
1902 
1903 /* {{{ */
php_snmp_write_valueretrieval(php_snmp_object * snmp_object,zval * newval)1904 static int php_snmp_write_valueretrieval(php_snmp_object *snmp_object, zval *newval)
1905 {
1906 	zend_long lval = zval_get_long(newval);
1907 
1908 	if (lval >= 0 && lval <= (SNMP_VALUE_LIBRARY|SNMP_VALUE_PLAIN|SNMP_VALUE_OBJECT)) {
1909 		snmp_object->valueretrieval = lval;
1910 	} else {
1911 		zend_value_error("SNMP retrieval method must be a bitmask of SNMP_VALUE_LIBRARY, SNMP_VALUE_PLAIN, and SNMP_VALUE_OBJECT");
1912 		return FAILURE;
1913 	}
1914 
1915 	return SUCCESS;
1916 }
1917 /* }}} */
1918 
1919 #define PHP_SNMP_BOOL_PROPERTY_WRITER_FUNCTION(name) \
1920 static int php_snmp_write_##name(php_snmp_object *snmp_object, zval *newval) \
1921 { \
1922 	zval ztmp; \
1923 	ZVAL_COPY(&ztmp, newval); \
1924 	convert_to_boolean(&ztmp); \
1925 	newval = &ztmp; \
1926 \
1927 	snmp_object->name = Z_TYPE_P(newval) == IS_TRUE? 1 : 0; \
1928 \
1929 	return SUCCESS; \
1930 }
1931 
1932 PHP_SNMP_BOOL_PROPERTY_WRITER_FUNCTION(quick_print)
PHP_SNMP_BOOL_PROPERTY_WRITER_FUNCTION(enum_print)1933 PHP_SNMP_BOOL_PROPERTY_WRITER_FUNCTION(enum_print)
1934 PHP_SNMP_BOOL_PROPERTY_WRITER_FUNCTION(oid_increasing_check)
1935 
1936 /* {{{ */
1937 static int php_snmp_write_oid_output_format(php_snmp_object *snmp_object, zval *newval)
1938 {
1939 	zend_long lval = zval_get_long(newval);
1940 
1941 	switch(lval) {
1942 		case NETSNMP_OID_OUTPUT_SUFFIX:
1943 		case NETSNMP_OID_OUTPUT_MODULE:
1944 		case NETSNMP_OID_OUTPUT_FULL:
1945 		case NETSNMP_OID_OUTPUT_NUMERIC:
1946 		case NETSNMP_OID_OUTPUT_UCD:
1947 		case NETSNMP_OID_OUTPUT_NONE:
1948 			snmp_object->oid_output_format = lval;
1949 			return SUCCESS;
1950 		default:
1951 			zend_value_error("SNMP output print format must be an SNMP_OID_OUTPUT_* constant");
1952 			return FAILURE;
1953 	}
1954 }
1955 /* }}} */
1956 
1957 /* {{{ */
php_snmp_write_exceptions_enabled(php_snmp_object * snmp_object,zval * newval)1958 static int php_snmp_write_exceptions_enabled(php_snmp_object *snmp_object, zval *newval)
1959 {
1960 	int ret = SUCCESS;
1961 
1962 	snmp_object->exceptions_enabled = zval_get_long(newval);
1963 
1964 	return ret;
1965 }
1966 /* }}} */
1967 
free_php_snmp_properties(zval * el)1968 static void free_php_snmp_properties(zval *el)  /* {{{ */
1969 {
1970 	pefree(Z_PTR_P(el), 1);
1971 }
1972 /* }}} */
1973 
1974 #define PHP_SNMP_PROPERTY_ENTRY_RECORD(name) \
1975 	{ "" #name "",		sizeof("" #name "") - 1,	php_snmp_read_##name,	php_snmp_write_##name }
1976 
1977 #define PHP_SNMP_READONLY_PROPERTY_ENTRY_RECORD(name) \
1978 	{ "" #name "",		sizeof("" #name "") - 1,	php_snmp_read_##name,	NULL }
1979 
1980 const php_snmp_prop_handler php_snmp_property_entries[] = {
1981 	PHP_SNMP_READONLY_PROPERTY_ENTRY_RECORD(info),
1982 	PHP_SNMP_PROPERTY_ENTRY_RECORD(max_oids),
1983 	PHP_SNMP_PROPERTY_ENTRY_RECORD(valueretrieval),
1984 	PHP_SNMP_PROPERTY_ENTRY_RECORD(quick_print),
1985 	PHP_SNMP_PROPERTY_ENTRY_RECORD(enum_print),
1986 	PHP_SNMP_PROPERTY_ENTRY_RECORD(oid_output_format),
1987 	PHP_SNMP_PROPERTY_ENTRY_RECORD(oid_increasing_check),
1988 	PHP_SNMP_PROPERTY_ENTRY_RECORD(exceptions_enabled),
1989 	{ NULL, 0, NULL, NULL}
1990 };
1991 /* }}} */
1992 
1993 /* {{{ PHP_MINIT_FUNCTION */
PHP_MINIT_FUNCTION(snmp)1994 PHP_MINIT_FUNCTION(snmp)
1995 {
1996 	netsnmp_log_handler *logh;
1997 
1998 	init_snmp("snmpapp");
1999 	/* net-snmp corrupts the CTYPE locale during initialization. */
2000 	zend_reset_lc_ctype_locale();
2001 
2002 #ifdef NETSNMP_DS_LIB_DONT_PERSIST_STATE
2003 	/* Prevent update of the snmpapp.conf file */
2004 	netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_DONT_PERSIST_STATE, 1);
2005 #endif
2006 
2007 	/* Disable logging, use exit status'es and related variabled to detect errors */
2008 	shutdown_snmp_logging();
2009 	logh = netsnmp_register_loghandler(NETSNMP_LOGHANDLER_NONE, LOG_ERR);
2010 	if (logh) {
2011 		logh->pri_max = LOG_ERR;
2012 	}
2013 
2014 	memcpy(&php_snmp_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
2015 	php_snmp_object_handlers.read_property = php_snmp_read_property;
2016 	php_snmp_object_handlers.write_property = php_snmp_write_property;
2017 	php_snmp_object_handlers.get_property_ptr_ptr = php_snmp_get_property_ptr_ptr;
2018 	php_snmp_object_handlers.has_property = php_snmp_has_property;
2019 	php_snmp_object_handlers.get_properties = php_snmp_get_properties;
2020 	php_snmp_object_handlers.get_gc = php_snmp_get_gc;
2021 
2022 	/* Register SNMP Class */
2023 	php_snmp_ce = register_class_SNMP();
2024 	php_snmp_ce->create_object = php_snmp_object_new;
2025 	php_snmp_ce->default_object_handlers = &php_snmp_object_handlers;
2026 	php_snmp_object_handlers.offset = XtOffsetOf(php_snmp_object, zo);
2027 	php_snmp_object_handlers.clone_obj = NULL;
2028 	php_snmp_object_handlers.free_obj = php_snmp_object_free_storage;
2029 
2030 	/* Register SNMP Class properties */
2031 	zend_hash_init(&php_snmp_properties, 0, NULL, free_php_snmp_properties, 1);
2032 	PHP_SNMP_ADD_PROPERTIES(&php_snmp_properties, php_snmp_property_entries);
2033 
2034 	/* Register SNMPException class */
2035 	php_snmp_exception_ce = register_class_SNMPException(spl_ce_RuntimeException);
2036 
2037 	register_snmp_symbols(module_number);
2038 
2039 	return SUCCESS;
2040 }
2041 /* }}} */
2042 
2043 /* {{{ PHP_MSHUTDOWN_FUNCTION */
PHP_MSHUTDOWN_FUNCTION(snmp)2044 PHP_MSHUTDOWN_FUNCTION(snmp)
2045 {
2046 	snmp_shutdown("snmpapp");
2047 
2048 	zend_hash_destroy(&php_snmp_properties);
2049 
2050 	return SUCCESS;
2051 }
2052 /* }}} */
2053 
2054 /* {{{ PHP_MINFO_FUNCTION */
PHP_MINFO_FUNCTION(snmp)2055 PHP_MINFO_FUNCTION(snmp)
2056 {
2057 	php_info_print_table_start();
2058 	php_info_print_table_row(2, "NET-SNMP Support", "enabled");
2059 	php_info_print_table_row(2, "NET-SNMP Version", netsnmp_get_version());
2060 	php_info_print_table_end();
2061 }
2062 /* }}} */
2063 
2064 /* {{{ snmp_module_deps[] */
2065 static const zend_module_dep snmp_module_deps[] = {
2066 	ZEND_MOD_REQUIRED("spl")
2067 	ZEND_MOD_END
2068 };
2069 /* }}} */
2070 
2071 /* {{{ snmp_module_entry */
2072 zend_module_entry snmp_module_entry = {
2073 	STANDARD_MODULE_HEADER_EX,
2074 	NULL,
2075 	snmp_module_deps,
2076 	"snmp",
2077 	ext_functions,
2078 	PHP_MINIT(snmp),
2079 	PHP_MSHUTDOWN(snmp),
2080 	NULL,
2081 	NULL,
2082 	PHP_MINFO(snmp),
2083 	PHP_SNMP_VERSION,
2084 	PHP_MODULE_GLOBALS(snmp),
2085 	PHP_GINIT(snmp),
2086 	NULL,
2087 	NULL,
2088 	STANDARD_MODULE_PROPERTIES_EX
2089 };
2090 /* }}} */
2091 
2092 #endif
2093