2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2017 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 );
53 Debug( LDAP_DEBUG_TRACE, "%s do_add\n",
54 op->o_log_prefix, 0, 0 );
57 * Parse the add request. It looks like this:
59 * AddRequest := [APPLICATION 14] SEQUENCE {
60 * name DistinguishedName,
61 * attrs SEQUENCE OF SEQUENCE {
63 * values SET OF AttributeValue
69 if ( ber_scanf( ber, "{m", /*}*/ &dn ) == LBER_ERROR ) {
70 Debug( LDAP_DEBUG_ANY, "%s do_add: ber_scanf failed\n",
71 op->o_log_prefix, 0, 0 );
72 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
73 return SLAPD_DISCONNECT;
76 Debug( LDAP_DEBUG_ARGS, "%s do_add: dn (%s)\n",
77 op->o_log_prefix, dn.bv_val, 0 );
80 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
81 tag = ber_next_element( ber, &len, last ) )
86 tmp.sml_nvalues = NULL;
88 rtag = ber_scanf( ber, "{m{W}}", &tmp.sml_type, &tmp.sml_values );
90 if ( rtag == LBER_ERROR ) {
91 Debug( LDAP_DEBUG_ANY, "%s do_add: decoding error\n",
92 op->o_log_prefix, 0, 0 );
93 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
94 rs->sr_err = SLAPD_DISCONNECT;
98 if ( tmp.sml_values == NULL ) {
99 Debug( LDAP_DEBUG_ANY, "%s do_add: no values for type %s\n",
100 op->o_log_prefix, tmp.sml_type.bv_val, 0 );
101 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
102 "no values for attribute type" );
106 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
107 mod->sml_op = LDAP_MOD_ADD;
109 mod->sml_next = NULL;
110 mod->sml_desc = NULL;
111 mod->sml_type = tmp.sml_type;
112 mod->sml_values = tmp.sml_values;
113 mod->sml_nvalues = NULL;
116 modtail = &mod->sml_next;
119 if ( ber_scanf( ber, /*{*/ "}") == LBER_ERROR ) {
120 Debug( LDAP_DEBUG_ANY, "%s do_add: ber_scanf failed\n",
121 op->o_log_prefix, 0, 0 );
122 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
123 rs->sr_err = SLAPD_DISCONNECT;
127 if ( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
128 Debug( LDAP_DEBUG_ANY, "%s do_add: get_ctrls failed\n",
129 op->o_log_prefix, 0, 0 );
133 rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn,
136 if ( rs->sr_err != LDAP_SUCCESS ) {
137 Debug( LDAP_DEBUG_ANY, "%s do_add: invalid dn (%s)\n",
138 op->o_log_prefix, dn.bv_val, 0 );
139 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
143 op->ora_e = entry_alloc();
144 ber_dupbv( &op->ora_e->e_name, &op->o_req_dn );
145 ber_dupbv( &op->ora_e->e_nname, &op->o_req_ndn );
147 Statslog( LDAP_DEBUG_STATS, "%s ADD dn=\"%s\"\n",
148 op->o_log_prefix, op->o_req_dn.bv_val, 0, 0, 0 );
150 if ( modlist == NULL ) {
151 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
152 "no attributes provided" );
156 if ( dn_match( &op->ora_e->e_nname, &slap_empty_bv ) ) {
157 /* protocolError may be a more appropriate error */
158 send_ldap_error( op, rs, LDAP_ALREADY_EXISTS,
159 "root DSE already exists" );
162 } else if ( dn_match( &op->ora_e->e_nname, &frontendDB->be_schemandn ) ) {
163 send_ldap_error( op, rs, LDAP_ALREADY_EXISTS,
164 "subschema subentry already exists" );
168 rs->sr_err = slap_mods_check( op, modlist, &rs->sr_text,
169 textbuf, textlen, NULL );
171 if ( rs->sr_err != LDAP_SUCCESS ) {
172 send_ldap_result( op, rs );
176 /* temporary; remove if not invoking backend function */
177 op->ora_modlist = modlist;
179 /* call this so global overlays/SLAPI have access to ora_e */
180 rs->sr_err = slap_mods2entry( op->ora_modlist, &op->ora_e,
181 1, 0, &rs->sr_text, textbuf, textlen );
182 if ( rs->sr_err != LDAP_SUCCESS ) {
183 send_ldap_result( op, rs );
189 oex.oe.oe_key = (void *)do_add;
191 LDAP_SLIST_INSERT_HEAD(&op->o_extra, &oex.oe, oe_next);
193 op->o_bd = frontendDB;
194 rc = frontendDB->be_add( op, rs );
195 LDAP_SLIST_REMOVE(&op->o_extra, &oex.oe, OpExtra, oe_next);
197 if ( rc == SLAPD_ASYNCOP ) {
203 if ( rc == LDAP_X_TXN_SPECIFY_OKAY ) {
209 if ( op->ora_e != NULL && oex.oe_db != NULL ) {
210 BackendDB *bd = op->o_bd;
212 op->o_bd = oex.oe_db;
214 be_entry_release_w( op, op->ora_e );
222 if ( modlist != NULL ) {
223 /* in case of error, free the values as well */
224 slap_mods_free( modlist, freevals );
227 if ( op->ora_e != NULL ) {
228 entry_free( op->ora_e );
230 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
231 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
237 fe_op_add( Operation *op, SlapReply *rs )
239 Modifications **modtail = &op->ora_modlist;
241 BackendDB *op_be, *bd = op->o_bd;
242 char textbuf[ SLAP_TEXT_BUFLEN ];
243 size_t textlen = sizeof( textbuf );
246 * We could be serving multiple database backends. Select the
247 * appropriate one, or send a referral to our "referral server"
248 * if we don't hold it.
250 op->o_bd = select_backend( &op->ora_e->e_nname, 1 );
251 if ( op->o_bd == NULL ) {
253 rs->sr_ref = referral_rewrite( default_referral,
254 NULL, &op->ora_e->e_name, LDAP_SCOPE_DEFAULT );
255 if ( !rs->sr_ref ) rs->sr_ref = default_referral;
257 rs->sr_err = LDAP_REFERRAL;
258 send_ldap_result( op, rs );
260 if ( rs->sr_ref != default_referral ) {
261 ber_bvarray_free( rs->sr_ref );
264 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
265 "no global superior knowledge" );
270 /* If we've got a glued backend, check the real backend */
272 if ( SLAP_GLUE_INSTANCE( op->o_bd )) {
273 op->o_bd = select_backend( &op->ora_e->e_nname, 0 );
276 /* check restrictions */
277 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
278 send_ldap_result( op, rs );
282 /* check for referrals */
283 if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
287 rs->sr_err = slap_mods_obsolete_check( op, op->ora_modlist,
288 &rs->sr_text, textbuf, textlen );
290 if ( rs->sr_err != LDAP_SUCCESS ) {
291 send_ldap_result( op, rs );
296 * do the add if 1 && (2 || 3)
297 * 1) there is an add function implemented in this backend;
298 * 2) this backend is master for what it holds;
299 * 3) it's a replica and the dn supplied is the updatedn.
301 if ( op->o_bd->be_add ) {
302 /* do the update here */
303 int repl_user = be_isupdate( op );
304 if ( !SLAP_SINGLE_SHADOW(op->o_bd) || repl_user ) {
305 int update = !BER_BVISEMPTY( &op->o_bd->be_update_ndn );
310 rs->sr_err = slap_mods_no_user_mod_check( op, op->ora_modlist,
311 &rs->sr_text, textbuf, textlen );
313 if ( rs->sr_err != LDAP_SUCCESS ) {
314 send_ldap_result( op, rs );
320 /* go to the last mod */
321 for ( modtail = &op->ora_modlist;
323 modtail = &(*modtail)->sml_next )
325 assert( (*modtail)->sml_op == LDAP_MOD_ADD );
326 assert( (*modtail)->sml_desc != NULL );
330 /* check for unmodifiable attributes */
331 rs->sr_err = slap_mods_no_repl_user_mod_check( op,
332 op->ora_modlist, &rs->sr_text, textbuf, textlen );
333 if ( rs->sr_err != LDAP_SUCCESS ) {
334 send_ldap_result( op, rs );
339 rc = op->o_bd->be_add( op, rs );
340 if ( rc == LDAP_SUCCESS ) {
342 /* NOTE: be_entry_release_w() is
343 * called by do_add(), so that global
344 * overlays on the way back can
345 * at least read the entry */
346 LDAP_SLIST_FOREACH(oex, &op->o_extra, oe_next) {
347 if ( oex->oe_key == (void *)do_add ) {
348 ((OpExtraDB *)oex)->oe_db = op->o_bd;
355 BerVarray defref = NULL;
357 defref = op->o_bd->be_update_refs
358 ? op->o_bd->be_update_refs : default_referral;
360 if ( defref != NULL ) {
361 rs->sr_ref = referral_rewrite( defref,
362 NULL, &op->ora_e->e_name, LDAP_SCOPE_DEFAULT );
363 if ( rs->sr_ref == NULL ) rs->sr_ref = defref;
364 rs->sr_err = LDAP_REFERRAL;
365 if (!rs->sr_ref) rs->sr_ref = default_referral;
366 send_ldap_result( op, rs );
368 if ( rs->sr_ref != default_referral ) {
369 ber_bvarray_free( rs->sr_ref );
372 send_ldap_error( op, rs,
373 LDAP_UNWILLING_TO_PERFORM,
374 "shadow context; no update referral" );
378 Debug( LDAP_DEBUG_ARGS, "do_add: no backend support\n", 0, 0, 0 );
379 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
380 "operation not supported within namingContext" );
395 char *textbuf, size_t textlen )
401 assert( (*e)->e_attrs == NULL );
404 for ( tail = &(*e)->e_attrs; *tail != NULL; tail = &(*tail)->a_next )
409 for( ; mods != NULL; mods = mods->sml_next ) {
412 assert( mods->sml_desc != NULL );
414 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
417 #define SLURPD_FRIENDLY
418 #ifdef SLURPD_FRIENDLY
423 * This check allows overlays to override operational
424 * attributes by setting them directly in the entry.
425 * We assume slap_mods_no_user_mod_check() was called
426 * with the user modifications.
433 j = mods->sml_numvals;
434 attr->a_numvals += j;
437 attr->a_vals = ch_realloc( attr->a_vals,
438 sizeof( struct berval ) * (i+j) );
440 /* checked for duplicates in slap_mods_check */
443 for ( j = 0; mods->sml_values[j].bv_val; j++ ) {
444 ber_dupbv( &attr->a_vals[i+j], &mods->sml_values[j] );
446 BER_BVZERO( &attr->a_vals[i+j] );
449 AC_MEMCPY( &attr->a_vals[i], mods->sml_values,
450 sizeof( struct berval ) * j );
453 if( mods->sml_nvalues ) {
454 attr->a_nvals = ch_realloc( attr->a_nvals,
455 sizeof( struct berval ) * (i+j) );
457 for ( j = 0; mods->sml_nvalues[j].bv_val; j++ ) {
458 ber_dupbv( &attr->a_nvals[i+j], &mods->sml_nvalues[j] );
460 BER_BVZERO( &attr->a_nvals[i+j] );
462 AC_MEMCPY( &attr->a_nvals[i], mods->sml_nvalues,
463 sizeof( struct berval ) * j );
466 attr->a_nvals = attr->a_vals;
471 snprintf( textbuf, textlen,
472 "attribute '%s' provided more than once",
473 mods->sml_desc->ad_cname.bv_val );
475 return LDAP_TYPE_OR_VALUE_EXISTS;
479 attr = attr_alloc( mods->sml_desc );
481 /* move values to attr structure */
482 i = mods->sml_numvals;
483 attr->a_numvals = mods->sml_numvals;
485 attr->a_vals = (BerVarray) ch_calloc( i+1, sizeof( BerValue ));
486 for ( i = 0; mods->sml_values[i].bv_val; i++ ) {
487 ber_dupbv( &attr->a_vals[i], &mods->sml_values[i] );
489 BER_BVZERO( &attr->a_vals[i] );
491 attr->a_vals = mods->sml_values;
494 if ( mods->sml_nvalues ) {
496 i = mods->sml_numvals;
497 attr->a_nvals = (BerVarray) ch_calloc( i+1, sizeof( BerValue ));
498 for ( i = 0; mods->sml_nvalues[i].bv_val; i++ ) {
499 ber_dupbv( &attr->a_nvals[i], &mods->sml_nvalues[i] );
501 BER_BVZERO( &attr->a_nvals[i] );
503 attr->a_nvals = mods->sml_nvalues;
506 attr->a_nvals = attr->a_vals;
510 tail = &attr->a_next;
521 Modifications **mods,
523 char *textbuf, size_t textlen )
525 Modifications *modhead = NULL;
527 Modifications **modtail = &modhead;
529 AttributeDescription *a_new_desc;
534 while ( a_new != NULL ) {
535 a_new_desc = a_new->a_desc;
536 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
538 mod->sml_op = LDAP_MOD_REPLACE;
541 mod->sml_type = a_new_desc->ad_cname;
543 count = a_new->a_numvals;
544 mod->sml_numvals = a_new->a_numvals;
546 mod->sml_values = (struct berval*) ch_malloc(
547 (count+1) * sizeof( struct berval) );
549 /* see slap_mods_check() comments...
550 * if a_vals == a_nvals, there is no normalizer.
551 * in this case, mod->sml_nvalues must be left NULL.
553 if ( a_new->a_vals != a_new->a_nvals ) {
554 mod->sml_nvalues = (struct berval*) ch_malloc(
555 (count+1) * sizeof( struct berval) );
557 mod->sml_nvalues = NULL;
560 for ( i = 0; i < count; i++ ) {
561 ber_dupbv(mod->sml_values+i, a_new->a_vals+i);
562 if ( mod->sml_nvalues ) {
563 ber_dupbv( mod->sml_nvalues+i, a_new->a_nvals+i );
567 mod->sml_values[count].bv_val = NULL;
568 mod->sml_values[count].bv_len = 0;
570 if ( mod->sml_nvalues ) {
571 mod->sml_nvalues[count].bv_val = NULL;
572 mod->sml_nvalues[count].bv_len = 0;
575 mod->sml_desc = a_new_desc;
578 modtail = &mod->sml_next;
579 a_new = a_new->a_next;
587 int slap_add_opattrs(
594 struct berval name, timestamp, csn = BER_BVNULL;
595 struct berval nname, tmp;
596 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
597 char csnbuf[ LDAP_PVT_CSNSTR_BUFSIZE ];
600 if ( SLAP_LASTMOD( op->o_bd ) ) {
604 timestamp.bv_val = timebuf;
605 a = attr_find( op->ora_e->e_attrs, slap_schema.si_ad_entryCSN );
610 if ( BER_BVISEMPTY( &op->o_csn )) {
613 csn.bv_len = sizeof(csnbuf);
614 slap_get_csn( op, &csn, manage_ctxcsn );
617 slap_queue_csn( op, &csn );
622 ptr = ber_bvchr( &csn, '#' );
624 timestamp.bv_len = STRLENOF("YYYYMMDDHHMMSSZ");
625 AC_MEMCPY( timebuf, csn.bv_val, timestamp.bv_len );
626 timebuf[timestamp.bv_len-1] = 'Z';
627 timebuf[timestamp.bv_len] = '\0';
629 time_t now = slap_get_time();
631 timestamp.bv_len = sizeof(timebuf);
633 slap_timestamp( &now, ×tamp );
636 if ( BER_BVISEMPTY( &op->o_dn ) ) {
637 BER_BVSTR( &name, SLAPD_ANONYMOUS );
644 a = attr_find( op->ora_e->e_attrs,
645 slap_schema.si_ad_entryUUID );
647 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
649 tmp.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
650 tmp.bv_val = uuidbuf;
652 attr_merge_normalize_one( op->ora_e,
653 slap_schema.si_ad_entryUUID, &tmp, op->o_tmpmemctx );
656 a = attr_find( op->ora_e->e_attrs,
657 slap_schema.si_ad_creatorsName );
659 attr_merge_one( op->ora_e,
660 slap_schema.si_ad_creatorsName, &name, &nname );
663 a = attr_find( op->ora_e->e_attrs,
664 slap_schema.si_ad_createTimestamp );
666 attr_merge_one( op->ora_e,
667 slap_schema.si_ad_createTimestamp, ×tamp, NULL );
671 attr_merge_one( op->ora_e,
672 slap_schema.si_ad_entryCSN, &csn, NULL );
675 a = attr_find( op->ora_e->e_attrs,
676 slap_schema.si_ad_modifiersName );
678 attr_merge_one( op->ora_e,
679 slap_schema.si_ad_modifiersName, &name, &nname );
682 a = attr_find( op->ora_e->e_attrs,
683 slap_schema.si_ad_modifyTimestamp );
685 attr_merge_one( op->ora_e,
686 slap_schema.si_ad_modifyTimestamp, ×tamp, NULL );