]> git.sur5r.net Git - openldap/blob - servers/slapd/back-tcl/tcl_search.c
Style changes, added cvs keywords
[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  * $Id$
12  *
13  * $Log$
14  */
15
16 #include "portable.h"
17
18 #include <stdio.h>
19
20 #include "slap.h"
21 #include "tcl_back.h"
22
23 int tcl_back_search (
24         Backend * be,
25         Connection * conn,
26         Operation * op,
27         char *base,
28         int scope,
29         int deref,
30         int sizelimit,
31         int timelimit,
32         Filter * filter,
33         char *filterstr,
34         char **attrs,
35         int attrsonly
36 )
37 {
38         char *attrs_tcl = NULL, *suf_tcl, *results, *command;
39         int i, err = 0, code;
40         struct tclinfo *ti = (struct tclinfo *) be->be_private;
41         Entry *e;
42
43         if (ti->ti_search == NULL) {
44                 send_ldap_result (conn, op, LDAP_UNWILLING_TO_PERFORM, NULL,
45                         "search not implemented");
46                 return (-1);
47         }
48
49         for ( i = 0; attrs != NULL && attrs[i] != NULL; i++ )
50                 ;
51         if (i > 0)
52                 attrs_tcl = Tcl_Merge(i, attrs);
53
54         for ( i = 0; be->be_suffix[i] != NULL; i++ )
55                 ;
56         suf_tcl = Tcl_Merge(i, be->be_suffix);
57
58         command = (char *) ch_malloc (strlen(ti->ti_search) + strlen(suf_tcl)
59                 + strlen(base) + 40 + strlen(filterstr) + (attrs_tcl == NULL ? 5
60                 : strlen(attrs_tcl)) + 72);
61         sprintf(command, "%s SEARCH {%ld} {%s} {%s} {%d} {%d} {%d} {%d} {%s} {%d} {%s}",
62                 ti->ti_search, op->o_msgid, suf_tcl, base, scope, deref,
63                 sizelimit, timelimit, filterstr, attrsonly ? 1 : 0, attrs_tcl == 
64                 NULL ? "{all}" : attrs_tcl);
65         Tcl_Free( attrs_tcl );
66         Tcl_Free( suf_tcl );
67
68         ldap_pvt_thread_mutex_lock( &tcl_interpreter_mutex );
69         code = Tcl_GlobalEval(ti->ti_ii->interp, command);
70         results = (char *) strdup(ti->ti_ii->interp->result);
71         ldap_pvt_thread_mutex_unlock( &tcl_interpreter_mutex );
72         free(command);
73
74         if (code != TCL_OK) {
75                 err = LDAP_OPERATIONS_ERROR;
76                 Debug(LDAP_DEBUG_ANY, "tcl_search_error: %s\n", results, 0, 0);
77         } else {
78                 interp_send_results ( be, conn, op, results, NULL, 0 );
79         }
80
81         if (err != LDAP_SUCCESS)
82                 send_ldap_result (conn, op, err, NULL, "internal backend error");
83         return (0);
84
85 }