]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/add.c
Sync with HEAD
[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         metainfo_t      *mi = ( metainfo_t * )op->o_bd->be_private;
39         metaconn_t      *mc;
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         int             do_retry = 1;
47
48         Debug(LDAP_DEBUG_ARGS, "==> meta_back_add: %s\n",
49                         op->o_req_dn.bv_val, 0, 0 );
50
51         /*
52          * get the current connection
53          */
54         mc = meta_back_getconn( op, rs, &candidate, LDAP_BACK_SENDERR );
55         if ( !mc || !meta_back_dobind( op, rs, mc, LDAP_BACK_SENDERR ) ) {
56                 return rs->sr_err;
57         }
58
59         assert( mc->mc_conns[ candidate ].msc_ld != NULL );
60
61         /*
62          * Rewrite the add dn, if needed
63          */
64         dc.target = &mi->mi_targets[ candidate ];
65         dc.conn = op->o_conn;
66         dc.rs = rs;
67         dc.ctx = "addDN";
68
69         if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
70                 send_ldap_result( op, rs );
71                 goto done;
72         }
73
74         /* Count number of attributes in entry ( +1 ) */
75         for ( i = 1, a = op->ora_e->e_attrs; a; i++, a = a->a_next );
76         
77         /* Create array of LDAPMods for ldap_add() */
78         attrs = ch_malloc( sizeof( LDAPMod * )*i );
79
80         dc.ctx = "addAttrDN";
81         isupdate = be_shadow_update( op );
82         for ( i = 0, a = op->ora_e->e_attrs; a; a = a->a_next ) {
83                 int                     j, is_oc = 0;
84
85                 if ( !isupdate && a->a_desc->ad_type->sat_no_user_mod  ) {
86                         continue;
87                 }
88
89                 if ( a->a_desc == slap_schema.si_ad_objectClass 
90                                 || a->a_desc == slap_schema.si_ad_structuralObjectClass )
91                 {
92                         is_oc = 1;
93                         mapped = a->a_desc->ad_cname;
94
95                 } else {
96                         ldap_back_map( &mi->mi_targets[ candidate ].mt_rwmap.rwm_at,
97                                         &a->a_desc->ad_cname, &mapped, BACKLDAP_MAP );
98                         if ( BER_BVISNULL( &mapped ) || BER_BVISEMPTY( &mapped ) ) {
99                                 continue;
100                         }
101                 }
102
103                 attrs[ i ] = ch_malloc( sizeof( LDAPMod ) );
104                 if ( attrs[ i ] == NULL ) {
105                         continue;
106                 }
107                 attrs[ i ]->mod_op = LDAP_MOD_BVALUES;
108                 attrs[ i ]->mod_type = mapped.bv_val;
109
110                 if ( is_oc ) {
111                         for ( j = 0; !BER_BVISNULL( &a->a_vals[ j ] ); j++ )
112                                 ;
113
114                         attrs[ i ]->mod_bvalues =
115                                 (struct berval **)ch_malloc( ( j + 1 ) *
116                                 sizeof( struct berval * ) );
117
118                         for ( j = 0; !BER_BVISNULL( &a->a_vals[ j ] ); ) {
119                                 struct ldapmapping      *mapping;
120
121                                 ldap_back_mapping( &mi->mi_targets[ candidate ].mt_rwmap.rwm_oc,
122                                                 &a->a_vals[ j ], &mapping, BACKLDAP_MAP );
123
124                                 if ( mapping == NULL ) {
125                                         if ( mi->mi_targets[ candidate ].mt_rwmap.rwm_oc.drop_missing ) {
126                                                 continue;
127                                         }
128                                         attrs[ i ]->mod_bvalues[ j ] = &a->a_vals[ j ];
129
130                                 } else {
131                                         attrs[ i ]->mod_bvalues[ j ] = &mapping->dst;
132                                 }
133                                 j++;
134                         }
135                         attrs[ i ]->mod_bvalues[ j ] = NULL;
136
137                 } else {
138                         /*
139                          * FIXME: dn-valued attrs should be rewritten
140                          * to allow their use in ACLs at the back-ldap
141                          * level.
142                          */
143                         if ( a->a_desc->ad_type->sat_syntax ==
144                                 slap_schema.si_syn_distinguishedName )
145                         {
146                                 (void)ldap_dnattr_rewrite( &dc, a->a_vals );
147                                 if ( a->a_vals == NULL ) {
148                                         continue;
149                                 }
150                         }
151
152                         for ( j = 0; !BER_BVISNULL( &a->a_vals[ j ] ); j++ )
153                                 ;
154                         
155                         attrs[ i ]->mod_bvalues = ch_malloc( ( j + 1 ) * sizeof( struct berval * ) );
156                         for ( j = 0; !BER_BVISNULL( &a->a_vals[ j ] ); j++ ) {
157                                 attrs[ i ]->mod_bvalues[ j ] = &a->a_vals[ j ];
158                         }
159                         attrs[ i ]->mod_bvalues[ j ] = NULL;
160                 }
161                 i++;
162         }
163         attrs[ i ] = NULL;
164
165 retry:;
166 #if 0
167         rs->sr_err = ldap_add_ext( mc->mc_conns[ candidate ].msc_ld, mdn.bv_val,
168                               attrs, op->o_ctrls, NULL, &msgid );
169         rs->sr_err = meta_back_op_result( op, rs, &mc->mc_conns[ candidate ], msgid, LDAP_BACK_SENDERR );
170 #endif
171         rs->sr_err = ldap_add_ext_s( mc->mc_conns[ candidate ].msc_ld, mdn.bv_val,
172                               attrs, op->o_ctrls, NULL );
173         if ( rs->sr_err == LDAP_UNAVAILABLE && do_retry ) {
174                 do_retry = 0;
175                 if ( meta_back_retry( op, rs, mc, candidate, LDAP_BACK_SENDERR ) ) {
176                         goto retry;
177                 }
178         }
179
180         for ( --i; i >= 0; --i ) {
181                 free( attrs[ i ]->mod_bvalues );
182                 free( attrs[ i ] );
183         }
184         free( attrs );
185         if ( mdn.bv_val != op->ora_e->e_dn ) {
186                 free( mdn.bv_val );
187                 BER_BVZERO( &mdn );
188         }
189
190         (void)meta_back_op_result( mc, op, rs, candidate );
191
192 done:;
193         meta_back_release_conn( op, mc );
194
195         return rs->sr_err;
196 }
197