1N/A#pragma ident "%Z%%M% %I% %E% SMI"
1N/A
1N/A/*
1N/A * The contents of this file are subject to the Netscape Public
1N/A * License Version 1.1 (the "License"); you may not use this file
1N/A * except in compliance with the License. You may obtain a copy of
1N/A * the License at http://www.mozilla.org/NPL/
1N/A *
1N/A * Software distributed under the License is distributed on an "AS
1N/A * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
1N/A * implied. See the License for the specific language governing
1N/A * rights and limitations under the License.
1N/A *
1N/A * The Original Code is Mozilla Communicator client code, released
1N/A * March 31, 1998.
1N/A *
1N/A * The Initial Developer of the Original Code is Netscape
1N/A * Communications Corporation. Portions created by Netscape are
1N/A * Copyright (C) 1998-1999 Netscape Communications Corporation. All
1N/A * Rights Reserved.
1N/A *
1N/A * Contributor(s):
1N/A */
1N/A/*
1N/A * Copyright (c) 1990 Regents of the University of Michigan.
1N/A * All rights reserved.
1N/A */
1N/A/*
1N/A * add.c
1N/A */
1N/A
1N/A#if 0
1N/A#ifndef lint
1N/Astatic char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
1N/A#endif
1N/A#endif
1N/A
1N/A#include "ldap-int.h"
1N/A
1N/A/*
1N/A * ldap_add - initiate an ldap add operation. Parameters:
1N/A *
1N/A * ld LDAP descriptor
1N/A * dn DN of the entry to add
1N/A * mods List of attributes for the entry. This is a null-
1N/A * terminated array of pointers to LDAPMod structures.
1N/A * only the type and values in the structures need be
1N/A * filled in.
1N/A *
1N/A * Example:
1N/A * LDAPMod *attrs[] = {
1N/A * { 0, "cn", { "babs jensen", "babs", 0 } },
1N/A * { 0, "sn", { "jensen", 0 } },
1N/A * { 0, "objectClass", { "person", 0 } },
1N/A * 0
1N/A * }
1N/A * msgid = ldap_add( ld, dn, attrs );
1N/A */
1N/Aint
1N/ALDAP_CALL
1N/Aldap_add( LDAP *ld, const char *dn, LDAPMod **attrs )
1N/A{
1N/A int msgid;
1N/A
1N/A LDAPDebug( LDAP_DEBUG_TRACE, "ldap_add\n", 0, 0, 0 );
1N/A
1N/A if ( ldap_add_ext( ld, dn, attrs, NULL, NULL, &msgid )
1N/A == LDAP_SUCCESS ) {
1N/A return( msgid );
1N/A } else {
1N/A return( -1 ); /* error is in ld handle */
1N/A }
1N/A}
1N/A
1N/A
1N/A/*
1N/A * LDAPv3 extended add.
1N/A * Returns an LDAP error code.
1N/A */
1N/Aint
1N/ALDAP_CALL
1N/Aldap_add_ext( LDAP *ld, const char *dn, LDAPMod **attrs,
1N/A LDAPControl **serverctrls, LDAPControl **clientctrls, int *msgidp )
1N/A{
1N/A BerElement *ber;
1N/A int i, rc, lderr;
1N/A
1N/A /*
1N/A * An add request looks like this:
1N/A * AddRequest ::= SEQUENCE {
1N/A * entry DistinguishedName,
1N/A * attrs SEQUENCE OF SEQUENCE {
1N/A * type AttributeType,
1N/A * values SET OF AttributeValue
1N/A * }
1N/A * }
1N/A */
1N/A
1N/A LDAPDebug( LDAP_DEBUG_TRACE, "ldap_add_ext\n", 0, 0, 0 );
1N/A
1N/A if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
1N/A return( LDAP_PARAM_ERROR );
1N/A }
1N/A
1N/A if ( !NSLDAPI_VALID_LDAPMESSAGE_POINTER( msgidp ))
1N/A {
1N/A LDAP_SET_LDERRNO( ld, LDAP_PARAM_ERROR, NULL, NULL );
1N/A return( LDAP_PARAM_ERROR );
1N/A }
1N/A if ( !NSLDAPI_VALID_NONEMPTY_LDAPMOD_ARRAY( attrs )
1N/A || msgidp == NULL ) {
1N/A lderr = LDAP_PARAM_ERROR;
1N/A LDAP_SET_LDERRNO( ld, lderr, NULL, NULL );
1N/A return( lderr );
1N/A }
1N/A
1N/A if ( dn == NULL ) {
1N/A dn = "";
1N/A }
1N/A
1N/A LDAP_MUTEX_LOCK( ld, LDAP_MSGID_LOCK );
1N/A *msgidp = ++ld->ld_msgid;
1N/A LDAP_MUTEX_UNLOCK( ld, LDAP_MSGID_LOCK );
1N/A
1N/A /* see if we should add to the cache */
1N/A if ( ld->ld_cache_on && ld->ld_cache_add != NULL ) {
1N/A LDAP_MUTEX_LOCK( ld, LDAP_CACHE_LOCK );
1N/A if ( (rc = (ld->ld_cache_add)( ld, *msgidp, LDAP_REQ_ADD, dn,
1N/A attrs )) != 0 ) {
1N/A *msgidp = rc;
1N/A LDAP_MUTEX_UNLOCK( ld, LDAP_CACHE_LOCK );
1N/A return( LDAP_SUCCESS );
1N/A }
1N/A LDAP_MUTEX_UNLOCK( ld, LDAP_CACHE_LOCK );
1N/A }
1N/A
1N/A /* create a message to send */
1N/A if (( lderr = nsldapi_alloc_ber_with_options( ld, &ber ))
1N/A != LDAP_SUCCESS ) {
1N/A return( lderr );
1N/A }
1N/A
1N/A if ( ber_printf( ber, "{it{s{", *msgidp, LDAP_REQ_ADD, dn )
1N/A == -1 ) {
1N/A lderr = LDAP_ENCODING_ERROR;
1N/A LDAP_SET_LDERRNO( ld, lderr, NULL, NULL );
1N/A ber_free( ber, 1 );
1N/A return( lderr );
1N/A }
1N/A
1N/A /* for each attribute in the entry... */
1N/A for ( i = 0; attrs[i] != NULL; i++ ) {
1N/A if ( ( attrs[i]->mod_op & LDAP_MOD_BVALUES) != 0 ) {
1N/A rc = ber_printf( ber, "{s[V]}", attrs[i]->mod_type,
1N/A attrs[i]->mod_bvalues );
1N/A } else {
1N/A rc = ber_printf( ber, "{s[v]}", attrs[i]->mod_type,
1N/A attrs[i]->mod_values );
1N/A }
1N/A if ( rc == -1 ) {
1N/A lderr = LDAP_ENCODING_ERROR;
1N/A LDAP_SET_LDERRNO( ld, lderr, NULL, NULL );
1N/A ber_free( ber, 1 );
1N/A return( lderr );
1N/A }
1N/A }
1N/A
1N/A if ( ber_printf( ber, "}}" ) == -1 ) {
1N/A lderr = LDAP_ENCODING_ERROR;
1N/A LDAP_SET_LDERRNO( ld, lderr, NULL, NULL );
1N/A ber_free( ber, 1 );
1N/A return( lderr );
1N/A }
1N/A
1N/A if (( lderr = nsldapi_put_controls( ld, serverctrls, 1, ber ))
1N/A != LDAP_SUCCESS ) {
1N/A ber_free( ber, 1 );
1N/A return( lderr );
1N/A }
1N/A
1N/A /* send the message */
1N/A rc = nsldapi_send_initial_request( ld, *msgidp, LDAP_REQ_ADD,
1N/A (char *) dn, ber );
1N/A *msgidp = rc;
1N/A return( rc < 0 ? LDAP_GET_LDERRNO( ld, NULL, NULL ) : LDAP_SUCCESS );
1N/A}
1N/A
1N/Aint
1N/ALDAP_CALL
1N/Aldap_add_s( LDAP *ld, const char *dn, LDAPMod **attrs )
1N/A{
1N/A return( ldap_add_ext_s( ld, dn, attrs, NULL, NULL ));
1N/A}
1N/A
1N/Aint LDAP_CALL
1N/Aldap_add_ext_s( LDAP *ld, const char *dn, LDAPMod **attrs,
1N/A LDAPControl **serverctrls, LDAPControl **clientctrls )
1N/A{
1N/A int err, msgid;
1N/A LDAPMessage *res;
1N/A
1N/A if (( err = ldap_add_ext( ld, dn, attrs, serverctrls, clientctrls,
1N/A &msgid )) != LDAP_SUCCESS ) {
1N/A return( err );
1N/A }
1N/A
1N/A if ( ldap_result( ld, msgid, 1, (struct timeval *)NULL, &res ) == -1 ) {
1N/A return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
1N/A }
1N/A
1N/A return( ldap_result2error( ld, res, 1 ) );
1N/A}