]> git.sur5r.net Git - openldap/blob - servers/slapd/back-tcl/tcl_search.c
Add OpenLDAP RCSid to *.[ch] in clients, libraries, and servers.
[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         char *base,
25         int scope,
26         int deref,
27         int sizelimit,
28         int timelimit,
29         Filter * filter,
30         char *filterstr,
31         char **attrs,
32         int attrsonly
33 )
34 {
35         char *attrs_tcl = NULL, *suf_tcl, *results, *command;
36         int i, err = 0, code;
37         struct tclinfo *ti = (struct tclinfo *) be->be_private;
38         Entry *e;
39
40         if (ti->ti_search == NULL) {
41                 send_ldap_result (conn, op, LDAP_UNWILLING_TO_PERFORM, NULL,
42                         "search not implemented", NULL, NULL );
43                 return (-1);
44         }
45
46         for (i = 0; attrs != NULL && attrs[i] != NULL; i++);
47         if (i > 0)
48                 attrs_tcl = Tcl_Merge (i, attrs);
49
50         for (i = 0; be->be_suffix[i] != NULL; i++);
51         suf_tcl = Tcl_Merge (i, be->be_suffix);
52
53         command = (char *) ch_malloc (strlen (ti->ti_search) + strlen (suf_tcl)
54                 + strlen (base) + 40 + strlen (filterstr) + (attrs_tcl ==
55                         NULL ? 5
56                         : strlen (attrs_tcl)) + 72);
57         sprintf (command,
58                 "%s SEARCH {%ld} {%s} {%s} {%d} {%d} {%d} {%d} {%s} {%d} {%s}",
59                 ti->ti_search, op->o_msgid, suf_tcl, base, scope, deref,
60                 sizelimit, timelimit, filterstr, attrsonly ? 1 : 0,
61                 attrs_tcl ==
62                 NULL ? "{all}" : attrs_tcl);
63         Tcl_Free (attrs_tcl);
64         Tcl_Free (suf_tcl);
65
66         ldap_pvt_thread_mutex_lock (&tcl_interpreter_mutex);
67         code = Tcl_GlobalEval (ti->ti_ii->interp, command);
68         results = (char *) ch_strdup (ti->ti_ii->interp->result);
69         ldap_pvt_thread_mutex_unlock (&tcl_interpreter_mutex);
70         free (command);
71
72         if (code != TCL_OK) {
73                 err = LDAP_OPERATIONS_ERROR;
74                 Debug (LDAP_DEBUG_SHELL, "tcl_search_error: %s\n", results,
75                         0, 0);
76         } else {
77                 interp_send_results (be, conn, op, results, NULL, 0);
78         }
79
80         if (err != LDAP_SUCCESS)
81                 send_ldap_result (conn, op, err, NULL,
82                         "internal backend error", NULL, NULL );
83
84         free (results);
85         return (err);
86 }