From 79a93ba6dc35ebe525e2d7587bc7e293e8cf3b81 Mon Sep 17 00:00:00 2001 From: Jakub Hrozek Date: Fri, 11 May 2012 16:27:46 +0200 Subject: SYSDB: Handle user and group renames better Fixes a regression in the local domain tools where sss_groupadd no longer detected a GID duplicate. The check for EEXIST is moved one level up into more high level function. The patch also adds the same rename support for users. I found it odd that we allowed a rename of groups but not users. There is a catch when storing a user -- his cached password would be gone. I think that renaming a user is such a rare operation that it's not severe, plus there is a warning in the logs. --- src/db/sysdb_ops.c | 46 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 7 deletions(-) (limited to 'src/db/sysdb_ops.c') diff --git a/src/db/sysdb_ops.c b/src/db/sysdb_ops.c index bedd8cfa..5b7dba3d 100644 --- a/src/db/sysdb_ops.c +++ b/src/db/sysdb_ops.c @@ -1165,13 +1165,8 @@ int sysdb_add_group(struct sysdb_ctx *sysdb, ret = sysdb_search_group_by_gid(tmp_ctx, sysdb, gid, NULL, &msg); if (ret != ENOENT) { - if (ret == EOK) { - ret = sysdb_delete_group(sysdb, NULL, gid); - } - - if (ret != EOK) { - goto done; - } + if (ret == EOK) ret = EEXIST; + goto done; } } @@ -1513,6 +1508,25 @@ int sysdb_store_user(struct sysdb_ctx *sysdb, /* users doesn't exist, turn into adding a user */ ret = sysdb_add_user(sysdb, name, uid, gid, gecos, homedir, shell, attrs, cache_timeout, now); + if (ret == EEXIST) { + /* This may be a user rename. If there is a user with the + * same UID, remove it and try to add the basic user again + */ + ret = sysdb_delete_user(sysdb, NULL, uid); + if (ret == ENOENT) { + /* Not found by UID, return the original EEXIST, + * this may be a conflict in MPG domain or something + * else */ + return EEXIST; + } else if (ret != EOK) { + goto done; + } + DEBUG(SSSDBG_MINOR_FAILURE, + ("A user with the same UID [%llu] was removed from the " + "cache\n", uid)); + ret = sysdb_add_user(sysdb, name, uid, gid, gecos, + homedir, shell, attrs, cache_timeout, now); + } goto done; } @@ -1641,6 +1655,24 @@ int sysdb_store_group(struct sysdb_ctx *sysdb, if (new_group) { /* group doesn't exist, turn into adding a group */ ret = sysdb_add_group(sysdb, name, gid, attrs, cache_timeout, now); + if (ret == EEXIST) { + /* This may be a group rename. If there is a group with the + * same GID, remove it and try to add the basic group again + */ + ret = sysdb_delete_group(sysdb, NULL, gid); + if (ret == ENOENT) { + /* Not found by GID, return the original EEXIST, + * this may be a conflict in MPG domain or something + * else */ + return EEXIST; + } else if (ret != EOK) { + goto done; + } + DEBUG(SSSDBG_MINOR_FAILURE, + ("A group with the same GID [%llu] was removed from the " + "cache\n", gid)); + ret = sysdb_add_group(sysdb, name, gid, attrs, cache_timeout, now); + } goto done; } -- cgit