r13616: Add new ldb functions: ldb_msg_add_steal_string() and
authorAndrew Bartlett <abartlet@samba.org>
Wed, 22 Feb 2006 09:28:58 +0000 (09:28 +0000)
committerGerald (Jerry) Carter <jerry@samba.org>
Wed, 10 Oct 2007 18:51:59 +0000 (13:51 -0500)
ldb_msg_add_steal_value().

These try to maintain the talloc heirachy, which must be correct
otherwise talloc_steal operations of entire attribute lists fails.

This fixes the currentTime value, found by using Microsoft's dcdiag
tool (before this commit, it pointed to invalid memory, due to the
changes in -r 13606)

Andrew Bartlett

source/dsdb/samdb/ldb_modules/rootdse.c
source/lib/ldb/common/ldb_msg.c
source/lib/ldb/include/ldb.h
source/lib/ldb/modules/operational.c

index 96236301b05638714d32d417bec8398b69846bbd..07e34f184189a0a67c1180c1c28514f6f2e10144 100644 (file)
@@ -64,8 +64,8 @@ static int rootdse_add_dynamic(struct ldb_module *module, struct ldb_request *re
        msg->dn = ldb_dn_explode(msg, "");
 
        if (do_attribute(s->attrs, "currentTime")) {
-               if (ldb_msg_add_string(msg, "currentTime", 
-                                      ldb_timestring(msg, time(NULL))) != 0) {
+               if (ldb_msg_add_steal_string(msg, "currentTime", 
+                                            ldb_timestring(msg, time(NULL))) != 0) {
                        goto failed;
                }
        }
@@ -77,8 +77,8 @@ static int rootdse_add_dynamic(struct ldb_module *module, struct ldb_request *re
                        if (!control) {
                                goto failed;
                        }
-                       if (ldb_msg_add_string(msg, "supportedControl",
-                                              control) != 0) {
+                       if (ldb_msg_add_steal_string(msg, "supportedControl",
+                                                    control) != 0) {
                                goto failed;
                        }
                }
@@ -95,12 +95,12 @@ static int rootdse_add_dynamic(struct ldb_module *module, struct ldb_request *re
                int i;
                for (i = 0; ops && ops[i]; i++) {
                        if (ops[i]->sasl_name) {
-                               const char *sasl_name = talloc_strdup(msg, ops[i]->sasl_name);
+                               char *sasl_name = talloc_strdup(msg, ops[i]->sasl_name);
                                if (!sasl_name) {
                                        goto failed;
                                }
-                               if (ldb_msg_add_string(msg, "supportedSASLMechanisms",
-                                                      sasl_name) != 0) {
+                               if (ldb_msg_add_steal_string(msg, "supportedSASLMechanisms",
+                                                            sasl_name) != 0) {
                                        goto failed;
                                }
                        }
index 497d2cb0d5591ce365c322d510c68d549c3447ef..8c59518296384084847164c8d9e052890d78b3b3 100644 (file)
@@ -198,6 +198,24 @@ int ldb_msg_add_value(struct ldb_message *msg,
 }
 
 
+/*
+  add a value to a message, stealing it into the 'right' place
+*/
+int ldb_msg_add_steal_value(struct ldb_message *msg, 
+                           const char *attr_name,
+                           struct ldb_val *val)
+{
+       int ret;
+       ret = ldb_msg_add_value(msg, attr_name, val);
+       if (ret == LDB_SUCCESS) {
+               struct ldb_message_element *el;
+               el = ldb_msg_find_element(msg, attr_name);
+               talloc_steal(el->values, val->data);
+       }
+       return ret;
+}
+
+
 /*
   add a string element to a message
 */
@@ -212,6 +230,20 @@ int ldb_msg_add_string(struct ldb_message *msg,
        return ldb_msg_add_value(msg, attr_name, &val);
 }
 
+/*
+  add a string element to a message, stealing it into the 'right' place
+*/
+int ldb_msg_add_steal_string(struct ldb_message *msg, 
+                            const char *attr_name, char *str)
+{
+       struct ldb_val val;
+
+       val.data = (uint8_t *)str;
+       val.length = strlen(str);
+
+       return ldb_msg_add_steal_value(msg, attr_name, &val);
+}
+
 /*
   add a printf formatted element to a message
 */
index 8a9e8bea76ad5a1d745ef71b4b5c067a242d5321..488f782c9b788faa8d9fe97d02ff08d5919352db 100644 (file)
@@ -1098,6 +1098,11 @@ int ldb_msg_add(struct ldb_message *msg,
 int ldb_msg_add_value(struct ldb_message *msg, 
                      const char *attr_name,
                      const struct ldb_val *val);
+int ldb_msg_add_steal_value(struct ldb_message *msg, 
+                     const char *attr_name,
+                     struct ldb_val *val);
+int ldb_msg_add_steal_string(struct ldb_message *msg, 
+                            const char *attr_name, char *str);
 int ldb_msg_add_string(struct ldb_message *msg, 
                       const char *attr_name, const char *str);
 int ldb_msg_add_fmt(struct ldb_message *msg, 
index 3deb9422f3a5a0b3560853c9aea650d2c253353d..8b7d6b35182fb2b6fe283f1c9b8ba1c831a82884 100644 (file)
@@ -86,7 +86,7 @@ static int construct_canonical_name(struct ldb_module *module, struct ldb_messag
        if (canonicalName == NULL) {
                return -1;
        }
-       return ldb_msg_add_string(msg, "canonicalName", canonicalName);
+       return ldb_msg_add_steal_string(msg, "canonicalName", canonicalName);
 }
 
 /*