]> git.sur5r.net Git - openldap/blob - servers/slapd/back-tcl/tcl_modify.c
Add OpenLDAP RCSid
[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         Backend * be,
22         Connection * conn,
23         Operation * op,
24         char *dn,
25         LDAPModList * modlist
26 )
27 {
28         char *command, *suf_tcl, *bp, *tcl_mods, *results;
29         int i, code, err = 0, len, bsize;
30         struct tclinfo *ti = (struct tclinfo *) be->be_private;
31
32         if (ti->ti_modify == NULL) {
33                 send_ldap_result (conn, op, LDAP_UNWILLING_TO_PERFORM, NULL,
34                         "modify not implemented", NULL, NULL );
35                 return (-1);
36         }
37
38         for (i = 0; be->be_suffix[i] != NULL; i++);
39         suf_tcl = Tcl_Merge (i, be->be_suffix);
40
41         tcl_mods = (char *) ch_malloc (BUFSIZ);
42         tcl_mods[0] = '\0';
43         bsize = BUFSIZ;
44         bp = tcl_mods;
45
46         for (; modlist != NULL; modlist = modlist->ml_next) {
47                 LDAPMod *mods = &modlist->ml_mod;
48                 char *op = NULL;
49
50                 switch (mods->mod_op & ~LDAP_MOD_BVALUES) {
51                 case LDAP_MOD_ADD:
52                         op = "add";
53                         break;
54                 case LDAP_MOD_DELETE:
55                         op = "delete";
56                         break;
57                 case LDAP_MOD_REPLACE:
58                         op = "replace";
59                         break;
60                 }
61
62                 len = strlen (mods->mod_type) + strlen (op) + 7;
63                 while (bp + len - tcl_mods > bsize) {
64                         bsize += BUFSIZ;
65                         tcl_mods = (char *) ch_realloc (tcl_mods, bsize);
66                 }
67                 sprintf (bp, "{ {%s: %s} ", op, mods->mod_type);
68                 bp += len;
69                 for (i = 0;
70                         mods->mod_bvalues != NULL && mods->mod_bvalues[i]
71                         != NULL;
72                         i++) {
73                         len = strlen (mods->mod_type) + strlen (
74                                 mods->mod_bvalues[i]->bv_val) + 5 +
75                                 (mods->mod_bvalues[i + 1] == NULL ? 2 : 0);
76                         while (bp + len - tcl_mods > bsize) {
77                                 bsize += BUFSIZ;
78                                 tcl_mods = (char *) ch_realloc (tcl_mods, bsize);
79                         }
80                         sprintf (bp, "{%s: %s} %s", mods->mod_type,
81                                 mods->mod_bvalues[i]->bv_val,
82                                 mods->mod_bvalues[i + 1] ==
83                                 NULL ? "} " : "");
84                         bp += len;
85                 }
86         }
87
88         command = (char *) ch_malloc (strlen (ti->ti_modify) + strlen (suf_tcl)
89                 + strlen (dn) + strlen (tcl_mods) + 64);
90         /* This space is simply for aesthetics--\  */
91         sprintf (command, "%s MODIFY {%ld} {%s} {%s} { %s}",
92                 ti->ti_modify, op->o_msgid, suf_tcl, dn, tcl_mods);
93         Tcl_Free (suf_tcl);
94         free (tcl_mods);
95
96         ldap_pvt_thread_mutex_lock (&tcl_interpreter_mutex);
97         code = Tcl_GlobalEval (ti->ti_ii->interp, command);
98         results = (char *) ch_strdup (ti->ti_ii->interp->result);
99         ldap_pvt_thread_mutex_unlock (&tcl_interpreter_mutex);
100         free (command);
101
102         if (code != TCL_OK) {
103                 err = LDAP_OPERATIONS_ERROR;
104                 Debug (LDAP_DEBUG_SHELL, "tcl_modify_error: %s\n", results,
105                         0, 0);
106         } else {
107                 interp_send_results (be, conn, op, results, NULL, 0);
108         }
109
110         if (err != LDAP_SUCCESS)
111                 send_ldap_result (conn, op, err, NULL,
112                         "internal backend error", NULL, NULL );
113
114         free (results);
115         return (err);
116 }