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