3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2004 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.
19 /* Portions Copyright (C) The Internet Society (1997).
20 * ASN.1 fragments are from RFC 2251; see RFC for full legal notices.
24 * An add request looks like this:
25 * AddRequest ::= SEQUENCE {
26 * entry DistinguishedName,
27 * attrs SEQUENCE OF SEQUENCE {
29 * values SET OF AttributeValue
38 #include <ac/socket.h>
39 #include <ac/string.h>
45 * ldap_add - initiate an ldap add operation. Parameters:
48 * dn DN of the entry to add
49 * mods List of attributes for the entry. This is a null-
50 * terminated array of pointers to LDAPMod structures.
51 * only the type and values in the structures need be
55 * LDAPMod *attrs[] = {
56 * { 0, "cn", { "babs jensen", "babs", 0 } },
57 * { 0, "sn", { "jensen", 0 } },
58 * { 0, "objectClass", { "person", 0 } },
61 * msgid = ldap_add( ld, dn, attrs );
64 ldap_add( LDAP *ld, LDAP_CONST char *dn, LDAPMod **attrs )
69 rc = ldap_add_ext( ld, dn, attrs, NULL, NULL, &msgid );
71 if ( rc != LDAP_SUCCESS )
79 * ldap_add_ext - initiate an ldap extended add operation. Parameters:
82 * dn DN of the entry to add
83 * mods List of attributes for the entry. This is a null-
84 * terminated array of pointers to LDAPMod structures.
85 * only the type and values in the structures need be
87 * sctrl Server Controls
88 * cctrl Client Controls
89 * msgidp Message ID pointer
92 * LDAPMod *attrs[] = {
93 * { 0, "cn", { "babs jensen", "babs", 0 } },
94 * { 0, "sn", { "jensen", 0 } },
95 * { 0, "objectClass", { "person", 0 } },
98 * rc = ldap_add_ext( ld, dn, attrs, NULL, NULL, &msgid );
105 LDAPControl **sctrls,
106 LDAPControl **cctrls,
114 LDAP_LOG ( OPERATION, ENTRY, "ldap_add_ext\n", 0, 0, 0 );
116 Debug( LDAP_DEBUG_TRACE, "ldap_add_ext\n", 0, 0, 0 );
118 assert( ld != NULL );
119 assert( LDAP_VALID( ld ) );
120 assert( dn != NULL );
121 assert( msgidp != NULL );
123 /* check client controls */
124 rc = ldap_int_client_controls( ld, cctrls );
125 if( rc != LDAP_SUCCESS ) return rc;
127 /* create a message to send */
128 if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
129 ld->ld_errno = LDAP_NO_MEMORY;
133 LDAP_NEXT_MSGID(ld, id);
134 rc = ber_printf( ber, "{it{s{", /* '}}}' */
135 id, LDAP_REQ_ADD, dn );
138 ld->ld_errno = LDAP_ENCODING_ERROR;
143 /* for each attribute in the entry... */
144 for ( i = 0; attrs[i] != NULL; i++ ) {
145 if ( ( attrs[i]->mod_op & LDAP_MOD_BVALUES) != 0 ) {
146 rc = ber_printf( ber, "{s[V]N}", attrs[i]->mod_type,
147 attrs[i]->mod_bvalues );
149 rc = ber_printf( ber, "{s[v]N}", attrs[i]->mod_type,
150 attrs[i]->mod_values );
153 ld->ld_errno = LDAP_ENCODING_ERROR;
159 if ( ber_printf( ber, /*{{*/ "N}N}" ) == -1 ) {
160 ld->ld_errno = LDAP_ENCODING_ERROR;
165 /* Put Server Controls */
166 if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
171 if ( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
172 ld->ld_errno = LDAP_ENCODING_ERROR;
177 /* send the message */
178 *msgidp = ldap_send_initial_request( ld, LDAP_REQ_ADD, dn, ber, id );
191 LDAPControl **sctrls,
192 LDAPControl **cctrls )
197 rc = ldap_add_ext( ld, dn, attrs, sctrls, cctrls, &msgid );
199 if ( rc != LDAP_SUCCESS )
202 if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, &res ) == -1 )
203 return( ld->ld_errno );
205 return( ldap_result2error( ld, res, 1 ) );
209 ldap_add_s( LDAP *ld, LDAP_CONST char *dn, LDAPMod **attrs )
211 return ldap_add_ext_s( ld, dn, attrs, NULL, NULL );