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