From 4620c178d666279640cf4b9b3ec732ac753b93be Mon Sep 17 00:00:00 2001
From: Jakub Hrozek <jhrozek@redhat.com>
Date: Wed, 29 Sep 2010 22:15:39 +0200
Subject: [PATCH 1/2] sysdb interface for adding fake users

---
 src/db/sysdb.h                       |    8 +++++
 src/db/sysdb_ops.c                   |   49 ++++++++++++++++++++++++++++++++++
 src/providers/ldap/ldap_id_cleanup.c |    7 +++--
 src/responder/nss/nsssrv_cmd.c       |    2 +-
 4 files changed, 62 insertions(+), 4 deletions(-)

diff --git a/src/db/sysdb.h b/src/db/sysdb.h
index 8e104ad..e5de28d 100644
--- a/src/db/sysdb.h
+++ b/src/db/sysdb.h
@@ -402,6 +402,14 @@ int sysdb_add_user(TALLOC_CTX *mem_ctx,
                    struct sysdb_attrs *attrs,
                    int cache_timeout);
 
+int sysdb_add_fake_user(TALLOC_CTX *mem_ctx,
+                         struct sysdb_ctx *ctx,
+                         struct sss_domain_info *domain,
+                         const char *name,
+                         uid_t uid,
+                         gid_t gid,
+                         struct sysdb_attrs *attrs);
+
 /* Add group (only basic attrs and w/o checks) */
 int sysdb_add_basic_group(TALLOC_CTX *mem_ctx,
                           struct sysdb_ctx *ctx,
diff --git a/src/db/sysdb_ops.c b/src/db/sysdb_ops.c
index 10b04c4..a507804 100644
--- a/src/db/sysdb_ops.c
+++ b/src/db/sysdb_ops.c
@@ -844,6 +844,55 @@ done:
     return ret;
 }
 
+int sysdb_add_fake_user(TALLOC_CTX *mem_ctx,
+                        struct sysdb_ctx *ctx,
+                        struct sss_domain_info *domain,
+                        const char *name,
+                        uid_t uid,
+                        gid_t gid,
+                        struct sysdb_attrs *attrs)
+{
+    TALLOC_CTX *tmpctx;
+    time_t now;
+    int ret;
+
+    tmpctx = talloc_new(mem_ctx);
+    if (!tmpctx) {
+        return ENOMEM;
+    }
+
+    /* try to add the group */
+    ret = sysdb_add_basic_user(tmpctx, ctx, domain, name, uid, gid,
+                               NULL, NULL, NULL);
+    if (ret) goto done;
+
+    if (!attrs) {
+        attrs = sysdb_new_attrs(tmpctx);
+        if (!attrs) {
+            ret = ENOMEM;
+            goto done;
+        }
+    }
+
+    now = time(NULL);
+
+    ret = sysdb_attrs_add_time_t(attrs, SYSDB_LAST_UPDATE, now);
+    if (ret) goto done;
+
+    ret = sysdb_attrs_add_time_t(attrs, SYSDB_CACHE_EXPIRE,
+                                 now-1);
+    if (ret) goto done;
+
+    ret = sysdb_set_user_attr(tmpctx, ctx,
+                              domain, name, attrs, SYSDB_MOD_REP);
+
+done:
+    if (ret != EOK) {
+        DEBUG(6, ("Error: %d (%s)\n", ret, strerror(ret)));
+    }
+    talloc_zfree(tmpctx);
+    return ret;
+}
 
 /* =Add-Basic-Group-NO-CHECKS============================================= */
 
diff --git a/src/providers/ldap/ldap_id_cleanup.c b/src/providers/ldap/ldap_id_cleanup.c
index 60bc171..c300cfb 100644
--- a/src/providers/ldap/ldap_id_cleanup.c
+++ b/src/providers/ldap/ldap_id_cleanup.c
@@ -306,7 +306,8 @@ static int cleanup_users(TALLOC_CTX *memctx, struct sdap_id_ctx *ctx)
             ret = cleanup_users_logged_in(uid_table, msgs[i]);
             if (ret == EOK) {
                 /* If the user is logged in, proceed to the next one */
-                DEBUG(5, ("User %s is still logged in, keeping data\n", name));
+                DEBUG(5, ("User %s is still logged in or a dummy entry, "
+                          "keeping data\n", name));
                 continue;
             } else if (ret != ENOENT) {
                 goto done;
@@ -337,9 +338,9 @@ static int cleanup_users_logged_in(hash_table_t *table,
     uid = ldb_msg_find_attr_as_uint64(msg,
                                       SYSDB_UIDNUM, 0);
     if (!uid) {
-        DEBUG(2, ("Entry %s has no UID Attribute ?!?\n",
+        DEBUG(2, ("Entry %s has no UID Attribute, fake user perhaps?\n",
                   ldb_dn_get_linearized(msg->dn)));
-        return EFAULT;
+        return EOK;
     }
 
     key.type = HASH_KEY_ULONG;
diff --git a/src/responder/nss/nsssrv_cmd.c b/src/responder/nss/nsssrv_cmd.c
index fbf4d0b..e81ca0d 100644
--- a/src/responder/nss/nsssrv_cmd.c
+++ b/src/responder/nss/nsssrv_cmd.c
@@ -274,7 +274,7 @@ static int fill_pwent(struct sss_packet *packet,
         gid = ldb_msg_find_attr_as_uint64(msg, SYSDB_GIDNUM, 0);
 
         if (!name || !uid || !gid) {
-            DEBUG(1, ("Incomplete user object for %s[%llu]! Skipping\n",
+            DEBUG(2, ("Incomplete or fake user object for %s[%llu]! Skipping\n",
                       name?name:"<NULL>", (unsigned long long int)uid));
             continue;
         }
-- 
1.7.2.3

