dos2unix: tiny shrink
[oweals/busybox.git] / selinux / setfiles.c
index d9fb50725251df3f17a316fc0717f66d83da08b7..905333f944138b3d1340609694ed81420ff94d3c 100644 (file)
@@ -25,14 +25,6 @@ struct globals {
        int excludeCtr;
        int errors;
        int verbose; /* getopt32 uses it, has to be int */
-       //smallint force;
-       //smallint progress;
-       //smallint debug;
-       //smallint dry_run;
-       //smallint quiet;
-       //smallint ignore_enoent;
-       //smallint take_log;
-       //smallint warn_no_match;
        smallint recurse; /* Recursive descent */
        smallint follow_mounts;
        /* Behavior flags determined based on setfiles vs. restorecon */
@@ -60,14 +52,6 @@ void BUG_setfiles_globals_too_big(void);
 #define excludeCtr         (G.excludeCtr        )
 #define errors             (G.errors            )
 #define verbose            (G.verbose           )
-//#define force              (G.force             )
-//#define progress           (G.progress          )
-//#define debug              (G.debug             )
-//#define dry_run            (G.dry_run           )
-//#define quiet              (G.quiet             )
-//#define ignore_enoent      (G.ignore_enoent     )
-//#define take_log           (G.take_log          )
-//#define warn_no_match      (G.warn_no_match     )
 #define recurse            (G.recurse           )
 #define follow_mounts      (G.follow_mounts     )
 #define expand_realpath    (G.expand_realpath   )
@@ -128,27 +112,26 @@ static void inc_err(void)
        }
 }
 
-static bool add_exclude(const char *const directory)
+static void add_exclude(const char *const directory)
 {
        struct stat sb;
        size_t len;
 
        if (directory == NULL || directory[0] != '/') {
-               bb_error_msg("full path required for exclude: %s", directory);
-               return 1;
+               bb_error_msg_and_die("full path required for exclude: %s", directory);
+
        }
        if (lstat(directory, &sb)) {
                bb_error_msg("directory \"%s\" not found, ignoring", directory);
-               return 0;
+               return;
        }
        if ((sb.st_mode & S_IFDIR) == 0) {
                bb_error_msg("\"%s\" is not a directory: mode %o, ignoring",
                        directory, sb.st_mode);
-               return 0;
+               return;
        }
        if (excludeCtr == MAX_EXCLUDES) {
-               bb_error_msg("maximum excludes %d exceeded", MAX_EXCLUDES);
-               return 1;
+               bb_error_msg_and_die("maximum excludes %d exceeded", MAX_EXCLUDES);
        }
 
        len = strlen(directory);
@@ -157,8 +140,6 @@ static bool add_exclude(const char *const directory)
        }
        excludeArray[excludeCtr].directory = xstrndup(directory, len);
        excludeArray[excludeCtr++].size = len;
-
-       return 0;
 }
 
 static bool exclude(const char *file)
