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