]> git.sur5r.net Git - openldap/blob - servers/slapd/back-tcl/tcl_search.c
Fix cursor initialization, scope IDs
[openldap] / servers / slapd / back-tcl / tcl_search.c
1 /* $OpenLDAP$ */
2 /* search.c - tcl search routines
3  *
4  * Copyright 1999, Ben Collins <bcollins@debian.org>, All rights reserved.
5  *
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.
10  */
11
12 #include "portable.h"
13
14 #include <stdio.h>
15
16 #include "slap.h"
17 #include "tcl_back.h"
18
19 int
20 tcl_back_search (
21         Operation * op,
22         SlapReply * rs )
23 {
24         char *attrs_tcl = NULL, *results, *command;
25         struct berval suf_tcl;
26         int i, code;
27         struct tclinfo *ti = (struct tclinfo *) op->o_bd->be_private;
28         AttributeName *an;
29
30         if (ti->ti_search.bv_len == 0) {
31                 send_ldap_error (op, rs, LDAP_UNWILLING_TO_PERFORM,
32                         "search not implemented" );
33                 return (-1);
34         }
35
36         for (i = 0, an = op->oq_search.rs_attrs; an && an->an_name.bv_val; an++, i++);
37         if (i > 0) {
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;
41                 sattrs[i] = NULL;
42                 attrs_tcl = Tcl_Merge (i, sattrs);
43                 ch_free(sattrs);
44         }
45
46         if (tcl_merge_bvlist (op->o_bd->be_suffix, &suf_tcl) == NULL) {
47                 Tcl_Free (attrs_tcl);
48                 send_ldap_error (op, rs, LDAP_OTHER, NULL);
49                 return (-1);
50         }
51
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);
55         sprintf (command,
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);
61         Tcl_Free (attrs_tcl);
62         Tcl_Free (suf_tcl.bv_val);
63
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);
68         free (command);
69
70         if (code != TCL_OK) {
71                 rs->sr_err = LDAP_OTHER;
72                 Debug (LDAP_DEBUG_SHELL, "tcl_search_error: %s\n", results,
73                         0, 0);
74         } else {
75                 interp_send_results (op, rs, results );
76         }
77
78         if (rs->sr_err != LDAP_SUCCESS) {
79                 rs->sr_text = "internal backend error";
80                 send_ldap_result (op, rs );
81         }
82
83         free (results);
84         return (rs->sr_err);
85 }