1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  *
3*0Sstevel@tonic-gate  * Portions Copyright %G% Sun Microsystems, Inc.
4*0Sstevel@tonic-gate  * All Rights Reserved
5*0Sstevel@tonic-gate  *
6*0Sstevel@tonic-gate  */
7*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
8*0Sstevel@tonic-gate /*
9*0Sstevel@tonic-gate  *  Copyright (c) 1990 Regents of the University of Michigan.
10*0Sstevel@tonic-gate  *  All rights reserved.
11*0Sstevel@tonic-gate  *
12*0Sstevel@tonic-gate  *  add.c
13*0Sstevel@tonic-gate  */
14*0Sstevel@tonic-gate 
15*0Sstevel@tonic-gate #ifndef lint
16*0Sstevel@tonic-gate static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
17*0Sstevel@tonic-gate #endif
18*0Sstevel@tonic-gate 
19*0Sstevel@tonic-gate #include <stdio.h>
20*0Sstevel@tonic-gate #include <string.h>
21*0Sstevel@tonic-gate 
22*0Sstevel@tonic-gate #ifdef MACOS
23*0Sstevel@tonic-gate #include "macos.h"
24*0Sstevel@tonic-gate #endif /* MACOS */
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate #if defined( DOS ) || defined( _WIN32 )
27*0Sstevel@tonic-gate #include <malloc.h>
28*0Sstevel@tonic-gate #include "msdos.h"
29*0Sstevel@tonic-gate #endif /* DOS */
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #if !defined( MACOS ) && !defined( DOS )
32*0Sstevel@tonic-gate #include <sys/types.h>
33*0Sstevel@tonic-gate #include <sys/socket.h>
34*0Sstevel@tonic-gate #endif /* !MACOS && !DOS */
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate #include "lber.h"
37*0Sstevel@tonic-gate #include "ldap.h"
38*0Sstevel@tonic-gate #include "ldap-private.h"
39*0Sstevel@tonic-gate #include "ldap-int.h"
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate BerElement * ldap_build_add_req(LDAP *ld, char *dn, LDAPMod **attrs,
43*0Sstevel@tonic-gate 								LDAPControl ** serverctrls)
44*0Sstevel@tonic-gate {
45*0Sstevel@tonic-gate 	BerElement * ber;
46*0Sstevel@tonic-gate 	int rc, i;
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate 	/*
49*0Sstevel@tonic-gate 	 * An add request looks like this:
50*0Sstevel@tonic-gate 	 *	AddRequest ::= [APPLICATION 8] SEQUENCE {
51*0Sstevel@tonic-gate 	 *		entry	DistinguishedName,
52*0Sstevel@tonic-gate 	 *		attrs	SEQUENCE OF SEQUENCE {
53*0Sstevel@tonic-gate 	 *			type	AttributeType,
54*0Sstevel@tonic-gate 	 *			values	SET OF AttributeValue
55*0Sstevel@tonic-gate 	 *		}
56*0Sstevel@tonic-gate 	 *	}
57*0Sstevel@tonic-gate 	 */
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate 	/* create a message to send */
60*0Sstevel@tonic-gate 	if ( (ber = alloc_ber_with_options( ld )) == NULLBER ) {
61*0Sstevel@tonic-gate 		ld->ld_errno = LDAP_NO_MEMORY;
62*0Sstevel@tonic-gate 		return( NULLBER );
63*0Sstevel@tonic-gate 	}
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate 	if ( ber_printf( ber, "{it{s{", ++ld->ld_msgid, LDAP_REQ_ADD, dn )
66*0Sstevel@tonic-gate 	    == -1 ) {
67*0Sstevel@tonic-gate 		ld->ld_errno = LDAP_ENCODING_ERROR;
68*0Sstevel@tonic-gate 		ber_free( ber, 1 );
69*0Sstevel@tonic-gate 		return( NULLBER );
70*0Sstevel@tonic-gate 	}
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate 	/* for each attribute in the entry... */
73*0Sstevel@tonic-gate 	for ( i = 0; attrs[i] != NULL; i++ ) {
74*0Sstevel@tonic-gate 		if ( ( attrs[i]->mod_op & LDAP_MOD_BVALUES) != 0 ) {
75*0Sstevel@tonic-gate 			rc = ber_printf( ber, "{s[V]}", attrs[i]->mod_type,
76*0Sstevel@tonic-gate 			    attrs[i]->mod_values );
77*0Sstevel@tonic-gate 		} else {
78*0Sstevel@tonic-gate 			rc = ber_printf( ber, "{s[v]}", attrs[i]->mod_type,
79*0Sstevel@tonic-gate 			    attrs[i]->mod_values );
80*0Sstevel@tonic-gate 		}
81*0Sstevel@tonic-gate 		if ( rc == -1 ) {
82*0Sstevel@tonic-gate 			ld->ld_errno = LDAP_ENCODING_ERROR;
83*0Sstevel@tonic-gate 			ber_free( ber, 1 );
84*0Sstevel@tonic-gate 			return(NULLBER);
85*0Sstevel@tonic-gate 		}
86*0Sstevel@tonic-gate 	}
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate 	if ( ber_printf( ber, "}}" ) == -1 ) {
89*0Sstevel@tonic-gate 		ld->ld_errno = LDAP_ENCODING_ERROR;
90*0Sstevel@tonic-gate 		ber_free( ber, 1 );
91*0Sstevel@tonic-gate 		return( NULLBER );
92*0Sstevel@tonic-gate 	}
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate /* LDAPv3 */
95*0Sstevel@tonic-gate 	/* Code controls if any */
96*0Sstevel@tonic-gate 	if (serverctrls && serverctrls[0]) {
97*0Sstevel@tonic-gate 		if (ldap_controls_code(ber, serverctrls) != LDAP_SUCCESS){
98*0Sstevel@tonic-gate 			ld->ld_errno = LDAP_ENCODING_ERROR;
99*0Sstevel@tonic-gate 			ber_free( ber, 1 );
100*0Sstevel@tonic-gate 			return( NULLBER );
101*0Sstevel@tonic-gate 		}
102*0Sstevel@tonic-gate 	} else if (ld->ld_srvctrls && ld->ld_srvctrls[0]) {
103*0Sstevel@tonic-gate 		/* Otherwise, is there any global server ctrls ? */
104*0Sstevel@tonic-gate 		if (ldap_controls_code(ber, ld->ld_srvctrls) != LDAP_SUCCESS){
105*0Sstevel@tonic-gate 			ld->ld_errno = LDAP_ENCODING_ERROR;
106*0Sstevel@tonic-gate 			ber_free( ber, 1 );
107*0Sstevel@tonic-gate 			return( NULLBER );
108*0Sstevel@tonic-gate 		}
109*0Sstevel@tonic-gate 	}
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate 	if ( ber_printf( ber, "}" ) == -1 ) {
112*0Sstevel@tonic-gate 		ld->ld_errno = LDAP_ENCODING_ERROR;
113*0Sstevel@tonic-gate 		ber_free( ber, 1 );
114*0Sstevel@tonic-gate 		return( NULLBER );
115*0Sstevel@tonic-gate 	}
116*0Sstevel@tonic-gate 
117*0Sstevel@tonic-gate 	return (ber);
118*0Sstevel@tonic-gate }
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate /*
122*0Sstevel@tonic-gate  * ldap_add - initiate an ldap (and X.500) add operation.  Parameters:
123*0Sstevel@tonic-gate  *
124*0Sstevel@tonic-gate  *	ld		LDAP descriptor
125*0Sstevel@tonic-gate  *	dn		DN of the entry to add
126*0Sstevel@tonic-gate  *	mods		List of attributes for the entry.  This is a null-
127*0Sstevel@tonic-gate  *			terminated array of pointers to LDAPMod structures.
128*0Sstevel@tonic-gate  *			only the type and values in the structures need be
129*0Sstevel@tonic-gate  *			filled in.
130*0Sstevel@tonic-gate  *
131*0Sstevel@tonic-gate  * Example:
132*0Sstevel@tonic-gate  *	LDAPMod	*attrs[] = {
133*0Sstevel@tonic-gate  *			{ 0, "cn", { "babs jensen", "babs", 0 } },
134*0Sstevel@tonic-gate  *			{ 0, "sn", { "jensen", 0 } },
135*0Sstevel@tonic-gate  *			{ 0, "objectClass", { "person", 0 } },
136*0Sstevel@tonic-gate  *			0
137*0Sstevel@tonic-gate  *		}
138*0Sstevel@tonic-gate  *	msgid = ldap_add( ld, dn, attrs );
139*0Sstevel@tonic-gate  */
140*0Sstevel@tonic-gate int ldap_add( LDAP *ld, char *dn, LDAPMod **attrs )
141*0Sstevel@tonic-gate {
142*0Sstevel@tonic-gate 	BerElement	*ber;
143*0Sstevel@tonic-gate 	int rv;
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate #ifdef _REENTRANT
146*0Sstevel@tonic-gate 	LOCK_LDAP(ld);
147*0Sstevel@tonic-gate #endif
148*0Sstevel@tonic-gate 	Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 87, "ldap_add\n"), 0, 0, 0 );
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate 	if ((ber = ldap_build_add_req(ld, dn, attrs, NULL)) == NULLBER){
151*0Sstevel@tonic-gate #ifdef _REENTRANT
152*0Sstevel@tonic-gate 		UNLOCK_LDAP(ld);
153*0Sstevel@tonic-gate #endif
154*0Sstevel@tonic-gate 		return (-1);
155*0Sstevel@tonic-gate 	}
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate 	/* send the message */
158*0Sstevel@tonic-gate 	rv = send_initial_request( ld, LDAP_REQ_ADD, dn, ber );
159*0Sstevel@tonic-gate #ifdef  _REENTRANT
160*0Sstevel@tonic-gate 	UNLOCK_LDAP(ld);
161*0Sstevel@tonic-gate #endif
162*0Sstevel@tonic-gate 	return (rv);
163*0Sstevel@tonic-gate }
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate int
166*0Sstevel@tonic-gate ldap_add_s( LDAP *ld, char *dn, LDAPMod **attrs )
167*0Sstevel@tonic-gate {
168*0Sstevel@tonic-gate 	int		msgid;
169*0Sstevel@tonic-gate 	LDAPMessage	*res;
170*0Sstevel@tonic-gate 
171*0Sstevel@tonic-gate 	if ( (msgid = ldap_add( ld, dn, attrs )) == -1 )
172*0Sstevel@tonic-gate 		return( ld->ld_errno );
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate 	if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, &res ) == -1 )
175*0Sstevel@tonic-gate 		return( ld->ld_errno );
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate 	return( ldap_result2error( ld, res, 1 ) );
178*0Sstevel@tonic-gate }
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate /* ldapv3 API extensions */
182*0Sstevel@tonic-gate /*
183*0Sstevel@tonic-gate  * ldap_add_ext - initiate an ldap (and X.500) add operation.  Parameters:
184*0Sstevel@tonic-gate  *
185*0Sstevel@tonic-gate  *	ld		LDAP descriptor
186*0Sstevel@tonic-gate  *	dn		DN of the entry to add
187*0Sstevel@tonic-gate  *	attrs	List of attributes for the entry.  This is a null-
188*0Sstevel@tonic-gate  *			terminated array of pointers to LDAPMod structures.
189*0Sstevel@tonic-gate  *			only the type and values in the structures need be
190*0Sstevel@tonic-gate  *			filled in.
191*0Sstevel@tonic-gate  *  serverctrls	List of server controls. This is a null-terminated
192*0Sstevel@tonic-gate  *			array of pointers to LDAPControl structures.
193*0Sstevel@tonic-gate  *  clientctrls	List of client controls.
194*0Sstevel@tonic-gate  *
195*0Sstevel@tonic-gate  * Example:
196*0Sstevel@tonic-gate  *	LDAPMod	*attrs[] = {
197*0Sstevel@tonic-gate  *			{ 0, "cn", { "babs jensen", "babs", 0 } },
198*0Sstevel@tonic-gate  *			{ 0, "sn", { "jensen", 0 } },
199*0Sstevel@tonic-gate  *			{ 0, "objectClass", { "person", 0 } },
200*0Sstevel@tonic-gate  *			0
201*0Sstevel@tonic-gate  *		}
202*0Sstevel@tonic-gate  *
203*0Sstevel@tonic-gate  *	retcode = ldap_add_ext( ld, dn, attrs, srvctrls, cltctrls, &msgid );
204*0Sstevel@tonic-gate  */
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate int ldap_add_ext(LDAP *ld, char *dn, LDAPMod **attrs,
207*0Sstevel@tonic-gate 				 LDAPControl ** serverctrls, LDAPControl **clientctrls, int *msgidp)
208*0Sstevel@tonic-gate {
209*0Sstevel@tonic-gate 	BerElement	*ber;
210*0Sstevel@tonic-gate 	int		i, rc;
211*0Sstevel@tonic-gate 	int rv;
212*0Sstevel@tonic-gate 
213*0Sstevel@tonic-gate #ifdef _REENTRANT
214*0Sstevel@tonic-gate 	LOCK_LDAP(ld);
215*0Sstevel@tonic-gate #endif
216*0Sstevel@tonic-gate 	Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 87, "ldap_add\n"), 0, 0, 0 );
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate 	if ((ber = ldap_build_add_req(ld, dn, attrs, serverctrls)) == NULLBER){
219*0Sstevel@tonic-gate 		rv = ld->ld_errno;
220*0Sstevel@tonic-gate 		if (rv == LDAP_SUCCESS)
221*0Sstevel@tonic-gate 			rv = LDAP_OTHER;
222*0Sstevel@tonic-gate #ifdef _REENTRANT
223*0Sstevel@tonic-gate 		UNLOCK_LDAP(ld);
224*0Sstevel@tonic-gate #endif
225*0Sstevel@tonic-gate 		return (rv);
226*0Sstevel@tonic-gate 	}
227*0Sstevel@tonic-gate 
228*0Sstevel@tonic-gate 	/* send the message */
229*0Sstevel@tonic-gate 	rv = send_initial_request( ld, LDAP_REQ_ADD, dn, ber );
230*0Sstevel@tonic-gate 	if (rv == -1) {
231*0Sstevel@tonic-gate 		rv = ld->ld_errno;
232*0Sstevel@tonic-gate 		if (rv == LDAP_SUCCESS){
233*0Sstevel@tonic-gate 			rv = LDAP_OTHER;
234*0Sstevel@tonic-gate 		}
235*0Sstevel@tonic-gate 
236*0Sstevel@tonic-gate #ifdef  _REENTRANT
237*0Sstevel@tonic-gate 		UNLOCK_LDAP(ld);
238*0Sstevel@tonic-gate #endif
239*0Sstevel@tonic-gate 		return (rv);
240*0Sstevel@tonic-gate 	}
241*0Sstevel@tonic-gate 
242*0Sstevel@tonic-gate 	*msgidp = rv;
243*0Sstevel@tonic-gate #ifdef  _REENTRANT
244*0Sstevel@tonic-gate 	UNLOCK_LDAP(ld);
245*0Sstevel@tonic-gate #endif
246*0Sstevel@tonic-gate 	return (LDAP_SUCCESS);
247*0Sstevel@tonic-gate }
248*0Sstevel@tonic-gate 
249*0Sstevel@tonic-gate int ldap_add_ext_s(LDAP *ld, char *dn, LDAPMod **attrs,
250*0Sstevel@tonic-gate 				   LDAPControl ** serverctrls, LDAPControl **clientctrls)
251*0Sstevel@tonic-gate {
252*0Sstevel@tonic-gate 	int msgid;
253*0Sstevel@tonic-gate 	int retcode = LDAP_SUCCESS;
254*0Sstevel@tonic-gate 	LDAPMessage *res;
255*0Sstevel@tonic-gate 
256*0Sstevel@tonic-gate 	if ((retcode = ldap_add_ext(ld, dn, attrs, serverctrls, clientctrls, &msgid)) != LDAP_SUCCESS)
257*0Sstevel@tonic-gate 		return (retcode);
258*0Sstevel@tonic-gate 	if (ldap_result(ld, msgid, 1, (struct timeval *)NULL, &res ) == -1)
259*0Sstevel@tonic-gate 		return (ld->ld_errno );
260*0Sstevel@tonic-gate 
261*0Sstevel@tonic-gate #ifdef  _REENTRANT
262*0Sstevel@tonic-gate 	LOCK_LDAP(ld);
263*0Sstevel@tonic-gate #endif
264*0Sstevel@tonic-gate 	retcode = ldap_parse_result(ld, res, &ld->ld_errno, &ld->ld_matched, &ld->ld_error,
265*0Sstevel@tonic-gate 								&ld->ld_referrals, &ld->ld_ret_ctrls, 1);
266*0Sstevel@tonic-gate 	if (retcode == LDAP_SUCCESS)
267*0Sstevel@tonic-gate 		retcode = ld->ld_errno;
268*0Sstevel@tonic-gate #ifdef  _REENTRANT
269*0Sstevel@tonic-gate 	UNLOCK_LDAP(ld);
270*0Sstevel@tonic-gate #endif
271*0Sstevel@tonic-gate 	return (retcode);
272*0Sstevel@tonic-gate }
273