3 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1990 Regents of the University of Michigan.
13 * Portions Copyright (C) The Internet Society (1997)
14 * ASN.1 fragments are from RFC 2251; see RFC for full legal notices.
18 * An add request looks like this:
19 * AddRequest ::= SEQUENCE {
20 * entry DistinguishedName,
21 * attrs SEQUENCE OF SEQUENCE {
23 * values SET OF AttributeValue
32 #include <ac/socket.h>
33 #include <ac/string.h>
39 * ldap_add - initiate an ldap add operation. Parameters:
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
49 * LDAPMod *attrs[] = {
50 * { 0, "cn", { "babs jensen", "babs", 0 } },
51 * { 0, "sn", { "jensen", 0 } },
52 * { 0, "objectClass", { "person", 0 } },
55 * msgid = ldap_add( ld, dn, attrs );
58 ldap_add( LDAP *ld, LDAP_CONST char *dn, LDAPMod **attrs )
63 rc = ldap_add_ext( ld, dn, attrs, NULL, NULL, &msgid );
65 if ( rc != LDAP_SUCCESS )
73 * ldap_add_ext - initiate an ldap extended add operation. Parameters:
76 * dn DN of the entry to add
77 * mods List of attributes for the entry. This is a null-
78 * terminated array of pointers to LDAPMod structures.
79 * only the type and values in the structures need be
81 * sctrl Server Controls
82 * cctrl Client Controls
83 * msgidp Message ID pointer
86 * LDAPMod *attrs[] = {
87 * { 0, "cn", { "babs jensen", "babs", 0 } },
88 * { 0, "sn", { "jensen", 0 } },
89 * { 0, "objectClass", { "person", 0 } },
92 * rc = ldap_add_ext( ld, dn, attrs, NULL, NULL, &msgid );
100 LDAPControl **cctrls,
108 LDAP_LOG ( OPERATION, ENTRY, "ldap_add_ext\n", 0, 0, 0 );
110 Debug( LDAP_DEBUG_TRACE, "ldap_add_ext\n", 0, 0, 0 );
112 assert( ld != NULL );
113 assert( LDAP_VALID( ld ) );
114 assert( dn != NULL );
115 assert( msgidp != NULL );
117 /* check client controls */
118 rc = ldap_int_client_controls( ld, cctrls );
119 if( rc != LDAP_SUCCESS ) return rc;
121 /* create a message to send */
122 if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
123 ld->ld_errno = LDAP_NO_MEMORY;
127 LDAP_NEXT_MSGID(ld, id);
128 rc = ber_printf( ber, "{it{s{", /* '}}}' */
129 id, LDAP_REQ_ADD, dn );
132 ld->ld_errno = LDAP_ENCODING_ERROR;
137 /* for each attribute in the entry... */
138 for ( i = 0; attrs[i] != NULL; i++ ) {
139 if ( ( attrs[i]->mod_op & LDAP_MOD_BVALUES) != 0 ) {
140 rc = ber_printf( ber, "{s[V]N}", attrs[i]->mod_type,
141 attrs[i]->mod_bvalues );
143 rc = ber_printf( ber, "{s[v]N}", attrs[i]->mod_type,
144 attrs[i]->mod_values );
147 ld->ld_errno = LDAP_ENCODING_ERROR;
153 if ( ber_printf( ber, /*{{*/ "N}N}" ) == -1 ) {
154 ld->ld_errno = LDAP_ENCODING_ERROR;
159 /* Put Server Controls */
160 if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
165 if ( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
166 ld->ld_errno = LDAP_ENCODING_ERROR;
171 /* send the message */
172 *msgidp = ldap_send_initial_request( ld, LDAP_REQ_ADD, dn, ber, id );
185 LDAPControl **sctrls,
186 LDAPControl **cctrls )
191 rc = ldap_add_ext( ld, dn, attrs, sctrls, cctrls, &msgid );
193 if ( rc != LDAP_SUCCESS )
196 if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, &res ) == -1 )
197 return( ld->ld_errno );
199 return( ldap_result2error( ld, res, 1 ) );
203 ldap_add_s( LDAP *ld, LDAP_CONST char *dn, LDAPMod **attrs )
205 return ldap_add_ext_s( ld, dn, attrs, NULL, NULL );