forked from php/php-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snmp.c
2451 lines (2146 loc) · 74.9 KB
/
snmp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
+----------------------------------------------------------------------+
| PHP Version 7 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2017 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Rasmus Lerdorf <[email protected]> |
| Mike Jackson <[email protected]> |
| Steven Lawrance <[email protected]> |
| Harrie Hazewinkel <[email protected]> |
| Johann Hanne <[email protected]> |
| Boris Lytockin <[email protected]> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "main/php_network.h"
#include "ext/standard/info.h"
#include "php_snmp.h"
#include "zend_exceptions.h"
#include "ext/spl/spl_exceptions.h"
#if HAVE_SNMP
#include <sys/types.h>
#ifdef PHP_WIN32
#include <winsock2.h>
#include <errno.h>
#include <process.h>
#include "win32/time.h"
#else
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#ifndef _OSD_POSIX
#include <sys/errno.h>
#else
#include <errno.h> /* BS2000/OSD uses <errno.h>, not <sys/errno.h> */
#endif
#include <netdb.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifndef __P
#ifdef __GNUC__
#define __P(args) args
#else
#define __P(args) ()
#endif
#endif
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
/* For net-snmp prior to 5.4 */
#ifndef HAVE_SHUTDOWN_SNMP_LOGGING
extern netsnmp_log_handler *logh_head;
#define shutdown_snmp_logging() \
{ \
snmp_disable_log(); \
while(NULL != logh_head) \
netsnmp_remove_loghandler( logh_head ); \
}
#endif
#define SNMP_VALUE_LIBRARY (0 << 0)
#define SNMP_VALUE_PLAIN (1 << 0)
#define SNMP_VALUE_OBJECT (1 << 1)
typedef struct snmp_session php_snmp_session;
#define PHP_SNMP_SESSION_RES_NAME "SNMP session"
#define PHP_SNMP_ADD_PROPERTIES(a, b) \
{ \
int i = 0; \
while (b[i].name != NULL) { \
php_snmp_add_property((a), (b)[i].name, (b)[i].name_length, \
(php_snmp_read_t)(b)[i].read_func, (php_snmp_write_t)(b)[i].write_func); \
i++; \
} \
}
#define PHP_SNMP_ERRNO_NOERROR 0
#define PHP_SNMP_ERRNO_GENERIC (1 << 1)
#define PHP_SNMP_ERRNO_TIMEOUT (1 << 2)
#define PHP_SNMP_ERRNO_ERROR_IN_REPLY (1 << 3)
#define PHP_SNMP_ERRNO_OID_NOT_INCREASING (1 << 4)
#define PHP_SNMP_ERRNO_OID_PARSING_ERROR (1 << 5)
#define PHP_SNMP_ERRNO_MULTIPLE_SET_QUERIES (1 << 6)
#define PHP_SNMP_ERRNO_ANY ( \
PHP_SNMP_ERRNO_GENERIC | \
PHP_SNMP_ERRNO_TIMEOUT | \
PHP_SNMP_ERRNO_ERROR_IN_REPLY | \
PHP_SNMP_ERRNO_OID_NOT_INCREASING | \
PHP_SNMP_ERRNO_OID_PARSING_ERROR | \
PHP_SNMP_ERRNO_MULTIPLE_SET_QUERIES | \
PHP_SNMP_ERRNO_NOERROR \
)
ZEND_DECLARE_MODULE_GLOBALS(snmp)
static PHP_GINIT_FUNCTION(snmp);
/* constant - can be shared among threads */
static oid objid_mib[] = {1, 3, 6, 1, 2, 1};
static int le_snmp_session;
/* Handlers */
static zend_object_handlers php_snmp_object_handlers;
/* Class entries */
zend_class_entry *php_snmp_ce;
zend_class_entry *php_snmp_exception_ce;
/* Class object properties */
static HashTable php_snmp_properties;
/* {{{ arginfo */
ZEND_BEGIN_ARG_INFO_EX(arginfo_snmpget, 0, 0, 3)
ZEND_ARG_INFO(0, host)
ZEND_ARG_INFO(0, community)
ZEND_ARG_INFO(0, object_id)
ZEND_ARG_INFO(0, timeout)
ZEND_ARG_INFO(0, retries)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_snmpgetnext, 0, 0, 3)
ZEND_ARG_INFO(0, host)
ZEND_ARG_INFO(0, community)
ZEND_ARG_INFO(0, object_id)
ZEND_ARG_INFO(0, timeout)
ZEND_ARG_INFO(0, retries)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_snmpwalk, 0, 0, 3)
ZEND_ARG_INFO(0, host)
ZEND_ARG_INFO(0, community)
ZEND_ARG_INFO(0, object_id)
ZEND_ARG_INFO(0, timeout)
ZEND_ARG_INFO(0, retries)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_snmprealwalk, 0, 0, 3)
ZEND_ARG_INFO(0, host)
ZEND_ARG_INFO(0, community)
ZEND_ARG_INFO(0, object_id)
ZEND_ARG_INFO(0, timeout)
ZEND_ARG_INFO(0, retries)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_snmpset, 0, 0, 5)
ZEND_ARG_INFO(0, host)
ZEND_ARG_INFO(0, community)
ZEND_ARG_INFO(0, object_id)
ZEND_ARG_INFO(0, type)
ZEND_ARG_INFO(0, value)
ZEND_ARG_INFO(0, timeout)
ZEND_ARG_INFO(0, retries)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp_get_quick_print, 0, 0, 1)
ZEND_ARG_INFO(0, d)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp_set_quick_print, 0, 0, 1)
ZEND_ARG_INFO(0, quick_print)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp_set_enum_print, 0, 0, 1)
ZEND_ARG_INFO(0, enum_print)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp_set_oid_output_format, 0, 0, 1)
ZEND_ARG_INFO(0, oid_format)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp2_get, 0, 0, 3)
ZEND_ARG_INFO(0, host)
ZEND_ARG_INFO(0, community)
ZEND_ARG_INFO(0, object_id)
ZEND_ARG_INFO(0, timeout)
ZEND_ARG_INFO(0, retries)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp2_getnext, 0, 0, 3)
ZEND_ARG_INFO(0, host)
ZEND_ARG_INFO(0, community)
ZEND_ARG_INFO(0, object_id)
ZEND_ARG_INFO(0, timeout)
ZEND_ARG_INFO(0, retries)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp2_walk, 0, 0, 3)
ZEND_ARG_INFO(0, host)
ZEND_ARG_INFO(0, community)
ZEND_ARG_INFO(0, object_id)
ZEND_ARG_INFO(0, timeout)
ZEND_ARG_INFO(0, retries)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp2_real_walk, 0, 0, 3)
ZEND_ARG_INFO(0, host)
ZEND_ARG_INFO(0, community)
ZEND_ARG_INFO(0, object_id)
ZEND_ARG_INFO(0, timeout)
ZEND_ARG_INFO(0, retries)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp2_set, 0, 0, 5)
ZEND_ARG_INFO(0, host)
ZEND_ARG_INFO(0, community)
ZEND_ARG_INFO(0, object_id)
ZEND_ARG_INFO(0, type)
ZEND_ARG_INFO(0, value)
ZEND_ARG_INFO(0, timeout)
ZEND_ARG_INFO(0, retries)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp3_get, 0, 0, 8)
ZEND_ARG_INFO(0, host)
ZEND_ARG_INFO(0, sec_name)
ZEND_ARG_INFO(0, sec_level)
ZEND_ARG_INFO(0, auth_protocol)
ZEND_ARG_INFO(0, auth_passphrase)
ZEND_ARG_INFO(0, priv_protocol)
ZEND_ARG_INFO(0, priv_passphrase)
ZEND_ARG_INFO(0, object_id)
ZEND_ARG_INFO(0, timeout)
ZEND_ARG_INFO(0, retries)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp3_getnext, 0, 0, 8)
ZEND_ARG_INFO(0, host)
ZEND_ARG_INFO(0, sec_name)
ZEND_ARG_INFO(0, sec_level)
ZEND_ARG_INFO(0, auth_protocol)
ZEND_ARG_INFO(0, auth_passphrase)
ZEND_ARG_INFO(0, priv_protocol)
ZEND_ARG_INFO(0, priv_passphrase)
ZEND_ARG_INFO(0, object_id)
ZEND_ARG_INFO(0, timeout)
ZEND_ARG_INFO(0, retries)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp3_walk, 0, 0, 8)
ZEND_ARG_INFO(0, host)
ZEND_ARG_INFO(0, sec_name)
ZEND_ARG_INFO(0, sec_level)
ZEND_ARG_INFO(0, auth_protocol)
ZEND_ARG_INFO(0, auth_passphrase)
ZEND_ARG_INFO(0, priv_protocol)
ZEND_ARG_INFO(0, priv_passphrase)
ZEND_ARG_INFO(0, object_id)
ZEND_ARG_INFO(0, timeout)
ZEND_ARG_INFO(0, retries)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp3_real_walk, 0, 0, 8)
ZEND_ARG_INFO(0, host)
ZEND_ARG_INFO(0, sec_name)
ZEND_ARG_INFO(0, sec_level)
ZEND_ARG_INFO(0, auth_protocol)
ZEND_ARG_INFO(0, auth_passphrase)
ZEND_ARG_INFO(0, priv_protocol)
ZEND_ARG_INFO(0, priv_passphrase)
ZEND_ARG_INFO(0, object_id)
ZEND_ARG_INFO(0, timeout)
ZEND_ARG_INFO(0, retries)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp3_set, 0, 0, 10)
ZEND_ARG_INFO(0, host)
ZEND_ARG_INFO(0, sec_name)
ZEND_ARG_INFO(0, sec_level)
ZEND_ARG_INFO(0, auth_protocol)
ZEND_ARG_INFO(0, auth_passphrase)
ZEND_ARG_INFO(0, priv_protocol)
ZEND_ARG_INFO(0, priv_passphrase)
ZEND_ARG_INFO(0, object_id)
ZEND_ARG_INFO(0, type)
ZEND_ARG_INFO(0, value)
ZEND_ARG_INFO(0, timeout)
ZEND_ARG_INFO(0, retries)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp_set_valueretrieval, 0, 0, 1)
ZEND_ARG_INFO(0, method)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(arginfo_snmp_get_valueretrieval, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp_read_mib, 0, 0, 1)
ZEND_ARG_INFO(0, filename)
ZEND_END_ARG_INFO()
/* OO arginfo */
ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp_create, 0, 0, 3)
ZEND_ARG_INFO(0, version)
ZEND_ARG_INFO(0, host)
ZEND_ARG_INFO(0, community)
ZEND_ARG_INFO(0, timeout)
ZEND_ARG_INFO(0, retries)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(arginfo_snmp_void, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp_setSecurity, 0, 0, 8)
ZEND_ARG_INFO(0, sec_level)
ZEND_ARG_INFO(0, auth_protocol)
ZEND_ARG_INFO(0, auth_passphrase)
ZEND_ARG_INFO(0, priv_protocol)
ZEND_ARG_INFO(0, priv_passphrase)
ZEND_ARG_INFO(0, contextName)
ZEND_ARG_INFO(0, contextEngineID)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp_get, 0, 0, 1)
ZEND_ARG_INFO(0, object_id)
ZEND_ARG_INFO(0, use_orignames)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp_walk, 0, 0, 4)
ZEND_ARG_INFO(0, object_id)
ZEND_ARG_INFO(0, suffix_keys)
ZEND_ARG_INFO(0, max_repetitions)
ZEND_ARG_INFO(0, non_repeaters)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp_set, 0, 0, 3)
ZEND_ARG_INFO(0, object_id)
ZEND_ARG_INFO(0, type)
ZEND_ARG_INFO(0, value)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp_class_set_quick_print, 0, 0, 1)
ZEND_ARG_INFO(0, quick_print)
ZEND_END_ARG_INFO()
/* }}} */
struct objid_query {
int count;
int offset;
int step;
zend_long non_repeaters;
zend_long max_repetitions;
int valueretrieval;
int array_output;
int oid_increasing_check;
snmpobjarg *vars;
};
/* {{{ snmp_functions[]
*/
const zend_function_entry snmp_functions[] = {
PHP_FE(snmpget, arginfo_snmpget)
PHP_FE(snmpgetnext, arginfo_snmpgetnext)
PHP_FE(snmpwalk, arginfo_snmpwalk)
PHP_FE(snmprealwalk, arginfo_snmprealwalk)
PHP_FALIAS(snmpwalkoid, snmprealwalk, arginfo_snmprealwalk)
PHP_FE(snmpset, arginfo_snmpset)
PHP_FE(snmp_get_quick_print, arginfo_snmp_get_quick_print)
PHP_FE(snmp_set_quick_print, arginfo_snmp_set_quick_print)
PHP_FE(snmp_set_enum_print, arginfo_snmp_set_enum_print)
PHP_FE(snmp_set_oid_output_format, arginfo_snmp_set_oid_output_format)
PHP_FALIAS(snmp_set_oid_numeric_print, snmp_set_oid_output_format, arginfo_snmp_set_oid_output_format)
PHP_FE(snmp2_get, arginfo_snmp2_get)
PHP_FE(snmp2_getnext, arginfo_snmp2_getnext)
PHP_FE(snmp2_walk, arginfo_snmp2_walk)
PHP_FE(snmp2_real_walk, arginfo_snmp2_real_walk)
PHP_FE(snmp2_set, arginfo_snmp2_set)
PHP_FE(snmp3_get, arginfo_snmp3_get)
PHP_FE(snmp3_getnext, arginfo_snmp3_getnext)
PHP_FE(snmp3_walk, arginfo_snmp3_walk)
PHP_FE(snmp3_real_walk, arginfo_snmp3_real_walk)
PHP_FE(snmp3_set, arginfo_snmp3_set)
PHP_FE(snmp_set_valueretrieval, arginfo_snmp_set_valueretrieval)
PHP_FE(snmp_get_valueretrieval, arginfo_snmp_get_valueretrieval)
PHP_FE(snmp_read_mib, arginfo_snmp_read_mib)
PHP_FE_END
};
/* }}} */
/* query an agent with GET method */
#define SNMP_CMD_GET (1<<0)
/* query an agent with GETNEXT method */
#define SNMP_CMD_GETNEXT (1<<1)
/* query an agent with SET method */
#define SNMP_CMD_SET (1<<2)
/* walk the mib */
#define SNMP_CMD_WALK (1<<3)
/* force values-only output */
#define SNMP_NUMERIC_KEYS (1<<7)
/* use user-supplied OID names for keys in array output mode in GET method */
#define SNMP_ORIGINAL_NAMES_AS_KEYS (1<<8)
/* use OID suffix (`index') for keys in array output mode in WALK method */
#define SNMP_USE_SUFFIX_AS_KEYS (1<<9)
#ifdef COMPILE_DL_SNMP
ZEND_GET_MODULE(snmp)
#endif
/* THREAD_LS snmp_module php_snmp_module; - may need one of these at some point */
/* {{{ PHP_GINIT_FUNCTION
*/
static PHP_GINIT_FUNCTION(snmp)
{
snmp_globals->valueretrieval = SNMP_VALUE_LIBRARY;
}
/* }}} */
#define PHP_SNMP_SESSION_FREE(a) { \
if ((*session)->a) { \
efree((*session)->a); \
(*session)->a = NULL; \
} \
}
static void netsnmp_session_free(php_snmp_session **session) /* {{{ */
{
if (*session) {
PHP_SNMP_SESSION_FREE(peername);
PHP_SNMP_SESSION_FREE(community);
PHP_SNMP_SESSION_FREE(securityName);
PHP_SNMP_SESSION_FREE(contextEngineID);
efree(*session);
*session = NULL;
}
}
/* }}} */
static void php_snmp_session_destructor(zend_resource *rsrc) /* {{{ */
{
php_snmp_session *session = (php_snmp_session *)rsrc->ptr;
netsnmp_session_free(&session);
}
/* }}} */
static void php_snmp_object_free_storage(zend_object *object) /* {{{ */
{
php_snmp_object *intern = php_snmp_fetch_object(object);
if (!intern) {
return;
}
netsnmp_session_free(&(intern->session));
zend_object_std_dtor(&intern->zo);
}
/* }}} */
static zend_object *php_snmp_object_new(zend_class_entry *class_type) /* {{{ */
{
php_snmp_object *intern;
/* Allocate memory for it */
intern = ecalloc(1, sizeof(php_snmp_object) + zend_object_properties_size(class_type));
zend_object_std_init(&intern->zo, class_type);
object_properties_init(&intern->zo, class_type);
intern->zo.handlers = &php_snmp_object_handlers;
return &intern->zo;
}
/* }}} */
/* {{{ php_snmp_error
*
* Record last SNMP-related error in object
*
*/
static void php_snmp_error(zval *object, const char *docref, int type, const char *format, ...)
{
va_list args;
php_snmp_object *snmp_object = NULL;
if (object) {
snmp_object = Z_SNMP_P(object);
if (type == PHP_SNMP_ERRNO_NOERROR) {
memset(snmp_object->snmp_errstr, 0, sizeof(snmp_object->snmp_errstr));
} else {
va_start(args, format);
vsnprintf(snmp_object->snmp_errstr, sizeof(snmp_object->snmp_errstr) - 1, format, args);
va_end(args);
}
snmp_object->snmp_errno = type;
}
if (type == PHP_SNMP_ERRNO_NOERROR) {
return;
}
if (object && (snmp_object->exceptions_enabled & type)) {
zend_throw_exception_ex(php_snmp_exception_ce, type, "%s", snmp_object->snmp_errstr);
} else {
va_start(args, format);
php_verror(docref, "", E_WARNING, format, args);
va_end(args);
}
}
/* }}} */
/* {{{ php_snmp_getvalue
*
* SNMP value to zval converter
*
*/
static void php_snmp_getvalue(struct variable_list *vars, zval *snmpval, int valueretrieval)
{
zval val;
char sbuf[512];
char *buf = &(sbuf[0]);
char *dbuf = (char *)NULL;
int buflen = sizeof(sbuf) - 1;
int val_len = vars->val_len;
/* use emalloc() for large values, use static array otherwize */
/* There is no way to know the size of buffer snprint_value() needs in order to print a value there.
* So we are forced to probe it
*/
while ((valueretrieval & SNMP_VALUE_PLAIN) == 0) {
*buf = '\0';
if (snprint_value(buf, buflen, vars->name, vars->name_length, vars) == -1) {
if (val_len > 512*1024) {
php_error_docref(NULL, E_WARNING, "snprint_value() asks for a buffer more than 512k, Net-SNMP bug?");
break;
}
/* buffer is not long enough to hold full output, double it */
val_len *= 2;
} else {
break;
}
if (buf == dbuf) {
dbuf = (char *)erealloc(dbuf, val_len + 1);
} else {
dbuf = (char *)emalloc(val_len + 1);
}
buf = dbuf;
buflen = val_len;
}
if((valueretrieval & SNMP_VALUE_PLAIN) && val_len > buflen){
dbuf = (char *)emalloc(val_len + 1);
buf = dbuf;
buflen = val_len;
}
if (valueretrieval & SNMP_VALUE_PLAIN) {
*buf = 0;
switch (vars->type) {
case ASN_BIT_STR: /* 0x03, asn1.h */
ZVAL_STRINGL(&val, (char *)vars->val.bitstring, vars->val_len);
break;
case ASN_OCTET_STR: /* 0x04, asn1.h */
case ASN_OPAQUE: /* 0x44, snmp_impl.h */
ZVAL_STRINGL(&val, (char *)vars->val.string, vars->val_len);
break;
case ASN_NULL: /* 0x05, asn1.h */
ZVAL_NULL(&val);
break;
case ASN_OBJECT_ID: /* 0x06, asn1.h */
snprint_objid(buf, buflen, vars->val.objid, vars->val_len / sizeof(oid));
ZVAL_STRING(&val, buf);
break;
case ASN_IPADDRESS: /* 0x40, snmp_impl.h */
snprintf(buf, buflen, "%d.%d.%d.%d",
(vars->val.string)[0], (vars->val.string)[1],
(vars->val.string)[2], (vars->val.string)[3]);
buf[buflen]=0;
ZVAL_STRING(&val, buf);
break;
case ASN_COUNTER: /* 0x41, snmp_impl.h */
case ASN_GAUGE: /* 0x42, snmp_impl.h */
/* ASN_UNSIGNED is the same as ASN_GAUGE */
case ASN_TIMETICKS: /* 0x43, snmp_impl.h */
case ASN_UINTEGER: /* 0x47, snmp_impl.h */
snprintf(buf, buflen, "%lu", *vars->val.integer);
buf[buflen]=0;
ZVAL_STRING(&val, buf);
break;
case ASN_INTEGER: /* 0x02, asn1.h */
snprintf(buf, buflen, "%ld", *vars->val.integer);
buf[buflen]=0;
ZVAL_STRING(&val, buf);
break;
#if defined(NETSNMP_WITH_OPAQUE_SPECIAL_TYPES) || defined(OPAQUE_SPECIAL_TYPES)
case ASN_OPAQUE_FLOAT: /* 0x78, asn1.h */
snprintf(buf, buflen, "%f", *vars->val.floatVal);
ZVAL_STRING(&val, buf);
break;
case ASN_OPAQUE_DOUBLE: /* 0x79, asn1.h */
snprintf(buf, buflen, "%Lf", *vars->val.doubleVal);
ZVAL_STRING(&val, buf);
break;
case ASN_OPAQUE_I64: /* 0x80, asn1.h */
printI64(buf, vars->val.counter64);
ZVAL_STRING(&val, buf);
break;
case ASN_OPAQUE_U64: /* 0x81, asn1.h */
#endif
case ASN_COUNTER64: /* 0x46, snmp_impl.h */
printU64(buf, vars->val.counter64);
ZVAL_STRING(&val, buf);
break;
default:
ZVAL_STRING(&val, "Unknown value type");
php_error_docref(NULL, E_WARNING, "Unknown value type: %u", vars->type);
break;
}
} else /* use Net-SNMP value translation */ {
/* we have desired string in buffer, just use it */
ZVAL_STRING(&val, buf);
}
if (valueretrieval & SNMP_VALUE_OBJECT) {
object_init(snmpval);
add_property_long(snmpval, "type", vars->type);
add_property_zval(snmpval, "value", &val);
} else {
ZVAL_COPY(snmpval, &val);
}
zval_ptr_dtor(&val);
if (dbuf){ /* malloc was used to store value */
efree(dbuf);
}
}
/* }}} */
/* {{{ php_snmp_internal
*
* SNMP object fetcher/setter for all SNMP versions
*
*/
static void php_snmp_internal(INTERNAL_FUNCTION_PARAMETERS, int st,
struct snmp_session *session,
struct objid_query *objid_query)
{
struct snmp_session *ss;
struct snmp_pdu *pdu=NULL, *response;
struct variable_list *vars;
oid root[MAX_NAME_LEN];
size_t rootlen = 0;
int status, count, found;
char buf[2048];
char buf2[2048];
int keepwalking=1;
char *err;
zval snmpval;
int snmp_errno;
/* we start with retval=FALSE. If any actual data is acquired, retval will be set to appropriate type */
RETVAL_FALSE;
/* reset errno and errstr */
php_snmp_error(getThis(), NULL, PHP_SNMP_ERRNO_NOERROR, "");
if (st & SNMP_CMD_WALK) { /* remember root OID */
memmove((char *)root, (char *)(objid_query->vars[0].name), (objid_query->vars[0].name_length) * sizeof(oid));
rootlen = objid_query->vars[0].name_length;
objid_query->offset = objid_query->count;
}
if ((ss = snmp_open(session)) == NULL) {
snmp_error(session, NULL, NULL, &err);
php_error_docref(NULL, E_WARNING, "Could not open snmp connection: %s", err);
free(err);
RETVAL_FALSE;
return;
}
if ((st & SNMP_CMD_SET) && objid_query->count > objid_query->step) {
php_snmp_error(getThis(), NULL, PHP_SNMP_ERRNO_MULTIPLE_SET_QUERIES, "Can not fit all OIDs for SET query into one packet, using multiple queries");
}
while (keepwalking) {
keepwalking = 0;
if (st & SNMP_CMD_WALK) {
if (session->version == SNMP_VERSION_1) {
pdu = snmp_pdu_create(SNMP_MSG_GETNEXT);
} else {
pdu = snmp_pdu_create(SNMP_MSG_GETBULK);
pdu->non_repeaters = objid_query->non_repeaters;
pdu->max_repetitions = objid_query->max_repetitions;
}
snmp_add_null_var(pdu, objid_query->vars[0].name, objid_query->vars[0].name_length);
} else {
if (st & SNMP_CMD_GET) {
pdu = snmp_pdu_create(SNMP_MSG_GET);
} else if (st & SNMP_CMD_GETNEXT) {
pdu = snmp_pdu_create(SNMP_MSG_GETNEXT);
} else if (st & SNMP_CMD_SET) {
pdu = snmp_pdu_create(SNMP_MSG_SET);
} else {
snmp_close(ss);
php_error_docref(NULL, E_ERROR, "Unknown SNMP command (internals)");
RETVAL_FALSE;
return;
}
for (count = 0; objid_query->offset < objid_query->count && count < objid_query->step; objid_query->offset++, count++){
if (st & SNMP_CMD_SET) {
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))) {
snprint_objid(buf, sizeof(buf), objid_query->vars[objid_query->offset].name, objid_query->vars[objid_query->offset].name_length);
php_snmp_error(getThis(), NULL, PHP_SNMP_ERRNO_OID_PARSING_ERROR, "Could not add variable: OID='%s' type='%c' value='%s': %s", buf, objid_query->vars[objid_query->offset].type, objid_query->vars[objid_query->offset].value, snmp_api_errstring(snmp_errno));
snmp_free_pdu(pdu);
snmp_close(ss);
RETVAL_FALSE;
return;
}
} else {
snmp_add_null_var(pdu, objid_query->vars[objid_query->offset].name, objid_query->vars[objid_query->offset].name_length);
}
}
if(pdu->variables == NULL){
snmp_free_pdu(pdu);
snmp_close(ss);
RETVAL_FALSE;
return;
}
}
retry:
status = snmp_synch_response(ss, pdu, &response);
if (status == STAT_SUCCESS) {
if (response->errstat == SNMP_ERR_NOERROR) {
if (st & SNMP_CMD_SET) {
if (objid_query->offset < objid_query->count) { /* we have unprocessed OIDs */
keepwalking = 1;
continue;
}
snmp_free_pdu(response);
snmp_close(ss);
RETVAL_TRUE;
return;
}
for (vars = response->variables; vars; vars = vars->next_variable) {
/* do not output errors as values */
if ( vars->type == SNMP_ENDOFMIBVIEW ||
vars->type == SNMP_NOSUCHOBJECT ||
vars->type == SNMP_NOSUCHINSTANCE ) {
if ((st & SNMP_CMD_WALK) && Z_TYPE_P(return_value) == IS_ARRAY) {
break;
}
snprint_objid(buf, sizeof(buf), vars->name, vars->name_length);
snprint_value(buf2, sizeof(buf2), vars->name, vars->name_length, vars);
php_snmp_error(getThis(), NULL, PHP_SNMP_ERRNO_ERROR_IN_REPLY, "Error in packet at '%s': %s", buf, buf2);
continue;
}
if ((st & SNMP_CMD_WALK) &&
(vars->name_length < rootlen || memcmp(root, vars->name, rootlen * sizeof(oid)))) { /* not part of this subtree */
if (Z_TYPE_P(return_value) == IS_ARRAY) { /* some records are fetched already, shut down further lookup */
keepwalking = 0;
} else {
/* first fetched OID is out of subtree, fallback to GET query */
st |= SNMP_CMD_GET;
st ^= SNMP_CMD_WALK;
objid_query->offset = 0;
keepwalking = 1;
}
break;
}
ZVAL_NULL(&snmpval);
php_snmp_getvalue(vars, &snmpval, objid_query->valueretrieval);
if (objid_query->array_output) {
if (Z_TYPE_P(return_value) == IS_TRUE || Z_TYPE_P(return_value) == IS_FALSE) {
array_init(return_value);
}
if (st & SNMP_NUMERIC_KEYS) {
add_next_index_zval(return_value, &snmpval);
} else if (st & SNMP_ORIGINAL_NAMES_AS_KEYS && st & SNMP_CMD_GET) {
found = 0;
for (count = 0; count < objid_query->count; count++) {
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) {
found = 1;
objid_query->vars[count].name_length = 0; /* mark this name as used */
break;
}
}
if (found) {
add_assoc_zval(return_value, objid_query->vars[count].oid, &snmpval);
} else {
snprint_objid(buf2, sizeof(buf2), vars->name, vars->name_length);
php_error_docref(NULL, E_WARNING, "Could not find original OID name for '%s'", buf2);
}
} else if (st & SNMP_USE_SUFFIX_AS_KEYS && st & SNMP_CMD_WALK) {
snprint_objid(buf2, sizeof(buf2), vars->name, vars->name_length);
if (rootlen <= vars->name_length && snmp_oid_compare(root, rootlen, vars->name, rootlen) == 0) {
buf2[0] = '\0';
count = rootlen;
while(count < vars->name_length){
sprintf(buf, "%lu.", vars->name[count]);
strcat(buf2, buf);
count++;
}
buf2[strlen(buf2) - 1] = '\0'; /* remove trailing '.' */
}
add_assoc_zval(return_value, buf2, &snmpval);
} else {
snprint_objid(buf2, sizeof(buf2), vars->name, vars->name_length);
add_assoc_zval(return_value, buf2, &snmpval);
}
} else {
ZVAL_COPY_VALUE(return_value, &snmpval);
break;
}
/* OID increase check */
if (st & SNMP_CMD_WALK) {
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) {
snprint_objid(buf2, sizeof(buf2), vars->name, vars->name_length);
php_snmp_error(getThis(), NULL, PHP_SNMP_ERRNO_OID_NOT_INCREASING, "Error: OID not increasing: %s", buf2);
keepwalking = 0;
} else {
memmove((char *)(objid_query->vars[0].name), (char *)vars->name, vars->name_length * sizeof(oid));
objid_query->vars[0].name_length = vars->name_length;
keepwalking = 1;
}
}
}
if (objid_query->offset < objid_query->count) { /* we have unprocessed OIDs */
keepwalking = 1;
}
} else {
if (st & SNMP_CMD_WALK && response->errstat == SNMP_ERR_TOOBIG && objid_query->max_repetitions > 1) { /* Answer will not fit into single packet */
objid_query->max_repetitions /= 2;
snmp_free_pdu(response);
keepwalking = 1;
continue;
}
if (!(st & SNMP_CMD_WALK) || response->errstat != SNMP_ERR_NOSUCHNAME || Z_TYPE_P(return_value) == IS_TRUE || Z_TYPE_P(return_value) == IS_FALSE) {
for (count=1, vars = response->variables;
vars && count != response->errindex;
vars = vars->next_variable, count++);
if (st & (SNMP_CMD_GET | SNMP_CMD_GETNEXT) && response->errstat == SNMP_ERR_TOOBIG && objid_query->step > 1) { /* Answer will not fit into single packet */
objid_query->offset = ((objid_query->offset > objid_query->step) ? (objid_query->offset - objid_query->step) : 0 );
objid_query->step /= 2;
snmp_free_pdu(response);
keepwalking = 1;
continue;
}
if (vars) {
snprint_objid(buf, sizeof(buf), vars->name, vars->name_length);
php_snmp_error(getThis(), NULL, PHP_SNMP_ERRNO_ERROR_IN_REPLY, "Error in packet at '%s': %s", buf, snmp_errstring(response->errstat));
} else {
php_snmp_error(getThis(), NULL, PHP_SNMP_ERRNO_ERROR_IN_REPLY, "Error in packet at %u object_id: %s", response->errindex, snmp_errstring(response->errstat));
}
if (st & (SNMP_CMD_GET | SNMP_CMD_GETNEXT)) { /* cut out bogus OID and retry */
if ((pdu = snmp_fix_pdu(response, ((st & SNMP_CMD_GET) ? SNMP_MSG_GET : SNMP_MSG_GETNEXT) )) != NULL) {
snmp_free_pdu(response);
goto retry;
}
}
snmp_free_pdu(response);
snmp_close(ss);
if (objid_query->array_output) {
zval_ptr_dtor(return_value);
}
RETVAL_FALSE;
return;
}
}
} else if (status == STAT_TIMEOUT) {
php_snmp_error(getThis(), NULL, PHP_SNMP_ERRNO_TIMEOUT, "No response from %s", session->peername);
if (objid_query->array_output) {
zval_ptr_dtor(return_value);
}
snmp_close(ss);
RETVAL_FALSE;
return;
} else { /* status == STAT_ERROR */
snmp_error(ss, NULL, NULL, &err);
php_snmp_error(getThis(), NULL, PHP_SNMP_ERRNO_GENERIC, "Fatal error: %s", err);
free(err);
if (objid_query->array_output) {
zval_ptr_dtor(return_value);
}
snmp_close(ss);
RETVAL_FALSE;
return;
}
if (response) {
snmp_free_pdu(response);
}
} /* keepwalking */
snmp_close(ss);
}
/* }}} */
/* {{{ php_snmp_parse_oid
*
* OID parser (and type, value for SNMP_SET command)
*/
static int php_snmp_parse_oid(zval *object, int st, struct objid_query *objid_query, zval *oid, zval *type, zval *value)
{
char *pptr;
uint32_t idx_type = 0, idx_value = 0;
zval *tmp_oid, *tmp_type, *tmp_value;
if (Z_TYPE_P(oid) != IS_ARRAY) {
convert_to_string_ex(oid);
}
if (st & SNMP_CMD_SET) {
if (Z_TYPE_P(type) != IS_ARRAY) {
convert_to_string_ex(type);
}
if (Z_TYPE_P(value) != IS_ARRAY) {
convert_to_string_ex(value);
}
}
objid_query->count = 0;
objid_query->array_output = ((st & SNMP_CMD_WALK) ? TRUE : FALSE);
if (Z_TYPE_P(oid) == IS_STRING) {
objid_query->vars = (snmpobjarg *)emalloc(sizeof(snmpobjarg));
objid_query->vars[objid_query->count].oid = Z_STRVAL_P(oid);
if (st & SNMP_CMD_SET) {
if (Z_TYPE_P(type) == IS_STRING && Z_TYPE_P(value) == IS_STRING) {
if (Z_STRLEN_P(type) != 1) {
php_error_docref(NULL, E_WARNING, "Bogus type '%s', should be single char, got %u", Z_STRVAL_P(type), Z_STRLEN_P(type));
efree(objid_query->vars);
return FALSE;
}
pptr = Z_STRVAL_P(type);
objid_query->vars[objid_query->count].type = *pptr;
objid_query->vars[objid_query->count].value = Z_STRVAL_P(value);
} else {
php_error_docref(NULL, E_WARNING, "Single objid and multiple type or values are not supported");
efree(objid_query->vars);
return FALSE;
}
}
objid_query->count++;
} else if (Z_TYPE_P(oid) == IS_ARRAY) { /* we got objid array */
if (zend_hash_num_elements(Z_ARRVAL_P(oid)) == 0) {
php_error_docref(NULL, E_WARNING, "Got empty OID array");
return FALSE;
}
objid_query->vars = (snmpobjarg *)safe_emalloc(sizeof(snmpobjarg), zend_hash_num_elements(Z_ARRVAL_P(oid)), 0);
objid_query->array_output = ( (st & SNMP_CMD_SET) ? FALSE : TRUE );
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(oid), tmp_oid) {
convert_to_string_ex(tmp_oid);
objid_query->vars[objid_query->count].oid = Z_STRVAL_P(tmp_oid);
if (st & SNMP_CMD_SET) {
if (Z_TYPE_P(type) == IS_STRING) {
pptr = Z_STRVAL_P(type);
objid_query->vars[objid_query->count].type = *pptr;
} else if (Z_TYPE_P(type) == IS_ARRAY) {
while (idx_type < Z_ARRVAL_P(type)->nNumUsed) {
tmp_type = &Z_ARRVAL_P(type)->arData[idx_type].val;
if (Z_TYPE_P(tmp_type) != IS_UNDEF) {