-
Notifications
You must be signed in to change notification settings - Fork 190
/
Copy pathcf-promises.c
329 lines (278 loc) · 11.3 KB
/
cf-promises.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
/*
Copyright (C) CFEngine AS
This file is part of CFEngine 3 - written and maintained by CFEngine AS.
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; 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, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
To the extent this program is licensed as part of the Enterprise
versions of CFEngine, the applicable Commerical Open Source License
(COSL) may apply to this file if you as a licensee so wish it. See
included file COSL.txt.
*/
#include <generic_agent.h>
#include <env_context.h>
#include <conversion.h>
#include <syntax.h>
#include <rlist.h>
#include <parser.h>
#include <sysinfo.h>
#include <man.h>
#include <time.h>
static GenericAgentConfig *CheckOpts(EvalContext *ctx, int argc, char **argv);
/*******************************************************************/
/* Command line options */
/*******************************************************************/
static const char *CF_PROMISES_SHORT_DESCRIPTION = "validate and analyze CFEngine policy code";
static const char *CF_PROMISES_MANPAGE_LONG_DESCRIPTION = "cf-promises is a tool for checking CFEngine policy code. "
"It operates by first parsing policy code checing for syntax errors. Second, it validates the integrity of "
"policy consisting of multiple files. Third, it checks for semantic errors, e.g. specific attribute set rules. "
"Finally, cf-promises attempts to expose errors by partially evaluating the policy, resolving as many variable and "
"classes promise statements as possible. At no point does cf-promises make any changes to the system.";
static const struct option OPTIONS[] =
{
{"help", no_argument, 0, 'h'},
{"bundlesequence", required_argument, 0, 'b'},
{"debug", no_argument, 0, 'd'},
{"verbose", no_argument, 0, 'v'},
{"dry-run", no_argument, 0, 'n'},
{"version", no_argument, 0, 'V'},
{"file", required_argument, 0, 'f'},
{"define", required_argument, 0, 'D'},
{"negate", required_argument, 0, 'N'},
{"inform", no_argument, 0, 'I'},
{"diagnostic", no_argument, 0, 'x'},
{"reports", no_argument, 0, 'r'},
{"policy-output-format", required_argument, 0, 'p'},
{"syntax-description", required_argument, 0, 's'},
{"full-check", no_argument, 0, 'c'},
{"warn", optional_argument, 0, 'W'},
{"legacy-output", no_argument, 0, 'l'},
{"color", optional_argument, 0, 'C'},
{NULL, 0, 0, '\0'}
};
static const char *HINTS[] =
{
"Print the help message",
"Use the specified bundlesequence for verification",
"Enable debugging output",
"Output verbose information about the behaviour of the agent",
"All talk and no action mode - make no changes, only inform of promises not kept",
"Output the version of the software",
"Specify an alternative input file than the default",
"Define a list of comma separated classes to be defined at the start of execution",
"Define a list of comma separated classes to be undefined at the start of execution",
"Print basic information about changes made to the system, i.e. promises repaired",
"Activate internal diagnostics (developers only)",
"Generate reports about configuration and insert into CFDB",
"Output the parsed policy. Possible values: 'none', 'cf', 'json'. Default is 'none'. (experimental)",
"Output a document describing the available syntax elements of CFEngine. Possible values: 'none', 'json'. Default is 'none'.",
"Ensure full policy integrity checks",
"Pass comma-separated <warnings>|all to enable non-default warnings, or error=<warnings>|all",
"Use legacy output format",
"Enable colorized output. Possible values: 'always', 'auto', 'never'. If option is used, the default value is 'auto'",
NULL
};
/*******************************************************************/
/* Level 0 : Main */
/*******************************************************************/
int main(int argc, char *argv[])
{
EvalContext *ctx = EvalContextNew();
GenericAgentConfig *config = CheckOpts(ctx, argc, argv);
GenericAgentConfigApply(ctx, config);
GenericAgentDiscoverContext(ctx, config);
Policy *policy = GenericAgentLoadPolicy(ctx, config);
if (!policy)
{
Log(LOG_LEVEL_ERR, "Input files contain errors.");
exit(EXIT_FAILURE);
}
if (SHOWREPORTS)
{
ShowPromises(policy->bundles, policy->bodies);
}
CheckForPolicyHub(ctx);
switch (config->agent_specific.common.policy_output_format)
{
case GENERIC_AGENT_CONFIG_COMMON_POLICY_OUTPUT_FORMAT_CF:
{
Policy *output_policy = ParserParseFile(config->input_file, config->agent_specific.common.parser_warnings,
config->agent_specific.common.parser_warnings_error);
Writer *writer = FileWriter(stdout);
PolicyToString(policy, writer);
WriterClose(writer);
PolicyDestroy(output_policy);
}
break;
case GENERIC_AGENT_CONFIG_COMMON_POLICY_OUTPUT_FORMAT_JSON:
{
Policy *output_policy = ParserParseFile(config->input_file, config->agent_specific.common.parser_warnings,
config->agent_specific.common.parser_warnings_error);
JsonElement *json_policy = PolicyToJson(output_policy);
Writer *writer = FileWriter(stdout);
JsonElementPrint(writer, json_policy, 2);
WriterClose(writer);
JsonElementDestroy(json_policy);
PolicyDestroy(output_policy);
}
break;
case GENERIC_AGENT_CONFIG_COMMON_POLICY_OUTPUT_FORMAT_NONE:
break;
}
GenericAgentConfigDestroy(config);
EvalContextDestroy(ctx);
}
/*******************************************************************/
/* Level 1 */
/*******************************************************************/
GenericAgentConfig *CheckOpts(EvalContext *ctx, int argc, char **argv)
{
extern char *optarg;
int optindex = 0;
int c;
GenericAgentConfig *config = GenericAgentConfigNewDefault(AGENT_TYPE_COMMON);
while ((c = getopt_long(argc, argv, "dvnIf:D:N:VSrxMb:i:p:s:cg:hW:lC::", OPTIONS, &optindex)) != EOF)
{
switch ((char) c)
{
case 'l':
LEGACY_OUTPUT = true;
break;
case 'c':
config->check_runnable = true;
break;
case 'f':
if (optarg && (strlen(optarg) < 5))
{
Log(LOG_LEVEL_ERR, " -f used but argument '%s' incorrect", optarg);
exit(EXIT_FAILURE);
}
GenericAgentConfigSetInputFile(config, GetWorkDir(), optarg);
MINUSF = true;
break;
case 'd':
LogSetGlobalLevel(LOG_LEVEL_DEBUG);
break;
case 'b':
if (optarg)
{
Rlist *bundlesequence = RlistFromSplitString(optarg, ',');
GenericAgentConfigSetBundleSequence(config, bundlesequence);
RlistDestroy(bundlesequence);
}
break;
case 'p':
if (strcmp("none", optarg) == 0)
{
config->agent_specific.common.policy_output_format = GENERIC_AGENT_CONFIG_COMMON_POLICY_OUTPUT_FORMAT_NONE;
}
else if (strcmp("cf", optarg) == 0)
{
config->agent_specific.common.policy_output_format = GENERIC_AGENT_CONFIG_COMMON_POLICY_OUTPUT_FORMAT_CF;
}
else if (strcmp("json", optarg) == 0)
{
config->agent_specific.common.policy_output_format = GENERIC_AGENT_CONFIG_COMMON_POLICY_OUTPUT_FORMAT_JSON;
}
else
{
Log(LOG_LEVEL_ERR, "Invalid policy output format: '%s'. Possible values are 'none', 'cf', 'json'", optarg);
exit(EXIT_FAILURE);
}
break;
case 's':
if (strcmp("none", optarg) == 0)
{
break;
}
else if (strcmp("json", optarg) == 0)
{
JsonElement *json_syntax = SyntaxToJson();
Writer *out = FileWriter(stdout);
JsonElementPrint(out, json_syntax, 0);
FileWriterDetach(out);
JsonElementDestroy(json_syntax);
exit(EXIT_SUCCESS);
}
else
{
Log(LOG_LEVEL_ERR, "Invalid syntax description output format: '%s'. Possible values are 'none', 'json'", optarg);
exit(EXIT_FAILURE);
}
break;
case 'K':
IGNORELOCK = true;
break;
case 'D':
config->heap_soft = StringSetFromString(optarg, ',');
break;
case 'N':
config->heap_negated = StringSetFromString(optarg, ',');
break;
case 'I':
LogSetGlobalLevel(LOG_LEVEL_INFO);
break;
case 'v':
LogSetGlobalLevel(LOG_LEVEL_VERBOSE);
break;
case 'n':
DONTDO = true;
IGNORELOCK = true;
LOOKUP = true;
EvalContextHeapAddHard(ctx, "opt_dry_run");
break;
case 'V':
PrintVersion();
exit(0);
case 'h':
PrintHelp("cf-promises", OPTIONS, HINTS, true);
exit(0);
case 'M':
{
Writer *out = FileWriter(stdout);
ManPageWrite(out, "cf-promises", time(NULL),
CF_PROMISES_SHORT_DESCRIPTION,
CF_PROMISES_MANPAGE_LONG_DESCRIPTION,
OPTIONS, HINTS,
true);
FileWriterDetach(out);
exit(EXIT_SUCCESS);
}
case 'r':
SHOWREPORTS = true;
break;
case 'W':
if (!GenericAgentConfigParseWarningOptions(config, optarg))
{
Log(LOG_LEVEL_ERR, "Error parsing warning option");
exit(EXIT_FAILURE);
}
break;
case 'x':
Log(LOG_LEVEL_ERR, "Self-diagnostic functionality is retired.");
exit(0);
case 'C':
if (!GenericAgentConfigParseColor(config, optarg))
{
exit(EXIT_FAILURE);
}
break;
default:
PrintHelp("cf-promises", OPTIONS, HINTS, true);
exit(1);
}
}
if (!GenericAgentConfigParseArguments(config, argc - optind, argv + optind))
{
Log(LOG_LEVEL_ERR, "Too many arguments");
exit(EXIT_FAILURE);
}
return config;
}