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