From fbb071581f0b90ffe85499d46641038e8b55ccaa Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fabiano=20Fid=C3=AAncio?= <fidencio@redhat.com>
Date: Wed, 25 May 2016 21:12:18 +0200
Subject: [PATCH 2/4] sysdb: add sysdb_{add,replace,delete}_string()
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

As the add_string() convenience can add, replace or delete a string
according to the operation received as its argument, some confusion can
easily happen due to its misleading name.

In order to improve the explicitness of our code, let's introduce
sysdb_add_string(), sysdb_replace_string() and sysdb_delete_string().

These new functions are basically wrappers of add_string() (now
sysdb_ldb_msg_string_helper()), calling it using the proper flag
according to each function.

Any code previously using add_string() is now adapted to use these brand
new functions.

Resolves: https://fedorahosted.org/sssd/ticket/1656

Signed-off-by: Fabiano Fidêncio <fabiano@fidencio.org>
---
 src/db/sysdb.c          | 22 ++++++++++++++++++++--
 src/db/sysdb_ops.c      | 30 ++++++++++++++----------------
 src/db/sysdb_private.h  |  8 ++++++--
 src/db/sysdb_ranges.c   | 19 ++++++++-----------
 src/db/sysdb_services.c |  9 +++------
 5 files changed, 51 insertions(+), 37 deletions(-)

diff --git a/src/db/sysdb.c b/src/db/sysdb.c
index f83d236c8e2d66d9e1ce03f84ec95dbd7c858213..81223ad7463c8b961ce6011d90b97172bb402814 100644
--- a/src/db/sysdb.c
+++ b/src/db/sysdb.c
@@ -2112,8 +2112,8 @@ done:
     return EOK;
 }
 
-int add_string(struct ldb_message *msg, int flags,
-               const char *attr, const char *value)
+static int sysdb_ldb_msg_string_helper(struct ldb_message *msg, int flags,
+                                       const char *attr, const char *value)
 {
     int ret;
 
@@ -2124,3 +2124,21 @@ int add_string(struct ldb_message *msg, int flags,
     }
     return ENOMEM;
 }
+
+int sysdb_add_string(struct ldb_message *msg,
+                     const char *attr, const char *value)
+{
+    return sysdb_ldb_msg_string_helper(msg, LDB_FLAG_MOD_ADD, attr, value);
+}
+
+int sysdb_replace_string(struct ldb_message *msg,
+                         const char *attr, const char *value)
+{
+    return sysdb_ldb_msg_string_helper(msg, LDB_FLAG_MOD_REPLACE, attr, value);
+}
+
+int sysdb_delete_string(struct ldb_message *msg,
+                        const char *attr, const char *value)
+{
+    return sysdb_ldb_msg_string_helper(msg, LDB_FLAG_MOD_DELETE, attr, value);
+}
diff --git a/src/db/sysdb_ops.c b/src/db/sysdb_ops.c
index 1a93956b02078dd1b67fb3b49a18ef54d9e76fb0..62378b39cd9b4548aa49024eba586305519fb2c9 100644
--- a/src/db/sysdb_ops.c
+++ b/src/db/sysdb_ops.c
@@ -1037,10 +1037,10 @@ int sysdb_add_basic_user(struct sss_domain_info *domain,
         ERROR_OUT(ret, ENOMEM, done);
     }
 
-    ret = add_string(msg, LDB_FLAG_MOD_ADD, SYSDB_OBJECTCLASS, SYSDB_USER_CLASS);
+    ret = sysdb_add_string(msg, SYSDB_OBJECTCLASS, SYSDB_USER_CLASS);
     if (ret) goto done;
 
-    ret = add_string(msg, LDB_FLAG_MOD_ADD, SYSDB_NAME, name);
+    ret = sysdb_add_string(msg, SYSDB_NAME, name);
     if (ret) goto done;
 
     ret = add_ulong(msg, LDB_FLAG_MOD_ADD, SYSDB_UIDNUM, (unsigned long)uid);
