]> git.sur5r.net Git - openldap/blob - servers/slapd/back-tcl/tcl_modify.c
Notices
[openldap] / servers / slapd / back-tcl / tcl_modify.c
1 /* $OpenLDAP$ */
2 /* modify.c - tcl modify 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_modify (
21         Operation * op,
22         SlapReply * rs
23 )
24 {
25         char *command, *bp, *tcl_mods, *results;
26         struct berval suf_tcl;
27         int i, code, len, bsize;
28         struct tclinfo *ti = (struct tclinfo *) op->o_bd->be_private;
29         Modifications *modlist = op->oq_modify.rs_modlist;
30
31         if (ti->ti_modify.bv_len == 0) {
32                 send_ldap_error (op, rs, LDAP_UNWILLING_TO_PERFORM,
33                         "modify not implemented" );
34                 return (-1);
35         }
36
37         if (tcl_merge_bvlist (op->o_bd->be_suffix, &suf_tcl) == NULL) {
38                 send_ldap_error (op, rs, LDAP_OTHER, NULL);
39                 return (-1);
40         }
41
42         tcl_mods = (char *) ch_malloc (BUFSIZ);
43         tcl_mods[0] = '\0';
44         bsize = BUFSIZ;
45         bp = tcl_mods;
46
47         for (; modlist != NULL; modlist = modlist->sml_next) {
48                 Modification *mods = &modlist->sml_mod;
49                 const struct berval 
50                         op_add = { sizeof("add") - 1, "add" },
51                         op_delete = { sizeof("delete") - 1, "delete" },
52                         op_replace = { sizeof("replace") - 1, "replace" },
53                         *op = NULL;
54
55                 switch (mods->sm_op & ~LDAP_MOD_BVALUES) {
56                 case LDAP_MOD_ADD:
57                         op = &op_add;
58                         break;
59                 case LDAP_MOD_DELETE:
60                         op = &op_delete;
61                         break;
62                 case LDAP_MOD_REPLACE:
63                         op = &op_replace;
64                         break;
65                 default:
66                         assert(0);
67                 }
68
69                 len = mods->sm_type.bv_len + op->bv_len + 7;
70                 while (bp + len - tcl_mods > bsize) {
71                         bsize += BUFSIZ;
72                         tcl_mods = (char *) ch_realloc (tcl_mods, bsize);
73                 }
74                 sprintf (bp, "{ {%s: %s} ", op->bv_val, mods->sm_type.bv_val);
75                 bp += len;
76                 for (i = 0;
77                         mods->sm_bvalues != NULL && mods->sm_bvalues[i].bv_val
78                         != NULL;
79                         i++) {
80                         len = mods->sm_type.bv_len +
81                                 mods->sm_bvalues[i].bv_len + 5 +
82                                 (mods->sm_bvalues[i + 1].bv_val == NULL ? 2 : 0);
83                         while (bp + len - tcl_mods > bsize) {
84                                 bsize += BUFSIZ;
85                                 tcl_mods = (char *) ch_realloc (tcl_mods, bsize);
86                         }
87                         sprintf (bp, "{%s: %s} %s", mods->sm_type.bv_val,
88                                 mods->sm_bvalues[i].bv_val,
89                                 mods->sm_bvalues[i + 1].bv_val ==
90                                 NULL ? "} " : "");
91                         bp += len;
92                 }
93         }
94
95         command = (char *) ch_malloc (ti->ti_modify.bv_len + suf_tcl.bv_len
96                 + op->o_req_dn.bv_len + strlen (tcl_mods) + 84);
97         /* This space is simply for aesthetics--\  */
98         sprintf (command, "%s MODIFY {%ld/%ld} {%s} {%s} { %s}",
99                 ti->ti_modify.bv_val, op->o_connid, (long) op->o_msgid,
100                 suf_tcl.bv_val, op->o_req_dn.bv_val, tcl_mods);
101         Tcl_Free (suf_tcl.bv_val);
102         free (tcl_mods);
103
104         ldap_pvt_thread_mutex_lock (&tcl_interpreter_mutex);
105         code = Tcl_GlobalEval (ti->ti_ii->interp, command);
106         results = (char *) ch_strdup (ti->ti_ii->interp->result);
107         ldap_pvt_thread_mutex_unlock (&tcl_interpreter_mutex);
108         free (command);
109
110         if (code != TCL_OK) {
111                 rs->sr_err = LDAP_OTHER;
112                 Debug (LDAP_DEBUG_SHELL, "tcl_modify_error: %s\n", results,
113                         0, 0);
114         } else {
115                 interp_send_results (op, rs, results);
116         }
117
118         if (rs->sr_err != LDAP_SUCCESS) {
119                 rs->sr_text = "internal backend error";
120                 send_ldap_result (op, rs);
121         }
122
123         free (results);
124         return (rs->sr_err);
125 }