File: verify_acl.c

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 (465 lines) | stat: -rw-r--r-- 12,186 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
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/*
  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.
*/

#include <verify_acl.h>

#include <actuator.h>
#include <acl_posix.h>
#include <files_names.h>
#include <promises.h>
#include <string_lib.h>
#include <rlist.h>
#include <eval_context.h>
#include <cf-agent-enterprise-stubs.h>
#include <cf-agent-windows-functions.h>

// Valid operations (first char of mode)
#define CF_VALID_OPS_METHOD_OVERWRITE "=+-"
#define CF_VALID_OPS_METHOD_APPEND "=+-"

static bool CheckACLSyntax(const char *file, Acl acl, const Promise *pp);

static void SetACLDefaults(const char *path, Acl *acl);
static bool CheckACESyntax(char *ace, char *valid_nperms, char *valid_ops, int deny_support, int mask_support,
                        const Promise *pp);
static bool CheckModeSyntax(char **mode_p, char *valid_nperms, char *valid_ops, const Promise *pp);
static bool CheckPermTypeSyntax(char *permt, int deny_support, const Promise *pp);
static int CheckAclDefault(const char *path, Acl *acl, const Promise *pp);


PromiseResult VerifyACL(EvalContext *ctx, const char *file, const Attributes *attr, const Promise *pp)
{
    assert(attr != NULL);
    Attributes a = *attr; // TODO: Remove this local copy
    if (!CheckACLSyntax(file, a.acl, pp))
    {
        RecordFailure(ctx, pp, attr, "Syntax error in access control list for '%s'", file);
        PromiseRef(LOG_LEVEL_ERR, pp);
        return PROMISE_RESULT_FAIL;
    }

    SetACLDefaults(file, &a.acl);

    PromiseResult result = PROMISE_RESULT_NOOP;

// decide which ACL API to use
    switch (a.acl.acl_type)
    {
    case ACL_TYPE_NONE: // fallthrough: acl_type defaults to generic
    case ACL_TYPE_GENERIC:

#if defined(__linux__)
        result = PromiseResultUpdate(result, CheckPosixLinuxACL(ctx, file, a.acl, &a, pp));
#elif defined(__MINGW32__)
        result = PromiseResultUpdate(result, Nova_CheckNtACL(ctx, file, a.acl, &a, pp));
#else
        RecordFailure(ctx, pp, attr, "ACLs are not yet supported on this system.");
        result = PromiseResultUpdate(result, PROMISE_RESULT_FAIL);
#endif
        break;

    case ACL_TYPE_POSIX:

#if defined(__linux__)
        result = PromiseResultUpdate(result, CheckPosixLinuxACL(ctx, file, a.acl, &a, pp));
#else
        RecordFailure(ctx, pp, attr, "Posix ACLs are not supported on this system");
        result = PromiseResultUpdate(result, PROMISE_RESULT_FAIL);
#endif
        break;

    case ACL_TYPE_NTFS_:
#ifdef __MINGW32__
        result = PromiseResultUpdate(result, Nova_CheckNtACL(ctx, file, a.acl, &a, pp));
#else
        RecordFailure(ctx, pp, attr, "NTFS ACLs are not supported on this system");
        result = PromiseResultUpdate(result, PROMISE_RESULT_FAIL);
#endif
        break;

    default:
        assert(false);
        RecordFailure(ctx, pp, attr, "Unknown ACL type - software error");
        break;
    }

    return result;
}

