From a26ea060ec4001daf5614bd9afcc092d29174662 Mon Sep 17 00:00:00 2001 From: Jakub Hrozek Date: Mon, 12 Dec 2011 18:47:27 +0100 Subject: sysdb_get_real_name helper function --- src/db/sysdb.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ src/db/sysdb.h | 4 ++++ 2 files changed, 50 insertions(+) (limited to 'src') diff --git a/src/db/sysdb.c b/src/db/sysdb.c index c49399f7..d66cc53c 100644 --- a/src/db/sysdb.c +++ b/src/db/sysdb.c @@ -1691,3 +1691,49 @@ done: } return ret; } + +errno_t sysdb_get_real_name(TALLOC_CTX *mem_ctx, + struct sysdb_ctx *sysdb, + const char *name, + const char **_cname) +{ + errno_t ret; + TALLOC_CTX *tmp_ctx; + struct ldb_result *res; + const char *cname; + + tmp_ctx = talloc_new(NULL); + if (!tmp_ctx) { + return ENOMEM; + } + + ret = sysdb_getpwnam(tmp_ctx, sysdb, name, &res); + if (ret != EOK) { + DEBUG(SSSDBG_OP_FAILURE, ("Cannot canonicalize username\n")); + goto done; + } + + if (res->count == 0) { + /* User is not cached yet */ + ret = ENOENT; + goto done; + } else if (res->count != 1) { + DEBUG(SSSDBG_CRIT_FAILURE, + ("sysdb_getpwnam returned count: [%d]\n", res->count)); + ret = EIO; + goto done; + } + + cname = ldb_msg_find_attr_as_string(res->msgs[0], SYSDB_NAME, NULL); + if (!cname) { + DEBUG(SSSDBG_CRIT_FAILURE, ("A user with no name?\n")); + ret = ENOENT; + goto done; + } + + ret = EOK; + *_cname = talloc_steal(mem_ctx, cname); +done: + talloc_free(tmp_ctx); + return ret; +} diff --git a/src/db/sysdb.h b/src/db/sysdb.h index 5fc36ab5..e5e781f6 100644 --- a/src/db/sysdb.h +++ b/src/db/sysdb.h @@ -248,6 +248,10 @@ errno_t sysdb_attrs_primary_name_list(struct sysdb_ctx *sysdb, size_t attr_count, const char *ldap_attr, char ***name_list); +errno_t sysdb_get_real_name(TALLOC_CTX *mem_ctx, + struct sysdb_ctx *sysdb, + const char *name, + const char **_cname); /* convert an ldb error into an errno error */ int sysdb_error_to_errno(int ldberr); -- cgit