]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/modify.c
ITS#2895 store a copy in the entry cache
[openldap] / servers / slapd / back-ldap / modify.c
1 /* modify.c - ldap backend modify function */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1999-2003 The OpenLDAP Foundation.
6  * Portions Copyright 1999-2003 Howard Chu.
7  * Portions Copyright 2000-2003 Pierangelo Masarati.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted only as authorized by the OpenLDAP
12  * Public License.
13  *
14  * A copy of this license is available in the file LICENSE in the
15  * top-level directory of the distribution or, alternatively, at
16  * <http://www.OpenLDAP.org/license.html>.
17  */
18 /* ACKNOWLEDGEMENTS:
19  * This work was initially developed by the Howard Chu for inclusion
20  * in OpenLDAP Software and subsequently enhanced by Pierangelo
21  * Masarati.
22  */
23
24 #include "portable.h"
25
26 #include <stdio.h>
27
28 #include <ac/string.h>
29 #include <ac/socket.h>
30
31 #include "slap.h"
32 #include "back-ldap.h"
33
34 int
35 ldap_back_modify(
36     Operation   *op,
37     SlapReply   *rs )
38 {
39         struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
40         struct ldapconn *lc;
41         LDAPMod **modv = NULL;
42         LDAPMod *mods;
43         Modifications *ml;
44         int i, j, rc;
45         struct berval mapped;
46         struct berval mdn = { 0, NULL };
47         ber_int_t msgid;
48         dncookie dc;
49 #ifdef LDAP_BACK_PROXY_AUTHZ 
50         LDAPControl **ctrls = NULL;
51 #endif /* LDAP_BACK_PROXY_AUTHZ */
52
53         lc = ldap_back_getconn(op, rs);
54         if ( !lc || !ldap_back_dobind( lc, op, rs ) ) {
55                 return( -1 );
56         }
57
58         /*
59          * Rewrite the modify dn, if needed
60          */
61         dc.rwmap = &li->rwmap;
62 #ifdef ENABLE_REWRITE
63         dc.conn = op->o_conn;
64         dc.rs = rs;
65         dc.ctx = "modifyDn";
66 #else
67         dc.tofrom = 1;
68         dc.normalized = 0;
69 #endif
70         if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
71                 send_ldap_result( op, rs );
72                 return -1;
73         }
74
75         for (i=0, ml=op->oq_modify.rs_modlist; ml; i++,ml=ml->sml_next)
76                 ;
77
78         mods = (LDAPMod *)ch_malloc(i*sizeof(LDAPMod));
79         if (mods == NULL) {
80                 rc = LDAP_NO_MEMORY;
81                 goto cleanup;
82         }
83         modv = (LDAPMod **)ch_malloc((i+1)*sizeof(LDAPMod *));
84         if (modv == NULL) {
85                 rc = LDAP_NO_MEMORY;
86                 goto cleanup;
87         }
88
89 #ifdef ENABLE_REWRITE
90         dc.ctx = "modifyAttrDN";
91 #endif
92         for (i=0, ml=op->oq_modify.rs_modlist; ml; ml=ml->sml_next) {
93                 int     is_oc = 0;
94
95                 if ( ml->sml_desc->ad_type->sat_no_user_mod  ) {
96                         continue;
97                 }
98
99                 if ( ml->sml_desc == slap_schema.si_ad_objectClass 
100                                 || ml->sml_desc == slap_schema.si_ad_structuralObjectClass ) {
101                         is_oc = 1;
102                         mapped = ml->sml_desc->ad_cname;
103
104                 } else {
105                         ldap_back_map(&li->rwmap.rwm_at,
106                                         &ml->sml_desc->ad_cname,
107                                         &mapped, BACKLDAP_MAP);
108                         if (mapped.bv_val == NULL || mapped.bv_val[0] == '\0') {
109                                 continue;
110                         }
111                 }
112
113                 modv[i] = &mods[i];
114                 mods[i].mod_op = ml->sml_op | LDAP_MOD_BVALUES;
115                 mods[i].mod_type = mapped.bv_val;
116
117                 if ( ml->sml_bvalues != NULL ) {
118                         if ( is_oc ) {
119                                 for (j = 0; ml->sml_bvalues[j].bv_val; j++);
120                                 mods[i].mod_bvalues = (struct berval **)ch_malloc((j+1) *
121                                         sizeof(struct berval *));
122                                 for (j = 0; ml->sml_bvalues[j].bv_val; j++) {
123                                         ldap_back_map(&li->rwmap.rwm_oc,
124                                                         &ml->sml_bvalues[j],
125                                                         &mapped, BACKLDAP_MAP);
126                                         if (mapped.bv_val == NULL || mapped.bv_val[0] == '\0') {
127                                                 continue;
128                                         }
129                                         mods[i].mod_bvalues[j] = &mapped;
130                                 }
131                                 mods[i].mod_bvalues[j] = NULL;
132
133                         } else {
134                                 if ( ml->sml_desc->ad_type->sat_syntax ==
135                                         slap_schema.si_syn_distinguishedName ) {
136                                         ldap_dnattr_rewrite( &dc, ml->sml_bvalues );
137                                 }
138
139                                 if ( ml->sml_bvalues == NULL ) {        
140                                         continue;
141                                 }
142
143                                 for (j = 0; ml->sml_bvalues[j].bv_val; j++);
144                                 mods[i].mod_bvalues = (struct berval **)ch_malloc((j+1) *
145                                         sizeof(struct berval *));
146                                 for (j = 0; ml->sml_bvalues[j].bv_val; j++)
147                                         mods[i].mod_bvalues[j] = &ml->sml_bvalues[j];
148                                 mods[i].mod_bvalues[j] = NULL;
149                         }
150
151                 } else {
152                         mods[i].mod_bvalues = NULL;
153                 }
154
155                 i++;
156         }
157         modv[i] = 0;
158
159 #ifdef LDAP_BACK_PROXY_AUTHZ
160         rc = ldap_back_proxy_authz_ctrl( lc, op, rs, &ctrls );
161         if ( rc != LDAP_SUCCESS ) {
162                 goto cleanup;
163         }
164 #endif /* LDAP_BACK_PROXY_AUTHZ */
165
166         rs->sr_err = ldap_modify_ext( lc->ld, mdn.bv_val, modv,
167 #ifdef LDAP_BACK_PROXY_AUTHZ
168                         ctrls,
169 #else /* ! LDAP_BACK_PROXY_AUTHZ */
170                         op->o_ctrls,
171 #endif /* ! LDAP_BACK_PROXY_AUTHZ */
172                         NULL, &msgid );
173
174 cleanup:;
175 #ifdef LDAP_BACK_PROXY_AUTHZ
176         if ( ctrls && ctrls != op->o_ctrls ) {
177                 free( ctrls[ 0 ] );
178                 free( ctrls );
179         }
180 #endif /* LDAP_BACK_PROXY_AUTHZ */
181
182         if ( mdn.bv_val != op->o_req_dn.bv_val ) {
183                 free( mdn.bv_val );
184         }
185         for (i=0; modv[i]; i++) {
186                 ch_free(modv[i]->mod_bvalues);
187         }
188         ch_free( mods );
189         ch_free( modv );
190
191 #ifdef LDAP_BACK_PROXY_AUTHZ
192         if ( rc != LDAP_SUCCESS ) {
193                 send_ldap_result( op, rs );
194                 return -1;
195         }
196 #endif /* LDAP_BACK_PROXY_AUTHZ */
197
198         return ldap_back_op_result( lc, op, rs, msgid, 1 );
199 }
200