]> 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         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, META_OP_REQUIRE_SINGLE,
54                         &op->o_req_ndn, &candidate );
55         if ( !lc ) {
56                 send_ldap_result( op, rs );
57                 return rs->sr_err;
58         }
59
60         if ( !meta_back_dobind( lc, op )
61                         || !meta_back_is_valid( lc, candidate ) ) {
62                 rs->sr_err = LDAP_UNAVAILABLE;
63                 send_ldap_result( op, rs );
64                 return rs->sr_err;
65         }
66
67         /*
68          * Rewrite the add 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 = "addDN";
74
75         if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
76                 send_ldap_result( op, rs );
77                 return rs->sr_err;
78         }
79
80         /* Count number of attributes in entry */
81         for ( i = 1, a = op->ora_e->e_attrs; a; i++, a = a->a_next );
82         
83         /* Create array of LDAPMods for ldap_add() */
84         attrs = ch_malloc( sizeof( LDAPMod * )*i );
85
86         dc.ctx = "addAttrDN";
87         isupdate = be_shadow_update( op );
88         for ( i = 0, a = op->ora_e->e_attrs; a; a = a->a_next ) {
89                 int                     j, is_oc = 0;
90
91                 if ( !isupdate && a->a_desc->ad_type->sat_no_user_mod  ) {
92                         continue;
93                 }
94
95                 if ( a->a_desc == slap_schema.si_ad_objectClass 
96                                 || a->a_desc == slap_schema.si_ad_structuralObjectClass )
97                 {
98                         is_oc = 1;
99                         mapped = a->a_desc->ad_cname;
100
101                 } else {
102                         ldap_back_map( &li->targets[ candidate ]->mt_rwmap.rwm_at,
103                                         &a->a_desc->ad_cname, &mapped, BACKLDAP_MAP );
104                         if ( BER_BVISNULL( &mapped ) || BER_BVISEMPTY( &mapped ) ) {
105                                 continue;
106                         }
107                 }
108
109                 attrs[ i ] = ch_malloc( sizeof( LDAPMod ) );
110                 if ( attrs[ i ] == NULL ) {
111                         continue;
112                 }
113                 attrs[ i ]->mod_op = LDAP_MOD_BVALUES;
114                 attrs[ i ]->mod_type = mapped.bv_val;
115
116                 if ( is_oc ) {
117                         for ( j = 0; !BER_BVISNULL( &a->a_vals[ j ] ); j++ )
118                                 ;
119
120                         attrs[ i ]->mod_bvalues =
121                                 (struct berval **)ch_malloc( ( j + 1 ) *
122                                 sizeof( struct berval * ) );
123
124                         for ( j = 0; !BER_BVISNULL( &a->a_vals[ j ] ); ) {
125                                 struct ldapmapping      *mapping;
126
127                                 ldap_back_mapping( &li->targets[ candidate ]->mt_rwmap.rwm_oc,
128                                                 &a->a_vals[ j ], &mapping, BACKLDAP_MAP );
129
130                                 if ( mapping == NULL ) {
131                                         if ( li->targets[ candidate ]->mt_rwmap.rwm_oc.drop_missing ) {
132                                                 continue;
133                                         }
134                                         attrs[ i ]->mod_bvalues[ j ] = &a->a_vals[ j ];
135
136                                 } else {
137                                         attrs[ i ]->mod_bvalues[ j ] = &mapping->dst;
138                                 }
139                                 j++;
140                         }
141                         attrs[ i ]->mod_bvalues[ j ] = NULL;
142
143                 } else {
144                         /*
145                          * FIXME: dn-valued attrs should be rewritten
146                          * to allow their use in ACLs at the back-ldap
147                          * level.
148                          */
149                         if ( a->a_desc->ad_type->sat_syntax ==
150                                 slap_schema.si_syn_distinguishedName )
151                         {
152                                 (void)ldap_dnattr_rewrite( &dc, a->a_vals );
153                                 if ( a->a_vals == NULL ) {
154                                         continue;
155                                 }
156                         }
157
158                         for ( j = 0; !BER_BVISNULL( &a->a_vals[ j ] ); j++ )
159                                 ;
160                         
161                         attrs[ i ]->mod_bvalues = ch_malloc( ( j + 1 ) * sizeof( struct berval * ) );
162                         for ( j = 0; !BER_BVISNULL( &a->a_vals[ j ] ); j++ ) {
163                                 attrs[ i ]->mod_bvalues[ j ] = &a->a_vals[ j ];
164                         }
165                         attrs[ i ]->mod_bvalues[ j ] = NULL;
166                 }
167                 i++;
168         }
169         attrs[ i ] = NULL;
170
171         rs->sr_err = ldap_add_ext_s( lc->mc_conns[ candidate ].msc_ld, mdn.bv_val,
172                               attrs, op->o_ctrls, NULL );
173         for ( --i; i >= 0; --i ) {
174                 free( attrs[ i ]->mod_bvalues );
175                 free( attrs[ i ] );
176         }
177         free( attrs );
178         if ( mdn.bv_val != op->ora_e->e_dn ) {
179                 free( mdn.bv_val );
180                 BER_BVZERO( &mdn );
181         }
182
183         return meta_back_op_result( lc, op, rs );
184 }
185