Home | History | Annotate | Line # | Download | only in back-wt
add.c revision 1.1.1.2
      1 /*	$NetBSD: add.c,v 1.1.1.2 2025/09/05 21:09:50 christos Exp $	*/
      2 
      3 /* OpenLDAP WiredTiger backend */
      4 /* $OpenLDAP$ */
      5 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
      6  *
      7  * Copyright 2002-2024 The OpenLDAP Foundation.
      8  * All rights reserved.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted only as authorized by the OpenLDAP
     12  * Public License.
     13  *
     14  * A copy of this license is available in the file LICENSE in the
     15  * top-level directory of the distribution or, alternatively, at
     16  * <http://www.OpenLDAP.org/license.html>.
     17  */
     18 /* ACKNOWLEDGEMENTS:
     19  * This work was developed by HAMANO Tsukasa <hamano (at) osstech.co.jp>
     20  * based on back-bdb for inclusion in OpenLDAP Software.
     21  * WiredTiger is a product of MongoDB Inc.
     22  */
     23 
     24 #include <sys/cdefs.h>
     25 __RCSID("$NetBSD: add.c,v 1.1.1.2 2025/09/05 21:09:50 christos Exp $");
     26 
     27 #include "portable.h"
     28 
     29 #include <stdio.h>
     30 #include "back-wt.h"
     31 #include "slap-config.h"
     32 
     33 int
     34 wt_add( Operation *op, SlapReply *rs )
     35 {
     36     struct wt_info *wi = (struct wt_info *) op->o_bd->be_private;
     37 	struct berval   pdn;
     38 	char textbuf[SLAP_TEXT_BUFLEN];
     39 	size_t textlen = sizeof textbuf;
     40 	AttributeDescription *children = slap_schema.si_ad_children;
     41 	AttributeDescription *entry = slap_schema.si_ad_entry;
     42 	ID eid = NOID;
     43 	LDAPControl **postread_ctrl = NULL;
     44 	LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
     45 	int num_ctrls = 0;
     46 	wt_ctx *wc;
     47 	Entry *e = NULL;
     48 	Entry *p = NULL;
     49 	ID pid = NOID;
     50 	int rc;
     51 
     52     Debug( LDAP_DEBUG_ARGS, "==> wt_add: %s\n", op->ora_e->e_name.bv_val );
     53 
     54 	ctrls[num_ctrls] = 0;
     55 
     56 	/* check entry's schema */
     57 	rs->sr_err = entry_schema_check(
     58 		op, op->ora_e, NULL,
     59 		get_relax(op), 1, NULL, &rs->sr_text, textbuf, textlen );
     60     if ( rs->sr_err != LDAP_SUCCESS ) {
     61         Debug( LDAP_DEBUG_TRACE,
     62 			   "wt_add: entry failed schema check: %s (%d)\n",
     63 			   rs->sr_text, rs->sr_err );
     64         goto return_results;
     65     }
     66 
     67     /* add opattrs to shadow as well, only missing attrs will actually
     68      * be added; helps compatibility with older OL versions */
     69     rs->sr_err = slap_add_opattrs( op, &rs->sr_text, textbuf, textlen, 1 );
     70     if ( rs->sr_err != LDAP_SUCCESS ) {
     71         Debug( LDAP_DEBUG_TRACE,
     72 			   "wt_add: entry failed op attrs add: %s (%d)\n",
     73 			   rs->sr_text, rs->sr_err );
     74         goto return_results;
     75     }
     76 
     77     if ( get_assert( op ) &&
     78 		 ( test_filter( op, op->ora_e, get_assertion( op ))
     79 		   != LDAP_COMPARE_TRUE ))
     80     {
     81         rs->sr_err = LDAP_ASSERTION_FAILED;
     82         goto return_results;
     83     }
     84 
     85 	/* Not used
     86 	 * subentry = is_entry_subentry( op->ora_e );
     87 	 */
     88 
     89     /*
     90      * Get the parent dn and see if the corresponding entry exists.
     91      */
     92     if ( be_issuffix( op->o_bd, &op->ora_e->e_nname ) ) {
     93         pdn = slap_empty_bv;
     94     } else {
     95         dnParent( &op->ora_e->e_nname, &pdn );
     96     }
     97 
     98 	wc = wt_ctx_get(op, wi);
     99 	if( !wc ){
    100         Debug( LDAP_DEBUG_ANY, "wt_add: wt_ctx_get failed\n" );
    101 		rs->sr_err = LDAP_OTHER;
    102 		rs->sr_text = "internal error";
    103         send_ldap_result( op, rs );
    104         return rs->sr_err;
    105 	}
    106 
    107 	rc = wt_dn2entry(op->o_bd, wc, &op->o_req_ndn, &e);
    108 	switch( rc ) {
    109 	case 0:
    110 		rs->sr_err = LDAP_ALREADY_EXISTS;
    111 		goto return_results;
    112 		break;
    113 	case WT_NOTFOUND:
    114 		break;
    115 	default:
    116 		/* TODO: retry handling */
    117         Debug( LDAP_DEBUG_ANY,
    118 			   "wt_add: error at wt_dn2entry() rc=%d\n", rc );
    119 		rs->sr_err = LDAP_OTHER;
    120 		rs->sr_text = "internal error";
    121 		goto return_results;
    122 	}
    123 
    124     /* get parent entry */
    125 	rc = wt_dn2pentry(op->o_bd, wc, &op->o_req_ndn, &p);
    126 	switch( rc ){
    127 	case 0:
    128 	case WT_NOTFOUND:
    129 		break;
    130 	default:
    131         Debug( LDAP_DEBUG_ANY,
    132 			   "wt_add: error at wt_dn2pentry() rc=%d\n", rc );
    133 		rs->sr_err = LDAP_OTHER;
    134 		rs->sr_text = "internal error";
    135 		goto return_results;
    136 	}
    137 
    138 	if ( !p )
    139 		p = (Entry *)&slap_entry_root;
    140 
    141 	if ( !bvmatch( &pdn, &p->e_nname ) ) {
    142 		rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
    143 									   op->o_tmpmemctx );
    144 		if ( p != (Entry *)&slap_entry_root ) {
    145 			rs->sr_ref = is_entry_referral( p )
    146 				? get_entry_referrals( op, p )
    147 				: NULL;
    148 			wt_entry_return( p );
    149 		} else {
    150 			rs->sr_ref = NULL;
    151 		}
    152 		p = NULL;
    153         Debug( LDAP_DEBUG_TRACE, "wt_add: parent does not exist\n" );
    154         rs->sr_err = LDAP_REFERRAL;
    155         rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
    156         goto return_results;
    157 	}
    158 
    159 	rs->sr_err = access_allowed( op, p,
    160 								 children, NULL, ACL_WADD, NULL );
    161 	if ( ! rs->sr_err ) {
    162 		/*
    163 		if ( p != (Entry *)&slap_entry_root )
    164 			wt_entry_return( op, p );
    165 		*/
    166 		p = NULL;
    167 
    168 		Debug( LDAP_DEBUG_TRACE, "wt_add: no write access to parent\n" );
    169 		rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
    170 		rs->sr_text = "no write access to parent";
    171 		goto return_results;;
    172 	}
    173 
    174 	if ( p != (Entry *)&slap_entry_root ) {
    175 		if ( is_entry_subentry( p ) ) {
    176 			wt_entry_return( p );
    177 			p = NULL;
    178 			/* parent is a subentry, don't allow add */
    179 			Debug( LDAP_DEBUG_TRACE, "wt_add: parent is subentry\n" );
    180 			rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
    181 			rs->sr_text = "parent is a subentry";
    182 			goto return_results;;
    183 		}
    184 
    185 		if ( is_entry_alias( p ) ) {
    186 			wt_entry_return( p );
    187 			p = NULL;
    188 			/* parent is an alias, don't allow add */
    189 			Debug( LDAP_DEBUG_TRACE, "wt_add: parent is alias\n" );
    190 			rs->sr_err = LDAP_ALIAS_PROBLEM;
    191 			rs->sr_text = "parent is an alias";
    192 			goto return_results;;
    193 		}
    194 
    195 		if ( is_entry_referral( p ) ) {
    196 			BerVarray ref = get_entry_referrals( op, p );
    197 			/* parent is a referral, don't allow add */
    198 			rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
    199 										   op->o_tmpmemctx );
    200 			rs->sr_ref = referral_rewrite( ref, &p->e_name,
    201 										   &op->o_req_dn, LDAP_SCOPE_DEFAULT );
    202 			ber_bvarray_free( ref );
    203 			wt_entry_return( p );
    204 			p = NULL;
    205 			Debug( LDAP_DEBUG_TRACE, "wt_add: parent is referral\n" );
    206 
    207 			rs->sr_err = LDAP_REFERRAL;
    208 			rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
    209 			goto return_results;
    210 		}
    211 	}
    212 
    213 #if 0
    214 	if ( subentry ) {
    215 		/* FIXME: */
    216 		/* parent must be an administrative point of the required kind */
    217 	}
    218 #endif
    219 
    220 	/* free parent */
    221 	if ( p != (Entry *)&slap_entry_root ) {
    222 		pid = p->e_id;
    223 		if ( p->e_nname.bv_len ) {
    224 			struct berval ppdn;
    225 
    226 			/* ITS#5326: use parent's DN if differs from provided one */
    227 			dnParent( &op->ora_e->e_name, &ppdn );
    228 			if ( !dn_match( &p->e_name, &ppdn ) ) {
    229 				struct berval rdn;
    230 				struct berval newdn;
    231 
    232 				dnRdn( &op->ora_e->e_name, &rdn );
    233 
    234 				build_new_dn( &newdn, &p->e_name, &rdn, NULL );
    235 				if ( op->ora_e->e_name.bv_val != op->o_req_dn.bv_val )
    236 					ber_memfree( op->ora_e->e_name.bv_val );
    237 				op->ora_e->e_name = newdn;
    238 
    239 				/* FIXME: should check whether
    240                  * dnNormalize(newdn) == e->e_nname ... */
    241 			}
    242 		}
    243 
    244 		wt_entry_return( p );
    245 	}
    246 	p = NULL;
    247 
    248 	rs->sr_err = access_allowed( op, op->ora_e,
    249 								 entry, NULL, ACL_WADD, NULL );
    250 
    251 	if ( ! rs->sr_err ) {
    252 		Debug( LDAP_DEBUG_TRACE, "wt_add: no write access to entry\n" );
    253 		rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
    254 		rs->sr_text = "no write access to entry";
    255 		goto return_results;
    256 	}
    257 
    258 	/*
    259 	 * Check ACL for attribute write access
    260 	 */
    261 	if (!acl_check_modlist(op, op->ora_e, op->ora_modlist)) {
    262 		Debug( LDAP_DEBUG_TRACE, "wt_add: no write access to attribute\n" );
    263 		rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
    264 		rs->sr_text = "no write access to attribute";
    265 		goto return_results;
    266 	}
    267 
    268 	rc = wc->session->begin_transaction(wc->session, "isolation=read-uncommitted");
    269 	if( rc ) {
    270 		Debug( LDAP_DEBUG_TRACE, "wt_add: begin_transaction failed: %s (%d)\n",
    271 			   wiredtiger_strerror(rc), rc );
    272 		rs->sr_err = LDAP_OTHER;
    273 		rs->sr_text = "begin_transaction failed";
    274 		goto return_results;
    275 	}
    276 	Debug( LDAP_DEBUG_TRACE, "wt_add: session id: %p\n", wc->session );
    277 
    278 	wt_next_id( op->o_bd, &eid );
    279 	op->ora_e->e_id = eid;
    280 
    281 	rc = wt_dn2id_add( op, wc, pid, op->ora_e );
    282 	if( rc ){
    283 		Debug( LDAP_DEBUG_TRACE,
    284 			   "wt_add: dn2id_add failed: %s (%d)\n",
    285 			   wiredtiger_strerror(rc), rc );
    286 		switch( rc ) {
    287 		case WT_DUPLICATE_KEY:
    288 			rs->sr_err = LDAP_ALREADY_EXISTS;
    289 			break;
    290 		default:
    291 			rs->sr_err = LDAP_OTHER;
    292 		}
    293 		wc->session->rollback_transaction(wc->session, NULL);
    294 		goto return_results;
    295 	}
    296 
    297 	rc = wt_id2entry_add( op, wc, op->ora_e );
    298 	if ( rc ) {
    299 		Debug( LDAP_DEBUG_TRACE,
    300 			   "wt_add: id2entry_add failed: %s (%d)\n",
    301 			   wiredtiger_strerror(rc), rc );
    302 		if ( rc == LDAP_ADMINLIMIT_EXCEEDED ) {
    303 			rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
    304 			rs->sr_text = "entry is too big";
    305 		} else {
    306 			rs->sr_err = LDAP_OTHER;
    307 			rs->sr_text = "entry store failed";
    308 		}
    309 		wc->session->rollback_transaction(wc->session, NULL);
    310 		goto return_results;
    311 	}
    312 
    313 	/* add indices */
    314 	rc = wt_index_entry_add( op, wc, op->ora_e );
    315 	if ( rc ) {
    316 		Debug(LDAP_DEBUG_TRACE,
    317 			  "<== wt_add: index add failed: %s (%d)\n",
    318 			  wiredtiger_strerror(rc), rc );
    319 		rs->sr_err = LDAP_OTHER;
    320 		rs->sr_text = "index add failed";
    321 		wc->session->rollback_transaction(wc->session, NULL);
    322 		goto return_results;
    323 	}
    324 
    325 	rc = wc->session->commit_transaction(wc->session, NULL);
    326 	if( rc ) {
    327 		Debug( LDAP_DEBUG_TRACE,
    328 			   "<== wt_add: commit_transaction failed: %s (%d)\n",
    329 			   wiredtiger_strerror(rc), rc );
    330 		rs->sr_err = LDAP_OTHER;
    331 		rs->sr_text = "commit_transaction failed";
    332 		goto return_results;
    333 	}
    334 
    335 	rs->sr_err = LDAP_SUCCESS;
    336 
    337 	/* post-read */
    338 	if( op->o_postread ) {
    339 		if( postread_ctrl == NULL ) {
    340 			postread_ctrl = &ctrls[num_ctrls++];
    341 			ctrls[num_ctrls] = NULL;
    342 		}
    343 		if ( slap_read_controls( op, rs, op->ora_e,
    344 								 &slap_post_read_bv, postread_ctrl ) )
    345 		{
    346 			Debug( LDAP_DEBUG_TRACE, "<=- wt_add: post-read failed!\n" );
    347 			if ( op->o_postread & SLAP_CONTROL_CRITICAL ) {
    348 				/* FIXME: is it correct to abort
    349                  * operation if control fails? */
    350 				goto return_results;
    351 			}
    352 		}
    353 	}
    354 
    355 	Debug(LDAP_DEBUG_TRACE,
    356 		  "wt_add: added%s id=%08lx dn=\"%s\"\n",
    357 		  op->o_noop ? " (no-op)" : "",
    358 		  op->ora_e->e_id, op->ora_e->e_dn );
    359 
    360 return_results:
    361 	send_ldap_result( op, rs );
    362 
    363 	slap_graduate_commit_csn( op );
    364 
    365 	if( postread_ctrl != NULL && (*postread_ctrl) != NULL ) {
    366         slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
    367         slap_sl_free( *postread_ctrl, op->o_tmpmemctx );
    368     }
    369     return rs->sr_err;
    370 }
    371 
    372 /*
    373  * Local variables:
    374  * indent-tabs-mode: t
    375  * tab-width: 4
    376  * c-basic-offset: 4
    377  * End:
    378  */
    379