]> git.sur5r.net Git - openldap/blob - servers/slapd/add.c
bail out is now the default; use noSuchAttribute as error code, as suggested by Kurt
[openldap] / servers / slapd / add.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*
7  * Copyright (c) 1995 Regents of the University of Michigan.
8  * All rights reserved.
9  *
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.
16  */
17
18 #include "portable.h"
19
20 #include <stdio.h>
21 #include <ac/string.h>
22 #include <ac/time.h>
23 #include <ac/socket.h>
24
25 #include "ldap_pvt.h"
26 #include "slap.h"
27 #include "slapi.h"
28
29 int
30 do_add( Connection *conn, Operation *op )
31 {
32         BerElement      *ber = op->o_ber;
33         char            *last;
34         struct berval dn = { 0, NULL };
35         ber_len_t       len;
36         ber_tag_t       tag;
37         Entry           *e;
38         Backend         *be;
39         Modifications   *modlist = NULL;
40         Modifications   **modtail = &modlist;
41         Modifications   tmp;
42         const char *text;
43         LDAPRDN         *rdn = NULL;
44         int             a_cnt;
45         int                     rc = LDAP_SUCCESS;
46         int     manageDSAit;
47
48         Slapi_PBlock *pb = op->o_pb;
49
50 #ifdef NEW_LOGGING
51         LDAP_LOG( OPERATION, ENTRY, "do_add: conn %d enter\n", conn->c_connid,0,0 );
52 #else
53         Debug( LDAP_DEBUG_TRACE, "do_add\n", 0, 0, 0 );
54 #endif
55         /*
56          * Parse the add request.  It looks like this:
57          *
58          *      AddRequest := [APPLICATION 14] SEQUENCE {
59          *              name    DistinguishedName,
60          *              attrs   SEQUENCE OF SEQUENCE {
61          *                      type    AttributeType,
62          *                      values  SET OF AttributeValue
63          *              }
64          *      }
65          */
66
67         /* get the name */
68         if ( ber_scanf( ber, "{m", /*}*/ &dn ) == LBER_ERROR ) {
69 #ifdef NEW_LOGGING
70                 LDAP_LOG( OPERATION, ERR, 
71                         "do_add: conn %d ber_scanf failed\n", conn->c_connid,0,0 );
72 #else
73                 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
74 #endif
75                 send_ldap_disconnect( conn, op,
76                         LDAP_PROTOCOL_ERROR, "decoding error" );
77                 return -1;
78         }
79
80         e = (Entry *) ch_calloc( 1, sizeof(Entry) );
81
82         rc = dnPrettyNormal( NULL, &dn, &e->e_name, &e->e_nname );
83
84         if( rc != LDAP_SUCCESS ) {
85 #ifdef NEW_LOGGING
86                 LDAP_LOG( OPERATION, ERR, 
87                         "do_add: conn %d invalid dn (%s)\n", conn->c_connid, dn.bv_val, 0 );
88 #else
89                 Debug( LDAP_DEBUG_ANY, "do_add: invalid dn (%s)\n", dn.bv_val, 0, 0 );
90 #endif
91                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
92                             "invalid DN", NULL, NULL );
93                 goto done;
94         }
95
96 #ifdef NEW_LOGGING
97         LDAP_LOG( OPERATION, ARGS, 
98                 "do_add: conn %d  dn (%s)\n", conn->c_connid, e->e_dn, 0 );
99 #else
100         Debug( LDAP_DEBUG_ARGS, "do_add: dn (%s)\n", e->e_dn, 0, 0 );
101 #endif
102
103         /* get the attrs */
104         for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
105             tag = ber_next_element( ber, &len, last ) )
106         {
107                 Modifications *mod;
108                 ber_tag_t rtag;
109
110                 rtag = ber_scanf( ber, "{m{W}}", &tmp.sml_type, &tmp.sml_bvalues );
111
112                 if ( rtag == LBER_ERROR ) {
113 #ifdef NEW_LOGGING
114                         LDAP_LOG( OPERATION, ERR, 
115                                    "do_add: conn %d      decoding error \n", conn->c_connid, 0, 0 );
116 #else
117                         Debug( LDAP_DEBUG_ANY, "do_add: decoding error\n", 0, 0, 0 );
118 #endif
119                         send_ldap_disconnect( conn, op,
120                                 LDAP_PROTOCOL_ERROR, "decoding error" );
121                         rc = -1;
122                         goto done;
123                 }
124
125                 if ( tmp.sml_bvalues == NULL ) {
126 #ifdef NEW_LOGGING
127                         LDAP_LOG( OPERATION, INFO, 
128                                 "do_add: conn %d         no values for type %s\n",
129                                 conn->c_connid, tmp.sml_type.bv_val, 0 );
130 #else
131                         Debug( LDAP_DEBUG_ANY, "no values for type %s\n",
132                                 tmp.sml_type.bv_val, 0, 0 );
133 #endif
134                         send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
135                                 NULL, "no values for attribute type", NULL, NULL );
136                         goto done;
137                 }
138                 mod  = (Modifications *) ch_malloc( sizeof(Modifications) );
139                 
140                 mod->sml_op = LDAP_MOD_ADD;
141                 mod->sml_next = NULL;
142                 mod->sml_desc = NULL;
143                 mod->sml_type = tmp.sml_type;
144                 mod->sml_bvalues = tmp.sml_bvalues;
145
146                 *modtail = mod;
147                 modtail = &mod->sml_next;
148         }
149
150         if ( ber_scanf( ber, /*{*/ "}") == LBER_ERROR ) {
151 #ifdef NEW_LOGGING
152                 LDAP_LOG( OPERATION, ERR, 
153                         "do_add: conn %d ber_scanf failed\n", conn->c_connid, 0, 0 );
154 #else
155                 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
156 #endif
157                 send_ldap_disconnect( conn, op,
158                         LDAP_PROTOCOL_ERROR, "decoding error" );
159                 rc = -1;
160                 goto done;
161         }
162
163         if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
164 #ifdef NEW_LOGGING
165                 LDAP_LOG( OPERATION, INFO, 
166                         "do_add: conn %d get_ctrls failed\n", conn->c_connid, 0, 0 );
167 #else
168                 Debug( LDAP_DEBUG_ANY, "do_add: get_ctrls failed\n", 0, 0, 0 );
169 #endif
170                 goto done;
171         } 
172
173         if ( modlist == NULL ) {
174                 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
175                         NULL, "no attributes provided", NULL, NULL );
176                 goto done;
177         }
178
179         Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu ADD dn=\"%s\"\n",
180             op->o_connid, op->o_opid, e->e_dn, 0, 0 );
181
182         if( e->e_nname.bv_len == 0 ) {
183                 /* protocolError may be a more appropriate error */
184                 send_ldap_result( conn, op, rc = LDAP_ALREADY_EXISTS,
185                         NULL, "root DSE already exists",
186                         NULL, NULL );
187                 goto done;
188
189         } else if ( bvmatch( &e->e_nname, &global_schemandn ) ) {
190                 send_ldap_result( conn, op, rc = LDAP_ALREADY_EXISTS,
191                         NULL, "subschema subentry already exists",
192                         NULL, NULL );
193                 goto done;
194         }
195
196         /*
197          * Get attribute type(s) and attribute value(s) of our rdn,
198          */
199         if ( ldap_bv2rdn( &e->e_name, &rdn, (char **)&text,
200                 LDAP_DN_FORMAT_LDAP ) )
201         {
202                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX,
203                         NULL, "unknown type(s) used in RDN",
204                         NULL, NULL );
205                 goto done;
206         }
207
208         /* Check for RDN attrs in entry */
209         for ( a_cnt = 0; rdn[ 0 ][ a_cnt ]; a_cnt++ ) {
210                 AttributeDescription    *desc = NULL;
211                 Modifications           *mod;
212                 MatchingRule            *mr;
213                 int                     i;
214
215                 rc = slap_bv2ad( &rdn[ 0 ][ a_cnt ]->la_attr, 
216                                 &desc, &text );
217
218                 if ( rc != LDAP_SUCCESS ) {
219                         send_ldap_result( conn, op, rc,
220                                         NULL, text, NULL, NULL );
221                         goto done;
222                 }
223
224                 for (mod = modlist; mod; mod = mod->sml_next) {
225                         AttributeDescription    *mod_desc = NULL;
226
227                         rc = slap_bv2ad( &mod->sml_type, 
228                                         &mod_desc, &text );
229                         if ( rc != LDAP_SUCCESS ) {
230                                 send_ldap_result( conn, op, rc,
231                                                 NULL, text, NULL, NULL );
232                                 goto done;
233                         }
234
235                         if (mod_desc == desc) {
236                                 break;
237                         }
238                 }
239
240                 if (mod == NULL) {
241 #define BAILOUT
242 #ifdef BAILOUT
243                         /* bail out */
244                         send_ldap_result( conn, op, 
245                                         rc = LDAP_NO_SUCH_ATTRIBUTE,
246                                         NULL,
247                                         "attribute in RDN not listed in entry", 
248                                         NULL, NULL );
249                         goto done;
250
251 #else /* ! BAILOUT */
252                         struct berval   bv;
253
254                         /* add attribute type and value to modlist */
255                         mod  = (Modifications *) ch_malloc( sizeof(Modifications) );
256                 
257                         mod->sml_op = LDAP_MOD_ADD;
258                         mod->sml_next = NULL;
259                         mod->sml_desc = NULL;
260
261                         ber_dupbv( &mod->sml_type,
262                                         &rdn[ 0 ][ a_cnt ]->la_attr );
263
264                         mod->sml_bvalues = NULL;
265                         ber_dupbv( &bv, &rdn[ 0 ][ a_cnt ]->la_value );
266                         ber_bvarray_add( &mod->sml_bvalues, &bv );
267
268                         *modtail = mod;
269                         modtail = &mod->sml_next;
270                         continue;
271 #endif /* ! BAILOUT */
272                 }
273
274                 mr = desc->ad_type->sat_equality;
275                 if (mr == NULL || !mr->smr_match ) {
276                         /* brrrr ... */
277                         continue;
278                 }
279
280                 for (i = 0; mod->sml_bvalues[ i ].bv_val; i++) {
281                         int             match = 0;
282                         
283                         rc = value_match(&match, desc, mr,
284                                         SLAP_MR_VALUE_SYNTAX_MATCH,
285                                         &mod->sml_bvalues[ i ],
286                                         &rdn[ 0 ][ a_cnt ]->la_value, &text);
287
288                         if ( rc != LDAP_SUCCESS ) {
289                                 send_ldap_result( conn, op, rc,
290                                                 NULL, text, NULL, NULL);
291                                 goto done;
292                         }
293
294                         if (match == 0) {
295                                 break;
296                         }
297                 }
298
299                 /* not found? */
300                 if (mod->sml_bvalues[ i ].bv_val == NULL) {
301 #ifdef BAILOUT
302                         /* bailout */
303                         send_ldap_result( conn, op, 
304                                         rc = LDAP_NO_SUCH_ATTRIBUTE,
305                                         NULL,
306                                         "value in RDN not listed in entry", 
307                                         NULL, NULL );
308                         goto done;
309
310 #else /* ! BAILOUT */
311                         struct berval   bv;
312
313                         /* add attribute type and value to modlist */
314                         ber_dupbv( &bv, &rdn[ 0 ][ a_cnt ]->la_value );
315                         ber_bvarray_add( &mod->sml_bvalues, &bv );
316                         continue;
317 #endif /* ! BAILOUT */
318                 }
319         }
320
321         manageDSAit = get_manageDSAit( op );
322
323         /*
324          * We could be serving multiple database backends.  Select the
325          * appropriate one, or send a referral to our "referral server"
326          * if we don't hold it.
327          */
328         be = select_backend( &e->e_nname, manageDSAit, 0 );
329         if ( be == NULL ) {
330                 BerVarray ref = referral_rewrite( default_referral,
331                         NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
332
333                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
334                         NULL, NULL, ref ? ref : default_referral, NULL );
335
336                 if ( ref ) ber_bvarray_free( ref );
337                 goto done;
338         }
339
340         /* check restrictions */
341         rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
342         if( rc != LDAP_SUCCESS ) {
343                 send_ldap_result( conn, op, rc,
344                         NULL, text, NULL, NULL );
345                 goto done;
346         }
347
348         /* check for referrals */
349         rc = backend_check_referrals( be, conn, op, &e->e_name, &e->e_nname );
350         if ( rc != LDAP_SUCCESS ) {
351                 goto done;
352         }
353
354 #if defined( LDAP_SLAPI )
355         slapi_x_backend_set_pb( pb, be );
356         slapi_x_connection_set_pb( pb, conn );
357         slapi_x_operation_set_pb( pb, op );
358         slapi_pblock_set( pb, SLAPI_ADD_ENTRY, (void *)e );
359         slapi_pblock_set( pb, SLAPI_ADD_TARGET, (void *)dn.bv_val );
360         slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
361
362         rc = doPluginFNs( be, SLAPI_PLUGIN_PRE_ADD_FN, pb );
363         if ( rc != 0 ) {
364                 /*
365                  * A preoperation plugin failure will abort the
366                  * entire operation.
367                  */
368 #ifdef NEW_LOGGING
369                 LDAP_LOG( OPERATION, INFO, "do_add: add preoperation plugin failed\n",
370                                 0, 0, 0);
371 #else
372                 Debug(LDAP_DEBUG_TRACE, "do_add: add preoperation plugin failed.\n",
373                                 0, 0, 0);
374                 if ( slapi_pblock_get( pb, SLAPI_RESULT_CODE, (void *)&rc ) != 0 )
375                         rc = LDAP_OTHER;
376                 goto done;
377 #endif
378         }
379 #endif /* defined( LDAP_SLAPI ) */
380
381         /*
382          * do the add if 1 && (2 || 3)
383          * 1) there is an add function implemented in this backend;
384          * 2) this backend is master for what it holds;
385          * 3) it's a replica and the dn supplied is the updatedn.
386          */
387         if ( be->be_add ) {
388                 /* do the update here */
389                 int repl_user = be_isupdate(be, &op->o_ndn );
390 #ifndef SLAPD_MULTIMASTER
391                 if ( !be->be_update_ndn.bv_len || repl_user )
392 #endif
393                 {
394                         int update = be->be_update_ndn.bv_len;
395                         char textbuf[SLAP_TEXT_BUFLEN];
396                         size_t textlen = sizeof textbuf;
397
398                         rc = slap_mods_check( modlist, update, &text,
399                                 textbuf, textlen );
400
401                         if( rc != LDAP_SUCCESS ) {
402                                 send_ldap_result( conn, op, rc,
403                                         NULL, text, NULL, NULL );
404                                 goto done;
405                         }
406
407                         if ( !repl_user ) {
408                                 for( modtail = &modlist;
409                                         *modtail != NULL;
410                                         modtail = &(*modtail)->sml_next )
411                                 {
412                                         assert( (*modtail)->sml_op == LDAP_MOD_ADD );
413                                         assert( (*modtail)->sml_desc != NULL );
414                                 }
415                                 rc = slap_mods_opattrs( be, op, modlist, modtail, &text,
416                                         textbuf, textlen );
417                                 if( rc != LDAP_SUCCESS ) {
418                                         send_ldap_result( conn, op, rc,
419                                                 NULL, text, NULL, NULL );
420                                         goto done;
421                                 }
422                         }
423
424                         rc = slap_mods2entry( modlist, &e, repl_user, &text,
425                                 textbuf, textlen );
426                         if( rc != LDAP_SUCCESS ) {
427                                 send_ldap_result( conn, op, rc,
428                                         NULL, text, NULL, NULL );
429                                 goto done;
430                         }
431
432                         if ( (*be->be_add)( be, conn, op, e ) == 0 ) {
433 #ifdef SLAPD_MULTIMASTER
434                                 if ( !repl_user )
435 #endif
436                                 {
437                                         replog( be, op, &e->e_name, &e->e_nname, e );
438                                 }
439                                 be_entry_release_w( be, conn, op, e );
440                                 e = NULL;
441                         }
442
443 #ifndef SLAPD_MULTIMASTER
444                 } else {
445                         BerVarray defref = be->be_update_refs
446                                 ? be->be_update_refs : default_referral;
447                         BerVarray ref = referral_rewrite( defref,
448                                 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
449
450                         send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
451                                 ref ? ref : defref, NULL );
452
453                         if ( ref ) ber_bvarray_free( ref );
454 #endif
455                 }
456         } else {
457 #ifdef NEW_LOGGING
458             LDAP_LOG( OPERATION, INFO, 
459                        "do_add: conn %d  no backend support\n", conn->c_connid, 0, 0 );
460 #else
461             Debug( LDAP_DEBUG_ARGS, "    do_add: no backend support\n", 0, 0, 0 );
462 #endif
463             send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
464                               NULL, "operation not supported within namingContext", NULL, NULL );
465         }
466
467 #if defined( LDAP_SLAPI )
468         /*
469          * Postoperation errors are silently ignored; the work has
470          * been done.
471          */
472         if ( doPluginFNs( be, SLAPI_PLUGIN_POST_ADD_FN, pb ) != 0) {
473 #ifdef NEW_LOGGING
474                 LDAP_LOG( OPERATION, INFO, "do_add: Add postoperation plugins failed\n",
475                                 0, 0, 0);
476 #else
477                 Debug(LDAP_DEBUG_TRACE, "do_add: Add postoperation plugins failed.\n",
478                                 0, 0, 0);
479 #endif
480         }
481 #endif /* defined( LDAP_SLAPI ) */
482
483 done:
484         if( modlist != NULL ) {
485                 slap_mods_free( modlist );
486         }
487         if( e != NULL ) {
488                 entry_free( e );
489         }
490
491         return rc;
492 }
493
494 int
495 slap_mods2entry(
496         Modifications *mods,
497         Entry **e,
498         int repl_user,
499         const char **text,
500         char *textbuf, size_t textlen )
501 {
502         Attribute **tail = &(*e)->e_attrs;
503         assert( *tail == NULL );
504
505         *text = textbuf;
506
507         for( ; mods != NULL; mods = mods->sml_next ) {
508                 Attribute *attr;
509
510                 assert( mods->sml_op == LDAP_MOD_ADD );
511                 assert( mods->sml_desc != NULL );
512
513                 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
514
515                 if( attr != NULL ) {
516 #define SLURPD_FRIENDLY
517 #ifdef SLURPD_FRIENDLY
518                         ber_len_t i,j;
519
520                         if( !repl_user ) {
521                                 snprintf( textbuf, textlen,
522                                         "attribute '%s' provided more than once",
523                                         mods->sml_desc->ad_cname.bv_val );
524                                 return LDAP_TYPE_OR_VALUE_EXISTS;
525                         }
526
527                         for( i=0; attr->a_vals[i].bv_val; i++ ) {
528                                 /* count them */
529                         }
530                         for( j=0; mods->sml_bvalues[j].bv_val; j++ ) {
531                                 /* count them */
532                         }
533                         j++;    /* NULL */
534                         
535                         attr->a_vals = ch_realloc( attr->a_vals,
536                                 sizeof( struct berval ) * (i+j) );
537
538                         /* should check for duplicates */
539
540                         AC_MEMCPY( &attr->a_vals[i], mods->sml_bvalues,
541                                 sizeof( struct berval ) * j );
542
543                         /* trim the mods array */
544                         ch_free( mods->sml_bvalues );
545                         mods->sml_bvalues = NULL;
546
547                         continue;
548 #else
549                         snprintf( textbuf, textlen,
550                                 "attribute '%s' provided more than once",
551                                 mods->sml_desc->ad_cname.bv_val );
552                         return LDAP_TYPE_OR_VALUE_EXISTS;
553 #endif
554                 }
555
556                 if( mods->sml_bvalues[1].bv_val != NULL ) {
557                         /* check for duplicates */
558                         int             i, j;
559                         MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
560
561                         /* check if the values we're adding already exist */
562                         if( mr == NULL || !mr->smr_match ) {
563                                 for ( i = 0; mods->sml_bvalues[i].bv_val != NULL; i++ ) {
564                                         /* test asserted values against themselves */
565                                         for( j = 0; j < i; j++ ) {
566                                                 if ( bvmatch( &mods->sml_bvalues[i],
567                                                         &mods->sml_bvalues[j] ) ) {
568                                                         /* value exists already */
569                                                         snprintf( textbuf, textlen,
570                                                                 "%s: value #%d provided more than once",
571                                                                 mods->sml_desc->ad_cname.bv_val, j );
572                                                         return LDAP_TYPE_OR_VALUE_EXISTS;
573                                                 }
574                                         }
575                                 }
576
577                         } else {
578                                 int             rc;
579                                 const char      *text = NULL;
580                                 char            textbuf[ SLAP_TEXT_BUFLEN ]  = { '\0' };
581                                 
582                                 rc = modify_check_duplicates( mods->sml_desc, mr,
583                                                 NULL, mods->sml_bvalues, 0,
584                                                 &text, textbuf, sizeof( textbuf ) );
585
586                                 if ( rc != LDAP_SUCCESS ) {
587                                         return rc;
588                                 }
589                         }
590                 }
591
592                 attr = ch_calloc( 1, sizeof(Attribute) );
593
594                 /* move ad to attr structure */
595                 attr->a_desc = mods->sml_desc;
596                 mods->sml_desc = NULL;
597
598                 /* move values to attr structure */
599                 /*      should check for duplicates */
600                 attr->a_vals = mods->sml_bvalues;
601                 mods->sml_bvalues = NULL;
602
603                 *tail = attr;
604                 tail = &attr->a_next;
605         }
606
607         return LDAP_SUCCESS;
608 }