-
Notifications
You must be signed in to change notification settings - Fork 345
/
semodule_unpackage.c
128 lines (108 loc) · 2.78 KB
/
semodule_unpackage.c
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
#include <sepol/module.h>
#include <getopt.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <errno.h>
static void usage(const char *progname)
{
printf("usage: %s ppfile modfile [fcfile]\n", progname);
}
int main(int argc, char **argv)
{
struct sepol_module_package *pkg = NULL;
struct sepol_policy_file *in = NULL, *out = NULL;
FILE *fp = NULL;
size_t len;
const char *ppfile, *modfile, *fcfile = NULL, *fcdata;
int ret;
if (argc < 3) {
usage(argv[0]);
return EXIT_FAILURE;
}
ppfile = argv[1];
modfile = argv[2];
if (argc >= 4)
fcfile = argv[3];
if (sepol_module_package_create(&pkg)) {
fprintf(stderr, "%s: Out of memory\n", argv[0]);
goto failure;
}
if (sepol_policy_file_create(&in)) {
fprintf(stderr, "%s: Out of memory\n", argv[0]);
goto failure;
}
fp = fopen(ppfile, "r");
if (!fp) {
fprintf(stderr, "%s: Could not open file %s: %s\n", argv[0], ppfile, strerror(errno));
goto failure;
}
sepol_policy_file_set_fp(in, fp);
if (sepol_module_package_read(pkg, in, 0) == -1) {
fprintf(stderr, "%s: Error while reading policy module from %s\n",
argv[0], ppfile);
goto failure;
}
sepol_policy_file_free(in);
in = NULL;
fclose(fp);
fp = NULL;
if (sepol_policy_file_create(&out)) {
fprintf(stderr, "%s: Out of memory\n", argv[0]);
goto failure;
}
fp = fopen(modfile, "w");
if (!fp) {
fprintf(stderr, "%s: Could not open file %s: %s\n", argv[0], modfile, strerror(errno));
goto failure;
}
sepol_policy_file_set_fp(out, fp);
if (sepol_policydb_write(sepol_module_package_get_policy(pkg), out)) {
fprintf(stderr, "%s: Error while writing module to %s\n", argv[0], modfile);
goto failure;
}
ret = fclose(fp);
fp = NULL;
if (ret) {
fprintf(stderr, "%s: Error while closing file %s: %s\n", argv[0], modfile, strerror(errno));
goto failure;
}
sepol_policy_file_free(out);
out = NULL;
len = sepol_module_package_get_file_contexts_len(pkg);
if (fcfile && len) {
fp = fopen(fcfile, "w");
if (!fp) {
fprintf(stderr, "%s: Could not open file %s: %s\n", argv[0], fcfile, strerror(errno));
goto failure;
}
fcdata = sepol_module_package_get_file_contexts(pkg);
if (fwrite(fcdata, 1, len, fp) != len) {
fprintf(stderr, "%s: Could not write file %s: %s\n", argv[0], fcfile, strerror(errno));
goto failure;
}
ret = fclose(fp);
fp = NULL;
if (ret) {
fprintf(stderr, "%s: Could not close file %s: %s\n", argv[0], fcfile, strerror(errno));
goto failure;
}
}
ret = EXIT_SUCCESS;
goto cleanup;
failure:
ret = EXIT_FAILURE;
cleanup:
if (fp)
fclose(fp);
sepol_policy_file_free(out);
sepol_module_package_free(pkg);
sepol_policy_file_free(in);
return ret;
}