]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/modify.c
fix ITS#3387
[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-2004 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 = BER_BVNULL;
47         ber_int_t msgid;
48         dncookie dc;
49         int isupdate;
50         int do_retry = 1;
51         LDAPControl **ctrls = NULL;
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_ndn, &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
93         isupdate = be_shadow_update( op );
94         for (i=0, ml=op->oq_modify.rs_modlist; ml; ml=ml->sml_next) {
95                 int     is_oc = 0;
96
97                 if ( !isupdate && ml->sml_desc->ad_type->sat_no_user_mod  ) {
98                         continue;
99                 }
100
101                 if ( ml->sml_desc == slap_schema.si_ad_objectClass 
102                                 || ml->sml_desc == slap_schema.si_ad_structuralObjectClass ) {
103                         is_oc = 1;
104                         mapped = ml->sml_desc->ad_cname;
105
106                 } else {
107                         ldap_back_map(&li->rwmap.rwm_at,
108                                         &ml->sml_desc->ad_cname,
109                                         &mapped, BACKLDAP_MAP);
110                         if (mapped.bv_val == NULL || mapped.bv_val[0] == '\0') {
111                                 continue;
112                         }
113                 }
114
115                 modv[i] = &mods[i];
116                 mods[i].mod_op = ml->sml_op | LDAP_MOD_BVALUES;
117                 mods[i].mod_type = mapped.bv_val;
118
119                 if ( ml->sml_values != NULL ) {
120                         if ( is_oc ) {
121                                 for (j = 0; ml->sml_values[j].bv_val; j++);
122                                 mods[i].mod_bvalues = (struct berval **)ch_malloc((j+1) *
123                                         sizeof(struct berval *));
124                                 for (j = 0; ml->sml_values[j].bv_val; j++) {
125                                         ldap_back_map(&li->rwmap.rwm_oc,
126                                                         &ml->sml_values[j],
127                                                         &mapped, BACKLDAP_MAP);
128                                         if (mapped.bv_val == NULL || mapped.bv_val[0] == '\0') {
129                                                 continue;
130                                         }
131                                         mods[i].mod_bvalues[j] = &mapped;
132                                 }
133                                 mods[i].mod_bvalues[j] = NULL;
134
135                         } else {
136                                 if ( ml->sml_desc->ad_type->sat_syntax ==
137                                         slap_schema.si_syn_distinguishedName ) {
138                                         ldap_dnattr_rewrite( &dc, ml->sml_values );
139                                 }
140
141                                 if ( ml->sml_values == NULL ) { 
142                                         continue;
143                                 }
144
145                                 for (j = 0; ml->sml_values[j].bv_val; j++);
146                                 mods[i].mod_bvalues = (struct berval **)ch_malloc((j+1) *
147                                         sizeof(struct berval *));
148                                 for (j = 0; ml->sml_values[j].bv_val; j++)
149                                         mods[i].mod_bvalues[j] = &ml->sml_values[j];
150                                 mods[i].mod_bvalues[j] = NULL;
151                         }
152
153                 } else {
154                         mods[i].mod_bvalues = NULL;
155                 }
156
157                 i++;
158         }
159         modv[i] = 0;
160
161         ctrls = op->o_ctrls;
162 #ifdef LDAP_BACK_PROXY_AUTHZ
163         rc = ldap_back_proxy_authz_ctrl( lc, op, rs, &ctrls );
164         if ( rc != LDAP_SUCCESS ) {
165                 send_ldap_result( op, rs );
166                 rc = -1;
167                 goto cleanup;
168         }
169 #endif /* LDAP_BACK_PROXY_AUTHZ */
170
171 retry:
172         rs->sr_err = ldap_modify_ext( lc->ld, mdn.bv_val, modv,
173                         ctrls, NULL, &msgid );
174         rc = ldap_back_op_result( lc, op, rs, msgid, 1 );
175         if ( rs->sr_err == LDAP_UNAVAILABLE && do_retry ) {
176                 do_retry = 0;
177                 if ( ldap_back_retry (lc, op, rs )) goto retry;
178         }
179
180 cleanup:;
181 #ifdef LDAP_BACK_PROXY_AUTHZ
182         if ( ctrls && ctrls != op->o_ctrls ) {
183                 free( ctrls[ 0 ] );
184                 free( ctrls );
185         }
186 #endif /* LDAP_BACK_PROXY_AUTHZ */
187
188         if ( mdn.bv_val != op->o_req_ndn.bv_val ) {
189                 free( mdn.bv_val );
190         }
191         for (i=0; modv[i]; i++) {
192                 ch_free(modv[i]->mod_bvalues);
193         }
194         ch_free( mods );
195         ch_free( modv );
196
197         return rc;
198 }
199