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