-
Notifications
You must be signed in to change notification settings - Fork 7
/
nm_snmp.c
3113 lines (2819 loc) · 90.8 KB
/
nm_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
/*-----------------------------------------------------------------------------
* nm_snmp.c
*
* This module implements the interface between Lua and the netsnmp library.
* They provide the Lua interface to the
*----------------------------------------------------------------------------*/
#ifdef WIN32
#include <winsock2.h>
#else
#include <sys/types.h>
#include <sys/param.h>
#include <netdb.h>
#endif
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include "lua.h"
#include "lauxlib.h"
#include "nm_snmp.h"
#include "nm_mib.h"
#include "except.h"
#include "nm_c64.h"
#define MYNAME "snmp"
#define MYVERSION VERSION
#define MYSYSTEM SYSTEM
#define USM_OID_LEN 12
#define DH_USM_OID_LEN 11
/*-----------------------------------------------------------------------------
* Local variables
*----------------------------------------------------------------------------*/
/*
* Path pointing to helper programs, e.g. straps
*/
#ifndef USE_SNMPTRAPD
static char nm_snmp_straps[256];
static int nm_snmp_straps_port = SNMP_TRAP_PORT;
static int nm_snmp_init_done = 0;
#else
static int nm_snmp_trap_port = NM_SNMP_TRAP_PORT;
#endif
/*
* Peer default (unspecified)
*/
static char peer_def[] = "0.0.0.0";
/*
* PDU pointer for reception of synchronous responses
*/
struct snmp_pdu *synch_response;
/*
* Pointer to the "mother" of all sessions
*/
static CmuSession nm_cmu_session;
/*
* List of active sessions
*/
static Tsession *nm_snmp_sessions = NULL;
/*
* Reference counters for pending requests
*/
static int nm_snmp_sync_reqs = 0;
static int nm_snmp_async_reqs = 0;
/*
* Flag indicating whether a user callback is executed
*/
static int nm_in_usr_cback = 0;
static CloseList *nm_close_list = NULL;
/*
* Trap reference count
*/
static int nm_snmp_ntraps = 0;
#ifndef USE_SNMPTRAPD
static u_char nm_snmp_trappkt[PACKET_LENGTH];
#endif
/*
* A global ref for the active Lua State
*/
static lua_State *lua_ref;
/*
* USM authentication and privacy stuff
*/
#ifdef REMOVE_THIS
static oid authKeyOid[MAX_OID_LEN] = { 1, 3, 6, 1, 6, 3, 15, 1, 2, 2, 1, 6 },
privKeyOid[MAX_OID_LEN] = {1, 3, 6, 1, 6, 3, 15, 1, 2, 2, 1, 9};
#endif
#ifdef REMOVE_THIS
ownAuthKeyOid[MAX_OID_LEN] = {1, 3, 6, 1, 6, 3, 15, 1, 2, 2, 1, 7},
ownPrivKeyOid[MAX_OID_LEN] = {1, 3, 6, 1, 6, 3, 15, 1, 2, 2, 1, 10},
usmUserCloneFrom[MAX_OID_LEN] = {1, 3, 6, 1, 6, 3, 15, 1, 2, 2, 1, 4},
usmUserSecurityName[MAX_OID_LEN] = {1, 3, 6, 1, 6, 3, 15, 1, 2, 2, 1, 3},
usmUserStatus[MAX_OID_LEN] = {1, 3, 6, 1, 6, 3, 15, 1, 2, 2, 1, 13},
/* diffie helman change key objects */
usmDHUserAuthKeyChange[MAX_OID_LEN] = {1, 3, 6, 1, 3, 101, 1, 1, 2, 1, 1 },
usmDHUserOwnAuthKeyChange[MAX_OID_LEN] = {1, 3, 6, 1, 3, 101, 1, 1, 2, 1, 2 },
usmDHUserPrivKeyChange[MAX_OID_LEN] = {1, 3, 6, 1, 3, 101, 1, 1, 2, 1, 3 },
usmDHUserOwnPrivKeyChange[MAX_OID_LEN] = {1, 3, 6, 1, 3, 101, 1, 1, 2, 1, 4 },
usmDHParameters[] = { 1,3,6,1,3,101,1,1,1,0 };
#endif
#ifdef REMOVE_THIS
static oid *authKeyChange = authKeyOid, *privKeyChange = privKeyOid;
#endif
size_t usmUserEngineIDLen = 0;
u_char *usmUserEngineID = NULL;
/*-----------------------------------------------------------------------------
* Defines
*----------------------------------------------------------------------------*/
#define NM_SNMP_PRINT_VALUE 0
#define NM_SNMP_PRINT_VARIABLE 1
#define MAX_PACKET_LENGTH PACKET_LENGTH
/*-----------------------------------------------------------------------------
* Functions copied from net-snmp library.
* - We need them in order to process traps.
*----------------------------------------------------------------------------*/
#ifndef USE_SNMPTRAPD
static const char *secLevelName[] = {
"BAD_SEC_LEVEL",
"noAuthNoPriv",
"authNoPriv",
"authPriv"
};
/*
* FROM NET-SNMP:
* Parses the packet received to determine version, either directly
* from packets version field or inferred from ASN.1 construct.
*/
static int
snmp_parse_version(u_char * data, size_t length)
{
u_char type;
long version = SNMPERR_BAD_VERSION;
data = asn_parse_sequence(data, &length, &type,
(ASN_SEQUENCE | ASN_CONSTRUCTOR), "version");
if (data) {
data =
asn_parse_int(data, &length, &type, &version, sizeof(version));
if (!data || type != ASN_INTEGER) {
return SNMPERR_BAD_VERSION;
}
}
return version;
}
#define DEBUGPRINTPDUTYPE(token, type) printf("### %s %d\n", token, type)
/*
* FROM NET-SNMP:
* Parses the packet received on the input session, and places the data into
* the input pdu. length is the length of the input packet.
* If any errors are encountered, -1 or USM error is returned.
* Otherwise, a 0 is returned.
*/
static int
_snmp_parse(void *sessp,
netsnmp_session * session,
netsnmp_pdu *pdu, u_char * data, size_t length)
{
u_char community[COMMUNITY_MAX_LEN];
size_t community_length = COMMUNITY_MAX_LEN;
int result = -1;
session->s_snmp_errno = 0;
session->s_errno = 0;
/*
* Ensure all incoming PDUs have a unique means of identification
* (This is not restricted to AgentX handling,
* though that is where the need becomes visible)
*/
pdu->transid = snmp_get_next_transid();
if (session->version != SNMP_DEFAULT_VERSION) {
pdu->version = session->version;
} else {
pdu->version = snmp_parse_version(data, length);
}
switch (pdu->version) {
case SNMP_VERSION_1:
case SNMP_VERSION_2c:
DEBUGMSGTL(("snmp_api", "Parsing SNMPv%ld message...\n",
(1 + pdu->version)));
/*
* authenticates message and returns length if valid
*/
if (pdu->version == SNMP_VERSION_1) {
DEBUGDUMPSECTION("recv", "SNMPv1 message\n");
} else {
DEBUGDUMPSECTION("recv", "SNMPv2c message\n");
}
data = snmp_comstr_parse(data, &length,
community, &community_length,
&pdu->version);
if (data == NULL)
return -1;
if (pdu->version != session->version &&
session->version != SNMP_DEFAULT_VERSION) {
session->s_snmp_errno = SNMPERR_BAD_VERSION;
return -1;
}
/*
* maybe get the community string.
*/
pdu->securityLevel = SNMP_SEC_LEVEL_NOAUTH;
pdu->securityModel = (pdu->version == SNMP_VERSION_1) ?
SNMP_SEC_MODEL_SNMPv1 : SNMP_SEC_MODEL_SNMPv2c;
SNMP_FREE(pdu->community);
pdu->community_len = 0;
pdu->community = (u_char *) 0;
if (community_length) {
pdu->community_len = community_length;
pdu->community = (u_char *) malloc(community_length);
if (pdu->community == NULL) {
session->s_snmp_errno = SNMPERR_MALLOC;
return -1;
}
memmove(pdu->community, community, community_length);
}
if (session->authenticator) {
data = session->authenticator(data, &length,
community, community_length);
if (data == NULL) {
session->s_snmp_errno = SNMPERR_AUTHENTICATION_FAILURE;
return -1;
}
}
DEBUGDUMPSECTION("recv", "PDU");
result = snmp_pdu_parse(pdu, data, &length);
if (result < 0) {
/*
* This indicates a parse error.
*/
snmp_increment_statistic(STAT_SNMPINASNPARSEERRS);
}
DEBUGINDENTADD(-6);
break;
case SNMP_VERSION_3:
result = snmpv3_parse(pdu, data, &length, NULL, session);
DEBUGMSGTL(("snmp_parse",
"Parsed SNMPv3 message (secName:%s, secLevel:%s): %s\n",
pdu->securityName, secLevelName[pdu->securityLevel],
snmp_api_errstring(result)));
if (result) {
if (!sessp) {
session->s_snmp_errno = result;
} else {
/*
* handle reportable errors
*/
switch (result) {
case SNMPERR_USM_AUTHENTICATIONFAILURE:
{
int res = session->s_snmp_errno;
session->s_snmp_errno = result;
if (session->callback) {
session->callback(NETSNMP_CALLBACK_OP_RECEIVED_MESSAGE,
session, pdu->reqid, pdu, session->callback_magic);
}
session->s_snmp_errno = res;
}
case SNMPERR_USM_UNKNOWNENGINEID:
case SNMPERR_USM_UNKNOWNSECURITYNAME:
case SNMPERR_USM_UNSUPPORTEDSECURITYLEVEL:
case SNMPERR_USM_NOTINTIMEWINDOW:
case SNMPERR_USM_DECRYPTIONERROR:
if (SNMP_CMD_CONFIRMED(pdu->command) ||
(pdu->command == 0
&& (pdu->flags & SNMP_MSG_FLAG_RPRT_BIT))) {
netsnmp_pdu *pdu2;
int flags = pdu->flags;
pdu->flags |= UCD_MSG_FLAG_FORCE_PDU_COPY;
pdu2 = snmp_clone_pdu(pdu);
pdu->flags = pdu2->flags = flags;
snmpv3_make_report(pdu2, result);
if (0 == snmp_sess_send(sessp, pdu2)) {
snmp_free_pdu(pdu2);
/*
* TODO: indicate error
*/
}
}
break;
default:
session->s_snmp_errno = result;
break;
}
}
}
break;
case SNMPERR_BAD_VERSION:
ERROR_MSG("error parsing snmp message version");
snmp_increment_statistic(STAT_SNMPINASNPARSEERRS);
session->s_snmp_errno = SNMPERR_BAD_VERSION;
break;
case SNMP_VERSION_sec:
case SNMP_VERSION_2u:
case SNMP_VERSION_2star:
case SNMP_VERSION_2p:
default:
ERROR_MSG("unsupported snmp message version");
snmp_increment_statistic(STAT_SNMPINBADVERSIONS);
/*
* need better way to determine OS independent
* INT32_MAX value, for now hardcode
*/
if (pdu->version < 0 || pdu->version > 2147483647) {
snmp_increment_statistic(STAT_SNMPINASNPARSEERRS);
}
session->s_snmp_errno = SNMPERR_BAD_VERSION;
break;
}
return result;
}
static int
snmp_parse(void *sessp,
netsnmp_session * pss,
netsnmp_pdu *pdu, u_char * data, size_t length)
{
int rc;
rc = _snmp_parse(sessp, pss, pdu, data, length);
if (rc) {
if (!pss->s_snmp_errno) {
pss->s_snmp_errno = SNMPERR_BAD_PARSE;
}
SET_SNMP_ERROR(pss->s_snmp_errno);
}
return rc;
}
int
snmp_pdu_parse(netsnmp_pdu *pdu, u_char * data, size_t * length)
{
u_char type;
u_char msg_type;
u_char *var_val;
int badtype = 0;
size_t len;
size_t four;
netsnmp_variable_list *vp = NULL;
oid objid[MAX_OID_LEN];
/*
* Get the PDU type
*/
data = asn_parse_header(data, length, &msg_type);
if (data == NULL)
return -1;
pdu->command = msg_type;
pdu->flags &= (~UCD_MSG_FLAG_RESPONSE_PDU);
/*
* get the fields in the PDU preceeding the variable-bindings sequence
*/
switch (pdu->command) {
case SNMP_MSG_TRAP:
/*
* enterprise
*/
pdu->enterprise_length = MAX_OID_LEN;
data = asn_parse_objid(data, length, &type, objid,
&pdu->enterprise_length);
if (data == NULL)
return -1;
pdu->enterprise =
(oid *) malloc(pdu->enterprise_length * sizeof(oid));
if (pdu->enterprise == NULL) {
return -1;
}
memmove(pdu->enterprise, objid,
pdu->enterprise_length * sizeof(oid));
/*
* agent-addr
*/
four = 4;
data = asn_parse_string(data, length, &type,
(u_char *) pdu->agent_addr, &four);
if (data == NULL)
return -1;
/*
* generic trap
*/
data = asn_parse_int(data, length, &type, (long *) &pdu->trap_type,
sizeof(pdu->trap_type));
if (data == NULL)
return -1;
/*
* specific trap
*/
data =
asn_parse_int(data, length, &type,
(long *) &pdu->specific_type,
sizeof(pdu->specific_type));
if (data == NULL)
return -1;
/*
* timestamp
*/
data = asn_parse_unsigned_int(data, length, &type, &pdu->time,
sizeof(pdu->time));
if (data == NULL)
return -1;
break;
case SNMP_MSG_RESPONSE:
case SNMP_MSG_REPORT:
pdu->flags |= UCD_MSG_FLAG_RESPONSE_PDU;
/*
* fallthrough
*/
case SNMP_MSG_GET:
case SNMP_MSG_GETNEXT:
case SNMP_MSG_GETBULK:
case SNMP_MSG_TRAP2:
case SNMP_MSG_INFORM:
case SNMP_MSG_SET:
/*
* PDU is not an SNMPv1 TRAP
*/
/*
* request id
*/
DEBUGDUMPHEADER("recv", "request_id");
data = asn_parse_int(data, length, &type, &pdu->reqid,
sizeof(pdu->reqid));
DEBUGINDENTLESS();
if (data == NULL) {
return -1;
}
/*
* error status (getbulk non-repeaters)
*/
DEBUGDUMPHEADER("recv", "error status");
data = asn_parse_int(data, length, &type, &pdu->errstat,
sizeof(pdu->errstat));
DEBUGINDENTLESS();
if (data == NULL) {
return -1;
}
/*
* error index (getbulk max-repetitions)
*/
DEBUGDUMPHEADER("recv", "error index");
data = asn_parse_int(data, length, &type, &pdu->errindex,
sizeof(pdu->errindex));
DEBUGINDENTLESS();
if (data == NULL) {
return -1;
}
break;
default:
snmp_log(LOG_ERR, "Bad PDU type received: 0x%.2x\n", pdu->command);
snmp_increment_statistic(STAT_SNMPINASNPARSEERRS);
return -1;
}
/*
* get header for variable-bindings sequence
*/
DEBUGDUMPSECTION("recv", "VarBindList");
data = asn_parse_sequence(data, length, &type,
(ASN_SEQUENCE | ASN_CONSTRUCTOR),
"varbinds");
if (data == NULL)
return -1;
/*
* get each varBind sequence
*/
while ((int) *length > 0) {
netsnmp_variable_list *vptemp;
vptemp = (netsnmp_variable_list *) malloc(sizeof(*vptemp));
if (0 == vptemp) {
return -1;
}
if (0 == vp) {
pdu->variables = vptemp;
} else {
vp->next_variable = vptemp;
}
vp = vptemp;
vp->next_variable = NULL;
vp->val.string = NULL;
vp->name_length = MAX_OID_LEN;
vp->name = 0;
vp->index = 0;
vp->data = 0;
vp->dataFreeHook = 0;
DEBUGDUMPSECTION("recv", "VarBind");
data = snmp_parse_var_op(data, objid, &vp->name_length, &vp->type,
&vp->val_len, &var_val, length);
if (data == NULL)
return -1;
if (snmp_set_var_objid(vp, objid, vp->name_length))
return -1;
len = MAX_PACKET_LENGTH;
DEBUGDUMPHEADER("recv", "Value");
switch ((short) vp->type) {
case ASN_INTEGER:
vp->val.integer = (long *) vp->buf;
vp->val_len = sizeof(long);
asn_parse_int(var_val, &len, &vp->type,
(long *) vp->val.integer,
sizeof(*vp->val.integer));
break;
case ASN_COUNTER:
case ASN_GAUGE:
case ASN_TIMETICKS:
case ASN_UINTEGER:
vp->val.integer = (long *) vp->buf;
vp->val_len = sizeof(u_long);
asn_parse_unsigned_int(var_val, &len, &vp->type,
(u_long *) vp->val.integer,
vp->val_len);
break;
#ifdef OPAQUE_SPECIAL_TYPES
case ASN_OPAQUE_COUNTER64:
case ASN_OPAQUE_U64:
#endif /* OPAQUE_SPECIAL_TYPES */
case ASN_COUNTER64:
vp->val.counter64 = (struct counter64 *) vp->buf;
vp->val_len = sizeof(struct counter64);
asn_parse_unsigned_int64(var_val, &len, &vp->type,
(struct counter64 *) vp->val.
counter64, vp->val_len);
break;
#ifdef OPAQUE_SPECIAL_TYPES
case ASN_OPAQUE_FLOAT:
vp->val.floatVal = (float *) vp->buf;
vp->val_len = sizeof(float);
asn_parse_float(var_val, &len, &vp->type,
vp->val.floatVal, vp->val_len);
break;
case ASN_OPAQUE_DOUBLE:
vp->val.doubleVal = (double *) vp->buf;
vp->val_len = sizeof(double);
asn_parse_double(var_val, &len, &vp->type,
vp->val.doubleVal, vp->val_len);
break;
case ASN_OPAQUE_I64:
vp->val.counter64 = (struct counter64 *) vp->buf;
vp->val_len = sizeof(struct counter64);
asn_parse_signed_int64(var_val, &len, &vp->type,
(struct counter64 *) vp->val.counter64,
sizeof(*vp->val.counter64));
break;
#endif /* OPAQUE_SPECIAL_TYPES */
case ASN_OCTET_STR:
case ASN_IPADDRESS:
case ASN_OPAQUE:
case ASN_NSAP:
if (vp->val_len < sizeof(vp->buf)) {
vp->val.string = (u_char *) vp->buf;
} else {
vp->val.string = (u_char *) malloc(vp->val_len);
}
if (vp->val.string == NULL) {
return -1;
}
asn_parse_string(var_val, &len, &vp->type, vp->val.string,
&vp->val_len);
break;
case ASN_OBJECT_ID:
vp->val_len = MAX_OID_LEN;
asn_parse_objid(var_val, &len, &vp->type, objid, &vp->val_len);
vp->val_len *= sizeof(oid);
vp->val.objid = (oid *) malloc(vp->val_len);
if (vp->val.objid == NULL) {
return -1;
}
memmove(vp->val.objid, objid, vp->val_len);
break;
case SNMP_NOSUCHOBJECT:
case SNMP_NOSUCHINSTANCE:
case SNMP_ENDOFMIBVIEW:
case ASN_NULL:
break;
case ASN_BIT_STR:
vp->val.bitstring = (u_char *) malloc(vp->val_len);
if (vp->val.bitstring == NULL) {
return -1;
}
asn_parse_bitstring(var_val, &len, &vp->type,
vp->val.bitstring, &vp->val_len);
break;
default:
snmp_log(LOG_ERR, "bad type returned (%x)\n", vp->type);
badtype = -1;
break;
}
DEBUGINDENTADD(-4);
}
return badtype;
}
void
snmp_free_var(netsnmp_variable_list * var)
{
if (!var)
return;
if (var->name != var->name_loc)
SNMP_FREE(var->name);
if (var->val.string != var->buf)
SNMP_FREE(var->val.string);
if (var->data) {
if (var->dataFreeHook) {
var->dataFreeHook(var->data);
var->data = NULL;
} else {
SNMP_FREE(var->data);
}
}
free((char *) var);
}
#endif /* USE_SNMPTRAPD */
/*-----------------------------------------------------------------------------
* Function prototypes
*----------------------------------------------------------------------------*/
static int nm_snmp_getreq(lua_State *L, int req_type, int req_mode);
static int nm_snmp_set_info_req(lua_State *L, int req_type,int req_mode);
static int nm_snmp_synch_req(lua_State *L, Tsession *nm_session, struct snmp_pdu *pdu, int islist);
static int nm_snmp_asynch_req(lua_State *L, Tsession *nm_session, struct snmp_pdu *pdu, int islist, int ref_cb, int ref_magic);
static int nm_snmp_callback(int op, CmuSession *session, int reqid, struct snmp_pdu *pdu, void *magic);
static void nm_snmp_freereqs(Tsession *nm_session);
static int nm_snmp_event(lua_State *L);
#ifdef USE_SNMPTRAPD
static void nm_snmp_trap(lua_State *L, char *buf, int rxlen);
#else
static void nm_snmp_trap(lua_State *L, u_char *packet, int length, struct sockaddr_in *from);
#endif
static Tsession *nm_snmp_getsession(lua_State *L);
/*-----------------------------------------------------------------------------
* Function wrapped to Lua
*----------------------------------------------------------------------------*/
static int nm_snmp_gettrapd(lua_State *L)
{
#ifdef USE_SNMPTRAPD
lua_pushstring(L, "snmptrapd");
#else
lua_pushstring(L, "straps");
#endif
return 1;
}
#if 0
static void nm_snmp_init_session(netsnmp_session *session)
{
memset(session, 0, sizeof(netsnmp_session));
session->remote_port = SNMP_DEFAULT_REMPORT;
session->timeout = SNMP_DEFAULT_TIMEOUT;
session->retries = SNMP_DEFAULT_RETRIES;
session->version = SNMP_DEFAULT_VERSION;
session->securityModel = SNMP_DEFAULT_SECMODEL;
session->rcvMsgMaxSize = SNMP_MAX_MSG_SIZE;
session->flags |= SNMP_FLAGS_DONT_PROBE;
}
#endif
/*-----------------------------------------------------------------------------
* nm_snmp_open
*
* Synopsis : snmp.open(INIT_TABLE)
* Lua Param: INIT TABLE
* Return : nil+error on failure, userdata + peer IP address to session on success
* Function : Opens an snmp session
*----------------------------------------------------------------------------*/
static int nm_snmp_open(lua_State *L) {
Tsession *nm_session;
int version;
int flg_nopeer;
char *peername;
struct in_addr peer_addr;
char *Apsz, *Xpsz;
/* Get a table from Lua stack */
if (!lua_istable(L, -1)) {
lua_pushnil(L);
lua_pushstring(L, "snmp: internal failure");
return 2;
}
/* Base parameters for the session */
#if 0
#ifdef REMOVE_THIS
memset((char *)&nm_cmu_session,0,sizeof(CmuSession));
#endif
nm_snmp_init_session(&nm_cmu_session);
#else
snmp_sess_init(&nm_cmu_session);
#endif
lua_pushstring(L, "version");
lua_gettable(L, -2);
version = lua_tonumber(L, -1);
lua_remove(L, -1);
nm_cmu_session.version = version;
if ((version == SNMP_VERSION_2c) || (version == SNMP_VERSION_1)){
lua_pushstring(L, "community");
lua_gettable(L, -2);
nm_cmu_session.community = (u_char *) lua_tostring(L, -1);
nm_cmu_session.community_len = strlen((char *)nm_cmu_session.community);
lua_remove(L, -1);
}
/* Version 3 authentication stuff */
if (version == SNMP_VERSION_3){
lua_pushstring(L, "user");
lua_gettable(L, -2);
if (!lua_isnil(L, -1)){
nm_cmu_session.securityName = (char *) lua_tostring(L, -1);
nm_cmu_session.securityNameLen = strlen(lua_tostring(L, -1));
}
lua_remove(L,-1);
lua_pushstring(L, "_securityLevel");
lua_gettable(L, -2);
if (!lua_isnil(L, -1))
nm_cmu_session.securityLevel = (int) lua_tonumber(L, -1);
lua_remove(L, -1);
nm_cmu_session.securityModel = SNMP_DEFAULT_SECMODEL;
/*
* Authentication Protocol
*/
lua_pushstring(L, "authType");
lua_gettable(L, -2);
if (!lua_isnil(L, -1)){
if (!strcmp(lua_tostring(L, -1), "MD5")){
nm_cmu_session.securityAuthProto = usmHMACMD5AuthProtocol;
nm_cmu_session.securityAuthProtoLen = USM_AUTH_PROTO_MD5_LEN;
} else if (!strcmp(lua_tostring(L, -1), "SHA")){
nm_cmu_session.securityAuthProto = usmHMACSHA1AuthProtocol;
nm_cmu_session.securityAuthProtoLen = USM_AUTH_PROTO_SHA_LEN;
} else if (!strcmp(lua_tostring(L, -1), "NOAUTH")){
nm_cmu_session.securityAuthProto = usmHMACSHA1AuthProtocol;
nm_cmu_session.securityAuthProtoLen = USM_AUTH_PROTO_NOAUTH_LEN;
}
}
lua_remove(L, -1);
/*
* Encryption Protocol
*/
lua_pushstring(L, "privType");
lua_gettable(L, -2);
if (!lua_isnil(L, -1)){
if (0) {}
#if defined(USM_PRIV_PROTO_DES_LEN) && !defined(NETSNMP_DISABLE_DES)
else if (!strcmp(lua_tostring(L, -1), "DES")){
nm_cmu_session.securityPrivProto = usmDESPrivProtocol;
nm_cmu_session.securityPrivProtoLen = USM_PRIV_PROTO_DES_LEN;
}
#endif
else if (!strcmp(lua_tostring(L, -1), "AES")){
nm_cmu_session.securityPrivProto = usmAESPrivProtocol;
nm_cmu_session.securityPrivProtoLen = USM_PRIV_PROTO_AES_LEN;
}
else if (!strcmp(lua_tostring(L, -1), "NOAUTH")){
nm_cmu_session.securityPrivProto = usmNoPrivProtocol;
nm_cmu_session.securityPrivProtoLen = USM_PRIV_PROTO_NOPRIV_LEN;
}
}
lua_remove(L, -1);
lua_pushstring(L, "authPassphrase");
lua_gettable(L, -2);
Apsz = (char *) lua_tostring(L, -1);
if (Apsz) {
nm_cmu_session.securityAuthKeyLen = USM_AUTH_KU_LEN;
if (generate_Ku(nm_cmu_session.securityAuthProto,
nm_cmu_session.securityAuthProtoLen,
(u_char *) Apsz, strlen(Apsz),
nm_cmu_session.securityAuthKey,
&nm_cmu_session.securityAuthKeyLen) != SNMPERR_SUCCESS) {
lua_remove(L, -1);
lua_pushnil(L);
lua_pushstring(L, "snmp: error generating key from authentication password.");
return 2;
}
}
lua_remove(L, -1);
/*
* Encryption password
*/
lua_pushstring(L, "privPassphrase");
lua_gettable(L, -2);
Xpsz = (char *) lua_tostring(L, -1);
if (Xpsz) {
nm_cmu_session.securityPrivKeyLen = USM_PRIV_KU_LEN;
if (generate_Ku(nm_cmu_session.securityAuthProto,
nm_cmu_session.securityAuthProtoLen,
(u_char *) Xpsz, strlen(Xpsz),
nm_cmu_session.securityPrivKey,
&nm_cmu_session.securityPrivKeyLen) != SNMPERR_SUCCESS) {
lua_remove(L, -1);
lua_pushnil(L);
lua_pushstring(L, "snmp: error generating key from privacy password.");
return 2;
}
}
lua_remove(L, -1);
lua_pushstring(L, "context");
lua_gettable(L, -2);
if (!lua_isnil(L, -1)){
nm_cmu_session.contextName = (char *) lua_tostring(L, -1);
nm_cmu_session.contextNameLen = strlen(lua_tostring(L, -1));
}
lua_remove(L, -1);
lua_pushstring(L, "engineID");
lua_gettable(L, -2);
if (!lua_isnil(L, -1)) {
size_t ebuf_len = 32;
size_t eout_len = 0;
u_char *ebuf = (u_char *)malloc(ebuf_len);
if (ebuf == NULL) {
lua_remove(L, -1);
lua_pushnil(L);
lua_pushstring(L, "snmp: intenal memory error.");
return 2;
}
if (!snmp_hex_to_binary(&ebuf, &ebuf_len, &eout_len, 1, lua_tostring(L, -1))){
free(ebuf);
lua_remove(L, -1);
lua_pushnil(L);
lua_pushstring(L, "snmp: bad authentication engine id");
return 2;
}
nm_cmu_session.securityEngineID = ebuf;
nm_cmu_session.securityEngineIDLen = eout_len;
}
lua_remove(L, -1);
lua_pushstring(L, "contextId");
lua_gettable(L, -2);
if (!lua_isnil(L, -1)) {
size_t ebuf_len = 32, eout_len = 0;
u_char *ebuf = (u_char *)malloc(ebuf_len);
if (ebuf == NULL) {
lua_remove(L, -1);
lua_pushnil(L);
lua_pushstring(L, "snmp: intenal memory error.");
return 2;
}
if (!snmp_hex_to_binary(&ebuf, &ebuf_len, &eout_len, 1, lua_tostring(L, -1))){
free(ebuf);
lua_remove(L, -1);
lua_pushnil(L);
lua_pushstring(L, "snmp: bad authentication engine id");
return 2;
}
nm_cmu_session.contextEngineID = ebuf;
nm_cmu_session.contextEngineIDLen = eout_len;
}
lua_remove(L, -1);
#ifdef HERE
lua_pushstring(L, "boots");
#endif
} /* Version 3 stuff */
lua_pushstring(L, "timeout");
lua_gettable(L, -2);
nm_cmu_session.timeout = lua_tonumber(L, -1);
lua_remove(L, -1);
/* timeout in microseconds */
nm_cmu_session.timeout *= DEFAULT_TIMEOUT;
lua_pushstring(L, "retries");
lua_gettable(L, -2);
nm_cmu_session.retries = lua_tonumber(L, -1);
lua_remove(L, -1);
lua_pushstring(L, "peer");
lua_gettable(L, -2);
peername = (char *) lua_tostring(L, -1);
nm_cmu_session.peername = peername;
lua_remove(L, -1);
/* Check whether we have a peer address (!= 0.0.0.0) */
if (!strcmp(nm_cmu_session.peername, peer_def)) {
flg_nopeer = TRUE;
peer_addr.s_addr = 0;
} else {
flg_nopeer = FALSE;
if (inet_aton((const char *) peername, &peer_addr) == 0){
/* invalid dot notation - try to resolve dns */
struct hostent *hp;
if ((hp = gethostbyname(peername)) == NULL) {
lua_pushnil(L);
lua_pushstring(L, "snmp: bad peer address");
return 2;
}
memcpy((char *)&peer_addr.s_addr, hp->h_addr, hp->h_length);
}
}
#ifdef REMOVE_THIS_2
lua_pushstring(L, "port");
lua_gettable(L, -2);
nm_cmu_session.remote_port = lua_tonumber(L, -1);
lua_remove(L, -1);
#endif
lua_pushstring(L, "localport");
lua_gettable(L, -2);
nm_cmu_session.local_port = lua_tonumber(L, -1);
lua_remove(L, -1);
/* Completa os outros campos da sessao CMU */
nm_cmu_session.authenticator = NULL;
/* Allocate Luasnmp session */
nm_session=(Tsession *)malloc(sizeof(Tsession));
if (nm_session == NULL) {
lua_pushnil(L);
lua_pushstring(L,"snmp: internal error - session allocation");
return 2;
}
memset((char *)nm_session,0,sizeof(Tsession));
#if 0
lua_pushstring(L, "trapport");
lua_gettable(L, -2);
nm_session->trap_port = lua_tonumber(L, -1);
lua_remove(L, -1);
#endif
lua_pushstring(L, "callback");
lua_gettable(L, -2);
if (!lua_isnil(L, -1)) {
lua_pushlightuserdata(L, &nm_session->defcb);
lua_pushvalue(L, -2);
lua_settable(L, LUA_REGISTRYINDEX);
nm_session->defcb = 1;
} else {
nm_session->defcb = -1;
}
lua_remove(L, -1);
lua_pushstring(L, "trap");
lua_gettable(L, -2);
if ((!lua_isnil(L, -1)) && (lua_isfunction(L, -1))) {
lua_pushlightuserdata(L, &nm_session->trapcb);
lua_pushvalue(L, -2);
lua_settable(L, LUA_REGISTRYINDEX);