Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / openldap / dist / servers / slapd / back-meta / modify.c
blob0fafd0c76fe47027a382e02e8aff716679465e7e
1 /* $OpenLDAP: pkg/ldap/servers/slapd/back-meta/modify.c,v 1.52.2.7 2008/02/12 00:25:47 quanah Exp $ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2008 The OpenLDAP Foundation.
5 * Portions Copyright 2001-2003 Pierangelo Masarati.
6 * Portions Copyright 1999-2003 Howard Chu.
7 * All rights reserved.
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.
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>.
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.
23 #include "portable.h"
25 #include <stdio.h>
27 #include <ac/string.h>
28 #include <ac/socket.h>
30 #include "slap.h"
31 #include "../back-ldap/back-ldap.h"
32 #include "back-meta.h"
34 int
35 meta_back_modify( Operation *op, SlapReply *rs )
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 int do_retry = 1;
51 LDAPControl **ctrls = NULL;
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;
58 assert( mc->mc_conns[ candidate ].msc_ld != NULL );
61 * Rewrite the modify dn, if needed
63 mt = mi->mi_targets[ candidate ];
64 dc.target = mt;
65 dc.conn = op->o_conn;
66 dc.rs = rs;
67 dc.ctx = "modifyDN";
69 if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
70 send_ldap_result( op, rs );
71 goto cleanup;
74 for ( i = 0, ml = op->orm_modlist; ml; i++ ,ml = ml->sml_next )
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;
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;
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;
95 if ( !isupdate && !get_relax( op ) && ml->sml_desc->ad_type->sat_no_user_mod )
97 continue;
100 if ( ml->sml_desc == slap_schema.si_ad_objectClass
101 || ml->sml_desc == slap_schema.si_ad_structuralObjectClass )
103 is_oc = 1;
104 mapped = ml->sml_desc->ad_cname;
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;
115 modv[ i ] = &mods[ i ];
116 mods[ i ].mod_op = ml->sml_op | LDAP_MOD_BVALUES;
117 mods[ i ].mod_type = mapped.bv_val;
120 * FIXME: dn-valued attrs should be rewritten
121 * to allow their use in ACLs at the back-ldap
122 * level.
124 if ( ml->sml_values != NULL ) {
125 if ( is_oc ) {
126 for ( j = 0; !BER_BVISNULL( &ml->sml_values[ j ] ); j++ )
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;
134 ldap_back_mapping( &mt->mt_rwmap.rwm_oc,
135 &ml->sml_values[ j ], &mapping, BACKLDAP_MAP );
137 if ( mapping == NULL ) {
138 if ( mt->mt_rwmap.rwm_oc.drop_missing ) {
139 continue;
141 mods[ i ].mod_bvalues[ j ] = &ml->sml_values[ j ];
143 } else {
144 mods[ i ].mod_bvalues[ j ] = &mapping->dst;
146 j++;
148 mods[ i ].mod_bvalues[ j ] = NULL;
150 } else {
151 if ( ml->sml_desc->ad_type->sat_syntax ==
152 slap_schema.si_syn_distinguishedName )
154 ( void )ldap_dnattr_rewrite( &dc, ml->sml_values );
155 if ( ml->sml_values == NULL ) {
156 continue;
160 for ( j = 0; !BER_BVISNULL( &ml->sml_values[ j ] ); j++ )
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 ];
168 mods[ i ].mod_bvalues[ j ] = NULL;
171 } else {
172 mods[ i ].mod_bvalues = NULL;
175 i++;
177 modv[ i ] = 0;
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;
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 );
191 if ( rs->sr_err == LDAP_UNAVAILABLE && do_retry ) {
192 do_retry = 0;
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;
200 cleanup:;
201 (void)mi->mi_ldap_extra->controls_free( op, rs, &ctrls );
203 if ( mdn.bv_val != op->o_req_dn.bv_val ) {
204 free( mdn.bv_val );
205 BER_BVZERO( &mdn );
207 if ( modv != NULL ) {
208 for ( i = 0; modv[ i ]; i++ ) {
209 free( modv[ i ]->mod_bvalues );
212 free( mods );
213 free( modv );
215 if ( mc ) {
216 meta_back_release_conn( mi, mc );
219 return rs->sr_err;