File: policy.h

package info (click to toggle)
cfengine3 3.24.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 37,552 kB
  • sloc: ansic: 163,161; sh: 10,296; python: 2,950; makefile: 1,744; lex: 784; yacc: 633; perl: 211; pascal: 157; xml: 21; sed: 13
file content (240 lines) | stat: -rw-r--r-- 7,826 bytes parent folder | download | duplicates (2)
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
/*
  Copyright 2024 Northern.tech AS

  This file is part of CFEngine 3 - written and maintained by Northern.tech 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 Commercial Open Source License
  (COSL) may apply to this file if you as a licensee so wish it. See
  included file COSL.txt.
*/

#ifndef CFENGINE_POLICY_H
#define CFENGINE_POLICY_H

#include <cf3.defs.h>

#include <writer.h>
#include <sequence.h>
#include <json.h>
#include <set.h>

typedef enum
{
    POLICY_ELEMENT_TYPE_POLICY,
    POLICY_ELEMENT_TYPE_BUNDLE,
    POLICY_ELEMENT_TYPE_BODY,
    POLICY_ELEMENT_TYPE_BUNDLE_SECTION,
    POLICY_ELEMENT_TYPE_PROMISE,
    POLICY_ELEMENT_TYPE_CONSTRAINT
} PolicyElementType;

typedef struct
{
    PolicyElementType type;
    const void *subject;
    char *message;
} PolicyError;

struct Policy_
{
    char *release_id;

    Seq *bundles;
    Seq *bodies;
    Seq *custom_promise_types;
    StringMap *policy_files_hashes;
};

typedef struct
{
    size_t start;
    size_t end;
    size_t line;
    size_t context;
} SourceOffset;

struct Bundle_
{
    Policy *parent_policy;

    char *type;
    char *name;
    char *ns;
    Rlist *args;

    Seq *sections;
    Seq *custom_sections;

    char *source_path;
    SourceOffset offset;
};

struct Body_
{
    Policy *parent_policy;

    char *type;
    char *name;
    char *ns;
    Rlist *args;

    Seq *conlist;
    bool is_custom;

    char *source_path;
    SourceOffset offset;
};

struct BundleSection_
{
    Bundle *parent_bundle;

    char *promise_type;
    Seq *promises;

    SourceOffset offset;
};

struct Promise_
{
    BundleSection *parent_section;

    char *classes;
    char *comment;
    char *promiser;
    Rval promisee;
    Seq *conlist;

    const Promise *org_pp;            /* A ptr to the unexpanded raw promise */

    SourceOffset offset;
};

struct Constraint_
{
    PolicyElementType type;
    union {
        Promise *promise;
        Body *body;
    } parent;

    char *lval;
    Rval rval;

    char *classes;
    bool references_body;

    SourceOffset offset;
};

const char *NamespaceDefault(void);

Policy *PolicyNew(void);
int PolicyCompare(const void *a, const void *b);
void PolicyDestroy(Policy *policy);
unsigned PolicyHash(const Policy *policy);

StringSet *PolicySourceFiles(const Policy *policy);
const char *PolicyGetPolicyFileHash(const Policy *policy, const char *policy_file_path);

Policy *PolicyMerge(Policy *a, Policy *b);
Body *PolicyGetBody(const Policy *policy, const char *ns, const char *type, const char *name);
Bundle *PolicyGetBundle(const Policy *policy, const char *ns, const char *type, const char *name);
bool PolicyIsRunnable(const Policy *policy);
const Policy *PolicyFromPromise(const Promise *promise);
char *BundleQualifiedName(const Bundle *bundle);

PolicyError *PolicyErrorNew(PolicyElementType type, const void *subject, const char *error_msg, ...);
void PolicyErrorDestroy(PolicyError *error);
void PolicyErrorWrite(Writer *writer, const PolicyError *error);

bool PolicyCheckPartial(const Policy *policy, Seq *errors);
bool PolicyCheckRunnable(const EvalContext *ctx, const Policy *policy, Seq *errors);

