Back to index

openldap  2.4.31
add.c
Go to the documentation of this file.
00001 /* add.c - ldap backend add function */
00002 /* $OpenLDAP$ */
00003 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00004  *
00005  * Copyright 1999-2012 The OpenLDAP Foundation.
00006  * Portions Copyright 2000-2003 Pierangelo Masarati.
00007  * Portions Copyright 1999-2003 Howard Chu.
00008  * All rights reserved.
00009  *
00010  * Redistribution and use in source and binary forms, with or without
00011  * modification, are permitted only as authorized by the OpenLDAP
00012  * Public License.
00013  *
00014  * A copy of this license is available in the file LICENSE in the
00015  * top-level directory of the distribution or, alternatively, at
00016  * <http://www.OpenLDAP.org/license.html>.
00017  */
00018 /* ACKNOWLEDGEMENTS:
00019  * This work was initially developed by the Howard Chu for inclusion
00020  * in OpenLDAP Software and subsequently enhanced by Pierangelo
00021  * Masarati.
00022  */
00023 
00024 #include "portable.h"
00025 
00026 #include <stdio.h>
00027 
00028 #include <ac/string.h>
00029 #include <ac/socket.h>
00030 
00031 #include "slap.h"
00032 #include "back-ldap.h"
00033 
00034 int
00035 ldap_back_add(
00036        Operation     *op,
00037        SlapReply     *rs )
00038 {
00039        ldapinfo_t           *li = (ldapinfo_t *)op->o_bd->be_private;
00040 
00041        ldapconn_t           *lc = NULL;
00042        int                  i = 0,
00043                             j = 0;
00044        Attribute            *a;
00045        LDAPMod                     **attrs = NULL,
00046                             *attrs2 = NULL;
00047        ber_int_t            msgid;
00048        int                  isupdate;
00049        ldap_back_send_t     retrying = LDAP_BACK_RETRYING;
00050        LDAPControl          **ctrls = NULL;
00051 
00052        rs->sr_err = LDAP_SUCCESS;
00053        
00054        Debug( LDAP_DEBUG_ARGS, "==> ldap_back_add(\"%s\")\n",
00055                      op->o_req_dn.bv_val, 0, 0 );
00056 
00057        if ( !ldap_back_dobind( &lc, op, rs, LDAP_BACK_SENDERR ) ) {
00058               lc = NULL;
00059               goto cleanup;
00060        }
00061 
00062        /* Count number of attributes in entry */
00063        for ( i = 1, a = op->oq_add.rs_e->e_attrs; a; i++, a = a->a_next )
00064               /* just count attrs */ ;
00065        
00066        /* Create array of LDAPMods for ldap_add() */
00067        attrs = (LDAPMod **)ch_malloc( sizeof( LDAPMod * )*i 
00068                      + sizeof( LDAPMod )*( i - 1 ) );
00069        attrs2 = ( LDAPMod * )&attrs[ i ];
00070 
00071        isupdate = be_shadow_update( op );
00072        for ( i = 0, a = op->oq_add.rs_e->e_attrs; a; a = a->a_next ) {
00073               if ( !isupdate && !get_relax( op ) && a->a_desc->ad_type->sat_no_user_mod  )
00074               {
00075                      continue;
00076               }
00077 
00078               attrs[ i ] = &attrs2[ i ];
00079               attrs[ i ]->mod_op = LDAP_MOD_BVALUES;
00080               attrs[ i ]->mod_type = a->a_desc->ad_cname.bv_val;
00081 
00082               for ( j = 0; a->a_vals[ j ].bv_val; j++ )
00083                      /* just count vals */ ;
00084               attrs[i]->mod_vals.modv_bvals = 
00085                      ch_malloc( ( j + 1 )*sizeof( struct berval * ) );
00086               for ( j = 0; a->a_vals[ j ].bv_val; j++ ) {
00087                      attrs[ i ]->mod_vals.modv_bvals[ j ] = &a->a_vals[ j ];
00088               }
00089               attrs[ i ]->mod_vals.modv_bvals[ j ] = NULL;
00090               i++;
00091        }
00092        attrs[ i ] = NULL;
00093 
00094 retry:
00095        ctrls = op->o_ctrls;
00096        rs->sr_err = ldap_back_controls_add( op, rs, lc, &ctrls );
00097        if ( rs->sr_err != LDAP_SUCCESS ) {
00098               send_ldap_result( op, rs );
00099               goto cleanup;
00100        }
00101 
00102        rs->sr_err = ldap_add_ext( lc->lc_ld, op->o_req_dn.bv_val, attrs,
00103                      ctrls, NULL, &msgid );
00104        rs->sr_err = ldap_back_op_result( lc, op, rs, msgid,
00105               li->li_timeout[ SLAP_OP_ADD ],
00106               ( LDAP_BACK_SENDRESULT | retrying ) );
00107        if ( rs->sr_err == LDAP_UNAVAILABLE && retrying ) {
00108               retrying &= ~LDAP_BACK_RETRYING;
00109               if ( ldap_back_retry( &lc, op, rs, LDAP_BACK_SENDERR ) ) {
00110                      /* if the identity changed, there might be need to re-authz */
00111                      (void)ldap_back_controls_free( op, rs, &ctrls );
00112                      goto retry;
00113               }
00114        }
00115 
00116 cleanup:
00117        (void)ldap_back_controls_free( op, rs, &ctrls );
00118 
00119        if ( attrs ) {
00120               for ( --i; i >= 0; --i ) {
00121                      ch_free( attrs[ i ]->mod_vals.modv_bvals );
00122               }
00123               ch_free( attrs );
00124        }
00125 
00126        if ( lc ) {
00127               ldap_back_release_conn( li, lc );
00128        }
00129 
00130        Debug( LDAP_DEBUG_ARGS, "<== ldap_back_add(\"%s\"): %d\n",
00131                      op->o_req_dn.bv_val, rs->sr_err, 0 );
00132 
00133        return rs->sr_err;
00134 }
00135