2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2005 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
15 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
16 * All rights reserved.
18 * Redistribution and use in source and binary forms are permitted
19 * provided that this notice is preserved and that due credit is given
20 * to the University of Michigan at Ann Arbor. The name of the University
21 * may not be used to endorse or promote products derived from this
22 * software without specific prior written permission. This software
23 * is provided ``as is'' without express or implied warranty.
29 #include <ac/string.h>
31 #include <ac/socket.h>
37 do_add( Operation *op, SlapReply *rs )
39 BerElement *ber = op->o_ber;
41 struct berval dn = BER_BVNULL;
44 Modifications *modlist = NULL;
45 Modifications **modtail = &modlist;
47 char textbuf[ SLAP_TEXT_BUFLEN ];
48 size_t textlen = sizeof( textbuf );
51 Debug( LDAP_DEBUG_TRACE, "do_add\n", 0, 0, 0 );
53 * Parse the add request. It looks like this:
55 * AddRequest := [APPLICATION 14] SEQUENCE {
56 * name DistinguishedName,
57 * attrs SEQUENCE OF SEQUENCE {
59 * values SET OF AttributeValue
65 if ( ber_scanf( ber, "{m", /*}*/ &dn ) == LBER_ERROR ) {
66 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
67 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
68 return SLAPD_DISCONNECT;
71 op->ora_e = (Entry *) ch_calloc( 1, sizeof(Entry) );
73 rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn,
76 if ( rs->sr_err != LDAP_SUCCESS ) {
77 Debug( LDAP_DEBUG_ANY, "do_add: invalid dn (%s)\n", dn.bv_val, 0, 0 );
78 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
82 ber_dupbv( &op->ora_e->e_name, &op->o_req_dn );
83 ber_dupbv( &op->ora_e->e_nname, &op->o_req_ndn );
85 Debug( LDAP_DEBUG_ARGS, "do_add: dn (%s)\n", op->ora_e->e_dn, 0, 0 );
88 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
89 tag = ber_next_element( ber, &len, last ) )
94 tmp.sml_nvalues = NULL;
96 rtag = ber_scanf( ber, "{m{W}}", &tmp.sml_type, &tmp.sml_values );
98 if ( rtag == LBER_ERROR ) {
99 Debug( LDAP_DEBUG_ANY, "do_add: decoding error\n", 0, 0, 0 );
100 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
101 rs->sr_err = SLAPD_DISCONNECT;
105 if ( tmp.sml_values == NULL ) {
106 Debug( LDAP_DEBUG_ANY, "no values for type %s\n",
107 tmp.sml_type.bv_val, 0, 0 );
108 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
109 "no values for attribute type" );
113 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
114 mod->sml_op = LDAP_MOD_ADD;
116 mod->sml_next = NULL;
117 mod->sml_desc = NULL;
118 mod->sml_type = tmp.sml_type;
119 mod->sml_values = tmp.sml_values;
120 mod->sml_nvalues = NULL;
123 modtail = &mod->sml_next;
126 if ( ber_scanf( ber, /*{*/ "}") == LBER_ERROR ) {
127 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
128 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
129 rs->sr_err = SLAPD_DISCONNECT;
133 if ( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
134 Debug( LDAP_DEBUG_ANY, "do_add: get_ctrls failed\n", 0, 0, 0 );
138 if ( modlist == NULL ) {
139 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
140 "no attributes provided" );
144 Statslog( LDAP_DEBUG_STATS, "%s ADD dn=\"%s\"\n",
145 op->o_log_prefix, op->ora_e->e_name.bv_val, 0, 0, 0 );
147 if ( dn_match( &op->ora_e->e_nname, &slap_empty_bv ) ) {
148 /* protocolError may be a more appropriate error */
149 send_ldap_error( op, rs, LDAP_ALREADY_EXISTS,
150 "root DSE already exists" );
153 } else if ( dn_match( &op->ora_e->e_nname, &frontendDB->be_schemandn ) ) {
154 send_ldap_error( op, rs, LDAP_ALREADY_EXISTS,
155 "subschema subentry already exists" );
159 rs->sr_err = slap_mods_check( modlist, &rs->sr_text,
160 textbuf, textlen, NULL );
162 if ( rs->sr_err != LDAP_SUCCESS ) {
163 send_ldap_result( op, rs );
167 /* temporary; remove if not invoking backend function */
168 op->ora_modlist = modlist;
170 /* call this so global overlays/SLAPI have access to ora_e */
171 rs->sr_err = slap_mods2entry( op->ora_modlist, &op->ora_e,
172 1, 0, &rs->sr_text, textbuf, textlen );
173 if ( rs->sr_err != LDAP_SUCCESS ) {
174 send_ldap_result( op, rs );
178 op->o_bd = frontendDB;
179 rc = frontendDB->be_add( op, rs );
181 if ( op->ora_e != NULL && op->o_private != NULL ) {
182 BackendDB *bd = op->o_bd;
184 op->o_bd = (BackendDB *)op->o_private;
185 op->o_private = NULL;
187 be_entry_release_w( op, op->ora_e );
191 op->o_private = NULL;
196 if ( modlist != NULL ) {
197 slap_mods_free( modlist, 0 );
200 if ( op->ora_e != NULL ) {
201 entry_free( op->ora_e );
203 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
204 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
210 fe_op_add( Operation *op, SlapReply *rs )
213 Modifications *modlist = op->ora_modlist;
214 Modifications **modtail = &modlist;
217 char textbuf[ SLAP_TEXT_BUFLEN ];
218 size_t textlen = sizeof( textbuf );
220 manageDSAit = get_manageDSAit( op );
223 * We could be serving multiple database backends. Select the
224 * appropriate one, or send a referral to our "referral server"
225 * if we don't hold it.
227 op->o_bd = select_backend( &op->ora_e->e_nname, manageDSAit, 1 );
228 if ( op->o_bd == NULL ) {
229 rs->sr_ref = referral_rewrite( default_referral,
230 NULL, &op->ora_e->e_name, LDAP_SCOPE_DEFAULT );
231 if ( !rs->sr_ref ) rs->sr_ref = default_referral;
233 rs->sr_err = LDAP_REFERRAL;
234 op->o_bd = frontendDB;
235 send_ldap_result( op, rs );
238 if ( rs->sr_ref != default_referral ) {
239 ber_bvarray_free( rs->sr_ref );
242 op->o_bd = frontendDB;
243 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
244 "no global superior knowledge" );
250 /* If we've got a glued backend, check the real backend */
252 if ( SLAP_GLUE_INSTANCE( op->o_bd )) {
253 op->o_bd = select_backend( &op->ora_e->e_nname, manageDSAit, 0 );
256 /* check restrictions */
257 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
258 send_ldap_result( op, rs );
262 /* check for referrals */
263 if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
267 rs->sr_err = slap_mods_obsolete_check( op, modlist,
268 &rs->sr_text, textbuf, textlen );
270 if ( rs->sr_err != LDAP_SUCCESS ) {
271 send_ldap_result( op, rs );
276 * do the add if 1 && (2 || 3)
277 * 1) there is an add function implemented in this backend;
278 * 2) this backend is master for what it holds;
279 * 3) it's a replica and the dn supplied is the updatedn.
281 if ( op->o_bd->be_add ) {
282 /* do the update here */
283 int repl_user = be_isupdate( op );
284 #ifndef SLAPD_MULTIMASTER
285 if ( !SLAP_SHADOW(op->o_bd) || repl_user )
288 int update = !BER_BVISEMPTY( &op->o_bd->be_update_ndn );
289 slap_callback cb = { NULL, slap_replog_cb, NULL, NULL };
294 rs->sr_err = slap_mods_no_user_mod_check( op, modlist,
295 &rs->sr_text, textbuf, textlen );
297 if ( rs->sr_err != LDAP_SUCCESS ) {
298 send_ldap_result( op, rs );
304 /* go to the last mod */
305 for ( modtail = &modlist;
307 modtail = &(*modtail)->sml_next )
309 assert( (*modtail)->sml_op == LDAP_MOD_ADD );
310 assert( (*modtail)->sml_desc != NULL );
314 /* check for duplicate values */
315 rs->sr_err = slap_mods_no_repl_user_mod_check( op,
316 modlist, &rs->sr_text, textbuf, textlen );
317 if ( rs->sr_err != LDAP_SUCCESS ) {
318 send_ldap_result( op, rs );
322 rs->sr_err = slap_mods2entry( *modtail, &op->ora_e,
323 0, 0, &rs->sr_text, textbuf, textlen );
324 if ( rs->sr_err != LDAP_SUCCESS ) {
325 send_ldap_result( op, rs );
330 #ifdef SLAPD_MULTIMASTER
334 cb.sc_next = op->o_callback;
335 op->o_callback = &cb;
337 rc = op->o_bd->be_add( op, rs );
338 if ( rc == LDAP_SUCCESS ) {
339 /* NOTE: be_entry_release_w() is
340 * called by do_add(), so that global
341 * overlays on the way back can
342 * at least read the entry */
343 op->o_private = op->o_bd;
346 #ifndef SLAPD_MULTIMASTER
348 BerVarray defref = NULL;
350 defref = op->o_bd->be_update_refs
351 ? op->o_bd->be_update_refs : default_referral;
353 if ( defref != NULL ) {
354 rs->sr_ref = referral_rewrite( defref,
355 NULL, &op->ora_e->e_name, LDAP_SCOPE_DEFAULT );
356 if ( rs->sr_ref == NULL ) rs->sr_ref = defref;
357 rs->sr_err = LDAP_REFERRAL;
358 if (!rs->sr_ref) rs->sr_ref = default_referral;
359 send_ldap_result( op, rs );
361 if ( rs->sr_ref != default_referral ) {
362 ber_bvarray_free( rs->sr_ref );
365 send_ldap_error( op, rs,
366 LDAP_UNWILLING_TO_PERFORM,
367 "shadow context; no update referral" );
369 #endif /* SLAPD_MULTIMASTER */
372 Debug( LDAP_DEBUG_ARGS, " do_add: no backend support\n", 0, 0, 0 );
373 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
374 "operation not supported within namingContext" );
388 char *textbuf, size_t textlen )
393 assert( (*e)->e_attrs == NULL );
396 for ( tail = &(*e)->e_attrs; *tail != NULL; tail = &(*tail)->a_next )
401 for( ; mods != NULL; mods = mods->sml_next ) {
404 assert( mods->sml_desc != NULL );
406 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
409 #define SLURPD_FRIENDLY
410 #ifdef SLURPD_FRIENDLY
415 * This check allows overlays to override operational
416 * attributes by setting them directly in the entry.
417 * We assume slap_mods_no_user_mod_check() was called
418 * with the user modifications.
424 for( i=0; attr->a_vals[i].bv_val; i++ ) {
427 for( j=0; mods->sml_values[j].bv_val; j++ ) {
432 attr->a_vals = ch_realloc( attr->a_vals,
433 sizeof( struct berval ) * (i+j) );
435 /* should check for duplicates */
438 for ( j = 0; mods->sml_values[j].bv_val; j++ ) {
439 ber_dupbv( &attr->a_vals[i+j], &mods->sml_values[j] );
441 BER_BVZERO( &attr->a_vals[i+j] );
444 AC_MEMCPY( &attr->a_vals[i], mods->sml_values,
445 sizeof( struct berval ) * j );
448 if( mods->sml_nvalues ) {
449 attr->a_nvals = ch_realloc( attr->a_nvals,
450 sizeof( struct berval ) * (i+j) );
452 for ( j = 0; mods->sml_nvalues[j].bv_val; j++ ) {
453 ber_dupbv( &attr->a_nvals[i+j], &mods->sml_nvalues[j] );
455 BER_BVZERO( &attr->a_nvals[i+j] );
457 AC_MEMCPY( &attr->a_nvals[i], mods->sml_nvalues,
458 sizeof( struct berval ) * j );
461 attr->a_nvals = attr->a_vals;
466 snprintf( textbuf, textlen,
467 "attribute '%s' provided more than once",
468 mods->sml_desc->ad_cname.bv_val );
469 return LDAP_TYPE_OR_VALUE_EXISTS;
473 if( mods->sml_values[1].bv_val != NULL ) {
474 /* check for duplicates */
476 MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
478 for ( i = 1; mods->sml_values[i].bv_val != NULL; i++ ) {
479 /* test asserted values against themselves */
480 for( j = 0; j < i; j++ ) {
481 rc = ordered_value_match( &match, mods->sml_desc, mr,
483 | SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX
484 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
485 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
487 ? &mods->sml_nvalues[i]
488 : &mods->sml_values[i],
490 ? &mods->sml_nvalues[j]
491 : &mods->sml_values[j],
494 if ( rc == LDAP_SUCCESS && match == 0 ) {
495 /* value exists already */
496 snprintf( textbuf, textlen,
497 "%s: value #%d provided more than once",
498 mods->sml_desc->ad_cname.bv_val, j );
499 return LDAP_TYPE_OR_VALUE_EXISTS;
501 } else if ( rc != LDAP_SUCCESS ) {
508 attr = ch_calloc( 1, sizeof(Attribute) );
510 /* move ad to attr structure */
511 attr->a_desc = mods->sml_desc;
513 /* move values to attr structure */
514 /* should check for duplicates */
517 for ( i = 0; mods->sml_values[i].bv_val; i++ ) /* EMPTY */;
518 attr->a_vals = (BerVarray) ch_calloc( i+1, sizeof( BerValue ));
519 for ( i = 0; mods->sml_values[i].bv_val; i++ ) {
520 ber_dupbv( &attr->a_vals[i], &mods->sml_values[i] );
522 BER_BVZERO( &attr->a_vals[i] );
524 attr->a_vals = mods->sml_values;
527 if ( mods->sml_nvalues ) {
530 for ( i = 0; mods->sml_nvalues[i].bv_val; i++ ) /* EMPTY */;
531 attr->a_nvals = (BerVarray) ch_calloc( i+1, sizeof( BerValue ));
532 for ( i = 0; mods->sml_nvalues[i].bv_val; i++ ) {
533 ber_dupbv( &attr->a_nvals[i], &mods->sml_nvalues[i] );
535 BER_BVZERO( &attr->a_nvals[i] );
537 attr->a_nvals = mods->sml_nvalues;
540 attr->a_nvals = attr->a_vals;
544 tail = &attr->a_next;
555 Modifications **mods,
557 char *textbuf, size_t textlen )
559 Modifications *modhead = NULL;
561 Modifications **modtail = &modhead;
563 AttributeDescription *a_new_desc;
568 while ( a_new != NULL ) {
569 a_new_desc = a_new->a_desc;
570 mod = (Modifications *) malloc( sizeof( Modifications ));
572 mod->sml_op = LDAP_MOD_REPLACE;
575 mod->sml_type = a_new_desc->ad_cname;
577 for ( count = 0; a_new->a_vals[count].bv_val; count++ ) /* EMPTY */;
579 mod->sml_values = (struct berval*) malloc(
580 (count+1) * sizeof( struct berval) );
582 /* see slap_mods_check() comments...
583 * if a_vals == a_nvals, there is no normalizer.
584 * in this case, mod->sml_nvalues must be left NULL.
586 if ( a_new->a_vals != a_new->a_nvals ) {
587 mod->sml_nvalues = (struct berval*) malloc(
588 (count+1) * sizeof( struct berval) );
590 mod->sml_nvalues = NULL;
593 for ( i = 0; i < count; i++ ) {
594 ber_dupbv(mod->sml_values+i, a_new->a_vals+i);
595 if ( mod->sml_nvalues ) {
596 ber_dupbv( mod->sml_nvalues+i, a_new->a_nvals+i );
600 mod->sml_values[count].bv_val = NULL;
601 mod->sml_values[count].bv_len = 0;
603 if ( mod->sml_nvalues ) {
604 mod->sml_nvalues[count].bv_val = NULL;
605 mod->sml_nvalues[count].bv_len = 0;
608 mod->sml_desc = a_new_desc;
611 modtail = &mod->sml_next;
612 a_new = a_new->a_next;
620 int slap_add_opattrs(
627 struct berval name, timestamp, csn = BER_BVNULL;
628 struct berval nname, tmp;
629 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
630 char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
633 a = attr_find( op->ora_e->e_attrs,
634 slap_schema.si_ad_structuralObjectClass );
640 oc = attr_find( op->ora_e->e_attrs, slap_schema.si_ad_objectClass );
642 rc = structural_class( oc->a_vals, &tmp, NULL, text,
644 if( rc != LDAP_SUCCESS ) return rc;
646 attr_merge_one( op->ora_e, slap_schema.si_ad_structuralObjectClass,
651 if ( SLAP_LASTMOD( op->o_bd ) ) {
653 timestamp.bv_val = timebuf;
654 if ( BER_BVISEMPTY( &op->o_csn )) {
655 if ( SLAP_SHADOW( op->o_bd ))
658 csn.bv_len = sizeof(csnbuf);
659 slap_get_csn( op, &csn, manage_ctxcsn );
663 ptr = strchr( csn.bv_val, '#' );
665 timestamp.bv_len = ptr - csn.bv_val;
666 if ( timestamp.bv_len >= sizeof(timebuf) )
667 timestamp.bv_len = sizeof(timebuf) - 1;
668 strncpy( timebuf, csn.bv_val, timestamp.bv_len );
669 timebuf[timestamp.bv_len] = '\0';
671 time_t now = slap_get_time();
673 timestamp.bv_len = sizeof(timebuf);
675 slap_timestamp( &now, ×tamp );
678 if ( BER_BVISEMPTY( &op->o_dn ) ) {
679 BER_BVSTR( &name, SLAPD_ANONYMOUS );
686 a = attr_find( op->ora_e->e_attrs,
687 slap_schema.si_ad_entryUUID );
689 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
691 tmp.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
692 tmp.bv_val = uuidbuf;
694 attr_merge_normalize_one( op->ora_e,
695 slap_schema.si_ad_entryUUID, &tmp, op->o_tmpmemctx );
698 a = attr_find( op->ora_e->e_attrs,
699 slap_schema.si_ad_creatorsName );
701 attr_merge_one( op->ora_e,
702 slap_schema.si_ad_creatorsName, &name, &nname );
705 a = attr_find( op->ora_e->e_attrs,
706 slap_schema.si_ad_createTimestamp );
708 attr_merge_one( op->ora_e,
709 slap_schema.si_ad_createTimestamp, ×tamp, NULL );
712 a = attr_find( op->ora_e->e_attrs,
713 slap_schema.si_ad_entryCSN );
715 attr_merge_one( op->ora_e,
716 slap_schema.si_ad_entryCSN, &csn, NULL );
719 a = attr_find( op->ora_e->e_attrs,
720 slap_schema.si_ad_modifiersName );
722 attr_merge_one( op->ora_e,
723 slap_schema.si_ad_modifiersName, &name, &nname );
726 a = attr_find( op->ora_e->e_attrs,
727 slap_schema.si_ad_modifyTimestamp );
729 attr_merge_one( op->ora_e,
730 slap_schema.si_ad_modifyTimestamp, ×tamp, NULL );