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 #ifndef CFENGINE_GENERIC_AGENT_H
26 #define CFENGINE_GENERIC_AGENT_H
27 
28 #include <cf3.defs.h>
29 
30 #include <policy.h>
31 #include <set.h>
32 
33 #define GENERIC_AGENT_CHECKSUM_SIZE ((2*CF_SHA1_LEN) + 1)
34 #define GENERIC_AGENT_CHECKSUM_METHOD HASH_METHOD_SHA1
35 
36 enum generic_agent_config_common_policy_output_format
37 {
38     GENERIC_AGENT_CONFIG_COMMON_POLICY_OUTPUT_FORMAT_NONE,
39     GENERIC_AGENT_CONFIG_COMMON_POLICY_OUTPUT_FORMAT_CF,
40     GENERIC_AGENT_CONFIG_COMMON_POLICY_OUTPUT_FORMAT_JSON,
41     GENERIC_AGENT_CONFIG_COMMON_POLICY_OUTPUT_FORMAT_CF_FULL,
42     GENERIC_AGENT_CONFIG_COMMON_POLICY_OUTPUT_FORMAT_JSON_FULL
43 };
44 
45 typedef struct
46 {
47     AgentType agent_type;
48 
49     Rlist *bundlesequence;
50 
51     char *original_input_file;
52     char *input_file;
53     char *input_dir;
54     char *tag_release_dir;
55 
56     bool check_not_writable_by_others;
57     bool check_runnable;
58 
59     StringSet *heap_soft;
60     StringSet *heap_negated;
61     bool ignore_locks;
62 
63     bool tty_interactive; // agent is running interactively, via tty/terminal interface
64     bool color;
65 
66     ProtocolVersion protocol_version;
67 
68     // agent state
69     bool ignore_missing_bundles;
70     bool ignore_missing_inputs;
71     bool ignore_preferred_augments; // --ignore-preferred-augments
72 
73     struct
74     {
75         struct
76         {
77             enum generic_agent_config_common_policy_output_format policy_output_format;
78             unsigned int parser_warnings;
79             unsigned int parser_warnings_error;
80             bool eval_functions;
81             char *show_classes;
82             char *show_variables;
83         } common;
84         struct
85         {
86             char *bootstrap_argument;
87             char *bootstrap_host;
88             char *bootstrap_port;
89             char *bootstrap_ip;
90             bool bootstrap_trust_server;
91             bool bootstrap_trigger_policy;
92             char *show_evaluated_classes;
93             char *show_evaluated_variables;
94 
95             // BODY AGENT CONTROL
96             bool report_class_log;
97         } agent;
98         struct
99         {
100             /* Time of the last validated_at timestamp seen. */
101             time_t last_validated_at;
102         } daemon;                                     /* execd, serverd etc */
103     } agent_specific;
104 
105 } GenericAgentConfig;
106 
107 ENTERPRISE_VOID_FUNC_2ARG_DECLARE(void, GenericAgentSetDefaultDigest, HashMethod *, digest, int *, digest_len);
108 const char *GenericAgentResolveInputPath(const GenericAgentConfig *config, const char *input_file);
109 void MarkAsPolicyServer(EvalContext *ctx);
110 void GenericAgentDiscoverContext(EvalContext *ctx, GenericAgentConfig *config, const char *program_name);
111 bool GenericAgentCheckPolicy(GenericAgentConfig *config, bool force_validation, bool write_validated_file);
112 
113 ENTERPRISE_VOID_FUNC_1ARG_DECLARE(void, GenericAgentAddEditionClasses, EvalContext *, ctx);
114 void GenericAgentInitialize(EvalContext *ctx, GenericAgentConfig *config);
115 void GenericAgentFinalize(EvalContext *ctx, GenericAgentConfig *config);
116 ENTERPRISE_VOID_FUNC_1ARG_DECLARE(void, GenericAgentWriteVersion, Writer *, w);
117 bool GenericAgentArePromisesValid(const GenericAgentConfig *config);
118 time_t ReadTimestampFromPolicyValidatedFile(const GenericAgentConfig *config, const char *maybe_dirname);
119 
120 bool GenericAgentIsPolicyReloadNeeded(const GenericAgentConfig *config);
121 
122 void CloseLog(void);
123 Seq *ControlBodyConstraints(const Policy *policy, AgentType agent);
124 
125 void SetFacility(const char *retval);
126 void CheckBundleParameters(char *scope, Rlist *args);
127 void WritePID(char *filename);
128 pid_t ReadPID(char *filename);
129 
130 bool GenericAgentConfigParseArguments(GenericAgentConfig *config, int argc, char **argv);
131 bool GenericAgentConfigParseWarningOptions(GenericAgentConfig *config, const char *warning_options);
132 bool GenericAgentConfigParseColor(GenericAgentConfig *config, const char *mode);
133 
134 Policy *SelectAndLoadPolicy(GenericAgentConfig *config, EvalContext *ctx, bool validate_policy, bool write_validated_file);
135 GenericAgentConfig *GenericAgentConfigNewDefault(AgentType agent_type, bool tty_interactive);
136 bool GetTTYInteractive(void);
137 void GenericAgentConfigDestroy(GenericAgentConfig *config);
138 void GenericAgentConfigApply(EvalContext *ctx, const GenericAgentConfig *config);
139 
140 bool CheckAndGenerateFailsafe(const char *inputdir, const char *input_file);
141 void GenericAgentConfigSetInputFile(GenericAgentConfig *config, const char *inputdir, const char *input_file);
142 void GenericAgentConfigSetBundleSequence(GenericAgentConfig *config, const Rlist *bundlesequence);
143 bool GenericAgentTagReleaseDirectory(const GenericAgentConfig *config, const char *dirname, bool write_validated, bool write_release);
144 
145 void GetReleaseIdFile(const char *base_path, char *filename, size_t max_size);
146 
147 bool GenericAgentPostLoadInit(const EvalContext *ctx);
148 
149 void SetupSignalsForAgent(void);
150 
151 void LoadAugments(EvalContext *ctx, GenericAgentConfig *config);
152 
153 void GenericAgentShowContextsFormatted(EvalContext *ctx, const char *regexp);
154 void GenericAgentShowVariablesFormatted(EvalContext *ctx, const char *regexp);
155 
156 #endif
157