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