]> git.sur5r.net Git - openldap/blob - servers/slapd/back-tcl/tcl_compare.c
Import Ben Collins <bcollins@debian.org> Back-TCL for SLAPD.
[openldap] / servers / slapd / back-tcl / tcl_compare.c
1 /*
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         char *dn,
25         Ava * ava
26 )
27 {
28         char *command, *suf_tcl, *results;
29         int i, code, err = 0;
30         struct tclinfo *ti = (struct tclinfo *) be->be_private;
31
32         if (ti->ti_compare == NULL) {
33                 send_ldap_result (conn, op, LDAP_UNWILLING_TO_PERFORM, NULL,
34                         "compare not implemented");
35                 return (-1);
36         }
37
38         for ( i = 0; be->be_suffix[i] != NULL; i++ )
39                 ;
40         suf_tcl = Tcl_Merge(i, be->be_suffix);
41
42         command = (char *) ch_malloc (strlen(ti->ti_compare) +
43                 strlen(suf_tcl) + strlen(dn) + strlen(ava->ava_type) +
44                 strlen(ava->ava_value.bv_val) + 64);
45         sprintf(command, "%s COMPARE {%ld} {%s} {%s} {%s: %s}",
46                 ti->ti_compare, op->o_msgid, suf_tcl, dn, ava->ava_type,
47         ava->ava_value.bv_val);
48         Tcl_Free(suf_tcl);
49
50         ldap_pvt_thread_mutex_lock( &tcl_interpreter_mutex );
51         code = Tcl_GlobalEval(ti->ti_ii->interp, command);
52         results = (char *) strdup(ti->ti_ii->interp->result);
53         ldap_pvt_thread_mutex_unlock( &tcl_interpreter_mutex );
54         free(command);
55
56         if (code != TCL_OK) {
57                 err = LDAP_OPERATIONS_ERROR;
58                 Debug(LDAP_DEBUG_ANY, "tcl_compare_error: %s\n", results, 0, 0);
59         } else {
60                 interp_send_results ( be, conn, op, results, NULL, 0 );
61         }
62
63         if (err != LDAP_SUCCESS)
64                 send_ldap_result (conn, op, err, NULL, "internal backend error");
65         return (0);
66
67 }