File: module.c

package info (click to toggle)
setools 3.3.7-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 9,840 kB
  • sloc: ansic: 82,247; tcl: 13,145; cpp: 4,885; makefile: 1,603; yacc: 779; lex: 296; python: 57; sh: 50
file content (236 lines) | stat: -rw-r--r-- 4,865 bytes parent folder | download | duplicates (3)
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/**
 *  @file
 *  Defines the public interface the QPol policy.
 *
 *  @author Jeremy A. Mowery jmowery@tresys.com
 *  @author Jason Tang jtang@tresys.com
 *  @author Brandon Whalen bwhalen@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 <config.h>

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

#include <qpol/module.h>
#include <qpol/util.h>
#include "qpol_internal.h"

#include <sepol/policydb.h>
#include <sepol/policydb/module.h>

int qpol_module_create_from_file(const char *path, qpol_module_t ** module)
{
	sepol_module_package_t *smp = NULL;
	sepol_policy_file_t *spf = NULL;
	FILE *infile = NULL;
	int error = 0;
	char *tmp = NULL;
	char *data = NULL;
	ssize_t size;

	if (module)
		*module = NULL;

	if (!path || !module) {
		errno = EINVAL;
		return STATUS_ERR;
	}

	if (!(*module = calloc(1, sizeof(qpol_module_t)))) {
		return STATUS_ERR;
	}

	if (!((*module)->path = strdup(path))) {
		error = errno;
		goto err;
	}

	if (sepol_policy_file_create(&spf)) {
		error = errno;
		goto err;
	}

	infile = fopen(path, "rb");
	if (!infile) {
		error = errno;
		goto err;
	}
	size = qpol_bunzip(infile, &data);

	if (size > 0) {
		if (!qpol_is_data_mod_pkg(data)) {
			error = ENOTSUP;
			goto err;
		}
		sepol_policy_file_set_mem(spf, data, size);
	} else {
		if (!qpol_is_file_mod_pkg(infile)) {
			error = ENOTSUP;
			goto err;
		}
		rewind(infile);
		sepol_policy_file_set_fp(spf, infile);
	}

	if (sepol_module_package_create(&smp)) {
		error = EIO;
		goto err;
	}

	if (sepol_module_package_info(spf, &((*module)->type), &((*module)->name), &tmp)) {
		error = EIO;
		goto err;
	}
	free(tmp);
	tmp = NULL;
	if (size > 0) {
		// Re setting the memory location has the effect of rewind
		// API is not accessible from here to explicitly "rewind" the
		// in-memory file.
		sepol_policy_file_set_mem(spf, data, size);
	} else {
		rewind(infile);
	}

	if (sepol_module_package_read(smp, spf, 0)) {
		error = EIO;
		goto err;
	}

	if (!((*module)->p = sepol_module_package_get_policy(smp))) {
		error = EIO;
		goto err;
	}
	/* set the module package's policy to NULL as the qpol module owns it now */
	smp->policy = NULL;

	(*module)->version = (*module)->p->p.version;
	(*module)->enabled = 1;

	sepol_module_package_free(smp);
	fclose(infile);
	if (data != NULL)
		free (data);
	sepol_policy_file_free(spf);

	return STATUS_SUCCESS;

      err:
	qpol_module_destroy(module);
	sepol_policy_file_free(spf);
	sepol_module_package_free(smp);
	if (infile)
		fclose(infile);
	if (data != NULL)
		free (data);
	if (tmp != NULL)
		free(tmp);
	errno = error;
	return STATUS_ERR;
}

void qpol_module_destroy(qpol_module_t ** module)
{
	if (!module || !(*module))
		return;

	free((*module)->path);
	free((*module)->name);
	sepol_policydb_free((*module)->p);
	free(*module);
	*module = NULL;
}

int qpol_module_get_path(const qpol_module_t * module, const char **path)
{
	if (!module || !path) {
		errno = EINVAL;
		return STATUS_ERR;
	}

	*path = module->path;

	return STATUS_SUCCESS;
}

int qpol_module_get_name(const qpol_module_t * module, const char **name)
{
	if (!module || !name) {
		errno = EINVAL;
		return STATUS_ERR;
	}

	*name = module->name;

	return STATUS_SUCCESS;
}

int qpol_module_get_version(const qpol_module_t * module, const char **version)
{
	if (!module || !version) {
		errno = EINVAL;
		return STATUS_ERR;
	}

	*version = module->version;

	return STATUS_SUCCESS;
}

int qpol_module_get_type(const qpol_module_t * module, int *type)
{
	if (!module || !type) {
		errno = EINVAL;
		return STATUS_ERR;
	}

	*type = module->type;

	return STATUS_SUCCESS;
}

int qpol_module_get_enabled(const qpol_module_t * module, int *enabled)
{
	if (!module || !enabled) {
		errno = EINVAL;
		return STATUS_ERR;
	}

	*enabled = module->enabled;

	return STATUS_SUCCESS;
}

int qpol_module_set_enabled(qpol_module_t * module, int enabled)
{
	if (!module) {
		errno = EINVAL;
		return STATUS_ERR;
	}

	if (enabled != module->enabled && module->parent) {
		module->parent->modified = 1;
	}
	module->enabled = enabled;

	return STATUS_SUCCESS;
}