]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/modify.c
fix ITS#3464
[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         struct berval           mdn = BER_BVNULL;
45         struct berval           mapped;
46         dncookie                dc;
47
48         lc = meta_back_getconn( op, rs, META_OP_REQUIRE_SINGLE,
49                         &op->o_req_ndn, &candidate );
50         if ( !lc ) {
51                 rc = -1;
52                 goto cleanup;
53         }
54         
55         if ( !meta_back_dobind( lc, op ) ) {
56                 rs->sr_err = LDAP_UNAVAILABLE;
57
58         } else if ( !meta_back_is_valid( lc, candidate ) ) {
59                 rs->sr_err = LDAP_OTHER;
60         }
61
62         if ( rs->sr_err != LDAP_SUCCESS ) {
63                 rc = -1;
64                 goto cleanup;
65         }
66
67         /*
68          * Rewrite the modify dn, if needed
69          */
70         dc.rwmap = &li->targets[ candidate ]->mt_rwmap;
71         dc.conn = op->o_conn;
72         dc.rs = rs;
73         dc.ctx = "modifyDN";
74
75         if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
76                 rc = -1;
77                 goto cleanup;
78         }
79
80         for ( i = 0, ml = op->orm_modlist; ml; i++ ,ml = ml->sml_next )
81                 ;
82
83         mods = ch_malloc( sizeof( LDAPMod )*i );
84         if ( mods == NULL ) {
85                 rs->sr_err = LDAP_NO_MEMORY;
86                 rc = -1;
87                 goto cleanup;
88         }
89         modv = ( LDAPMod ** )ch_malloc( ( i + 1 )*sizeof( LDAPMod * ) );
90         if ( modv == NULL ) {
91                 rs->sr_err = LDAP_NO_MEMORY;
92                 rc = -1;
93                 goto cleanup;
94         }
95
96         dc.ctx = "modifyAttrDN";
97         for ( i = 0, ml = op->orm_modlist; ml; ml = ml->sml_next ) {
98                 int j;
99
100                 if ( ml->sml_desc->ad_type->sat_no_user_mod  ) {
101                         continue;
102                 }
103
104                 ldap_back_map( &li->targets[ candidate ]->mt_rwmap.rwm_at,
105                                 &ml->sml_desc->ad_cname, &mapped,
106                                 BACKLDAP_MAP );
107                 if ( BER_BVISNULL( &mapped ) || mapped.bv_val[0] == '\0' ) {
108                         continue;
109                 }
110
111                 modv[ i ] = &mods[ i ];
112                 mods[ i ].mod_op = ml->sml_op | LDAP_MOD_BVALUES;
113                 mods[ i ].mod_type = mapped.bv_val;
114
115                 /*
116                  * FIXME: dn-valued attrs should be rewritten
117                  * to allow their use in ACLs at the back-ldap
118                  * level.
119                  */
120                 if ( strcmp( ml->sml_desc->ad_type->sat_syntax->ssyn_oid,
121                                         SLAPD_DN_SYNTAX ) == 0 )
122                 {
123                         ( void )ldap_dnattr_rewrite( &dc, ml->sml_values );
124                 }
125
126                 if ( ml->sml_values != NULL ) {
127                         for ( j = 0; ml->sml_values[ j ].bv_val; j++ )
128                                 ;
129                         mods[ i ].mod_bvalues =
130                                 (struct berval **)ch_malloc( ( j + 1 ) *
131                                 sizeof(struct berval *) );
132                         for ( j = 0; ml->sml_values[ j ].bv_val; j++ ) {
133                                 mods[ i ].mod_bvalues[ j ] = &ml->sml_values[ j ];
134                         }
135                         mods[ i ].mod_bvalues[ j ] = NULL;
136
137                 } else {
138                         mods[ i ].mod_bvalues = NULL;
139                 }
140
141                 i++;
142         }
143         modv[ i ] = 0;
144
145         rc = ldap_modify_ext_s( lc->mc_conns[ candidate ].msc_ld, mdn.bv_val,
146                         modv, NULL, NULL ) != LDAP_SUCCESS;
147
148 cleanup:;
149         if ( mdn.bv_val != op->o_req_dn.bv_val ) {
150                 free( mdn.bv_val );
151                 BER_BVZERO( &mdn );
152         }
153         if ( modv != NULL ) {
154                 for ( i = 0; modv[ i ]; i++ ) {
155                         free( modv[ i ]->mod_bvalues );
156                 }
157         }
158         free( mods );
159         free( modv );
160         
161         if ( rc == 0 ) {
162                 return meta_back_op_result( lc, op, rs ) == LDAP_SUCCESS
163                         ? 0 : 1;
164         } /* else */
165
166         send_ldap_result( op, rs );
167
168         return rc;
169 }
170