s4-ldb: use TYPESAFE_QSORT() in the rest of the ldb code
[ira/wip.git] / source4 / lib / ldb / common / ldb_msg.c
index d40dcde010ace2c7c180139eec421d642cd10545..f4adb560b1dfe8521b478bee670f339013946745 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)
@@ -65,7 +63,7 @@ struct ldb_message_element *ldb_msg_find_element(const struct ldb_message *msg,
 int ldb_val_equal_exact(const struct ldb_val *v1, const struct ldb_val *v2)
 {
        if (v1->length != v2->length) return 0;
-
+       if (v1->data == v2->data) return 1;
        if (v1->length == 0) return 1;
 
        if (memcmp(v1->data, v2->data, v1->length) == 0) {
@@ -119,19 +117,18 @@ struct ldb_val ldb_val_dup(void *mem_ctx, const struct ldb_val *v)
 /*
   add an empty element to a message
 */
-int ldb_msg_add_empty(struct ldb_message *msg, const char *attr_name, int flags)
+int ldb_msg_add_empty( struct ldb_message *msg,
+                       const char *attr_name,
+                       int flags,
+                       struct ldb_message_element **return_el)
 {
        struct ldb_message_element *els;
 
-       if (! ldb_valid_attr_name(attr_name)) {
-               return -1;
-       }
-
        els = talloc_realloc(msg, msg->elements, 
                             struct ldb_message_element, msg->num_elements+1);
        if (!els) {
                errno = ENOMEM;
-               return -1;
+               return LDB_ERR_OPERATIONS_ERROR;
        }
 
        els[msg->num_elements].values = NULL;
@@ -140,13 +137,17 @@ int ldb_msg_add_empty(struct ldb_message *msg, const char *attr_name, int flags)
        els[msg->num_elements].name = talloc_strdup(els, attr_name);
        if (!els[msg->num_elements].name) {
                errno = ENOMEM;
-               return -1;
+               return LDB_ERR_OPERATIONS_ERROR;
        }
 
        msg->elements = els;
        msg->num_elements++;
 
-       return 0;
+       if (return_el) {
+               *return_el = &els[msg->num_elements-1];
+       }
+
+       return LDB_SUCCESS;
 }
 
 /*
@@ -156,14 +157,17 @@ int ldb_msg_add(struct ldb_message *msg,
                const struct ldb_message_element *el, 
                int flags)
 {
-       if (ldb_msg_add_empty(msg, el->name, flags) != 0) {
-               return -1;
+       /* 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 0;
+       return LDB_SUCCESS;
 }
 
 /*
@@ -171,30 +175,35 @@ 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)
+                     const struct ldb_val *val,
+                     struct ldb_message_element **return_el)
 {
        struct ldb_message_element *el;
        struct ldb_val *vals;
+       int ret;
 
        el = ldb_msg_find_element(msg, attr_name);
        if (!el) {
-               ldb_msg_add_empty(msg, attr_name, 0);
-               el = ldb_msg_find_element(msg, attr_name);
-       }
-       if (!el) {
-               return -1;
+               ret = ldb_msg_add_empty(msg, attr_name, 0, &el);
+               if (ret != LDB_SUCCESS) {
+                       return ret;
+               }
        }
 
        vals = talloc_realloc(msg, el->values, struct ldb_val, el->num_values+1);
        if (!vals) {
                errno = ENOMEM;
-               return -1;
+               return LDB_ERR_OPERATIONS_ERROR;
        }
        el->values = vals;
        el->values[el->num_values] = *val;
        el->num_values++;
 
-       return 0;
+       if (return_el) {
+               *return_el = el;
+       }
+
+       return LDB_SUCCESS;
 }
 
 
@@ -206,10 +215,10 @@ int ldb_msg_add_steal_value(struct ldb_message *msg,
                            struct ldb_val *val)
 {
        int ret;
-       ret = ldb_msg_add_value(msg, attr_name, val);
+       struct ldb_message_element *el;
+
+       ret = ldb_msg_add_value(msg, attr_name, val, &el);
        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;
@@ -227,7 +236,12 @@ int ldb_msg_add_string(struct ldb_message *msg,
        val.data = discard_const_p(uint8_t, str);
        val.length = strlen(str);
 
-       return ldb_msg_add_value(msg, attr_name, &val);
+       if (val.length == 0) {
+               /* allow empty strings as non-existant attributes */
+               return LDB_SUCCESS;
+       }
+
+       return ldb_msg_add_value(msg, attr_name, &val, NULL);
 }
 
 /*
@@ -244,6 +258,18 @@ int ldb_msg_add_steal_string(struct ldb_message *msg,
        return ldb_msg_add_steal_value(msg, attr_name, &val);
 }
 
+/*
+  add a DN element to a message
+  WARNING: this uses the linearized string from the dn, and does not
+  copy the string.
+*/
+int ldb_msg_add_linearized_dn(struct ldb_message *msg, const char *attr_name,
+                             struct ldb_dn *dn)
+{
+       return ldb_msg_add_steal_string(msg, attr_name,
+                                       ldb_dn_alloc_linearized(msg, dn));
+}
+
 /*
   add a printf formatted element to a message
 */
@@ -258,7 +284,7 @@ int ldb_msg_add_fmt(struct ldb_message *msg,
        str = talloc_vasprintf(msg, fmt, ap);
        va_end(ap);
 
-       if (str == NULL) return -1;
+       if (str == NULL) return LDB_ERR_OPERATIONS_ERROR;
 
        val.data   = (uint8_t *)str;
        val.length = strlen(str);
@@ -302,7 +328,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) {
@@ -326,10 +353,19 @@ unsigned int ldb_msg_find_attr_as_uint(const struct ldb_message *msg,
                                       const char *attr_name,
                                       unsigned int default_value)
 {
+       unsigned int ret;
        const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
        if (!v || !v->data) {
                return default_value;
        }
+
+       /* in LDAP there're only int32_t values */
+       errno = 0;
+       ret = strtol((const char *)v->data, NULL, 0);
+       if (errno == 0) {
+               return ret;
+       }
+
        return strtoul((const char *)v->data, NULL, 0);
 }
 
@@ -348,10 +384,19 @@ uint64_t ldb_msg_find_attr_as_uint64(const struct ldb_message *msg,
                                     const char *attr_name,
                                     uint64_t default_value)
 {
+       uint64_t ret;
        const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
        if (!v || !v->data) {
                return default_value;
        }
+
+       /* in LDAP there're only int64_t values */
+       errno = 0;
+       ret = strtoll((const char *)v->data, NULL, 0);
+       if (errno == 0) {
+               return ret;
+       }
+
        return strtoull((const char *)v->data, NULL, 0);
 }
 
@@ -374,10 +419,10 @@ int ldb_msg_find_attr_as_bool(const struct ldb_message *msg,
        if (!v || !v->data) {
                return default_value;
        }
-       if (strcasecmp(v->data, "FALSE") == 0) {
+       if (v->length == 5 && strncasecmp((const char *)v->data, "FALSE", 5) == 0) {
                return 0;
        }
-       if (strcasecmp(v->data, "TRUE") == 0) {
+       if (v->length == 4 && strncasecmp((const char *)v->data, "TRUE", 4) == 0) {
                return 1;
        }
        return default_value;
@@ -394,17 +439,24 @@ const char *ldb_msg_find_attr_as_string(const struct ldb_message *msg,
        return (const char *)v->data;
 }
 
-struct ldb_dn *ldb_msg_find_attr_as_dn(void *mem_ctx,
+struct ldb_dn *ldb_msg_find_attr_as_dn(struct ldb_context *ldb,
+                                      void *mem_ctx,
                                       const struct ldb_message *msg,
                                       const char *attr_name)
 {
+       struct ldb_dn *res_dn;
        const struct ldb_val *v;
 
        v = ldb_msg_find_ldb_val(msg, attr_name);
        if (!v || !v->data) {
                return NULL;
        }
-       return ldb_dn_explode(mem_ctx, (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;
+       }
+       return res_dn;
 }
 
 /*
@@ -412,8 +464,8 @@ struct ldb_dn *ldb_msg_find_attr_as_dn(void *mem_ctx,
 */
 void ldb_msg_sort_elements(struct ldb_message *msg)
 {
-       qsort(msg->elements, msg->num_elements, sizeof(struct ldb_message_element), 
-             (comparison_fn_t)ldb_msg_element_compare_name);
+       TYPESAFE_QSORT(msg->elements, msg->num_elements,
+                      ldb_msg_element_compare_name);
 }
 
 /*
@@ -430,7 +482,6 @@ struct ldb_message *ldb_msg_copy_shallow(TALLOC_CTX *mem_ctx,
        if (msg2 == NULL) return NULL;
 
        *msg2 = *msg;
-       msg2->private_data = NULL;
 
        msg2->elements = talloc_array(msg2, struct ldb_message_element, 
                                      msg2->num_elements);
@@ -505,7 +556,7 @@ struct ldb_message *ldb_msg_canonicalize(struct ldb_context *ldb,
                if (ldb_msg_element_compare_name(el1, el2) == 0) {
                        el1->values = talloc_realloc(msg2->elements, el1->values, struct ldb_val, 
                                                       el1->num_values + el2->num_values);
-                       if (el1->values == NULL) {
+                       if (el1->num_values + el2->num_values > 0 && el1->values == NULL) {
                                return NULL;
                        }
                        memcpy(el1->values + el1->num_values,
@@ -539,6 +590,9 @@ struct ldb_message *ldb_msg_diff(struct ldb_context *ldb,
        unsigned int i;
 
        mod = ldb_msg_new(ldb);
+       if (mod == NULL) {
+               return NULL;
+       }
 
        mod->dn = msg1->dn;
        mod->num_elements = 0;
@@ -546,6 +600,7 @@ struct ldb_message *ldb_msg_diff(struct ldb_context *ldb,
 
        msg2 = ldb_msg_canonicalize(ldb, msg2);
        if (msg2 == NULL) {
+               talloc_free(mod);
                return NULL;
        }
        
@@ -560,7 +615,8 @@ struct ldb_message *ldb_msg_diff(struct ldb_context *ldb,
 
                if (ldb_msg_add(mod, 
                                &msg2->elements[i],
-                               el?LDB_FLAG_MOD_REPLACE:LDB_FLAG_MOD_ADD) != 0) {
+                               el?LDB_FLAG_MOD_REPLACE:LDB_FLAG_MOD_ADD) != LDB_SUCCESS) {
+                       talloc_free(mod);
                        return NULL;
                }
        }
@@ -568,10 +624,11 @@ struct ldb_message *ldb_msg_diff(struct ldb_context *ldb,
        /* look in msg1 to find elements that need to be deleted */
        for (i=0;i<msg1->num_elements;i++) {
                el = ldb_msg_find_element(msg2, msg1->elements[i].name);
-               if (!el) {
+               if (el == NULL) {
                        if (ldb_msg_add_empty(mod, 
                                              msg1->elements[i].name,
-                                             LDB_FLAG_MOD_DELETE) != 0) {
+                                             LDB_FLAG_MOD_DELETE, NULL) != LDB_SUCCESS) {
+                               talloc_free(mod);
                                return NULL;
                        }
                }
@@ -591,11 +648,6 @@ int ldb_msg_sanity_check(struct ldb_context *ldb,
                ldb_set_errstring(ldb, "ldb message lacks a DN!");
                return LDB_ERR_INVALID_DN_SYNTAX;
        }
-       if (msg->dn->comp_num == 0) {
-               /* root dse has empty dn */
-               ldb_set_errstring(ldb, "DN on new ldb message is '' (not permitted)!");
-               return LDB_ERR_ENTRY_ALREADY_EXISTS;
-       }
 
        /* basic syntax checks */
        for (i = 0; i < msg->num_elements; i++) {
@@ -606,7 +658,7 @@ int ldb_msg_sanity_check(struct ldb_context *ldb,
                                /* TODO: return also an error string */
                                ldb_asprintf_errstring(ldb, "Element %s has empty attribute in ldb message (%s)!",
                                                            msg->elements[i].name, 
-                                                           ldb_dn_linearize(mem_ctx, msg->dn));
+                                                           ldb_dn_get_linearized(msg->dn));
                                talloc_free(mem_ctx);
                                return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
                        }
@@ -627,12 +679,12 @@ const char **ldb_attr_list_copy(TALLOC_CTX *mem_ctx, const char * const *attrs)
 {
        const char **ret;
        int i;
-       for (i=0;attrs[i];i++) /* noop */ ;
+       for (i=0;attrs && attrs[i];i++) /* noop */ ;
        ret = talloc_array(mem_ctx, const char *, i+1);
        if (ret == NULL) {
                return NULL;
        }
-       for (i=0;attrs[i];i++) {
+       for (i=0;attrs && attrs[i];i++) {
                ret[i] = attrs[i];
        }
        ret[i] = attrs[i];
@@ -642,18 +694,26 @@ const char **ldb_attr_list_copy(TALLOC_CTX *mem_ctx, const char * const *attrs)
 
 /*
   copy an attribute list. This only copies the array, not the elements
-  (ie. the elements are left as the same pointers)
+  (ie. the elements are left as the same pointers).  The new attribute is added to the list.
 */
 const char **ldb_attr_list_copy_add(TALLOC_CTX *mem_ctx, const char * const *attrs, const char *new_attr)
 {
        const char **ret;
        int i;
-       for (i=0;attrs[i];i++) /* noop */ ;
+       bool found = false;
+       for (i=0;attrs && 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;
        }
-       for (i=0;attrs[i];i++) {
+       for (i=0;attrs && attrs[i];i++) {
                ret[i] = attrs[i];
        }
        ret[i] = new_attr;
@@ -668,7 +728,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;
                }
@@ -684,13 +744,13 @@ int ldb_msg_rename_attr(struct ldb_message *msg, const char *attr, const char *r
 {
        struct ldb_message_element *el = ldb_msg_find_element(msg, attr);
        if (el == NULL) {
-               return 0;
+               return LDB_SUCCESS;
        }
        el->name = talloc_strdup(msg->elements, replace);
        if (el->name == NULL) {
-               return -1;
+               return LDB_ERR_OPERATIONS_ERROR;
        }
-       return 0;
+       return LDB_SUCCESS;
 }
 
 
@@ -701,14 +761,30 @@ int ldb_msg_copy_attr(struct ldb_message *msg, const char *attr, const char *rep
 {
        struct ldb_message_element *el = ldb_msg_find_element(msg, attr);
        if (el == NULL) {
-               return 0;
+               return LDB_SUCCESS;
        }
        if (ldb_msg_add(msg, el, 0) != 0) {
-               return -1;
+               return LDB_ERR_OPERATIONS_ERROR;
        }
        return ldb_msg_rename_attr(msg, attr, replace);
 }
 
+/*
+  remove the specified element in a search result
+*/
+void ldb_msg_remove_element(struct ldb_message *msg, struct ldb_message_element *el)
+{
+       int n = (el - msg->elements);
+       if (n >= msg->num_elements) {
+               /* should we abort() here? */
+               return;
+       }
+       if (n != msg->num_elements-1) {
+               memmove(el, el+1, ((msg->num_elements-1) - n)*sizeof(*el));
+       }
+       msg->num_elements--;
+}
+
 
 /*
   remove the specified attribute in a search result
@@ -717,36 +793,43 @@ 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--;
+               ldb_msg_remove_element(msg, el);
        }
 }
 
 /*
-  return a LDAP formatted time string
+  return a LDAP formatted GeneralizedTime string
 */
 char *ldb_timestring(TALLOC_CTX *mem_ctx, time_t t)
 {
        struct tm *tm = gmtime(&t);
+       char *ts;
+       int r;
 
        if (!tm) {
                return NULL;
        }
 
+       /* we now excatly how long this string will be */
+       ts = talloc_array(mem_ctx, char, 18);
+
        /* formatted like: 20040408072012.0Z */
-       return talloc_asprintf(mem_ctx, 
-                              "%04u%02u%02u%02u%02u%02u.0Z",
-                              tm->tm_year+1900, tm->tm_mon+1,
-                              tm->tm_mday, tm->tm_hour, tm->tm_min,
-                              tm->tm_sec);
-}
+       r = snprintf(ts, 18,
+                       "%04u%02u%02u%02u%02u%02u.0Z",
+                       tm->tm_year+1900, tm->tm_mon+1,
+                       tm->tm_mday, tm->tm_hour, tm->tm_min,
+                       tm->tm_sec);
+
+       if (r != 17) {
+               talloc_free(ts);
+               return NULL;
+       }
 
+       return ts;
+}
 
 /*
-  convert a LDAP time string to a time_t. Return 0 if unable to convert
+  convert a LDAP GeneralizedTime string to a time_t. Return 0 if unable to convert
 */
 time_t ldb_string_to_time(const char *s)
 {
@@ -755,7 +838,7 @@ time_t ldb_string_to_time(const char *s)
        if (s == NULL) return 0;
        
        memset(&tm, 0, sizeof(tm));
-       if (sscanf(s, "%04u%02u%02u%02u%02u%02u", 
+       if (sscanf(s, "%04u%02u%02u%02u%02u%02u.0Z",
                   &tm.tm_year, &tm.tm_mon, &tm.tm_mday, 
                   &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {
                return 0;
@@ -766,6 +849,87 @@ time_t ldb_string_to_time(const char *s)
        return timegm(&tm);
 }
 
+/*
+  convert a LDAP GeneralizedTime string in ldb_val format to a
+  time_t.
+*/
+int ldb_val_to_time(const struct ldb_val *v, time_t *t)
+{
+       struct tm tm;
+
+       if (v == NULL || !v->data || v->length < 17) {
+               return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
+       }
+
+       memset(&tm, 0, sizeof(tm));
+
+       if (sscanf((char *)v->data, "%04u%02u%02u%02u%02u%02u.0Z",
+                  &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
+                  &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {
+               return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
+       }
+       tm.tm_year -= 1900;
+       tm.tm_mon -= 1;
+
+       *t = timegm(&tm);
+
+       return LDB_SUCCESS;
+}
+
+/*
+  return a LDAP formatted UTCTime string
+*/
+char *ldb_timestring_utc(TALLOC_CTX *mem_ctx, time_t t)
+{
+       struct tm *tm = gmtime(&t);
+       char *ts;
+       int r;
+
+       if (!tm) {
+               return NULL;
+       }
+
+       /* we now excatly how long this string will be */
+       ts = talloc_array(mem_ctx, char, 14);
+
+       /* formatted like: 20040408072012.0Z => 040408072012Z */
+       r = snprintf(ts, 14,
+                       "%02u%02u%02u%02u%02u%02uZ",
+                       (tm->tm_year+1900)%100, tm->tm_mon+1,
+                       tm->tm_mday, tm->tm_hour, tm->tm_min,
+                       tm->tm_sec);
+
+       if (r != 13) {
+               talloc_free(ts);
+               return NULL;
+       }
+
+       return ts;
+}
+
+/*
+  convert a LDAP UTCTime string to a time_t. Return 0 if unable to convert
+*/
+time_t ldb_string_utc_to_time(const char *s)
+{
+       struct tm tm;
+       
+       if (s == NULL) return 0;
+       
+       memset(&tm, 0, sizeof(tm));
+       if (sscanf(s, "%02u%02u%02u%02u%02u%02uZ",
+                  &tm.tm_year, &tm.tm_mon, &tm.tm_mday, 
+                  &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {
+               return 0;
+       }
+       if (tm.tm_year < 50) {
+               tm.tm_year += 100;
+       }
+       tm.tm_mon -= 1;
+       
+       return timegm(&tm);
+}
+
 
 /*
   dump a set of results to a file. Useful from within gdb
@@ -783,20 +947,27 @@ void ldb_dump_results(struct ldb_context *ldb, struct ldb_result *result, FILE *
        }
 }
 
-int ldb_msg_check_string_attribute(const struct ldb_message *msg, const char *name, const char *value)
+/*
+  checks for a string attribute. Returns "1" on match and otherwise "0".
+*/
+int ldb_msg_check_string_attribute(const struct ldb_message *msg,
+                                  const char *name, const char *value)
 {
        struct ldb_message_element *el;
        struct ldb_val val;
        
        el = ldb_msg_find_element(msg, name);
-       if (el == NULL)
+       if (el == NULL) {
                return 0;
+       }
 
-       val.data = discard_const(value);
+       val.data = discard_const_p(uint8_t, value);
        val.length = strlen(value);
 
-       if (ldb_msg_find_val(el, &val))
+       if (ldb_msg_find_val(el, &val)) {
                return 1;
+       }
 
        return 0;
 }
+