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 (and X.500) 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, char *dn, LDAPMod **attrs )
48 * An add request looks like this:
49 * AddRequest ::= SEQUENCE {
50 * entry DistinguishedName,
51 * attrs SEQUENCE OF SEQUENCE {
53 * values SET OF AttributeValue
58 Debug( LDAP_DEBUG_TRACE, "ldap_add\n", 0, 0, 0 );
60 /* create a message to send */
61 if ( (ber = ldap_alloc_ber_with_options( ld )) == NULLBER ) {
65 if ( ber_printf( ber, "{it{s{", ++ld->ld_msgid, LDAP_REQ_ADD, dn )
67 ld->ld_errno = LDAP_ENCODING_ERROR;
72 /* for each attribute in the entry... */
73 for ( i = 0; attrs[i] != NULL; i++ ) {
74 if ( ( attrs[i]->mod_op & LDAP_MOD_BVALUES) != 0 ) {
75 rc = ber_printf( ber, "{s[V]}", attrs[i]->mod_type,
76 attrs[i]->mod_values );
78 rc = ber_printf( ber, "{s[v]}", attrs[i]->mod_type,
79 attrs[i]->mod_values );
82 ld->ld_errno = LDAP_ENCODING_ERROR;
88 if ( ber_printf( ber, "}}}" ) == -1 ) {
89 ld->ld_errno = LDAP_ENCODING_ERROR;
94 /* send the message */
95 return( ldap_send_initial_request( ld, LDAP_REQ_ADD, dn, ber ));
99 ldap_add_s( LDAP *ld, char *dn, LDAPMod **attrs )
104 if ( (msgid = ldap_add( ld, dn, attrs )) == -1 )
105 return( ld->ld_errno );
107 if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, &res ) == -1 )
108 return( ld->ld_errno );
110 return( ldap_result2error( ld, res, 1 ) );