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