]> git.sur5r.net Git - openldap/blob - servers/slapd/back-tcl/tcl_compare.c
2f7b522dcf09995cb1fddca7864e253d223e29f3
[openldap] / servers / slapd / back-tcl / tcl_compare.c
1 /* $OpenLDAP$ */
2 /* compare.c - tcl compare 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_compare (
21         Backend * be,
22         Connection * conn,
23         Operation * op,
24         struct berval *dn,
25         struct berval *ndn,
26         AttributeAssertion * ava
27 )
28 {
29         char *command, *results;
30         struct berval suf_tcl;
31         int code, err = 0;
32         struct tclinfo *ti = (struct tclinfo *) be->be_private;
33
34         if (ti->ti_compare.bv_len == 0) {
35                 send_ldap_result (conn, op, LDAP_UNWILLING_TO_PERFORM, NULL,
36                         "compare not implemented", NULL, NULL );
37                 return (-1);
38         }
39
40         if (tcl_merge_bvlist (be->be_suffix, &suf_tcl) == NULL) {
41                 send_ldap_result (conn, op, LDAP_OPERATIONS_ERROR, NULL,
42                         NULL, NULL, NULL );
43                 return (-1);
44         }
45
46         command = (char *) ch_malloc (ti->ti_compare.bv_len +
47                 suf_tcl.bv_len + dn->bv_len + ava->aa_desc->ad_cname.bv_len +
48                 ava->aa_value.bv_len + 64);
49         sprintf (command, "%s COMPARE {%ld} {%s} {%s} {%s: %s}",
50                 ti->ti_compare.bv_val, (long) op->o_msgid, suf_tcl.bv_val, 
51                 dn->bv_val,
52                 ava->aa_desc->ad_cname.bv_val, ava->aa_value.bv_val);
53         Tcl_Free (suf_tcl.bv_val);
54
55         ldap_pvt_thread_mutex_lock (&tcl_interpreter_mutex);
56         code = Tcl_GlobalEval (ti->ti_ii->interp, command);
57         results = (char *) ch_strdup (ti->ti_ii->interp->result);
58         ldap_pvt_thread_mutex_unlock (&tcl_interpreter_mutex);
59         free (command);
60
61         if (code != TCL_OK) {
62                 err = LDAP_OPERATIONS_ERROR;
63                 Debug (LDAP_DEBUG_SHELL, "tcl_compare_error: %s\n", results,
64                         0, 0);
65         } else {
66                 interp_send_results (be, conn, op, results, NULL, 0);
67         }
68
69         if (err != LDAP_SUCCESS)
70                 send_ldap_result (conn, op, err, NULL,
71                         "internal backend error", NULL, NULL );
72
73         free (results);
74         return (err);
75 }