From 40158438ebfcb4b2f7ed5c08117b54b79ce435be Mon Sep 17 00:00:00 2001 From: Jarkko Sakkinen Date: Mon, 15 Nov 2010 10:58:20 -0800 Subject: [PATCH] Implemented smack_get_user_label(). Fixed bug in internal function update_user(). Now HASH_ADD_KEYPTR is correctly used instead of HASH_ADD_STRING() since user name string is dynamically allocated. --- src/smack.h | 15 +++++++++++++-- src/smack_users.c | 14 +++++++++++++- tests/check_rules.c | 2 +- tests/check_users.c | 22 +++++++++++++++++++++- tests/check_xattr.c | 2 +- 5 files changed, 49 insertions(+), 6 deletions(-) diff --git a/src/smack.h b/src/smack.h index 5ae8b55..5fdc175 100644 --- a/src/smack.h +++ b/src/smack.h @@ -157,7 +157,7 @@ extern void smack_destroy_users(smack_users_t handle); /*! * Read users from a given file. * - * @param handle handle to a users db + * @param handle handle to an users db * @param path path to the file containing users * @return 0 on success */ @@ -166,13 +166,24 @@ extern int smack_read_users_from_file(smack_users_t handle, const char *path); /*! * Write users to a given file. * - * @param handle handle to a users db + * @param handle handle to an users db * @param path path to the users file * @return 0 on success */ extern int smack_write_users_to_file(smack_users_t handle, const char *path); /*! + * Get label of user. + * + * @param handle handle to an users db + * @param user user name + * + * @return pointer to a string containing label of the user. Returns NULL + * on failure. + */ +const char *smack_get_user_label(smack_users_t handle, const char *user); + +/*! * Set SMACK64 security attribute for a given path. * * @param path path to a file diff --git a/src/smack_users.c b/src/smack_users.c index c01997c..d388915 100644 --- a/src/smack_users.c +++ b/src/smack_users.c @@ -123,6 +123,18 @@ int smack_write_users_to_file(smack_users_t handle, const char *path) return 0; } +const char *smack_get_user_label(smack_users_t handle, const char *user) +{ + struct smack_user *u; + + HASH_FIND_STR(handle->users, user, u); + + if (u == NULL) + return; + + return u->label; +} + static int update_user(struct smack_user **users, const char *user, const char *label) { @@ -135,7 +147,7 @@ static int update_user(struct smack_user **users, if (u == NULL) { u = calloc(1, sizeof(struct smack_user)); u->user = strdup(user); - HASH_ADD_STR(*users, user, u); + HASH_ADD_KEYPTR( hh, *users, u->user, strlen(u->user), u); } strcpy(u->label, label); diff --git a/tests/check_rules.c b/tests/check_rules.c index 5b13661..17021ca 100644 --- a/tests/check_rules.c +++ b/tests/check_rules.c @@ -171,7 +171,7 @@ Suite *ruleset_suite (void) Suite *s; TCase *tc_core; - s = suite_create("Smack"); + s = suite_create("Rules"); tc_core = tcase_create("Rules"); tcase_add_test(tc_core, test_add_new_rule); diff --git a/tests/check_users.c b/tests/check_users.c index cf829bb..109603a 100644 --- a/tests/check_users.c +++ b/tests/check_users.c @@ -42,15 +42,35 @@ START_TEST(test_rw_users) } END_TEST +START_TEST(test_user_label) +{ + int rc; + const char *l; + + smack_users_t users = smack_create_users(); + fail_unless(users != NULL, "Users creation failed"); + + rc = smack_read_users_from_file(users, "data/rw_users-in.txt"); + fail_unless(rc == 0, "Failed to read users"); + + l = smack_get_user_label(users, "bar"); + fail_unless(l != NULL, "Label not found"); + fail_unless(strcmp(l, "Orange") == 0, "Unexcepted label %s", l); + + smack_destroy_users(users); +} +END_TEST + Suite *ruleset_suite (void) { Suite *s; TCase *tc_core; - s = suite_create("Smack"); + s = suite_create("User"); tc_core = tcase_create("Users"); tcase_add_test(tc_core, test_rw_users); + tcase_add_test(tc_core, test_user_label); suite_add_tcase(s, tc_core); return s; diff --git a/tests/check_xattr.c b/tests/check_xattr.c index d74eb9c..9dba508 100644 --- a/tests/check_xattr.c +++ b/tests/check_xattr.c @@ -99,7 +99,7 @@ Suite *ruleset_suite (void) Suite *s; TCase *tc_core; - s = suite_create("Smack"); + s = suite_create("Xattr"); tc_core = tcase_create("Xattr"); tcase_add_test(tc_core, test_set_smack_to_file); -- 2.7.4