cfengine  3.15.4
About: CFEngine is a configuration management system for configuring and maintaining Unix-like computers (using an own high level policy language). Community version.
  Fossies Dox: cfengine-3.15.4.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

policy.h
Go to the documentation of this file.
1 /*
2  Copyright 2019 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_POLICY_H
26 #define CFENGINE_POLICY_H
27 
28 #include <cf3.defs.h>
29 
30 #include <writer.h>
31 #include <sequence.h>
32 #include <json.h>
33 #include <set.h>
34 
35 typedef enum
36 {
44 
45 typedef struct
46 {
48  const void *subject;
49  char *message;
50 } PolicyError;
51 
52 struct Policy_
53 {
54  char *release_id;
55 
59 };
60 
61 typedef struct
62 {
63  size_t start;
64  size_t end;
65  size_t line;
66  size_t context;
67 } SourceOffset;
68 
69 struct Bundle_
70 {
72 
73  char *type;
74  char *name;
75  char *ns;
77 
79 
80  char *source_path;
82 };
83 
84 struct Body_
85 {
87 
88  char *type;
89  char *name;
90  char *ns;
92 
94 
95  char *source_path;
97 };
98 
100 {
102 
103  char *name;
105 
107 };
108 
109 struct Promise_
110 {
112 
113  char *classes;
114  char *comment;
115  char *promiser;
118 
119  const Promise *org_pp; /* A ptr to the unexpanded raw promise */
120 
122 };
123 
125 {
127  union {
131 
132  char *lval;
134 
135  char *classes;
137 
139 };
140 
141 const char *NamespaceDefault(void);
142 
143 Policy *PolicyNew(void);
144 int PolicyCompare(const void *a, const void *b);
145 void PolicyDestroy(Policy *policy);
146 unsigned PolicyHash(const Policy *policy);
147 
148 StringSet *PolicySourceFiles(const Policy *policy);
149 const char *PolicyGetPolicyFileHash(const Policy *policy, const char *policy_file_path);
150 
152 Body *PolicyGetBody(const Policy *policy, const char *ns, const char *type, const char *name);
153 Bundle *PolicyGetBundle(const Policy *policy, const char *ns, const char *type, const char *name);
154 bool PolicyIsRunnable(const Policy *policy);
155 const Policy *PolicyFromPromise(const Promise *promise);
156 char *BundleQualifiedName(const Bundle *bundle);
157 
158 PolicyError *PolicyErrorNew(PolicyElementType type, const void *subject, const char *error_msg, ...);
159 void PolicyErrorDestroy(PolicyError *error);
160 void PolicyErrorWrite(Writer *writer, const PolicyError *error);
161 
162 bool PolicyCheckPartial(const Policy *policy, Seq *errors);
163 bool PolicyCheckRunnable(const EvalContext *ctx, const Policy *policy, Seq *errors, bool ignore_missing_bundles);
164 
165 Bundle *PolicyAppendBundle(Policy *policy, const char *ns, const char *name, const char *type, const Rlist *args, const char *source_path);
166 Body *PolicyAppendBody(Policy *policy, const char *ns, const char *name, const char *type, Rlist *args, const char *source_path);
167 
168 JsonElement *PolicyToJson(const Policy *policy);
169 JsonElement *BundleToJson(const Bundle *bundle);
170 JsonElement *BodyToJson(const Body *body);
171 Policy *PolicyFromJson(JsonElement *json_policy);
172 void PolicyToString(const Policy *policy, Writer *writer);
173 
174 PromiseType *BundleAppendPromiseType(Bundle *bundle, const char *name);
175 const PromiseType *BundleGetPromiseType(const Bundle *bp, const char *name);
176 
177 Constraint *BodyAppendConstraint(Body *body, const char *lval, Rval rval, const char *classes, bool references_body);
178 Seq *BodyGetConstraint(Body *body, const char *lval);
179 bool BodyHasConstraint(const Body *body, const char *lval);
180 
181 const char *ConstraintGetNamespace(const Constraint *cp);
182 
183 Promise *PromiseTypeAppendPromise(PromiseType *type, const char *promiser, Rval promisee, const char *classes, const char *varclasses);
184 void PromiseTypeDestroy(PromiseType *promise_type);
185 
186 void PromiseDestroy(Promise *pp);
187 
188 Constraint *PromiseAppendConstraint(Promise *promise, const char *lval, Rval rval, bool references_body);
189 
190 const char *PromiseGetNamespace(const Promise *pp);
191 const Bundle *PromiseGetBundle(const Promise *pp);
192 const Policy *PromiseGetPolicy(const Promise *pp);
193 
194 void PromisePath(Writer *w, const Promise *pp);
195 const char *PromiseGetHandle(const Promise *pp);
196 int PromiseGetConstraintAsInt(const EvalContext *ctx, const char *lval, const Promise *pp);
197 bool PromiseGetConstraintAsReal(const EvalContext *ctx, const char *lval, const Promise *list, double *value_out);
198 mode_t PromiseGetConstraintAsOctal(const EvalContext *ctx, const char *lval, const Promise *list);
199 uid_t PromiseGetConstraintAsUid(const EvalContext *ctx, const char *lval, const Promise *pp);
200 gid_t PromiseGetConstraintAsGid(const EvalContext *ctx, char *lval, const Promise *pp);
201 Rlist *PromiseGetConstraintAsList(const EvalContext *ctx, const char *lval, const Promise *pp);
202 int PromiseGetConstraintAsBoolean(const EvalContext *ctx, const char *lval, const Promise *list);
203 Constraint *PromiseGetConstraintWithType(const Promise *promise, const char *lval, RvalType type);
204 Constraint *PromiseGetImmediateConstraint(const Promise *promise, const char *lval);
205 void *PromiseGetConstraintAsRval(const Promise *promise, const char *lval, RvalType type);
206 Constraint *PromiseGetConstraint(const Promise *promise, const char *lval);
207 
208 bool PromiseBundleOrBodyConstraintExists(const EvalContext *ctx, const char *lval, const Promise *pp);
209 
210 void PromiseRecheckAllConstraints(const EvalContext *ctx, const Promise *pp);
211 
212 void ConstraintDestroy(Constraint *cp);
213 int ConstraintsGetAsBoolean(const EvalContext *ctx, const char *lval, const Seq *constraints);
214 const char *ConstraintContext(const Constraint *cp);
215 Constraint *EffectiveConstraint(const EvalContext *ctx, Seq *constraints);
216 
217 void *PromiseGetImmediateRvalValue(const char *lval, const Promise *pp, RvalType rtype);
218 
219 char *QualifiedNameNamespaceComponent(const char *qualified_name);
220 char *QualifiedNameScopeComponent(const char *qualified_name);
221 bool BundleTypeCheck(const char *name);
222 Rval DefaultBundleConstraint(const Promise *pp, char *promisetype);
223 
224 #endif
RvalType
Definition: cf3.defs.h:605
const char * PromiseGetNamespace(const Promise *pp)
Definition: policy.c:2666
bool PolicyCheckPartial(const Policy *policy, Seq *errors)
Check a partial policy DOM for errors.
Definition: policy.c:1072
bool BodyHasConstraint(const Body *body, const char *lval)
Definition: policy.c:2405
const char * ConstraintGetNamespace(const Constraint *cp)
Definition: policy.c:455
Policy * PolicyFromJson(JsonElement *json_policy)
Deserialize a policy from JSON.
Definition: policy.c:2351
Constraint * PromiseGetImmediateConstraint(const Promise *promise, const char *lval)
Get the first constraint from the promise.
Definition: policy.c:2992
bool BundleTypeCheck(const char *name)
Check whether the promise type is allowed one.
Definition: policy.c:3223
JsonElement * PolicyToJson(const Policy *policy)
Serialize a policy as JSON.
Definition: policy.c:1961
const Policy * PolicyFromPromise(const Promise *promise)
Convenience function to get the policy object associated with a promise.
Definition: policy.c:477
void PolicyDestroy(Policy *policy)
Definition: policy.c:121
Rlist * PromiseGetConstraintAsList(const EvalContext *ctx, const char *lval, const Promise *pp)
Get the Rlist value of the first effective constraint found matching, from a promise.
Definition: policy.c:2920
const char * PromiseGetHandle(const Promise *pp)
Return handle of the promise.
Definition: policy.c:2713
unsigned PolicyHash(const Policy *policy)
Definition: policy.c:219
Seq * BodyGetConstraint(Body *body, const char *lval)
A sequence of constraints matching the l-value.
Definition: policy.c:2389
PolicyError * PolicyErrorNew(PolicyElementType type, const void *subject, const char *error_msg,...)
Definition: policy.c:1144
uid_t PromiseGetConstraintAsUid(const EvalContext *ctx, const char *lval, const Promise *pp)
Get the uid value of the first effective constraint found matching, from a promise.
Definition: policy.c:2845
const char * ConstraintContext(const Constraint *cp)
Get the context of the given constraint.
Definition: policy.c:2424
int PromiseGetConstraintAsInt(const EvalContext *ctx, const char *lval, const Promise *pp)
Get the int value of the first effective constraint found matching, from a promise.
Definition: policy.c:2724
Constraint * PromiseGetConstraintWithType(const Promise *promise, const char *lval, RvalType type)
Definition: policy.c:2964
Bundle * PolicyGetBundle(const Policy *policy, const char *ns, const char *type, const char *name)
Query a policy for a bundle.
Definition: policy.c:339
Body * PolicyAppendBody(Policy *policy, const char *ns, const char *name, const char *type, Rlist *args, const char *source_path)
Definition: policy.c:1348
void PromiseTypeDestroy(PromiseType *promise_type)
Definition: policy.c:1315
const Policy * PromiseGetPolicy(const Promise *pp)
Definition: policy.c:2676
Rval DefaultBundleConstraint(const Promise *pp, char *promisetype)
Return a default bundle name for this method/service.
Definition: policy.c:84
Bundle * PolicyAppendBundle(Policy *policy, const char *ns, const char *name, const char *type, const Rlist *args, const char *source_path)
Definition: policy.c:1326
const Bundle * PromiseGetBundle(const Promise *pp)
Definition: policy.c:2671
JsonElement * BundleToJson(const Bundle *bundle)
Serialize a bundle as JSON.
Definition: policy.c:1872
PromiseType * BundleAppendPromiseType(Bundle *bundle, const char *name)
Definition: policy.c:1376
PolicyElementType
Definition: policy.h:36
@ POLICY_ELEMENT_TYPE_BODY
Definition: policy.h:39
@ POLICY_ELEMENT_TYPE_BUNDLE
Definition: policy.h:38
@ POLICY_ELEMENT_TYPE_CONSTRAINT
Definition: policy.h:42
@ POLICY_ELEMENT_TYPE_PROMISE
Definition: policy.h:41
@ POLICY_ELEMENT_TYPE_PROMISE_TYPE
Definition: policy.h:40
@ POLICY_ELEMENT_TYPE_POLICY
Definition: policy.h:37
void PolicyErrorWrite(Writer *writer, const PolicyError *error)
Definition: policy.c:1276
void PolicyToString(const Policy *policy, Writer *writer)
Pretty-print a policy.
Definition: policy.c:2141
void PolicyErrorDestroy(PolicyError *error)
Definition: policy.c:1161
Constraint * BodyAppendConstraint(Body *body, const char *lval, Rval rval, const char *classes, bool references_body)
Definition: policy.c:1576
mode_t PromiseGetConstraintAsOctal(const EvalContext *ctx, const char *lval, const Promise *list)
Get the octal value of the first effective constraint found matching, from a promise.
Definition: policy.c:2801
bool PolicyIsRunnable(const Policy *policy)
Check to see if a policy is runnable (contains body common control)
Definition: policy.c:372
void * PromiseGetConstraintAsRval(const Promise *promise, const char *lval, RvalType type)
Get the Rval value of the first effective constraint that matches the given type.
Definition: policy.c:3054
void PromiseRecheckAllConstraints(const EvalContext *ctx, const Promise *pp)
Definition: policy.c:3080
Promise * PromiseTypeAppendPromise(PromiseType *type, const char *promiser, Rval promisee, const char *classes, const char *varclasses)
Definition: policy.c:1406
void ConstraintDestroy(Constraint *cp)
Definition: policy.c:2461
Body * PolicyGetBody(const Policy *policy, const char *ns, const char *type, const char *name)
Query a policy for a body.
Definition: policy.c:306
bool PromiseBundleOrBodyConstraintExists(const EvalContext *ctx, const char *lval, const Promise *pp)
Definition: policy.c:2593
Policy * PolicyMerge(Policy *a, Policy *b)
Merge two partial policy objects. The memory for the child objects of the original policies are trans...
Definition: policy.c:385
Constraint * PromiseGetConstraint(const Promise *promise, const char *lval)
Get the first effective constraint from the promise, also does some checking.
Definition: policy.c:2944
bool PolicyCheckRunnable(const EvalContext *ctx, const Policy *policy, Seq *errors, bool ignore_missing_bundles)
Check a runnable policy DOM for errors.
Definition: policy.c:1049
Policy * PolicyNew(void)
Definition: policy.c:100
Constraint * PromiseAppendConstraint(Promise *promise, const char *lval, Rval rval, bool references_body)
Definition: policy.c:1506
char * QualifiedNameScopeComponent(const char *qualified_name)
Definition: policy.c:818
int PromiseGetConstraintAsBoolean(const EvalContext *ctx, const char *lval, const Promise *list)
Get the trinary boolean value of the first effective constraint found matching, from a promise.
Definition: policy.c:2481
void PromisePath(Writer *w, const Promise *pp)
Write a string describing the promise location in policy, e.g. /default/foo/packages/'emacs'.
Definition: policy.c:2699
const char * PolicyGetPolicyFileHash(const Policy *policy, const char *policy_file_path)
Definition: policy.c:275
const PromiseType * BundleGetPromiseType(const Bundle *bp, const char *name)
Definition: policy.c:1600
JsonElement * BodyToJson(const Body *body)
Serialize a body as JSON.
Definition: policy.c:1925
StringSet * PolicySourceFiles(const Policy *policy)
Definition: policy.c:240
char * QualifiedNameNamespaceComponent(const char *qualified_name)
Definition: policy.c:800
gid_t PromiseGetConstraintAsGid(const EvalContext *ctx, char *lval, const Promise *pp)
Get the uid value of the first effective constraint found matching, from a promise.
Definition: policy.c:2887
bool PromiseGetConstraintAsReal(const EvalContext *ctx, const char *lval, const Promise *list, double *value_out)
Get the real value of the first effective constraint found matching, from a promise.
Definition: policy.c:2750
const char * NamespaceDefault(void)
Definition: policy.c:93
char * BundleQualifiedName(const Bundle *bundle)
Definition: policy.c:490
int ConstraintsGetAsBoolean(const EvalContext *ctx, const char *lval, const Seq *constraints)
Get the trinary boolean value of the first effective constraint found matching.
Definition: policy.c:2541
void * PromiseGetImmediateRvalValue(const char *lval, const Promise *pp, RvalType rtype)
Get the Rval value of the first constraint that matches the given type. Checks that this constraint d...
Definition: policy.c:3031
int PolicyCompare(const void *a, const void *b)
Definition: policy.c:114
void PromiseDestroy(Promise *pp)
Definition: policy.c:1471
Constraint * EffectiveConstraint(const EvalContext *ctx, Seq *constraints)
Returns the first effective constraint from a list of candidates, depending on evaluation state.
Definition: policy.c:2445
Definition: policy.h:85
char * type
Definition: policy.h:88
SourceOffset offset
Definition: policy.h:96
char * source_path
Definition: policy.h:95
char * ns
Definition: policy.h:90
Seq * conlist
Definition: policy.h:93
Policy * parent_policy
Definition: policy.h:86
Rlist * args
Definition: policy.h:91
char * name
Definition: policy.h:89
Definition: policy.h:70
char * name
Definition: policy.h:74
char * type
Definition: policy.h:73
SourceOffset offset
Definition: policy.h:81
Seq * promise_types
Definition: policy.h:78
char * ns
Definition: policy.h:75
char * source_path
Definition: policy.h:80
Policy * parent_policy
Definition: policy.h:71
Rlist * args
Definition: policy.h:76
Body * body
Definition: policy.h:129
Rval rval
Definition: policy.h:133
SourceOffset offset
Definition: policy.h:138
Promise * promise
Definition: policy.h:128
char * classes
Definition: policy.h:135
char * lval
Definition: policy.h:132
bool references_body
Definition: policy.h:136
union Constraint_::@22 parent
PolicyElementType type
Definition: policy.h:126
char * message
Definition: policy.h:49
const void * subject
Definition: policy.h:48
PolicyElementType type
Definition: policy.h:47
Definition: policy.h:53
Seq * bundles
Definition: policy.h:56
char * release_id
Definition: policy.h:54
StringMap * policy_files_hashes
Definition: policy.h:58
Seq * bodies
Definition: policy.h:57
Seq * promises
Definition: policy.h:104
SourceOffset offset
Definition: policy.h:106
Bundle * parent_bundle
Definition: policy.h:101
char * name
Definition: policy.h:103
PromiseType * parent_promise_type
Definition: policy.h:111
const Promise * org_pp
Definition: policy.h:119
Seq * conlist
Definition: policy.h:117
char * comment
Definition: policy.h:114
char * promiser
Definition: policy.h:115
char * classes
Definition: policy.h:113
Rval promisee
Definition: policy.h:116
SourceOffset offset
Definition: policy.h:121
Definition: rlist.h:35
Definition: cf3.defs.h:614
Sequence data-structure.
Definition: sequence.h:50
size_t line
Definition: policy.h:65
size_t end
Definition: policy.h:64
size_t start
Definition: policy.h:63
size_t context
Definition: policy.h:66
Definition: map.h:212
Definition: set.h:138
Definition: writer.c:45