]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/add.c
Sync with HEAD
[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-2005 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         struct ldapconn *lc;
40         int             i = 0,
41                         j = 0;
42         Attribute       *a;
43         LDAPMod         **attrs = NULL,
44                         *attrs2 = NULL;
45         ber_int_t       msgid;
46         int             isupdate;
47         int             do_retry = 1;
48         LDAPControl     **ctrls = NULL;
49         int             rc = LDAP_SUCCESS;
50
51         Debug( LDAP_DEBUG_ARGS, "==> ldap_back_add(\"%s\")\n",
52                         op->o_req_dn.bv_val, 0, 0 );
53
54         lc = ldap_back_getconn( op, rs );
55         if ( !lc || !ldap_back_dobind( lc, op, rs ) ) {
56                 rc = -1;
57                 goto cleanup;
58         }
59
60         /* Count number of attributes in entry */
61         for ( i = 1, a = op->oq_add.rs_e->e_attrs; a; i++, a = a->a_next )
62                 /* just count attrs */ ;
63         
64         /* Create array of LDAPMods for ldap_add() */
65         attrs = (LDAPMod **)ch_malloc( sizeof( LDAPMod * )*i 
66                         + sizeof( LDAPMod )*( i - 1 ) );
67         attrs2 = ( LDAPMod * )&attrs[ i ];
68
69         isupdate = be_shadow_update( op );
70         for ( i = 0, a = op->oq_add.rs_e->e_attrs; a; a = a->a_next ) {
71                 if ( !isupdate && a->a_desc->ad_type->sat_no_user_mod  ) {
72                         continue;
73                 }
74
75                 attrs[ i ] = &attrs2[ i ];
76                 attrs[ i ]->mod_op = LDAP_MOD_BVALUES;
77                 attrs[ i ]->mod_type = a->a_desc->ad_cname.bv_val;
78
79                 for ( j = 0; a->a_vals[ j ].bv_val; j++ )
80                         /* just count vals */ ;
81                 attrs[i]->mod_vals.modv_bvals = 
82                         ch_malloc( ( j + 1 )*sizeof( struct berval * ) );
83                 for ( j = 0; a->a_vals[ j ].bv_val; j++ ) {
84                         attrs[ i ]->mod_vals.modv_bvals[ j ] = &a->a_vals[ j ];
85                 }
86                 attrs[ i ]->mod_vals.modv_bvals[ j ] = NULL;
87                 i++;
88         }
89         attrs[ i ] = NULL;
90
91         ctrls = op->o_ctrls;
92         rc = ldap_back_proxy_authz_ctrl( lc, op, rs, &ctrls );
93         if ( rc != LDAP_SUCCESS ) {
94                 send_ldap_result( op, rs );
95                 rc = -1;
96                 goto cleanup;
97         }
98
99 retry:
100         rs->sr_err = ldap_add_ext( lc->lc_ld, op->o_req_dn.bv_val, attrs,
101                         ctrls, NULL, &msgid );
102         rc = ldap_back_op_result( lc, op, rs, msgid, 1 );
103         if ( rs->sr_err == LDAP_UNAVAILABLE && do_retry ) {
104                 do_retry = 0;
105                 if ( ldap_back_retry( lc, op, rs ) ) {
106                         goto retry;
107                 }
108         }
109
110 cleanup:
111         (void)ldap_back_proxy_authz_ctrl_free( op, &ctrls );
112
113         if ( attrs ) {
114                 for ( --i; i >= 0; --i ) {
115                         ch_free( attrs[ i ]->mod_vals.modv_bvals );
116                 }
117                 ch_free( attrs );
118         }
119
120         Debug( LDAP_DEBUG_ARGS, "<== ldap_back_add(\"%s\"): %d\n",
121                         op->o_req_dn.bv_val, rc, 0 );
122
123         return rc;
124 }
125