]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/modify.c
12c1d7107f991ff38e10f42949c06952fd51ac28
[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         struct metainfo *li = ( struct metainfo * )op->o_bd->be_private;
38         struct metaconn *lc;
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             msgid, do_retry = 1;
49
50         lc = meta_back_getconn( op, rs, &candidate, LDAP_BACK_SENDERR );
51         if ( !lc || !meta_back_dobind( lc, op, LDAP_BACK_SENDERR ) ) {
52                 return rs->sr_err;
53         }
54         
55         if ( !meta_back_is_valid( lc, candidate ) ) {
56                 rs->sr_err = LDAP_OTHER;
57                 send_ldap_result( op, rs );
58                 return rs->sr_err;
59         }
60
61         /*
62          * Rewrite the modify dn, if needed
63          */
64         dc.rwmap = &li->mi_targets[ candidate ]->mt_rwmap;
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                 rc = -1;
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_NO_MEMORY;
80                 rc = -1;
81                 goto cleanup;
82         }
83         modv = ( LDAPMod ** )ch_malloc( ( i + 1 )*sizeof( LDAPMod * ) );
84         if ( modv == NULL ) {
85                 rs->sr_err = LDAP_NO_MEMORY;
86                 rc = -1;
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 && ml->sml_desc->ad_type->sat_no_user_mod  ) {
96                         continue;
97                 }
98
99                 if ( ml->sml_desc == slap_schema.si_ad_objectClass 
100                                 || ml->sml_desc == slap_schema.si_ad_structuralObjectClass )
101                 {
102                         is_oc = 1;
103                         mapped = ml->sml_desc->ad_cname;
104
105                 } else {
106                         ldap_back_map( &li->mi_targets[ candidate ]->mt_rwmap.rwm_at,
107                                         &ml->sml_desc->ad_cname, &mapped,
108                                         BACKLDAP_MAP );
109                         if ( BER_BVISNULL( &mapped ) || BER_BVISEMPTY( &mapped ) ) {
110                                 continue;
111                         }
112                 }
113
114                 modv[ i ] = &mods[ i ];
115                 mods[ i ].mod_op = ml->sml_op | LDAP_MOD_BVALUES;
116                 mods[ i ].mod_type = mapped.bv_val;
117
118                 /*
119                  * FIXME: dn-valued attrs should be rewritten
120                  * to allow their use in ACLs at the back-ldap
121                  * level.
122                  */
123                 if ( ml->sml_values != NULL ) {
124                         if ( is_oc ) {
125                                 for ( j = 0; !BER_BVISNULL( &ml->sml_values[ j ] ); j++ )
126                                         ;
127                                 mods[ i ].mod_bvalues =
128                                         (struct berval **)ch_malloc( ( j + 1 ) *
129                                         sizeof( struct berval * ) );
130                                 for ( j = 0; !BER_BVISNULL( &ml->sml_values[ j ] ); ) {
131                                         struct ldapmapping      *mapping;
132
133                                         ldap_back_mapping( &li->mi_targets[ candidate ]->mt_rwmap.rwm_oc,
134                                                         &ml->sml_values[ j ], &mapping, BACKLDAP_MAP );
135
136                                         if ( mapping == NULL ) {
137                                                 if ( li->mi_targets[ candidate ]->mt_rwmap.rwm_oc.drop_missing ) {
138                                                         continue;
139                                                 }
140                                                 mods[ i ].mod_bvalues[ j ] = &ml->sml_values[ j ];
141
142                                         } else {
143                                                 mods[ i ].mod_bvalues[ j ] = &mapping->dst;
144                                         }
145                                         j++;
146                                 }
147                                 mods[ i ].mod_bvalues[ j ] = NULL;
148
149                         } else {
150                                 if ( ml->sml_desc->ad_type->sat_syntax ==
151                                                 slap_schema.si_syn_distinguishedName )
152                                 {
153                                         ( void )ldap_dnattr_rewrite( &dc, ml->sml_values );
154                                         if ( ml->sml_values == NULL ) {
155                                                 continue;
156                                         }
157                                 }
158
159                                 for ( j = 0; !BER_BVISNULL( &ml->sml_values[ j ] ); j++ )
160                                         ;
161                                 mods[ i ].mod_bvalues =
162                                         (struct berval **)ch_malloc( ( j + 1 ) *
163                                         sizeof( struct berval * ) );
164                                 for ( j = 0; !BER_BVISNULL( &ml->sml_values[ j ] ); j++ ) {
165                                         mods[ i ].mod_bvalues[ j ] = &ml->sml_values[ j ];
166                                 }
167                                 mods[ i ].mod_bvalues[ j ] = NULL;
168                         }
169
170                 } else {
171                         mods[ i ].mod_bvalues = NULL;
172                 }
173
174                 i++;
175         }
176         modv[ i ] = 0;
177
178 retry:;
179         rs->sr_err = ldap_modify_ext_s( lc->mc_conns[ candidate ].msc_ld, mdn.bv_val,
180                         modv, op->o_ctrls, NULL );
181         if ( rs->sr_err == LDAP_UNAVAILABLE && do_retry ) {
182                 do_retry = 0;
183                 if ( meta_back_retry( op, rs, lc, candidate, LDAP_BACK_SENDERR ) ) {
184                         goto retry;
185                 }
186         }
187
188 cleanup:;
189         if ( mdn.bv_val != op->o_req_dn.bv_val ) {
190                 free( mdn.bv_val );
191                 BER_BVZERO( &mdn );
192         }
193         if ( modv != NULL ) {
194                 for ( i = 0; modv[ i ]; i++ ) {
195                         free( modv[ i ]->mod_bvalues );
196                 }
197         }
198         free( mods );
199         free( modv );
200
201         if ( rc != -1 ) {
202                 return meta_back_op_result( lc, op, rs, candidate );
203         }
204         
205         send_ldap_result( op, rs );
206
207         return rs->sr_err;
208 }
209