/src/sudo/plugins/sudoers/env_pattern.c
Line | Count | Source |
1 | | /* |
2 | | * SPDX-License-Identifier: ISC |
3 | | * |
4 | | * Copyright (c) 2017 Todd C. Miller <Todd.Miller@sudo.ws> |
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 | 8.77M | { |
31 | 8.77M | size_t len, sep_pos; |
32 | 8.77M | bool iswild = false, match = false; |
33 | 8.77M | bool saw_sep = false; |
34 | 8.77M | const char *cp; |
35 | 8.77M | debug_decl(matches_env_pattern, SUDOERS_DEBUG_ENV); |
36 | | |
37 | | /* Locate position of the '=' separator in var=value. */ |
38 | 8.77M | sep_pos = strcspn(var, "="); |
39 | | |
40 | | /* Locate '*' wildcard and compute len. */ |
41 | 72.4M | for (cp = pattern; *cp != '\0'; cp++) { |
42 | 64.1M | if (*cp == '*') { |
43 | 524k | iswild = true; |
44 | 524k | break; |
45 | 524k | } |
46 | 64.1M | } |
47 | 8.77M | len = (size_t)(cp - pattern); |
48 | | |
49 | 8.77M | if (iswild) { |
50 | | /* Match up to the '*' wildcard. */ |
51 | 524k | if (strncmp(pattern, var, len) == 0) { |
52 | 39.3k | while (*cp != '\0') { |
53 | 39.3k | if (*cp == '*') { |
54 | | /* Collapse sequential '*'s */ |
55 | 22.0k | do { |
56 | 22.0k | cp++; |
57 | 22.0k | } while (*cp == '*'); |
58 | | /* A '*' at the end of a pattern matches anything. */ |
59 | 22.0k | if (*cp == '\0') { |
60 | 4.65k | match = true; |
61 | 4.65k | break; |
62 | 4.65k | } |
63 | | /* Keep track of whether we matched an equal sign. */ |
64 | 17.3k | if (*cp == '=') |
65 | 17.3k | saw_sep = true; |
66 | | /* Look for first match of text after the '*' */ |
67 | 200k | while ((saw_sep || len != sep_pos) && |
68 | 200k | var[len] != '\0' && var[len] != *cp) |
69 | 183k | len++; |
70 | 17.3k | } |
71 | 34.7k | if (var[len] != *cp) |
72 | 17.3k | break; |
73 | 17.3k | cp++; |
74 | 17.3k | len++; |
75 | 17.3k | } |
76 | 22.0k | if (*cp == '\0' && (len == sep_pos || var[len] == '\0')) |
77 | 1.04k | match = true; |
78 | 22.0k | } |
79 | 8.24M | } else { |
80 | 8.24M | if (strncmp(pattern, var, len) == 0 && |
81 | 43.8k | (len == sep_pos || var[len] == '\0')) { |
82 | 43.5k | match = true; |
83 | 43.5k | } |
84 | 8.24M | } |
85 | 8.77M | if (match) |
86 | 48.1k | *full_match = len > sep_pos + 1; |
87 | | debug_return_bool(match); |
88 | 8.77M | } |