xref: /aosp_15_r20/external/selinux/semodule-utils/semodule_package/semodule_unpackage.c (revision 2d543d20722ada2425b5bdab9d0d1d29470e7bba)
1*2d543d20SAndroid Build Coastguard Worker #include <sepol/module.h>
2*2d543d20SAndroid Build Coastguard Worker #include <getopt.h>
3*2d543d20SAndroid Build Coastguard Worker #include <fcntl.h>
4*2d543d20SAndroid Build Coastguard Worker #include <stdio.h>
5*2d543d20SAndroid Build Coastguard Worker #include <stdlib.h>
6*2d543d20SAndroid Build Coastguard Worker #include <string.h>
7*2d543d20SAndroid Build Coastguard Worker #include <unistd.h>
8*2d543d20SAndroid Build Coastguard Worker #include <sys/types.h>
9*2d543d20SAndroid Build Coastguard Worker #include <sys/stat.h>
10*2d543d20SAndroid Build Coastguard Worker #include <sys/mman.h>
11*2d543d20SAndroid Build Coastguard Worker #include <fcntl.h>
12*2d543d20SAndroid Build Coastguard Worker #include <errno.h>
13*2d543d20SAndroid Build Coastguard Worker 
usage(const char * progname)14*2d543d20SAndroid Build Coastguard Worker static void usage(const char *progname)
15*2d543d20SAndroid Build Coastguard Worker {
16*2d543d20SAndroid Build Coastguard Worker 	printf("usage: %s ppfile modfile [fcfile]\n", progname);
17*2d543d20SAndroid Build Coastguard Worker }
18*2d543d20SAndroid Build Coastguard Worker 
main(int argc,char ** argv)19*2d543d20SAndroid Build Coastguard Worker int main(int argc, char **argv)
20*2d543d20SAndroid Build Coastguard Worker {
21*2d543d20SAndroid Build Coastguard Worker 	struct sepol_module_package *pkg = NULL;
22*2d543d20SAndroid Build Coastguard Worker 	struct sepol_policy_file *in = NULL, *out = NULL;
23*2d543d20SAndroid Build Coastguard Worker 	FILE *fp = NULL;
24*2d543d20SAndroid Build Coastguard Worker 	size_t len;
25*2d543d20SAndroid Build Coastguard Worker 	const char *ppfile, *modfile, *fcfile = NULL, *fcdata;
26*2d543d20SAndroid Build Coastguard Worker 	int ret;
27*2d543d20SAndroid Build Coastguard Worker 
28*2d543d20SAndroid Build Coastguard Worker 	if (argc < 3) {
29*2d543d20SAndroid Build Coastguard Worker 		usage(argv[0]);
30*2d543d20SAndroid Build Coastguard Worker 		return EXIT_FAILURE;
31*2d543d20SAndroid Build Coastguard Worker 	}
32*2d543d20SAndroid Build Coastguard Worker 
33*2d543d20SAndroid Build Coastguard Worker 	ppfile = argv[1];
34*2d543d20SAndroid Build Coastguard Worker 	modfile = argv[2];
35*2d543d20SAndroid Build Coastguard Worker 	if (argc >= 4)
36*2d543d20SAndroid Build Coastguard Worker 		fcfile = argv[3];
37*2d543d20SAndroid Build Coastguard Worker 
38*2d543d20SAndroid Build Coastguard Worker 	if (sepol_module_package_create(&pkg)) {
39*2d543d20SAndroid Build Coastguard Worker 		fprintf(stderr, "%s:  Out of memory\n", argv[0]);
40*2d543d20SAndroid Build Coastguard Worker 		goto failure;
41*2d543d20SAndroid Build Coastguard Worker 	}
42*2d543d20SAndroid Build Coastguard Worker 
43*2d543d20SAndroid Build Coastguard Worker 	if (sepol_policy_file_create(&in)) {
44*2d543d20SAndroid Build Coastguard Worker 		fprintf(stderr, "%s:  Out of memory\n", argv[0]);
45*2d543d20SAndroid Build Coastguard Worker 		goto failure;
46*2d543d20SAndroid Build Coastguard Worker 	}
47*2d543d20SAndroid Build Coastguard Worker 
48*2d543d20SAndroid Build Coastguard Worker 	fp = fopen(ppfile, "r");
49*2d543d20SAndroid Build Coastguard Worker 	if (!fp) {
50*2d543d20SAndroid Build Coastguard Worker 		fprintf(stderr, "%s:  Could not open file %s:  %s\n", argv[0], ppfile, strerror(errno));
51*2d543d20SAndroid Build Coastguard Worker 		goto failure;
52*2d543d20SAndroid Build Coastguard Worker 	}
53*2d543d20SAndroid Build Coastguard Worker 	sepol_policy_file_set_fp(in, fp);
54*2d543d20SAndroid Build Coastguard Worker 
55*2d543d20SAndroid Build Coastguard Worker 	if (sepol_module_package_read(pkg, in, 0) == -1) {
56*2d543d20SAndroid Build Coastguard Worker 		fprintf(stderr, "%s:  Error while reading policy module from %s\n",
57*2d543d20SAndroid Build Coastguard Worker 			argv[0], ppfile);
58*2d543d20SAndroid Build Coastguard Worker 		goto failure;
59*2d543d20SAndroid Build Coastguard Worker 	}
60*2d543d20SAndroid Build Coastguard Worker 
61*2d543d20SAndroid Build Coastguard Worker 	sepol_policy_file_free(in);
62*2d543d20SAndroid Build Coastguard Worker 	in = NULL;
63*2d543d20SAndroid Build Coastguard Worker 	fclose(fp);
64*2d543d20SAndroid Build Coastguard Worker 	fp = NULL;
65*2d543d20SAndroid Build Coastguard Worker 
66*2d543d20SAndroid Build Coastguard Worker 	if (sepol_policy_file_create(&out)) {
67*2d543d20SAndroid Build Coastguard Worker 		fprintf(stderr, "%s:  Out of memory\n", argv[0]);
68*2d543d20SAndroid Build Coastguard Worker 		goto failure;
69*2d543d20SAndroid Build Coastguard Worker 	}
70*2d543d20SAndroid Build Coastguard Worker 
71*2d543d20SAndroid Build Coastguard Worker 	fp = fopen(modfile, "w");
72*2d543d20SAndroid Build Coastguard Worker 	if (!fp) {
73*2d543d20SAndroid Build Coastguard Worker 		fprintf(stderr, "%s:  Could not open file %s:  %s\n", argv[0], modfile, strerror(errno));
74*2d543d20SAndroid Build Coastguard Worker 		goto failure;
75*2d543d20SAndroid Build Coastguard Worker 	}
76*2d543d20SAndroid Build Coastguard Worker 	sepol_policy_file_set_fp(out, fp);
77*2d543d20SAndroid Build Coastguard Worker 
78*2d543d20SAndroid Build Coastguard Worker 	if (sepol_policydb_write(sepol_module_package_get_policy(pkg), out)) {
79*2d543d20SAndroid Build Coastguard Worker 		fprintf(stderr, "%s:  Error while writing module to %s\n", argv[0], modfile);
80*2d543d20SAndroid Build Coastguard Worker 		goto failure;
81*2d543d20SAndroid Build Coastguard Worker 	}
82*2d543d20SAndroid Build Coastguard Worker 
83*2d543d20SAndroid Build Coastguard Worker 	ret = fclose(fp);
84*2d543d20SAndroid Build Coastguard Worker 	fp = NULL;
85*2d543d20SAndroid Build Coastguard Worker 	if (ret) {
86*2d543d20SAndroid Build Coastguard Worker 		fprintf(stderr, "%s:  Error while closing file %s:  %s\n", argv[0], modfile, strerror(errno));
87*2d543d20SAndroid Build Coastguard Worker 		goto failure;
88*2d543d20SAndroid Build Coastguard Worker 	}
89*2d543d20SAndroid Build Coastguard Worker 
90*2d543d20SAndroid Build Coastguard Worker 	sepol_policy_file_free(out);
91*2d543d20SAndroid Build Coastguard Worker 	out = NULL;
92*2d543d20SAndroid Build Coastguard Worker 
93*2d543d20SAndroid Build Coastguard Worker 	len = sepol_module_package_get_file_contexts_len(pkg);
94*2d543d20SAndroid Build Coastguard Worker 	if (fcfile && len) {
95*2d543d20SAndroid Build Coastguard Worker 		fp = fopen(fcfile, "w");
96*2d543d20SAndroid Build Coastguard Worker 		if (!fp) {
97*2d543d20SAndroid Build Coastguard Worker 			fprintf(stderr, "%s:  Could not open file %s:  %s\n", argv[0], fcfile, strerror(errno));
98*2d543d20SAndroid Build Coastguard Worker 			goto failure;
99*2d543d20SAndroid Build Coastguard Worker 		}
100*2d543d20SAndroid Build Coastguard Worker 		fcdata = sepol_module_package_get_file_contexts(pkg);
101*2d543d20SAndroid Build Coastguard Worker 		if (fwrite(fcdata, 1, len, fp) != len) {
102*2d543d20SAndroid Build Coastguard Worker 			fprintf(stderr, "%s:  Could not write file %s:  %s\n", argv[0], fcfile, strerror(errno));
103*2d543d20SAndroid Build Coastguard Worker 			goto failure;
104*2d543d20SAndroid Build Coastguard Worker 		}
105*2d543d20SAndroid Build Coastguard Worker 
106*2d543d20SAndroid Build Coastguard Worker 		ret = fclose(fp);
107*2d543d20SAndroid Build Coastguard Worker 		fp = NULL;
108*2d543d20SAndroid Build Coastguard Worker 		if (ret) {
109*2d543d20SAndroid Build Coastguard Worker 			fprintf(stderr, "%s:  Could not close file %s:  %s\n", argv[0], fcfile, strerror(errno));
110*2d543d20SAndroid Build Coastguard Worker 			goto failure;
111*2d543d20SAndroid Build Coastguard Worker 		}
112*2d543d20SAndroid Build Coastguard Worker 	}
113*2d543d20SAndroid Build Coastguard Worker 
114*2d543d20SAndroid Build Coastguard Worker 	ret = EXIT_SUCCESS;
115*2d543d20SAndroid Build Coastguard Worker 	goto cleanup;
116*2d543d20SAndroid Build Coastguard Worker 
117*2d543d20SAndroid Build Coastguard Worker failure:
118*2d543d20SAndroid Build Coastguard Worker 	ret = EXIT_FAILURE;
119*2d543d20SAndroid Build Coastguard Worker 
120*2d543d20SAndroid Build Coastguard Worker cleanup:
121*2d543d20SAndroid Build Coastguard Worker 	if (fp)
122*2d543d20SAndroid Build Coastguard Worker 		fclose(fp);
123*2d543d20SAndroid Build Coastguard Worker 	sepol_policy_file_free(out);
124*2d543d20SAndroid Build Coastguard Worker 	sepol_module_package_free(pkg);
125*2d543d20SAndroid Build Coastguard Worker 	sepol_policy_file_free(in);
126*2d543d20SAndroid Build Coastguard Worker 
127*2d543d20SAndroid Build Coastguard Worker 	return ret;
128*2d543d20SAndroid Build Coastguard Worker }
129