]> git.sur5r.net Git - openldap/blob - servers/slapd/back-tcl/tcl_search.c
Add dummy reference to lutil_uuidstr() for dynamically loaded back-bdb
[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         Backend * be,
22         Connection * conn,
23         Operation * op,
24         struct berval *base,
25         struct berval *nbase,
26         int scope,
27         int deref,
28         int sizelimit,
29         int timelimit,
30         Filter * filter,
31         struct berval *filterstr,
32         AttributeName *attrs,
33         int attrsonly
34 )
35 {
36         char *attrs_tcl = NULL, *results, *command;
37         struct berval suf_tcl;
38         int i, err = 0, code;
39         struct tclinfo *ti = (struct tclinfo *) be->be_private;
40         AttributeName *an;
41
42         if (ti->ti_search.bv_len == 0) {
43                 send_ldap_result (conn, op, LDAP_UNWILLING_TO_PERFORM, NULL,
44                         "search not implemented", NULL, NULL );
45                 return (-1);
46         }
47
48         for (i = 0, an = attrs; an && an->an_name.bv_val; an++, i++);
49         if (i > 0) {
50                 char **sattrs = ch_malloc( (i+1) * sizeof(char *));
51                 for (i = 0, an = attrs; an->an_name.bv_val; an++, i++)
52                         sattrs[i] = an->an_name.bv_val;
53                 sattrs[i] = NULL;
54                 attrs_tcl = Tcl_Merge (i, sattrs);
55                 ch_free(sattrs);
56         }
57
58         if (tcl_merge_bvlist (be->be_suffix, &suf_tcl) == NULL) {
59                 Tcl_Free (attrs_tcl);
60                 send_ldap_result (conn, op, LDAP_OPERATIONS_ERROR, NULL,
61                         NULL, NULL, NULL );
62                 return (-1);
63         }
64
65         command = (char *) ch_malloc (ti->ti_search.bv_len + suf_tcl.bv_len
66                 + base->bv_len + 60 + filterstr->bv_len + 
67                 (attrs_tcl == NULL ? 5 : strlen (attrs_tcl)) + 72);
68         sprintf (command,
69                 "%s SEARCH {%ld/%ld} {%s} {%s} {%d} {%d} {%d} {%d} {%s} {%d} {%s}",
70                 ti->ti_search.bv_val, op->o_connid, (long) op->o_msgid,
71                 suf_tcl.bv_val, base->bv_val, scope, deref,
72                 sizelimit, timelimit, filterstr->bv_val, attrsonly ? 1 : 0,
73                 attrs_tcl == NULL ? "{all}" : attrs_tcl);
74         Tcl_Free (attrs_tcl);
75         Tcl_Free (suf_tcl.bv_val);
76
77         ldap_pvt_thread_mutex_lock (&tcl_interpreter_mutex);
78         code = Tcl_GlobalEval (ti->ti_ii->interp, command);
79         results = (char *) ch_strdup (ti->ti_ii->interp->result);
80         ldap_pvt_thread_mutex_unlock (&tcl_interpreter_mutex);
81         free (command);
82
83         if (code != TCL_OK) {
84                 err = LDAP_OPERATIONS_ERROR;
85                 Debug (LDAP_DEBUG_SHELL, "tcl_search_error: %s\n", results,
86                         0, 0);
87         } else {
88                 interp_send_results (be, conn, op, results, attrs, 0);
89         }
90
91         if (err != LDAP_SUCCESS)
92                 send_ldap_result (conn, op, err, NULL,
93                         "internal backend error", NULL, NULL );
94
95         free (results);
96         return (err);
97 }