]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/add.c
e49ea111308f6ac8c4acaf2490c1b6ab5c1427ac
[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         Attribute *a;
42         LDAPMod **attrs;
43         struct berval mdn = BER_BVNULL, mapped;
44         dncookie dc;
45
46 #ifdef NEW_LOGGING
47         LDAP_LOG( BACK_META, ENTRY, "meta_back_add: %s\n",
48                         op->o_req_dn.bv_val, 0, 0 );
49 #else /* !NEW_LOGGING */
50         Debug(LDAP_DEBUG_ARGS, "==> meta_back_add: %s\n",
51                         op->o_req_dn.bv_val, 0, 0 );
52 #endif /* !NEW_LOGGING */
53
54         /*
55          * get the current connection
56          */
57         lc = meta_back_getconn( op, rs, META_OP_REQUIRE_SINGLE,
58                         &op->o_req_ndn, &candidate );
59         if ( !lc ) {
60                 send_ldap_result( op, rs );
61         }
62
63         if ( !meta_back_dobind( lc, op )
64                         || !meta_back_is_valid( lc, candidate ) ) {
65                 rs->sr_err = LDAP_OTHER;
66                 send_ldap_result( op, rs );
67                 return -1;
68         }
69
70         /*
71          * Rewrite the add dn, if needed
72          */
73         dc.rwmap = &li->targets[ candidate ]->rwmap;
74         dc.conn = op->o_conn;
75         dc.rs = rs;
76         dc.ctx = "addDN";
77
78         if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
79                 send_ldap_result( op, rs );
80                 return -1;
81         }
82
83         /* Count number of attributes in entry */
84         for ( i = 1, a = op->oq_add.rs_e->e_attrs; a; i++, a = a->a_next );
85         
86         /* Create array of LDAPMods for ldap_add() */
87         attrs = ch_malloc( sizeof( LDAPMod * )*i );
88
89         for ( i = 0, a = op->oq_add.rs_e->e_attrs; a; a = a->a_next ) {
90                 int     j, is_oc = 0;
91
92                 if ( a->a_desc->ad_type->sat_no_user_mod  ) {
93                         continue;
94                 }
95
96                 if ( a->a_desc == slap_schema.si_ad_objectClass
97                                 || a->a_desc == slap_schema.si_ad_structuralObjectClass )
98                 {
99                         is_oc = 1;
100                         mapped = a->a_desc->ad_cname;
101
102                 } else {
103                         ldap_back_map( &li->targets[ candidate ]->rwmap.rwm_at,
104                                         &a->a_desc->ad_cname, &mapped, BACKLDAP_MAP );
105                         if ( mapped.bv_val == NULL || mapped.bv_val[0] == '\0' ) {
106                                 continue;
107                         }
108                 }
109
110                 attrs[ i ] = ch_malloc( sizeof( LDAPMod ) );
111                 if ( attrs[ i ] == NULL ) {
112                         continue;
113                 }
114                 attrs[ i ]->mod_op = LDAP_MOD_BVALUES;
115                 attrs[ i ]->mod_type = mapped.bv_val;
116
117                 if ( is_oc ) {
118                         for ( j = 0; !BER_BVISNULL( &a->a_vals[ j ] ); j++ )
119                                 ;
120
121                         attrs[ i ]->mod_bvalues =
122                                 (struct berval **)ch_malloc( ( j + 1 ) *
123                                 sizeof( struct berval * ) );
124
125                         for ( j = 0; !BER_BVISNULL( &a->a_vals[ j ] ); ) {
126                                 struct ldapmapping      *mapping;
127
128                                 ldap_back_mapping( &li->targets[ candidate ]->rwmap.rwm_oc,
129                                                 &a->a_vals[ j ], &mapping, BACKLDAP_MAP );
130
131                                 if ( mapping == NULL ) {
132                                         if ( li->targets[ candidate ]->rwmap.rwm_oc.drop_missing ) {
133                                                 continue;
134                                         }
135                                         attrs[ i ]->mod_bvalues[ j ] = &a->a_vals[ j ];
136
137                                 } else {
138                                         attrs[ i ]->mod_bvalues[ j ] = &mapping->dst;
139                                 }
140                                 j++;
141                         }
142
143                         if ( j == 0 ) {
144                                 ch_free( attrs[ i ]->mod_bvalues );
145                                 continue;
146                         }
147
148                         attrs[ i ]->mod_bvalues[ j ] = NULL;
149
150                 } else {
151                         /*
152                          * FIXME: dn-valued attrs should be rewritten
153                          * to allow their use in ACLs at the back-ldap
154                          * level.
155                          */
156                         if ( a->a_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) {
157                                 (void)ldap_dnattr_rewrite( &dc, a->a_vals );
158                                 if ( a->a_vals == NULL ) {
159                                         continue;
160                                 }
161                         }
162
163                         for ( j = 0; a->a_vals[ j ].bv_val; j++ );
164                         attrs[ i ]->mod_bvalues = ch_malloc((j+1)*sizeof(struct berval *));
165                         for ( j = 0; a->a_vals[ j ].bv_val; j++ ) {
166                                 attrs[ i ]->mod_bvalues[ j ] = &a->a_vals[ j ];
167                         }
168                         attrs[ i ]->mod_bvalues[ j ] = NULL;
169                 }
170                 i++;
171         }
172         attrs[ i ] = NULL;
173
174         ldap_add_s( lc->conns[ candidate ].ld, mdn.bv_val, attrs );
175         for ( --i; i >= 0; --i ) {
176                 free( attrs[ i ]->mod_bvalues );
177                 free( attrs[ i ] );
178         }
179         free( attrs );
180         if ( mdn.bv_val != op->oq_add.rs_e->e_dn ) {
181                 free( mdn.bv_val );
182         }
183         return meta_back_op_result( lc, op, rs );
184 }
185