2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2007 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 );
52 Debug( LDAP_DEBUG_TRACE, "%s do_add\n",
53 op->o_log_prefix, 0, 0 );
56 * Parse the add request. It looks like this:
58 * AddRequest := [APPLICATION 14] SEQUENCE {
59 * name DistinguishedName,
60 * attrs SEQUENCE OF SEQUENCE {
62 * values SET OF AttributeValue
68 if ( ber_scanf( ber, "{m", /*}*/ &dn ) == LBER_ERROR ) {
69 Debug( LDAP_DEBUG_ANY, "%s do_add: ber_scanf failed\n",
70 op->o_log_prefix, 0, 0 );
71 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
72 return SLAPD_DISCONNECT;
75 Debug( LDAP_DEBUG_ARGS, "%s do_add: dn (%s)\n",
76 op->o_log_prefix, dn.bv_val, 0 );
79 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
80 tag = ber_next_element( ber, &len, last ) )
85 tmp.sml_nvalues = NULL;
87 rtag = ber_scanf( ber, "{m{W}}", &tmp.sml_type, &tmp.sml_values );
89 if ( rtag == LBER_ERROR ) {
90 Debug( LDAP_DEBUG_ANY, "%s do_add: decoding error\n",
91 op->o_log_prefix, 0, 0 );
92 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
93 rs->sr_err = SLAPD_DISCONNECT;
97 if ( tmp.sml_values == NULL ) {
98 Debug( LDAP_DEBUG_ANY, "%s do_add: no values for type %s\n",
99 op->o_log_prefix, tmp.sml_type.bv_val, 0 );
100 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
101 "no values for attribute type" );
105 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
106 mod->sml_op = LDAP_MOD_ADD;
108 mod->sml_next = NULL;
109 mod->sml_desc = NULL;
110 mod->sml_type = tmp.sml_type;
111 mod->sml_values = tmp.sml_values;
112 mod->sml_nvalues = NULL;
115 modtail = &mod->sml_next;
118 if ( ber_scanf( ber, /*{*/ "}") == LBER_ERROR ) {
119 Debug( LDAP_DEBUG_ANY, "%s do_add: ber_scanf failed\n",
120 op->o_log_prefix, 0, 0 );
121 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
122 rs->sr_err = SLAPD_DISCONNECT;
126 if ( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
127 Debug( LDAP_DEBUG_ANY, "%s do_add: get_ctrls failed\n",
128 op->o_log_prefix, 0, 0 );
132 rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn,
135 if ( rs->sr_err != LDAP_SUCCESS ) {
136 Debug( LDAP_DEBUG_ANY, "%s do_add: invalid dn (%s)\n",
137 op->o_log_prefix, dn.bv_val, 0 );
138 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
142 op->ora_e = entry_alloc();
143 ber_dupbv( &op->ora_e->e_name, &op->o_req_dn );
144 ber_dupbv( &op->ora_e->e_nname, &op->o_req_ndn );
146 Statslog( LDAP_DEBUG_STATS, "%s ADD dn=\"%s\"\n",
147 op->o_log_prefix, op->o_req_dn.bv_val, 0, 0, 0 );
149 if ( modlist == NULL ) {
150 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
151 "no attributes provided" );
155 if ( dn_match( &op->ora_e->e_nname, &slap_empty_bv ) ) {
156 /* protocolError may be a more appropriate error */
157 send_ldap_error( op, rs, LDAP_ALREADY_EXISTS,
158 "root DSE already exists" );
161 } else if ( dn_match( &op->ora_e->e_nname, &frontendDB->be_schemandn ) ) {
162 send_ldap_error( op, rs, LDAP_ALREADY_EXISTS,
163 "subschema subentry already exists" );
167 rs->sr_err = slap_mods_check( op, modlist, &rs->sr_text,
168 textbuf, textlen, NULL );
170 if ( rs->sr_err != LDAP_SUCCESS ) {
171 send_ldap_result( op, rs );
175 /* temporary; remove if not invoking backend function */
176 op->ora_modlist = modlist;
178 /* call this so global overlays/SLAPI have access to ora_e */
179 rs->sr_err = slap_mods2entry( op->ora_modlist, &op->ora_e,
180 1, 0, &rs->sr_text, textbuf, textlen );
181 if ( rs->sr_err != LDAP_SUCCESS ) {
182 send_ldap_result( op, rs );
188 op->o_bd = frontendDB;
189 rc = frontendDB->be_add( op, rs );
192 if ( rc == LDAP_X_TXN_SPECIFY_OKAY ) {
198 if ( op->ora_e != NULL && op->o_private != NULL ) {
199 BackendDB *bd = op->o_bd;
201 op->o_bd = (BackendDB *)op->o_private;
202 op->o_private = NULL;
204 be_entry_release_w( op, op->ora_e );
208 op->o_private = NULL;
213 if ( modlist != NULL ) {
214 /* in case of error, free the values as well */
215 slap_mods_free( modlist, freevals );
218 if ( op->ora_e != NULL ) {
219 entry_free( op->ora_e );
221 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
222 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
228 fe_op_add( Operation *op, SlapReply *rs )
230 Modifications **modtail = &op->ora_modlist;
232 BackendDB *op_be, *bd = op->o_bd;
233 char textbuf[ SLAP_TEXT_BUFLEN ];
234 size_t textlen = sizeof( textbuf );
237 * We could be serving multiple database backends. Select the
238 * appropriate one, or send a referral to our "referral server"
239 * if we don't hold it.
241 op->o_bd = select_backend( &op->ora_e->e_nname, 1 );
242 if ( op->o_bd == NULL ) {
244 rs->sr_ref = referral_rewrite( default_referral,
245 NULL, &op->ora_e->e_name, LDAP_SCOPE_DEFAULT );
246 if ( !rs->sr_ref ) rs->sr_ref = default_referral;
248 rs->sr_err = LDAP_REFERRAL;
249 send_ldap_result( op, rs );
251 if ( rs->sr_ref != default_referral ) {
252 ber_bvarray_free( rs->sr_ref );
255 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
256 "no global superior knowledge" );
261 /* If we've got a glued backend, check the real backend */
263 if ( SLAP_GLUE_INSTANCE( op->o_bd )) {
264 op->o_bd = select_backend( &op->ora_e->e_nname, 0 );
267 /* check restrictions */
268 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
269 send_ldap_result( op, rs );
273 /* check for referrals */
274 if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
278 rs->sr_err = slap_mods_obsolete_check( op, op->ora_modlist,
279 &rs->sr_text, textbuf, textlen );
281 if ( rs->sr_err != LDAP_SUCCESS ) {
282 send_ldap_result( op, rs );
287 * do the add if 1 && (2 || 3)
288 * 1) there is an add function implemented in this backend;
289 * 2) this backend is master for what it holds;
290 * 3) it's a replica and the dn supplied is the updatedn.
292 if ( op->o_bd->be_add ) {
293 /* do the update here */
294 int repl_user = be_isupdate( op );
295 if ( !SLAP_SINGLE_SHADOW(op->o_bd) || repl_user ) {
296 int update = !BER_BVISEMPTY( &op->o_bd->be_update_ndn );
301 rs->sr_err = slap_mods_no_user_mod_check( op, op->ora_modlist,
302 &rs->sr_text, textbuf, textlen );
304 if ( rs->sr_err != LDAP_SUCCESS ) {
305 send_ldap_result( op, rs );
311 /* go to the last mod */
312 for ( modtail = &op->ora_modlist;
314 modtail = &(*modtail)->sml_next )
316 assert( (*modtail)->sml_op == LDAP_MOD_ADD );
317 assert( (*modtail)->sml_desc != NULL );
321 /* check for unmodifiable attributes */
322 rs->sr_err = slap_mods_no_repl_user_mod_check( op,
323 op->ora_modlist, &rs->sr_text, textbuf, textlen );
324 if ( rs->sr_err != LDAP_SUCCESS ) {
325 send_ldap_result( op, rs );
330 rc = op->o_bd->be_add( op, rs );
331 if ( rc == LDAP_SUCCESS ) {
332 /* NOTE: be_entry_release_w() is
333 * called by do_add(), so that global
334 * overlays on the way back can
335 * at least read the entry */
336 op->o_private = op->o_bd;
340 BerVarray defref = NULL;
342 defref = op->o_bd->be_update_refs
343 ? op->o_bd->be_update_refs : default_referral;
345 if ( defref != NULL ) {
346 rs->sr_ref = referral_rewrite( defref,
347 NULL, &op->ora_e->e_name, LDAP_SCOPE_DEFAULT );
348 if ( rs->sr_ref == NULL ) rs->sr_ref = defref;
349 rs->sr_err = LDAP_REFERRAL;
350 if (!rs->sr_ref) rs->sr_ref = default_referral;
351 send_ldap_result( op, rs );
353 if ( rs->sr_ref != default_referral ) {
354 ber_bvarray_free( rs->sr_ref );
357 send_ldap_error( op, rs,
358 LDAP_UNWILLING_TO_PERFORM,
359 "shadow context; no update referral" );
363 Debug( LDAP_DEBUG_ARGS, "do_add: no backend support\n", 0, 0, 0 );
364 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
365 "operation not supported within namingContext" );
380 char *textbuf, size_t textlen )
385 assert( (*e)->e_attrs == NULL );
388 for ( tail = &(*e)->e_attrs; *tail != NULL; tail = &(*tail)->a_next )
393 for( ; mods != NULL; mods = mods->sml_next ) {
396 assert( mods->sml_desc != NULL );
398 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
401 #define SLURPD_FRIENDLY
402 #ifdef SLURPD_FRIENDLY
407 * This check allows overlays to override operational
408 * attributes by setting them directly in the entry.
409 * We assume slap_mods_no_user_mod_check() was called
410 * with the user modifications.
416 for( i=0; attr->a_vals[i].bv_val; i++ ) {
419 for( j=0; mods->sml_values[j].bv_val; j++ ) {
424 attr->a_vals = ch_realloc( attr->a_vals,
425 sizeof( struct berval ) * (i+j) );
427 /* checked for duplicates in slap_mods_check */
430 for ( j = 0; mods->sml_values[j].bv_val; j++ ) {
431 ber_dupbv( &attr->a_vals[i+j], &mods->sml_values[j] );
433 BER_BVZERO( &attr->a_vals[i+j] );
436 AC_MEMCPY( &attr->a_vals[i], mods->sml_values,
437 sizeof( struct berval ) * j );
440 if( mods->sml_nvalues ) {
441 attr->a_nvals = ch_realloc( attr->a_nvals,
442 sizeof( struct berval ) * (i+j) );
444 for ( j = 0; mods->sml_nvalues[j].bv_val; j++ ) {
445 ber_dupbv( &attr->a_nvals[i+j], &mods->sml_nvalues[j] );
447 BER_BVZERO( &attr->a_nvals[i+j] );
449 AC_MEMCPY( &attr->a_nvals[i], mods->sml_nvalues,
450 sizeof( struct berval ) * j );
453 attr->a_nvals = attr->a_vals;
458 snprintf( textbuf, textlen,
459 "attribute '%s' provided more than once",
460 mods->sml_desc->ad_cname.bv_val );
462 return LDAP_TYPE_OR_VALUE_EXISTS;
466 attr = attr_alloc( mods->sml_desc );
468 /* move values to attr structure */
471 for ( i = 0; mods->sml_values[i].bv_val; i++ ) /* EMPTY */;
472 attr->a_vals = (BerVarray) ch_calloc( i+1, sizeof( BerValue ));
473 for ( i = 0; mods->sml_values[i].bv_val; i++ ) {
474 ber_dupbv( &attr->a_vals[i], &mods->sml_values[i] );
476 BER_BVZERO( &attr->a_vals[i] );
478 attr->a_vals = mods->sml_values;
481 if ( mods->sml_nvalues ) {
484 for ( i = 0; mods->sml_nvalues[i].bv_val; i++ ) /* EMPTY */;
485 attr->a_nvals = (BerVarray) ch_calloc( i+1, sizeof( BerValue ));
486 for ( i = 0; mods->sml_nvalues[i].bv_val; i++ ) {
487 ber_dupbv( &attr->a_nvals[i], &mods->sml_nvalues[i] );
489 BER_BVZERO( &attr->a_nvals[i] );
491 attr->a_nvals = mods->sml_nvalues;
494 attr->a_nvals = attr->a_vals;
498 tail = &attr->a_next;
509 Modifications **mods,
511 char *textbuf, size_t textlen )
513 Modifications *modhead = NULL;
515 Modifications **modtail = &modhead;
517 AttributeDescription *a_new_desc;
522 while ( a_new != NULL ) {
523 a_new_desc = a_new->a_desc;
524 mod = (Modifications *) malloc( sizeof( Modifications ));
526 mod->sml_op = LDAP_MOD_REPLACE;
529 mod->sml_type = a_new_desc->ad_cname;
531 for ( count = 0; a_new->a_vals[count].bv_val; count++ ) /* EMPTY */;
533 mod->sml_values = (struct berval*) malloc(
534 (count+1) * sizeof( struct berval) );
536 /* see slap_mods_check() comments...
537 * if a_vals == a_nvals, there is no normalizer.
538 * in this case, mod->sml_nvalues must be left NULL.
540 if ( a_new->a_vals != a_new->a_nvals ) {
541 mod->sml_nvalues = (struct berval*) malloc(
542 (count+1) * sizeof( struct berval) );
544 mod->sml_nvalues = NULL;
547 for ( i = 0; i < count; i++ ) {
548 ber_dupbv(mod->sml_values+i, a_new->a_vals+i);
549 if ( mod->sml_nvalues ) {
550 ber_dupbv( mod->sml_nvalues+i, a_new->a_nvals+i );
554 mod->sml_values[count].bv_val = NULL;
555 mod->sml_values[count].bv_len = 0;
557 if ( mod->sml_nvalues ) {
558 mod->sml_nvalues[count].bv_val = NULL;
559 mod->sml_nvalues[count].bv_len = 0;
562 mod->sml_desc = a_new_desc;
565 modtail = &mod->sml_next;
566 a_new = a_new->a_next;
574 int slap_add_opattrs(
581 struct berval name, timestamp, csn = BER_BVNULL;
582 struct berval nname, tmp;
583 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
584 char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
587 if ( SLAP_LASTMOD( op->o_bd ) ) {
591 timestamp.bv_val = timebuf;
592 a = attr_find( op->ora_e->e_attrs, slap_schema.si_ad_entryCSN );
597 if ( BER_BVISEMPTY( &op->o_csn )) {
600 csn.bv_len = sizeof(csnbuf);
601 slap_get_csn( op, &csn, manage_ctxcsn );
604 slap_queue_csn( op, &csn );
609 ptr = ber_bvchr( &csn, '#' );
611 timestamp.bv_len = STRLENOF("YYYYMMDDHHMMSSZ");
612 AC_MEMCPY( timebuf, csn.bv_val, timestamp.bv_len );
613 timebuf[timestamp.bv_len-1] = 'Z';
614 timebuf[timestamp.bv_len] = '\0';
616 time_t now = slap_get_time();
618 timestamp.bv_len = sizeof(timebuf);
620 slap_timestamp( &now, ×tamp );
623 if ( BER_BVISEMPTY( &op->o_dn ) ) {
624 BER_BVSTR( &name, SLAPD_ANONYMOUS );
631 a = attr_find( op->ora_e->e_attrs,
632 slap_schema.si_ad_entryUUID );
634 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
636 tmp.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
637 tmp.bv_val = uuidbuf;
639 attr_merge_normalize_one( op->ora_e,
640 slap_schema.si_ad_entryUUID, &tmp, op->o_tmpmemctx );
643 a = attr_find( op->ora_e->e_attrs,
644 slap_schema.si_ad_creatorsName );
646 attr_merge_one( op->ora_e,
647 slap_schema.si_ad_creatorsName, &name, &nname );
650 a = attr_find( op->ora_e->e_attrs,
651 slap_schema.si_ad_createTimestamp );
653 attr_merge_one( op->ora_e,
654 slap_schema.si_ad_createTimestamp, ×tamp, NULL );
658 attr_merge_one( op->ora_e,
659 slap_schema.si_ad_entryCSN, &csn, NULL );
662 a = attr_find( op->ora_e->e_attrs,
663 slap_schema.si_ad_modifiersName );
665 attr_merge_one( op->ora_e,
666 slap_schema.si_ad_modifiersName, &name, &nname );
669 a = attr_find( op->ora_e->e_attrs,
670 slap_schema.si_ad_modifyTimestamp );
672 attr_merge_one( op->ora_e,
673 slap_schema.si_ad_modifyTimestamp, ×tamp, NULL );