Back to index

lightning-sunbird  0.9+nobinonly
add.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Mozilla Communicator client code, released
00015  * March 31, 1998.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 /*
00038  *  Copyright (c) 1990 Regents of the University of Michigan.
00039  *  All rights reserved.
00040  */
00041 /*
00042  *  add.c
00043  */
00044 
00045 #if 0
00046 #ifndef lint 
00047 static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
00048 #endif
00049 #endif
00050 
00051 #include "ldap-int.h"
00052 
00053 /*
00054  * ldap_add - initiate an ldap add operation.  Parameters:
00055  *
00056  *     ld            LDAP descriptor
00057  *     dn            DN of the entry to add
00058  *     mods          List of attributes for the entry.  This is a null-
00059  *                   terminated array of pointers to LDAPMod structures.
00060  *                   only the type and values in the structures need be
00061  *                   filled in.
00062  *
00063  * Example:
00064  *     LDAPMod       *attrs[] = { 
00065  *                   { 0, "cn", { "babs jensen", "babs", 0 } },
00066  *                   { 0, "sn", { "jensen", 0 } },
00067  *                   { 0, "objectClass", { "person", 0 } },
00068  *                   0
00069  *            }
00070  *     msgid = ldap_add( ld, dn, attrs );
00071  */
00072 int
00073 LDAP_CALL
00074 ldap_add( LDAP *ld, const char *dn, LDAPMod **attrs )
00075 {
00076        int           msgid;
00077 
00078        LDAPDebug( LDAP_DEBUG_TRACE, "ldap_add\n", 0, 0, 0 );
00079 
00080        if ( ldap_add_ext( ld, dn, attrs, NULL, NULL, &msgid )
00081            == LDAP_SUCCESS ) {
00082               return( msgid );
00083        } else {
00084               return( -1 ); /* error is in ld handle */
00085        }
00086 }
00087 
00088 
00089 /*
00090  * LDAPv3 extended add.
00091  * Returns an LDAP error code.
00092  */
00093 int
00094 LDAP_CALL
00095 ldap_add_ext( LDAP *ld, const char *dn, LDAPMod **attrs,
00096     LDAPControl **serverctrls, LDAPControl **clientctrls, int *msgidp )
00097 {
00098        BerElement    *ber;
00099        int           i, rc, lderr;
00100 
00101        /*
00102         * An add request looks like this:
00103         *     AddRequest ::= SEQUENCE {
00104         *            entry  DistinguishedName,
00105         *            attrs  SEQUENCE OF SEQUENCE {
00106         *                   type   AttributeType,
00107         *                   values SET OF AttributeValue
00108         *            }
00109         *     }
00110         */
00111 
00112        LDAPDebug( LDAP_DEBUG_TRACE, "ldap_add_ext\n", 0, 0, 0 );
00113 
00114        if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
00115               return( LDAP_PARAM_ERROR );
00116        }
00117 
00118        if ( !NSLDAPI_VALID_LDAPMESSAGE_POINTER( msgidp )) 
00119         {
00120               LDAP_SET_LDERRNO( ld, LDAP_PARAM_ERROR, NULL, NULL );
00121               return( LDAP_PARAM_ERROR );
00122        }
00123        if ( !NSLDAPI_VALID_NONEMPTY_LDAPMOD_ARRAY( attrs )
00124            || msgidp == NULL ) {
00125               lderr = LDAP_PARAM_ERROR;
00126               LDAP_SET_LDERRNO( ld, lderr, NULL, NULL );
00127               return( lderr );
00128        }
00129 
00130        if ( dn == NULL ) {
00131               dn = "";
00132        }
00133 
00134        LDAP_MUTEX_LOCK( ld, LDAP_MSGID_LOCK );
00135        *msgidp = ++ld->ld_msgid;
00136        LDAP_MUTEX_UNLOCK( ld, LDAP_MSGID_LOCK );
00137 
00138        /* see if we should add to the cache */
00139        if ( ld->ld_cache_on && ld->ld_cache_add != NULL ) {
00140               LDAP_MUTEX_LOCK( ld, LDAP_CACHE_LOCK );
00141               if ( (rc = (ld->ld_cache_add)( ld, *msgidp, LDAP_REQ_ADD, dn,
00142                   attrs )) != 0 ) {
00143                      *msgidp = rc;
00144                      LDAP_MUTEX_UNLOCK( ld, LDAP_CACHE_LOCK );
00145                      return( LDAP_SUCCESS );
00146               }
00147               LDAP_MUTEX_UNLOCK( ld, LDAP_CACHE_LOCK );
00148        }
00149 
00150        /* create a message to send */
00151        if (( lderr = nsldapi_alloc_ber_with_options( ld, &ber ))
00152            != LDAP_SUCCESS ) {
00153               return( lderr );
00154        }
00155 
00156        if ( ber_printf( ber, "{it{s{", *msgidp, LDAP_REQ_ADD, dn )
00157            == -1 ) {
00158               lderr = LDAP_ENCODING_ERROR;
00159               LDAP_SET_LDERRNO( ld, lderr, NULL, NULL );
00160               ber_free( ber, 1 );
00161               return( lderr );
00162        }
00163 
00164        /* for each attribute in the entry... */
00165        for ( i = 0; attrs[i] != NULL; i++ ) {
00166               if ( ( attrs[i]->mod_op & LDAP_MOD_BVALUES) != 0 ) {
00167                      rc = ber_printf( ber, "{s[V]}", attrs[i]->mod_type,
00168                          attrs[i]->mod_bvalues );
00169               } else {
00170                      rc = ber_printf( ber, "{s[v]}", attrs[i]->mod_type,
00171                          attrs[i]->mod_values );
00172               }
00173               if ( rc == -1 ) {
00174                      lderr = LDAP_ENCODING_ERROR;
00175                      LDAP_SET_LDERRNO( ld, lderr, NULL, NULL );
00176                      ber_free( ber, 1 );
00177                      return( lderr );
00178               }
00179        }
00180 
00181        if ( ber_printf( ber, "}}" ) == -1 ) {
00182               lderr = LDAP_ENCODING_ERROR;
00183               LDAP_SET_LDERRNO( ld, lderr, NULL, NULL );
00184               ber_free( ber, 1 );
00185               return( lderr );
00186        }
00187 
00188        if (( lderr = nsldapi_put_controls( ld, serverctrls, 1, ber ))
00189            != LDAP_SUCCESS ) {
00190               ber_free( ber, 1 );
00191               return( lderr );
00192        }
00193 
00194        /* send the message */
00195        rc = nsldapi_send_initial_request( ld, *msgidp, LDAP_REQ_ADD,
00196                                           (char *) dn, ber );
00197        *msgidp = rc;
00198        return( rc < 0 ? LDAP_GET_LDERRNO( ld, NULL, NULL ) : LDAP_SUCCESS );
00199 }
00200 
00201 int
00202 LDAP_CALL
00203 ldap_add_s( LDAP *ld, const char *dn, LDAPMod **attrs )
00204 {
00205        return( ldap_add_ext_s( ld, dn, attrs, NULL, NULL ));
00206 }
00207 
00208 int LDAP_CALL
00209 ldap_add_ext_s( LDAP *ld, const char *dn, LDAPMod **attrs,
00210        LDAPControl **serverctrls, LDAPControl **clientctrls )
00211 {
00212        int           err, msgid;
00213        LDAPMessage   *res;
00214 
00215        if (( err = ldap_add_ext( ld, dn, attrs, serverctrls, clientctrls,
00216            &msgid )) != LDAP_SUCCESS ) {
00217               return( err );
00218        }
00219 
00220        if ( ldap_result( ld, msgid, 1, (struct timeval *)NULL, &res ) == -1 ) {
00221               return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
00222        }
00223 
00224        return( ldap_result2error( ld, res, 1 ) );
00225 }