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