Revert "utils: use common code for apply_rules and apply_cipso"
authorJarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
Mon, 9 Dec 2013 13:05:27 +0000 (15:05 +0200)
committerJarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
Mon, 9 Dec 2013 13:05:27 +0000 (15:05 +0200)
This reverts commit 68e38ff3936597a3189d29f57a68dae5ac08db1e.

utils/common.c
utils/common.h
utils/smackcipso.c

index c429001..1b6efdd 100644 (file)
 #include <dirent.h>
 #include <errno.h>
 #include <fcntl.h>
+#include <ftw.h>
 #include <stdio.h>
 #include <string.h>
 #include <sys/smack.h>
 
+#define SMACK_MAGIC 0x43415d53
+
+static int apply_cipso_cb(const char *fpath, const struct stat *sb,
+                         int typeflag, struct FTW *ftwbuf);
+
 int clear(void)
 {
        int fd;
@@ -50,8 +56,7 @@ int clear(void)
        return ret;
 }
 
-static int apply_dir(const char *path,
-       int (*func)(const char *path, int fd, int arg), int arg)
+int apply_rules(const char *path, int clear)
 {
        struct smack_accesses *rules = NULL;
        DIR *dir;
@@ -94,7 +99,7 @@ static int apply_dir(const char *path,
                                break;
                        }
 
-                       ret = func(dent->d_name, fd, arg);
+                       ret = smack_accesses_add_from_file(rules, fd);
                        close(fd);
                        if (ret < 0) {
                                fprintf(stderr, "Reading rules from '%s' failed.\n",
@@ -131,19 +136,36 @@ static int apply_dir(const char *path,
                return -1;
        }
 
-       ret = func(path, fd, arg);
+       ret = apply_rules_file(path, fd, clear);
        close(fd);
        return ret;
 }
 
-int apply_rules(const char *path, int clear)
-{
-       return apply_dir(path, apply_rules_file, 0);
-}
-
 int apply_cipso(const char *path)
 {
-       return apply_dir(path, apply_cipso_file, 0);
+       struct stat sbuf;
+       int fd;
+       int ret;
+
+       if (stat(path, &sbuf)) {
+               fprintf(stderr, "stat() failed for '%s' : %s\n", path,
+                       strerror(errno));
+               return -1;
+       }
+
+       if (S_ISDIR(sbuf.st_mode))
+               return nftw(path, apply_cipso_cb, 1, FTW_PHYS|FTW_ACTIONRETVAL);
+
+       fd = open(path, O_RDONLY);
+       if (fd < 0) {
+               fprintf(stderr, "open() failed for '%s' : %s\n", path,
+                       strerror(errno));
+               return -1;
+       }
+
+       ret = apply_cipso_file(path, fd);
+       close(fd);
+       return ret;
 }
 
 int apply_rules_file(const char *path, int fd, int clear)
@@ -178,7 +200,7 @@ int apply_rules_file(const char *path, int fd, int clear)
        return ret;
 }
 
-int apply_cipso_file(const char *path, int fd, int arg /* not used */)
+int apply_cipso_file(const char *path, int fd)
 {
        struct smack_cipso *cipso = NULL;
        int ret;
@@ -208,3 +230,26 @@ int apply_cipso_file(const char *path, int fd, int arg /* not used */)
 
        return 0;
 }
+
+static int apply_cipso_cb(const char *fpath, const struct stat *sb,
+                         int typeflag, struct FTW *ftwbuf)
+{
+       int fd;
+       int ret;
+
+       if (typeflag == FTW_D)
+               return ftwbuf->level ? FTW_SKIP_SUBTREE : FTW_CONTINUE;
+       else if (typeflag != FTW_F)
+               return FTW_STOP;
+
+       fd = open(fpath, O_RDONLY);
+       if (fd < 0) {
+               fprintf(stderr, "open() failed for '%s' : %s\n", fpath,
+                       strerror(errno));
+               return -1;
+       }
+
+       ret = apply_cipso_file(fpath, fd) ? FTW_STOP : FTW_CONTINUE;
+       close(fd);
+       return ret;
+}
index 2249361..1eab142 100644 (file)
@@ -28,6 +28,6 @@ int clear(void);
 int apply_rules(const char *path, int clear);
 int apply_cipso(const char *path);
 int apply_rules_file(const char *path, int fd, int clear);
-int apply_cipso_file(const char *path, int fd, int arg);
+int apply_cipso_file(const char *path, int fd);
 
 #endif // COMMON_H
index 3e2d9fc..643f080 100644 (file)
@@ -36,7 +36,7 @@ int main(int argc, char **argv)
        }
 
        if (argc == 1) {
-               if (apply_cipso_file(NULL, STDIN_FILENO, 0))
+               if (apply_cipso_file(NULL, STDIN_FILENO))
                        exit(1);
        } else {
                if (apply_cipso(argv[1]))