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