1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1999-2021 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
meta_back_modify(Operation * op,SlapReply * rs)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 
53 	mc = meta_back_getconn( op, rs, &candidate, LDAP_BACK_SENDERR );
54 	if ( !mc || !meta_back_dobind( op, rs, mc, LDAP_BACK_SENDERR ) ) {
55 		return rs->sr_err;
56 	}
57 
58 	assert( mc->mc_conns[ candidate ].msc_ld != NULL );
59 
60 	/*
61 	 * Rewrite the modify dn, if needed
62 	 */
63 	mt = mi->mi_targets[ candidate ];
64 	dc.target = mt;
65 	dc.conn = op->o_conn;
66 	dc.rs = rs;
67 	dc.ctx = "modifyDN";
68 
69 	if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
70 		send_ldap_result( op, rs );
71 		goto cleanup;
72 	}
73 
74 	for ( i = 0, ml = op->orm_modlist; ml; i++ ,ml = ml->sml_next )
75 		;
76 
77 	mods = ch_malloc( sizeof( LDAPMod )*i );
78 	if ( mods == NULL ) {
79 		rs->sr_err = LDAP_OTHER;
80 		send_ldap_result( op, rs );
81 		goto cleanup;
82 	}
83 	modv = ( LDAPMod ** )ch_malloc( ( i + 1 )*sizeof( LDAPMod * ) );
84 	if ( modv == NULL ) {
85 		rs->sr_err = LDAP_OTHER;
86 		send_ldap_result( op, rs );
87 		goto cleanup;
88 	}
89 
90 	dc.ctx = "modifyAttrDN";
91 	isupdate = be_shadow_update( op );
92 	for ( i = 0, ml = op->orm_modlist; ml; ml = ml->sml_next ) {
93 		int	j, is_oc = 0;
94 
95 		if ( !isupdate && !get_relax( op ) && ml->sml_desc->ad_type->sat_no_user_mod  )
96 		{
97 			continue;
98 		}
99 
100 		if ( ml->sml_desc == slap_schema.si_ad_objectClass
101 				|| ml->sml_desc == slap_schema.si_ad_structuralObjectClass )
102 		{
103 			is_oc = 1;
104 			mapped = ml->sml_desc->ad_cname;
105 
106 		} else {
107 			ldap_back_map( &mt->mt_rwmap.rwm_at,
108 					&ml->sml_desc->ad_cname, &mapped,
109 					BACKLDAP_MAP );
110 			if ( BER_BVISNULL( &mapped ) || BER_BVISEMPTY( &mapped ) ) {
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 		/*
120 		 * FIXME: dn-valued attrs should be rewritten
121 		 * to allow their use in ACLs at the back-ldap
122 		 * level.
123 		 */
124 		if ( ml->sml_values != NULL ) {
125 			if ( is_oc ) {
126 				for ( j = 0; !BER_BVISNULL( &ml->sml_values[ j ] ); j++ )
127 					;
128 				mods[ i ].mod_bvalues =
129 					(struct berval **)ch_malloc( ( j + 1 ) *
130 					sizeof( struct berval * ) );
131 				for ( j = 0; !BER_BVISNULL( &ml->sml_values[ j ] ); ) {
132 					struct ldapmapping	*mapping;
133 
134 					ldap_back_mapping( &mt->mt_rwmap.rwm_oc,
135 							&ml->sml_values[ j ], &mapping, BACKLDAP_MAP );
136 
137 					if ( mapping == NULL ) {
138 						if ( mt->mt_rwmap.rwm_oc.drop_missing ) {
139 							continue;
140 						}
141 						mods[ i ].mod_bvalues[ j ] = &ml->sml_values[ j ];
142 
143 					} else {
144 						mods[ i ].mod_bvalues[ j ] = &mapping->dst;
145 					}
146 					j++;
147 				}
148 				mods[ i ].mod_bvalues[ j ] = NULL;
149 
150 			} else {
151 				if ( ml->sml_desc->ad_type->sat_syntax ==
152 						slap_schema.si_syn_distinguishedName )
153 				{
154 					( void )ldap_dnattr_rewrite( &dc, ml->sml_values );
155 					if ( ml->sml_values == NULL ) {
156 						continue;
157 					}
158 				}
159 
160 				for ( j = 0; !BER_BVISNULL( &ml->sml_values[ j ] ); j++ )
161 					;
162 				mods[ i ].mod_bvalues =
163 					(struct berval **)ch_malloc( ( j + 1 ) *
164 					sizeof( struct berval * ) );
165 				for ( j = 0; !BER_BVISNULL( &ml->sml_values[ j ] ); j++ ) {
166 					mods[ i ].mod_bvalues[ j ] = &ml->sml_values[ j ];
167 				}
168 				mods[ i ].mod_bvalues[ j ] = NULL;
169 			}
170 
171 		} else {
172 			mods[ i ].mod_bvalues = NULL;
173 		}
174 
175 		i++;
176 	}
177 	modv[ i ] = 0;
178 
179 retry:;
180 	ctrls = op->o_ctrls;
181 	rc = meta_back_controls_add( op, rs, mc, candidate, &ctrls );
182 	if ( rc != LDAP_SUCCESS ) {
183 		send_ldap_result( op, rs );
184 		goto cleanup;
185 	}
186 
187 	rs->sr_err = ldap_modify_ext( mc->mc_conns[ candidate ].msc_ld, mdn.bv_val,
188 			modv, ctrls, NULL, &msgid );
189 	rs->sr_err = meta_back_op_result( mc, op, rs, candidate, msgid,
190 		mt->mt_timeout[ SLAP_OP_MODIFY ], ( LDAP_BACK_SENDRESULT | retrying ) );
191 	if ( rs->sr_err == LDAP_UNAVAILABLE && retrying ) {
192 		retrying &= ~LDAP_BACK_RETRYING;
193 		if ( meta_back_retry( op, rs, &mc, candidate, LDAP_BACK_SENDERR ) ) {
194 			/* if the identity changed, there might be need to re-authz */
195 			(void)mi->mi_ldap_extra->controls_free( op, rs, &ctrls );
196 			goto retry;
197 		}
198 	}
199 
200 cleanup:;
201 	(void)mi->mi_ldap_extra->controls_free( op, rs, &ctrls );
202 
203 	if ( mdn.bv_val != op->o_req_dn.bv_val ) {
204 		free( mdn.bv_val );
205 		BER_BVZERO( &mdn );
206 	}
207 	if ( modv != NULL ) {
208 		for ( i = 0; modv[ i ]; i++ ) {
209 			free( modv[ i ]->mod_bvalues );
210 		}
211 	}
212 	free( mods );
213 	free( modv );
214 
215 	if ( mc ) {
216 		meta_back_release_conn( mi, mc );
217 	}
218 
219 	return rs->sr_err;
220 }
221 
222