Fix the mess with ldb includes.
[metze/samba/wip.git] / source4 / lib / ldb / common / ldb_msg.c
index bed3647d3ad5261a735075a511bc2a7d6f5bedc2..ad53a3d29d27bc0fc1fe376c28d6272cb0c1bf0b 100644 (file)
@@ -10,7 +10,7 @@
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
-   version 2 of the License, or (at your option) any later version.
+   version 3 of the License, or (at your option) any later version.
 
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -18,8 +18,7 @@
    Lesser General Public License for more details.
 
    You should have received a copy of the GNU Lesser General Public
-   License along with this library; if not, write to the Free Software
-   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+   License along with this library; if not, see <http://www.gnu.org/licenses/>.
 */
 
 /*
@@ -32,8 +31,7 @@
  *  Author: Andrew Tridgell
  */
 
-#include "includes.h"
-#include "ldb/include/includes.h"
+#include "ldb_private.h"
 
 /*
   create a new ldb_message in a given memory context (NULL for top level)
@@ -126,11 +124,6 @@ int ldb_msg_add_empty(     struct ldb_message *msg,
 {
        struct ldb_message_element *els;
 
-       /* FIXME: we should probably leave this to the schema module to check */
-       if (! ldb_valid_attr_name(attr_name)) {
-               return LDB_ERR_OPERATIONS_ERROR;
-       }
-
        els = talloc_realloc(msg, msg->elements, 
                             struct ldb_message_element, msg->num_elements+1);
        if (!els) {
@@ -164,11 +157,14 @@ int ldb_msg_add(struct ldb_message *msg,
                const struct ldb_message_element *el, 
                int flags)
 {
+       /* We have to copy this, just in case *el is a pointer into
+        * what ldb_msg_add_empty() is about to realloc() */
+       struct ldb_message_element el_copy = *el;
        if (ldb_msg_add_empty(msg, el->name, flags, NULL) != 0) {
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
-       msg->elements[msg->num_elements-1] = *el;
+       msg->elements[msg->num_elements-1] = el_copy;
        msg->elements[msg->num_elements-1].flags = flags;
 
        return LDB_SUCCESS;
@@ -320,7 +316,8 @@ int ldb_msg_element_compare_name(struct ldb_message_element *el1,
   convenience functions to return common types from a message
   these return the first value if the attribute is multi-valued
 */
-const struct ldb_val *ldb_msg_find_ldb_val(const struct ldb_message *msg, const char *attr_name)
+const struct ldb_val *ldb_msg_find_ldb_val(const struct ldb_message *msg, 
+                                          const char *attr_name)
 {
        struct ldb_message_element *el = ldb_msg_find_element(msg, attr_name);
        if (!el || el->num_values == 0) {
@@ -392,10 +389,10 @@ int ldb_msg_find_attr_as_bool(const struct ldb_message *msg,
        if (!v || !v->data) {
                return default_value;
        }
-       if (strcasecmp((const char *)v->data, "FALSE") == 0) {
+       if (v->length == 5 && strncasecmp((const char *)v->data, "FALSE", 5) == 0) {
                return 0;
        }
-       if (strcasecmp((const char *)v->data, "TRUE") == 0) {
+       if (v->length == 4 && strncasecmp((const char *)v->data, "TRUE", 4) == 0) {
                return 1;
        }
        return default_value;
@@ -424,7 +421,7 @@ struct ldb_dn *ldb_msg_find_attr_as_dn(struct ldb_context *ldb,
        if (!v || !v->data) {
                return NULL;
        }
-       res_dn = ldb_dn_new(mem_ctx, ldb, (const char *)v->data);
+       res_dn = ldb_dn_from_ldb_val(mem_ctx, ldb, v);
        if ( ! ldb_dn_validate(res_dn)) {
                talloc_free(res_dn);
                return NULL;
@@ -667,7 +664,15 @@ const char **ldb_attr_list_copy_add(TALLOC_CTX *mem_ctx, const char * const *att
 {
        const char **ret;
        int i;
-       for (i=0;attrs[i];i++) /* noop */ ;
+       bool found = false;
+       for (i=0;attrs[i];i++) {
+               if (ldb_attr_cmp(attrs[i], new_attr) == 0) {
+                       found = true;
+               }
+       }
+       if (found) {
+               return ldb_attr_list_copy(mem_ctx, attrs);
+       }
        ret = talloc_array(mem_ctx, const char *, i+2);
        if (ret == NULL) {
                return NULL;
@@ -687,7 +692,7 @@ const char **ldb_attr_list_copy_add(TALLOC_CTX *mem_ctx, const char * const *att
 int ldb_attr_in_list(const char * const *attrs, const char *attr)
 {
        int i;
-       for (i=0;attrs[i];i++) {
+       for (i=0;attrs && attrs[i];i++) {
                if (ldb_attr_cmp(attrs[i], attr) == 0) {
                        return 1;
                }
@@ -728,22 +733,6 @@ int ldb_msg_copy_attr(struct ldb_message *msg, const char *attr, const char *rep
        return ldb_msg_rename_attr(msg, attr, replace);
 }
 
-
-/*
-  remove the specified attribute in a search result
-*/
-void ldb_msg_remove_attr(struct ldb_message *msg, const char *attr)
-{
-       struct ldb_message_element *el = ldb_msg_find_element(msg, attr);
-       if (el) {
-               int n = (el - msg->elements);
-               if (n != msg->num_elements-1) {
-                       memmove(el, el+1, ((msg->num_elements-1) - n)*sizeof(*el));
-               }
-               msg->num_elements--;
-       }
-}
-
 /*
   remove the specified element in a search result
 */
@@ -756,6 +745,18 @@ void ldb_msg_remove_element(struct ldb_message *msg, struct ldb_message_element
        msg->num_elements--;
 }
 
+
+/*
+  remove the specified attribute in a search result
+*/
+void ldb_msg_remove_attr(struct ldb_message *msg, const char *attr)
+{
+       struct ldb_message_element *el = ldb_msg_find_element(msg, attr);
+       if (el) {
+               ldb_msg_remove_element(msg, el);
+       }
+}
+
 /*
   return a LDAP formatted GeneralizedTime string
 */