xref: /titanic_50/usr/src/lib/libldap5/sources/ldap/common/add.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
2*7c478bd9Sstevel@tonic-gate 
3*7c478bd9Sstevel@tonic-gate /*
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the Netscape Public
5*7c478bd9Sstevel@tonic-gate  * License Version 1.1 (the "License"); you may not use this file
6*7c478bd9Sstevel@tonic-gate  * except in compliance with the License. You may obtain a copy of
7*7c478bd9Sstevel@tonic-gate  * the License at http://www.mozilla.org/NPL/
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * Software distributed under the License is distributed on an "AS
10*7c478bd9Sstevel@tonic-gate  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11*7c478bd9Sstevel@tonic-gate  * implied. See the License for the specific language governing
12*7c478bd9Sstevel@tonic-gate  * rights and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * The Original Code is Mozilla Communicator client code, released
15*7c478bd9Sstevel@tonic-gate  * March 31, 1998.
16*7c478bd9Sstevel@tonic-gate  *
17*7c478bd9Sstevel@tonic-gate  * The Initial Developer of the Original Code is Netscape
18*7c478bd9Sstevel@tonic-gate  * Communications Corporation. Portions created by Netscape are
19*7c478bd9Sstevel@tonic-gate  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
20*7c478bd9Sstevel@tonic-gate  * Rights Reserved.
21*7c478bd9Sstevel@tonic-gate  *
22*7c478bd9Sstevel@tonic-gate  * Contributor(s):
23*7c478bd9Sstevel@tonic-gate  */
24*7c478bd9Sstevel@tonic-gate /*
25*7c478bd9Sstevel@tonic-gate  *  Copyright (c) 1990 Regents of the University of Michigan.
26*7c478bd9Sstevel@tonic-gate  *  All rights reserved.
27*7c478bd9Sstevel@tonic-gate  */
28*7c478bd9Sstevel@tonic-gate /*
29*7c478bd9Sstevel@tonic-gate  *  add.c
30*7c478bd9Sstevel@tonic-gate  */
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate #if 0
33*7c478bd9Sstevel@tonic-gate #ifndef lint
34*7c478bd9Sstevel@tonic-gate static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
35*7c478bd9Sstevel@tonic-gate #endif
36*7c478bd9Sstevel@tonic-gate #endif
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate #include "ldap-int.h"
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate /*
41*7c478bd9Sstevel@tonic-gate  * ldap_add - initiate an ldap add operation.  Parameters:
42*7c478bd9Sstevel@tonic-gate  *
43*7c478bd9Sstevel@tonic-gate  *	ld		LDAP descriptor
44*7c478bd9Sstevel@tonic-gate  *	dn		DN of the entry to add
45*7c478bd9Sstevel@tonic-gate  *	mods		List of attributes for the entry.  This is a null-
46*7c478bd9Sstevel@tonic-gate  *			terminated array of pointers to LDAPMod structures.
47*7c478bd9Sstevel@tonic-gate  *			only the type and values in the structures need be
48*7c478bd9Sstevel@tonic-gate  *			filled in.
49*7c478bd9Sstevel@tonic-gate  *
50*7c478bd9Sstevel@tonic-gate  * Example:
51*7c478bd9Sstevel@tonic-gate  *	LDAPMod	*attrs[] = {
52*7c478bd9Sstevel@tonic-gate  *			{ 0, "cn", { "babs jensen", "babs", 0 } },
53*7c478bd9Sstevel@tonic-gate  *			{ 0, "sn", { "jensen", 0 } },
54*7c478bd9Sstevel@tonic-gate  *			{ 0, "objectClass", { "person", 0 } },
55*7c478bd9Sstevel@tonic-gate  *			0
56*7c478bd9Sstevel@tonic-gate  *		}
57*7c478bd9Sstevel@tonic-gate  *	msgid = ldap_add( ld, dn, attrs );
58*7c478bd9Sstevel@tonic-gate  */
59*7c478bd9Sstevel@tonic-gate int
60*7c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_add(LDAP * ld,const char * dn,LDAPMod ** attrs)61*7c478bd9Sstevel@tonic-gate ldap_add( LDAP *ld, const char *dn, LDAPMod **attrs )
62*7c478bd9Sstevel@tonic-gate {
63*7c478bd9Sstevel@tonic-gate 	int		msgid;
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate 	LDAPDebug( LDAP_DEBUG_TRACE, "ldap_add\n", 0, 0, 0 );
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate 	if ( ldap_add_ext( ld, dn, attrs, NULL, NULL, &msgid )
68*7c478bd9Sstevel@tonic-gate 	    == LDAP_SUCCESS ) {
69*7c478bd9Sstevel@tonic-gate 		return( msgid );
70*7c478bd9Sstevel@tonic-gate 	} else {
71*7c478bd9Sstevel@tonic-gate 		return( -1 );	/* error is in ld handle */
72*7c478bd9Sstevel@tonic-gate 	}
73*7c478bd9Sstevel@tonic-gate }
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate /*
77*7c478bd9Sstevel@tonic-gate  * LDAPv3 extended add.
78*7c478bd9Sstevel@tonic-gate  * Returns an LDAP error code.
79*7c478bd9Sstevel@tonic-gate  */
80*7c478bd9Sstevel@tonic-gate int
81*7c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_add_ext(LDAP * ld,const char * dn,LDAPMod ** attrs,LDAPControl ** serverctrls,LDAPControl ** clientctrls,int * msgidp)82*7c478bd9Sstevel@tonic-gate ldap_add_ext( LDAP *ld, const char *dn, LDAPMod **attrs,
83*7c478bd9Sstevel@tonic-gate     LDAPControl **serverctrls, LDAPControl **clientctrls, int *msgidp )
84*7c478bd9Sstevel@tonic-gate {
85*7c478bd9Sstevel@tonic-gate 	BerElement	*ber;
86*7c478bd9Sstevel@tonic-gate 	int		i, rc, lderr;
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate 	/*
89*7c478bd9Sstevel@tonic-gate 	 * An add request looks like this:
90*7c478bd9Sstevel@tonic-gate 	 *	AddRequest ::= SEQUENCE {
91*7c478bd9Sstevel@tonic-gate 	 *		entry	DistinguishedName,
92*7c478bd9Sstevel@tonic-gate 	 *		attrs	SEQUENCE OF SEQUENCE {
93*7c478bd9Sstevel@tonic-gate 	 *			type	AttributeType,
94*7c478bd9Sstevel@tonic-gate 	 *			values	SET OF AttributeValue
95*7c478bd9Sstevel@tonic-gate 	 *		}
96*7c478bd9Sstevel@tonic-gate 	 *	}
97*7c478bd9Sstevel@tonic-gate 	 */
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate 	LDAPDebug( LDAP_DEBUG_TRACE, "ldap_add_ext\n", 0, 0, 0 );
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate 	if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
102*7c478bd9Sstevel@tonic-gate 		return( LDAP_PARAM_ERROR );
103*7c478bd9Sstevel@tonic-gate 	}
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate 	if ( !NSLDAPI_VALID_LDAPMESSAGE_POINTER( msgidp ))
106*7c478bd9Sstevel@tonic-gate         {
107*7c478bd9Sstevel@tonic-gate 		LDAP_SET_LDERRNO( ld, LDAP_PARAM_ERROR, NULL, NULL );
108*7c478bd9Sstevel@tonic-gate 		return( LDAP_PARAM_ERROR );
109*7c478bd9Sstevel@tonic-gate 	}
110*7c478bd9Sstevel@tonic-gate 	if ( !NSLDAPI_VALID_NONEMPTY_LDAPMOD_ARRAY( attrs )
111*7c478bd9Sstevel@tonic-gate 	    || msgidp == NULL ) {
112*7c478bd9Sstevel@tonic-gate 		lderr = LDAP_PARAM_ERROR;
113*7c478bd9Sstevel@tonic-gate 		LDAP_SET_LDERRNO( ld, lderr, NULL, NULL );
114*7c478bd9Sstevel@tonic-gate 		return( lderr );
115*7c478bd9Sstevel@tonic-gate 	}
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate 	if ( dn == NULL ) {
118*7c478bd9Sstevel@tonic-gate 		dn = "";
119*7c478bd9Sstevel@tonic-gate 	}
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate 	LDAP_MUTEX_LOCK( ld, LDAP_MSGID_LOCK );
122*7c478bd9Sstevel@tonic-gate 	*msgidp = ++ld->ld_msgid;
123*7c478bd9Sstevel@tonic-gate 	LDAP_MUTEX_UNLOCK( ld, LDAP_MSGID_LOCK );
124*7c478bd9Sstevel@tonic-gate 
125*7c478bd9Sstevel@tonic-gate 	/* see if we should add to the cache */
126*7c478bd9Sstevel@tonic-gate 	if ( ld->ld_cache_on && ld->ld_cache_add != NULL ) {
127*7c478bd9Sstevel@tonic-gate 		LDAP_MUTEX_LOCK( ld, LDAP_CACHE_LOCK );
128*7c478bd9Sstevel@tonic-gate 		if ( (rc = (ld->ld_cache_add)( ld, *msgidp, LDAP_REQ_ADD, dn,
129*7c478bd9Sstevel@tonic-gate 		    attrs )) != 0 ) {
130*7c478bd9Sstevel@tonic-gate 			*msgidp = rc;
131*7c478bd9Sstevel@tonic-gate 			LDAP_MUTEX_UNLOCK( ld, LDAP_CACHE_LOCK );
132*7c478bd9Sstevel@tonic-gate 			return( LDAP_SUCCESS );
133*7c478bd9Sstevel@tonic-gate 		}
134*7c478bd9Sstevel@tonic-gate 		LDAP_MUTEX_UNLOCK( ld, LDAP_CACHE_LOCK );
135*7c478bd9Sstevel@tonic-gate 	}
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate 	/* create a message to send */
138*7c478bd9Sstevel@tonic-gate 	if (( lderr = nsldapi_alloc_ber_with_options( ld, &ber ))
139*7c478bd9Sstevel@tonic-gate 	    != LDAP_SUCCESS ) {
140*7c478bd9Sstevel@tonic-gate 		return( lderr );
141*7c478bd9Sstevel@tonic-gate 	}
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate 	if ( ber_printf( ber, "{it{s{", *msgidp, LDAP_REQ_ADD, dn )
144*7c478bd9Sstevel@tonic-gate 	    == -1 ) {
145*7c478bd9Sstevel@tonic-gate 		lderr = LDAP_ENCODING_ERROR;
146*7c478bd9Sstevel@tonic-gate 		LDAP_SET_LDERRNO( ld, lderr, NULL, NULL );
147*7c478bd9Sstevel@tonic-gate 		ber_free( ber, 1 );
148*7c478bd9Sstevel@tonic-gate 		return( lderr );
149*7c478bd9Sstevel@tonic-gate 	}
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate 	/* for each attribute in the entry... */
152*7c478bd9Sstevel@tonic-gate 	for ( i = 0; attrs[i] != NULL; i++ ) {
153*7c478bd9Sstevel@tonic-gate 		if ( ( attrs[i]->mod_op & LDAP_MOD_BVALUES) != 0 ) {
154*7c478bd9Sstevel@tonic-gate 			rc = ber_printf( ber, "{s[V]}", attrs[i]->mod_type,
155*7c478bd9Sstevel@tonic-gate 			    attrs[i]->mod_bvalues );
156*7c478bd9Sstevel@tonic-gate 		} else {
157*7c478bd9Sstevel@tonic-gate 			rc = ber_printf( ber, "{s[v]}", attrs[i]->mod_type,
158*7c478bd9Sstevel@tonic-gate 			    attrs[i]->mod_values );
159*7c478bd9Sstevel@tonic-gate 		}
160*7c478bd9Sstevel@tonic-gate 		if ( rc == -1 ) {
161*7c478bd9Sstevel@tonic-gate 			lderr = LDAP_ENCODING_ERROR;
162*7c478bd9Sstevel@tonic-gate 			LDAP_SET_LDERRNO( ld, lderr, NULL, NULL );
163*7c478bd9Sstevel@tonic-gate 			ber_free( ber, 1 );
164*7c478bd9Sstevel@tonic-gate 			return( lderr );
165*7c478bd9Sstevel@tonic-gate 		}
166*7c478bd9Sstevel@tonic-gate 	}
167*7c478bd9Sstevel@tonic-gate 
168*7c478bd9Sstevel@tonic-gate 	if ( ber_printf( ber, "}}" ) == -1 ) {
169*7c478bd9Sstevel@tonic-gate 		lderr = LDAP_ENCODING_ERROR;
170*7c478bd9Sstevel@tonic-gate 		LDAP_SET_LDERRNO( ld, lderr, NULL, NULL );
171*7c478bd9Sstevel@tonic-gate 		ber_free( ber, 1 );
172*7c478bd9Sstevel@tonic-gate 		return( lderr );
173*7c478bd9Sstevel@tonic-gate 	}
174*7c478bd9Sstevel@tonic-gate 
175*7c478bd9Sstevel@tonic-gate 	if (( lderr = nsldapi_put_controls( ld, serverctrls, 1, ber ))
176*7c478bd9Sstevel@tonic-gate 	    != LDAP_SUCCESS ) {
177*7c478bd9Sstevel@tonic-gate 		ber_free( ber, 1 );
178*7c478bd9Sstevel@tonic-gate 		return( lderr );
179*7c478bd9Sstevel@tonic-gate 	}
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate 	/* send the message */
182*7c478bd9Sstevel@tonic-gate 	rc = nsldapi_send_initial_request( ld, *msgidp, LDAP_REQ_ADD,
183*7c478bd9Sstevel@tonic-gate 						(char *) dn, ber );
184*7c478bd9Sstevel@tonic-gate 	*msgidp = rc;
185*7c478bd9Sstevel@tonic-gate 	return( rc < 0 ? LDAP_GET_LDERRNO( ld, NULL, NULL ) : LDAP_SUCCESS );
186*7c478bd9Sstevel@tonic-gate }
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate int
189*7c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_add_s(LDAP * ld,const char * dn,LDAPMod ** attrs)190*7c478bd9Sstevel@tonic-gate ldap_add_s( LDAP *ld, const char *dn, LDAPMod **attrs )
191*7c478bd9Sstevel@tonic-gate {
192*7c478bd9Sstevel@tonic-gate 	return( ldap_add_ext_s( ld, dn, attrs, NULL, NULL ));
193*7c478bd9Sstevel@tonic-gate }
194*7c478bd9Sstevel@tonic-gate 
195*7c478bd9Sstevel@tonic-gate int LDAP_CALL
ldap_add_ext_s(LDAP * ld,const char * dn,LDAPMod ** attrs,LDAPControl ** serverctrls,LDAPControl ** clientctrls)196*7c478bd9Sstevel@tonic-gate ldap_add_ext_s( LDAP *ld, const char *dn, LDAPMod **attrs,
197*7c478bd9Sstevel@tonic-gate 	LDAPControl **serverctrls, LDAPControl **clientctrls )
198*7c478bd9Sstevel@tonic-gate {
199*7c478bd9Sstevel@tonic-gate 	int		err, msgid;
200*7c478bd9Sstevel@tonic-gate 	LDAPMessage	*res;
201*7c478bd9Sstevel@tonic-gate 
202*7c478bd9Sstevel@tonic-gate 	if (( err = ldap_add_ext( ld, dn, attrs, serverctrls, clientctrls,
203*7c478bd9Sstevel@tonic-gate 	    &msgid )) != LDAP_SUCCESS ) {
204*7c478bd9Sstevel@tonic-gate 		return( err );
205*7c478bd9Sstevel@tonic-gate 	}
206*7c478bd9Sstevel@tonic-gate 
207*7c478bd9Sstevel@tonic-gate 	if ( ldap_result( ld, msgid, 1, (struct timeval *)NULL, &res ) == -1 ) {
208*7c478bd9Sstevel@tonic-gate 		return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
209*7c478bd9Sstevel@tonic-gate 	}
210*7c478bd9Sstevel@tonic-gate 
211*7c478bd9Sstevel@tonic-gate 	return( ldap_result2error( ld, res, 1 ) );
212*7c478bd9Sstevel@tonic-gate }
213