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