]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/modify.c
Sync with HEAD
[openldap] / servers / slapd / back-meta / modify.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1999-2005 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         metaconn_t      *mc;
39         int             rc = 0;
40         LDAPMod         **modv = NULL;
41         LDAPMod         *mods = NULL;
42         Modifications   *ml;
43         int             candidate = -1, i;
44         int             isupdate;
45         struct berval   mdn = BER_BVNULL;
46         struct berval   mapped;
47         dncookie        dc;
48         int             do_retry = 1;
49
50         mc = meta_back_getconn( op, rs, &candidate, LDAP_BACK_SENDERR );
51         if ( !mc || !meta_back_dobind( op, rs, mc, LDAP_BACK_SENDERR ) ) {
52                 return rs->sr_err;
53         }
54
55         assert( mc->mc_conns[ candidate ].msc_ld != NULL );
56
57         /*
58          * Rewrite the modify dn, if needed
59          */
60         dc.target = &mi->mi_targets[ candidate ];
61         dc.conn = op->o_conn;
62         dc.rs = rs;
63         dc.ctx = "modifyDN";
64
65         if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
66                 rc = -1;
67                 goto cleanup;
68         }
69
70         for ( i = 0, ml = op->orm_modlist; ml; i++ ,ml = ml->sml_next )
71                 ;
72
73         mods = ch_malloc( sizeof( LDAPMod )*i );
74         if ( mods == NULL ) {
75                 rs->sr_err = LDAP_NO_MEMORY;
76                 rc = -1;
77                 goto cleanup;
78         }
79         modv = ( LDAPMod ** )ch_malloc( ( i + 1 )*sizeof( LDAPMod * ) );
80         if ( modv == NULL ) {
81                 rs->sr_err = LDAP_NO_MEMORY;
82                 rc = -1;
83                 goto cleanup;
84         }
85
86         dc.ctx = "modifyAttrDN";
87         isupdate = be_shadow_update( op );
88         for ( i = 0, ml = op->orm_modlist; ml; ml = ml->sml_next ) {
89                 int     j, is_oc = 0;
90
91                 if ( !isupdate && ml->sml_desc->ad_type->sat_no_user_mod  ) {
92                         continue;
93                 }
94
95                 if ( ml->sml_desc == slap_schema.si_ad_objectClass 
96                                 || ml->sml_desc == slap_schema.si_ad_structuralObjectClass )
97                 {
98                         is_oc = 1;
99                         mapped = ml->sml_desc->ad_cname;
100
101                 } else {
102                         ldap_back_map( &mi->mi_targets[ candidate ].mt_rwmap.rwm_at,
103                                         &ml->sml_desc->ad_cname, &mapped,
104                                         BACKLDAP_MAP );
105                         if ( BER_BVISNULL( &mapped ) || BER_BVISEMPTY( &mapped ) ) {
106                                 continue;
107                         }
108                 }
109
110                 modv[ i ] = &mods[ i ];
111                 mods[ i ].mod_op = ml->sml_op | LDAP_MOD_BVALUES;
112                 mods[ i ].mod_type = mapped.bv_val;
113
114                 /*
115                  * FIXME: dn-valued attrs should be rewritten
116                  * to allow their use in ACLs at the back-ldap
117                  * level.
118                  */
119                 if ( ml->sml_values != NULL ) {
120                         if ( is_oc ) {
121                                 for ( j = 0; !BER_BVISNULL( &ml->sml_values[ j ] ); j++ )
122                                         ;
123                                 mods[ i ].mod_bvalues =
124                                         (struct berval **)ch_malloc( ( j + 1 ) *
125                                         sizeof( struct berval * ) );
126                                 for ( j = 0; !BER_BVISNULL( &ml->sml_values[ j ] ); ) {
127                                         struct ldapmapping      *mapping;
128
129                                         ldap_back_mapping( &mi->mi_targets[ candidate ].mt_rwmap.rwm_oc,
130                                                         &ml->sml_values[ j ], &mapping, BACKLDAP_MAP );
131
132                                         if ( mapping == NULL ) {
133                                                 if ( mi->mi_targets[ candidate ].mt_rwmap.rwm_oc.drop_missing ) {
134                                                         continue;
135                                                 }
136                                                 mods[ i ].mod_bvalues[ j ] = &ml->sml_values[ j ];
137
138                                         } else {
139                                                 mods[ i ].mod_bvalues[ j ] = &mapping->dst;
140                                         }
141                                         j++;
142                                 }
143                                 mods[ i ].mod_bvalues[ j ] = NULL;
144
145                         } else {
146                                 if ( ml->sml_desc->ad_type->sat_syntax ==
147                                                 slap_schema.si_syn_distinguishedName )
148                                 {
149                                         ( void )ldap_dnattr_rewrite( &dc, ml->sml_values );
150                                         if ( ml->sml_values == NULL ) {
151                                                 continue;
152                                         }
153                                 }
154
155                                 for ( j = 0; !BER_BVISNULL( &ml->sml_values[ j ] ); j++ )
156                                         ;
157                                 mods[ i ].mod_bvalues =
158                                         (struct berval **)ch_malloc( ( j + 1 ) *
159                                         sizeof( struct berval * ) );
160                                 for ( j = 0; !BER_BVISNULL( &ml->sml_values[ j ] ); j++ ) {
161                                         mods[ i ].mod_bvalues[ j ] = &ml->sml_values[ j ];
162                                 }
163                                 mods[ i ].mod_bvalues[ j ] = NULL;
164                         }
165
166                 } else {
167                         mods[ i ].mod_bvalues = NULL;
168                 }
169
170                 i++;
171         }
172         modv[ i ] = 0;
173
174 retry:;
175         rs->sr_err = ldap_modify_ext_s( mc->mc_conns[ candidate ].msc_ld, mdn.bv_val,
176                         modv, op->o_ctrls, NULL );
177         if ( rs->sr_err == LDAP_UNAVAILABLE && do_retry ) {
178                 do_retry = 0;
179                 if ( meta_back_retry( op, rs, mc, candidate, LDAP_BACK_SENDERR ) ) {
180                         goto retry;
181                 }
182         }
183
184 cleanup:;
185         if ( mdn.bv_val != op->o_req_dn.bv_val ) {
186                 free( mdn.bv_val );
187                 BER_BVZERO( &mdn );
188         }
189         if ( modv != NULL ) {
190                 for ( i = 0; modv[ i ]; i++ ) {
191                         free( modv[ i ]->mod_bvalues );
192                 }
193         }
194         free( mods );
195         free( modv );
196
197         if ( rc != -1 ) {
198                 rc = meta_back_op_result( mc, op, rs, candidate );
199
200         } else {
201                 send_ldap_result( op, rs );
202                 rc = 0;
203         }
204
205         meta_back_release_conn( op, mc );
206
207         return rc;
208 }
209