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