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