summaryrefslogtreecommitdiffstats
path: root/libseaudit/src/bool_message.c
blob: f105cf01c569621da128442fab0d4c48f8b450fe (plain)
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
/**
 *  @file
 *  Implementation of a single boolean change log message.
 *
 *  @author Jeremy A. Mowery jmowery@tresys.com
 *  @author Jason Tang jtang@tresys.com
 *
 *  Copyright (C) 2006-2007 Tresys Technology, LLC
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library 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
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "seaudit_internal.h"

#include <apol/util.h>

#include <errno.h>
#include <stdlib.h>
#include <string.h>

/******************** protected functions below ********************/

static void seaudit_bool_change_free(void *elem)
{
	if (elem != NULL) {
		seaudit_bool_message_change_t *b = elem;
		free(b);
	}
}

seaudit_bool_message_t *bool_message_create(void)
{
	seaudit_bool_message_t *boolm = calloc(1, sizeof(seaudit_bool_message_t));
	if (boolm == NULL) {
		return NULL;
	}
	if ((boolm->changes = apol_vector_create(seaudit_bool_change_free)) == NULL) {
		bool_message_free(boolm);
		return NULL;
	}
	return boolm;
}

int bool_change_append(seaudit_log_t * log, seaudit_bool_message_t * boolm, const char *name, int value)
{
	char *s = strdup(name);
	seaudit_bool_message_change_t *bc = NULL;
	int error;
	if (s == NULL || apol_bst_insert_and_get(log->bools, (void **)&s, NULL) < 0) {
		error = errno;
		free(s);
		ERR(log, "%s", strerror(error));
		errno = error;
		return -1;
	}
	if ((bc = calloc(1, sizeof(*bc))) == NULL || apol_vector_append(boolm->changes, bc) < 0) {
		error = errno;
		free(s);
		ERR(log, "%s", strerror(error));
		errno = error;
		return -1;
	}
	bc->boolean = s;
	bc->value = value;
	return 0;
}

void bool_message_free(seaudit_bool_message_t * boolm)
{
	if (boolm != NULL) {
		apol_vector_destroy(&boolm->changes);
		free(boolm);
	}
}

char *bool_message_to_string(const seaudit_message_t * msg, const char *date)
{
	seaudit_bool_message_t *boolm = msg->data.boolm;
	const char *host = msg->host;
	const char *manager = msg->manager;
	char *s = NULL, *misc_string;
	size_t len = 0;
	char *open_brace = "", *close_brace = "";
	if (apol_vector_get_size(boolm->changes) > 0) {
		open_brace = "{ ";
		close_brace = " }";
	}
	if (apol_str_appendf(&s, &len, "%s %s %s: security: committed booleans: %s", date, host, manager, open_brace) < 0) {
		return NULL;
	}
	if ((misc_string = bool_message_to_misc_string(boolm)) == NULL ||
	    apol_str_appendf(&s, &len, misc_string) < 0 || apol_str_append(&s, &len, close_brace) < 0) {
		free(misc_string);
		return NULL;
	}
	free(misc_string);
	return s;
}

char *bool_message_to_string_html(const seaudit_message_t * msg, const char *date)
{
	seaudit_bool_message_t *boolm = msg->data.boolm;
	const char *host = msg->host;
	const char *manager = msg->manager;
	char *s = NULL, *misc_string;
	size_t len = 0;
	char *open_brace = "", *close_brace = "";
	if (apol_vector_get_size(boolm->changes) > 0) {
		open_brace = "{ ";
		close_brace = " }";
	}
	if (apol_str_appendf(&s, &len,
			     "<font class=\"message_date\">%s</font> "
			     "<font class=\"host_name\">%s</font> "
			     "%s: security: committed booleans: %s", date, host, manager, open_brace) < 0) {
		return NULL;
	}
	if ((misc_string = bool_message_to_misc_string(boolm)) == NULL ||
	    apol_str_appendf(&s, &len, misc_string) < 0 || apol_str_appendf(&s, &len, "%s%s<br>", s, close_brace) < 0) {
		free(misc_string);
		return NULL;
	}
	free(misc_string);
	return s;
}

char *bool_message_to_misc_string(const seaudit_bool_message_t * boolm)
{
	char *s = NULL;
	size_t len = 0, i;
	for (i = 0; i < apol_vector_get_size(boolm->changes); i++) {
		seaudit_bool_message_change_t *bc = apol_vector_get_element(boolm->changes, i);
		if (apol_str_appendf(&s, &len, "%s%s:%d", (i == 0 ? "" : ", "), bc->boolean, bc->value) < 0) {
			return NULL;
		}
	}
	if (s == NULL) {
		return strdup("");
	}
	return s;
}