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.
16 #include <ac/socket.h>
17 #include <ac/string.h>
23 * ldap_add - initiate an ldap add operation. Parameters:
26 * dn DN of the entry to add
27 * mods List of attributes for the entry. This is a null-
28 * terminated array of pointers to LDAPMod structures.
29 * only the type and values in the structures need be
33 * LDAPMod *attrs[] = {
34 * { 0, "cn", { "babs jensen", "babs", 0 } },
35 * { 0, "sn", { "jensen", 0 } },
36 * { 0, "objectClass", { "person", 0 } },
39 * msgid = ldap_add( ld, dn, attrs );
42 ldap_add( LDAP *ld, LDAP_CONST char *dn, LDAPMod **attrs )
47 rc = ldap_add_ext( ld, dn, attrs, NULL, NULL, &msgid );
49 if ( rc != LDAP_SUCCESS )
57 * ldap_add_ext - initiate an ldap extended add operation. Parameters:
60 * dn DN of the entry to add
61 * mods List of attributes for the entry. This is a null-
62 * terminated array of pointers to LDAPMod structures.
63 * only the type and values in the structures need be
65 * sctrl Server Controls
66 * cctrl Client Controls
67 * msgidp Message ID pointer
70 * LDAPMod *attrs[] = {
71 * { 0, "cn", { "babs jensen", "babs", 0 } },
72 * { 0, "sn", { "jensen", 0 } },
73 * { 0, "objectClass", { "person", 0 } },
76 * rc = ldap_add_ext( ld, dn, attrs, NULL, NULL, &msgid );
79 ldap_add_ext( LDAP *ld, LDAP_CONST char *dn, LDAPMod **attrs,
88 * An add request looks like this:
89 * AddRequest ::= SEQUENCE {
90 * entry DistinguishedName,
91 * attrs SEQUENCE OF SEQUENCE {
93 * values SET OF AttributeValue
98 Debug( LDAP_DEBUG_TRACE, "ldap_add\n", 0, 0, 0 );
100 /* create a message to send */
101 if ( (ber = ldap_alloc_ber_with_options( ld )) == NULLBER ) {
102 ld->ld_errno = LDAP_NO_MEMORY;
106 rc = ber_printf( ber, "{it{s{", /* leave open '}}}' */
107 ++ld->ld_msgid, LDAP_REQ_ADD, dn );
110 ld->ld_errno = LDAP_ENCODING_ERROR;
115 /* for each attribute in the entry... */
116 for ( i = 0; attrs[i] != NULL; i++ ) {
117 if ( ( attrs[i]->mod_op & LDAP_MOD_BVALUES) != 0 ) {
118 rc = ber_printf( ber, "{s[V]}", attrs[i]->mod_type,
119 attrs[i]->mod_values );
121 rc = ber_printf( ber, "{s[v]}", attrs[i]->mod_type,
122 attrs[i]->mod_values );
125 ld->ld_errno = LDAP_ENCODING_ERROR;
132 if ( ber_printf( ber, "}}" ) == -1 ) {
133 ld->ld_errno = LDAP_ENCODING_ERROR;
138 /* Put Server Controls */
139 if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
145 if ( ber_printf( ber, "}" ) == -1 ) {
146 ld->ld_errno = LDAP_ENCODING_ERROR;
151 /* send the message */
152 *msgidp = ldap_send_initial_request( ld, LDAP_REQ_ADD, dn, ber );
165 LDAPControl **sctrls,
166 LDAPControl **cctrls )
171 rc = ldap_add_ext( ld, dn, attrs, sctrls, cctrls, &msgid );
173 if ( rc != LDAP_SUCCESS )
176 if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, &res ) == -1 )
177 return( ld->ld_errno );
179 return( ldap_result2error( ld, res, 1 ) );
183 ldap_add_s( LDAP *ld, LDAP_CONST char *dn, LDAPMod **attrs )
185 return ldap_add_ext_s( ld, dn, attrs, NULL, NULL );