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 ) )
116 rtag = ber_scanf( ber, "{m{W}}", &tmp.sml_type, &tmp.sml_bvalues );
118 if ( rtag == LBER_ERROR ) {
120 LDAP_LOG( OPERATION, ERR,
121 "do_add: conn %d decoding error \n", conn->c_connid, 0, 0 );
123 Debug( LDAP_DEBUG_ANY, "do_add: decoding error\n", 0, 0, 0 );
125 send_ldap_disconnect( conn, op,
126 LDAP_PROTOCOL_ERROR, "decoding error" );
131 if ( tmp.sml_bvalues == NULL ) {
133 LDAP_LOG( OPERATION, INFO,
134 "do_add: conn %d no values for type %s\n",
135 conn->c_connid, tmp.sml_type.bv_val, 0 );
137 Debug( LDAP_DEBUG_ANY, "no values for type %s\n",
138 tmp.sml_type.bv_val, 0, 0 );
140 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
141 NULL, "no values for attribute type", NULL, NULL );
144 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
146 mod->sml_op = LDAP_MOD_ADD;
147 mod->sml_next = NULL;
148 mod->sml_desc = NULL;
149 mod->sml_type = tmp.sml_type;
150 mod->sml_bvalues = tmp.sml_bvalues;
153 modtail = &mod->sml_next;
156 if ( ber_scanf( ber, /*{*/ "}") == LBER_ERROR ) {
158 LDAP_LOG( OPERATION, ERR,
159 "do_add: conn %d ber_scanf failed\n", conn->c_connid, 0, 0 );
161 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
163 send_ldap_disconnect( conn, op,
164 LDAP_PROTOCOL_ERROR, "decoding error" );
169 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
171 LDAP_LOG( OPERATION, INFO,
172 "do_add: conn %d get_ctrls failed\n", conn->c_connid, 0, 0 );
174 Debug( LDAP_DEBUG_ANY, "do_add: get_ctrls failed\n", 0, 0, 0 );
179 if ( modlist == NULL ) {
180 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
181 NULL, "no attributes provided", NULL, NULL );
185 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu ADD dn=\"%s\"\n",
186 op->o_connid, op->o_opid, e->e_dn, 0, 0 );
188 if( e->e_nname.bv_len == 0 ) {
189 /* protocolError may be a more appropriate error */
190 send_ldap_result( conn, op, rc = LDAP_ALREADY_EXISTS,
191 NULL, "root DSE already exists",
195 } else if ( bvmatch( &e->e_nname, &global_schemandn ) ) {
196 send_ldap_result( conn, op, rc = LDAP_ALREADY_EXISTS,
197 NULL, "subschema subentry already exists",
202 manageDSAit = get_manageDSAit( op );
205 * We could be serving multiple database backends. Select the
206 * appropriate one, or send a referral to our "referral server"
207 * if we don't hold it.
209 be = select_backend( &e->e_nname, manageDSAit, 0 );
211 BerVarray ref = referral_rewrite( default_referral,
212 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
214 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
215 NULL, NULL, ref ? ref : default_referral, NULL );
217 if ( ref ) ber_bvarray_free( ref );
221 /* check restrictions */
222 rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
223 if( rc != LDAP_SUCCESS ) {
224 send_ldap_result( conn, op, rc,
225 NULL, text, NULL, NULL );
229 /* check for referrals */
230 rc = backend_check_referrals( be, conn, op, &e->e_name, &e->e_nname );
231 if ( rc != LDAP_SUCCESS ) {
236 pb = initAddPlugin( be, conn, op, &dn, e, manageDSAit );
237 #endif /* LDAP_SLAPI */
240 * do the add if 1 && (2 || 3)
241 * 1) there is an add function implemented in this backend;
242 * 2) this backend is master for what it holds;
243 * 3) it's a replica and the dn supplied is the updatedn.
246 /* do the update here */
247 int repl_user = be_isupdate(be, &op->o_ndn );
248 #ifndef SLAPD_MULTIMASTER
249 if ( !be->be_update_ndn.bv_len || repl_user )
252 int update = be->be_update_ndn.bv_len;
253 char textbuf[SLAP_TEXT_BUFLEN];
254 size_t textlen = sizeof textbuf;
256 rc = slap_mods_check( modlist, update, &text,
259 if( rc != LDAP_SUCCESS ) {
260 send_ldap_result( conn, op, rc,
261 NULL, text, NULL, NULL );
266 for( modtail = &modlist;
268 modtail = &(*modtail)->sml_next )
270 assert( (*modtail)->sml_op == LDAP_MOD_ADD );
271 assert( (*modtail)->sml_desc != NULL );
273 rc = slap_mods_opattrs( be, op, modlist, modtail, &text,
275 if( rc != LDAP_SUCCESS ) {
276 send_ldap_result( conn, op, rc,
277 NULL, text, NULL, NULL );
282 rc = slap_mods2entry( modlist, &e, repl_user, &text,
284 if( rc != LDAP_SUCCESS ) {
285 send_ldap_result( conn, op, rc,
286 NULL, text, NULL, NULL );
292 * Call the preoperation plugin here, because the entry
293 * will actually contain something.
295 rc = doPreAddPluginFNs( be, pb );
296 if ( rc != LDAP_SUCCESS ) {
297 /* plugin will have sent result */
300 #endif /* LDAP_SLAPI */
302 if ( (*be->be_add)( be, conn, op, e ) == 0 ) {
303 #ifdef SLAPD_MULTIMASTER
307 replog( be, op, &e->e_name, &e->e_nname, e );
309 be_entry_release_w( be, conn, op, e );
313 #ifndef SLAPD_MULTIMASTER
319 * SLAPI_ADD_ENTRY will be empty, but this may be acceptable
320 * on replicas (for now, it involves the minimum code intrusion).
322 rc = doPreAddPluginFNs( be, pb );
323 if ( rc != LDAP_SUCCESS ) {
324 /* plugin will have sent result */
327 #endif /* LDAP_SLAPI */
329 defref = be->be_update_refs
330 ? be->be_update_refs : default_referral;
331 ref = referral_rewrite( defref,
332 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
334 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
335 ref ? ref : defref, NULL );
337 if ( ref ) ber_bvarray_free( ref );
338 #endif /* SLAPD_MULTIMASTER */
342 rc = doPreAddPluginFNs( be, pb );
343 if ( rc != LDAP_SUCCESS ) {
344 /* plugin will have sent result */
349 LDAP_LOG( OPERATION, INFO,
350 "do_add: conn %d no backend support\n", conn->c_connid, 0, 0 );
352 Debug( LDAP_DEBUG_ARGS, " do_add: no backend support\n", 0, 0, 0 );
354 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
355 NULL, "operation not supported within namingContext", NULL, NULL );
359 doPostAddPluginFNs( be, pb );
360 #endif /* LDAP_SLAPI */
363 if( modlist != NULL ) {
364 slap_mods_free( modlist );
379 char *textbuf, size_t textlen )
381 Attribute **tail = &(*e)->e_attrs;
382 assert( *tail == NULL );
386 for( ; mods != NULL; mods = mods->sml_next ) {
389 assert( mods->sml_op == LDAP_MOD_ADD );
390 assert( mods->sml_desc != NULL );
392 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
395 #define SLURPD_FRIENDLY
396 #ifdef SLURPD_FRIENDLY
400 snprintf( textbuf, textlen,
401 "attribute '%s' provided more than once",
402 mods->sml_desc->ad_cname.bv_val );
403 return LDAP_TYPE_OR_VALUE_EXISTS;
406 for( i=0; attr->a_vals[i].bv_val; i++ ) {
409 for( j=0; mods->sml_bvalues[j].bv_val; j++ ) {
414 attr->a_vals = ch_realloc( attr->a_vals,
415 sizeof( struct berval ) * (i+j) );
417 /* should check for duplicates */
419 AC_MEMCPY( &attr->a_vals[i], mods->sml_bvalues,
420 sizeof( struct berval ) * j );
422 /* trim the mods array */
423 ch_free( mods->sml_bvalues );
424 mods->sml_bvalues = NULL;
428 snprintf( textbuf, textlen,
429 "attribute '%s' provided more than once",
430 mods->sml_desc->ad_cname.bv_val );
431 return LDAP_TYPE_OR_VALUE_EXISTS;
435 if( mods->sml_bvalues[1].bv_val != NULL ) {
436 /* check for duplicates */
438 MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
440 /* check if the values we're adding already exist */
441 if( mr == NULL || !mr->smr_match ) {
442 for ( i = 0; mods->sml_bvalues[i].bv_val != NULL; i++ ) {
443 /* test asserted values against themselves */
444 for( j = 0; j < i; j++ ) {
445 if ( bvmatch( &mods->sml_bvalues[i],
446 &mods->sml_bvalues[j] ) ) {
447 /* value exists already */
448 snprintf( textbuf, textlen,
449 "%s: value #%d provided more than once",
450 mods->sml_desc->ad_cname.bv_val, j );
451 return LDAP_TYPE_OR_VALUE_EXISTS;
458 const char *text = NULL;
459 char textbuf[ SLAP_TEXT_BUFLEN ] = { '\0' };
461 rc = modify_check_duplicates( mods->sml_desc, mr,
462 NULL, mods->sml_bvalues, 0,
463 &text, textbuf, sizeof( textbuf ) );
465 if ( rc != LDAP_SUCCESS ) {
471 attr = ch_calloc( 1, sizeof(Attribute) );
473 /* move ad to attr structure */
474 attr->a_desc = mods->sml_desc;
475 mods->sml_desc = NULL;
477 /* move values to attr structure */
478 /* should check for duplicates */
479 attr->a_vals = mods->sml_bvalues;
480 mods->sml_bvalues = NULL;
483 tail = &attr->a_next;
490 static Slapi_PBlock *initAddPlugin( Backend *be, Connection *conn, Operation *op,
491 struct berval *dn, Entry *e, int manageDSAit )
497 slapi_x_backend_set_pb( pb, be );
498 slapi_x_connection_set_pb( pb, conn );
499 slapi_x_operation_set_pb( pb, op );
501 slapi_pblock_set( pb, SLAPI_ADD_TARGET, (void *)dn->bv_val );
502 slapi_pblock_set( pb, SLAPI_ADD_ENTRY, (void *)e );
503 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
508 static int doPreAddPluginFNs( Backend *be, Slapi_PBlock *pb )
512 rc = doPluginFNs( be, SLAPI_PLUGIN_PRE_ADD_FN, pb );
515 * A preoperation plugin failure will abort the
519 LDAP_LOG( OPERATION, INFO, "do_add: add preoperation plugin failed\n",
522 Debug(LDAP_DEBUG_TRACE, "do_add: add preoperation plugin failed.\n",
524 if ( slapi_pblock_get( pb, SLAPI_RESULT_CODE, (void *)&rc ) != 0 )
534 static void doPostAddPluginFNs( Backend *be, Slapi_PBlock *pb )
538 rc = doPluginFNs( be, SLAPI_PLUGIN_POST_ADD_FN, pb );
541 LDAP_LOG( OPERATION, INFO, "do_add: add postoperation plugin failed\n",
544 Debug(LDAP_DEBUG_TRACE, "do_add: add preoperation plugin failed.\n",
549 #endif /* LDAP_SLAPI */