s4-ldb: use TYPESAFE_QSORT() in the rest of the ldb code
[ira/wip.git] / source4 / lib / ldb / common / ldb_msg.c
index ad53a3d29d27bc0fc1fe376c28d6272cb0c1bf0b..f4adb560b1dfe8521b478bee670f339013946745 100644 (file)
@@ -63,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) {
@@ -258,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
 */
@@ -341,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);
 }
 
@@ -363,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);
 }
 
@@ -434,8 +464,8 @@ struct ldb_dn *ldb_msg_find_attr_as_dn(struct ldb_context *ldb,
 */
 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);
 }
 
 /*
@@ -526,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,
@@ -560,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;
@@ -567,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;
        }
        
@@ -581,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;
                }
        }
@@ -589,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, NULL) != 0) {
+                                             LDB_FLAG_MOD_DELETE, NULL) != LDB_SUCCESS) {
+                               talloc_free(mod);
                                return NULL;
                        }
                }
@@ -643,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];
@@ -665,7 +701,7 @@ const char **ldb_attr_list_copy_add(TALLOC_CTX *mem_ctx, const char * const *att
        const char **ret;
        int i;
        bool found = false;
-       for (i=0;attrs[i];i++) {
+       for (i=0;attrs && attrs[i];i++) {
                if (ldb_attr_cmp(attrs[i], new_attr) == 0) {
                        found = true;
                }
@@ -677,7 +713,7 @@ const char **ldb_attr_list_copy_add(TALLOC_CTX *mem_ctx, const char * const *att
        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;
@@ -739,6 +775,10 @@ int ldb_msg_copy_attr(struct ldb_message *msg, const char *attr, const char *rep
 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));
        }
@@ -798,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;
@@ -809,6 +849,33 @@ 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
 */
@@ -850,7 +917,7 @@ time_t ldb_string_utc_to_time(const char *s)
        if (s == NULL) return 0;
        
        memset(&tm, 0, sizeof(tm));
-       if (sscanf(s, "%02u%02u%02u%02u%02u%02u", 
+       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;
@@ -880,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_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;
 }
+