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