2 /* search.c - tcl search routines
4 * Copyright 1999, Ben Collins <bcollins@debian.org>, All rights reserved.
6 * Redistribution and use in source and binary forms are permitted only
7 * as authorized by the OpenLDAP Public License. A copy of this
8 * license is available at http://www.OpenLDAP.org/license.html or
9 * in file LICENSE in the top-level directory of the distribution.
24 char *attrs_tcl = NULL, *results, *command;
25 struct berval suf_tcl;
27 struct tclinfo *ti = (struct tclinfo *) op->o_bd->be_private;
30 if (ti->ti_search.bv_len == 0) {
31 send_ldap_error (op, rs, LDAP_UNWILLING_TO_PERFORM,
32 "search not implemented" );
36 for (i = 0, an = op->oq_search.rs_attrs; an && an->an_name.bv_val; an++, i++);
38 char **sattrs = ch_malloc( (i+1) * sizeof(char *));
39 for (i = 0, an = op->oq_search.rs_attrs; an->an_name.bv_val; an++, i++)
40 sattrs[i] = an->an_name.bv_val;
42 attrs_tcl = Tcl_Merge (i, sattrs);
46 if (tcl_merge_bvlist (op->o_bd->be_suffix, &suf_tcl) == NULL) {
48 send_ldap_error (op, rs, LDAP_OTHER, NULL);
52 command = (char *) ch_malloc (ti->ti_search.bv_len + suf_tcl.bv_len
53 + op->o_req_dn.bv_len + 60 + op->oq_search.rs_filterstr.bv_len +
54 (attrs_tcl == NULL ? 5 : strlen (attrs_tcl)) + 72);
56 "%s SEARCH {%ld/%ld} {%s} {%s} {%d} {%d} {%d} {%d} {%s} {%d} {%s}",
57 ti->ti_search.bv_val, op->o_connid, (long) op->o_msgid,
58 suf_tcl.bv_val, op->o_req_dn.bv_val, op->oq_search.rs_scope, op->oq_search.rs_deref,
59 op->oq_search.rs_slimit, op->oq_search.rs_tlimit, op->oq_search.rs_filterstr.bv_val,
60 op->oq_search.rs_attrsonly ? 1 : 0, attrs_tcl == NULL ? "{all}" : attrs_tcl);
62 Tcl_Free (suf_tcl.bv_val);
64 ldap_pvt_thread_mutex_lock (&tcl_interpreter_mutex);
65 code = Tcl_GlobalEval (ti->ti_ii->interp, command);
66 results = (char *) ch_strdup (ti->ti_ii->interp->result);
67 ldap_pvt_thread_mutex_unlock (&tcl_interpreter_mutex);
71 rs->sr_err = LDAP_OTHER;
72 Debug (LDAP_DEBUG_SHELL, "tcl_search_error: %s\n", results,
75 interp_send_results (op, rs, results );
78 if (rs->sr_err != LDAP_SUCCESS) {
79 rs->sr_text = "internal backend error";
80 send_ldap_result (op, rs );