static bool CheckACLSyntax(const char *file, Acl acl, const Promise *pp)
{
    bool valid = true;
    int deny_support = false;
    int mask_support = false;
    char *valid_ops = NULL;
    char *valid_nperms = NULL;
    Rlist *rp;

// set unset fields to defautls
    SetACLDefaults(file, &acl);

// find valid values for op

    switch (acl.acl_method)
    {
    case ACL_METHOD_OVERWRITE:
        valid_ops = CF_VALID_OPS_METHOD_OVERWRITE;
        break;

    case ACL_METHOD_APPEND:
        valid_ops = CF_VALID_OPS_METHOD_APPEND;
        break;

    default:
        // never executed: should be set to a default value by now
        break;
    }

    switch (acl.acl_type)
    {
    case ACL_TYPE_GENERIC:        // generic ACL type: cannot include native or deny-type permissions
        valid_nperms = "";
        deny_support = false;
        mask_support = false;
        break;

    case ACL_TYPE_POSIX:
        valid_nperms = CF_VALID_NPERMS_POSIX;
        deny_support = false;   // posix does not support deny-type permissions
        mask_support = true;    // mask-ACE is allowed in POSIX
        break;

    case ACL_TYPE_NTFS_:
        valid_nperms = CF_VALID_NPERMS_NTFS;
        deny_support = true;
        mask_support = false;
        break;

    default:
        // never executed: should be set to a default value by now
        break;
    }

// check that acl_default is set to a valid value

    if (!CheckAclDefault(file, &acl, pp))
    {
        return false;
    }

    for (rp = acl.acl_entries; rp != NULL; rp = rp->next)
    {
        valid = CheckACESyntax(RlistScalarValue(rp), valid_ops, valid_nperms, deny_support, mask_support, pp);

        if (!valid)             // wrong syntax in this ace
        {
            Log(LOG_LEVEL_ERR, "ACL: The ACE '%s' contains errors", RlistScalarValue(rp));
            PromiseRef(LOG_LEVEL_ERR, pp);
            break;
        }
    }

    for (rp = acl.acl_default_entries; rp != NULL; rp = rp->next)
    {
        valid = CheckACESyntax(RlistScalarValue(rp), valid_ops, valid_nperms, deny_support, mask_support, pp);

        if (!valid)             // wrong syntax in this ace
        {
            Log(LOG_LEVEL_ERR, "ACL: The ACE '%s' contains errors", RlistScalarValue(rp));
            PromiseRef(LOG_LEVEL_ERR, pp);
            break;
        }
    }

    return valid;
}

/**
 * Set unset fields with documented defaults, to these defaults.
 **/

static void SetACLDefaults(const char *path, Acl *acl)
{
// default: acl_method => append

    if (acl->acl_method == ACL_METHOD_NONE)
    {
        acl->acl_method = ACL_METHOD_APPEND;
    }

// default: acl_type => generic

    if (acl->acl_type == ACL_TYPE_NONE)
    {
        acl->acl_type = ACL_TYPE_GENERIC;
    }

// default on directories: acl_default => nochange

    if ((acl->acl_default == ACL_DEFAULT_NONE) &&
        (IsDir(ToChangesPath(path))))
    {
        acl->acl_default = ACL_DEFAULT_NO_CHANGE;
    }
}

static int CheckAclDefault(const char *path, Acl *acl, const Promise *pp)
/*
  Checks that acl_default is set to a valid value for this acl type.
  Returns true if so, or false otherwise.
*/
{
    int valid = false;

    switch (acl->acl_default)
    {
    case ACL_DEFAULT_NONE:      // unset is always valid
        valid = true;

        break;

    case ACL_DEFAULT_SPECIFY:        // NOTE: we assume all acls support specify

        // fallthrough
    case ACL_DEFAULT_ACCESS:

        // fallthrough
    default:

        if (IsDir(path))
        {
            valid = true;
        }
        else
        {
            Log(LOG_LEVEL_ERR, "acl_default can only be set on directories.");
            PromiseRef(LOG_LEVEL_ERR, pp);
            valid = false;
        }

        break;
    }

    return valid;
}

