parser_merge.c 3.54 KB
Newer Older
1
/*
John Johansen's avatar
John Johansen committed
2 3
 *   Copyright (c) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
 *   NOVELL (All rights reserved)
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
 *
 *   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 <linux/unistd.h>

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

#include "parser.h"

29

30 31 32 33 34
static int file_comp(const void *c1, const void *c2)
{
	struct cod_entry **e1, **e2;
	e1 = (struct cod_entry **)c1;
	e2 = (struct cod_entry **)c2;
35 36
	int res = 0;

37
	//PERROR("strcmp %s %s\n", (*e1)->name, (*e2)->name);
38 39 40 41 42 43 44 45 46 47 48
	if ((*e1)->namespace) {
		if ((*e2)->namespace)
			res = strcmp((*e1)->namespace, (*e2)->namespace);
		else
			return 1;
	} else if ((*e2)->namespace) {
		return -1;
	}
	if (res)
		return res;

49 50 51 52 53 54 55 56 57 58 59
	if ((*e1)->link_name) {
		if ((*e2)->link_name)
			res = strcmp((*e1)->link_name, (*e2)->link_name);
		else
			return 1;
	} else if ((*e2)->link_name) {
		return -1;
	}
	if (res)
		return res;

60 61 62 63 64
	if ((*e1)->link_name)
		res = (*e2)->subset - (*e1)->subset;
	if (res)
		return res;

65 66 67
	if ((*e1)->deny != (*e2)->deny)
		return (*e1)->deny < (*e2)->deny ? -1 : 1;

John Johansen's avatar
John Johansen committed
68 69 70 71 72 73
	/* rules with ptrace and change_profile can only merge with
	 * rules with exact same perm */
	if (((*e1)->mode & (AA_CHANGE_PROFILE | AA_PTRACE_PERMS)) !=
	    ((*e2)->mode & (AA_CHANGE_PROFILE | AA_PTRACE_PERMS)))
		return 1;

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
	return strcmp((*e1)->name, (*e2)->name);
}

static int process_file_entries(struct codomain *cod)
{
	int n, count;
	struct cod_entry *flist, *cur, *next;
	struct cod_entry **table;

	for (flist = cod->entries, n = 0; flist; flist = flist->next)
		n++;

	count = n;
	if (count < 2)
		return 1;

	table = malloc(sizeof(struct cod_entry *) * (count + 1));
	if (!table) {
		PERROR(_("Couldn't merge entries. Out of Memory\n"));
		return 0;
	}

	n = 0;
	for (flist = cod->entries; flist; flist = flist->next) {
		table[n] = flist;
		n++;
	}

	qsort(table, count, sizeof(struct cod_entry *), file_comp);
	table[count] = NULL;

105

106 107 108 109
	/* walk the sorted table merging similar entries */
	for (cur = table[0], next = table[1], n = 1; next != NULL; n++, next = table[n]) {
		if (file_comp(&cur, &next) == 0) {
			/* check for merged x consistency */
John Johansen's avatar
John Johansen committed
110
			if (!is_merged_x_consistent(cur->mode, next->mode)) {
111
				PERROR(_("profile %s: has merged rule %s with "
112
					 "conflicting x modifiers\n"),
113
					 cod->name, cur->name);
114 115
				return 0;
			}
116 117
//if (next->audit)
//fprintf(stderr, "warning: merging rule 0x%x %s\n", next->audit, next->name);
John Johansen's avatar
John Johansen committed
118
			cur->mode |= next->mode;
119
			cur->audit |= next->audit;
120
			free(next->name);
121 122
			if (next->link_name)
				free(next->link_name);
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
			free(next);
			table[n] = NULL;
		} else {
			cur = next;
		}
	}

	/* rebuild the file_entry chain */
	cur = table[0];
	for (n = 1; n < count; n++) {
		if (table[n] != NULL) {
			cur->next = table[n];
			cur = table[n];
		}
	}
	cur->next = NULL;
	cod->entries = table[0];

	free(table);

	return 1;
}

int codomain_merge_rules(struct codomain *cod)
{
	if (!process_file_entries(cod))
		goto fail;

	/* XXX  return error from this */
	merge_hat_rules(cod);

	return 1;
fail:
	return 0;
}