From: José Bollo Date: Tue, 10 Dec 2013 13:43:32 +0000 (+0100) Subject: chsmack: use of 'smack_set_label_for_path' X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=f1dfd859b132d72ba288c92c1d6db24deec2dfec;p=framework%2Fsecurity%2Fsmack.git chsmack: use of 'smack_set_label_for_path' 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 --- diff --git a/utils/chsmack.c b/utils/chsmack.c index 0d5cce5..a41bd96 100644 --- a/utils/chsmack.c +++ b/utils/chsmack.c @@ -30,6 +30,7 @@ #include #include #include +#include static const char usage[] = "Usage: %s [options] \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]); }