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, "do_add\n", 0, 0, 0 );
54 * Parse the add request. It looks like this:
56 * AddRequest := [APPLICATION 14] SEQUENCE {
57 * name DistinguishedName,
58 * attrs SEQUENCE OF SEQUENCE {
60 * values SET OF AttributeValue
66 if ( ber_scanf( ber, "{m", /*}*/ &dn ) == LBER_ERROR ) {
67 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
68 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
69 return SLAPD_DISCONNECT;
72 op->ora_e = entry_alloc();
74 rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn,
77 if ( rs->sr_err != LDAP_SUCCESS ) {
78 Debug( LDAP_DEBUG_ANY, "do_add: invalid dn (%s)\n", dn.bv_val, 0, 0 );
79 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
83 ber_dupbv( &op->ora_e->e_name, &op->o_req_dn );
84 ber_dupbv( &op->ora_e->e_nname, &op->o_req_ndn );
86 Debug( LDAP_DEBUG_ARGS, "do_add: dn (%s)\n", op->ora_e->e_dn, 0, 0 );
89 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
90 tag = ber_next_element( ber, &len, last ) )
95 tmp.sml_nvalues = NULL;
97 rtag = ber_scanf( ber, "{m{W}}", &tmp.sml_type, &tmp.sml_values );
99 if ( rtag == LBER_ERROR ) {
100 Debug( LDAP_DEBUG_ANY, "do_add: decoding error\n", 0, 0, 0 );
101 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
102 rs->sr_err = SLAPD_DISCONNECT;
106 if ( tmp.sml_values == NULL ) {
107 Debug( LDAP_DEBUG_ANY, "no values for type %s\n",
108 tmp.sml_type.bv_val, 0, 0 );
109 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
110 "no values for attribute type" );
114 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
115 mod->sml_op = LDAP_MOD_ADD;
117 mod->sml_next = NULL;
118 mod->sml_desc = NULL;
119 mod->sml_type = tmp.sml_type;
120 mod->sml_values = tmp.sml_values;
121 mod->sml_nvalues = NULL;
124 modtail = &mod->sml_next;
127 if ( ber_scanf( ber, /*{*/ "}") == LBER_ERROR ) {
128 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
129 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
130 rs->sr_err = SLAPD_DISCONNECT;
134 if ( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
135 Debug( LDAP_DEBUG_ANY, "do_add: get_ctrls failed\n", 0, 0, 0 );
139 if ( modlist == NULL ) {
140 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
141 "no attributes provided" );
145 Statslog( LDAP_DEBUG_STATS, "%s ADD dn=\"%s\"\n",
146 op->o_log_prefix, op->ora_e->e_name.bv_val, 0, 0, 0 );
148 if ( dn_match( &op->ora_e->e_nname, &slap_empty_bv ) ) {
149 /* protocolError may be a more appropriate error */
150 send_ldap_error( op, rs, LDAP_ALREADY_EXISTS,
151 "root DSE already exists" );
154 } else if ( dn_match( &op->ora_e->e_nname, &frontendDB->be_schemandn ) ) {
155 send_ldap_error( op, rs, LDAP_ALREADY_EXISTS,
156 "subschema subentry already exists" );
160 rs->sr_err = slap_mods_check( op, modlist, &rs->sr_text,
161 textbuf, textlen, NULL );
163 if ( rs->sr_err != LDAP_SUCCESS ) {
164 send_ldap_result( op, rs );
168 /* temporary; remove if not invoking backend function */
169 op->ora_modlist = modlist;
171 /* call this so global overlays/SLAPI have access to ora_e */
172 rs->sr_err = slap_mods2entry( op->ora_modlist, &op->ora_e,
173 1, 0, &rs->sr_text, textbuf, textlen );
174 if ( rs->sr_err != LDAP_SUCCESS ) {
175 send_ldap_result( op, rs );
181 op->o_bd = frontendDB;
182 rc = frontendDB->be_add( op, rs );
185 if ( rc == LDAP_X_TXN_SPECIFY_OKAY ) {
191 if ( op->ora_e != NULL && op->o_private != NULL ) {
192 BackendDB *bd = op->o_bd;
194 op->o_bd = (BackendDB *)op->o_private;
195 op->o_private = NULL;
197 be_entry_release_w( op, op->ora_e );
201 op->o_private = NULL;
206 if ( modlist != NULL ) {
207 /* in case of error, free the values as well */
208 slap_mods_free( modlist, freevals );
211 if ( op->ora_e != NULL ) {
212 entry_free( op->ora_e );
214 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
215 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
221 fe_op_add( Operation *op, SlapReply *rs )
224 Modifications **modtail = &op->ora_modlist;
226 BackendDB *op_be, *bd = op->o_bd;
227 char textbuf[ SLAP_TEXT_BUFLEN ];
228 size_t textlen = sizeof( textbuf );
230 manageDSAit = get_manageDSAit( op );
233 * We could be serving multiple database backends. Select the
234 * appropriate one, or send a referral to our "referral server"
235 * if we don't hold it.
237 op->o_bd = select_backend( &op->ora_e->e_nname, manageDSAit, 1 );
238 if ( op->o_bd == NULL ) {
240 rs->sr_ref = referral_rewrite( default_referral,
241 NULL, &op->ora_e->e_name, LDAP_SCOPE_DEFAULT );
242 if ( !rs->sr_ref ) rs->sr_ref = default_referral;
244 rs->sr_err = LDAP_REFERRAL;
245 send_ldap_result( op, rs );
247 if ( rs->sr_ref != default_referral ) {
248 ber_bvarray_free( rs->sr_ref );
251 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
252 "no global superior knowledge" );
257 /* If we've got a glued backend, check the real backend */
259 if ( SLAP_GLUE_INSTANCE( op->o_bd )) {
260 op->o_bd = select_backend( &op->ora_e->e_nname, manageDSAit, 0 );
263 /* check restrictions */
264 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
265 send_ldap_result( op, rs );
269 /* check for referrals */
270 if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
274 rs->sr_err = slap_mods_obsolete_check( op, op->ora_modlist,
275 &rs->sr_text, textbuf, textlen );
277 if ( rs->sr_err != LDAP_SUCCESS ) {
278 send_ldap_result( op, rs );
283 * do the add if 1 && (2 || 3)
284 * 1) there is an add function implemented in this backend;
285 * 2) this backend is master for what it holds;
286 * 3) it's a replica and the dn supplied is the updatedn.
288 if ( op->o_bd->be_add ) {
289 /* do the update here */
290 int repl_user = be_isupdate( op );
291 if ( !SLAP_SINGLE_SHADOW(op->o_bd) || repl_user ) {
292 int update = !BER_BVISEMPTY( &op->o_bd->be_update_ndn );
297 rs->sr_err = slap_mods_no_user_mod_check( op, op->ora_modlist,
298 &rs->sr_text, textbuf, textlen );
300 if ( rs->sr_err != LDAP_SUCCESS ) {
301 send_ldap_result( op, rs );
307 /* go to the last mod */
308 for ( modtail = &op->ora_modlist;
310 modtail = &(*modtail)->sml_next )
312 assert( (*modtail)->sml_op == LDAP_MOD_ADD );
313 assert( (*modtail)->sml_desc != NULL );
317 /* check for unmodifiable attributes */
318 rs->sr_err = slap_mods_no_repl_user_mod_check( op,
319 op->ora_modlist, &rs->sr_text, textbuf, textlen );
320 if ( rs->sr_err != LDAP_SUCCESS ) {
321 send_ldap_result( op, rs );
326 rc = op->o_bd->be_add( op, rs );
327 if ( rc == LDAP_SUCCESS ) {
328 /* NOTE: be_entry_release_w() is
329 * called by do_add(), so that global
330 * overlays on the way back can
331 * at least read the entry */
332 op->o_private = op->o_bd;
336 BerVarray defref = NULL;
338 defref = op->o_bd->be_update_refs
339 ? op->o_bd->be_update_refs : default_referral;
341 if ( defref != NULL ) {
342 rs->sr_ref = referral_rewrite( defref,
343 NULL, &op->ora_e->e_name, LDAP_SCOPE_DEFAULT );
344 if ( rs->sr_ref == NULL ) rs->sr_ref = defref;
345 rs->sr_err = LDAP_REFERRAL;
346 if (!rs->sr_ref) rs->sr_ref = default_referral;
347 send_ldap_result( op, rs );
349 if ( rs->sr_ref != default_referral ) {
350 ber_bvarray_free( rs->sr_ref );
353 send_ldap_error( op, rs,
354 LDAP_UNWILLING_TO_PERFORM,
355 "shadow context; no update referral" );
359 Debug( LDAP_DEBUG_ARGS, "do_add: no backend support\n", 0, 0, 0 );
360 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
361 "operation not supported within namingContext" );
376 char *textbuf, size_t textlen )
381 assert( (*e)->e_attrs == NULL );
384 for ( tail = &(*e)->e_attrs; *tail != NULL; tail = &(*tail)->a_next )
389 for( ; mods != NULL; mods = mods->sml_next ) {
392 assert( mods->sml_desc != NULL );
394 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
397 #define SLURPD_FRIENDLY
398 #ifdef SLURPD_FRIENDLY
403 * This check allows overlays to override operational
404 * attributes by setting them directly in the entry.
405 * We assume slap_mods_no_user_mod_check() was called
406 * with the user modifications.
412 for( i=0; attr->a_vals[i].bv_val; i++ ) {
415 for( j=0; mods->sml_values[j].bv_val; j++ ) {
420 attr->a_vals = ch_realloc( attr->a_vals,
421 sizeof( struct berval ) * (i+j) );
423 /* checked for duplicates in slap_mods_check */
426 for ( j = 0; mods->sml_values[j].bv_val; j++ ) {
427 ber_dupbv( &attr->a_vals[i+j], &mods->sml_values[j] );
429 BER_BVZERO( &attr->a_vals[i+j] );
432 AC_MEMCPY( &attr->a_vals[i], mods->sml_values,
433 sizeof( struct berval ) * j );
436 if( mods->sml_nvalues ) {
437 attr->a_nvals = ch_realloc( attr->a_nvals,
438 sizeof( struct berval ) * (i+j) );
440 for ( j = 0; mods->sml_nvalues[j].bv_val; j++ ) {
441 ber_dupbv( &attr->a_nvals[i+j], &mods->sml_nvalues[j] );
443 BER_BVZERO( &attr->a_nvals[i+j] );
445 AC_MEMCPY( &attr->a_nvals[i], mods->sml_nvalues,
446 sizeof( struct berval ) * j );
449 attr->a_nvals = attr->a_vals;
454 snprintf( textbuf, textlen,
455 "attribute '%s' provided more than once",
456 mods->sml_desc->ad_cname.bv_val );
458 return LDAP_TYPE_OR_VALUE_EXISTS;
462 attr = attr_alloc( mods->sml_desc );
464 /* move values to attr structure */
467 for ( i = 0; mods->sml_values[i].bv_val; i++ ) /* EMPTY */;
468 attr->a_vals = (BerVarray) ch_calloc( i+1, sizeof( BerValue ));
469 for ( i = 0; mods->sml_values[i].bv_val; i++ ) {
470 ber_dupbv( &attr->a_vals[i], &mods->sml_values[i] );
472 BER_BVZERO( &attr->a_vals[i] );
474 attr->a_vals = mods->sml_values;
477 if ( mods->sml_nvalues ) {
480 for ( i = 0; mods->sml_nvalues[i].bv_val; i++ ) /* EMPTY */;
481 attr->a_nvals = (BerVarray) ch_calloc( i+1, sizeof( BerValue ));
482 for ( i = 0; mods->sml_nvalues[i].bv_val; i++ ) {
483 ber_dupbv( &attr->a_nvals[i], &mods->sml_nvalues[i] );
485 BER_BVZERO( &attr->a_nvals[i] );
487 attr->a_nvals = mods->sml_nvalues;
490 attr->a_nvals = attr->a_vals;
494 tail = &attr->a_next;
505 Modifications **mods,
507 char *textbuf, size_t textlen )
509 Modifications *modhead = NULL;
511 Modifications **modtail = &modhead;
513 AttributeDescription *a_new_desc;
518 while ( a_new != NULL ) {
519 a_new_desc = a_new->a_desc;
520 mod = (Modifications *) malloc( sizeof( Modifications ));
522 mod->sml_op = LDAP_MOD_REPLACE;
525 mod->sml_type = a_new_desc->ad_cname;
527 for ( count = 0; a_new->a_vals[count].bv_val; count++ ) /* EMPTY */;
529 mod->sml_values = (struct berval*) malloc(
530 (count+1) * sizeof( struct berval) );
532 /* see slap_mods_check() comments...
533 * if a_vals == a_nvals, there is no normalizer.
534 * in this case, mod->sml_nvalues must be left NULL.
536 if ( a_new->a_vals != a_new->a_nvals ) {
537 mod->sml_nvalues = (struct berval*) malloc(
538 (count+1) * sizeof( struct berval) );
540 mod->sml_nvalues = NULL;
543 for ( i = 0; i < count; i++ ) {
544 ber_dupbv(mod->sml_values+i, a_new->a_vals+i);
545 if ( mod->sml_nvalues ) {
546 ber_dupbv( mod->sml_nvalues+i, a_new->a_nvals+i );
550 mod->sml_values[count].bv_val = NULL;
551 mod->sml_values[count].bv_len = 0;
553 if ( mod->sml_nvalues ) {
554 mod->sml_nvalues[count].bv_val = NULL;
555 mod->sml_nvalues[count].bv_len = 0;
558 mod->sml_desc = a_new_desc;
561 modtail = &mod->sml_next;
562 a_new = a_new->a_next;
570 int slap_add_opattrs(
577 struct berval name, timestamp, csn = BER_BVNULL;
578 struct berval nname, tmp;
579 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
580 char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
583 if ( SLAP_LASTMOD( op->o_bd ) ) {
587 timestamp.bv_val = timebuf;
588 a = attr_find( op->ora_e->e_attrs, slap_schema.si_ad_entryCSN );
593 if ( BER_BVISEMPTY( &op->o_csn )) {
596 csn.bv_len = sizeof(csnbuf);
597 slap_get_csn( op, &csn, manage_ctxcsn );
600 slap_queue_csn( op, &csn );
605 ptr = ber_bvchr( &csn, '#' );
607 timestamp.bv_len = STRLENOF("YYYYMMDDHHMMSSZ");
608 AC_MEMCPY( timebuf, csn.bv_val, timestamp.bv_len );
609 timebuf[timestamp.bv_len-1] = 'Z';
610 timebuf[timestamp.bv_len] = '\0';
612 time_t now = slap_get_time();
614 timestamp.bv_len = sizeof(timebuf);
616 slap_timestamp( &now, ×tamp );
619 if ( BER_BVISEMPTY( &op->o_dn ) ) {
620 BER_BVSTR( &name, SLAPD_ANONYMOUS );
627 a = attr_find( op->ora_e->e_attrs,
628 slap_schema.si_ad_entryUUID );
630 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
632 tmp.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
633 tmp.bv_val = uuidbuf;
635 attr_merge_normalize_one( op->ora_e,
636 slap_schema.si_ad_entryUUID, &tmp, op->o_tmpmemctx );
639 a = attr_find( op->ora_e->e_attrs,
640 slap_schema.si_ad_creatorsName );
642 attr_merge_one( op->ora_e,
643 slap_schema.si_ad_creatorsName, &name, &nname );
646 a = attr_find( op->ora_e->e_attrs,
647 slap_schema.si_ad_createTimestamp );
649 attr_merge_one( op->ora_e,
650 slap_schema.si_ad_createTimestamp, ×tamp, NULL );
654 attr_merge_one( op->ora_e,
655 slap_schema.si_ad_entryCSN, &csn, NULL );
658 a = attr_find( op->ora_e->e_attrs,
659 slap_schema.si_ad_modifiersName );
661 attr_merge_one( op->ora_e,
662 slap_schema.si_ad_modifiersName, &name, &nname );
665 a = attr_find( op->ora_e->e_attrs,
666 slap_schema.si_ad_modifyTimestamp );
668 attr_merge_one( op->ora_e,
669 slap_schema.si_ad_modifyTimestamp, ×tamp, NULL );