| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#include <errno.h> |
| 21 |
#include <getopt.h> |
| 22 |
#include <libgen.h> |
| 23 |
#include <signal.h> |
| 24 |
#include <stdarg.h> |
| 25 |
#include <stdlib.h> |
| 26 |
#include <string.h> |
| 27 |
#include <unistd.h> |
| 28 |
|
| 29 |
#include <sepol/module.h> |
| 30 |
#include <sepol/module_to_cil.h> |
| 31 |
#include <sepol/policydb/module.h> |
| 32 |
|
| 33 |
char *progname; |
| 34 |
|
| 35 |
__attribute__ ((format(printf, 1, 2))) |
| 36 |
static void log_err(const char *fmt, ...) |
| 37 |
{ |
| 38 |
va_list argptr; |
| 39 |
va_start(argptr, fmt); |
| 40 |
if (vfprintf(stderr, fmt, argptr) < 0) { |
| 41 |
_exit(EXIT_FAILURE); |
| 42 |
} |
| 43 |
va_end(argptr); |
| 44 |
if (fprintf(stderr, "\n") < 0) { |
| 45 |
_exit(EXIT_FAILURE); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
static __attribute__((__noreturn__)) void usage(int err) |
| 50 |
{ |
| 51 |
fprintf(stderr, "Usage: %s [OPTIONS] [IN_FILE [OUT_FILE]]\n", progname); |
| 52 |
fprintf(stderr, "\n"); |
| 53 |
fprintf(stderr, "Read an SELinux policy package (.pp) and output the equivilent CIL.\n"); |
| 54 |
fprintf(stderr, "If IN_FILE is not provided or is -, read SELinux policy package from\n"); |
| 55 |
fprintf(stderr, "standard input. If OUT_FILE is not provided or is -, output CIL to\n"); |
| 56 |
fprintf(stderr, "standard output.\n"); |
| 57 |
fprintf(stderr, "\n"); |
| 58 |
fprintf(stderr, "Options:\n"); |
| 59 |
fprintf(stderr, " -h, --help print this message and exit\n"); |
| 60 |
exit(err); |
| 61 |
} |
| 62 |
|
| 63 |
int main(int argc, char **argv) |
| 64 |
{ |
| 65 |
int rc = -1; |
| 66 |
int opt; |
| 67 |
static struct option long_opts[] = { |
| 68 |
{ "help", 0, NULL, 'h' }, |
| 69 |
{ NULL, 0, NULL, 0 } |
| 70 |
}; |
| 71 |
struct sepol_module_package *mod_pkg = NULL; |
| 72 |
const char *ifile = NULL; |
| 73 |
const char *ofile = NULL; |
| 74 |
FILE *in = NULL; |
| 75 |
FILE *out = NULL; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
signal(SIGPIPE, SIG_IGN); |
| 80 |
|
| 81 |
progname = basename(argv[0]); |
| 82 |
|
| 83 |
while ((opt = getopt_long(argc, argv, "h", long_opts, NULL)) != -1) { |
| 84 |
switch (opt) { |
| 85 |
case 'h': |
| 86 |
usage(0); |
| 87 |
case '?': |
| 88 |
default: |
| 89 |
usage(1); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
if (argc >= optind + 1 && strcmp(argv[1], "-") != 0) { |
| 94 |
ifile = argv[1]; |
| 95 |
in = fopen(ifile, "rb"); |
| 96 |
if (in == NULL) { |
| 97 |
log_err("Failed to open %s: %s", ifile, strerror(errno)); |
| 98 |
rc = -1; |
| 99 |
goto exit; |
| 100 |
} |
| 101 |
} else { |
| 102 |
ifile = "stdin"; |
| 103 |
in = stdin; |
| 104 |
} |
| 105 |
|
| 106 |
if (argc >= optind + 2 && strcmp(argv[2], "-") != 0) { |
| 107 |
ofile = argv[2]; |
| 108 |
out = fopen(ofile, "w"); |
| 109 |
if (out == NULL) { |
| 110 |
log_err("Failed to open %s: %s", ofile, strerror(errno)); |
| 111 |
rc = -1; |
| 112 |
goto exit; |
| 113 |
} |
| 114 |
} else { |
| 115 |
out = stdout; |
| 116 |
} |
| 117 |
|
| 118 |
if (argc >= optind + 3) { |
| 119 |
log_err("Too many arguments"); |
| 120 |
usage(1); |
| 121 |
} |
| 122 |
|
| 123 |
rc = sepol_ppfile_to_module_package(in, &mod_pkg); |
| 124 |
if (rc != 0) { |
| 125 |
goto exit; |
| 126 |
} |
| 127 |
fclose(in); |
| 128 |
in = NULL; |
| 129 |
|
| 130 |
if (ofile) { |
| 131 |
char *mod_name = mod_pkg->policy->p.name; |
| 132 |
char *cil_path = strdup(ofile); |
| 133 |
if (cil_path == NULL) { |
| 134 |
log_err("No memory available for strdup\n"); |
| 135 |
rc = -1; |
| 136 |
goto exit; |
| 137 |
} |
| 138 |
char *cil_name = basename(cil_path); |
| 139 |
char *separator = strrchr(cil_name, '.'); |
| 140 |
if (separator) { |
| 141 |
*separator = '\0'; |
| 142 |
} |
| 143 |
if (mod_name && strcmp(mod_name, cil_name) != 0) { |
| 144 |
fprintf(stderr, "Warning: SELinux userspace will refer to the module from %s as %s rather than %s\n", ifile, mod_name, cil_name); |
| 145 |
} |
| 146 |
free(cil_path); |
| 147 |
} |
| 148 |
|
| 149 |
rc = sepol_module_package_to_cil(out, mod_pkg); |
| 150 |
if (rc != 0) { |
| 151 |
goto exit; |
| 152 |
} |
| 153 |
|
| 154 |
exit: |
| 155 |
if (in != NULL) { |
| 156 |
fclose(in); |
| 157 |
} |
| 158 |
if (out != NULL) { |
| 159 |
fclose(out); |
| 160 |
} |
| 161 |
sepol_module_package_free(mod_pkg); |
| 162 |
|
| 163 |
return rc; |
| 164 |
} |