2012-08-29 22:31:12 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2014-08-16 11:37:42 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2015-06-16 13:49:19 +00:00
|
|
|
#include <getopt.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <selinux/selinux.h>
|
2015-08-13 11:09:00 +00:00
|
|
|
#include <sepol/sepol.h>
|
2012-08-29 22:31:12 +00:00
|
|
|
|
|
|
|
#include "../src/label_file.h"
|
2016-09-15 16:14:33 +00:00
|
|
|
#include "../src/regex.h"
|
2012-08-29 22:31:12 +00:00
|
|
|
|
2021-05-03 15:10:46 +00:00
|
|
|
static const char *policy_file;
|
2015-08-13 11:09:00 +00:00
|
|
|
static int ctx_err;
|
|
|
|
|
|
|
|
static int validate_context(char **ctxp)
|
2015-06-16 13:49:19 +00:00
|
|
|
{
|
2015-08-13 11:09:00 +00:00
|
|
|
char *ctx = *ctxp;
|
|
|
|
|
|
|
|
if (policy_file && sepol_check_context(ctx) < 0) {
|
|
|
|
ctx_err = -1;
|
|
|
|
return ctx_err;
|
|
|
|
}
|
|
|
|
|
2015-06-16 13:49:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int process_file(struct selabel_handle *rec, const char *filename)
|
2012-08-29 22:31:12 +00:00
|
|
|
{
|
|
|
|
unsigned int line_num;
|
2015-06-16 13:49:19 +00:00
|
|
|
int rc;
|
2012-08-29 22:31:12 +00:00
|
|
|
char *line_buf = NULL;
|
2015-06-16 13:49:19 +00:00
|
|
|
size_t line_len = 0;
|
2012-08-29 22:31:12 +00:00
|
|
|
FILE *context_file;
|
2015-06-16 13:49:19 +00:00
|
|
|
const char *prefix = NULL;
|
2012-08-29 22:31:12 +00:00
|
|
|
|
|
|
|
context_file = fopen(filename, "r");
|
2012-09-14 14:51:09 +00:00
|
|
|
if (!context_file) {
|
2015-06-16 13:49:19 +00:00
|
|
|
fprintf(stderr, "Error opening %s: %s\n",
|
|
|
|
filename, strerror(errno));
|
2012-08-29 22:31:12 +00:00
|
|
|
return -1;
|
2012-09-14 14:51:09 +00:00
|
|
|
}
|
2012-08-29 22:31:12 +00:00
|
|
|
|
|
|
|
line_num = 0;
|
2015-06-22 13:11:33 +00:00
|
|
|
rc = 0;
|
2015-06-16 13:49:19 +00:00
|
|
|
while (getline(&line_buf, &line_len, context_file) > 0) {
|
|
|
|
rc = process_line(rec, filename, prefix, line_buf, ++line_num);
|
2015-08-13 11:09:00 +00:00
|
|
|
if (rc || ctx_err) {
|
|
|
|
/* With -p option need to check and fail if ctx err as
|
|
|
|
* process_line() context validation on Linux does not
|
|
|
|
* return an error, but does print the error line to
|
|
|
|
* stderr. Android will set both to error and print
|
|
|
|
* the error line. */
|
|
|
|
rc = -1;
|
2015-06-16 13:49:19 +00:00
|
|
|
goto out;
|
2015-08-13 11:09:00 +00:00
|
|
|
}
|
2012-08-29 22:31:12 +00:00
|
|
|
}
|
2015-06-16 13:49:19 +00:00
|
|
|
out:
|
2012-08-29 22:31:12 +00:00
|
|
|
free(line_buf);
|
|
|
|
fclose(context_file);
|
2015-06-16 13:49:19 +00:00
|
|
|
return rc;
|
2012-08-29 22:31:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* File Format
|
|
|
|
*
|
|
|
|
* u32 - magic number
|
|
|
|
* u32 - version
|
2014-07-09 17:02:46 +00:00
|
|
|
* u32 - length of pcre version EXCLUDING nul
|
|
|
|
* char - pcre version string EXCLUDING nul
|
2012-08-29 22:31:12 +00:00
|
|
|
* u32 - number of stems
|
|
|
|
* ** Stems
|
2015-06-16 13:49:19 +00:00
|
|
|
* u32 - length of stem EXCLUDING nul
|
|
|
|
* char - stem char array INCLUDING nul
|
2012-08-29 22:31:12 +00:00
|
|
|
* u32 - number of regexs
|
|
|
|
* ** Regexes
|
2015-06-16 13:49:19 +00:00
|
|
|
* u32 - length of upcoming context INCLUDING nul
|
|
|
|
* char - char array of the raw context
|
2012-08-29 22:31:12 +00:00
|
|
|
* u32 - length of the upcoming regex_str
|
|
|
|
* char - char array of the original regex string including the stem.
|
2015-05-27 16:14:59 +00:00
|
|
|
* u32 - mode bits for >= SELINUX_COMPILED_FCONTEXT_MODE
|
|
|
|
* mode_t for <= SELINUX_COMPILED_FCONTEXT_PCRE_VERS
|
2012-08-29 22:31:12 +00:00
|
|
|
* s32 - stemid associated with the regex
|
|
|
|
* u32 - spec has meta characters
|
2015-07-04 10:57:09 +00:00
|
|
|
* u32 - The specs prefix_len if >= SELINUX_COMPILED_FCONTEXT_PREFIX_LEN
|
2012-08-29 22:31:12 +00:00
|
|
|
* u32 - data length of the pcre regex
|
2019-08-05 20:11:20 +00:00
|
|
|
* char - a buffer holding the raw pcre regex info
|
2012-08-29 22:31:12 +00:00
|
|
|
* u32 - data length of the pcre regex study daya
|
|
|
|
* char - a buffer holding the raw pcre regex study data
|
|
|
|
*/
|
2016-09-15 16:14:33 +00:00
|
|
|
static int write_binary_file(struct saved_data *data, int fd,
|
|
|
|
int do_write_precompregex)
|
2012-08-29 22:31:12 +00:00
|
|
|
{
|
|
|
|
struct spec *specs = data->spec_arr;
|
|
|
|
FILE *bin_file;
|
|
|
|
size_t len;
|
|
|
|
uint32_t magic = SELINUX_MAGIC_COMPILED_FCONTEXT;
|
|
|
|
uint32_t section_len;
|
|
|
|
uint32_t i;
|
2013-02-06 00:31:35 +00:00
|
|
|
int rc;
|
2016-09-15 16:14:33 +00:00
|
|
|
const char *reg_version;
|
2016-09-28 10:28:30 +00:00
|
|
|
const char *reg_arch;
|
2012-08-29 22:31:12 +00:00
|
|
|
|
2013-10-09 20:32:50 +00:00
|
|
|
bin_file = fdopen(fd, "w");
|
2012-08-29 22:31:12 +00:00
|
|
|
if (!bin_file) {
|
|
|
|
perror("fopen output_file");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* write some magic number */
|
|
|
|
len = fwrite(&magic, sizeof(uint32_t), 1, bin_file);
|
|
|
|
if (len != 1)
|
2013-02-06 00:31:35 +00:00
|
|
|
goto err;
|
2012-08-29 22:31:12 +00:00
|
|
|
|
|
|
|
/* write the version */
|
|
|
|
section_len = SELINUX_COMPILED_FCONTEXT_MAX_VERS;
|
|
|
|
len = fwrite(§ion_len, sizeof(uint32_t), 1, bin_file);
|
|
|
|
if (len != 1)
|
2013-02-06 00:31:35 +00:00
|
|
|
goto err;
|
2012-08-29 22:31:12 +00:00
|
|
|
|
2016-09-15 16:14:33 +00:00
|
|
|
/* write version of the regex back-end */
|
|
|
|
reg_version = regex_version();
|
|
|
|
if (!reg_version)
|
|
|
|
goto err;
|
|
|
|
section_len = strlen(reg_version);
|
2014-07-09 17:02:46 +00:00
|
|
|
len = fwrite(§ion_len, sizeof(uint32_t), 1, bin_file);
|
|
|
|
if (len != 1)
|
|
|
|
goto err;
|
2016-09-15 16:14:33 +00:00
|
|
|
len = fwrite(reg_version, sizeof(char), section_len, bin_file);
|
2014-07-09 17:02:46 +00:00
|
|
|
if (len != section_len)
|
|
|
|
goto err;
|
|
|
|
|
2016-09-28 10:28:30 +00:00
|
|
|
/* write regex arch string */
|
|
|
|
reg_arch = regex_arch_string();
|
|
|
|
if (!reg_arch)
|
|
|
|
goto err;
|
|
|
|
section_len = strlen(reg_arch);
|
|
|
|
len = fwrite(§ion_len, sizeof(uint32_t), 1, bin_file);
|
|
|
|
if (len != 1)
|
|
|
|
goto err;
|
|
|
|
len = fwrite(reg_arch, sizeof(char), section_len, bin_file);
|
|
|
|
if (len != section_len)
|
|
|
|
goto err;
|
|
|
|
|
2012-08-29 22:31:12 +00:00
|
|
|
/* write the number of stems coming */
|
|
|
|
section_len = data->num_stems;
|
|
|
|
len = fwrite(§ion_len, sizeof(uint32_t), 1, bin_file);
|
|
|
|
if (len != 1)
|
2013-02-06 00:31:35 +00:00
|
|
|
goto err;
|
2012-08-29 22:31:12 +00:00
|
|
|
|
|
|
|
for (i = 0; i < section_len; i++) {
|
|
|
|
char *stem = data->stem_arr[i].buf;
|
|
|
|
uint32_t stem_len = data->stem_arr[i].len;
|
|
|
|
|
|
|
|
/* write the strlen (aka no nul) */
|
|
|
|
len = fwrite(&stem_len, sizeof(uint32_t), 1, bin_file);
|
|
|
|
if (len != 1)
|
2013-02-06 00:31:35 +00:00
|
|
|
goto err;
|
2012-08-29 22:31:12 +00:00
|
|
|
|
|
|
|
/* include the nul in the file */
|
|
|
|
stem_len += 1;
|
|
|
|
len = fwrite(stem, sizeof(char), stem_len, bin_file);
|
|
|
|
if (len != stem_len)
|
2013-02-06 00:31:35 +00:00
|
|
|
goto err;
|
2012-08-29 22:31:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* write the number of regexes coming */
|
|
|
|
section_len = data->nspec;
|
|
|
|
len = fwrite(§ion_len, sizeof(uint32_t), 1, bin_file);
|
|
|
|
if (len != 1)
|
2013-02-06 00:31:35 +00:00
|
|
|
goto err;
|
2012-08-29 22:31:12 +00:00
|
|
|
|
|
|
|
for (i = 0; i < section_len; i++) {
|
|
|
|
char *context = specs[i].lr.ctx_raw;
|
|
|
|
char *regex_str = specs[i].regex_str;
|
|
|
|
mode_t mode = specs[i].mode;
|
2015-07-04 10:57:09 +00:00
|
|
|
size_t prefix_len = specs[i].prefix_len;
|
2012-08-29 22:31:12 +00:00
|
|
|
int32_t stem_id = specs[i].stem_id;
|
2016-09-15 16:14:33 +00:00
|
|
|
struct regex_data *re = specs[i].regex;
|
2012-08-29 22:31:12 +00:00
|
|
|
uint32_t to_write;
|
|
|
|
|
|
|
|
/* length of the context string (including nul) */
|
|
|
|
to_write = strlen(context) + 1;
|
|
|
|
len = fwrite(&to_write, sizeof(uint32_t), 1, bin_file);
|
|
|
|
if (len != 1)
|
2013-02-06 00:31:35 +00:00
|
|
|
goto err;
|
2012-08-29 22:31:12 +00:00
|
|
|
|
|
|
|
/* original context strin (including nul) */
|
|
|
|
len = fwrite(context, sizeof(char), to_write, bin_file);
|
|
|
|
if (len != to_write)
|
2013-02-06 00:31:35 +00:00
|
|
|
goto err;
|
2012-08-29 22:31:12 +00:00
|
|
|
|
|
|
|
/* length of the original regex string (including nul) */
|
|
|
|
to_write = strlen(regex_str) + 1;
|
|
|
|
len = fwrite(&to_write, sizeof(uint32_t), 1, bin_file);
|
|
|
|
if (len != 1)
|
2013-02-06 00:31:35 +00:00
|
|
|
goto err;
|
2012-08-29 22:31:12 +00:00
|
|
|
|
|
|
|
/* original regex string */
|
|
|
|
len = fwrite(regex_str, sizeof(char), to_write, bin_file);
|
|
|
|
if (len != to_write)
|
2013-02-06 00:31:35 +00:00
|
|
|
goto err;
|
2012-08-29 22:31:12 +00:00
|
|
|
|
|
|
|
/* binary F_MODE bits */
|
2015-05-27 16:14:59 +00:00
|
|
|
to_write = mode;
|
|
|
|
len = fwrite(&to_write, sizeof(uint32_t), 1, bin_file);
|
2012-08-29 22:31:12 +00:00
|
|
|
if (len != 1)
|
2013-02-06 00:31:35 +00:00
|
|
|
goto err;
|
2012-08-29 22:31:12 +00:00
|
|
|
|
|
|
|
/* stem for this regex (could be -1) */
|
|
|
|
len = fwrite(&stem_id, sizeof(stem_id), 1, bin_file);
|
|
|
|
if (len != 1)
|
2013-02-06 00:31:35 +00:00
|
|
|
goto err;
|
2012-08-29 22:31:12 +00:00
|
|
|
|
|
|
|
/* does this spec have a metaChar? */
|
|
|
|
to_write = specs[i].hasMetaChars;
|
|
|
|
len = fwrite(&to_write, sizeof(to_write), 1, bin_file);
|
|
|
|
if (len != 1)
|
2013-02-06 00:31:35 +00:00
|
|
|
goto err;
|
2012-08-29 22:31:12 +00:00
|
|
|
|
2015-07-04 10:57:09 +00:00
|
|
|
/* For SELINUX_COMPILED_FCONTEXT_PREFIX_LEN */
|
|
|
|
to_write = prefix_len;
|
|
|
|
len = fwrite(&to_write, sizeof(to_write), 1, bin_file);
|
|
|
|
if (len != 1)
|
|
|
|
goto err;
|
|
|
|
|
2016-09-15 16:14:33 +00:00
|
|
|
/* Write regex related data */
|
|
|
|
rc = regex_writef(re, bin_file, do_write_precompregex);
|
2012-08-29 22:31:12 +00:00
|
|
|
if (rc < 0)
|
2013-02-06 00:31:35 +00:00
|
|
|
goto err;
|
2012-08-29 22:31:12 +00:00
|
|
|
}
|
|
|
|
|
2013-02-06 00:31:35 +00:00
|
|
|
rc = 0;
|
|
|
|
out:
|
2012-08-29 22:31:12 +00:00
|
|
|
fclose(bin_file);
|
2013-02-06 00:31:35 +00:00
|
|
|
return rc;
|
|
|
|
err:
|
|
|
|
rc = -1;
|
|
|
|
goto out;
|
2012-08-29 22:31:12 +00:00
|
|
|
}
|
|
|
|
|
2015-06-16 13:49:19 +00:00
|
|
|
static void free_specs(struct saved_data *data)
|
2012-08-29 22:31:12 +00:00
|
|
|
{
|
|
|
|
struct spec *specs = data->spec_arr;
|
|
|
|
unsigned int num_entries = data->nspec;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = 0; i < num_entries; i++) {
|
|
|
|
free(specs[i].lr.ctx_raw);
|
|
|
|
free(specs[i].lr.ctx_trans);
|
|
|
|
free(specs[i].regex_str);
|
2015-06-16 13:49:19 +00:00
|
|
|
free(specs[i].type_str);
|
2016-09-15 16:14:33 +00:00
|
|
|
regex_data_free(specs[i].regex);
|
2012-08-29 22:31:12 +00:00
|
|
|
}
|
|
|
|
free(specs);
|
|
|
|
|
|
|
|
num_entries = data->num_stems;
|
2015-06-16 13:49:19 +00:00
|
|
|
for (i = 0; i < num_entries; i++)
|
2012-08-29 22:31:12 +00:00
|
|
|
free(data->stem_arr[i].buf);
|
|
|
|
free(data->stem_arr);
|
|
|
|
|
|
|
|
memset(data, 0, sizeof(*data));
|
2015-06-16 13:49:19 +00:00
|
|
|
}
|
|
|
|
|
2016-10-17 20:24:07 +00:00
|
|
|
static __attribute__ ((__noreturn__)) void usage(const char *progname)
|
2015-06-16 13:49:19 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr,
|
2015-08-13 11:09:00 +00:00
|
|
|
"usage: %s [-o out_file] [-p policy_file] fc_file\n"
|
|
|
|
"Where:\n\t"
|
|
|
|
"-o Optional file name of the PCRE formatted binary\n\t"
|
|
|
|
" file to be output. If not specified the default\n\t"
|
|
|
|
" will be fc_file with the .bin suffix appended.\n\t"
|
|
|
|
"-p Optional binary policy file that will be used to\n\t"
|
|
|
|
" validate contexts defined in the fc_file.\n\t"
|
2016-09-28 10:28:32 +00:00
|
|
|
"-r Omit precompiled regular expressions from the output.\n\t"
|
2016-09-15 16:14:33 +00:00
|
|
|
" (PCRE2 only. Compiled PCRE2 regular expressions are\n\t"
|
2016-09-28 10:28:32 +00:00
|
|
|
" not portable across architectures. Use this flag\n\t"
|
|
|
|
" if you know that you build for an incompatible\n\t"
|
|
|
|
" architecture to save space. When linked against\n\t"
|
|
|
|
" PCRE1 this flag is ignored.)\n\t"
|
2016-09-28 10:28:31 +00:00
|
|
|
"-i Print regular expression info end exit. That is, back\n\t"
|
|
|
|
" end version and architecture identifier.\n\t"
|
|
|
|
" Arch identifier format (PCRE2):\n\t"
|
|
|
|
" <pointer width>-<size type width>-<endianness>, e.g.,\n\t"
|
|
|
|
" \"8-8-el\" for x86_64.\n\t"
|
2015-08-13 11:09:00 +00:00
|
|
|
"fc_file The text based file contexts file to be processed.\n",
|
|
|
|
progname);
|
2015-06-16 13:49:19 +00:00
|
|
|
exit(EXIT_FAILURE);
|
2012-08-29 22:31:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2015-06-16 13:49:19 +00:00
|
|
|
const char *path = NULL;
|
|
|
|
const char *out_file = NULL;
|
2016-09-28 10:28:32 +00:00
|
|
|
int do_write_precompregex = 1;
|
2012-08-29 22:31:12 +00:00
|
|
|
char stack_path[PATH_MAX + 1];
|
2015-06-16 13:49:19 +00:00
|
|
|
char *tmp = NULL;
|
|
|
|
int fd, rc, opt;
|
2015-08-13 11:09:00 +00:00
|
|
|
FILE *policy_fp = NULL;
|
2014-08-16 11:37:42 +00:00
|
|
|
struct stat buf;
|
2015-06-16 13:49:19 +00:00
|
|
|
struct selabel_handle *rec = NULL;
|
|
|
|
struct saved_data *data = NULL;
|
|
|
|
|
|
|
|
if (argc < 2)
|
|
|
|
usage(argv[0]);
|
|
|
|
|
2016-09-28 10:28:31 +00:00
|
|
|
while ((opt = getopt(argc, argv, "io:p:r")) > 0) {
|
2015-06-16 13:49:19 +00:00
|
|
|
switch (opt) {
|
|
|
|
case 'o':
|
|
|
|
out_file = optarg;
|
|
|
|
break;
|
2015-08-13 11:09:00 +00:00
|
|
|
case 'p':
|
|
|
|
policy_file = optarg;
|
|
|
|
break;
|
2016-09-15 16:14:33 +00:00
|
|
|
case 'r':
|
2016-09-28 10:28:32 +00:00
|
|
|
do_write_precompregex = 0;
|
2016-09-15 16:14:33 +00:00
|
|
|
break;
|
2016-09-28 10:28:31 +00:00
|
|
|
case 'i':
|
|
|
|
printf("%s (%s)\n", regex_version(),
|
|
|
|
regex_arch_string());
|
|
|
|
return 0;
|
2015-06-16 13:49:19 +00:00
|
|
|
default:
|
|
|
|
usage(argv[0]);
|
|
|
|
}
|
2012-08-29 22:31:12 +00:00
|
|
|
}
|
|
|
|
|
2015-06-16 13:49:19 +00:00
|
|
|
if (optind >= argc)
|
|
|
|
usage(argv[0]);
|
2012-08-29 22:31:12 +00:00
|
|
|
|
2015-06-16 13:49:19 +00:00
|
|
|
path = argv[optind];
|
2014-08-16 11:37:42 +00:00
|
|
|
if (stat(path, &buf) < 0) {
|
2016-09-26 18:46:31 +00:00
|
|
|
fprintf(stderr, "%s: could not stat: %s: %s\n", argv[0], path, strerror(errno));
|
2014-08-16 11:37:42 +00:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2015-08-13 11:09:00 +00:00
|
|
|
/* Open binary policy if supplied. */
|
|
|
|
if (policy_file) {
|
|
|
|
policy_fp = fopen(policy_file, "r");
|
|
|
|
|
|
|
|
if (!policy_fp) {
|
2016-09-26 18:46:31 +00:00
|
|
|
fprintf(stderr, "%s: failed to open %s: %s\n",
|
|
|
|
argv[0], policy_file, strerror(errno));
|
2015-08-13 11:09:00 +00:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sepol_set_policydb_from_file(policy_fp) < 0) {
|
2016-09-26 18:46:31 +00:00
|
|
|
fprintf(stderr, "%s: failed to load policy from %s\n",
|
|
|
|
argv[0], policy_file);
|
2015-08-13 11:09:00 +00:00
|
|
|
fclose(policy_fp);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-16 13:49:19 +00:00
|
|
|
/* Generate dummy handle for process_line() function */
|
|
|
|
rec = (struct selabel_handle *)calloc(1, sizeof(*rec));
|
|
|
|
if (!rec) {
|
2016-09-26 18:46:31 +00:00
|
|
|
fprintf(stderr, "%s: calloc failed: %s\n", argv[0], strerror(errno));
|
2015-08-13 11:09:00 +00:00
|
|
|
if (policy_fp)
|
|
|
|
fclose(policy_fp);
|
2015-06-16 13:49:19 +00:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
rec->backend = SELABEL_CTX_FILE;
|
|
|
|
|
|
|
|
/* Need to set validation on to get the bin file generated by the
|
|
|
|
* process_line function, however as the bin file being generated
|
|
|
|
* may not be related to the currently loaded policy (that it
|
|
|
|
* would be validated against), then set callback to ignore any
|
2015-08-13 11:09:00 +00:00
|
|
|
* validation - unless the -p option is used in which case if an
|
|
|
|
* error is detected, the process will be aborted. */
|
2015-06-16 13:49:19 +00:00
|
|
|
rec->validating = 1;
|
|
|
|
selinux_set_callback(SELINUX_CB_VALIDATE,
|
|
|
|
(union selinux_callback)&validate_context);
|
|
|
|
|
|
|
|
data = (struct saved_data *)calloc(1, sizeof(*data));
|
|
|
|
if (!data) {
|
2016-09-26 18:46:31 +00:00
|
|
|
fprintf(stderr, "%s: calloc failed: %s\n", argv[0], strerror(errno));
|
2015-06-16 13:49:19 +00:00
|
|
|
free(rec);
|
2015-08-13 11:09:00 +00:00
|
|
|
if (policy_fp)
|
|
|
|
fclose(policy_fp);
|
2015-06-16 13:49:19 +00:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
rec->data = data;
|
|
|
|
|
|
|
|
rc = process_file(rec, path);
|
2016-09-26 18:46:31 +00:00
|
|
|
if (rc < 0) {
|
|
|
|
fprintf(stderr, "%s: process_file failed\n", argv[0]);
|
2015-06-16 13:49:19 +00:00
|
|
|
goto err;
|
2016-09-26 18:46:31 +00:00
|
|
|
}
|
2012-08-29 22:31:12 +00:00
|
|
|
|
2015-06-16 13:49:19 +00:00
|
|
|
rc = sort_specs(data);
|
2016-09-26 18:46:31 +00:00
|
|
|
if (rc) {
|
|
|
|
fprintf(stderr, "%s: sort_specs failed\n", argv[0]);
|
2015-06-16 13:49:19 +00:00
|
|
|
goto err;
|
2016-09-26 18:46:31 +00:00
|
|
|
}
|
2015-06-16 13:49:19 +00:00
|
|
|
|
|
|
|
if (out_file)
|
|
|
|
rc = snprintf(stack_path, sizeof(stack_path), "%s", out_file);
|
|
|
|
else
|
|
|
|
rc = snprintf(stack_path, sizeof(stack_path), "%s.bin", path);
|
2012-08-29 22:31:12 +00:00
|
|
|
|
2016-09-26 18:46:31 +00:00
|
|
|
if (rc < 0 || rc >= (int)sizeof(stack_path)) {
|
|
|
|
fprintf(stderr, "%s: snprintf failed\n", argv[0]);
|
2015-06-16 13:49:19 +00:00
|
|
|
goto err;
|
2016-09-26 18:46:31 +00:00
|
|
|
}
|
2013-10-09 20:32:50 +00:00
|
|
|
|
2015-06-16 13:49:19 +00:00
|
|
|
tmp = malloc(strlen(stack_path) + 7);
|
2016-09-26 18:46:31 +00:00
|
|
|
if (!tmp) {
|
|
|
|
fprintf(stderr, "%s: malloc failed: %s\n", argv[0], strerror(errno));
|
2015-06-16 13:49:19 +00:00
|
|
|
goto err;
|
2016-09-26 18:46:31 +00:00
|
|
|
}
|
2015-06-16 13:49:19 +00:00
|
|
|
|
|
|
|
rc = sprintf(tmp, "%sXXXXXX", stack_path);
|
2016-09-26 18:46:31 +00:00
|
|
|
if (rc < 0) {
|
|
|
|
fprintf(stderr, "%s: sprintf failed\n", argv[0]);
|
2015-06-16 13:49:19 +00:00
|
|
|
goto err;
|
2016-09-26 18:46:31 +00:00
|
|
|
}
|
2013-10-09 20:32:50 +00:00
|
|
|
|
|
|
|
fd = mkstemp(tmp);
|
2016-09-26 18:46:31 +00:00
|
|
|
if (fd < 0) {
|
|
|
|
fprintf(stderr, "%s: mkstemp %s failed: %s\n", argv[0], tmp, strerror(errno));
|
2013-10-09 20:32:50 +00:00
|
|
|
goto err;
|
2016-09-26 18:46:31 +00:00
|
|
|
}
|
2013-10-09 20:32:50 +00:00
|
|
|
|
2014-08-16 11:37:42 +00:00
|
|
|
rc = fchmod(fd, buf.st_mode);
|
|
|
|
if (rc < 0) {
|
2016-09-26 18:46:31 +00:00
|
|
|
fprintf(stderr, "%s: fchmod %s failed: %s\n", argv[0], tmp, strerror(errno));
|
2015-06-16 13:49:19 +00:00
|
|
|
goto err_unlink;
|
2014-08-16 11:37:42 +00:00
|
|
|
}
|
|
|
|
|
2016-09-15 16:14:33 +00:00
|
|
|
rc = write_binary_file(data, fd, do_write_precompregex);
|
2016-09-26 18:46:31 +00:00
|
|
|
if (rc < 0) {
|
|
|
|
fprintf(stderr, "%s: write_binary_file %s failed\n", argv[0], tmp);
|
2015-06-16 13:49:19 +00:00
|
|
|
goto err_unlink;
|
2016-09-26 18:46:31 +00:00
|
|
|
}
|
2012-08-29 22:31:12 +00:00
|
|
|
|
2015-06-16 13:49:19 +00:00
|
|
|
rc = rename(tmp, stack_path);
|
2016-09-26 18:46:31 +00:00
|
|
|
if (rc < 0) {
|
|
|
|
fprintf(stderr, "%s: rename %s -> %s failed: %s\n", argv[0], tmp, stack_path, strerror(errno));
|
2015-06-16 13:49:19 +00:00
|
|
|
goto err_unlink;
|
2016-09-26 18:46:31 +00:00
|
|
|
}
|
2012-08-29 22:31:12 +00:00
|
|
|
|
2013-10-09 20:32:50 +00:00
|
|
|
rc = 0;
|
|
|
|
out:
|
2015-08-13 11:09:00 +00:00
|
|
|
if (policy_fp)
|
|
|
|
fclose(policy_fp);
|
|
|
|
|
2015-06-16 13:49:19 +00:00
|
|
|
free_specs(data);
|
|
|
|
free(rec);
|
|
|
|
free(data);
|
2013-10-09 20:32:50 +00:00
|
|
|
free(tmp);
|
|
|
|
return rc;
|
2015-06-16 13:49:19 +00:00
|
|
|
|
|
|
|
err_unlink:
|
|
|
|
unlink(tmp);
|
2013-10-09 20:32:50 +00:00
|
|
|
err:
|
|
|
|
rc = -1;
|
|
|
|
goto out;
|
2012-08-29 22:31:12 +00:00
|
|
|
}
|