From 4324595089454a767bc0fae3016fe71551452f99 Mon Sep 17 00:00:00 2001
From: Stephen Gallagher <sgallagh@redhat.com>
Date: Sat, 28 Jan 2012 10:34:02 -0500
Subject: [PATCH 1/5] SYSDB: extend sysdb_store_service() to accept additional
 attributes

---
 src/db/sysdb.h                       |    3 ++-
 src/db/sysdb_ops.c                   |   16 +++++++++++-----
 src/db/sysdb_services.c              |   27 +++++++++++++++++++++++----
 src/db/sysdb_services.h              |    2 ++
 src/providers/proxy/proxy_services.c |    2 ++
 src/tests/sysdb-tests.c              |    8 ++++----
 6 files changed, 44 insertions(+), 14 deletions(-)

diff --git a/src/db/sysdb.h b/src/db/sysdb.h
index c4d64be1c7354c8a29b24540c70409abef56a97e..5863afc903adae9437f399892c23892429d3ca15 100644
--- a/src/db/sysdb.h
+++ b/src/db/sysdb.h
@@ -553,7 +553,8 @@ int sysdb_store_group(struct sysdb_ctx *sysdb,
 
 enum sysdb_member_type {
     SYSDB_MEMBER_USER,
-    SYSDB_MEMBER_GROUP
+    SYSDB_MEMBER_GROUP,
+    SYSDB_MEMBER_SERVICE
 };
 
 int sysdb_add_group_member(struct sysdb_ctx *sysdb,
diff --git a/src/db/sysdb_ops.c b/src/db/sysdb_ops.c
index 4c4bc5ce7724b17955a55983c7c2bf464d131d30..49567ff7eb1cde251c1bfe82816fb2972d040110 100644
--- a/src/db/sysdb_ops.c
+++ b/src/db/sysdb_ops.c
@@ -21,6 +21,7 @@
 
 #include "util/util.h"
 #include "db/sysdb_private.h"
+#include "db/sysdb_services.h"
 #include "util/crypto/sss_crypto.h"
 #include <time.h>
 
@@ -2949,21 +2950,26 @@ errno_t sysdb_remove_attrs(struct sysdb_ctx *sysdb,
     msg = ldb_msg_new(NULL);
     if (!msg) return ENOMEM;
 
-    if (type == SYSDB_MEMBER_USER) {
+    switch(type) {
+    case SYSDB_MEMBER_USER:
         msg->dn = sysdb_user_dn(sysdb, msg, sysdb->domain->name, name);
         if (!msg->dn) {
             ret = ENOMEM;
             goto done;
         }
-    } else if (type == SYSDB_MEMBER_GROUP) {
+        break;
+
+    case SYSDB_MEMBER_GROUP:
         msg->dn = sysdb_group_dn(sysdb, msg, sysdb->domain->name, name);
         if (!msg->dn) {
             ret = ENOMEM;
             goto done;
         }
-    } else {
-        ret = EINVAL;
-        goto done;
+        break;
+
+    case SYSDB_MEMBER_SERVICE:
+        msg->dn = sysdb_svc_dn(sysdb, msg, sysdb->domain->name, name);
+        break;
     }
 
     ret = sysdb_transaction_start(sysdb);
diff --git a/src/db/sysdb_services.c b/src/db/sysdb_services.c
index 55a0c438c202a4df3f879e213df1834916e83334..00917fb9345025427fb9a58d32c96b0e06dd2a35 100644
--- a/src/db/sysdb_services.c
+++ b/src/db/sysdb_services.c
@@ -183,6 +183,8 @@ sysdb_store_service(struct sysdb_ctx *sysdb,
                     int port,
                     const char **aliases,
                     const char **protocols,
+                    struct sysdb_attrs *extra_attrs,
+                    char **remove_attrs,
                     uint64_t cache_timeout,
                     time_t now)
 {
@@ -367,11 +369,16 @@ sysdb_store_service(struct sysdb_ctx *sysdb,
     if (ret != EOK) goto done;
 
     /* Set the cache timeout */
-    attrs = sysdb_new_attrs(tmp_ctx);
-    if (!attrs) {
-        ret = ENOMEM;
-        goto done;
+    if (!extra_attrs) {
+        attrs = sysdb_new_attrs(tmp_ctx);
+        if (!attrs) {
+            ret = ENOMEM;
+            goto done;
+        }
+    } else {
+        attrs = extra_attrs;
     }
+
     ret = sysdb_attrs_add_time_t(attrs, SYSDB_LAST_UPDATE, now);
     if (ret) goto done;
 
@@ -383,6 +390,18 @@ sysdb_store_service(struct sysdb_ctx *sysdb,
     ret = sysdb_set_entry_attr(sysdb, update_dn, attrs, SYSDB_MOD_REP);
     if (ret != EOK) goto done;
 
+    if (remove_attrs) {
+        ret = sysdb_remove_attrs(sysdb, name,
+                                 SYSDB_MEMBER_SERVICE,
+                                 remove_attrs);
+        if (ret != EOK) {
+            DEBUG(SSSDBG_MINOR_FAILURE,
+                  ("Could not remove missing attributes: [%s]\n",
+                   strerror(ret)));
+            goto done;
+        }
+    }
+
     ret = sysdb_transaction_commit(sysdb);
     if (ret == EOK) in_transaction = false;
 
diff --git a/src/db/sysdb_services.h b/src/db/sysdb_services.h
index c4ad1d34bb67abaf3bb2c40f10e38dfc29141246..97817d88b7a6b1f15b391109a0bfdae354bd22b7 100644
--- a/src/db/sysdb_services.h
+++ b/src/db/sysdb_services.h
@@ -70,6 +70,8 @@ sysdb_store_service(struct sysdb_ctx *sysdb,
                     int port,
                     const char **aliases,
                     const char **protocols,
+                    struct sysdb_attrs *extra_attrs,
+                    char **remove_attrs,
                     uint64_t cache_timeout,
                     time_t now);
 
diff --git a/src/providers/proxy/proxy_services.c b/src/providers/proxy/proxy_services.c
index ca098f01155e392768df6e6a69c5bd6cd445756b..79508a219b6eb9c4ae8af4f5f2e6d21ff3d75e57 100644
--- a/src/providers/proxy/proxy_services.c
+++ b/src/providers/proxy/proxy_services.c
@@ -92,6 +92,7 @@ proxy_save_service(struct sysdb_ctx *sysdb,
                               ntohs(svc->s_port),
                               cased_aliases,
                               protocols,
+                              NULL, NULL,
                               cache_timeout,
                               now);
 done:
@@ -337,6 +338,7 @@ again:
                                   svc->s_port,
                                   const_aliases,
                                   protocols,
+                                  NULL, NULL,
                                   ctx->entry_cache_timeout,
                                   now);
         if (ret) {
diff --git a/src/tests/sysdb-tests.c b/src/tests/sysdb-tests.c
index fa034cec818f8651b166d77e2b428d76abc26fdc..886491a77198a88fd0375f0d00195bb63ef47d43 100644
--- a/src/tests/sysdb-tests.c
+++ b/src/tests/sysdb-tests.c
@@ -3015,7 +3015,7 @@ START_TEST(test_sysdb_store_services)
     ret = sysdb_store_service(test_ctx->sysdb,
                               primary_name, port,
                               aliases, protocols,
-                              1, 1);
+                              NULL, NULL, 1, 1);
     fail_if (ret != EOK);
 
     /* Search by name and make sure the results match */
@@ -3032,7 +3032,7 @@ START_TEST(test_sysdb_store_services)
     ret = sysdb_store_service(test_ctx->sysdb,
                               alt_primary_name, port,
                               aliases, protocols,
-                              1, 1);
+                              NULL, NULL, 1, 1);
     fail_if (ret != EOK, "[%s]", strerror(ret));
 
     services_check_match_name(test_ctx,
@@ -3049,14 +3049,14 @@ START_TEST(test_sysdb_store_services)
     ret = sysdb_store_service(test_ctx->sysdb,
                               primary_name, port,
                               aliases, protocols,
-                              1, 1);
+                              NULL, NULL, 1, 1);
     fail_if (ret != EOK, "[%s]", strerror(ret));
 
     /* Change the port number */
     ret = sysdb_store_service(test_ctx->sysdb,
                               primary_name, altport,
                               aliases, protocols,
-                              1, 1);
+                              NULL, NULL, 1, 1);
     fail_if (ret != EOK, "[%s]", strerror(ret));
 
     /* Search by name and make sure the results match */
-- 
1.7.7.6

