2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2003 The OpenLDAP Foundation.
5 * Portions Copyright 2003 IBM Corporation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
17 * This work was initially developed by the Apurva Kumar for inclusion
18 * in OpenLDAP Software.
25 #include <ac/socket.h>
26 #include <ac/string.h>
31 #include "../back-ldap/back-ldap.h"
32 #include "back-meta.h"
34 #undef ldap_debug /* silence a warning in ldap-int.h */
36 #include "../../../libraries/libldap/ldap-int.h"
39 static struct berval bv_queryid_any = BER_BVC( "(queryid=*)" );
42 add_attribute(AttributeDescription *ad,
54 normalize_values( Attribute* attr );
62 struct timeval tv; /* time */
63 enum type_of_result err;
72 struct exception* result )
81 result->type = SUCCESS;
83 if ( e->e_dn != NULL ) {
84 tmplen = strlen( e->e_dn );
85 size = LDIF_SIZE_NEEDED( 2, tmplen );
88 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
89 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
91 tmplen = a->a_desc->ad_cname.bv_len;
92 size += LDIF_SIZE_NEEDED( tmplen, bv.bv_len);
95 if ((size < size_init) && result) {
96 result->type = SIZE_ERR;
105 struct berval* query_uuid,
106 struct exception* result )
108 struct entry_info info;
110 Modifications* modlist = NULL;
111 const char* text = NULL;
112 BerVarray value_array;
113 Attribute *uuid_attr, *attr;
116 SlapReply sreply = {REP_RESULT};
118 Operation op_tmp = *op;
121 sreply.sr_entry = NULL;
122 sreply.sr_nentries = 0;
124 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
126 dnPrettyNormal(0, &rs->sr_entry->e_name, &op_tmp.o_req_dn, &op_tmp.o_req_ndn, op->o_tmpmemctx);
127 ber_dupbv( &e->e_name, &op_tmp.o_req_dn );
128 ber_dupbv( &e->e_nname, &op_tmp.o_req_ndn );
129 sl_free( op_tmp.o_req_ndn.bv_val, op->o_tmpmemctx );
130 sl_free( op_tmp.o_req_dn.bv_val, op->o_tmpmemctx );
131 op_tmp.o_req_dn = e->e_name;
132 op_tmp.o_req_ndn = e->e_nname;
136 e->e_bv.bv_val = NULL;
138 /* add queryid attribute */
139 value_array = (struct berval *)malloc(2 * sizeof( struct berval) );
140 ber_dupbv(value_array, query_uuid);
141 value_array[1].bv_val = NULL;
142 value_array[1].bv_len = 0;
144 uuid_attr = add_attribute(slap_schema.si_ad_queryid, e, value_array);
146 /* append the attribute list from the fetched entry */
147 uuid_attr->a_next = rs->sr_entry->e_attrs;
148 rs->sr_entry->e_attrs = NULL;
150 for ( attr = e->e_attrs; attr; attr = attr->a_next ) {
151 if ( normalize_values( attr ) ) {
152 info.err = MERGE_ERR;
153 result->rc = info.err;
159 info.uuid = query_uuid;
160 info.size_init = get_entry_size( rs->sr_entry, 0, 0 );
163 info.glue_be = op->o_bd;
165 cb.sc_private = &info;
166 cb.sc_response = null_response;
168 op_tmp.o_tag = LDAP_REQ_ADD;
169 op_tmp.o_protocol = LDAP_VERSION3;
170 op_tmp.o_callback = &cb;
171 op_tmp.o_time = slap_get_time();
172 op_tmp.o_do_not_cache = 1;
175 rc = op->o_bd->be_add( &op_tmp, &sreply );
177 if ( rc != LDAP_SUCCESS ) {
178 if ( rc == LDAP_ALREADY_EXISTS ) {
179 slap_entry2mods( e, &modlist, &text );
180 op_tmp.o_tag = LDAP_REQ_MODIFY;
181 op_tmp.orm_modlist = modlist;
182 op_tmp.o_req_dn = e->e_name;
183 op_tmp.o_req_ndn = e->e_nname;
184 rc = op->o_bd->be_modify( &op_tmp, &sreply );
185 result->rc = info.added;
186 } else if ( rc == LDAP_REFERRAL ||
187 rc == LDAP_NO_SUCH_OBJECT ) {
188 syncrepl_add_glue( &op_tmp, e );
189 result->rc = info.added;
193 if ( modlist != NULL ) slap_mods_free( modlist );
196 result->rc = info.added;
197 be_entry_release_w( &op_tmp, e );
201 info.size_final = get_entry_size( e, info.size_init, result );
203 info.size_final = info.size_init;
205 return ( info.size_final - info.size_init );
209 add_attribute(AttributeDescription *ad,
211 BerVarray value_array)
213 Attribute* new_attr, *last_attr;
216 if (e->e_attrs == NULL)
219 for (last_attr = e->e_attrs; last_attr->a_next;
220 last_attr = last_attr->a_next)
223 new_attr = (Attribute*)malloc(sizeof(Attribute));
225 last_attr->a_next = new_attr;
227 e->e_attrs = new_attr;
229 new_attr->a_next = NULL;
230 new_attr->a_desc = NULL;
231 new_attr->a_vals = value_array;
232 new_attr->a_desc = ad;
246 normalize_values( Attribute* attr )
250 if (attr->a_vals == NULL) {
251 attr->a_nvals = NULL;
255 for ( nvals = 0; attr->a_vals[nvals].bv_val; nvals++ )
258 attr->a_nvals = (struct berval*)ch_malloc((nvals+1)*sizeof(struct berval));
260 if ( attr->a_desc->ad_type->sat_equality &&
261 attr->a_desc->ad_type->sat_equality->smr_normalize )
263 for ( i = 0; i < nvals; i++ ) {
264 rc = attr->a_desc->ad_type->sat_equality->smr_normalize(
265 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
266 attr->a_desc->ad_type->sat_syntax,
267 attr->a_desc->ad_type->sat_equality,
268 &attr->a_vals[i], &attr->a_nvals[i], NULL );
271 LDAP_LOG( OPERATION, DETAIL1,
272 "Error in normalizing attribute %s value %d (%d)\n",
273 attr->a_desc->ad_cname.bv_val, i, rc );
275 Debug( LDAP_DEBUG_ANY,
276 "Error in normalizing attribute %s value %d (%d)\n",
277 attr->a_desc->ad_cname.bv_val, i, rc );
283 for ( i = 0; i < nvals; i++ ) {
284 ber_dupbv( &attr->a_nvals[i], &attr->a_vals[i] );
288 attr->a_nvals[i].bv_val = NULL;
289 attr->a_nvals[i].bv_len = 0;