1 /* Authors: Karl MacMillan <[email protected]>
2 *
3 * Copyright (C) 2004 Tresys Technology, LLC
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, version 2.
7 */
8
9 #include <sepol/module.h>
10
11 #include <getopt.h>
12 #include <fcntl.h>
13 #include <stdio.h>
14 #include <errno.h>
15 #include <sys/mman.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <string.h>
21
22 #define LINKPOLICY_VERSION "1.0"
23
usage(const char * program_name)24 static void usage(const char *program_name)
25 {
26 printf("usage: %s [-hVv] [-o outfile] basemodpkg modpkg1 [modpkg2]...\n",
27 program_name);
28 }
29
load_module(const char * filename,const char * progname)30 static sepol_module_package_t *load_module(const char *filename, const char *progname)
31 {
32 int ret;
33 FILE *fp = NULL;
34 struct sepol_policy_file *pf = NULL;
35 sepol_module_package_t *p = NULL;
36
37 if (sepol_module_package_create(&p)) {
38 fprintf(stderr, "%s: Out of memory\n", progname);
39 goto bad;
40 }
41 if (sepol_policy_file_create(&pf)) {
42 fprintf(stderr, "%s: Out of memory\n", progname);
43 goto bad;
44 }
45 fp = fopen(filename, "re");
46 if (!fp) {
47 fprintf(stderr, "%s: Could not open package %s: %s\n", progname,
48 filename, strerror(errno));
49 goto bad;
50 }
51 sepol_policy_file_set_fp(pf, fp);
52
53 printf("%s: loading package from file %s\n", progname, filename);
54
55 ret = sepol_module_package_read(p, pf, 0);
56 if (ret) {
57 fprintf(stderr, "%s: Error while reading package from %s\n",
58 progname, filename);
59 goto bad;
60 }
61 fclose(fp);
62 sepol_policy_file_free(pf);
63 return p;
64 bad:
65 sepol_module_package_free(p);
66 sepol_policy_file_free(pf);
67 if (fp)
68 fclose(fp);
69 return NULL;
70 }
71
main(int argc,char ** argv)72 int main(int argc, char **argv)
73 {
74 int ch, i, ret, show_version = 0, verbose = 0, num_mods = 0;
75 const char *basename, *outname = NULL;
76 sepol_module_package_t *base = NULL, **mods = NULL;
77 struct sepol_policy_file *pf = NULL;
78
79 while ((ch = getopt(argc, argv, "ho:Vv")) != EOF) {
80 switch (ch) {
81 case 'h':
82 usage(argv[0]);
83 return EXIT_SUCCESS;
84 case 'V':
85 show_version = 1;
86 break;
87 case 'v':
88 verbose = 1;
89 break;
90 case 'o':
91 outname = optarg;
92 break;
93 default:
94 usage(argv[0]);
95 return EXIT_FAILURE;
96 }
97 }
98
99 if (show_version) {
100 printf("%s\n", LINKPOLICY_VERSION);
101 return EXIT_SUCCESS;
102 }
103
104 /* check args */
105 if (argc < 3 || optind + 2 > argc) {
106 fprintf(stderr,
107 "%s: You must provide the base module package and at least one other module package\n",
108 argv[0]);
109 usage(argv[0]);
110 return EXIT_FAILURE;
111 }
112
113 basename = argv[optind++];
114 base = load_module(basename, argv[0]);
115 if (!base) {
116 fprintf(stderr,
117 "%s: Could not load base module from file %s\n",
118 argv[0], basename);
119 goto failure;
120 }
121
122 num_mods = argc - optind;
123 mods = calloc(num_mods, sizeof(sepol_module_package_t *));
124 if (!mods) {
125 fprintf(stderr, "%s: Out of memory\n", argv[0]);
126 goto failure;
127 }
128
129 for (i = 0; optind < argc; optind++, i++) {
130 mods[i] = load_module(argv[optind], argv[0]);
131 if (!mods[i]) {
132 fprintf(stderr,
133 "%s: Could not load module from file %s\n",
134 argv[0], argv[optind]);
135 goto failure;
136 }
137 }
138
139 if (sepol_link_packages(NULL, base, mods, num_mods, verbose)) {
140 fprintf(stderr, "%s: Error while linking packages\n", argv[0]);
141 goto failure;
142 }
143
144 if (outname) {
145 FILE *outfile = fopen(outname, "we");
146 if (!outfile) {
147 fprintf(stderr, "%s: Could not open output file %s: %s\n",
148 argv[0], outname, strerror(errno));
149 goto failure;
150 }
151
152 if (sepol_policy_file_create(&pf)) {
153 fprintf(stderr, "%s: Out of memory\n", argv[0]);
154 fclose(outfile);
155 goto failure;
156 }
157 sepol_policy_file_set_fp(pf, outfile);
158 if (sepol_module_package_write(base, pf)) {
159 fprintf(stderr, "%s: Error writing linked package.\n",
160 argv[0]);
161 sepol_policy_file_free(pf);
162 fclose(outfile);
163 goto failure;
164 }
165 sepol_policy_file_free(pf);
166
167 if (fclose(outfile)) {
168 fprintf(stderr, "%s: Error closing linked package: %s\n",
169 argv[0], strerror(errno));
170 goto failure;
171 }
172 }
173
174 ret = EXIT_SUCCESS;
175 goto cleanup;
176
177 failure:
178 ret = EXIT_FAILURE;
179
180 cleanup:
181 if (mods) {
182 for (i = 0; i < num_mods; i++)
183 sepol_module_package_free(mods[i]);
184 free(mods);
185 }
186 sepol_module_package_free(base);
187
188 return ret;
189 }
190