Back to index

openldap  2.4.31
add.c
Go to the documentation of this file.
00001 /* add.c */
00002 /* $OpenLDAP$ */
00003 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00004  *
00005  * Copyright 1998-2012 The OpenLDAP Foundation.
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted only as authorized by the OpenLDAP
00010  * Public License.
00011  *
00012  * A copy of this license is available in the file LICENSE in the
00013  * top-level directory of the distribution or, alternatively, at
00014  * <http://www.OpenLDAP.org/license.html>.
00015  */
00016 /* Portions Copyright (c) 1990 Regents of the University of Michigan.
00017  * All rights reserved.
00018  */
00019 
00020 #include "portable.h"
00021 
00022 #include <stdio.h>
00023 
00024 #include <ac/socket.h>
00025 #include <ac/string.h>
00026 #include <ac/time.h>
00027 
00028 #include "ldap-int.h"
00029 
00030 /* An LDAP Add Request/Response looks like this:
00031  *        AddRequest ::= [APPLICATION 8] SEQUENCE {
00032  *            entry           LDAPDN,
00033  *            attributes      AttributeList }
00034  *
00035  *        AttributeList ::= SEQUENCE OF attribute Attribute
00036  *
00037  *        Attribute ::= PartialAttribute(WITH COMPONENTS {
00038  *             ...,
00039  *             vals (SIZE(1..MAX))})
00040  *
00041  *        PartialAttribute ::= SEQUENCE {
00042  *             type       AttributeDescription,
00043  *             vals       SET OF value AttributeValue }
00044  *
00045  *        AttributeDescription ::= LDAPString           
00046  *             -- Constrained to <attributedescription> [RFC4512]
00047  *                                      
00048  *        AttributeValue ::= OCTET STRING
00049  *        
00050  *        AddResponse ::= [APPLICATION 9] LDAPResult 
00051  * (Source: RFC 4511)
00052  */
00053 
00054 /*
00055  * ldap_add - initiate an ldap add operation.  Parameters:
00056  *
00057  *     ld            LDAP descriptor
00058  *     dn            DN of the entry to add
00059  *     mods          List of attributes for the entry.  This is a null-
00060  *                   terminated array of pointers to LDAPMod structures.
00061  *                   only the type and values in the structures need be
00062  *                   filled in.
00063  *
00064  * Example:
00065  *     LDAPMod       *attrs[] = { 
00066  *                   { 0, "cn", { "babs jensen", "babs", 0 } },
00067  *                   { 0, "sn", { "jensen", 0 } },
00068  *                   { 0, "objectClass", { "person", 0 } },
00069  *                   0
00070  *            }
00071  *     msgid = ldap_add( ld, dn, attrs );
00072  */
00073 int
00074 ldap_add( LDAP *ld, LDAP_CONST char *dn, LDAPMod **attrs )
00075 {
00076        int rc;
00077        int msgid;
00078 
00079        rc = ldap_add_ext( ld, dn, attrs, NULL, NULL, &msgid );
00080 
00081        if ( rc != LDAP_SUCCESS )
00082               return -1;
00083 
00084        return msgid;
00085 }
00086 
00087 
00088 /*
00089  * ldap_add_ext - initiate an ldap extended add operation.  Parameters:
00090  *
00091  *     ld            LDAP descriptor
00092  *     dn            DN of the entry to add
00093  *     mods          List of attributes for the entry.  This is a null-
00094  *                   terminated array of pointers to LDAPMod structures.
00095  *                   only the type and values in the structures need be
00096  *                   filled in.
00097  *     sctrl  Server Controls
00098  *     cctrl  Client Controls
00099  *     msgidp Message ID pointer
00100  *
00101  * Example:
00102  *     LDAPMod       *attrs[] = { 
00103  *                   { 0, "cn", { "babs jensen", "babs", 0 } },
00104  *                   { 0, "sn", { "jensen", 0 } },
00105  *                   { 0, "objectClass", { "person", 0 } },
00106  *                   0
00107  *            }
00108  *     rc = ldap_add_ext( ld, dn, attrs, NULL, NULL, &msgid );
00109  */
00110 int
00111 ldap_add_ext(
00112        LDAP *ld,
00113        LDAP_CONST char *dn,
00114        LDAPMod **attrs,
00115        LDAPControl **sctrls,
00116        LDAPControl **cctrls,
00117        int    *msgidp )
00118 {
00119        BerElement    *ber;
00120        int           i, rc;
00121        ber_int_t     id;
00122 
00123        Debug( LDAP_DEBUG_TRACE, "ldap_add_ext\n", 0, 0, 0 );
00124        assert( ld != NULL );
00125        assert( LDAP_VALID( ld ) );
00126        assert( dn != NULL );
00127        assert( msgidp != NULL );
00128 
00129        /* check client controls */
00130        rc = ldap_int_client_controls( ld, cctrls );
00131        if( rc != LDAP_SUCCESS ) return rc;
00132 
00133        /* create a message to send */
00134        if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
00135               ld->ld_errno = LDAP_NO_MEMORY;
00136               return ld->ld_errno;
00137        }
00138 
00139        LDAP_NEXT_MSGID(ld, id);
00140        rc = ber_printf( ber, "{it{s{", /* '}}}' */
00141               id, LDAP_REQ_ADD, dn );
00142 
00143        if ( rc == -1 ) {
00144               ld->ld_errno = LDAP_ENCODING_ERROR;
00145               ber_free( ber, 1 );
00146               return ld->ld_errno;
00147        }
00148 
00149        /* allow attrs to be NULL ("touch"; should fail...) */
00150        if ( attrs ) {
00151               /* for each attribute in the entry... */
00152               for ( i = 0; attrs[i] != NULL; i++ ) {
00153                      if ( ( attrs[i]->mod_op & LDAP_MOD_BVALUES) != 0 ) {
00154                             int j;
00155 
00156                             if ( attrs[i]->mod_bvalues == NULL ) {
00157                                    ld->ld_errno = LDAP_PARAM_ERROR;
00158                                    ber_free( ber, 1 );
00159                                    return ld->ld_errno;
00160                             }
00161 
00162                             for ( j = 0; attrs[i]->mod_bvalues[ j ] != NULL; j++ ) {
00163                                    if ( attrs[i]->mod_bvalues[ j ]->bv_val == NULL ) {
00164                                           ld->ld_errno = LDAP_PARAM_ERROR;
00165                                           ber_free( ber, 1 );
00166                                           return ld->ld_errno;
00167                                    }
00168                             }
00169 
00170                             rc = ber_printf( ber, "{s[V]N}", attrs[i]->mod_type,
00171                                 attrs[i]->mod_bvalues );
00172 
00173                      } else {
00174                             if ( attrs[i]->mod_values == NULL ) {
00175                                    ld->ld_errno = LDAP_PARAM_ERROR;
00176                                    ber_free( ber, 1 );
00177                                    return ld->ld_errno;
00178                             }
00179 
00180                             rc = ber_printf( ber, "{s[v]N}", attrs[i]->mod_type,
00181                                 attrs[i]->mod_values );
00182                      }
00183                      if ( rc == -1 ) {
00184                             ld->ld_errno = LDAP_ENCODING_ERROR;
00185                             ber_free( ber, 1 );
00186                             return ld->ld_errno;
00187                      }
00188               }
00189        }
00190 
00191        if ( ber_printf( ber, /*{{*/ "N}N}" ) == -1 ) {
00192               ld->ld_errno = LDAP_ENCODING_ERROR;
00193               ber_free( ber, 1 );
00194               return ld->ld_errno;
00195        }
00196 
00197        /* Put Server Controls */
00198        if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
00199               ber_free( ber, 1 );
00200               return ld->ld_errno;
00201        }
00202 
00203        if ( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
00204               ld->ld_errno = LDAP_ENCODING_ERROR;
00205               ber_free( ber, 1 );
00206               return ld->ld_errno;
00207        }
00208 
00209        /* send the message */
00210        *msgidp = ldap_send_initial_request( ld, LDAP_REQ_ADD, dn, ber, id );
00211 
00212        if(*msgidp < 0)
00213               return ld->ld_errno;
00214 
00215        return LDAP_SUCCESS;
00216 }
00217 
00218 int
00219 ldap_add_ext_s(
00220        LDAP *ld,
00221        LDAP_CONST char *dn,
00222        LDAPMod **attrs,
00223        LDAPControl **sctrls,
00224        LDAPControl **cctrls )
00225 {
00226        int           msgid, rc;
00227        LDAPMessage   *res;
00228 
00229        rc = ldap_add_ext( ld, dn, attrs, sctrls, cctrls, &msgid );
00230 
00231        if ( rc != LDAP_SUCCESS )
00232               return( rc );
00233 
00234        if ( ldap_result( ld, msgid, LDAP_MSG_ALL, (struct timeval *) NULL, &res ) == -1 || !res )
00235               return( ld->ld_errno );
00236 
00237        return( ldap_result2error( ld, res, 1 ) );
00238 }
00239 
00240 int
00241 ldap_add_s( LDAP *ld, LDAP_CONST char *dn, LDAPMod **attrs )
00242 {
00243        return ldap_add_ext_s( ld, dn, attrs, NULL, NULL );
00244 }
00245