]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/modify.c
From HEAD:
[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 #ifdef LDAP_BACK_PROXY_AUTHZ 
51         LDAPControl **ctrls = NULL;
52 #endif /* LDAP_BACK_PROXY_AUTHZ */
53
54         lc = ldap_back_getconn(op, rs);
55         if ( !lc || !ldap_back_dobind( lc, op, rs ) ) {
56                 return( -1 );
57         }
58
59         /*
60          * Rewrite the modify dn, if needed
61          */
62         dc.rwmap = &li->rwmap;
63 #ifdef ENABLE_REWRITE
64         dc.conn = op->o_conn;
65         dc.rs = rs;
66         dc.ctx = "modifyDN";
67 #else
68         dc.tofrom = 1;
69         dc.normalized = 0;
70 #endif
71         if ( ldap_back_dn_massage( &dc, &op->o_req_ndn, &mdn ) ) {
72                 send_ldap_result( op, rs );
73                 return -1;
74         }
75
76         for (i=0, ml=op->oq_modify.rs_modlist; ml; i++,ml=ml->sml_next)
77                 ;
78
79         mods = (LDAPMod *)ch_malloc(i*sizeof(LDAPMod));
80         if (mods == NULL) {
81                 rc = LDAP_NO_MEMORY;
82                 goto cleanup;
83         }
84         modv = (LDAPMod **)ch_malloc((i+1)*sizeof(LDAPMod *));
85         if (modv == NULL) {
86                 rc = LDAP_NO_MEMORY;
87                 goto cleanup;
88         }
89
90 #ifdef ENABLE_REWRITE
91         dc.ctx = "modifyAttrDN";
92 #endif
93
94         isupdate = be_shadow_update( op );
95         for (i=0, ml=op->oq_modify.rs_modlist; ml; ml=ml->sml_next) {
96                 int     is_oc = 0;
97
98                 if ( !isupdate && ml->sml_desc->ad_type->sat_no_user_mod  ) {
99                         continue;
100                 }
101
102                 if ( ml->sml_desc == slap_schema.si_ad_objectClass 
103                                 || ml->sml_desc == slap_schema.si_ad_structuralObjectClass ) {
104                         is_oc = 1;
105                         mapped = ml->sml_desc->ad_cname;
106
107                 } else {
108                         ldap_back_map(&li->rwmap.rwm_at,
109                                         &ml->sml_desc->ad_cname,
110                                         &mapped, BACKLDAP_MAP);
111                         if (mapped.bv_val == NULL || mapped.bv_val[0] == '\0') {
112                                 continue;
113                         }
114                 }
115
116                 modv[i] = &mods[i];
117                 mods[i].mod_op = ml->sml_op | LDAP_MOD_BVALUES;
118                 mods[i].mod_type = mapped.bv_val;
119
120                 if ( ml->sml_values != NULL ) {
121                         if ( is_oc ) {
122                                 for (j = 0; ml->sml_values[j].bv_val; j++);
123                                 mods[i].mod_values = (struct berval **)ch_malloc((j+1) *
124                                         sizeof(struct berval *));
125                                 for (j = 0; ml->sml_values[j].bv_val; j++) {
126                                         ldap_back_map(&li->rwmap.rwm_oc,
127                                                         &ml->sml_values[j],
128                                                         &mapped, BACKLDAP_MAP);
129                                         if (mapped.bv_val == NULL || mapped.bv_val[0] == '\0') {
130                                                 continue;
131                                         }
132                                         mods[i].mod_values[j] = &mapped;
133                                 }
134                                 mods[i].mod_values[j] = NULL;
135
136                         } else {
137                                 if ( ml->sml_desc->ad_type->sat_syntax ==
138                                         slap_schema.si_syn_distinguishedName ) {
139                                         ldap_dnattr_rewrite( &dc, ml->sml_values );
140                                 }
141
142                                 if ( ml->sml_values == NULL ) { 
143                                         continue;
144                                 }
145
146                                 for (j = 0; ml->sml_values[j].bv_val; j++);
147                                 mods[i].mod_values = (struct berval **)ch_malloc((j+1) *
148                                         sizeof(struct berval *));
149                                 for (j = 0; ml->sml_values[j].bv_val; j++)
150                                         mods[i].mod_values[j] = &ml->sml_values[j];
151                                 mods[i].mod_values[j] = NULL;
152                         }
153
154                 } else {
155                         mods[i].mod_values = NULL;
156                 }
157
158                 i++;
159         }
160         modv[i] = 0;
161
162 #ifdef LDAP_BACK_PROXY_AUTHZ
163         rc = ldap_back_proxy_authz_ctrl( lc, op, rs, &ctrls );
164         if ( rc != LDAP_SUCCESS ) {
165                 goto cleanup;
166         }
167 #endif /* LDAP_BACK_PROXY_AUTHZ */
168
169         rs->sr_err = ldap_modify_ext( lc->ld, mdn.bv_val, modv,
170 #ifdef LDAP_BACK_PROXY_AUTHZ
171                         ctrls,
172 #else /* ! LDAP_BACK_PROXY_AUTHZ */
173                         op->o_ctrls,
174 #endif /* ! LDAP_BACK_PROXY_AUTHZ */
175                         NULL, &msgid );
176
177 cleanup:;
178 #ifdef LDAP_BACK_PROXY_AUTHZ
179         if ( ctrls && ctrls != op->o_ctrls ) {
180                 free( ctrls[ 0 ] );
181                 free( ctrls );
182         }
183 #endif /* LDAP_BACK_PROXY_AUTHZ */
184
185         if ( mdn.bv_val != op->o_req_ndn.bv_val ) {
186                 free( mdn.bv_val );
187         }
188         for (i=0; modv[i]; i++) {
189                 ch_free(modv[i]->mod_values);
190         }
191         ch_free( mods );
192         ch_free( modv );
193
194 #ifdef LDAP_BACK_PROXY_AUTHZ
195         if ( rc != LDAP_SUCCESS ) {
196                 send_ldap_result( op, rs );
197                 return -1;
198         }
199 #endif /* LDAP_BACK_PROXY_AUTHZ */
200
201         return ldap_back_op_result( lc, op, rs, msgid, 1 );
202 }
203