]> git.sur5r.net Git - openldap/blob - servers/slapd/add.c
5e87f2e4de3a3d64a6574b24ff4c329ae352212d
[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, &e->e_name, &e->e_nname );
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 #ifdef NEW_LOGGING
95         LDAP_LOG( OPERATION, ARGS, 
96                 "do_add: conn %d  dn (%s)\n", op->o_connid, e->e_dn, 0 );
97 #else
98         Debug( LDAP_DEBUG_ARGS, "do_add: dn (%s)\n", e->e_dn, 0, 0 );
99 #endif
100
101         op->o_req_dn = e->e_name;
102         op->o_req_ndn = e->e_nname;
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 #ifdef SLAP_NVALUES
112                 tmp.sml_nvalues = NULL;
113 #endif
114
115                 rtag = ber_scanf( ber, "{m{W}}", &tmp.sml_type, &tmp.sml_values );
116
117                 if ( rtag == LBER_ERROR ) {
118 #ifdef NEW_LOGGING
119                         LDAP_LOG( OPERATION, ERR, 
120                                    "do_add: conn %d      decoding error \n", op->o_connid, 0, 0 );
121 #else
122                         Debug( LDAP_DEBUG_ANY, "do_add: decoding error\n", 0, 0, 0 );
123 #endif
124                         send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
125                         rs->sr_err = -1;
126                         goto done;
127                 }
128
129                 if ( tmp.sml_values == NULL ) {
130 #ifdef NEW_LOGGING
131                         LDAP_LOG( OPERATION, INFO, 
132                                 "do_add: conn %d         no values for type %s\n",
133                                 op->o_connid, tmp.sml_type.bv_val, 0 );
134 #else
135                         Debug( LDAP_DEBUG_ANY, "no values for type %s\n",
136                                 tmp.sml_type.bv_val, 0, 0 );
137 #endif
138                         send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "no values for attribute type" );
139                         goto done;
140                 }
141
142                 mod  = (Modifications *) ch_malloc( sizeof(Modifications) );
143                 mod->sml_op = LDAP_MOD_ADD;
144                 mod->sml_next = NULL;
145                 mod->sml_desc = NULL;
146                 mod->sml_type = tmp.sml_type;
147                 mod->sml_values = tmp.sml_values;
148 #ifdef SLAP_NVALUES
149                 mod->sml_nvalues = NULL;
150 #endif
151
152                 *modtail = mod;
153                 modtail = &mod->sml_next;
154         }
155
156         if ( ber_scanf( ber, /*{*/ "}") == LBER_ERROR ) {
157 #ifdef NEW_LOGGING
158                 LDAP_LOG( OPERATION, ERR, 
159                         "do_add: conn %d ber_scanf failed\n", op->o_connid, 0, 0 );
160 #else
161                 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
162 #endif
163                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
164                 rs->sr_err = -1;
165                 goto done;
166         }
167
168         if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
169 #ifdef NEW_LOGGING
170                 LDAP_LOG( OPERATION, INFO, 
171                         "do_add: conn %d get_ctrls failed\n", op->o_connid, 0, 0 );
172 #else
173                 Debug( LDAP_DEBUG_ANY, "do_add: get_ctrls failed\n", 0, 0, 0 );
174 #endif
175                 goto done;
176         } 
177
178         if ( modlist == NULL ) {
179                 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "no attributes provided" );
180                 goto done;
181         }
182
183         Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu ADD dn=\"%s\"\n",
184             op->o_connid, op->o_opid, e->e_name.bv_val, 0, 0 );
185
186         if( e->e_nname.bv_len == 0 ) {
187                 /* protocolError may be a more appropriate error */
188                 send_ldap_error( op, rs, LDAP_ALREADY_EXISTS, "root DSE already exists" );
189                 goto done;
190
191         } else if ( bvmatch( &e->e_nname, &global_schemandn ) ) {
192                 send_ldap_error( op, rs, LDAP_ALREADY_EXISTS, "subschema subentry already exists" );
193                 goto done;
194         }
195
196         manageDSAit = get_manageDSAit( op );
197
198         /*
199          * We could be serving multiple database backends.  Select the
200          * appropriate one, or send a referral to our "referral server"
201          * if we don't hold it.
202          */
203         op->o_bd = select_backend( &e->e_nname, manageDSAit, 0 );
204         if ( op->o_bd == NULL ) {
205                 rs->sr_ref = referral_rewrite( default_referral,
206                         NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
207
208                 rs->sr_err = LDAP_REFERRAL;
209                 if (!rs->sr_ref) rs->sr_ref = default_referral;
210                 send_ldap_result( op, rs );
211
212                 if ( rs->sr_ref != default_referral ) ber_bvarray_free( rs->sr_ref );
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 );
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                         BerVarray ref;
307 #ifdef LDAP_SLAPI
308                         /*
309                          * SLAPI_ADD_ENTRY will be empty, but this may be acceptable
310                          * on replicas (for now, it involves the minimum code intrusion).
311                          */
312                         rs->sr_err = doPreAddPluginFNs( op );
313                         if ( rs->sr_err != LDAP_SUCCESS ) {
314                                 /* plugin will have sent result */
315                                 goto done;
316                         }
317 #endif /* LDAP_SLAPI */
318
319                         defref = op->o_bd->be_update_refs
320                                 ? op->o_bd->be_update_refs : default_referral;
321                         rs->sr_ref = referral_rewrite( defref,
322                                 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
323
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 #endif /* SLAPD_MULTIMASTER */
330                 }
331         } else {
332 #ifdef LDAP_SLAPI
333             rs->sr_err = doPreAddPluginFNs( op );
334             if ( rs->sr_err != LDAP_SUCCESS ) {
335                 /* plugin will have sent result */
336                 goto done;
337             }
338 #endif
339 #ifdef NEW_LOGGING
340             LDAP_LOG( OPERATION, INFO, 
341                        "do_add: conn %d  no backend support\n", op->o_connid, 0, 0 );
342 #else
343             Debug( LDAP_DEBUG_ARGS, "    do_add: no backend support\n", 0, 0, 0 );
344 #endif
345             send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
346                               "operation not supported within namingContext" );
347         }
348
349 #ifdef LDAP_SLAPI
350         doPostAddPluginFNs( op );
351 #endif /* LDAP_SLAPI */
352
353 done:
354         if( modlist != NULL ) {
355                 slap_mods_free( modlist );
356         }
357         if( e != NULL ) {
358                 entry_free( e );
359         }
360
361         return rs->sr_err;
362 }
363
364 int
365 slap_mods2entry(
366         Modifications *mods,
367         Entry **e,
368         int repl_user,
369         const char **text,
370         char *textbuf, size_t textlen )
371 {
372         Attribute **tail = &(*e)->e_attrs;
373         assert( *tail == NULL );
374
375         *text = textbuf;
376
377         for( ; mods != NULL; mods = mods->sml_next ) {
378                 Attribute *attr;
379
380                 assert( mods->sml_op == LDAP_MOD_ADD );
381                 assert( mods->sml_desc != NULL );
382
383                 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
384
385                 if( attr != NULL ) {
386 #define SLURPD_FRIENDLY
387 #ifdef SLURPD_FRIENDLY
388                         ber_len_t i,j;
389
390                         if( !repl_user ) {
391                                 snprintf( textbuf, textlen,
392                                         "attribute '%s' provided more than once",
393                                         mods->sml_desc->ad_cname.bv_val );
394                                 return LDAP_TYPE_OR_VALUE_EXISTS;
395                         }
396
397                         for( i=0; attr->a_vals[i].bv_val; i++ ) {
398                                 /* count them */
399                         }
400                         for( j=0; mods->sml_values[j].bv_val; j++ ) {
401                                 /* count them */
402                         }
403                         j++;    /* NULL */
404                         
405                         attr->a_vals = ch_realloc( attr->a_vals,
406                                 sizeof( struct berval ) * (i+j) );
407
408                         /* should check for duplicates */
409
410                         AC_MEMCPY( &attr->a_vals[i], mods->sml_values,
411                                 sizeof( struct berval ) * j );
412
413                         /* trim the mods array */
414                         ch_free( mods->sml_values );
415                         mods->sml_values = NULL;
416
417 #ifdef SLAP_NVALUES
418                         if( mods->sml_nvalues ) {
419                                 attr->a_nvals = ch_realloc( attr->a_nvals,
420                                         sizeof( struct berval ) * (i+j) );
421
422                                 AC_MEMCPY( &attr->a_nvals[i], mods->sml_nvalues,
423                                         sizeof( struct berval ) * j );
424
425                                 /* trim the mods array */
426                                 ch_free( mods->sml_nvalues );
427                                 mods->sml_nvalues = NULL;
428                         } else {
429                                 attr->a_nvals = attr->a_vals;
430                         }
431 #endif
432
433                         continue;
434 #else
435                         snprintf( textbuf, textlen,
436                                 "attribute '%s' provided more than once",
437                                 mods->sml_desc->ad_cname.bv_val );
438                         return LDAP_TYPE_OR_VALUE_EXISTS;
439 #endif
440                 }
441
442                 if( mods->sml_values[1].bv_val != NULL ) {
443                         /* check for duplicates */
444                         int             i, j;
445                         MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
446
447                         /* check if the values we're adding already exist */
448                         if( mr == NULL || !mr->smr_match ) {
449                                 for ( i = 0; mods->sml_bvalues[i].bv_val != NULL; i++ ) {
450                                         /* test asserted values against themselves */
451                                         for( j = 0; j < i; j++ ) {
452                                                 if ( bvmatch( &mods->sml_bvalues[i],
453                                                         &mods->sml_bvalues[j] ) ) {
454                                                         /* value exists already */
455                                                         snprintf( textbuf, textlen,
456                                                                 "%s: value #%d provided more than once",
457                                                                 mods->sml_desc->ad_cname.bv_val, j );
458                                                         return LDAP_TYPE_OR_VALUE_EXISTS;
459                                                 }
460                                         }
461                                 }
462
463                         } else {
464                                 int             rc;
465 #ifdef SLAP_NVALUES
466                                 int match;
467
468                                 for ( i = 0; mods->sml_nvalues[i].bv_val != NULL; i++ ) {
469                                         /* test asserted values against themselves */
470                                         for( j = 0; j < i; j++ ) {
471                                                 rc = value_match( &match, mods->sml_desc, mr,
472                                                         SLAP_MR_EQUALITY | SLAP_MR_VALUE_OF_ASSERTION_SYNTAX
473                                                         | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
474                                                         | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
475                                                         &mods->sml_nvalues[i], &mods->sml_nvalues[j], text );
476                                                 if ( rc == LDAP_SUCCESS && match == 0 ) {
477                                                         /* value exists already */
478                                                         snprintf( textbuf, textlen,
479                                                                 "%s: value #%d provided more than once",
480                                                                 mods->sml_desc->ad_cname.bv_val, j );
481                                                         return LDAP_TYPE_OR_VALUE_EXISTS;
482                                                 }
483                                         }
484                                 }
485 #else
486                                 rc = modify_check_duplicates( mods->sml_desc, mr,
487                                                 NULL, mods->sml_bvalues, 0,
488                                                 text, textbuf, textlen );
489 #endif
490                                 if ( rc != LDAP_SUCCESS ) {
491                                         return rc;
492                                 }
493                         }
494                 }
495
496                 attr = ch_calloc( 1, sizeof(Attribute) );
497
498                 /* move ad to attr structure */
499                 attr->a_desc = mods->sml_desc;
500                 mods->sml_desc = NULL;
501
502                 /* move values to attr structure */
503                 /*      should check for duplicates */
504                 attr->a_vals = mods->sml_values;
505                 mods->sml_values = NULL;
506
507 #ifdef SLAP_NVALUES
508                 if ( mods->sml_nvalues ) {
509                         attr->a_nvals = mods->sml_nvalues;
510                         mods->sml_nvalues = NULL;
511                 } else {
512                         attr->a_nvals = attr->a_vals;
513                 }
514 #endif
515
516                 *tail = attr;
517                 tail = &attr->a_next;
518         }
519
520         return LDAP_SUCCESS;
521 }
522
523 #ifdef LDAP_SLAPI
524 static void initAddPlugin( Operation *op,
525         struct berval *dn, Entry *e, int manageDSAit )
526 {
527         slapi_x_pblock_set_operation( op->o_pb, op );
528         slapi_pblock_set( op->o_pb, SLAPI_ADD_TARGET, (void *)dn->bv_val );
529         slapi_pblock_set( op->o_pb, SLAPI_ADD_ENTRY, (void *)e );
530         slapi_pblock_set( op->o_pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
531 }
532
533 static int doPreAddPluginFNs( Operation *op )
534 {
535         int rc;
536
537         rc = doPluginFNs( op->o_bd, SLAPI_PLUGIN_PRE_ADD_FN, op->o_pb );
538         if ( rc != 0 ) {
539                 /*
540                  * A preoperation plugin failure will abort the
541                  * entire operation.
542                  */
543 #ifdef NEW_LOGGING
544                 LDAP_LOG( OPERATION, INFO, "do_add: add preoperation plugin failed\n",
545                                 0, 0, 0);
546 #else
547                 Debug(LDAP_DEBUG_TRACE, "do_add: add preoperation plugin failed.\n",
548                                 0, 0, 0);
549                 if ( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE, (void *)&rc ) != 0 )
550                         rc = LDAP_OTHER;
551 #endif
552         } else {
553                 rc = LDAP_SUCCESS;
554         }
555
556         return rc;
557 }
558
559 static void doPostAddPluginFNs( Operation *op )
560 {
561         int rc;
562
563         rc = doPluginFNs( op->o_bd, SLAPI_PLUGIN_POST_ADD_FN, op->o_pb );
564         if ( rc != 0 ) {
565 #ifdef NEW_LOGGING
566                 LDAP_LOG( OPERATION, INFO, "do_add: add postoperation plugin failed\n",
567                                 0, 0, 0);
568 #else
569                 Debug(LDAP_DEBUG_TRACE, "do_add: add preoperation plugin failed.\n",
570                                 0, 0, 0);
571 #endif
572         }
573 }
574 #endif /* LDAP_SLAPI */