Debian Codegraph @

servers/slapd/back-meta/modify.c

raw
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3 *
4 * Copyright 1999-2024 The OpenLDAP Foundation.
5 * Portions Copyright 2001-2003 Pierangelo Masarati.
6 * Portions Copyright 1999-2003 Howard Chu.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
11 * Public License.
12 *
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
16 */
17 /* ACKNOWLEDGEMENTS:
18 * This work was initially developed by the Howard Chu for inclusion
19 * in OpenLDAP Software and subsequently enhanced by Pierangelo
20 * Masarati.
21 */
22
23 #include "portable.h"
24
25 #include <stdio.h>
26
27 #include <ac/string.h>
28 #include <ac/socket.h>
29
30 #include "slap.h"
31 #include "../back-ldap/back-ldap.h"
32 #include "back-meta.h"
33
34 int
35 meta_back_modify( Operation *op, SlapReply *rs )
36 {
37 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
38 metatarget_t *mt;
39 metaconn_t *mc;
40 int rc = 0;
41 LDAPMod **modv = NULL;
42 LDAPMod *mods = NULL;
43 Modifications *ml;
44 int candidate = -1, i;
45 int isupdate;
46 struct berval mdn = BER_BVNULL;
47 struct berval mapped;
48 dncookie dc;
49 int msgid;
50 ldap_back_send_t retrying = LDAP_BACK_RETRYING;
51 LDAPControl **ctrls = NULL;
52 SlapReply *candidates = NULL;
53
54 candidates = meta_back_candidates_get( op );
55 mc = meta_back_getconn( op, rs, &candidate, LDAP_BACK_SENDERR, candidates );
56 if ( !mc || !meta_back_dobind( op, rs, mc, LDAP_BACK_SENDERR, candidates ) ) {
57 op->o_tmpfree( candidates, op->o_tmpmemctx );
58 return rs->sr_err;
59 }
60
61 assert( mc->mc_conns[ candidate ].msc_ld != NULL );
62
63 /*
64 * Rewrite the modify dn, if needed
65 */
66 mt = mi->mi_targets[ candidate ];
67 dc.target = mt;
68 dc.conn = op->o_conn;
69 dc.rs = rs;
70 dc.ctx = "modifyDN";
71
72 if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
73 send_ldap_result( op, rs );
74 goto cleanup;
75 }
76
77 for ( i = 0, ml = op->orm_modlist; ml; i++ ,ml = ml->sml_next )
78 ;
79
80 modv = ch_malloc( ( i + 1 )*sizeof( LDAPMod * ) + i*sizeof( LDAPMod ) );
81 mods = (LDAPMod *)&modv[ i + 1 ];
82
83 dc.ctx = "modifyAttrDN";
84 isupdate = be_shadow_update( op );
85 for ( i = 0, ml = op->orm_modlist; ml; ml = ml->sml_next ) {
86 int j, is_oc = 0;
87
88 if ( !isupdate && !get_relax( op ) && ml->sml_desc->ad_type->sat_no_user_mod )
89 {
90 continue;
91 }
92
93 if ( ml->sml_desc == slap_schema.si_ad_objectClass
94 || ml->sml_desc == slap_schema.si_ad_structuralObjectClass )
95 {
96 is_oc = 1;
97 mapped = ml->sml_desc->ad_cname;
98
99 } else {
100 ldap_back_map( &mt->mt_rwmap.rwm_at,
101 &ml->sml_desc->ad_cname, &mapped,
102 BACKLDAP_MAP );
103 if ( BER_BVISNULL( &mapped ) || BER_BVISEMPTY( &mapped ) ) {
104 continue;
105 }
106 }
107
108 modv[ i ] = &mods[ i ];
109 mods[ i ].mod_op = ml->sml_op | LDAP_MOD_BVALUES;
110 mods[ i ].mod_type = mapped.bv_val;
111
112 /*
113 * FIXME: dn-valued attrs should be rewritten
114 * to allow their use in ACLs at the back-ldap
115 * level.
116 */
117 if ( ml->sml_values != NULL ) {
118 if ( is_oc ) {
119 for ( j = 0; !BER_BVISNULL( &ml->sml_values[ j ] ); j++ )
120 ;
121 mods[ i ].mod_bvalues =
122 (struct berval **)ch_malloc( ( j + 1 ) *
123 sizeof( struct berval * ) );
124 for ( j = 0; !BER_BVISNULL( &ml->sml_values[ j ] ); ) {
125 struct ldapmapping *mapping;
126
127 ldap_back_mapping( &mt->mt_rwmap.rwm_oc,
128 &ml->sml_values[ j ], &mapping, BACKLDAP_MAP );
129
130 if ( mapping == NULL ) {
131 if ( mt->mt_rwmap.rwm_oc.drop_missing ) {
132 continue;
133 }
134 mods[ i ].mod_bvalues[ j ] = &ml->sml_values[ j ];
135
136 } else {
137 mods[ i ].mod_bvalues[ j ] = &mapping->dst;
138 }
139 j++;
140 }
141 mods[ i ].mod_bvalues[ j ] = NULL;
142
143 } else {
144 if ( ml->sml_desc->ad_type->sat_syntax ==
145 slap_schema.si_syn_distinguishedName )
146 {
147 ( void )ldap_dnattr_rewrite( &dc, ml->sml_values );
148 if ( ml->sml_values == NULL ) {
149 continue;
150 }
151 }
152
153 for ( j = 0; !BER_BVISNULL( &ml->sml_values[ j ] ); j++ )
154 ;
155 mods[ i ].mod_bvalues =
156 (struct berval **)ch_malloc( ( j + 1 ) *
157 sizeof( struct berval * ) );
158 for ( j = 0; !BER_BVISNULL( &ml->sml_values[ j ] ); j++ ) {
159 mods[ i ].mod_bvalues[ j ] = &ml->sml_values[ j ];
160 }
161 mods[ i ].mod_bvalues[ j ] = NULL;
162 }
163
164 } else {
165 mods[ i ].mod_bvalues = NULL;
166 }
167
168 i++;
169 }
170 modv[ i ] = 0;
171
172 retry:;
173 ctrls = op->o_ctrls;
174 rc = meta_back_controls_add( op, rs, mc, candidate, &ctrls );
175 if ( rc != LDAP_SUCCESS ) {
176 send_ldap_result( op, rs );
177 goto cleanup;
178 }
179
180 rs->sr_err = ldap_modify_ext( mc->mc_conns[ candidate ].msc_ld, mdn.bv_val,
181 modv, ctrls, NULL, &msgid );
182 rs->sr_err = meta_back_op_result( mc, op, rs, candidate, msgid,
183 mt->mt_timeout[ SLAP_OP_MODIFY ], ( LDAP_BACK_SENDRESULT | retrying ) );
184 if ( rs->sr_err == LDAP_UNAVAILABLE && retrying ) {
185 retrying &= ~LDAP_BACK_RETRYING;
186 if ( meta_back_retry( op, rs, &mc, candidate, LDAP_BACK_SENDERR, candidates ) ) {
187 /* if the identity changed, there might be need to re-authz */
188 (void)mi->mi_ldap_extra->controls_free( op, rs, &ctrls );
189 goto retry;
190 }
191 }
192
193 cleanup:;
194 (void)mi->mi_ldap_extra->controls_free( op, rs, &ctrls );
195
196 if ( mdn.bv_val != op->o_req_dn.bv_val ) {
197 free( mdn.bv_val );
198 BER_BVZERO( &mdn );
199 }
200 if ( modv != NULL ) {
201 for ( i = 0; modv[ i ]; i++ ) {
202 ch_free( modv[ i ]->mod_bvalues );
203 }
204 }
205 ch_free( modv );
206
207 if ( mc ) {
208 meta_back_release_conn( mi, mc );
209 }
210
211 op->o_tmpfree( candidates, op->o_tmpmemctx );
212 return rs->sr_err;
213 }
214