Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sudo-project
GitHub Repository: sudo-project/sudo
Path: blob/main/plugins/sudoers/env_pattern.c
1532 views
1
/*
2
* SPDX-License-Identifier: ISC
3
*
4
* Copyright (c) 2017 Todd C. Miller <[email protected]>
5
*
6
* Permission to use, copy, modify, and distribute this software for any
7
* purpose with or without fee is hereby granted, provided that the above
8
* copyright notice and this permission notice appear in all copies.
9
*
10
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
*/
18
19
#include <config.h>
20
21
#include <stdio.h>
22
#include <stdlib.h>
23
#include <string.h>
24
25
#include <sudoers.h>
26
27
/* extern for regress tests */
28
bool
29
matches_env_pattern(const char *pattern, const char *var, bool *full_match)
30
{
31
size_t len, sep_pos;
32
bool iswild = false, match = false;
33
bool saw_sep = false;
34
const char *cp;
35
debug_decl(matches_env_pattern, SUDOERS_DEBUG_ENV);
36
37
/* Locate position of the '=' separator in var=value. */
38
sep_pos = strcspn(var, "=");
39
40
/* Locate '*' wildcard and compute len. */
41
for (cp = pattern; *cp != '\0'; cp++) {
42
if (*cp == '*') {
43
iswild = true;
44
break;
45
}
46
}
47
len = (size_t)(cp - pattern);
48
49
if (iswild) {
50
/* Match up to the '*' wildcard. */
51
if (strncmp(pattern, var, len) == 0) {
52
while (*cp != '\0') {
53
if (*cp == '*') {
54
/* Collapse sequential '*'s */
55
do {
56
cp++;
57
} while (*cp == '*');
58
/* A '*' at the end of a pattern matches anything. */
59
if (*cp == '\0') {
60
match = true;
61
break;
62
}
63
/* Keep track of whether we matched an equal sign. */
64
if (*cp == '=')
65
saw_sep = true;
66
/* Look for first match of text after the '*' */
67
while ((saw_sep || len != sep_pos) &&
68
var[len] != '\0' && var[len] != *cp)
69
len++;
70
}
71
if (var[len] != *cp)
72
break;
73
cp++;
74
len++;
75
}
76
if (*cp == '\0' && (len == sep_pos || var[len] == '\0'))
77
match = true;
78
}
79
} else {
80
if (strncmp(pattern, var, len) == 0 &&
81
(len == sep_pos || var[len] == '\0')) {
82
match = true;
83
}
84
}
85
if (match)
86
*full_match = len > sep_pos + 1;
87
debug_return_bool(match);
88
}
89
90