3 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1995 Regents of the University of Michigan.
10 * Redistribution and use in source and binary forms are permitted
11 * provided that this notice is preserved and that due credit is given
12 * to the University of Michigan at Ann Arbor. The name of the University
13 * may not be used to endorse or promote products derived from this
14 * software without specific prior written permission. This software
15 * is provided ``as is'' without express or implied warranty.
21 #include <ac/string.h>
23 #include <ac/socket.h>
30 static Slapi_PBlock *initAddPlugin( Backend *be, Connection *conn, Operation *op,
31 struct berval *dn, Entry *e, int manageDSAit );
32 static int doPreAddPluginFNs( Backend *be, Slapi_PBlock *pb );
33 static void doPostAddPluginFNs( Backend *be, Slapi_PBlock *pb );
34 #endif /* LDAP_SLAPI */
37 do_add( Connection *conn, Operation *op )
39 BerElement *ber = op->o_ber;
41 struct berval dn = { 0, NULL };
46 Modifications *modlist = NULL;
47 Modifications **modtail = &modlist;
50 int rc = LDAP_SUCCESS;
53 Slapi_PBlock *pb = NULL;
54 #endif /* LDAP_SLAPI */
57 LDAP_LOG( OPERATION, ENTRY, "do_add: conn %d enter\n", conn->c_connid,0,0 );
59 Debug( LDAP_DEBUG_TRACE, "do_add\n", 0, 0, 0 );
62 * Parse the add request. It looks like this:
64 * AddRequest := [APPLICATION 14] SEQUENCE {
65 * name DistinguishedName,
66 * attrs SEQUENCE OF SEQUENCE {
68 * values SET OF AttributeValue
74 if ( ber_scanf( ber, "{m", /*}*/ &dn ) == LBER_ERROR ) {
76 LDAP_LOG( OPERATION, ERR,
77 "do_add: conn %d ber_scanf failed\n", conn->c_connid,0,0 );
79 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
81 send_ldap_disconnect( conn, op,
82 LDAP_PROTOCOL_ERROR, "decoding error" );
86 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
88 rc = dnPrettyNormal( NULL, &dn, &e->e_name, &e->e_nname );
90 if( rc != LDAP_SUCCESS ) {
92 LDAP_LOG( OPERATION, ERR,
93 "do_add: conn %d invalid dn (%s)\n", conn->c_connid, dn.bv_val, 0 );
95 Debug( LDAP_DEBUG_ANY, "do_add: invalid dn (%s)\n", dn.bv_val, 0, 0 );
97 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
98 "invalid DN", NULL, NULL );
103 LDAP_LOG( OPERATION, ARGS,
104 "do_add: conn %d dn (%s)\n", conn->c_connid, e->e_dn, 0 );
106 Debug( LDAP_DEBUG_ARGS, "do_add: dn (%s)\n", e->e_dn, 0, 0 );
110 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
111 tag = ber_next_element( ber, &len, last ) )
117 tmp.sml_nvalues = NULL;
120 rtag = ber_scanf( ber, "{m{W}}", &tmp.sml_type, &tmp.sml_values );
122 if ( rtag == LBER_ERROR ) {
124 LDAP_LOG( OPERATION, ERR,
125 "do_add: conn %d decoding error \n", conn->c_connid, 0, 0 );
127 Debug( LDAP_DEBUG_ANY, "do_add: decoding error\n", 0, 0, 0 );
129 send_ldap_disconnect( conn, op,
130 LDAP_PROTOCOL_ERROR, "decoding error" );
135 if ( tmp.sml_values == NULL ) {
137 LDAP_LOG( OPERATION, INFO,
138 "do_add: conn %d no values for type %s\n",
139 conn->c_connid, tmp.sml_type.bv_val, 0 );
141 Debug( LDAP_DEBUG_ANY, "no values for type %s\n",
142 tmp.sml_type.bv_val, 0, 0 );
144 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
145 NULL, "no values for attribute type", NULL, NULL );
149 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
150 mod->sml_op = LDAP_MOD_ADD;
151 mod->sml_next = NULL;
152 mod->sml_desc = NULL;
153 mod->sml_type = tmp.sml_type;
154 mod->sml_values = tmp.sml_values;
156 mod->sml_nvalues = NULL;
160 modtail = &mod->sml_next;
163 if ( ber_scanf( ber, /*{*/ "}") == LBER_ERROR ) {
165 LDAP_LOG( OPERATION, ERR,
166 "do_add: conn %d ber_scanf failed\n", conn->c_connid, 0, 0 );
168 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
170 send_ldap_disconnect( conn, op,
171 LDAP_PROTOCOL_ERROR, "decoding error" );
176 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
178 LDAP_LOG( OPERATION, INFO,
179 "do_add: conn %d get_ctrls failed\n", conn->c_connid, 0, 0 );
181 Debug( LDAP_DEBUG_ANY, "do_add: get_ctrls failed\n", 0, 0, 0 );
186 if ( modlist == NULL ) {
187 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
188 NULL, "no attributes provided", NULL, NULL );
192 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu ADD dn=\"%s\"\n",
193 op->o_connid, op->o_opid, e->e_dn, 0, 0 );
195 if( e->e_nname.bv_len == 0 ) {
196 /* protocolError may be a more appropriate error */
197 send_ldap_result( conn, op, rc = LDAP_ALREADY_EXISTS,
198 NULL, "root DSE already exists",
202 } else if ( bvmatch( &e->e_nname, &global_schemandn ) ) {
203 send_ldap_result( conn, op, rc = LDAP_ALREADY_EXISTS,
204 NULL, "subschema subentry already exists",
209 manageDSAit = get_manageDSAit( op );
212 * We could be serving multiple database backends. Select the
213 * appropriate one, or send a referral to our "referral server"
214 * if we don't hold it.
216 be = select_backend( &e->e_nname, manageDSAit, 0 );
218 BerVarray ref = referral_rewrite( default_referral,
219 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
221 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
222 NULL, NULL, ref ? ref : default_referral, NULL );
224 if ( ref ) ber_bvarray_free( ref );
228 /* check restrictions */
229 rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
230 if( rc != LDAP_SUCCESS ) {
231 send_ldap_result( conn, op, rc,
232 NULL, text, NULL, NULL );
236 /* check for referrals */
237 rc = backend_check_referrals( be, conn, op, &e->e_name, &e->e_nname );
238 if ( rc != LDAP_SUCCESS ) {
243 pb = initAddPlugin( be, conn, op, &dn, e, manageDSAit );
244 #endif /* LDAP_SLAPI */
247 * do the add if 1 && (2 || 3)
248 * 1) there is an add function implemented in this backend;
249 * 2) this backend is master for what it holds;
250 * 3) it's a replica and the dn supplied is the updatedn.
253 /* do the update here */
254 int repl_user = be_isupdate(be, &op->o_ndn );
255 #ifndef SLAPD_MULTIMASTER
256 if ( !be->be_update_ndn.bv_len || repl_user )
259 int update = be->be_update_ndn.bv_len;
260 char textbuf[SLAP_TEXT_BUFLEN];
261 size_t textlen = sizeof textbuf;
263 rc = slap_mods_check( modlist, update, &text,
266 if( rc != LDAP_SUCCESS ) {
267 send_ldap_result( conn, op, rc,
268 NULL, text, NULL, NULL );
273 for( modtail = &modlist;
275 modtail = &(*modtail)->sml_next )
277 assert( (*modtail)->sml_op == LDAP_MOD_ADD );
278 assert( (*modtail)->sml_desc != NULL );
280 rc = slap_mods_opattrs( be, op, modlist, modtail, &text,
282 if( rc != LDAP_SUCCESS ) {
283 send_ldap_result( conn, op, rc,
284 NULL, text, NULL, NULL );
289 rc = slap_mods2entry( modlist, &e, repl_user, &text,
291 if( rc != LDAP_SUCCESS ) {
292 send_ldap_result( conn, op, rc,
293 NULL, text, NULL, NULL );
299 * Call the preoperation plugin here, because the entry
300 * will actually contain something.
302 rc = doPreAddPluginFNs( be, pb );
303 if ( rc != LDAP_SUCCESS ) {
304 /* plugin will have sent result */
307 #endif /* LDAP_SLAPI */
309 if ( (*be->be_add)( be, conn, op, e ) == 0 ) {
310 #ifdef SLAPD_MULTIMASTER
314 replog( be, op, &e->e_name, &e->e_nname, e );
316 be_entry_release_w( be, conn, op, e );
320 #ifndef SLAPD_MULTIMASTER
326 * SLAPI_ADD_ENTRY will be empty, but this may be acceptable
327 * on replicas (for now, it involves the minimum code intrusion).
329 rc = doPreAddPluginFNs( be, pb );
330 if ( rc != LDAP_SUCCESS ) {
331 /* plugin will have sent result */
334 #endif /* LDAP_SLAPI */
336 defref = be->be_update_refs
337 ? be->be_update_refs : default_referral;
338 ref = referral_rewrite( defref,
339 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
341 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
342 ref ? ref : defref, NULL );
344 if ( ref ) ber_bvarray_free( ref );
345 #endif /* SLAPD_MULTIMASTER */
349 rc = doPreAddPluginFNs( be, pb );
350 if ( rc != LDAP_SUCCESS ) {
351 /* plugin will have sent result */
356 LDAP_LOG( OPERATION, INFO,
357 "do_add: conn %d no backend support\n", conn->c_connid, 0, 0 );
359 Debug( LDAP_DEBUG_ARGS, " do_add: no backend support\n", 0, 0, 0 );
361 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
362 NULL, "operation not supported within namingContext", NULL, NULL );
366 doPostAddPluginFNs( be, pb );
367 #endif /* LDAP_SLAPI */
370 if( modlist != NULL ) {
371 slap_mods_free( modlist );
386 char *textbuf, size_t textlen )
388 Attribute **tail = &(*e)->e_attrs;
389 assert( *tail == NULL );
393 for( ; mods != NULL; mods = mods->sml_next ) {
396 assert( mods->sml_op == LDAP_MOD_ADD );
397 assert( mods->sml_desc != NULL );
399 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
402 #define SLURPD_FRIENDLY
403 #ifdef SLURPD_FRIENDLY
407 snprintf( textbuf, textlen,
408 "attribute '%s' provided more than once",
409 mods->sml_desc->ad_cname.bv_val );
410 return LDAP_TYPE_OR_VALUE_EXISTS;
413 for( i=0; attr->a_vals[i].bv_val; i++ ) {
416 for( j=0; mods->sml_values[j].bv_val; j++ ) {
421 attr->a_vals = ch_realloc( attr->a_vals,
422 sizeof( struct berval ) * (i+j) );
424 /* should check for duplicates */
426 AC_MEMCPY( &attr->a_vals[i], mods->sml_values,
427 sizeof( struct berval ) * j );
429 /* trim the mods array */
430 ch_free( mods->sml_values );
431 mods->sml_values = NULL;
434 if( attr->a_nvals ) {
435 attr->a_nvals = ch_realloc( attr->a_nvals,
436 sizeof( struct berval ) * (i+j) );
438 AC_MEMCPY( &attr->a_nvals[i], mods->sml_nvalues,
439 sizeof( struct berval ) * j );
441 /* trim the mods array */
442 ch_free( mods->sml_nvalues );
443 mods->sml_nvalues = NULL;
449 snprintf( textbuf, textlen,
450 "attribute '%s' provided more than once",
451 mods->sml_desc->ad_cname.bv_val );
452 return LDAP_TYPE_OR_VALUE_EXISTS;
456 if( mods->sml_values[1].bv_val != NULL ) {
457 /* check for duplicates */
459 MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
461 /* check if the values we're adding already exist */
462 if( mr == NULL || !mr->smr_match ) {
463 for ( i = 0; mods->sml_bvalues[i].bv_val != NULL; i++ ) {
464 /* test asserted values against themselves */
465 for( j = 0; j < i; j++ ) {
466 if ( bvmatch( &mods->sml_bvalues[i],
467 &mods->sml_bvalues[j] ) ) {
468 /* value exists already */
469 snprintf( textbuf, textlen,
470 "%s: value #%d provided more than once",
471 mods->sml_desc->ad_cname.bv_val, j );
472 return LDAP_TYPE_OR_VALUE_EXISTS;
479 const char *text = NULL;
480 char textbuf[ SLAP_TEXT_BUFLEN ] = { '\0' };
482 rc = modify_check_duplicates( mods->sml_desc, mr,
483 NULL, mods->sml_bvalues, 0,
484 &text, textbuf, sizeof( textbuf ) );
486 if ( rc != LDAP_SUCCESS ) {
492 attr = ch_calloc( 1, sizeof(Attribute) );
494 /* move ad to attr structure */
495 attr->a_desc = mods->sml_desc;
496 mods->sml_desc = NULL;
498 /* move values to attr structure */
499 /* should check for duplicates */
500 attr->a_vals = mods->sml_values;
501 mods->sml_values = NULL;
504 attr->a_nvals = mods->sml_nvalues;
505 mods->sml_nvalues = NULL;
509 tail = &attr->a_next;
516 static Slapi_PBlock *initAddPlugin( Backend *be, Connection *conn, Operation *op,
517 struct berval *dn, Entry *e, int manageDSAit )
523 slapi_x_backend_set_pb( pb, be );
524 slapi_x_connection_set_pb( pb, conn );
525 slapi_x_operation_set_pb( pb, op );
527 slapi_pblock_set( pb, SLAPI_ADD_TARGET, (void *)dn->bv_val );
528 slapi_pblock_set( pb, SLAPI_ADD_ENTRY, (void *)e );
529 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
534 static int doPreAddPluginFNs( Backend *be, Slapi_PBlock *pb )
538 rc = doPluginFNs( be, SLAPI_PLUGIN_PRE_ADD_FN, pb );
541 * A preoperation plugin failure will abort the
545 LDAP_LOG( OPERATION, INFO, "do_add: add preoperation plugin failed\n",
548 Debug(LDAP_DEBUG_TRACE, "do_add: add preoperation plugin failed.\n",
550 if ( slapi_pblock_get( pb, SLAPI_RESULT_CODE, (void *)&rc ) != 0 )
560 static void doPostAddPluginFNs( Backend *be, Slapi_PBlock *pb )
564 rc = doPluginFNs( be, SLAPI_PLUGIN_POST_ADD_FN, pb );
567 LDAP_LOG( OPERATION, INFO, "do_add: add postoperation plugin failed\n",
570 Debug(LDAP_DEBUG_TRACE, "do_add: add preoperation plugin failed.\n",
575 #endif /* LDAP_SLAPI */