]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/add.c
0de1f0ba89d60f4761593f4bab5339312cde13b4
[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-2004 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         Debug(LDAP_DEBUG_ARGS, "==> meta_back_add: %s\n",
47                         op->o_req_dn.bv_val, 0, 0 );
48
49         /*
50          * get the current connection
51          */
52         lc = meta_back_getconn( op, rs, META_OP_REQUIRE_SINGLE,
53                         &op->o_req_ndn, &candidate );
54         if ( !lc ) {
55                 send_ldap_result( op, rs );
56         }
57
58         if ( !meta_back_dobind( lc, op )
59                         || !meta_back_is_valid( lc, candidate ) ) {
60                 rs->sr_err = LDAP_OTHER;
61                 send_ldap_result( op, rs );
62                 return -1;
63         }
64
65         /*
66          * Rewrite the add dn, if needed
67          */
68         dc.rwmap = &li->targets[ candidate ]->rwmap;
69         dc.conn = op->o_conn;
70         dc.rs = rs;
71         dc.ctx = "addDN";
72
73         if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
74                 send_ldap_result( op, rs );
75                 return -1;
76         }
77
78         /* Count number of attributes in entry */
79         for ( i = 1, a = op->oq_add.rs_e->e_attrs; a; i++, a = a->a_next );
80         
81         /* Create array of LDAPMods for ldap_add() */
82         attrs = ch_malloc( sizeof( LDAPMod * )*i );
83
84         for ( i = 0, a = op->oq_add.rs_e->e_attrs; a; a = a->a_next ) {
85                 int j;
86
87                 if ( a->a_desc->ad_type->sat_no_user_mod  ) {
88                         continue;
89                 }
90
91                 ldap_back_map( &li->targets[ candidate ]->rwmap.rwm_at,
92                                 &a->a_desc->ad_cname, &mapped, BACKLDAP_MAP );
93                 if ( mapped.bv_val == NULL || mapped.bv_val[0] == '\0' ) {
94                         continue;
95                 }
96
97                 attrs[ i ] = ch_malloc( sizeof( LDAPMod ) );
98                 if ( attrs[ i ] == NULL ) {
99                         continue;
100                 }
101                 attrs[ i ]->mod_op = LDAP_MOD_BVALUES;
102                 attrs[ i ]->mod_type = mapped.bv_val;
103
104                 /*
105                  * FIXME: dn-valued attrs should be rewritten
106                  * to allow their use in ACLs at the back-ldap
107                  * level.
108                  */
109                 if ( strcmp( a->a_desc->ad_type->sat_syntax->ssyn_oid,
110                                         SLAPD_DN_SYNTAX ) == 0 ) {
111                         (void)ldap_dnattr_rewrite( &dc, a->a_vals );
112                 }
113
114                 for ( j = 0; a->a_vals[ j ].bv_val; j++ );
115                 attrs[ i ]->mod_vals.modv_bvals = ch_malloc((j+1)*sizeof(struct berval *));
116                 for ( j = 0; a->a_vals[ j ].bv_val; j++ ) {
117                         attrs[ i ]->mod_vals.modv_bvals[ j ] = &a->a_vals[ j ];
118                 }
119                 attrs[ i ]->mod_vals.modv_bvals[ j ] = NULL;
120                 i++;
121         }
122         attrs[ i ] = NULL;
123
124         ldap_add_s( lc->conns[ candidate ].ld, mdn.bv_val, attrs );
125         for ( --i; i >= 0; --i ) {
126                 free( attrs[ i ]->mod_vals.modv_bvals );
127                 free( attrs[ i ] );
128         }
129         free( attrs );
130         if ( mdn.bv_val != op->oq_add.rs_e->e_dn ) {
131                 free( mdn.bv_val );
132         }
133         return meta_back_op_result( lc, op, rs );
134 }
135