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 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
|
/* Authors: Karl MacMillan <kmacmillan@tresys.com>
*
* Copyright (C) 2004 Tresys Technology, LLC
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 2.
*/
#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 *prog)
{
printf("usage: %s -o <output file> -m <module> [-f <file contexts>]\n",
prog);
printf("Options:\n");
printf(" -o --outfile Output file (required)\n");
printf(" -m --module Module file (required)\n");
printf(" -f --fc File contexts file\n");
printf(" -s --seuser Seusers file (only valid in base)\n");
printf
(" -u --user_extra user_extra file (only valid in base)\n");
printf(" -n --nc Netfilter contexts file\n");
printf(" -h --help Show this help message\n");
}
static int file_to_data(const char *path, char **data, size_t * len, const char *progname)
{
int fd;
struct stat sb;
fd = open(path, O_RDONLY | O_CLOEXEC);
if (fd < 0) {
fprintf(stderr, "%s: Failed to open %s: %s\n", progname, path,
strerror(errno));
return -1;
}
if (fstat(fd, &sb) < 0) {
fprintf(stderr, "%s: Failed to fstat %s: %s\n", progname,
path, strerror(errno));
goto err;
}
if (!sb.st_size) {
close(fd);
*len = 0;
return 0;
}
*data = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (*data == MAP_FAILED) {
fprintf(stderr, "%s: Failed to mmap %s: %s\n", progname, path,
strerror(errno));
goto err;
}
*len = sb.st_size;
close(fd);
return 0;
err:
close(fd);
return -1;
}
int main(int argc, char **argv)
{
struct sepol_module_package *pkg = NULL;
struct sepol_policy_file *mod = NULL, *out = NULL;
FILE *fp = NULL;
char *module = NULL, *file_contexts = NULL, *seusers =
NULL, *user_extra = NULL;
char *fcdata = NULL, *outfile = NULL, *seusersdata =
NULL, *user_extradata = NULL;
char *netfilter_contexts = NULL, *ncdata = NULL;
size_t fclen = 0, seuserslen = 0, user_extralen = 0, nclen = 0;
int i, ret;
const struct option opts[] = {
{"module", required_argument, NULL, 'm'},
{"fc", required_argument, NULL, 'f'},
{"seuser", required_argument, NULL, 's'},
{"user_extra", required_argument, NULL, 'u'},
{"nc", required_argument, NULL, 'n'},
{"outfile", required_argument, NULL, 'o'},
{"help", 0, NULL, 'h'},
{NULL, 0, NULL, 0}
};
while ((i = getopt_long(argc, argv, "m:f:s:u:o:n:h", opts, NULL)) != -1) {
switch (i) {
case 'h':
usage(argv[0]);
return EXIT_SUCCESS;
case 'm':
if (module) {
fprintf(stderr,
"May not specify more than one module\n");
return EXIT_FAILURE;
}
module = strdup(optarg);
if (!module) {
fprintf(stderr, "%s: Out of memory\n", argv[0]);
return EXIT_FAILURE;
}
break;
case 'f':
if (file_contexts) {
fprintf(stderr,
"May not specify more than one file context file\n");
return EXIT_FAILURE;
}
file_contexts = strdup(optarg);
if (!file_contexts) {
fprintf(stderr, "%s: Out of memory\n", argv[0]);
return EXIT_FAILURE;
}
break;
case 'o':
if (outfile) {
fprintf(stderr,
"May not specify more than one output file\n");
return EXIT_FAILURE;
}
outfile = strdup(optarg);
if (!outfile) {
fprintf(stderr, "%s: Out of memory\n", argv[0]);
return EXIT_FAILURE;
}
break;
case 's':
if (seusers) {
fprintf(stderr,
"May not specify more than one seuser file\n");
return EXIT_FAILURE;
}
seusers = strdup(optarg);
if (!seusers) {
fprintf(stderr, "%s: Out of memory\n", argv[0]);
return EXIT_FAILURE;
}
break;
case 'u':
if (user_extra) {
fprintf(stderr,
"May not specify more than one user_extra file\n");
return EXIT_FAILURE;
}
user_extra = strdup(optarg);
if (!user_extra) {
fprintf(stderr, "%s: Out of memory\n", argv[0]);
return EXIT_FAILURE;
}
break;
case 'n':
if (netfilter_contexts) {
fprintf(stderr,
"May not specify more than one netfilter contexts file\n");
return EXIT_FAILURE;
}
netfilter_contexts = strdup(optarg);
if (!netfilter_contexts) {
fprintf(stderr, "%s: Out of memory\n", argv[0]);
return EXIT_FAILURE;
}
break;
default:
usage(argv[0]);
return EXIT_FAILURE;
}
}
if (optind < argc) {
fprintf(stderr, "%s: Superfluous command line arguments: ", argv[0]);
while (optind < argc)
fprintf(stderr, "%s ", argv[optind++]);
fprintf(stderr, "\n");
usage(argv[0]);
return EXIT_FAILURE;
}
if (!module || !outfile) {
usage(argv[0]);
return EXIT_FAILURE;
}
if (file_contexts && file_to_data(file_contexts, &fcdata, &fclen, argv[0]))
goto failure;
if (seusers && file_to_data(seusers, &seusersdata, &seuserslen, argv[0]))
goto failure;
if (user_extra && file_to_data(user_extra, &user_extradata, &user_extralen, argv[0]))
goto failure;
if (netfilter_contexts && file_to_data(netfilter_contexts, &ncdata, &nclen, argv[0]))
goto failure;
if (sepol_policy_file_create(&mod)) {
fprintf(stderr, "%s: Out of memory\n", argv[0]);
goto failure;
}
fp = fopen(module, "re");
if (!fp) {
fprintf(stderr, "%s: Could not open file %s: %s\n", argv[0],
module, strerror(errno));
goto failure;
}
sepol_policy_file_set_fp(mod, fp);
if (sepol_module_package_create(&pkg)) {
fprintf(stderr, "%s: Out of memory\n", argv[0]);
goto failure;
}
if (sepol_policydb_read(sepol_module_package_get_policy(pkg), mod)) {
fprintf(stderr,
"%s: Error while reading policy module from %s\n",
argv[0], module);
goto failure;
}
fclose(fp);
fp = NULL;
if (fclen && sepol_module_package_set_file_contexts(pkg, fcdata, fclen)) {
fprintf(stderr, "%s: Error while setting file contexts\n", argv[0]);
goto failure;
}
if (seuserslen && sepol_module_package_set_seusers(pkg, seusersdata, seuserslen)) {
fprintf(stderr, "%s: Error while setting seusers\n", argv[0]);
goto failure;
}
if (user_extra && sepol_module_package_set_user_extra(pkg, user_extradata, user_extralen)) {
fprintf(stderr, "%s: Error while setting extra users\n", argv[0]);
goto failure;
}
if (nclen && sepol_module_package_set_netfilter_contexts(pkg, ncdata, nclen)) {
fprintf(stderr, "%s: Error while setting netfilter contexts\n", argv[0]);
goto failure;
}
if (sepol_policy_file_create(&out)) {
fprintf(stderr, "%s: Out of memory\n", argv[0]);
goto failure;
}
fp = fopen(outfile, "we");
if (!fp) {
fprintf(stderr, "%s: Could not open file %s: %s\n", argv[0],
outfile, strerror(errno));
goto failure;
}
sepol_policy_file_set_fp(out, fp);
if (sepol_module_package_write(pkg, out)) {
fprintf(stderr,
"%s: Error while writing module package to %s\n",
argv[0], argv[1]);
goto failure;
}
ret = fclose(fp);
fp = NULL;
if (ret) {
fprintf(stderr, "%s: Could not close file %s: %s\n", argv[0],
outfile, strerror(errno));
goto failure;
}
ret = EXIT_SUCCESS;
goto cleanup;
failure:
ret = EXIT_FAILURE;
cleanup:
if (fp)
fclose(fp);
sepol_policy_file_free(out);
if (nclen)
munmap(ncdata, nclen);
if (user_extradata)
munmap(user_extradata, user_extralen);
if (seuserslen)
munmap(seusersdata, seuserslen);
if (fclen)
munmap(fcdata, fclen);
sepol_module_package_free(pkg);
sepol_policy_file_free(mod);
free(netfilter_contexts);
free(user_extra);
free(seusers);
free(outfile);
free(file_contexts);
free(module);
return ret;
}
|