@@ -1054,19 +1054,19 @@ int sysdb_add_basic_user(struct sss_domain_info *domain,
      * admins to decide if they want to keep it in sync if they change
      * one of the 2 */
     if (gecos && *gecos) {
-        ret = add_string(msg, LDB_FLAG_MOD_ADD, SYSDB_FULLNAME, gecos);
+        ret = sysdb_add_string(msg, SYSDB_FULLNAME, gecos);
         if (ret) goto done;
-        ret = add_string(msg, LDB_FLAG_MOD_ADD, SYSDB_GECOS, gecos);
+        ret = sysdb_add_string(msg, SYSDB_GECOS, gecos);
         if (ret) goto done;
     }
 
     if (homedir && *homedir) {
-        ret = add_string(msg, LDB_FLAG_MOD_ADD, SYSDB_HOMEDIR, homedir);
+        ret = sysdb_add_string(msg, SYSDB_HOMEDIR, homedir);
         if (ret) goto done;
     }
 
     if (shell && *shell) {
-        ret = add_string(msg, LDB_FLAG_MOD_ADD, SYSDB_SHELL, shell);
+        ret = sysdb_add_string(msg, SYSDB_SHELL, shell);
         if (ret) goto done;
     }
 
@@ -1137,11 +1137,11 @@ sysdb_remove_ghost_from_group(struct sss_domain_info *dom,
     }
 
     if (add_member) {
-        ret = add_string(msg, LDB_FLAG_MOD_ADD, SYSDB_MEMBER, userdn);
+        ret = sysdb_add_string(msg, SYSDB_MEMBER, userdn);
         if (ret) goto done;
     }
 
-    ret = add_string(msg, LDB_FLAG_MOD_DELETE, SYSDB_GHOST, name);
+    ret = sysdb_delete_string(msg, SYSDB_GHOST, name);
     if (ret) goto done;
 
     /* Delete aliases from the ghost attribute as well */
@@ -1452,10 +1452,10 @@ int sysdb_add_basic_group(struct sss_domain_info *domain,
         ERROR_OUT(ret, ENOMEM, done);
     }
 
-    ret = add_string(msg, LDB_FLAG_MOD_ADD, SYSDB_OBJECTCLASS, SYSDB_GROUP_CLASS);
+    ret = sysdb_add_string(msg, SYSDB_OBJECTCLASS, SYSDB_GROUP_CLASS);
     if (ret) goto done;
 
-    ret = add_string(msg, LDB_FLAG_MOD_ADD, SYSDB_NAME, name);
+    ret = sysdb_add_string(msg, SYSDB_NAME, name);
     if (ret) goto done;
 
     ret = add_ulong(msg, LDB_FLAG_MOD_ADD, SYSDB_GIDNUM, (unsigned long)gid);
@@ -1765,16 +1765,14 @@ int sysdb_add_basic_netgroup(struct sss_domain_info *domain,
         ERROR_OUT(ret, ENOMEM, done);
     }
 
-    ret = add_string(msg, LDB_FLAG_MOD_ADD,
-                     SYSDB_OBJECTCLASS, SYSDB_NETGROUP_CLASS);
+    ret = sysdb_add_string(msg, SYSDB_OBJECTCLASS, SYSDB_NETGROUP_CLASS);
     if (ret) goto done;
 
-    ret = add_string(msg, LDB_FLAG_MOD_ADD, SYSDB_NAME, name);
+    ret = sysdb_add_string(msg, SYSDB_NAME, name);
     if (ret) goto done;
 
     if (description && *description) {
-        ret = add_string(msg, LDB_FLAG_MOD_ADD,
-                         SYSDB_DESCRIPTION, description);
+        ret = sysdb_add_string(msg, SYSDB_DESCRIPTION, description);
         if (ret) goto done;
     }
 
@@ -2794,7 +2792,7 @@ int sysdb_delete_user(struct sss_domain_info *domain,
 
             msg->dn = msgs[i]->dn;
 
-            ret = add_string(msg, LDB_FLAG_MOD_DELETE, SYSDB_GHOST, name);
+            ret = sysdb_delete_string(msg, SYSDB_GHOST, name);
             if (ret) goto fail;
 
             ret = ldb_modify(domain->sysdb->ldb, msg);
diff --git a/src/db/sysdb_private.h b/src/db/sysdb_private.h
index c2c8d7a0585463806997c43bd020436edddd8517..75a58c6d3702502cc9ca94750b94c44920b69c72 100644
--- a/src/db/sysdb_private.h
+++ b/src/db/sysdb_private.h
@@ -124,8 +124,12 @@ int sysdb_upgrade_14(struct sysdb_ctx *sysdb, const char **ver);
 int sysdb_upgrade_15(struct sysdb_ctx *sysdb, const char **ver);
 int sysdb_upgrade_16(struct sysdb_ctx *sysdb, const char **ver);
 
-int add_string(struct ldb_message *msg, int flags,
-               const char *attr, const char *value);
+int sysdb_add_string(struct ldb_message *msg,
+                     const char *attr, const char *value);
+int sysdb_replace_string(struct ldb_message *msg,
+                         const char *attr, const char *value);
+int sysdb_delete_string(struct ldb_message *msg,
+                        const char *attr, const char *value);
 int add_ulong(struct ldb_message *msg, int flags,
               const char *attr, unsigned long value);
 
diff --git a/src/db/sysdb_ranges.c b/src/db/sysdb_ranges.c
index 431afd10e3bc405c3b9c51bd649419a7cfc5dddb..da599bf30c9a7478bf735ab8132dc2838bb59f87 100644
--- a/src/db/sysdb_ranges.c
+++ b/src/db/sysdb_ranges.c
@@ -198,13 +198,12 @@ errno_t sysdb_range_create(struct sysdb_ctx *sysdb, struct range_info *range)
         goto done;
     }
 
-    ret = add_string(msg, LDB_FLAG_MOD_ADD, SYSDB_OBJECTCLASS,
-                     SYSDB_ID_RANGE_CLASS);
+    ret = sysdb_add_string(msg, SYSDB_OBJECTCLASS, SYSDB_ID_RANGE_CLASS);
     if (ret) goto done;
 
     if (range->trusted_dom_sid == NULL && range->secondary_base_rid != 0) {
-        ret = add_string(msg, LDB_FLAG_MOD_ADD, SYSDB_OBJECTCLASS,
-                         SYSDB_DOMAIN_ID_RANGE_CLASS);
+        ret = sysdb_add_string(msg, SYSDB_OBJECTCLASS,
+                               SYSDB_DOMAIN_ID_RANGE_CLASS);
         if (ret) goto done;
 
         ret = add_ulong(msg, LDB_FLAG_MOD_ADD, SYSDB_SECONDARY_BASE_RID,
@@ -212,16 +211,15 @@ errno_t sysdb_range_create(struct sysdb_ctx *sysdb, struct range_info *range)
         if (ret) goto done;
     } else if (range->trusted_dom_sid != NULL &&
                range->secondary_base_rid == 0) {
-        ret = add_string(msg, LDB_FLAG_MOD_ADD, SYSDB_OBJECTCLASS,
-                         SYSDB_TRUSTED_AD_DOMAIN_RANGE_CLASS);
+        ret = sysdb_add_string(msg, SYSDB_OBJECTCLASS,
+                               SYSDB_TRUSTED_AD_DOMAIN_RANGE_CLASS);
         if (ret) goto done;
 
-        ret = add_string(msg, LDB_FLAG_MOD_ADD, SYSDB_DOMAIN_ID,
-                         range->trusted_dom_sid);
+        ret = sysdb_add_string(msg, SYSDB_DOMAIN_ID, range->trusted_dom_sid);
         if (ret) goto done;
     }
 
-    ret = add_string(msg, LDB_FLAG_MOD_ADD, SYSDB_NAME, range->name);
+    ret = sysdb_add_string(msg, SYSDB_NAME, range->name);
     if (ret) goto done;
 
     ret = add_ulong(msg, LDB_FLAG_MOD_ADD, SYSDB_BASE_ID,
@@ -240,8 +238,7 @@ errno_t sysdb_range_create(struct sysdb_ctx *sysdb, struct range_info *range)
                     (unsigned long)time(NULL));
     if (ret) goto done;
 
-    ret = add_string(msg, LDB_FLAG_MOD_ADD, SYSDB_ID_RANGE_TYPE,
-                     range->range_type);
+    ret = sysdb_add_string(msg, SYSDB_ID_RANGE_TYPE, range->range_type);
     if (ret) goto done;
 
     ret = ldb_add(sysdb->ldb, msg);
diff --git a/src/db/sysdb_services.c b/src/db/sysdb_services.c
index 37f7ec5ff7e36605acd3b259c1622f1b1086a388..20c88b72474f4e4e593658e720b8e931f02bd8d4 100644
--- a/src/db/sysdb_services.c
+++ b/src/db/sysdb_services.c
@@ -460,13 +460,11 @@ sysdb_svc_add(TALLOC_CTX *mem_ctx,
     }
 
     /* Objectclass */
-    ret = add_string(msg, LDB_FLAG_MOD_ADD,
-                     SYSDB_OBJECTCLASS, SYSDB_SVC_CLASS);
+    ret = sysdb_add_string(msg, SYSDB_OBJECTCLASS, SYSDB_SVC_CLASS);
     if (ret != EOK) goto done;
 
     /* Set the primary name */
-    ret = add_string(msg, LDB_FLAG_MOD_ADD,
-                     SYSDB_NAME, primary_name);
+    ret = sysdb_add_string(msg, SYSDB_NAME, primary_name);
     if (ret != EOK) goto done;
 
     /* Set the port number */
@@ -623,8 +621,7 @@ sysdb_svc_remove_alias(struct sysdb_ctx *sysdb,
 
     msg->dn = dn;
 
-    ret = add_string(msg, SYSDB_MOD_DEL,
-                     SYSDB_NAME_ALIAS, alias);
+    ret = sysdb_delete_string(msg, SYSDB_NAME_ALIAS, alias);
     if (ret != EOK) goto done;
 
     lret = ldb_modify(sysdb->ldb, msg);
-- 
2.7.4

