]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/modify.c
1eed28ccc08f6e08f989a5b592bc830abc6d6bb2
[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, META_OP_REQUIRE_SINGLE,
50                         &op->o_req_ndn, &candidate );
51         if ( !lc ) {
52                 rc = -1;
53                 goto cleanup;
54         }
55         
56         if ( !meta_back_dobind( lc, op ) ) {
57                 rs->sr_err = LDAP_UNAVAILABLE;
58
59         } else if ( !meta_back_is_valid( lc, candidate ) ) {
60                 rs->sr_err = LDAP_OTHER;
61         }
62
63         if ( rs->sr_err != LDAP_SUCCESS ) {
64                 rc = -1;
65                 goto cleanup;
66         }
67
68         /*
69          * Rewrite the modify dn, if needed
70          */
71         dc.rwmap = &li->targets[ candidate ]->mt_rwmap;
72         dc.conn = op->o_conn;
73         dc.rs = rs;
74         dc.ctx = "modifyDN";
75
76         if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
77                 rc = -1;
78                 goto cleanup;
79         }
80
81         for ( i = 0, ml = op->orm_modlist; ml; i++ ,ml = ml->sml_next )
82                 ;
83
84         mods = ch_malloc( sizeof( LDAPMod )*i );
85         if ( mods == NULL ) {
86                 rs->sr_err = LDAP_NO_MEMORY;
87                 rc = -1;
88                 goto cleanup;
89         }
90         modv = ( LDAPMod ** )ch_malloc( ( i + 1 )*sizeof( LDAPMod * ) );
91         if ( modv == NULL ) {
92                 rs->sr_err = LDAP_NO_MEMORY;
93                 rc = -1;
94                 goto cleanup;
95         }
96
97         dc.ctx = "modifyAttrDN";
98         isupdate = be_shadow_update( op );
99         for ( i = 0, ml = op->orm_modlist; ml; ml = ml->sml_next ) {
100                 int j;
101
102                 if ( !isupdate && ml->sml_desc->ad_type->sat_no_user_mod  ) {
103                         continue;
104                 }
105
106                 ldap_back_map( &li->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                 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                         /* mod_op must be delete all values */
124                         if ( strcmp( ml->sml_desc->ad_type->sat_syntax->ssyn_oid,
125                                                 SLAPD_DN_SYNTAX ) == 0 )
126                         {
127                                 ( void )ldap_dnattr_rewrite( &dc, ml->sml_values );
128                         }
129
130                         if ( ml->sml_values != NULL ) {
131                                 for ( j = 0; !BER_BVISNULL( &ml->sml_values[ j ] ); j++ )
132                                         ;
133                                 mods[ i ].mod_bvalues =
134                                         (struct berval **)ch_malloc( ( j + 1 ) *
135                                         sizeof(struct berval *) );
136                                 for ( j = 0; !BER_BVISNULL( &ml->sml_values[ j ] ); j++ ) {
137                                         mods[ i ].mod_bvalues[ j ] = &ml->sml_values[ j ];
138                                 }
139                                 mods[ i ].mod_bvalues[ j ] = NULL;
140                         }
141
142                 } else {
143                         mods[ i ].mod_bvalues = NULL;
144                 }
145
146                 i++;
147         }
148         modv[ i ] = 0;
149
150         rc = ldap_modify_ext_s( lc->mc_conns[ candidate ].msc_ld, mdn.bv_val,
151                         modv, NULL, NULL ) != LDAP_SUCCESS;
152
153 cleanup:;
154         if ( mdn.bv_val != op->o_req_dn.bv_val ) {
155                 free( mdn.bv_val );
156                 BER_BVZERO( &mdn );
157         }
158         if ( modv != NULL ) {
159                 for ( i = 0; modv[ i ]; i++ ) {
160                         free( modv[ i ]->mod_bvalues );
161                 }
162         }
163         free( mods );
164         free( modv );
165         
166         if ( rc == 0 ) {
167                 return meta_back_op_result( lc, op, rs ) == LDAP_SUCCESS
168                         ? 0 : 1;
169         } /* else */
170
171         send_ldap_result( op, rs );
172
173         return rc;
174 }
175