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