3 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1990 Regents of the University of Michigan.
14 * An add request looks like this:
15 * AddRequest ::= SEQUENCE {
16 * entry DistinguishedName,
17 * attrs SEQUENCE OF SEQUENCE {
19 * values SET OF AttributeValue
28 #include <ac/socket.h>
29 #include <ac/string.h>
35 * ldap_add - initiate an ldap add operation. Parameters:
38 * dn DN of the entry to add
39 * mods List of attributes for the entry. This is a null-
40 * terminated array of pointers to LDAPMod structures.
41 * only the type and values in the structures need be
45 * LDAPMod *attrs[] = {
46 * { 0, "cn", { "babs jensen", "babs", 0 } },
47 * { 0, "sn", { "jensen", 0 } },
48 * { 0, "objectClass", { "person", 0 } },
51 * msgid = ldap_add( ld, dn, attrs );
54 ldap_add( LDAP *ld, LDAP_CONST char *dn, LDAPMod **attrs )
59 rc = ldap_add_ext( ld, dn, attrs, NULL, NULL, &msgid );
61 if ( rc != LDAP_SUCCESS )
69 * ldap_add_ext - initiate an ldap extended add operation. Parameters:
72 * dn DN of the entry to add
73 * mods List of attributes for the entry. This is a null-
74 * terminated array of pointers to LDAPMod structures.
75 * only the type and values in the structures need be
77 * sctrl Server Controls
78 * cctrl Client Controls
79 * msgidp Message ID pointer
82 * LDAPMod *attrs[] = {
83 * { 0, "cn", { "babs jensen", "babs", 0 } },
84 * { 0, "sn", { "jensen", 0 } },
85 * { 0, "objectClass", { "person", 0 } },
88 * rc = ldap_add_ext( ld, dn, attrs, NULL, NULL, &msgid );
102 Debug( LDAP_DEBUG_TRACE, "ldap_add\n", 0, 0, 0 );
103 assert( ld != NULL );
104 assert( LDAP_VALID( ld ) );
105 assert( dn != NULL );
106 assert( msgidp != NULL );
108 /* check client controls */
109 rc = ldap_int_client_controls( ld, cctrls );
110 if( rc != LDAP_SUCCESS ) return rc;
112 /* create a message to send */
113 if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
114 ld->ld_errno = LDAP_NO_MEMORY;
118 rc = ber_printf( ber, "{it{s{", /* '}}}' */
119 ++ld->ld_msgid, LDAP_REQ_ADD, dn );
122 ld->ld_errno = LDAP_ENCODING_ERROR;
127 /* for each attribute in the entry... */
128 for ( i = 0; attrs[i] != NULL; i++ ) {
129 if ( ( attrs[i]->mod_op & LDAP_MOD_BVALUES) != 0 ) {
130 rc = ber_printf( ber, "{s[V]N}", attrs[i]->mod_type,
131 attrs[i]->mod_bvalues );
133 rc = ber_printf( ber, "{s[v]N}", attrs[i]->mod_type,
134 attrs[i]->mod_values );
137 ld->ld_errno = LDAP_ENCODING_ERROR;
143 if ( ber_printf( ber, /*{{*/ "N}N}" ) == -1 ) {
144 ld->ld_errno = LDAP_ENCODING_ERROR;
149 /* Put Server Controls */
150 if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
155 if ( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
156 ld->ld_errno = LDAP_ENCODING_ERROR;
161 /* send the message */
162 *msgidp = ldap_send_initial_request( ld, LDAP_REQ_ADD, dn, ber );
175 LDAPControl **sctrls,
176 LDAPControl **cctrls )
181 rc = ldap_add_ext( ld, dn, attrs, sctrls, cctrls, &msgid );
183 if ( rc != LDAP_SUCCESS )
186 if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, &res ) == -1 )
187 return( ld->ld_errno );
189 return( ldap_result2error( ld, res, 1 ) );
193 ldap_add_s( LDAP *ld, LDAP_CONST char *dn, LDAPMod **attrs )
195 return ldap_add_ext_s( ld, dn, attrs, NULL, NULL );