static bool CheckACESyntax(
    char *ace,
    char *valid_ops,
    char *valid_nperms,
    int deny_support,
    int mask_support,
    const Promise *pp)
{
    char *str = ace;
    bool chkid = false;

// first element must be "user", "group" or "all"

    if (strncmp(str, "user:", 5) == 0)
    {
        str += 5;
        chkid = true;
    }
    else if (strncmp(str, "group:", 6) == 0)
    {
        str += 6;
        chkid = true;
    }
    else if (strncmp(str, "all:", 4) == 0)
    {
        str += 4;
        chkid = false;
    }
    else if (strncmp(str, "mask:", 5) == 0)
    {

        if (mask_support)
        {
            str += 5;
            chkid = false;
        }
        else
        {
            Log(LOG_LEVEL_ERR, "This ACL type does not support mask ACE.");
            PromiseRef(LOG_LEVEL_ERR, pp);
            return false;
        }

    }
    else
    {
        Log(LOG_LEVEL_ERR, "ACL: ACE '%s' does not start with user:/group:/all", ace);
        PromiseRef(LOG_LEVEL_ERR, pp);
        return false;
    }

    if (chkid)                  // look for following "id:"
    {
        if (*str == ':')
        {
            Log(LOG_LEVEL_ERR, "ACL: ACE '%s': id cannot be empty or contain ':'", ace);
            return false;
        }

        // skip id-string (no check: allow any id-string)

        while (true)
        {
            str++;
            if (*str == ':')
            {
                str++;
                break;
            }
            else if (*str == '\0')
            {
                Log(LOG_LEVEL_ERR, "ACL: Nothing following id string in ACE '%s'", ace);
                return false;
            }
        }
    }

// check the mode-string (also skips to next field)
    const bool valid_mode = CheckModeSyntax(&str, valid_ops, valid_nperms, pp);

    if (!valid_mode)
    {
        Log(LOG_LEVEL_ERR, "ACL: Malformed mode-string in ACE '%s'", ace);
        PromiseRef(LOG_LEVEL_ERR, pp);
        return false;
    }

    if (*str == '\0')           // mode was the last field
    {
        return true;
    }

    str++;

// last field; must be a perm_type field
    const bool valid_permt = CheckPermTypeSyntax(str, deny_support, pp);

    if (!valid_permt)
    {
        Log(LOG_LEVEL_ERR, "ACL: Malformed perm_type syntax in ACE '%s'", ace);
        return false;
    }

    return true;
}

static bool CheckModeSyntax(char **mode_p, char *valid_ops, char *valid_nperms, const Promise *pp)
/*
  Checks the syntax of a ':' or NULL terminated mode string.
  Moves the string pointer to the character following the mode
  (i.e. ':' or '\0')
*/
{
    char *mode = *mode_p;
    bool valid = false;

// mode is allowed to be empty

    if ((*mode == '\0') || (*mode == ':'))
    {
        return true;
    }

    while (true)
    {
        mode = ScanPastChars(valid_ops, mode);

        mode = ScanPastChars(CF_VALID_GPERMS, mode);

        if (*mode == CF_NATIVE_PERMS_SEP_START)
        {
            mode++;

            mode = ScanPastChars(valid_nperms, mode);

            if (*mode == CF_NATIVE_PERMS_SEP_END)
            {
                mode++;
            }
            else
            {
                Log(LOG_LEVEL_ERR, "ACL: Invalid native permission '%c', or missing native end separator", *mode);
                PromiseRef(LOG_LEVEL_ERR, pp);
                valid = false;
                break;
            }
        }

        if ((*mode == '\0') || (*mode == ':'))      // end of mode-string
        {
            valid = true;
            break;
        }
        else if (*mode == ',')  // one more iteration
        {
            mode++;
        }
        else
        {
            Log(LOG_LEVEL_ERR, "ACL: Mode string contains invalid characters");
            PromiseRef(LOG_LEVEL_ERR, pp);
            valid = false;
            break;
        }
    }

    *mode_p = mode;             // move pointer to past mode-field

    return valid;
}

static bool CheckPermTypeSyntax(char *permt, int deny_support, const Promise *pp)
/*
  Checks if the given string corresponds to the perm_type syntax.
  Only "allow" or "deny", followed by NULL-termination are valid.
  In addition, "deny" is only valid for ACL types supporting it.
 */
{
    bool valid = false;

    if (strcmp(permt, "allow") == 0)
    {
        valid = true;
    }
    else if (strcmp(permt, "deny") == 0)
    {
        if (deny_support)
        {
            valid = true;
        }
        else
        {
            Log(LOG_LEVEL_ERR, "Deny permission not supported by this ACL type");
            PromiseRef(LOG_LEVEL_ERR, pp);
        }
    }

    return valid;
}