Bundle *PolicyAppendBundle(Policy *policy, const char *ns, const char *name, const char *type, const Rlist *args, const char *source_path);
Body *PolicyAppendBody(Policy *policy, const char *ns, const char *name,
                       const char *type, Rlist *args, const char *source_path,
                       bool is_custom);
Body *PolicyAppendPromiseBlock(Policy *policy, const char *ns, const char *name, const char *type, Rlist *args, const char *source_path);

JsonElement *PolicyToJson(const Policy *policy);
JsonElement *BundleToJson(const Bundle *bundle);
JsonElement *BodyToJson(const Body *body);
Policy *PolicyFromJson(JsonElement *json_policy);
void PolicyToString(const Policy *policy, Writer *writer);

BundleSection *BundleAppendSection(Bundle *bundle, const char *promise_type);
const BundleSection *BundleGetSection(const Bundle *bp, const char *promise_type);

Constraint *BodyAppendConstraint(Body *body, const char *lval, Rval rval, const char *classes, bool references_body);
Seq *BodyGetConstraint(Body *body, const char *lval);
bool BodyHasConstraint(const Body *body, const char *lval);

const char *ConstraintGetNamespace(const Constraint *cp);

Promise *BundleSectionAppendPromise(BundleSection *section, const char *promiser, Rval promisee, const char *classes, const char *varclasses);
void BundleSectionDestroy(BundleSection *section);

void PromiseDestroy(Promise *pp);

Constraint *PromiseAppendConstraint(Promise *promise, const char *lval, Rval rval, bool references_body);

const char *PromiseGetNamespace(const Promise *pp);
const Bundle *PromiseGetBundle(const Promise *pp);
const Policy *PromiseGetPolicy(const Promise *pp);

static inline const char *PromiseGetPromiseType(const Promise *pp)
{
    assert(pp != NULL);
    return pp->parent_section->promise_type;
}

void PromisePath(Writer *w, const Promise *pp);
const char *PromiseGetHandle(const Promise *pp);
int PromiseGetConstraintAsInt(const EvalContext *ctx, const char *lval, const Promise *pp);
bool PromiseGetConstraintAsReal(const EvalContext *ctx, const char *lval, const Promise *list, double *value_out);
mode_t PromiseGetConstraintAsOctal(const EvalContext *ctx, const char *lval, const Promise *list);
uid_t PromiseGetConstraintAsUid(const EvalContext *ctx, const char *lval, const Promise *pp);
gid_t PromiseGetConstraintAsGid(const EvalContext *ctx, char *lval, const Promise *pp);
Rlist *PromiseGetConstraintAsList(const EvalContext *ctx, const char *lval, const Promise *pp);
int PromiseGetConstraintAsBoolean(const EvalContext *ctx, const char *lval, const Promise *list);
int PromiseGetConstraintAsBooleanWithDefault(const EvalContext *ctx, const char *lval, const Promise *pp,
                                             int default_val, bool with_warning);
Constraint *PromiseGetConstraintWithType(const Promise *promise, const char *lval, RvalType type);
Constraint *PromiseGetImmediateConstraint(const Promise *promise, const char *lval);
void *PromiseGetConstraintAsRval(const Promise *promise, const char *lval, RvalType type);
Constraint *PromiseGetConstraint(const Promise *promise, const char *lval);

bool PromiseBundleOrBodyConstraintExists(const EvalContext *ctx, const char *lval, const Promise *pp);

void PromiseRecheckAllConstraints(const EvalContext *ctx, const Promise *pp);

void ConstraintDestroy(Constraint *cp);
int ConstraintsGetAsBoolean(const EvalContext *ctx, const char *lval, const Seq *constraints);
const char *ConstraintContext(const Constraint *cp);
Constraint *EffectiveConstraint(const EvalContext *ctx, Seq *constraints);

void *PromiseGetImmediateRvalValue(const char *lval, const Promise *pp, RvalType rtype);

char *QualifiedNameNamespaceComponent(const char *qualified_name);
char *QualifiedNameScopeComponent(const char *qualified_name);
bool BundleTypeCheck(const char *name);
Rval DefaultBundleConstraint(const Promise *pp, char *promisetype);

bool PolicyHasCustomPromiseType(const Policy *policy, const char *name);

#endif