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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
|
/*
* Copyright (c) 2006, Nate Nielsen
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the
* above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or
* other materials provided with the distribution.
* * The names of contributors to this software may not be
* used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
*
* CONTRIBUTORS
* Nate Nielsen <nielsen@memberwebs.com>
*/
#include "usuals.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/queue.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/un.h>
#include <syslog.h>
#include <unistd.h>
#include <stdarg.h>
#include <ctype.h>
#include <fcntl.h>
#include <bsnmp/snmpmod.h>
#ifdef WITH_PCRE
#include <pcre.h>
#else
#include <regex.h>
#endif
#include "regex_tree.h"
#include "regex_oid.h"
#define DEFAULT_CONFIG CONF_PREFIX "/rrdbot"
#define DEFAULT_SOCKET "/var/run/snmp-regex.sock"
#define DEFAULT_EXPIRES 0
/* our module handle */
static struct lmodule *module;
/* OIDs */
static const struct asn_oid oid_regex = OIDX_regex;
/* the Object Resource registration index */
static u_int reg_index = 0;
/* Various match types */
enum {
TYPE_UNKNOWN = 0,
TYPE_COUNTER,
TYPE_VALUE
};
struct data_entry {
uint32_t index;
TAILQ_ENTRY(data_entry) link;
int type;
char *descr;
uint64_t expires;
#ifdef WITH_PCRE
pcre *regex;
pcre_extra *extra;
#else
regex_t regex;
#endif
char *result;
uint64_t last_update;
int64_t value_int;
char *value_str;
};
TAILQ_HEAD(data_entry_list, data_entry);
/* list of regexes */
static struct data_entry_list entries = TAILQ_HEAD_INITIALIZER(entries);
static uint32_t entry_count = 0;
/* configuration */
static u_char *regex_config = NULL;
static char *config_memory = NULL;
static uint64_t option_expires = DEFAULT_EXPIRES;
/* The socket to read from */
static u_char *regex_sock = NULL;
static int sock_fd = -1;
static void *sock_sel = NULL;
#define LINE_LENGTH 1024
struct connection {
int fd;
void *sel;
char line_buffer[LINE_LENGTH];
TAILQ_ENTRY(connection) link;
};
TAILQ_HEAD(connection_list, connection);
/* List of connections */
static struct connection_list connections = TAILQ_HEAD_INITIALIZER(connections);
/* -----------------------------------------------------------------------------
* HELPERS
*/
static void
emsg(const char *format, ...)
{
va_list va;
va_start(va, format);
vsyslog(LOG_ERR, format, va);
va_end(va);
}
static void
strcln (char* data, char ch)
{
char* p;
for (p = data; *data; data++, p++) {
while (*data == ch)
data++;
*p = *data;
}
/* null terminate */
*p = 0;
}
static void
stretrim (char* data)
{
char* t = data + strlen (data);
while (t > data && isspace (*(t - 1))) {
t--;
*t = 0;
}
}
static char*
strbtrim (const char* data)
{
while (*data && isspace (*data))
++data;
return (char*)data;
}
static char*
strtrim (char* data)
{
data = (char*)strbtrim (data);
stretrim (data);
return data;
}
static uint64_t
getcurrticks ()
{
return get_ticks ();
}
#ifndef WITH_PCRE
static const char*
regex_msg (int code)
{
ASSERT (code != 0);
switch (code) {
case REG_NOMATCH:
return "The regexec() function failed to match";
case REG_BADPAT:
return "invalid regular expression";
case REG_ECOLLATE:
return "invalid collating element";
case REG_ECTYPE:
return "invalid character class";
case REG_EESCAPE:
return "'\' applied to unescapable character";
case REG_ESUBREG:
return "invalid backreference number";
case REG_EBRACK:
return "brackets '[ ]' not balanced";
case REG_EPAREN:
return "parentheses '( )' not balanced";
case REG_EBRACE:
return "braces '{ }' not balanced";
case REG_BADBR:
return "invalid repetition count(s) in '{ }'";
case REG_ERANGE:
return "invalid character range in '[ ]'";
case REG_ESPACE:
return "ran out of memory";
case REG_BADRPT:
return "'?', '*', or '+' operand invalid";
case REG_EMPTY:
return "empty (sub)expression";
case REG_ILLSEQ:
return "illegal byte sequence (bad multibyte character)";
case REG_ASSERT:
case REG_INVARG:
default:
return "internal or unknown error";
}
}
#endif /* WITH_PCRE */
/* -----------------------------------------------------------------------------
* MATCHING
*/
static int
process_match (struct data_entry *data, char *result)
{
char *t;
int64_t i;
switch(data->type) {
case TYPE_COUNTER:
data->value_int++;
if (result)
free (result);
break;
case TYPE_VALUE:
if (!result) {
emsg ("match, but no result data for '%s'", data->descr);
return -1;
}
/* Integer value */
i = strtoll (result, &t, 10);
if (!*t)
data->value_int = i;
else
data->value_int = 0;
/* String value */
if (data->value_str)
free (data->value_str);
data->value_str = result;
break;
default:
ASSERT(0);
break;
}
data->last_update = getcurrticks ();
/*
fprintf(stderr, "updated '%s' to value '%d' at '%lld'\n",
data->descr, data->value_str, data->last_update);
*/
return 0;
}
static char*
process_result (struct data_entry *data, char *line, int len,
#ifdef WITH_PCRE
int *ovector, int substrings)
#else
regmatch_t *pm)
#endif
{
char *result;
char *p, *t;
int rlen, l;
int idx;
/* Some nice little optimizations */
if (!data->result)
return NULL;
if (strchr (data->result, '\\') == NULL)
return strdup (data->result);
/* Figure out the string length */
rlen = strlen (data->result) + 1;
for (p = data->result; *p; p++) {
if(p[0] == '\\') {
p++;
if(p[0] == '\\' || !isdigit(p[0]))
continue;
idx = p[0] - '0';
ASSERT (idx >= 0 && idx <= 9);
#ifdef WITH_PCRE
if (idx < substrings) {
ASSERT (ovector[(idx * 2) + 1] >= ovector[idx * 2]);
ASSERT (ovector[(idx * 2) + 1] < len);
ASSERT (ovector[idx * 2] < len);
rlen += (ovector[(idx * 2) + 1]) - (ovector[idx * 2]);
rlen += 1;
}
#else /* WITH_PCRE */
if (pm[idx].rm_so != -1 && pm[idx].rm_eo != -1) {
ASSERT (pm[idx].rm_eo >= pm[idx].rm_so);
ASSERT (pm[idx].rm_eo < len);
ASSERT (pm[idx].rm_so < len);
rlen += (pm[idx].rm_eo - pm[idx].rm_so);
rlen += 1;
}
#endif /* WITH_PCRE */
}
}
result = (char*)calloc(rlen, 1);
if (!result) {
emsg ("out of memory");
return NULL;
}
for (p = data->result, t = result; *p; p++) {
if (p[0] == '\\') {
p++;
if (isdigit(p[0])) {
idx = p[0] - '0';
#ifdef WITH_PCRE
if (idx < substrings) {
l = (ovector[(idx * 2) + 1]) - (ovector[idx * 2]);
memcpy (t, line + (ovector[idx * 2]), l);
t += l;
}
#else /* WITH_PCRE */
if (pm[idx].rm_so != -1 && pm[idx].rm_eo != -1) {
l = pm[idx].rm_eo - pm[idx].rm_so;
memcpy (t, line + pm[idx].rm_so, l);
t += l;
}
#endif /* WITH _PCRE */
continue;
}
}
*t++ = *p;
}
*t = 0;
return result;
}
static void
process_log (char *line, int len)
{
struct data_entry *data;
char *result;
int r;
#ifdef WITH_PCRE
int ovector[30];
#else
regmatch_t pm[10];
#endif
for (data = TAILQ_FIRST (&entries); data;
data = TAILQ_NEXT (data, link)) {
result = NULL;
#ifdef WITH_PCRE
memset (ovector, 0, sizeof (ovector));
r = pcre_exec (data->regex, data->extra, line, len, 0,
PCRE_NOTEMPTY | PCRE_NO_UTF8_CHECK, ovector, 30);
if (r == PCRE_ERROR_NOMATCH)
continue;
else if (r == PCRE_ERROR_NOMEMORY) {
emsg ("out of memory");
return;
} else if (r < 0) {
emsg ("internal error in matching code: %d", r);
return;
}
result = process_result (data, line, len, ovector, r);
#else
r = regexec (&(data->regex), line, 10, pm, 0);
if (r == REG_NOMATCH)
continue;
else if (r != 0) {
emsg ("internal error in matching code: %d", r);
return;
}
result = process_result (data, line, len, pm);
#endif
process_match (data, result);
}
}
/* -----------------------------------------------------------------------------
* SOCKET HANDLING
*/
static void
close_connection (struct connection *conn)
{
if (conn->sel)
fd_deselect (conn->sel);
conn->sel = NULL;
if (conn->fd >= 0)
close (conn->fd);
conn->fd = -1;
free (conn);
TAILQ_REMOVE (&connections, conn, link);
}
static void
io_data (int fd, void *user_data)
{
struct connection *conn = (struct connection*)user_data;
char *line_buffer;
char *t;
int len;
int r, l;
ASSERT (fd == conn->fd);
line_buffer = conn->line_buffer;
len = strlen (line_buffer);
ASSERT (len < LINE_LENGTH);
do {
r = read (fd, line_buffer + len, (LINE_LENGTH - 1) - len);
/* An error, close */
if (r < 0 && errno != EAGAIN) {
emsg ("couldn't read from socket: %s: %s", regex_sock, strerror (errno));
close_connection (conn);
return;
/* Connection is finished, close */
} else if (r == 0) {
close_connection (conn);
return;
/* Got data, null terminate */
} else if (r > 0) {
len += r;
ASSERT (len < LINE_LENGTH);
line_buffer[len] = 0;
}
for (;;) {
t = strchr (line_buffer, '\n');
if (t == NULL) {
/* Break really long lines */
if (len >= LINE_LENGTH - 1)
t = line_buffer + len;
else
break;
}
/* Break line (also DOS line) */
*t = 0;
if (line_buffer != t && *(t - 1) == '\r')
*(t - 1) = 0;
l = (t + 1) - line_buffer;
/* Send it off */
process_log (line_buffer, l);
/* Move data to front of buffer */
ASSERT (l <= len);
memmove (line_buffer, t + 1, (len - l) + 1);
len -= l;
}
} while (r > 0);
}
static void
io_accept (int fd, void *unused)
{
struct connection *conn;
int nfd;
nfd = accept (fd, NULL, NULL);
if (nfd < 0) {
if (errno != EAGAIN || errno != EINTR)
emsg ("couldn't accept socket connection: %s: %s", regex_sock, strerror (errno));
return;
}
fcntl (nfd, F_SETFL, O_NONBLOCK | fcntl(nfd, F_GETFL, 0));
conn = calloc (1, sizeof (*conn));
if (!conn) {
emsg ("out of memory");
close (nfd);
return;
}
conn->fd = nfd;
TAILQ_INSERT_HEAD (&connections, conn, link);
conn->sel = fd_select (nfd, io_data, conn, module);
if (!conn->sel) {
emsg ("couldn't list on socket connection: %s: %s", regex_sock, strerror (errno));
close_connection (conn);
}
}
static void
close_sock ()
{
struct connection *conn;
if (sock_sel)
fd_deselect (sock_sel);
sock_sel = NULL;
if (sock_fd != -1)
close (sock_fd);
sock_fd = -1;
while ((conn = TAILQ_FIRST(&connections)) != NULL)
close_connection (conn);
}
static int
open_sock ()
{
struct stat sb;
struct sockaddr_un addr;
int len;
close_sock ();
ASSERT (regex_sock && regex_sock[0]);
if (stat (regex_sock, &sb) == 0) {
/* Complain if it's not a sock */
if (!S_ISSOCK (sb.st_mode)) {
emsg ("file or directory already exists: %s", regex_sock);
return -1;
}
/* If it's a socket delete */
unlink (regex_sock);
} else if (errno != ENOENT) {
emsg ("couldn't access file: %s: %s", regex_sock, strerror (errno));
return -1;
}
/* Create socket and ... */
sock_fd = socket (AF_UNIX, SOCK_STREAM, 0);
if (sock_fd < 0) {
emsg ("couldn't create socket: %s", strerror (errno));
return -1;
}
memset (&addr, 0, sizeof (addr));
addr.sun_family = AF_UNIX;
strlcpy (addr.sun_path, regex_sock, sizeof (addr.sun_path));
len = sizeof(addr) - (sizeof(addr.sun_path) - strlen (addr.sun_path));
/* ... bind and ... */
if (bind (sock_fd, (struct sockaddr*)&addr, len) != 0) {
emsg ("couldn't bind to socket: %s: %s", regex_sock, strerror (errno));
return -1;
}
/* ... listen to it ... */
if (listen (sock_fd, 5) < 0) {
emsg ("couldn't listen on socket: %s: %s", regex_sock, strerror (errno));
return -1;
}
/* ... watch for new connections ... */
sock_sel = fd_select (sock_fd, io_accept, NULL, module);
if (!sock_sel) {
emsg ("couldn't listen on socket: %s: %s", regex_sock, strerror (errno));
return -1;
}
return 0;
}
/* -----------------------------------------------------------------------------
* CONFIG PARSING
*/
static void
config_free (struct data_entry *data)
{
#ifdef WITH_PCRE
if (data->regex)
free (data->regex);
if (data->extra)
free (data->extra);
#else
regfree (&(data->regex));
#endif
if (data->value_str)
free (data->value_str);
free (data);
}
static void
config_free_all ()
{
struct data_entry *data;
while ((data = TAILQ_FIRST(&entries)) != NULL) {
TAILQ_REMOVE (&entries, data, link);
config_free (data);
}
}
static int
config_entry (struct data_entry *data, char *name, char *regex,
char *result, char *flags, int line)
{
#ifdef WITH_PCRE
const char *errptr;
int erroffset;
int options = 0;
#else
int options = REG_EXTENDED;
int r;
#endif
ASSERT (regex);
ASSERT (flags);
ASSERT (data);
ASSERT (name);
/* No result area means this is a counter */
data->type = result ? TYPE_VALUE : TYPE_COUNTER;
/* Parse any options we have */
for (; *flags; flags++) {
switch (flags[0]) {
case 'i':
#ifdef WITH_PCRE
options |= PCRE_CASELESS;
#else
options |= REG_ICASE;
#endif
break;
default:
emsg ("[line %d] invalid flag: %c", line, flags[0]);
return -1;
}
}
/* The name */
data->descr = name;
/* Parse the regex */
#ifdef WITH_PCRE
data->regex = pcre_compile (regex, options, &errptr, &erroffset, NULL);
if (!data->regex) {
emsg ("[line %d] invalid regular expression: %s", line, errptr);
return -1;
}
/* Optimize the regex, ignore errors */
data->extra = pcre_study (data->regex, 0, &errptr);
#else
r = regcomp (&(data->regex), regex, options);
if (r != 0) {
emsg ("[line %d] invalid regular expression: %s", line, regex_msg (r));
return -1;
}
#endif
/* Replacement data */
data->result = result;
/* Options */
data->expires = option_expires;
return 0;
}
static int
config_line (char *name, char *value, int line)
{
struct data_entry *data;
char *regex = NULL;
char *result = NULL;
char *flags = NULL;
char delimiter;
char *t;
int r;
/* config_parse trims this for us */
ASSERT (!isspace(value[0]));
/* First thing is the delimiter */
if (!*value)
return -2;
delimiter = value[0];
++value;
/* Look for the next delimiter */
t = strchr (value, delimiter);
if (!t)
return -2;
*t = 0;
regex = value;
value = t + 1;
/* And the result */
t = strchr (value, delimiter);
if (t) {
*t = 0;
t++;
result = value;
}
/* The flags */
flags = t ? t : value;
/* Now populate an entry */
data = (struct data_entry*)calloc(1, sizeof (*data));
if (!data) {
emsg ("out of memory");
return -1;
}
/* Now make an entry out of it all */
r = config_entry (data, name, regex, result, flags, line);
if (r < 0) {
free (data);
return r;
}
/* Put it in our table */
data->index = entry_count++;
INSERT_OBJECT_INT (data, &entries);
return 0;
}
static void
config_var (char *name, char *value, int line)
{
char *t2;
int i;
/* The expires variable */
if (strcmp (name, "expire") == 0 || strcmp (name, "expires") == 0) {
i = strtol (value, &t2, 10);
if (i <= 0 || *t2)
emsg ("invalid value for '%s' variable. ignoring: %s", name, value);
else
option_expires = i * 100;
return;
}
emsg ("unknown or invalid variable. ignoring: %s", name);
}
static int
config_read ()
{
FILE *f = NULL;
long len;
ASSERT (regex_config && regex_config[0]);
f = fopen (regex_config, "r");
if (f == NULL) {
emsg ("couldn't open config file: %s", regex_config);
return -1;
}
/* Figure out size */
if (fseek (f, 0, SEEK_END) == -1 || (len = ftell (f)) == -1 || fseek (f, 0, SEEK_SET) == -1) {
emsg ("couldn't seek config file: %s", regex_config);
return -1;
}
ASSERT (!config_memory);
if ((config_memory = (char*)malloc (len + 2)) == NULL) {
emsg ("out of memory");
return -1;
}
/* And read in one block */
if(fread(config_memory, 1, len, f) != len) {
emsg ("couldn't read config file: %s", regex_config);
free (config_memory);
config_memory = NULL;
return -1;
}
fclose (f);
/* Null terminate the data */
config_memory[len] = '\n';
config_memory[len + 1] = 0;
return 0;
}
static int
config_parse ()
{
char* next;
char* p;
char* t;
int line = 0;
int var = 0;
int r;
config_free_all ();
/* Reads raw config file into config_memory */
if (config_read () < 0)
return -1;
ASSERT (config_memory);
/* Remove nasty dos line endings */
strcln(config_memory, '\r');
next = config_memory;
while ((t = strchr (next, '\n')) != NULL) {
*t = 0;
p = next;
next = t + 1;
t = strbtrim (p);
line++;
/* blank lines, comments */
if (!*t || *t == '#')
continue;
t = t + strcspn (t, ":=");
if (!*t) {
emsg ("invalid config line: %s", p);
return -1;
}
/* Equal sign denotes a variable */
var = (*t == '=');
*t = 0;
t++;
/* Pass variables to the appropriate place */
if (var) {
config_var (strtrim (p), strtrim (t), line);
/* And config lines to the appropriate place */
} else {
r = config_line (strtrim (p), strtrim (t), line);
if (r < 0) {
/* If -2 was returned, no error message was printed */
if (r == -2)
emsg ("[line %d] invalid configuration file", line);
return -1;
}
}
}
return 0;
}
/* -----------------------------------------------------------------------------
* CALLBACKS
*/
int
op_regexconfig (struct snmp_context *ctx, struct snmp_value *value,
u_int sub, u_int iidx, enum snmp_op op)
{
asn_subid_t which = value->var.subs[sub - 1];
int r = SNMP_ERR_NOERROR;
switch (which) {
case LEAF_regexConfig:
if (op == SNMP_OP_GET)
return string_get (value, regex_config, -1);
/* Remainder only at initialization */
if (community != COMM_INITIALIZE)
return SNMP_ERR_NOT_WRITEABLE;
switch (op) {
case SNMP_OP_SET:
if ((r = string_save (value, ctx, -1, ®ex_config)) == SNMP_ERR_NOERROR) {
if (!regex_config[0])
r = SNMP_ERR_WRONG_VALUE;
else if (config_parse () < 0)
r = SNMP_ERR_GENERR;
}
if (r != SNMP_ERR_NOERROR)
string_rollback (ctx, ®ex_config);
break;
case SNMP_OP_COMMIT:
string_commit (ctx);
break;
case SNMP_OP_ROLLBACK:
string_rollback (ctx, ®ex_config);
break;
default:
ASSERT(0);
break;
}
return r;
case LEAF_regexSocket:
if (op == SNMP_OP_GET)
return string_get (value, regex_sock, -1);
/* Remainder only at initialization */
if (community != COMM_INITIALIZE)
return SNMP_ERR_NOT_WRITEABLE;
switch (op) {
case SNMP_OP_SET:
if ((r = string_save (value, ctx, -1, ®ex_sock)) == SNMP_ERR_NOERROR) {
if (!regex_sock[0])
r = SNMP_ERR_WRONG_VALUE;
else if (open_sock () < 0)
r = SNMP_ERR_GENERR;
}
if (r != SNMP_ERR_NOERROR)
string_rollback (ctx, ®ex_config);
break;
case SNMP_OP_COMMIT:
string_commit (ctx);
break;
case SNMP_OP_ROLLBACK:
string_rollback (ctx, ®ex_sock);
break;
default:
ASSERT(0);
break;
}
return r;
}
ASSERT(0);
return -1;
}
int
op_regex (struct snmp_context *ctx, struct snmp_value *value,
u_int sub, u_int iidx, enum snmp_op op)
{
asn_subid_t which = value->var.subs[sub - 1];
switch (op) {
case SNMP_OP_GET:
break;
case SNMP_OP_SET:
return SNMP_ERR_NOT_WRITEABLE;
case SNMP_OP_ROLLBACK:
case SNMP_OP_COMMIT:
return SNMP_ERR_NOERROR;
default:
ASSERT(0);
break;
}
switch (which) {
case LEAF_regexCount:
value->v.integer = entry_count;
break;
default:
ASSERT(0);
break;
}
return SNMP_ERR_NOERROR;
}
int
op_regexentry (struct snmp_context *ctx, struct snmp_value *value,
u_int sub, u_int iidx, enum snmp_op op)
{
asn_subid_t which = value->var.subs[sub - 1];
struct data_entry *data;
uint64_t ticks;
int expired = 0;
switch (op) {
case SNMP_OP_GETNEXT:
data = NEXT_OBJECT_INT (&entries, &value->var, sub);
if (data == NULL)
return SNMP_ERR_NOSUCHNAME;
value->var.len = sub + 1;
value->var.subs[sub] = data->index;
break;
case SNMP_OP_GET:
data = FIND_OBJECT_INT (&entries, &value->var, sub);
if (data == NULL)
return SNMP_ERR_NOSUCHNAME;
break;
case SNMP_OP_SET:
if (index_decode (&value->var, sub, iidx, &data))
return SNMP_ERR_NO_CREATION;
data = FIND_OBJECT_INT (&entries, &value->var, sub);
if (data != NULL)
return SNMP_ERR_NOT_WRITEABLE;
return SNMP_ERR_NO_CREATION;
default:
ASSERT(0);
break;
}
/* Figure out if we're expired or not */
ticks = getcurrticks ();
if (ticks == 0)
return SNMP_ERR_GENERR;
if (data->expires && data->last_update) {
if (ticks > (data->last_update + data->expires))
expired = 1;
}
switch (which) {
case LEAF_regexIndex:
value->v.integer = data->index;
break;
case LEAF_regexDescr:
return (string_get (value, data->descr, -1));
case LEAF_regexLast:
if (data->last_update && !expired)
value->v.uint32 = (ticks - data->last_update);
else
value->v.uint32 = 0;
break;
case LEAF_regexInteger:
value->v.uint32 = expired ? 0 : data->value_int;
break;
case LEAF_regexValue:
return (string_get (value, (data->value_str && !expired) ?
data->value_str : "", -1));
default:
ASSERT(0);
break;
};
return SNMP_ERR_NOERROR;
}
/* -----------------------------------------------------------------------------
* MODULE
*/
/* the initialisation function */
static int
module_init (struct lmodule *mod, int argc, char *argv[])
{
module = mod;
if (argc != 0) {
syslog(LOG_ERR, "bad number of arguments for %s", __func__);
return EINVAL;
}
regex_config = strdup (DEFAULT_CONFIG);
if (!regex_config)
return ENOMEM;
regex_sock = strdup (DEFAULT_SOCKET);
if (!regex_sock) {
free (regex_config);
return ENOMEM;
}
return 0;
}
/* Module is started */
static void
module_start (void)
{
reg_index = or_register (&oid_regex, "The MIB for regex data.", module);
}
/* Called, when the module is to be unloaded after it was successfully loaded */
static int
module_fini (void)
{
if (reg_index)
or_unregister (reg_index);
ASSERT (regex_config);
free (regex_config);
ASSERT (regex_sock);
free (regex_sock);
close_sock ();
config_free_all ();
return 0;
}
const struct snmp_module config = {
.comment = "This module implements SNMP listing of data from regular expressions",
.init = module_init,
.start = module_start,
.fini = module_fini,
.tree = regex_ctree,
.tree_size = regex_CTREE_SIZE,
};
|