2 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6 * Copyright (c) 1990 Regents of the University of Michigan.
13 * An add request looks like this:
14 * AddRequest ::= SEQUENCE {
15 * entry DistinguishedName,
16 * attrs SEQUENCE OF SEQUENCE {
18 * values SET OF AttributeValue
27 #include <ac/socket.h>
28 #include <ac/string.h>
34 * ldap_add - initiate an ldap add operation. Parameters:
37 * dn DN of the entry to add
38 * mods List of attributes for the entry. This is a null-
39 * terminated array of pointers to LDAPMod structures.
40 * only the type and values in the structures need be
44 * LDAPMod *attrs[] = {
45 * { 0, "cn", { "babs jensen", "babs", 0 } },
46 * { 0, "sn", { "jensen", 0 } },
47 * { 0, "objectClass", { "person", 0 } },
50 * msgid = ldap_add( ld, dn, attrs );
53 ldap_add( LDAP *ld, LDAP_CONST char *dn, LDAPMod **attrs )
58 rc = ldap_add_ext( ld, dn, attrs, NULL, NULL, &msgid );
60 if ( rc != LDAP_SUCCESS )
68 * ldap_add_ext - initiate an ldap extended add operation. Parameters:
71 * dn DN of the entry to add
72 * mods List of attributes for the entry. This is a null-
73 * terminated array of pointers to LDAPMod structures.
74 * only the type and values in the structures need be
76 * sctrl Server Controls
77 * cctrl Client Controls
78 * msgidp Message ID pointer
81 * LDAPMod *attrs[] = {
82 * { 0, "cn", { "babs jensen", "babs", 0 } },
83 * { 0, "sn", { "jensen", 0 } },
84 * { 0, "objectClass", { "person", 0 } },
87 * rc = ldap_add_ext( ld, dn, attrs, NULL, NULL, &msgid );
101 Debug( LDAP_DEBUG_TRACE, "ldap_add\n", 0, 0, 0 );
102 assert( ld != NULL );
103 assert( LDAP_VALID( ld ) );
104 assert( dn != NULL );
105 assert( msgidp != NULL );
107 /* create a message to send */
108 if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
109 ld->ld_errno = LDAP_NO_MEMORY;
113 rc = ber_printf( ber, "{it{s{", /* '}}}' */
114 ++ld->ld_msgid, LDAP_REQ_ADD, dn );
117 ld->ld_errno = LDAP_ENCODING_ERROR;
122 /* for each attribute in the entry... */
123 for ( i = 0; attrs[i] != NULL; i++ ) {
124 if ( ( attrs[i]->mod_op & LDAP_MOD_BVALUES) != 0 ) {
125 rc = ber_printf( ber, "{s[V]}", attrs[i]->mod_type,
126 attrs[i]->mod_values );
128 rc = ber_printf( ber, "{s[v]}", attrs[i]->mod_type,
129 attrs[i]->mod_values );
132 ld->ld_errno = LDAP_ENCODING_ERROR;
138 if ( ber_printf( ber, /*{{*/ "}}" ) == -1 ) {
139 ld->ld_errno = LDAP_ENCODING_ERROR;
144 /* Put Server Controls */
145 if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
150 if ( ber_printf( ber, /*{*/ "}" ) == -1 ) {
151 ld->ld_errno = LDAP_ENCODING_ERROR;
156 /* send the message */
157 *msgidp = ldap_send_initial_request( ld, LDAP_REQ_ADD, dn, ber );
170 LDAPControl **sctrls,
171 LDAPControl **cctrls )
176 rc = ldap_add_ext( ld, dn, attrs, sctrls, cctrls, &msgid );
178 if ( rc != LDAP_SUCCESS )
181 if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, &res ) == -1 )
182 return( ld->ld_errno );
184 return( ldap_result2error( ld, res, 1 ) );
188 ldap_add_s( LDAP *ld, LDAP_CONST char *dn, LDAPMod **attrs )
190 return ldap_add_ext_s( ld, dn, attrs, NULL, NULL );