@@ -208,7 +189,7 @@ static int match(const char *name, struct stat *sb, char **con)
                        file_sep = strrchr(tmp_path, '/');
                        if (file_sep == tmp_path) {
                                file_sep++;
-                               p[0] = '\0';
+                               path[0] = '\0';
                                p = path;
                        } else if (file_sep) {
                                *file_sep++ = '\0';
@@ -283,35 +264,28 @@ static bool only_changed_user(const char *a, const char *b)
 
 static int restore(const char *file)
 {
-       char *my_file = xstrdup(file);
-       char *my_file_orig = my_file;
+       char *my_file;
        struct stat my_sb;
        int i, j, ret;
        char *context = NULL;
        char *newcon = NULL;
        bool user_only_changed = 0;
-       size_t len = strlen(my_file);
        int retval = 0;
 
-       /* Skip the extra slashes at the beginning and end, if present. */
-       if (file[0] == '/' && file[1] == '/')
-               my_file++;
-       if (len > 1 && my_file[len - 1] == '/')
-               my_file[len - 1] = '\0';
+       my_file = bb_simplify_path(file);
 
        i = match(my_file, &my_sb, &newcon);
 
        if (i < 0) /* No matching specification. */
                goto out;
 
-
        if (FLAG_p_progress) {
                count++;
                if (count % 0x400 == 0) { /* every 1024 times */
                        count = (count % (80*0x400));
                        if (count == 0)
-                               fputc('\n', stdout);
-                       fputc('*', stdout);
+                               bb_putchar('\n');
+                       bb_putchar('*');
                        fflush(stdout);
                }
        }
@@ -405,7 +379,7 @@ static int restore(const char *file)
  out:
        freecon(context);
        freecon(newcon);
-       free(my_file_orig);
+       free(my_file);
        return retval;
  err:
        retval--; /* -1 */
@@ -435,7 +409,7 @@ static int apply_spec(const char *file,
 
 static int canoncon(const char *path, unsigned lineno, char **contextp)
 {
-       static const char err_msg[] = "%s: line %u has invalid context %s";
+       static const char err_msg[] ALIGN1 = "%s: line %u has invalid context %s";
 
        char *tmpcon;
        char *context = *contextp;
@@ -512,13 +486,12 @@ static int process_one(char *name)
        goto out;
 }
 
-int setfiles_main(int argc, char **argv);
+int setfiles_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int setfiles_main(int argc, char **argv)
 {
        struct stat sb;
        int rc, i = 0;
        const char *input_filename = NULL;
-       int use_input_file = 0;
        char *buf = NULL;
        size_t buf_len;
        int flags;
@@ -564,10 +537,10 @@ int setfiles_main(int argc, char **argv)
        opt_complementary = "e::vv:v--p:p--v:v--q:q--v";
        /* Option order must match OPT_x definitions! */
        if (applet_name[0] == 'r') { /* restorecon */
-               flags = getopt32(argc, argv, "de:f:ilnpqrsvo:FWR",
+               flags = getopt32(argv, "de:f:ilnpqrsvo:FWR",
                        &exclude_dir, &input_filename, &out_filename, &verbose);
        } else { /* setfiles */
-               flags = getopt32(argc, argv, "de:f:ilnpqr:svo:FW"
+               flags = getopt32(argv, "de:f:ilnpqr:svo:FW"
                                USE_FEATURE_SETFILES_CHECK_OPTION("c:"),
                        &exclude_dir, &input_filename, &rootpath, &out_filename,
                                 USE_FEATURE_SETFILES_CHECK_OPTION(&policyfile,)
@@ -593,39 +566,15 @@ int setfiles_main(int argc, char **argv)
        }
 #endif
 
-       //if (flags & OPT_d) {
-       //      debug = 1;
-       //}
-       if (flags & OPT_e) {
-               if (exclude_dir == NULL) {
-                       bb_show_usage();
-               }
-               while (exclude_dir) {
-                       if (add_exclude(llist_pop(&exclude_dir)))
-                               exit(1);
-               }
-       }
-       //if (flags & OPT_i) {
-       //      ignore_enoent = 1;
-       //}
-       //if (flags & OPT_l) {
-       //      take_log = 1;
-       //}
-       //if (flags & OPT_F) {
-       //      force = 1;
-       //}
-       //if (flags & OPT_n) {
-       //      dry_run = 1;
-       //}
+       while (exclude_dir)
+               add_exclude(llist_pop(&exclude_dir));
+
        if (flags & OPT_o) {
                outfile = stdout;
                if (NOT_LONE_CHAR(out_filename, '-')) {
                        outfile = xfopen(out_filename, "w");
                }
        }
-       //if (flags & OPT_q) {
-       //      quiet = 1;
-       //}
        if (applet_name[0] == 'r') { /* restorecon */
                if (flags & (OPT_r | OPT_R))
                        recurse = 1;
@@ -634,16 +583,9 @@ int setfiles_main(int argc, char **argv)
                        rootpathlen = strlen(rootpath);
        }
        if (flags & OPT_s) {
-               use_input_file = 1;
                input_filename = "-";
                add_assoc = 0;
        }
-       //if (flags & OPT_p) {
-       //      progress = 1;
-       //}
-       //if (flags & OPT_W) {
-       //      warn_no_match = 1;
-       //}
 
        if (applet_name[0] == 's') { /* setfiles */
                /* Use our own invalid context checking function so that
@@ -653,7 +595,7 @@ int setfiles_main(int argc, char **argv)
                if (argc == 1)
                        bb_show_usage();
                if (stat(argv[optind], &sb) < 0) {
-                       bb_perror_msg_and_die("%s", argv[optind]);
+                       bb_simple_perror_msg_and_die(argv[optind]);
                }
                if (!S_ISREG(sb.st_mode)) {
                        bb_error_msg_and_die("spec file %s is not a regular file", argv[optind]);
@@ -661,7 +603,7 @@ int setfiles_main(int argc, char **argv)
                /* Load the file contexts configuration and check it. */
                rc = matchpathcon_init(argv[optind]);
                if (rc < 0) {
-                       bb_perror_msg_and_die("%s", argv[optind]);
+                       bb_simple_perror_msg_and_die(argv[optind]);
                }
 
                optind++;
@@ -670,7 +612,7 @@ int setfiles_main(int argc, char **argv)
                        exit(1);
        }
 
-       if (use_input_file) {
+       if (input_filename) {
                ssize_t len;
                FILE *f = stdin;