-
Notifications
You must be signed in to change notification settings - Fork 61
/
util.c
2113 lines (1757 loc) · 56.9 KB
/
util.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
/*
* Copyright (C) 2011 Tildeslash Ltd. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations
* including the two.
*
* You must obey the GNU General Public License in all respects
* for all of the code used other than OpenSSL. If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. If you
* do not wish to do so, delete this exception statement from your
* version. If you delete this exception statement from all source
* files in the program, then also delete it here.
*/
#include <config.h>
#ifdef HAVE_STDIO_H
#include <stdio.h>
#endif
#ifdef HAVE_STDARG_H
#include <stdarg.h>
#endif
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#ifdef HAVE_CTYPE_H
#include <ctype.h>
#endif
#ifdef HAVE_SIGNAL_H
#include <signal.h>
#endif
#if TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# if HAVE_SYS_TIME_H
# include <sys/time.h>
# else
# include <time.h>
# endif
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#ifdef HAVE_CRYPT_H
#include <crypt.h>
#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
#ifdef HAVE_SECURITY_PAM_APPL_H
#include <security/pam_appl.h>
#endif
#ifdef HAVE_PAM_PAM_APPL_H
#include <pam/pam_appl.h>
#endif
#ifdef HAVE_PWD_H
#include <pwd.h>
#endif
#ifdef HAVE_GRP_H
#include <grp.h>
#endif
#include "monitor.h"
#include "engine.h"
#include "md5.h"
#include "sha.h"
#include "base64.h"
#include "alert.h"
#include "process.h"
#include "event.h"
/* Private prototypes */
static char x2c(char *hex);
static char *is_str_defined(char *);
static void printevents(unsigned int);
#ifdef HAVE_LIBPAM
#ifdef SOLARIS
static int PAMquery(int, struct pam_message **, struct pam_response **, void *);
#else
static int PAMquery(int, const struct pam_message **, struct pam_response **, void *);
#endif
static int PAMcheckPasswd(const char *, const char *);
static Auth_T PAMcheckUserGroup(const char *);
#endif
struct ad_user {
const char *login;
const char *passwd;
};
/* Unsafe URL characters: <>\"#%{}|\\^[] ` */
static const unsigned char urlunsafe[256] = {
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
};
static const unsigned char b2x[][256] = {
"00", "01", "02", "03", "04", "05", "06", "07",
"08", "09", "0A", "0B", "0C", "0D", "0E", "0F",
"10", "11", "12", "13", "14", "15", "16", "17",
"18", "19", "1A", "1B", "1C", "1D", "1E", "1F",
"20", "21", "22", "23", "24", "25", "26", "27",
"28", "29", "2A", "2B", "2C", "2D", "2E", "2F",
"30", "31", "32", "33", "34", "35", "36", "37",
"38", "39", "3A", "3B", "3C", "3D", "3E", "3F",
"40", "41", "42", "43", "44", "45", "46", "47",
"48", "49", "4A", "4B", "4C", "4D", "4E", "4F",
"50", "51", "52", "53", "54", "55", "56", "57",
"58", "59", "5A", "5B", "5C", "5D", "5E", "5F",
"60", "61", "62", "63", "64", "65", "66", "67",
"68", "69", "6A", "6B", "6C", "6D", "6E", "6F",
"70", "71", "72", "73", "74", "75", "76", "77",
"78", "79", "7A", "7B", "7C", "7D", "7E", "7F",
"80", "81", "82", "83", "84", "85", "86", "87",
"88", "89", "8A", "8B", "8C", "8D", "8E", "8F",
"90", "91", "92", "93", "94", "95", "96", "97",
"98", "99", "9A", "9B", "9C", "9D", "9E", "9F",
"A0", "A1", "A2", "A3", "A4", "A5", "A6", "A7",
"A8", "A9", "AA", "AB", "AC", "AD", "AE", "AF",
"B0", "B1", "B2", "B3", "B4", "B5", "B6", "B7",
"B8", "B9", "BA", "BB", "BC", "BD", "BE", "BF",
"C0", "C1", "C2", "C3", "C4", "C5", "C6", "C7",
"C8", "C9", "CA", "CB", "CC", "CD", "CE", "CF",
"D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7",
"D8", "D9", "DA", "DB", "DC", "DD", "DE", "DF",
"E0", "E1", "E2", "E3", "E4", "E5", "E6", "E7",
"E8", "E9", "EA", "EB", "EC", "ED", "EE", "EF",
"F0", "F1", "F2", "F3", "F4", "F5", "F6", "F7",
"F8", "F9", "FA", "FB", "FC", "FD", "FE", "FF"
};
/**
* General purpose utility methods.
*
* @author Jan-Henrik Haukeland, <[email protected]>
* @author "Martin Pala" <[email protected]>
* @author Christian Hopp <[email protected]>
* @author Michael Amster, <[email protected]>
*
* @file
*/
/* ------------------------------------------------------------------ Public */
char *Util_basename(char* path) {
char *fname;
ASSERT(path);
fname= strrchr(path, '/');
return(fname ? ++fname : path);
}
char *Util_chomp(char *s) {
ASSERT(s);
for (; *s; s++) {
if (('\r' == *s) || ('\n' == *s)) {
*s= 0; break;
}
}
return s;
}
char *Util_trim(char *s) {
ASSERT(s);
Util_ltrim(s);
Util_rtrim(s);
return s;
}
char *Util_ltrim(char *s) {
char *t= s;
ASSERT(s);
while(*t==' ' || *t=='\t' || *t=='\r' || *t=='\n') t++;
if(t!=s) {
char *r= s;
do {
*r++= *t;
} while(*t++);
}
return s;
}
char *Util_rtrim(char *s) {
char *t= s;
ASSERT(s);
while(*s) s++;
while(*--s==' ' || *s=='\t' || *s=='\r' || *s=='\n') *s= 0;
return t;
}
void Util_trimQuotes(char *s) {
char *t= s;
char tmp=0;
ASSERT(s);
if(*t==39 || *t==34 ) {
tmp=*t;
t++;
} else
return;
while ( *t != tmp && *t != '\0' ) {
*(t-1) = *t;
t++;
}
*(t-1) = '\0';
return;
}
char *Util_trunc(char *s, int n) {
ASSERT(n>=0);
if (s) {
int sl = strlen(s);
if (sl > (n + 4)) {
int e = n+3;
for (; n < e; n++)
s[n]= '.';
s[n]= 0;
}
}
return s;
}
char *Util_replace(char *s, char old, char new) {
char *t= s;
while (s&&*s) { if(*s==old) *s=new; s++; }
return (t);
}
char *Util_replaceString(char **src, const char *old, const char *new) {
int i;
int d;
ASSERT(src);
ASSERT(*src);
ASSERT(old);
ASSERT(new);
i= Util_countWords(*src, old);
d= strlen(new)-strlen(old);
if(i==0)
return *src;
if(d>0)
d*= i;
else
d= 0;
{
char *p, *q;
int l= strlen(old);
char *buf= xcalloc(sizeof(char), strlen(*src)+d+1);
q= *src;
*buf= 0;
while((p= strstr(q, old))) {
*p= '\0';
strcat(buf, q);
strcat(buf, new);
p+= l;
q= p;
}
strcat(buf, q);
FREE(*src);
*src= buf;
}
return *src;
}
int Util_countWords(char *s, const char *word) {
int i= 0;
char *p= s;
ASSERT(s && word);
while((p= strstr(p, word))) { i++; p++; }
return i;
}
int Util_startsWith(const char *a, const char *b) {
if((!a || !b) || toupper((int)*a)!=toupper((int)*b)) return FALSE;
while(*a && *b)
if(toupper((int)*a++) != toupper((int)*b++))
return FALSE;
return TRUE;
}
void Util_handleEscapes(char *buf) {
int editpos;
int insertpos;
ASSERT(buf);
for(editpos=insertpos=0; *(buf+editpos)!='\0'; editpos++, insertpos++) {
if(*(buf+editpos) == '\\' ) {
switch(*(buf+editpos+1)) {
case 'n':
*(buf+insertpos)='\n';
editpos++;
break;
case 't':
*(buf+insertpos)='\t';
editpos++;
break;
case 'r':
*(buf+insertpos)='\r';
editpos++;
break;
case ' ':
*(buf+insertpos)=' ';
editpos++;
break;
case '0':
if(*(buf+editpos+2)=='x') {
if((*(buf+editpos+3)=='0' && *(buf+editpos+4)=='0')) {
/* Don't swap \0x00 with 0 to avoid truncating the string.
Currently the only place where we support sending of 0 bytes
is in check_generic(). The \0x00 -> 0 byte swap is performed
there and in-place.
*/
*(buf+insertpos)=*(buf+editpos);
} else {
*(buf+insertpos)=x2c(&buf[editpos+3]);
editpos+=4;
}
}
break;
case '\\':
*(buf+insertpos)='\\';
editpos++;
break;
default:
*(buf+insertpos)=*(buf+editpos);
}
} else {
*(buf+insertpos)=*(buf+editpos);
}
}
*(buf+insertpos)='\0';
}
int Util_handle0Escapes(char *buf) {
int editpos;
int insertpos;
ASSERT(buf);
for(editpos=insertpos=0; *(buf+editpos)!='\0'; editpos++, insertpos++) {
if(*(buf+editpos) == '\\' ) {
switch(*(buf+editpos+1)) {
case '0':
if(*(buf+editpos+2)=='x') {
*(buf+insertpos)=x2c(&buf[editpos+3]);
editpos+=4;
}
break;
default:
*(buf+insertpos)=*(buf+editpos);
}
} else {
*(buf+insertpos)=*(buf+editpos);
}
}
*(buf+insertpos)='\0';
return insertpos;
}
char *Util_digest2Bytes(unsigned char *digest, int mdlen, MD_T result) {
int i;
unsigned char *tmp= (unsigned char*)result;
static unsigned char hex[] = "0123456789abcdef";
ASSERT(mdlen * 2 < MD_SIZE); // Overflow guard
for(i= 0; i < mdlen; i++) {
*tmp++ = hex[digest[i] >> 4];
*tmp++ = hex[digest[i] & 0xf];
}
*tmp = '\0';
return result;
}
Service_T Util_getService(const char *name) {
Service_T s;
ASSERT(name);
for(s= servicelist; s; s= s->next) {
if(IS(s->name, name)) {
return s;
}
}
return NULL;
}
int Util_getNumberOfServices() {
int i= 0;
Service_T s;
for(s= servicelist; s; s= s->next) i+=1;
return i;
}
int Util_existService(const char *name) {
ASSERT(name);
return Util_getService(name)?TRUE:FALSE;
}
void Util_printRunList() {
printf("Runtime constants:\n");
printf(" %-18s = %s\n", "Control file", is_str_defined(Run.controlfile));
printf(" %-18s = %s\n", "Log file", is_str_defined(Run.logfile));
printf(" %-18s = %s\n", "Pid file", is_str_defined(Run.pidfile));
printf(" %-18s = %s\n", "Debug", Run.debug?"True":"False");
printf(" %-18s = %s\n", "Log", Run.dolog?"True":"False");
printf(" %-18s = %s\n", "Use syslog", Run.use_syslog?"True":"False");
printf(" %-18s = %s\n", "Is Daemon", Run.isdaemon?"True":"False");
printf(" %-18s = %s\n", "Use process engine", Run.doprocess?"True":"False");
printf(" %-18s = %d seconds with start delay %d seconds\n", "Poll time", Run.polltime, Run.startdelay);
printf(" %-18s = %d bytes\n", "Expect buffer", Run.expectbuffer);
if(Run.eventlist_dir) {
char slots[STRLEN];
if(Run.eventlist_slots < 0)
snprintf(slots, STRLEN, "unlimited");
else
snprintf(slots, STRLEN, "%d", Run.eventlist_slots);
printf(" %-18s = base directory %s with %s slots\n",
"Event queue", Run.eventlist_dir, slots);
}
if(Run.mmonits) {
Mmonit_T c;
printf(" %-18s = ", "M/Monit(s)");
for(c= Run.mmonits; c; c= c->next) {
printf("%s with timeout %d seconds%s%s%s%s%s%s",
c->url->url,
c->timeout,
(c->ssl.use_ssl && c->ssl.version) ? " ssl version " : "",
(c->ssl.use_ssl && c->ssl.version) ? sslnames[c->ssl.version] : "",
c->ssl.certmd5?" server cert md5 sum ":"",
c->ssl.certmd5?c->ssl.certmd5:"",
c->url->user?" using credentials":"",
c->next?",\n = ":"");
}
if (! Run.dommonitcredentials)
printf("\n register without credentials");
printf("\n");
}
if(Run.mailservers) {
MailServer_T mta;
printf(" %-18s = ", "Mail server(s)");
for(mta= Run.mailservers; mta; mta= mta->next)
printf("%s:%d%s%s",
mta->host,
mta->port,
mta->ssl.use_ssl?"(ssl)":"",
mta->next?", ":" ");
printf("with timeout %d seconds", Run.mailserver_timeout);
if(Run.mail_hostname)
printf(" using '%s' as my hostname", Run.mail_hostname);
printf("\n");
}
printf(" %-18s = %s\n", "Mail from", is_str_defined(Run.MailFormat.from));
printf(" %-18s = %s\n", "Mail subject",
is_str_defined(Run.MailFormat.subject));
printf(" %-18s = %-.20s%s\n", "Mail message",
Run.MailFormat.message?
Run.MailFormat.message:"(not defined)",
Run.MailFormat.message?"..(truncated)":"");
printf(" %-18s = %s\n", "Start monit httpd", Run.dohttpd?"True":"False");
if(Run.dohttpd) {
printf(" %-18s = %s\n", "httpd bind address",
Run.bind_addr?Run.bind_addr:"Any/All");
printf(" %-18s = %d\n", "httpd portnumber", Run.httpdport);
printf(" %-18s = %s\n", "httpd signature", Run.httpdsig?"True":"False");
printf(" %-18s = %s\n", "Use ssl encryption", Run.httpdssl?"True":"False");
if(Run.httpdssl) {
printf(" %-18s = %s\n", "PEM key/cert file", Run.httpsslpem);
if(Run.httpsslclientpem!=NULL) {
printf(" %-18s = %s\n", "Client cert file", Run.httpsslclientpem);
} else {
printf(" %-18s = %s\n", "Client cert file", "None");
}
printf(" %-18s = %s\n", "Allow self certs",
Run.allowselfcert?"True":"False");
}
printf(" %-18s = %s\n", "httpd auth. style",
(Run.credentials!=NULL)&&has_hosts_allow()?
"Basic Authentication and Host/Net allow list":
(Run.credentials!=NULL)?"Basic Authentication":
has_hosts_allow()?"Host/Net allow list":
"No authentication!");
}
{
Mail_T list;
for(list= Run.maillist; list; list= list->next) {
printf(" %-18s = %s\n", "Alert mail to", is_str_defined(list->to));
printf(" %-16s = ", "Alert on");
printevents(list->events);
if(list->reminder)
printf(" %-16s = %u cycles\n", "Alert reminder", list->reminder);
}
}
printf("\n");
}
void Util_printService(Service_T s) {
int sgheader = FALSE;
Port_T n;
Icmp_T i;
Mail_T r;
Filesystem_T dl;
Resource_T q;
Timestamp_T t;
ActionRate_T ar;
Size_T sl;
Match_T ml;
Dependant_T d;
ServiceGroup_T sg;
ServiceGroupMember_T sgm;
char buf[STRLEN];
ASSERT(s);
snprintf(buf, sizeof(buf), "%s Name", servicetypes[s->type]);
printf("%-21s = %s\n", buf, s->name);
for (sg = servicegrouplist; sg; sg = sg->next) {
for (sgm = sg->members; sgm; sgm = sgm->next) {
if (! strcasecmp(sgm->name, s->name)) {
if (! sgheader) {
printf(" %-20s = %s", "Group", sg->name);
sgheader = TRUE;
} else
printf(", %s", sg->name);
}
}
}
if (sgheader)
printf("\n");
if(s->type == TYPE_PROCESS) {
if (s->matchlist)
printf(" %-20s = %s\n", "Match", s->path);
else
printf(" %-20s = %s\n", "Pid file", s->path);
} else if(s->type != TYPE_HOST && s->type != TYPE_SYSTEM) {
printf(" %-20s = %s\n", "Path", s->path);
}
printf(" %-20s = %s\n", "Monitoring mode", modenames[s->mode]);
if(s->start) {
int i = 0;
printf(" %-20s = '", "Start program");
while(s->start->arg[i]) {
if(i) printf(" ");
printf("%s", s->start->arg[i++]);
}
printf("'");
if(s->start->has_uid)
printf(" as uid %d", s->start->uid);
if(s->start->has_gid)
printf(" as gid %d", s->start->gid);
printf(" timeout %d second(s)", s->start->timeout);
printf("\n");
}
if(s->stop) {
int i = 0;
printf(" %-20s = '", "Stop program");
while(s->stop->arg[i]) {
if(i) printf(" ");
printf("%s", s->stop->arg[i++]);
}
printf("'");
if(s->stop->has_uid)
printf(" as uid %d", s->stop->uid);
if(s->stop->has_gid)
printf(" as gid %d", s->stop->gid);
printf(" timeout %d second(s)", s->stop->timeout);
printf("\n");
}
if(s->type != TYPE_SYSTEM) {
printf(" %-20s = ", "Existence");
printf("if does not exist %s ", Util_getEventratio(s->action_NONEXIST->failed, buf, sizeof(buf)));
printf("then %s ", Util_describeAction(s->action_NONEXIST->failed, buf, sizeof(buf)));
printf("else if succeeded %s ", Util_getEventratio(s->action_NONEXIST->succeeded, buf, sizeof(buf)));
printf("then %s", Util_describeAction(s->action_NONEXIST->succeeded, buf, sizeof(buf)));
printf("\n");
}
for(d= s->dependantlist; d; d= d->next)
if(d->dependant != NULL)
printf(" %-20s = %s\n", "Depends on Service", d->dependant);
if(s->type == TYPE_PROCESS) {
printf(" %-20s = ", "Pid");
printf("if changed %s ", Util_getEventratio(s->action_PID->failed, buf, sizeof(buf)));
printf("then %s", Util_describeAction(s->action_PID->failed, buf, sizeof(buf)));
printf("\n");
printf(" %-20s = ", "Ppid");
printf("if changed %s ", Util_getEventratio(s->action_PPID->failed, buf, sizeof(buf)));
printf("then %s", Util_describeAction(s->action_PPID->failed, buf, sizeof(buf)));
printf("\n");
}
if(s->type == TYPE_FILESYSTEM) {
printf(" %-20s = ", "Filesystem flags");
printf("if changed %s ", Util_getEventratio(s->action_FSFLAG->failed, buf, sizeof(buf)));
printf("then %s", Util_describeAction(s->action_FSFLAG->failed, buf, sizeof(buf)));
printf("\n");
}
if(s->checksum && s->checksum->action) {
Checksum_T cs= s->checksum;
EventAction_T a= cs->action;
printf(" %-20s = ", "Checksum");
if(cs->test_changes) {
printf("if changed %s %s ", checksumnames[cs->type], Util_getEventratio(a->failed, buf, sizeof(buf)));
printf("then %s", Util_describeAction(a->failed, buf, sizeof(buf)));
} else {
printf("if failed %s(%s) %s ", cs->hash, checksumnames[cs->type], Util_getEventratio(a->failed, buf, sizeof(buf)));
printf("then %s ", Util_describeAction(a->failed, buf, sizeof(buf)));
printf("else if succeeded %s ", Util_getEventratio(a->succeeded, buf, sizeof(buf)));
printf("then %s", Util_describeAction(a->succeeded, buf, sizeof(buf)));
}
printf("\n");
}
if(s->perm && s->perm->action) {
EventAction_T a= s->perm->action;
printf(" %-20s = ", "Permission");
printf("if failed %04o %s ", s->perm->perm, Util_getEventratio(a->failed, buf, sizeof(buf)));
printf("then %s ", Util_describeAction(a->failed, buf, sizeof(buf)));
printf("else if succeeded %s ", Util_getEventratio(a->succeeded, buf, sizeof(buf)));
printf("then %s", Util_describeAction(a->succeeded, buf, sizeof(buf)));
printf("\n");
}
if(s->uid && s->uid->action) {
EventAction_T a= s->uid->action;
printf(" %-20s = ", "UID");
printf("if failed %d %s ", (int)s->uid->uid, Util_getEventratio(a->failed, buf, sizeof(buf)));
printf("then %s ", Util_describeAction(a->failed, buf, sizeof(buf)));
printf("else if succeeded %s ", Util_getEventratio(a->succeeded, buf, sizeof(buf)));
printf("then %s", Util_describeAction(a->succeeded, buf, sizeof(buf)));
printf("\n");
}
if(s->gid && s->gid->action) {
EventAction_T a= s->gid->action;
printf(" %-20s = ", "GID");
printf("if failed %d %s ", (int)s->gid->gid, Util_getEventratio(a->failed, buf, sizeof(buf)));
printf("then %s ", Util_describeAction(a->failed, buf, sizeof(buf)));
printf("else if succeeded %s ", Util_getEventratio(a->succeeded, buf, sizeof(buf)));
printf("then %s", Util_describeAction(a->succeeded, buf, sizeof(buf)));
printf("\n");
}
if(s->icmplist)
for(i= s->icmplist; i; i= i->next) {
EventAction_T a= i->action;
printf(" %-20s = ", "ICMP");
printf("if failed %s count %d with timeout %d seconds %s ", icmpnames[i->type], i->count, i->timeout, Util_getEventratio(a->failed, buf, sizeof(buf)));
printf("then %s ", Util_describeAction(a->failed, buf, sizeof(buf)));
printf("else if succeeded %s ", Util_getEventratio(a->succeeded, buf, sizeof(buf)));
printf("then %s", Util_describeAction(a->succeeded, buf, sizeof(buf)));
printf("\n");
}
if(s->portlist) {
for(n= s->portlist; n; n= n->next) {
EventAction_T a= n->action;
if(n->family == AF_INET) {
printf(" %-20s = ", "Port");
printf("if failed %s:%d%s [%s via %s] with timeout %d seconds %s ", n->hostname, n->port, n->request ? n->request : "", n->protocol->name, Util_portTypeDescription(n), n->timeout, Util_getEventratio(a->failed, buf, sizeof(buf)));
printf("then %s ", Util_describeAction(a->failed, buf, sizeof(buf)));
printf("else if succeeded %s ", Util_getEventratio(a->succeeded, buf, sizeof(buf)));
printf("then %s", Util_describeAction(a->succeeded, buf, sizeof(buf)));
printf("\n");
if(n->SSL.certmd5 != NULL)
printf(" %-20s = %s\n", "Server cert md5 sum", n->SSL.certmd5);
} else if(n->family == AF_UNIX) {
printf(" %-20s = ", "Unix Socket");
printf("if failed %s [protocol %s] with timeout %d seconds %s ", n->pathname, n->protocol->name, n->timeout, Util_getEventratio(a->failed, buf, sizeof(buf)));
printf("then %s ", Util_describeAction(a->failed, buf, sizeof(buf)));
printf("else if succeeded %s ", Util_getEventratio(a->succeeded, buf, sizeof(buf)));
printf("then %s", Util_describeAction(a->succeeded, buf, sizeof(buf)));
printf("\n");
}
}
}
for(t= s->timestamplist; t; t= t->next) {
EventAction_T a= t->action;
printf(" %-20s = ", "Timestamp");
if(t->test_changes) {
printf("if changed %s ", Util_getEventratio(a->failed, buf, sizeof(buf)));
printf("then %s", Util_describeAction(a->failed, buf, sizeof(buf)));
} else {
printf("if %s %d second(s) %s ", operatornames[t->operator], t->time, Util_getEventratio(a->failed, buf, sizeof(buf)));
printf("then %s ", Util_describeAction(a->failed, buf, sizeof(buf)));
printf("else if succeeded %s ", Util_getEventratio(a->succeeded, buf, sizeof(buf)));
printf("then %s", Util_describeAction(a->succeeded, buf, sizeof(buf)));
}
printf("\n");
}
for(sl= s->sizelist; sl; sl= sl->next) {
EventAction_T a= sl->action;
printf(" %-20s = ", "Size");
if(sl->test_changes) {
printf("if changed %s ", Util_getEventratio(a->failed, buf, sizeof(buf)));
printf("then %s", Util_describeAction(a->failed, buf, sizeof(buf)));
} else {
printf("if %s %llu byte(s) %s ", operatornames[sl->operator], sl->size, Util_getEventratio(a->failed, buf, sizeof(buf)));
printf("then %s ", Util_describeAction(a->failed, buf, sizeof(buf)));
printf("else if succeeded %s ", Util_getEventratio(a->succeeded, buf, sizeof(buf)));
printf("then %s", Util_describeAction(a->succeeded, buf, sizeof(buf)));
}
printf("\n");
}
if (s->type != TYPE_PROCESS) {
for(ml= s->matchlist; ml; ml= ml->next) {
EventAction_T a= ml->action;
printf(" %-20s = ", "Regex");
printf("if%s match \"%s\" %s ", ml->not ? " not" : "", ml->match_string, Util_getEventratio(a->failed, buf, sizeof(buf)));
printf("then %s", Util_describeAction(a->failed, buf, sizeof(buf)));
printf("\n");
}
}
for(dl= s->filesystemlist; dl; dl= dl->next) {
EventAction_T a= dl->action;
if(dl->resource == RESOURCE_ID_INODE) {
printf(" %-20s = ", "Inodes usage limit");
if(dl->limit_absolute > -1) {
printf("if %s %ld %s ", operatornames[dl->operator], dl->limit_absolute, Util_getEventratio(a->failed, buf, sizeof(buf)));
printf("then %s ", Util_describeAction(a->failed, buf, sizeof(buf)));
printf("else if succeeded %s ", Util_getEventratio(a->succeeded, buf, sizeof(buf)));
printf("then %s", Util_describeAction(a->succeeded, buf, sizeof(buf)));
} else {
printf("if %s %.1f%% %s ", operatornames[dl->operator], dl->limit_percent / 10., Util_getEventratio(a->failed, buf, sizeof(buf)));
printf("then %s ", Util_describeAction(a->failed, buf, sizeof(buf)));
printf("else if succeeded %s ", Util_getEventratio(a->succeeded, buf, sizeof(buf)));
printf("then %s", Util_describeAction(a->succeeded, buf, sizeof(buf)));
}
printf("\n");
} else if(dl->resource == RESOURCE_ID_SPACE) {
printf(" %-20s = ", "Space usage limit");
if(dl->limit_absolute > -1) {
printf("if %s %ld blocks %s ", operatornames[dl->operator], dl->limit_absolute, Util_getEventratio(a->failed, buf, sizeof(buf)));
printf("then %s ", Util_describeAction(a->failed, buf, sizeof(buf)));
printf("else if succeeded %s ", Util_getEventratio(a->succeeded, buf, sizeof(buf)));
printf("then %s", Util_describeAction(a->succeeded, buf, sizeof(buf)));
} else {
printf("if %s %.1f%% %s ", operatornames[dl->operator], dl->limit_percent / 10., Util_getEventratio(a->failed, buf, sizeof(buf)));
printf("then %s ", Util_describeAction(a->failed, buf, sizeof(buf)));
printf("else if succeeded %s ", Util_getEventratio(a->succeeded, buf, sizeof(buf)));
printf("then %s", Util_describeAction(a->succeeded, buf, sizeof(buf)));
}
printf("\n");
}
}
for(q= s->resourcelist; q; q= q->next) {
EventAction_T a= q->action;
switch(q->resource_id) {
case RESOURCE_ID_CPU_PERCENT:
printf(" %-20s = ", "CPU usage limit");
break;
case RESOURCE_ID_TOTAL_CPU_PERCENT:
printf(" %-20s = ", "CPU usage limit (incl. children)");
break;
case RESOURCE_ID_CPUUSER:
printf(" %-20s = ", "CPU user limit");
break;
case RESOURCE_ID_CPUSYSTEM:
printf(" %-20s = ", "CPU system limit");
break;
case RESOURCE_ID_CPUWAIT:
printf(" %-20s = ", "CPU wait limit");
break;
case RESOURCE_ID_MEM_PERCENT:
printf(" %-20s = ", "Memory usage limit");
break;
case RESOURCE_ID_MEM_KBYTE:
printf(" %-20s = ", "Memory amount limit");
break;
case RESOURCE_ID_SWAP_PERCENT:
printf(" %-20s = ", "Swap usage limit");
break;
case RESOURCE_ID_SWAP_KBYTE:
printf(" %-20s = ", "Swap amount limit");
break;
case RESOURCE_ID_LOAD1:
printf(" %-20s = ", "Load avg. (1min)");
break;
case RESOURCE_ID_LOAD5:
printf(" %-20s = ", "Load avg. (5min)");
break;
case RESOURCE_ID_LOAD15:
printf(" %-20s = ", "Load avg. (15min)");
break;
case RESOURCE_ID_CHILDREN:
printf(" %-20s = ", "Children");
break;
case RESOURCE_ID_TOTAL_MEM_KBYTE:
printf(" %-20s = ", "Memory amount limit (incl. children)");
break;
case RESOURCE_ID_TOTAL_MEM_PERCENT:
printf(" %-20s = ", "Memory usage limit (incl. children)");
break;
}
switch(q->resource_id) {
case RESOURCE_ID_CPU_PERCENT:
case RESOURCE_ID_TOTAL_CPU_PERCENT:
case RESOURCE_ID_TOTAL_MEM_PERCENT:
case RESOURCE_ID_CPUUSER:
case RESOURCE_ID_CPUSYSTEM:
case RESOURCE_ID_CPUWAIT:
case RESOURCE_ID_MEM_PERCENT:
case RESOURCE_ID_SWAP_PERCENT:
printf("if %s %.1f%% %s ", operatornames[q->operator], q->limit / 10.0, Util_getEventratio(a->failed, buf, sizeof(buf)));
printf("then %s ", Util_describeAction(a->failed, buf, sizeof(buf)));
printf("else if succeeded %s ", Util_getEventratio(a->succeeded, buf, sizeof(buf)));
printf("then %s", Util_describeAction(a->succeeded, buf, sizeof(buf)));
break;
case RESOURCE_ID_MEM_KBYTE:
case RESOURCE_ID_SWAP_KBYTE:
printf("if %s %ldkB %s ", operatornames[q->operator], q->limit, Util_getEventratio(a->failed, buf, sizeof(buf)));
printf("then %s ", Util_describeAction(a->failed, buf, sizeof(buf)));
printf("else if succeeded %s ", Util_getEventratio(a->succeeded, buf, sizeof(buf)));
printf("then %s", Util_describeAction(a->succeeded, buf, sizeof(buf)));
break;
case RESOURCE_ID_LOAD1:
case RESOURCE_ID_LOAD5:
case RESOURCE_ID_LOAD15:
printf("if %s %.1f %s ", operatornames[q->operator], q->limit / 10.0, Util_getEventratio(a->failed, buf, sizeof(buf)));