3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2005 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,
113 Debug( LDAP_DEBUG_TRACE, "ldap_add_ext\n", 0, 0, 0 );
114 assert( ld != NULL );
115 assert( LDAP_VALID( ld ) );
116 assert( dn != NULL );
117 assert( msgidp != NULL );
119 /* check client controls */
120 rc = ldap_int_client_controls( ld, cctrls );
121 if( rc != LDAP_SUCCESS ) return rc;
123 /* create a message to send */
124 if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
125 ld->ld_errno = LDAP_NO_MEMORY;
129 LDAP_NEXT_MSGID(ld, id);
130 rc = ber_printf( ber, "{it{s{", /* '}}}' */
131 id, LDAP_REQ_ADD, dn );
134 ld->ld_errno = LDAP_ENCODING_ERROR;
139 /* for each attribute in the entry... */
140 for ( i = 0; attrs[i] != NULL; i++ ) {
141 if ( ( attrs[i]->mod_op & LDAP_MOD_BVALUES) != 0 ) {
142 rc = ber_printf( ber, "{s[V]N}", attrs[i]->mod_type,
143 attrs[i]->mod_bvalues );
145 rc = ber_printf( ber, "{s[v]N}", attrs[i]->mod_type,
146 attrs[i]->mod_values );
149 ld->ld_errno = LDAP_ENCODING_ERROR;
155 if ( ber_printf( ber, /*{{*/ "N}N}" ) == -1 ) {
156 ld->ld_errno = LDAP_ENCODING_ERROR;
161 /* Put Server Controls */
162 if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
167 if ( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
168 ld->ld_errno = LDAP_ENCODING_ERROR;
173 /* send the message */
174 *msgidp = ldap_send_initial_request( ld, LDAP_REQ_ADD, dn, ber, id );
187 LDAPControl **sctrls,
188 LDAPControl **cctrls )
193 rc = ldap_add_ext( ld, dn, attrs, sctrls, cctrls, &msgid );
195 if ( rc != LDAP_SUCCESS )
198 if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, &res ) == -1 )
199 return( ld->ld_errno );
201 return( ldap_result2error( ld, res, 1 ) );
205 ldap_add_s( LDAP *ld, LDAP_CONST char *dn, LDAPMod **attrs )
207 return ldap_add_ext_s( ld, dn, attrs, NULL, NULL );