3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2010 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* Portions Copyright (c) 1990 Regents of the University of Michigan.
17 * All rights reserved.
24 #include <ac/socket.h>
25 #include <ac/string.h>
30 /* An LDAP Add Request/Response looks like this:
31 * AddRequest ::= [APPLICATION 8] SEQUENCE {
33 * attributes AttributeList }
35 * AttributeList ::= SEQUENCE OF attribute Attribute
37 * Attribute ::= PartialAttribute(WITH COMPONENTS {
39 * vals (SIZE(1..MAX))})
41 * PartialAttribute ::= SEQUENCE {
42 * type AttributeDescription,
43 * vals SET OF value AttributeValue }
45 * AttributeDescription ::= LDAPString
46 * -- Constrained to <attributedescription> [RFC4512]
48 * AttributeValue ::= OCTET STRING
50 * AddResponse ::= [APPLICATION 9] LDAPResult
55 * ldap_add - initiate an ldap add operation. Parameters:
58 * dn DN of the entry to add
59 * mods List of attributes for the entry. This is a null-
60 * terminated array of pointers to LDAPMod structures.
61 * only the type and values in the structures need be
65 * LDAPMod *attrs[] = {
66 * { 0, "cn", { "babs jensen", "babs", 0 } },
67 * { 0, "sn", { "jensen", 0 } },
68 * { 0, "objectClass", { "person", 0 } },
71 * msgid = ldap_add( ld, dn, attrs );
74 ldap_add( LDAP *ld, LDAP_CONST char *dn, LDAPMod **attrs )
79 rc = ldap_add_ext( ld, dn, attrs, NULL, NULL, &msgid );
81 if ( rc != LDAP_SUCCESS )
89 * ldap_add_ext - initiate an ldap extended add operation. Parameters:
92 * dn DN of the entry to add
93 * mods List of attributes for the entry. This is a null-
94 * terminated array of pointers to LDAPMod structures.
95 * only the type and values in the structures need be
97 * sctrl Server Controls
98 * cctrl Client Controls
99 * msgidp Message ID pointer
102 * LDAPMod *attrs[] = {
103 * { 0, "cn", { "babs jensen", "babs", 0 } },
104 * { 0, "sn", { "jensen", 0 } },
105 * { 0, "objectClass", { "person", 0 } },
108 * rc = ldap_add_ext( ld, dn, attrs, NULL, NULL, &msgid );
115 LDAPControl **sctrls,
116 LDAPControl **cctrls,
123 Debug( LDAP_DEBUG_TRACE, "ldap_add_ext\n", 0, 0, 0 );
124 assert( ld != NULL );
125 assert( LDAP_VALID( ld ) );
126 assert( dn != NULL );
127 assert( msgidp != NULL );
129 /* check client controls */
130 rc = ldap_int_client_controls( ld, cctrls );
131 if( rc != LDAP_SUCCESS ) return rc;
133 /* create a message to send */
134 if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
135 ld->ld_errno = LDAP_NO_MEMORY;
139 LDAP_NEXT_MSGID(ld, id);
140 rc = ber_printf( ber, "{it{s{", /* '}}}' */
141 id, LDAP_REQ_ADD, dn );
144 ld->ld_errno = LDAP_ENCODING_ERROR;
149 /* allow attrs to be NULL ("touch"; should fail...) */
151 /* for each attribute in the entry... */
152 for ( i = 0; attrs[i] != NULL; i++ ) {
153 if ( ( attrs[i]->mod_op & LDAP_MOD_BVALUES) != 0 ) {
156 if ( attrs[i]->mod_bvalues == NULL ) {
157 ld->ld_errno = LDAP_PARAM_ERROR;
162 for ( j = 0; attrs[i]->mod_bvalues[ j ] != NULL; j++ ) {
163 if ( attrs[i]->mod_bvalues[ j ]->bv_val == NULL ) {
164 ld->ld_errno = LDAP_PARAM_ERROR;
170 rc = ber_printf( ber, "{s[V]N}", attrs[i]->mod_type,
171 attrs[i]->mod_bvalues );
174 if ( attrs[i]->mod_values == NULL ) {
175 ld->ld_errno = LDAP_PARAM_ERROR;
180 rc = ber_printf( ber, "{s[v]N}", attrs[i]->mod_type,
181 attrs[i]->mod_values );
184 ld->ld_errno = LDAP_ENCODING_ERROR;
191 if ( ber_printf( ber, /*{{*/ "N}N}" ) == -1 ) {
192 ld->ld_errno = LDAP_ENCODING_ERROR;
197 /* Put Server Controls */
198 if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
203 if ( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
204 ld->ld_errno = LDAP_ENCODING_ERROR;
209 /* send the message */
210 *msgidp = ldap_send_initial_request( ld, LDAP_REQ_ADD, dn, ber, id );
223 LDAPControl **sctrls,
224 LDAPControl **cctrls )
229 rc = ldap_add_ext( ld, dn, attrs, sctrls, cctrls, &msgid );
231 if ( rc != LDAP_SUCCESS )
234 if ( ldap_result( ld, msgid, LDAP_MSG_ALL, (struct timeval *) NULL, &res ) == -1 || !res )
235 return( ld->ld_errno );
237 return( ldap_result2error( ld, res, 1 ) );
241 ldap_add_s( LDAP *ld, LDAP_CONST char *dn, LDAPMod **attrs )
243 return ldap_add_ext_s( ld, dn, attrs, NULL, NULL );