]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/add.c
a) implement a new candidate selection procedure, based on target naming
[openldap] / servers / slapd / back-meta / add.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
24 #include "portable.h"
25
26 #include <stdio.h>
27
28 #include <ac/string.h>
29 #include <ac/socket.h>
30
31 #include "slap.h"
32 #include "../back-ldap/back-ldap.h"
33 #include "back-meta.h"
34
35 int
36 meta_back_add( Operation *op, SlapReply *rs )
37 {
38         struct metainfo *li = ( struct metainfo * )op->o_bd->be_private;
39         struct metaconn *lc;
40         int             i, candidate = -1;
41         int             isupdate;
42         Attribute       *a;
43         LDAPMod         **attrs;
44         struct berval   mdn = BER_BVNULL, mapped;
45         dncookie        dc;
46
47         Debug(LDAP_DEBUG_ARGS, "==> meta_back_add: %s\n",
48                         op->o_req_dn.bv_val, 0, 0 );
49
50         /*
51          * get the current connection
52          */
53         lc = meta_back_getconn( op, rs, &candidate, LDAP_BACK_SENDERR );
54         if ( !lc || !meta_back_dobind( lc, op, LDAP_BACK_SENDERR ) ) {
55                 return rs->sr_err;
56         }
57
58         if ( !meta_back_is_valid( lc, candidate ) ) {
59                 rs->sr_err = LDAP_UNAVAILABLE;
60                 send_ldap_result( op, rs );
61                 return rs->sr_err;
62         }
63
64         /*
65          * Rewrite the add dn, if needed
66          */
67         dc.rwmap = &li->mi_targets[ candidate ]->mt_rwmap;
68         dc.conn = op->o_conn;
69         dc.rs = rs;
70         dc.ctx = "addDN";
71
72         if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
73                 send_ldap_result( op, rs );
74                 return rs->sr_err;
75         }
76
77         /* Count number of attributes in entry ( +1 ) */
78         for ( i = 1, a = op->ora_e->e_attrs; a; i++, a = a->a_next );
79         
80         /* Create array of LDAPMods for ldap_add() */
81         attrs = ch_malloc( sizeof( LDAPMod * )*i );
82
83         dc.ctx = "addAttrDN";
84         isupdate = be_shadow_update( op );
85         for ( i = 0, a = op->ora_e->e_attrs; a; a = a->a_next ) {
86                 int                     j, is_oc = 0;
87
88                 if ( !isupdate && a->a_desc->ad_type->sat_no_user_mod  ) {
89                         continue;
90                 }
91
92                 if ( a->a_desc == slap_schema.si_ad_objectClass 
93                                 || a->a_desc == slap_schema.si_ad_structuralObjectClass )
94                 {
95                         is_oc = 1;
96                         mapped = a->a_desc->ad_cname;
97
98                 } else {
99                         ldap_back_map( &li->mi_targets[ candidate ]->mt_rwmap.rwm_at,
100                                         &a->a_desc->ad_cname, &mapped, BACKLDAP_MAP );
101                         if ( BER_BVISNULL( &mapped ) || BER_BVISEMPTY( &mapped ) ) {
102                                 continue;
103                         }
104                 }
105
106                 attrs[ i ] = ch_malloc( sizeof( LDAPMod ) );
107                 if ( attrs[ i ] == NULL ) {
108                         continue;
109                 }
110                 attrs[ i ]->mod_op = LDAP_MOD_BVALUES;
111                 attrs[ i ]->mod_type = mapped.bv_val;
112
113                 if ( is_oc ) {
114                         for ( j = 0; !BER_BVISNULL( &a->a_vals[ j ] ); j++ )
115                                 ;
116
117                         attrs[ i ]->mod_bvalues =
118                                 (struct berval **)ch_malloc( ( j + 1 ) *
119                                 sizeof( struct berval * ) );
120
121                         for ( j = 0; !BER_BVISNULL( &a->a_vals[ j ] ); ) {
122                                 struct ldapmapping      *mapping;
123
124                                 ldap_back_mapping( &li->mi_targets[ candidate ]->mt_rwmap.rwm_oc,
125                                                 &a->a_vals[ j ], &mapping, BACKLDAP_MAP );
126
127                                 if ( mapping == NULL ) {
128                                         if ( li->mi_targets[ candidate ]->mt_rwmap.rwm_oc.drop_missing ) {
129                                                 continue;
130                                         }
131                                         attrs[ i ]->mod_bvalues[ j ] = &a->a_vals[ j ];
132
133                                 } else {
134                                         attrs[ i ]->mod_bvalues[ j ] = &mapping->dst;
135                                 }
136                                 j++;
137                         }
138                         attrs[ i ]->mod_bvalues[ j ] = NULL;
139
140                 } else {
141                         /*
142                          * FIXME: dn-valued attrs should be rewritten
143                          * to allow their use in ACLs at the back-ldap
144                          * level.
145                          */
146                         if ( a->a_desc->ad_type->sat_syntax ==
147                                 slap_schema.si_syn_distinguishedName )
148                         {
149                                 (void)ldap_dnattr_rewrite( &dc, a->a_vals );
150                                 if ( a->a_vals == NULL ) {
151                                         continue;
152                                 }
153                         }
154
155                         for ( j = 0; !BER_BVISNULL( &a->a_vals[ j ] ); j++ )
156                                 ;
157                         
158                         attrs[ i ]->mod_bvalues = ch_malloc( ( j + 1 ) * sizeof( struct berval * ) );
159                         for ( j = 0; !BER_BVISNULL( &a->a_vals[ j ] ); j++ ) {
160                                 attrs[ i ]->mod_bvalues[ j ] = &a->a_vals[ j ];
161                         }
162                         attrs[ i ]->mod_bvalues[ j ] = NULL;
163                 }
164                 i++;
165         }
166         attrs[ i ] = NULL;
167
168         rs->sr_err = ldap_add_ext_s( lc->mc_conns[ candidate ].msc_ld, mdn.bv_val,
169                               attrs, op->o_ctrls, NULL );
170         for ( --i; i >= 0; --i ) {
171                 free( attrs[ i ]->mod_bvalues );
172                 free( attrs[ i ] );
173         }
174         free( attrs );
175         if ( mdn.bv_val != op->ora_e->e_dn ) {
176                 free( mdn.bv_val );
177                 BER_BVZERO( &mdn );
178         }
179
180         return meta_back_op_result( lc, op, rs, candidate );
181 }
182