chsmack: use of 'smack_set_label_for_path'
authorJosé Bollo <jose.bollo@open.eurogiciel.org>
Tue, 10 Dec 2013 13:43:32 +0000 (14:43 +0100)
committerJosé Bollo <jose.bollo@open.eurogiciel.org>
Tue, 17 Dec 2013 11:01:22 +0000 (12:01 +0100)
To prepare to the future libsmack that will have a function
for writing Smack labels and to prepare to the handling of
symbolic links, the write of the labels is put in the
function 'smack_set_label_for_path'.

Signed-off-by: José Bollo <jose.bollo@open.eurogiciel.org>
utils/chsmack.c

index 0d5cce5..a41bd96 100644 (file)
@@ -30,6 +30,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <getopt.h>
+#include <errno.h>
 
 static const char usage[] =
        "Usage: %s [options] <path>\n"
@@ -64,6 +65,34 @@ static ssize_t smack_label_length(const char *label)
        return -1;
 }
 
+/*!
+  * Set the SMACK label in an extended attribute.
+  *
+  * @param path path of the file
+  * @param xattr the extended attribute containing the SMACK label
+  * @param follow whether or not to follow symbolic link
+  * @param label output variable for the returned label
+  * @return Returns length of the label on success and negative value
+  * on failure.
+  */
+static int smack_set_label_for_path(const char *path,
+                                 const char *xattr,
+                                 int follow,
+                                 const char *label)
+{
+       int len;
+       int ret;
+
+       len = (int)smack_label_length(label);
+       if (len < 0)
+               return -2;
+
+       ret = follow ?
+               setxattr(path, xattr, label, len, 0) :
+               lsetxattr(path, xattr, label, len, 0);
+       return ret;
+}
+
 int main(int argc, char *argv[])
 {
        static struct option options[] = {
@@ -132,29 +161,29 @@ int main(int argc, char *argv[])
        for (i = optind; i < argc; i++) {
                if (option_flag) {
                        if (strlen(access_buf) > 0) {
-                               rc = lsetxattr(argv[i], XATTR_NAME_SMACK,
-                                              access_buf, strlen(access_buf), 0);
+                               rc = smack_set_label_for_path(argv[i],
+                                                       XATTR_NAME_SMACK, 0, access_buf);
                                if (rc < 0)
                                        perror(argv[i]);
                        }
 
                        if (strlen(exec_buf) > 0) {
-                               rc = lsetxattr(argv[i], XATTR_NAME_SMACKEXEC,
-                                              exec_buf, strlen(exec_buf), 0);
+                               rc = smack_set_label_for_path(argv[i],
+                                                       XATTR_NAME_SMACKEXEC, 0, exec_buf);
                                if (rc < 0)
                                        perror(argv[i]);
                        }
 
                        if (strlen(mmap_buf) > 0) {
-                               rc = lsetxattr(argv[i], XATTR_NAME_SMACKMMAP,
-                                              mmap_buf, strlen(mmap_buf), 0);
+                               rc = smack_set_label_for_path(argv[i],
+                                                       XATTR_NAME_SMACKMMAP, 0, mmap_buf);
                                if (rc < 0)
                                        perror(argv[i]);
                        }
 
                        if (transmute_flag) {
-                               rc = lsetxattr(argv[i], XATTR_NAME_SMACKTRANSMUTE,
-                                              "TRUE", 4, 0);
+                               rc = smack_set_label_for_path(argv[i],
+                                                       XATTR_NAME_SMACKTRANSMUTE, 0, "TRUE");
                                if (rc < 0)
                                        perror(argv[i]);
                        }