File: parser_alias.c

package info (click to toggle)
apparmor 2.7.103-4
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 11,920 kB
  • sloc: ansic: 12,022; perl: 10,644; sh: 8,119; cpp: 2,505; yacc: 1,592; python: 1,489; makefile: 1,138; lex: 1,003; pascal: 399; ruby: 374; exp: 250; java: 212; xml: 159
file content (213 lines) | stat: -rw-r--r-- 5,016 bytes parent folder | download
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
/*
 *   Copyright (c) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
 *   NOVELL (All rights reserved)
 *
 *   This program is free software; you can redistribute it and/or
 *   modify it under the terms of version 2 of the GNU General Public
 *   License 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, contact Novell, Inc.
 */

#include <search.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <libintl.h>
#define _(s) gettext(s)

#include "immunix.h"
#include "parser.h"

struct alias_rule {
	char *from;
	char *to;
};

static void *alias_table;

static int compare_alias(const void *a, const void *b)
{
	char *a_name = ((struct alias_rule *) a)->from;
	char *b_name = ((struct alias_rule *) b)->from;
	int res = strcmp(a_name, b_name);
	if (res == 0) {
		a_name = ((struct alias_rule *) a)->to;
		b_name = ((struct alias_rule *) b)->to;
		res = strcmp(a_name, b_name);
	}
	return res;
}

int new_alias(const char *from, const char *to)
{
	struct alias_rule *alias, **result;

	alias = calloc(1, sizeof(struct alias_rule));
	if (!alias) {
		PERROR("Failed to allocate memory: %s\n", strerror(errno));
		goto fail;
	}
	alias->from = strdup(from);
	if (!alias->from) {
		PERROR("Failed to allocate memory: %s\n", strerror(errno));
		goto fail;
	}
	alias->to = strdup(to);
	if (!alias->to) {
		PERROR("Failed to allocate memory: %s\n", strerror(errno));
		goto fail;
	}

	result = (struct alias_rule **) tsearch(alias, &alias_table, (comparison_fn_t) &compare_alias);
	if (!result) {
		PERROR("Failed to allocate memory: %s\n", strerror(errno));
		goto fail;
	}

	if (*result != alias) {
		/* already existing alias */
		PERROR("'%s' is already defined\n", from);
		goto fail;
	}

	return 1;

fail:
	if (alias) {
		if (alias->from)
			free(alias->from);
		if (alias->to)
			free(alias->to);
		free(alias);
	}
	/* just drop duplicate aliases don't actually fail */
	return 1;
}

static char *do_alias(struct alias_rule *alias, const char *target)
{
	int len = strlen(target) - strlen(alias->from) + strlen(alias->to);
	char *new = malloc(len + 1);
	if (!new) {
		PERROR("Failed to allocate memory: %s\n", strerror(errno));
		return NULL;
	}
	sprintf(new, "%s%s", alias->to, target + strlen(alias->from));
/*fprintf(stderr, "replaced alias: from: %s, to: %s, name: %s\n  %s\n", alias->from, alias->to, target, new);*/
	return new;
}

static struct codomain *target_cod;
static struct cod_entry *target_list;
static void process_entries(const void *nodep, VISIT value, int __unused level)
{
	struct alias_rule **t = (struct alias_rule **) nodep;
	struct cod_entry *entry, *dup = NULL;
	int len;

	if (value == preorder || value == endorder)
		return;

	len = strlen((*t)->from);

	list_for_each(target_list, entry) {
		if (entry->mode & (AA_SHARED_PERMS & AA_PTRACE_PERMS) ||
		    entry->alias_ignore)
			continue;
		if (entry->name && strncmp((*t)->from, entry->name, len) == 0) {
			char *new = do_alias(*t, entry->name);
			if (!new)
				return;
			dup = copy_cod_entry(entry);
			free(dup->name);
			dup->name = new;
		}
		if (entry->link_name &&
		    strncmp((*t)->from, entry->link_name, len) == 0) {
			char *new = do_alias(*t, entry->link_name);
			if (!new)
				return;
			if (!dup)
				dup = copy_cod_entry(entry);
			free(dup->link_name);
			dup->link_name = new;
		}
		if (dup) {
			dup->alias_ignore = 1;
			/* adds to the front of the list, list iteratition
			 * will skip it
			 */
			entry->next = dup;

			dup = NULL;
		}
	}
}

static struct codomain *target_cod;
static void process_name(const void *nodep, VISIT value, int __unused level)
{
	struct alias_rule **t = (struct alias_rule **) nodep;
	struct codomain *cod = target_cod;
	char *name;
	int len;

	if (value == preorder || value == endorder)
		return;

	len = strlen((*t)->from);

	if (cod->attachment)
		name = cod->attachment;
	else
		name = cod->name;

	if (name && strncmp((*t)->from, name, len) == 0) {
		struct alt_name *alt;
		char *new = do_alias(*t, name);
		if (!new)
			return;
		/* aliases create alternate names */
		alt = calloc(1, sizeof(struct alt_name));
		if (!alt)
			return;
		alt->name = new;
		alt->next = cod->altnames;
		cod->altnames = alt;
	}
}

void replace_aliases(struct codomain *cod)
{
	target_cod = cod;
	twalk(alias_table, process_name);

	if (cod->entries) {
		target_list = cod->entries;
		target_cod = cod;
		twalk(alias_table, process_entries);
	}
}

static void free_alias(void *nodep)
{
	struct alias_rule *t = (struct alias_rule *)nodep;
	free(t->from);
	free(t->to);
	free(t);
}

void free_aliases(void)
{
	if (alias_table)
		tdestroy(alias_table, &free_alias);
	alias_table = NULL;
}