-
Notifications
You must be signed in to change notification settings - Fork 0
/
gradm_replace.c
121 lines (102 loc) · 2.97 KB
/
gradm_replace.c
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
/*
* Copyright (C) 2002-2014 Bradley Spengler, Open Source Security, Inc.
* http://www.grsecurity.net [email protected]
*
* This file is part of gradm.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation.
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "gradm.h"
typedef struct _replace_string_entry
{
struct _replace_string_entry *next;
const char *name;
char *replacewith;
} replace_string_entry;
static replace_string_entry *replace_list;
char *lookup_replace_string(const char *name)
{
replace_string_entry *tmp;
for (tmp = replace_list; tmp; tmp = tmp->next) {
if (!strcmp(tmp->name, name))
return tmp->replacewith;
}
return NULL;
}
/* called with already strdup'd strings */
void add_replace_string(const char *name, char *replacewith)
{
replace_string_entry *entry;
replace_string_entry *tmp;
/* replace an existing entry if the name is redefined */
for (entry = replace_list; entry; entry = entry->next) {
if (!strcmp(entry->name, name)) {
free(entry->replacewith);
entry->replacewith = replacewith;
return;
}
if (entry->next == NULL)
break;
}
tmp = (replace_string_entry *)gr_alloc(sizeof(replace_string_entry));
tmp->next = NULL;
tmp->name = name;
tmp->replacewith = replacewith;
if (replace_list == NULL)
replace_list = tmp;
if (entry != NULL)
entry->next = tmp;
return;
}
/* returns newly allocated string */
char *process_string_replace(const char *str)
{
char *p, *p2;
char *replacewith;
char *newstr;
unsigned int newlen;
p = strstr(str, "$(");
if (p == NULL)
goto normal_str;
p2 = strchr(p, ')');
if (p2 == NULL) {
fprintf(stderr, "Error: Missing terminating \")\" for symbol on line %ld "
"of %s.\n", lineno, current_acl_file);
exit(EXIT_FAILURE);
}
*p2 = '\0';
replacewith = lookup_replace_string(p + 2);
if (replacewith == NULL) {
fprintf(stderr, "Error: Undefined symbol \"%s\" on line %ld "
"of %s.\n", p + 2, lineno, current_acl_file);
exit(EXIT_FAILURE);
}
*p = '\0';
newlen = strlen(str) + strlen(p2 + 1) + strlen(replacewith);
newstr = (char *)gr_alloc(newlen + 1);
strcpy(newstr, str);
strcat(newstr, replacewith);
strcat(newstr, p2 + 1);
newstr[newlen] = '\0';
*p = '$';
*p2 = ')';
if (newstr[0] != '/' && (newlen < 5 || strncmp(newstr, "$HOME", 5))) {
fprintf(stderr, "Error: Malformed path on line %ld of %s.\n",
lineno, current_acl_file);
exit(EXIT_FAILURE);
}
return newstr;
normal_str:
return strdup(str);
}