summaryrefslogtreecommitdiffstats
path: root/libapol/src/render.c
blob: 4bc19b6da3c349929b2fab2f20b4e5fa0498ee09 (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
154
155
156
157
158
/**
 * @file
 *
 * Routines to render various data structures used by libapol.
 *
 * @author Jeremy A. Mowery jmowery@tresys.com
 * @author Jason Tang  jtang@tresys.com
 * @author David Windsor dwindsor@tresys.com
 *
 * Copyright (C) 2003-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 <config.h>

#include <apol/context-query.h>
#include <apol/policy.h>
#include <apol/render.h>

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

#ifndef WORDS_BIGENDIAN
extern void swab(const void *from, void *to, ssize_t n);
#endif

#if LINK_SHARED == 1
__asm__(".symver apol_ipv4_addr_render_old,apol_ipv4_addr_render@");
__asm__(".symver apol_ipv4_addr_render_new,apol_ipv4_addr_render@@VERS_4.1");
#endif

/**
 * @brief Internal version of apol_ipv4_addr_render() version 4.1
 *
 * Implementation of the exported function apol_ipv4_addr_render()
 * for version 4.1; this symbol name is not exported.
 */
char *apol_ipv4_addr_render_new(const apol_policy_t * policydb, uint32_t addr[4])
{
	char buf[40], *b;
	unsigned char *p = (unsigned char *)&(addr[0]);
	snprintf(buf, sizeof(buf), "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
	if ((b = strdup(buf)) == NULL) {
		ERR(policydb, "%s", strerror(ENOMEM));
	}
	return b;
}

#if LINK_SHARED == 0
char *apol_ipv4_addr_render(const apol_policy_t * policydb, uint32_t addr[4])
{
	return apol_ipv4_addr_render_new(policydb, addr);
}
#endif

/**
 * @brief Internal version of apol_ipv4_addr_render() version 4.0 or earlier
 * @deprecated use the 4.1 version.
 * @see apol_ipv4_addr_render()
 */
char *apol_ipv4_addr_render_old(apol_policy_t * policydb, uint32_t addr)
{
	char buf[40], *b;
	unsigned char *p = (unsigned char *)&addr;
	snprintf(buf, sizeof(buf), "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
	if ((b = strdup(buf)) == NULL) {
		ERR(policydb, "%s", strerror(ENOMEM));
	}
	return b;
}

char *apol_ipv6_addr_render(const apol_policy_t * policydb, uint32_t addr[4])
{
	uint16_t tmp[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
	int i, sz = 0, retv;
	char buf[40], *b;	       /* 8 * 4 hex digits + 7 * ':' + '\0' == max size of string */
	int contract = 0, prev_contr = 0, contr_idx_end = -1;
	for (i = 0; i < 4; i++) {
		uint32_t a;
#ifdef WORDS_BIGENDIAN
		a = addr[i];
#else
		swab(addr + i, &a, sizeof(a));
#endif
		/* have to use division and mod here, so as to ignore
		 * host system's byte ordering */
		tmp[2 * i] = a % (1 << 16);
		tmp[2 * i + 1] = a / (1 << 16);
	}

	for (i = 0; i < 8; i++) {
		if (tmp[i] == 0) {
			contract++;
			if (i == 7 && contr_idx_end == -1)
				contr_idx_end = 8;
		} else {
			if (contract > prev_contr) {
				contr_idx_end = i;
			}
			prev_contr = contract;
			contract = 0;
		}
	}

	if (prev_contr > contract)
		contract = prev_contr;

	for (i = 0; i < 8; i++) {
		if (i == contr_idx_end - contract) {
			retv = snprintf(buf + sz, 40 - sz, i ? ":" : "::");
			sz += retv;
		} else if (i > contr_idx_end - contract && i < contr_idx_end) {
			continue;
		} else {
			retv = snprintf(buf + sz, 40 - sz, i == 7 ? "%04x" : "%04x:", tmp[i]);
			sz += retv;
		}
	}

	buf[sz] = '\0';
	if ((b = strdup(buf)) == NULL) {
		ERR(policydb, "%s", strerror(ENOMEM));
	}
	return b;
}

char *apol_qpol_context_render(const apol_policy_t * p, const qpol_context_t * context)
{
	apol_context_t *c = NULL;
	char *rendered_context;

	if (p == NULL || context == NULL) {
		ERR(p, "%s", strerror(EINVAL));
		errno = EINVAL;
		return NULL;
	}

	if ((c = apol_context_create_from_qpol_context(p, context)) == NULL) {
		return NULL;
	}
	rendered_context = apol_context_render(p, c);
	apol_context_destroy(&c);
	return rendered_context;
}