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