From 544e20ffff6da79a33947efd4413c84ec558ac42 Mon Sep 17 00:00:00 2001 From: Roman Rakus Date: Tue, 11 Dec 2012 15:00:14 +0100 Subject: Account: Allow to delete instance of LMI_MemberOfGroup Effectively it means this allows to remove user from group Signed-off-by: Roman Rakus --- src/account/LMI_MemberOfGroupProvider.c | 77 ++++++++++++++++++++++++++++++++- 1 file changed, 76 insertions(+), 1 deletion(-) diff --git a/src/account/LMI_MemberOfGroupProvider.c b/src/account/LMI_MemberOfGroupProvider.c index a2bd1c8..e3480cd 100644 --- a/src/account/LMI_MemberOfGroupProvider.c +++ b/src/account/LMI_MemberOfGroupProvider.c @@ -171,7 +171,82 @@ static CMPIStatus LMI_MemberOfGroupDeleteInstance( const CMPIResult* cr, const CMPIObjectPath* cop) { - CMReturn(CMPI_RC_ERR_NOT_SUPPORTED); + LMI_MemberOfGroup lmog; + LMI_GroupRef lg_ref; + LMI_IdentityRef li_ref; + + struct lu_context *luc = NULL; + struct lu_ent *lue_g = lu_ent_new(); + struct lu_ent *lue_u = lu_ent_new(); + struct lu_error *error = NULL; + + char *group_name = NULL; + uid_t user_id = -1; + GValueArray *groups = NULL; + char errmsg[256]; + unsigned int i = 0; /* iterator */ + int found = 0; /* indicator */ + + LMI_MemberOfGroup_InitFromObjectPath(&lmog, _cb, cop); + LMI_GroupRef_InitFromObjectPath(&lg_ref, _cb, lmog.Collection.value); + LMI_IdentityRef_InitFromObjectPath(&li_ref, _cb, lmog.Member.value); + + group_name = lg_ref.Name.chars; + user_id = (uid_t)atol(strrchr(li_ref.InstanceID.chars, ':') + 1); + + luc = lu_start(NULL, 0, NULL, NULL, lu_prompt_console_quiet, NULL, &error); + if (!luc) { + fprintf(stderr, "Error initializing: %s\n", lu_strerror(error)); + exit(1); + } + + if (!lu_user_lookup_id(luc, user_id, lue_u, &error)) { + snprintf(errmsg, 256, "User with id %d not found: %s\n", user_id, + lu_strerror(error)); + lu_end(luc); + CMReturnWithChars(_cb, CMPI_RC_ERR_FAILED, errmsg); + } + + if (!lu_group_lookup_name(luc, group_name, lue_g, &error)) { + snprintf(errmsg, 256, "Group with name %s not found: %s\n", group_name, + lu_strerror(error)); + lu_ent_free(lue_u); + lu_end(luc); + CMReturnWithChars(_cb, CMPI_RC_ERR_FAILED, errmsg); + } + groups = lu_ent_get(lue_g, LU_MEMBERNAME); + for (found = 0, i = 0; groups && i < groups->n_values; i++) { + if (0 != strcmp(group_name, g_value_get_string(g_value_array_get_nth( + groups, i)))) { + found = 1; + break; + } + } + if (!found) { + snprintf(errmsg, 256, "User with id %d is not in group %s or is " + "users' primary group\n", user_id, group_name); + lu_ent_free(lue_u); + lu_ent_free(lue_g); + lu_end(luc); + CMReturnWithChars(_cb, CMPI_RC_ERR_FAILED, errmsg); + } else { + /* And now remove the user from the group */ + g_value_array_remove(groups, i); + lu_ent_set(lue_g, LU_MEMBERNAME, groups); + if(!lu_group_modify(luc, lue_g, &error)) { + snprintf(errmsg, 256, "Modification of group %s failed: %s\n", + group_name, lu_strerror(error)); + lu_ent_free(lue_u); + lu_ent_free(lue_g); + lu_end(luc); + CMReturnWithChars(_cb, CMPI_RC_ERR_FAILED, errmsg); + } + } + + lu_ent_free(lue_u); + lu_ent_free(lue_g); + lu_end(luc); + CMReturn(CMPI_RC_OK); } static CMPIStatus LMI_MemberOfGroupExecQuery( -- cgit