r23798: updated old Temple Place FSF addresses to new URL
[jra/samba/.git] / source3 / lib / ldb / ldb_ldap / ldb_ldap.c
index 9de67e5ad7f306f178abc74cac3c44679ac1a3e9..51445d651feb99ff75de7ced2cf2f91ba531a723 100644 (file)
@@ -11,7 +11,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
@@ -19,8 +19,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/>.
 */
 
 /*
@@ -154,7 +153,8 @@ static LDAPMod **lldb_msg_to_mods(void *mem_ctx, const struct ldb_message *msg,
                        if (!mods[num_mods]->mod_vals.modv_bvals[j]) {
                                goto failed;
                        }
-                       mods[num_mods]->mod_vals.modv_bvals[j]->bv_val = el->values[j].data;
+                       mods[num_mods]->mod_vals.modv_bvals[j]->bv_val =
+                               (char *)el->values[j].data;
                        mods[num_mods]->mod_vals.modv_bvals[j]->bv_len = el->values[j].length;
                }
                mods[num_mods]->mod_vals.modv_bvals[j] = NULL;
@@ -210,10 +210,16 @@ static int lldb_add_msg_attr(struct ldb_context *ldb,
        }
 
        for (i=0;i<count;i++) {
-               el->values[i].data = talloc_memdup(el->values, bval[i]->bv_val, bval[i]->bv_len);
+               /* we have to ensure this is null terminated so that
+                  ldb_msg_find_attr_as_string() can work */
+               el->values[i].data =
+                       (uint8_t *)talloc_size(el->values, bval[i]->bv_len+1);
                if (!el->values[i].data) {
+                       errno = ENOMEM;
                        return -1;
                }
+               memcpy(el->values[i].data, bval[i]->bv_val, bval[i]->bv_len);
+               el->values[i].data[bval[i]->bv_len] = 0;
                el->values[i].length = bval[i]->bv_len;
                el->num_values++;
        }
@@ -265,7 +271,9 @@ static int lldb_search(struct ldb_module *module, struct ldb_request *req)
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
-       expression = ldb_filter_from_tree(lldb_ac, req->op.search.tree);
+       expression = ldb_filter_from_tree(
+               lldb_ac,
+               discard_const_p(struct ldb_parse_tree, req->op.search.tree));
        if (expression == NULL) {
                return LDB_ERR_OPERATIONS_ERROR;
        }
@@ -458,8 +466,8 @@ static int lldb_rename(struct ldb_module *module, struct ldb_request *req)
        }
 
        newrdn = talloc_asprintf(lldb_ac, "%s=%s",
-                                req->op.rename.newdn->components[0].name,
-                                ldb_dn_escape_value(lldb, req->op.rename.newdn->components[0].value));
+                                ldb_dn_get_rdn_name(req->op.rename.newdn),
+                                ldb_dn_escape_value(lldb, *(ldb_dn_get_rdn_val(req->op.rename.newdn))));
        if (!newrdn) {
                return LDB_ERR_OPERATIONS_ERROR;
        }
@@ -492,9 +500,11 @@ static int lldb_parse_result(struct ldb_handle *handle, LDAPMessage *result)
        char **referralsp = NULL;
        LDAPControl **serverctrlsp = NULL;
        int ret = LDB_SUCCESS;
-
+       
        type = ldap_msgtype(result);
 
+       handle->status = 0;
+
        switch (type) {
 
        case LDAP_RES_SEARCH_ENTRY:
@@ -626,15 +636,19 @@ static int lldb_parse_result(struct ldb_handle *handle, LDAPMessage *result)
        }
 
        if (matcheddnp) ldap_memfree(matcheddnp);
-       if (errmsgp) {
+       if (errmsgp && *errmsgp) {
                ldb_set_errstring(ac->module->ldb, errmsgp);
+       } else if (handle->status) {
+               ldb_set_errstring(ac->module->ldb, ldap_err2string(handle->status));
+       }
+       if (errmsgp) {
                ldap_memfree(errmsgp);
        }
        if (referralsp) ldap_value_free(referralsp);
        if (serverctrlsp) ldap_controls_free(serverctrlsp);
 
        ldap_msgfree(result);
-       return ret;
+       return lldb_ldap_to_ldb(handle->status);
 
 error:
        handle->state = LDB_ASYNC_DONE;
@@ -806,11 +820,12 @@ static int lldb_connect(struct ldb_context *ldb,
        }
 
        *module = talloc(ldb, struct ldb_module);
-       if (!module) {
+       if (*module == NULL) {
                ldb_oom(ldb);
                talloc_free(lldb);
                return -1;
        }
+       talloc_set_name_const(*module, "ldb_ldap backend");
        (*module)->ldb = ldb;
        (*module)->prev = (*module)->next = NULL;
        (*module)->private_data = lldb;