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