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