1 /*
2 Copyright 2021 Northern.tech AS
3
4 This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
5
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; version 3.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
18
19 To the extent this program is licensed as part of the Enterprise
20 versions of CFEngine, the applicable Commercial Open Source License
21 (COSL) may apply to this file if you as a licensee so wish it. See
22 included file COSL.txt.
23 */
24
25 #include <audit.h>
26 #include <misc_lib.h>
27 #include <conversion.h>
28 #include <logging.h>
29 #include <string_lib.h>
30 #include <cleanup.h>
31
32 int PR_KEPT = 0; /* GLOBAL_X */
33 int PR_REPAIRED = 0; /* GLOBAL_X */
34 int PR_NOTKEPT = 0; /* GLOBAL_X */
35
36 static bool END_AUDIT_REQUIRED = false; /* GLOBAL_X */
37
BeginAudit()38 void BeginAudit()
39 {
40 END_AUDIT_REQUIRED = true;
41 }
42
UpdatePromiseCounters(PromiseResult status)43 void UpdatePromiseCounters(PromiseResult status)
44 {
45 switch (status)
46 {
47 case PROMISE_RESULT_CHANGE:
48 PR_REPAIRED++;
49 break;
50
51 case PROMISE_RESULT_NOOP:
52 PR_KEPT++;
53 break;
54
55 case PROMISE_RESULT_WARN:
56 case PROMISE_RESULT_TIMEOUT:
57 case PROMISE_RESULT_FAIL:
58 case PROMISE_RESULT_DENIED:
59 case PROMISE_RESULT_INTERRUPTED:
60 PR_NOTKEPT++;
61 break;
62
63 default:
64 ProgrammingError("Unexpected status '%c' has been passed to UpdatePromiseCounters", status);
65 }
66 }
67
EndAudit(const EvalContext * ctx,int background_tasks)68 void EndAudit(const EvalContext *ctx, int background_tasks)
69 {
70 if (!END_AUDIT_REQUIRED)
71 {
72 return;
73 }
74
75 double total = (double) (PR_KEPT + PR_NOTKEPT + PR_REPAIRED) / 100.0;
76
77 const char *version = EvalContextVariableControlCommonGet(ctx, COMMON_CONTROL_VERSION);
78 if (!version)
79 {
80 version = "(not specified)";
81 }
82
83 if (total == 0)
84 {
85 Log(LOG_LEVEL_VERBOSE, "Outcome of version '%s', no checks were scheduled", version);
86 return;
87 }
88 else
89 {
90 LogTotalCompliance(version, background_tasks);
91 }
92 }
93
FatalError(const EvalContext * ctx,char * s,...)94 void FatalError(const EvalContext *ctx, char *s, ...)
95 {
96 if (s)
97 {
98 va_list ap;
99 char buf[CF_BUFSIZE] = "";
100
101 va_start(ap, s);
102 vsnprintf(buf, CF_BUFSIZE - 1, s, ap);
103 va_end(ap);
104 Log(LOG_LEVEL_ERR, "Fatal CFEngine error: %s", buf);
105 }
106
107 EndAudit(ctx, 0);
108 /* calling abort would bypass cleanup handlers and trigger subtle bugs */
109 DoCleanupAndExit(EXIT_FAILURE);
110 }
111