]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/add.c
a38ed4ce7cadf0ccf67c802738dac648108800d7
[openldap] / servers / slapd / back-ldap / add.c
1 /* add.c - ldap backend add function */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1999-2015 The OpenLDAP Foundation.
6  * Portions Copyright 2000-2003 Pierangelo Masarati.
7  * Portions Copyright 1999-2003 Howard Chu.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted only as authorized by the OpenLDAP
12  * Public License.
13  *
14  * A copy of this license is available in the file LICENSE in the
15  * top-level directory of the distribution or, alternatively, at
16  * <http://www.OpenLDAP.org/license.html>.
17  */
18 /* ACKNOWLEDGEMENTS:
19  * This work was initially developed by the Howard Chu for inclusion
20  * in OpenLDAP Software and subsequently enhanced by Pierangelo
21  * Masarati.
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.h"
33
34 int
35 ldap_back_add(
36         Operation       *op,
37         SlapReply       *rs )
38 {
39         ldapinfo_t              *li = (ldapinfo_t *)op->o_bd->be_private;
40
41         ldapconn_t              *lc = NULL;
42         int                     i = 0,
43                                 j = 0;
44         Attribute               *a;
45         LDAPMod                 **attrs = NULL,
46                                 *attrs2 = NULL;
47         ber_int_t               msgid;
48         int                     isupdate;
49         ldap_back_send_t        retrying = LDAP_BACK_RETRYING;
50         LDAPControl             **ctrls = NULL;
51
52         rs->sr_err = LDAP_SUCCESS;
53         
54         Debug( LDAP_DEBUG_ARGS, "==> ldap_back_add(\"%s\")\n",
55                         op->o_req_dn.bv_val, 0, 0 );
56
57         if ( !ldap_back_dobind( &lc, op, rs, LDAP_BACK_SENDERR ) ) {
58                 lc = NULL;
59                 goto cleanup;
60         }
61
62         /* Count number of attributes in entry */
63         for ( i = 1, a = op->oq_add.rs_e->e_attrs; a; i++, a = a->a_next )
64                 /* just count attrs */ ;
65         
66         /* Create array of LDAPMods for ldap_add() */
67         attrs = (LDAPMod **)ch_malloc( sizeof( LDAPMod * )*i 
68                         + sizeof( LDAPMod )*( i - 1 ) );
69         attrs2 = ( LDAPMod * )&attrs[ i ];
70
71         isupdate = be_shadow_update( op );
72         for ( i = 0, a = op->oq_add.rs_e->e_attrs; a; a = a->a_next ) {
73                 if ( !isupdate && !get_relax( op ) && a->a_desc->ad_type->sat_no_user_mod  )
74                 {
75                         continue;
76                 }
77
78                 attrs[ i ] = &attrs2[ i ];
79                 attrs[ i ]->mod_op = LDAP_MOD_BVALUES;
80                 attrs[ i ]->mod_type = a->a_desc->ad_cname.bv_val;
81
82                 for ( j = 0; a->a_vals[ j ].bv_val; j++ )
83                         /* just count vals */ ;
84                 attrs[i]->mod_vals.modv_bvals = 
85                         ch_malloc( ( j + 1 )*sizeof( struct berval * ) );
86                 for ( j = 0; a->a_vals[ j ].bv_val; j++ ) {
87                         attrs[ i ]->mod_vals.modv_bvals[ j ] = &a->a_vals[ j ];
88                 }
89                 attrs[ i ]->mod_vals.modv_bvals[ j ] = NULL;
90                 i++;
91         }
92         attrs[ i ] = NULL;
93
94 retry:
95         ctrls = op->o_ctrls;
96         rs->sr_err = ldap_back_controls_add( op, rs, lc, &ctrls );
97         if ( rs->sr_err != LDAP_SUCCESS ) {
98                 send_ldap_result( op, rs );
99                 goto cleanup;
100         }
101
102         rs->sr_err = ldap_add_ext( lc->lc_ld, op->o_req_dn.bv_val, attrs,
103                         ctrls, NULL, &msgid );
104         rs->sr_err = ldap_back_op_result( lc, op, rs, msgid,
105                 li->li_timeout[ SLAP_OP_ADD ],
106                 ( LDAP_BACK_SENDRESULT | retrying ) );
107         if ( rs->sr_err == LDAP_UNAVAILABLE && retrying ) {
108                 retrying &= ~LDAP_BACK_RETRYING;
109                 if ( ldap_back_retry( &lc, op, rs, LDAP_BACK_SENDERR ) ) {
110                         /* if the identity changed, there might be need to re-authz */
111                         (void)ldap_back_controls_free( op, rs, &ctrls );
112                         goto retry;
113                 }
114         }
115
116         ldap_pvt_thread_mutex_lock( &li->li_counter_mutex );
117         ldap_pvt_mp_add( li->li_ops_completed[ SLAP_OP_ADD ], 1 );
118         ldap_pvt_thread_mutex_unlock( &li->li_counter_mutex );
119
120 cleanup:
121         (void)ldap_back_controls_free( op, rs, &ctrls );
122
123         if ( attrs ) {
124                 for ( --i; i >= 0; --i ) {
125                         ch_free( attrs[ i ]->mod_vals.modv_bvals );
126                 }
127                 ch_free( attrs );
128         }
129
130         if ( lc ) {
131                 ldap_back_release_conn( li, lc );
132         }
133
134         Debug( LDAP_DEBUG_ARGS, "<== ldap_back_add(\"%s\"): %d\n",
135                         op->o_req_dn.bv_val, rs->sr_err, 0 );
136
137         return rs->sr_err;
138 }
139