]> git.sur5r.net Git - openldap/blob - servers/slapd/back-tcl/tcl_search.c
Import Ben Collins <bcollins@debian.org> Back-TCL for SLAPD.
[openldap] / servers / slapd / back-tcl / tcl_search.c
1 /*
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 tcl_back_search (
20         Backend * be,
21         Connection * conn,
22         Operation * op,
23         char *base,
24         int scope,
25         int deref,
26         int sizelimit,
27         int timelimit,
28         Filter * filter,
29         char *filterstr,
30         char **attrs,
31         int attrsonly
32 )
33 {
34         char *attrs_tcl = NULL, *suf_tcl, *results, *command;
35         int i, err = 0, code;
36         struct tclinfo *ti = (struct tclinfo *) be->be_private;
37         Entry *e;
38
39         if (ti->ti_search == NULL) {
40                 send_ldap_result (conn, op, LDAP_UNWILLING_TO_PERFORM, NULL,
41                         "search not implemented");
42                 return (-1);
43         }
44
45         for ( i = 0; attrs != NULL && attrs[i] != NULL; i++ )
46                 ;
47         if (i > 0)
48                 attrs_tcl = Tcl_Merge(i, attrs);
49
50         for ( i = 0; be->be_suffix[i] != NULL; i++ )
51                 ;
52         suf_tcl = Tcl_Merge(i, be->be_suffix);
53
54         command = (char *) ch_malloc (strlen(ti->ti_search) + strlen(suf_tcl)
55                 + strlen(base) + 40 + strlen(filterstr) + (attrs_tcl == NULL ? 5
56                 : strlen(attrs_tcl)) + 72);
57         sprintf(command, "%s SEARCH {%ld} {%s} {%s} {%d} {%d} {%d} {%d} {%s} {%d} {%s}",
58                 ti->ti_search, op->o_msgid, suf_tcl, base, scope, deref,
59                 sizelimit, timelimit, filterstr, attrsonly ? 1 : 0, attrs_tcl == 
60                 NULL ? "{all}" : attrs_tcl);
61         Tcl_Free( attrs_tcl );
62         Tcl_Free( suf_tcl );
63
64         ldap_pvt_thread_mutex_lock( &tcl_interpreter_mutex );
65         code = Tcl_GlobalEval(ti->ti_ii->interp, command);
66         results = (char *) 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                 err = LDAP_OPERATIONS_ERROR;
72                 Debug(LDAP_DEBUG_ANY, "tcl_search_error: %s\n", results, 0, 0);
73         } else {
74                 interp_send_results ( be, conn, op, results, NULL, 0 );
75         }
76
77         if (err != LDAP_SUCCESS)
78                 send_ldap_result (conn, op, err, NULL, "internal backend error");
79         return (0);
80
81 }