From 2ca36a0b66ad976851672a918f3f1be2c733b6f3 Mon Sep 17 00:00:00 2001 From: Jarkko Sakkinen Date: Wed, 17 Nov 2010 19:21:50 -0800 Subject: [PATCH] Streamlined API. --- src/smack.h | 41 ++++++++++++--------- src/smack_xattr.c | 100 +++++++++++++++++++++++++++++----------------------- tests/check_xattr.c | 18 +++++----- 3 files changed, 88 insertions(+), 71 deletions(-) diff --git a/src/smack.h b/src/smack.h index 9e324a8..75b75bb 100644 --- a/src/smack.h +++ b/src/smack.h @@ -42,11 +42,6 @@ typedef struct smack_rules *smack_rules_t; */ typedef struct smack_users *smack_users_t; -/*! - * Flags for extended attributes. - */ -#define SMACK_XATTR_SYMLINK 1 - #ifdef __cplusplus extern "C" { #endif @@ -218,15 +213,13 @@ extern int smack_remove_user(smack_users_t handle, const char *user); const char *smack_get_user_label(smack_users_t handle, const char *user); /*! - * Set SMACK64 security attribute for a given path. + * Set SMACK64 security attribute for a given file. * * @param path path to a file * @param smack new value - * @param flags set flags * @return 0 on success */ -extern int smack_set_smack_to_file(const char *path, const char *smack, - int flags); +extern int smack_set_smack_to_file(const char *path, const char *smack); /*! * Get SMACK64 security attribute for a given path. @@ -234,11 +227,28 @@ extern int smack_set_smack_to_file(const char *path, const char *smack, * * @param path path to a file * @param smack current value - * @param flags set flags * @return 0 on success */ -extern int smack_get_smack_from_file(const char *path, char **smack, - int flags); +extern int smack_get_smack_from_file(const char *path, char **smack); + +/*! + * Set SMACK64 security attribute for a given file or symbolic link. + * + * @param path path to a file + * @param smack new value + * @return 0 on success + */ +extern int smack_set_smack_to_file_or_symlink(const char *path, const char *smack); + +/*! + * Get SMACK64 security attribute for a given file or symlink. + * Allocated memory must be freed by the caller. + * + * @param path path to a file + * @param smack current value + * @return 0 on success + */ +extern int smack_get_smack_from_file_or_symlink(const char *path, char **smack); /*! * Get SMACK64 security attribute for a given pid. @@ -254,11 +264,9 @@ extern int smack_get_smack_from_proc(int pid, char **smack); * * @param path path to a file * @param smack new value - * @param flags set flags * @return 0 on success */ -extern int smack_set_smackexec_to_file(const char *path, const char *smack, - int flags); +extern int smack_set_smackexec_to_file(const char *path, const char *smack); /*! * Get SMACK64EXEC security attribute for a given path. @@ -269,8 +277,7 @@ extern int smack_set_smackexec_to_file(const char *path, const char *smack, * @param flags set flags * @return 0 on success */ -extern int smack_get_smackexec_from_file(const char *path, char **smack, - int flags); +extern int smack_get_smackexec_from_file(const char *path, char **smack); #ifdef __cplusplus diff --git a/src/smack_xattr.c b/src/smack_xattr.c index c1fef69..b28b538 100644 --- a/src/smack_xattr.c +++ b/src/smack_xattr.c @@ -35,7 +35,7 @@ #define SMACK_PROC_PATH "/proc/%d/attr/current" #define LINE_BUFFER_SIZE 255 -int smack_set_smack_to_file(const char *path, const char *smack, int flags) +int smack_set_smack_to_file(const char *path, const char *smack) { size_t size; int ret; @@ -44,34 +44,21 @@ int smack_set_smack_to_file(const char *path, const char *smack, int flags) if (size > SMACK64_LEN) return -1; - if ((flags & SMACK_XATTR_SYMLINK) == 0) - ret = setxattr(path, SMACK64, smack, size, 0); - else - ret = lsetxattr(path, SMACK64, smack, size, 0); - - return ret; + return setxattr(path, SMACK64, smack, size, 0); } -int smack_get_smack_from_file(const char *path, char **smack, int flags) +int smack_get_smack_from_file(const char *path, char **smack) { ssize_t ret; char *buf; - if ((flags & SMACK_XATTR_SYMLINK) == 0) - ret = getxattr(path, SMACK64, NULL, 0); - else - ret = lgetxattr(path, SMACK64, NULL, 0); - + ret = getxattr(path, SMACK64, NULL, 0); if (ret < 0) return -1; buf = malloc(ret + 1); - if ((flags & SMACK_XATTR_SYMLINK) == 0) - ret = getxattr(path, SMACK64, buf, ret); - else - ret = lgetxattr(path, SMACK64, buf, ret); - + ret = getxattr(path, SMACK64, buf, ret); if (ret < 0) { free(buf); return -1; @@ -82,28 +69,42 @@ int smack_get_smack_from_file(const char *path, char **smack, int flags) return 0; } -int smack_get_smack_from_proc(int pid, char **smack) +int smack_set_smack_to_file_or_symlink(const char *path, const char *smack) { - char buf[LINE_BUFFER_SIZE]; - FILE *file; + size_t size; + int ret; - snprintf(buf, LINE_BUFFER_SIZE, SMACK_PROC_PATH, pid); + size = strlen(smack); + if (size > SMACK64_LEN) + return -1; - file = fopen(buf, "r"); - if (file == NULL) + return lsetxattr(path, SMACK64, smack, size, 0); +} + +int smack_get_smack_from_file_or_symlink(const char *path, char **smack) +{ + ssize_t ret; + char *buf; + + ret = lgetxattr(path, SMACK64, NULL, 0); + if (ret < 0) return -1; - if (fgets(buf, LINE_BUFFER_SIZE, file) == NULL) { - fclose(file); + buf = malloc(ret + 1); + + ret = lgetxattr(path, SMACK64, buf, ret); + if (ret < 0) { + free(buf); return -1; } - fclose(file); - *smack = strdup(buf); - return *smack != NULL ? 0 : - 1; + buf[ret] = '\0'; + *smack = buf; + return 0; + } -int smack_set_smackexec_to_file(const char *path, const char *smack, int flags) +int smack_set_smackexec_to_file(const char *path, const char *smack) { size_t size; int ret; @@ -112,34 +113,23 @@ int smack_set_smackexec_to_file(const char *path, const char *smack, int flags) if (size > SMACK64_LEN) return -1; - if ((flags & SMACK_XATTR_SYMLINK) == 0) - ret = setxattr(path, SMACK64EXEC, smack, size, 0); - else - ret = lsetxattr(path, SMACK64EXEC, smack, size, 0); + ret = setxattr(path, SMACK64EXEC, smack, size, 0); return ret; } -int smack_get_smackexec_from_file(const char *path, char **smack, int flags) +int smack_get_smackexec_from_file(const char *path, char **smack) { ssize_t ret; char *buf; - if ((flags & SMACK_XATTR_SYMLINK) == 0) - ret = getxattr(path, SMACK64EXEC, NULL, 0); - else - ret = lgetxattr(path, SMACK64EXEC, NULL, 0); - + ret = getxattr(path, SMACK64EXEC, NULL, 0); if (ret < 0) return -1; buf = malloc(ret + 1); - if ((flags & SMACK_XATTR_SYMLINK) == 0) - ret = getxattr(path, SMACK64EXEC, buf, ret); - else - ret = lgetxattr(path, SMACK64EXEC, buf, ret); - + ret = getxattr(path, SMACK64EXEC, buf, ret); if (ret < 0) { free(buf); return -1; @@ -150,3 +140,23 @@ int smack_get_smackexec_from_file(const char *path, char **smack, int flags) return 0; } +int smack_get_smack_from_proc(int pid, char **smack) +{ + char buf[LINE_BUFFER_SIZE]; + FILE *file; + + snprintf(buf, LINE_BUFFER_SIZE, SMACK_PROC_PATH, pid); + + file = fopen(buf, "r"); + if (file == NULL) + return -1; + + if (fgets(buf, LINE_BUFFER_SIZE, file) == NULL) { + fclose(file); + return -1; + } + + fclose(file); + *smack = strdup(buf); + return *smack != NULL ? 0 : - 1; +} diff --git a/tests/check_xattr.c b/tests/check_xattr.c index ceef60c..fd415f6 100644 --- a/tests/check_xattr.c +++ b/tests/check_xattr.c @@ -30,17 +30,17 @@ static int files_equal(const char *filename1, const char *filename2); START_TEST(test_set_smack_to_file) { FILE *file; - int rc = NULL; - char *smack; + int rc = 0; + char *smack = NULL; file = fopen("set_smack-dummy.txt", "w"); fprintf(file, "dummy\n"); fclose(file); - rc = smack_set_smack_to_file("set_smack-dummy.txt", "Apple", 0); + rc = smack_set_smack_to_file("set_smack-dummy.txt", "Apple"); fail_unless(rc == 0, "Failed to set SMACK64"); - rc = smack_get_smack_from_file("set_smack-dummy.txt", &smack, 0); + rc = smack_get_smack_from_file("set_smack-dummy.txt", &smack); fail_unless(rc == 0, "Failed to get SMACK64"); rc = strcmp(smack, "Apple"); @@ -53,15 +53,15 @@ END_TEST START_TEST(test_set_smack_to_file_symlink) { FILE *file; - int rc; + int rc = 0; char *smack = NULL; symlink("unknown.txt", "set_smack-symlink.txt"); - rc = smack_set_smack_to_file("set_smack-symlink.txt", "Apple", SMACK_XATTR_SYMLINK); + rc = smack_set_smack_to_file_or_symlink("set_smack-symlink.txt", "Apple"); fail_unless(rc == 0, "Failed to set SMACK64"); - rc = smack_get_smack_from_file("set_smack-symlink.txt", &smack, SMACK_XATTR_SYMLINK); + rc = smack_get_smack_from_file_or_symlink("set_smack-symlink.txt", &smack); fail_unless(rc == 0, "Failed to get SMACK64"); rc = strcmp(smack, "Apple"); @@ -81,10 +81,10 @@ START_TEST(test_set_smackexec_to_file) fprintf(file, "dummy\n"); fclose(file); - rc = smack_set_smackexec_to_file("set_smack-dummy.txt", "Apple", 0); + rc = smack_set_smackexec_to_file("set_smack-dummy.txt", "Apple"); fail_unless(rc == 0, "Failed to set SMACK64EXEC"); - rc = smack_get_smackexec_from_file("set_smack-dummy.txt", &smack, 0); + rc = smack_get_smackexec_from_file("set_smack-dummy.txt", &smack); fail_unless(rc == 0, "Failed to get SMACK64EXEC"); rc = strcmp(smack, "Apple"); -- 2.7.4