]> git.sur5r.net Git - openldap/blob - servers/slapd/add.c
d0fcef1dbed7ffbbc13ba20799f2b8e763080884
[openldap] / servers / slapd / add.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2005 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in the file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
16  * All rights reserved.
17  *
18  * Redistribution and use in source and binary forms are permitted
19  * provided that this notice is preserved and that due credit is given
20  * to the University of Michigan at Ann Arbor. The name of the University
21  * may not be used to endorse or promote products derived from this
22  * software without specific prior written permission. This software
23  * is provided ``as is'' without express or implied warranty.
24  */
25
26 #include "portable.h"
27
28 #include <stdio.h>
29 #include <ac/string.h>
30 #include <ac/time.h>
31 #include <ac/socket.h>
32
33 #include "slap.h"
34
35 int
36 do_add( Operation *op, SlapReply *rs )
37 {
38         BerElement      *ber = op->o_ber;
39         char            *last;
40         struct berval   dn = BER_BVNULL;
41         ber_len_t       len;
42         ber_tag_t       tag;
43         Modifications   *modlist = NULL;
44         Modifications   **modtail = &modlist;
45         Modifications   tmp;
46         char            textbuf[ SLAP_TEXT_BUFLEN ];
47         size_t          textlen = sizeof( textbuf );
48         int             rc = 0;
49
50         Debug( LDAP_DEBUG_TRACE, "do_add\n", 0, 0, 0 );
51         /*
52          * Parse the add request.  It looks like this:
53          *
54          *      AddRequest := [APPLICATION 14] SEQUENCE {
55          *              name    DistinguishedName,
56          *              attrs   SEQUENCE OF SEQUENCE {
57          *                      type    AttributeType,
58          *                      values  SET OF AttributeValue
59          *              }
60          *      }
61          */
62
63         /* get the name */
64         if ( ber_scanf( ber, "{m", /*}*/ &dn ) == LBER_ERROR ) {
65                 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
66                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
67                 return SLAPD_DISCONNECT;
68         }
69
70         op->ora_e = (Entry *) ch_calloc( 1, sizeof(Entry) );
71
72         rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn,
73                 op->o_tmpmemctx );
74
75         if ( rs->sr_err != LDAP_SUCCESS ) {
76                 Debug( LDAP_DEBUG_ANY, "do_add: invalid dn (%s)\n", dn.bv_val, 0, 0 );
77                 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
78                 goto done;
79         }
80
81         ber_dupbv( &op->ora_e->e_name, &op->o_req_dn );
82         ber_dupbv( &op->ora_e->e_nname, &op->o_req_ndn );
83
84         Debug( LDAP_DEBUG_ARGS, "do_add: dn (%s)\n", op->ora_e->e_dn, 0, 0 );
85
86         /* get the attrs */
87         for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
88             tag = ber_next_element( ber, &len, last ) )
89         {
90                 Modifications *mod;
91                 ber_tag_t rtag;
92
93                 tmp.sml_nvalues = NULL;
94
95                 rtag = ber_scanf( ber, "{m{W}}", &tmp.sml_type, &tmp.sml_values );
96
97                 if ( rtag == LBER_ERROR ) {
98                         Debug( LDAP_DEBUG_ANY, "do_add: decoding error\n", 0, 0, 0 );
99                         send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
100                         rs->sr_err = SLAPD_DISCONNECT;
101                         goto done;
102                 }
103
104                 if ( tmp.sml_values == NULL ) {
105                         Debug( LDAP_DEBUG_ANY, "no values for type %s\n",
106                                 tmp.sml_type.bv_val, 0, 0 );
107                         send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
108                                 "no values for attribute type" );
109                         goto done;
110                 }
111
112                 mod  = (Modifications *) ch_malloc( sizeof(Modifications) );
113                 mod->sml_op = LDAP_MOD_ADD;
114                 mod->sml_flags = 0;
115                 mod->sml_next = NULL;
116                 mod->sml_desc = NULL;
117                 mod->sml_type = tmp.sml_type;
118                 mod->sml_values = tmp.sml_values;
119                 mod->sml_nvalues = NULL;
120
121                 *modtail = mod;
122                 modtail = &mod->sml_next;
123         }
124
125         if ( ber_scanf( ber, /*{*/ "}") == LBER_ERROR ) {
126                 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
127                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
128                 rs->sr_err = SLAPD_DISCONNECT;
129                 goto done;
130         }
131
132         if ( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
133                 Debug( LDAP_DEBUG_ANY, "do_add: get_ctrls failed\n", 0, 0, 0 );
134                 goto done;
135         } 
136
137         if ( modlist == NULL ) {
138                 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
139                         "no attributes provided" );
140                 goto done;
141         }
142
143         Statslog( LDAP_DEBUG_STATS, "%s ADD dn=\"%s\"\n",
144             op->o_log_prefix, op->ora_e->e_name.bv_val, 0, 0, 0 );
145
146         if ( dn_match( &op->ora_e->e_nname, &slap_empty_bv ) ) {
147                 /* protocolError may be a more appropriate error */
148                 send_ldap_error( op, rs, LDAP_ALREADY_EXISTS,
149                         "root DSE already exists" );
150                 goto done;
151
152         } else if ( dn_match( &op->ora_e->e_nname, &frontendDB->be_schemandn ) ) {
153                 send_ldap_error( op, rs, LDAP_ALREADY_EXISTS,
154                         "subschema subentry already exists" );
155                 goto done;
156         }
157
158         rs->sr_err = slap_mods_check( modlist, &rs->sr_text,
159                 textbuf, textlen, NULL );
160
161         if ( rs->sr_err != LDAP_SUCCESS ) {
162                 send_ldap_result( op, rs );
163                 goto done;
164         }
165
166         /* temporary; remove if not invoking backend function */
167         op->ora_modlist = modlist;
168
169         /* call this so global overlays/SLAPI have access to ora_e */
170         rs->sr_err = slap_mods2entry( op->ora_modlist, &op->ora_e,
171                 1, 0, &rs->sr_text, textbuf, textlen );
172         if ( rs->sr_err != LDAP_SUCCESS ) {
173                 send_ldap_result( op, rs );
174                 goto done;
175         }
176
177         op->o_bd = frontendDB;
178         rc = frontendDB->be_add( op, rs );
179         if ( rc == 0 ) {
180                 if ( op->ora_e != NULL && op->o_private != NULL ) {
181                         BackendDB       *bd = op->o_bd;
182
183                         op->o_bd = (BackendDB *)op->o_private;
184                         op->o_private = NULL;
185
186                         be_entry_release_w( op, op->ora_e );
187
188                         op->ora_e = NULL;
189                         op->o_bd = bd;
190                         op->o_private = NULL;
191                 }
192         }
193
194 done:;
195         slap_graduate_commit_csn( op );
196
197         if ( modlist != NULL ) {
198                 slap_mods_free( modlist );
199         }
200         if ( op->ora_e != NULL ) {
201                 entry_free( op->ora_e );
202         }
203         op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
204         op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
205
206         return rc;
207 }
208
209 int
210 fe_op_add( Operation *op, SlapReply *rs )
211 {
212         int             manageDSAit;
213         Modifications   *modlist = op->ora_modlist;
214         Modifications   **modtail = &modlist;
215         int             rc = 0;
216         BackendDB *op_be;
217         char            textbuf[ SLAP_TEXT_BUFLEN ];
218         size_t          textlen = sizeof( textbuf );
219
220         manageDSAit = get_manageDSAit( op );
221
222         /*
223          * We could be serving multiple database backends.  Select the
224          * appropriate one, or send a referral to our "referral server"
225          * if we don't hold it.
226          */
227         op->o_bd = select_backend( &op->ora_e->e_nname, manageDSAit, 1 );
228         if ( op->o_bd == NULL ) {
229                 rs->sr_ref = referral_rewrite( default_referral,
230                         NULL, &op->ora_e->e_name, LDAP_SCOPE_DEFAULT );
231                 if ( !rs->sr_ref ) rs->sr_ref = default_referral;
232                 if ( rs->sr_ref ) {
233                         rs->sr_err = LDAP_REFERRAL;
234                         op->o_bd = frontendDB;
235                         send_ldap_result( op, rs );
236                         op->o_bd = NULL;
237
238                         if ( rs->sr_ref != default_referral ) {
239                                 ber_bvarray_free( rs->sr_ref );
240                         }
241                 } else {
242                         op->o_bd = frontendDB;
243                         send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
244                                 "no global superior knowledge" );
245                         op->o_bd = NULL;
246                 }
247                 goto done;
248         }
249
250         /* If we've got a glued backend, check the real backend */
251         op_be = op->o_bd;
252         if ( SLAP_GLUE_INSTANCE( op->o_bd )) {
253                 op->o_bd = select_backend( &op->ora_e->e_nname, manageDSAit, 0 );
254         }
255
256         /* check restrictions */
257         if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
258                 send_ldap_result( op, rs );
259                 goto done;
260         }
261
262         /* check for referrals */
263         if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
264                 goto done;
265         }
266
267         rs->sr_err = slap_mods_obsolete_check( op, modlist,
268                 &rs->sr_text, textbuf, textlen );
269
270         if ( rs->sr_err != LDAP_SUCCESS ) {
271                 send_ldap_result( op, rs );
272                 goto done;
273         }
274
275         /*
276          * do the add if 1 && (2 || 3)
277          * 1) there is an add function implemented in this backend;
278          * 2) this backend is master for what it holds;
279          * 3) it's a replica and the dn supplied is the updatedn.
280          */
281         if ( op->o_bd->be_add ) {
282                 /* do the update here */
283                 int repl_user = be_isupdate( op );
284 #ifndef SLAPD_MULTIMASTER
285                 if ( !SLAP_SHADOW(op->o_bd) || repl_user )
286 #endif
287                 {
288                         int             update = !BER_BVISEMPTY( &op->o_bd->be_update_ndn );
289                         slap_callback   cb = { NULL, slap_replog_cb, NULL, NULL };
290
291                         op->o_bd = op_be;
292
293                         if ( !update ) {
294                                 rs->sr_err = slap_mods_no_user_mod_check( op, modlist,
295                                         &rs->sr_text, textbuf, textlen );
296
297                                 if ( rs->sr_err != LDAP_SUCCESS ) {
298                                         send_ldap_result( op, rs );
299                                         goto done;
300                                 }
301                         }
302
303                         if ( !repl_user ) {
304                                 /* go to the last mod */
305                                 for ( modtail = &modlist;
306                                                 *modtail != NULL;
307                                                 modtail = &(*modtail)->sml_next )
308                                 {
309                                         assert( (*modtail)->sml_op == LDAP_MOD_ADD );
310                                         assert( (*modtail)->sml_desc != NULL );
311                                 }
312
313
314                                 rs->sr_err = slap_mods_opattrs( op, modlist,
315                                                 modtail, &rs->sr_text,
316                                                 textbuf, textlen, 1 );
317                                 if ( rs->sr_err != LDAP_SUCCESS ) {
318                                         send_ldap_result( op, rs );
319                                         goto done;
320                                 }
321
322                                 /* check for duplicate values */
323                                 rs->sr_err = slap_mods_no_repl_user_mod_check( op,
324                                         modlist, &rs->sr_text, textbuf, textlen );
325                                 if ( rs->sr_err != LDAP_SUCCESS ) {
326                                         send_ldap_result( op, rs );
327                                         goto done;
328                                 }
329
330                                 rs->sr_err = slap_mods2entry( *modtail, &op->ora_e,
331                                         0, 0, &rs->sr_text, textbuf, textlen );
332                                 if ( rs->sr_err != LDAP_SUCCESS ) {
333                                         send_ldap_result( op, rs );
334                                         goto done;
335                                 }
336                         }
337
338 #ifdef SLAPD_MULTIMASTER
339                         if ( !repl_user )
340 #endif
341                         {
342                                 cb.sc_next = op->o_callback;
343                                 op->o_callback = &cb;
344                         }
345                         rc = op->o_bd->be_add( op, rs );
346                         if ( rc == LDAP_SUCCESS ) {
347                                 /* NOTE: be_entry_release_w() is
348                                  * called by do_add(), so that global
349                                  * overlays on the way back can
350                                  * at least read the entry */
351                                 op->o_private = op->o_bd;
352                         }
353
354 #ifndef SLAPD_MULTIMASTER
355                 } else {
356                         BerVarray defref = NULL;
357
358                         defref = op->o_bd->be_update_refs
359                                 ? op->o_bd->be_update_refs : default_referral;
360
361                         if ( defref != NULL ) {
362                                 rs->sr_ref = referral_rewrite( defref,
363                                         NULL, &op->ora_e->e_name, LDAP_SCOPE_DEFAULT );
364                                 if ( rs->sr_ref == NULL ) rs->sr_ref = defref;
365                                 rs->sr_err = LDAP_REFERRAL;
366                                 if (!rs->sr_ref) rs->sr_ref = default_referral;
367                                 send_ldap_result( op, rs );
368
369                                 if ( rs->sr_ref != default_referral ) {
370                                         ber_bvarray_free( rs->sr_ref );
371                                 }
372                         } else {
373                                 send_ldap_error( op, rs,
374                                         LDAP_UNWILLING_TO_PERFORM,
375                                         "shadow context; no update referral" );
376                         }
377 #endif /* SLAPD_MULTIMASTER */
378                 }
379         } else {
380             Debug( LDAP_DEBUG_ARGS, "    do_add: no backend support\n", 0, 0, 0 );
381             send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
382                         "operation not supported within namingContext" );
383         }
384
385 done:;
386         return rc;
387 }
388
389 int
390 slap_mods2entry(
391         Modifications *mods,
392         Entry **e,
393         int initial,
394         int dup,
395         const char **text,
396         char *textbuf, size_t textlen )
397 {
398         Attribute **tail;
399
400         if ( initial ) {
401                 assert( (*e)->e_attrs == NULL );
402         }
403
404         for ( tail = &(*e)->e_attrs; *tail != NULL; tail = &(*tail)->a_next )
405                 ;
406
407         *text = textbuf;
408
409         for( ; mods != NULL; mods = mods->sml_next ) {
410                 Attribute *attr;
411
412                 assert( mods->sml_desc != NULL );
413
414                 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
415
416                 if( attr != NULL ) {
417 #define SLURPD_FRIENDLY
418 #ifdef SLURPD_FRIENDLY
419                         ber_len_t i,j;
420
421                         if ( !initial ) {
422                                 /*      
423                                  * This check allows overlays to override operational
424                                  * attributes by setting them directly in the entry.
425                                  * We assume slap_mods_no_user_mod_check() was called
426                                  * with the user modifications.
427                                  */
428                                 *text = NULL;
429                                 return LDAP_SUCCESS;
430                         }
431
432                         for( i=0; attr->a_vals[i].bv_val; i++ ) {
433                                 /* count them */
434                         }
435                         for( j=0; mods->sml_values[j].bv_val; j++ ) {
436                                 /* count them */
437                         }
438                         j++;    /* NULL */
439                         
440                         attr->a_vals = ch_realloc( attr->a_vals,
441                                 sizeof( struct berval ) * (i+j) );
442
443                         /* should check for duplicates */
444
445                         if ( dup ) {
446                                 for ( j = 0; mods->sml_values[j].bv_val; j++ ) {
447                                         ber_dupbv( &attr->a_vals[i+j], &mods->sml_values[j] );
448                                 }
449                                 BER_BVZERO( &attr->a_vals[i+j] );       
450                         } else {
451                                 AC_MEMCPY( &attr->a_vals[i], mods->sml_values,
452                                         sizeof( struct berval ) * j );
453                                 ch_free( mods->sml_values );
454                                 mods->sml_values = NULL;
455                         }
456
457                         if( mods->sml_nvalues ) {
458                                 attr->a_nvals = ch_realloc( attr->a_nvals,
459                                         sizeof( struct berval ) * (i+j) );
460                                 if ( dup ) {
461                                         for ( j = 0; mods->sml_nvalues[j].bv_val; j++ ) {
462                                                 ber_dupbv( &attr->a_nvals[i+j], &mods->sml_nvalues[j] );
463                                         }
464                                         BER_BVZERO( &attr->a_nvals[i+j] );      
465                                 } else {
466                                         AC_MEMCPY( &attr->a_nvals[i], mods->sml_nvalues,
467                                                 sizeof( struct berval ) * j );
468                                         ch_free( mods->sml_nvalues );
469                                         mods->sml_nvalues = NULL;
470                                 }
471                         } else {
472                                 attr->a_nvals = attr->a_vals;
473                         }
474
475                         continue;
476 #else
477                         snprintf( textbuf, textlen,
478                                 "attribute '%s' provided more than once",
479                                 mods->sml_desc->ad_cname.bv_val );
480                         return LDAP_TYPE_OR_VALUE_EXISTS;
481 #endif
482                 }
483
484                 if( mods->sml_values[1].bv_val != NULL ) {
485                         /* check for duplicates */
486                         int             i, j, rc, match;
487                         MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
488
489                         for ( i = 1; mods->sml_values[i].bv_val != NULL; i++ ) {
490                                 /* test asserted values against themselves */
491                                 for( j = 0; j < i; j++ ) {
492                                         rc = ordered_value_match( &match, mods->sml_desc, mr,
493                                                 SLAP_MR_EQUALITY
494                                                 | SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX
495                                                 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
496                                                 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
497                                                 mods->sml_nvalues
498                                                         ? &mods->sml_nvalues[i]
499                                                         : &mods->sml_values[i],
500                                                 mods->sml_nvalues
501                                                         ? &mods->sml_nvalues[j]
502                                                         : &mods->sml_values[j],
503                                                 text );
504
505                                         if ( rc == LDAP_SUCCESS && match == 0 ) {
506                                                 /* value exists already */
507                                                 snprintf( textbuf, textlen,
508                                                         "%s: value #%d provided more than once",
509                                                         mods->sml_desc->ad_cname.bv_val, j );
510                                                 return LDAP_TYPE_OR_VALUE_EXISTS;
511
512                                         } else if ( rc != LDAP_SUCCESS ) {
513                                                 return rc;
514                                         }
515                                 }
516                         }
517                 }
518
519                 attr = ch_calloc( 1, sizeof(Attribute) );
520
521                 /* move ad to attr structure */
522                 attr->a_desc = mods->sml_desc;
523                 if ( !dup ) mods->sml_desc = NULL;
524
525                 /* move values to attr structure */
526                 /*      should check for duplicates */
527                 if ( dup ) { 
528                         int i;
529                         for ( i = 0; mods->sml_values[i].bv_val; i++ ) /* EMPTY */;
530                         attr->a_vals = (BerVarray) ch_calloc( i+1, sizeof( BerValue ));
531                         for ( i = 0; mods->sml_values[i].bv_val; i++ ) {
532                                 ber_dupbv( &attr->a_vals[i], &mods->sml_values[i] );
533                         }
534                         BER_BVZERO( &attr->a_vals[i] );
535                 } else {
536                         attr->a_vals = mods->sml_values;
537                         mods->sml_values = NULL;
538                 }
539
540                 if ( mods->sml_nvalues ) {
541                         if ( dup ) {
542                                 int i;
543                                 for ( i = 0; mods->sml_nvalues[i].bv_val; i++ ) /* EMPTY */;
544                                 attr->a_nvals = (BerVarray) ch_calloc( i+1, sizeof( BerValue ));
545                                 for ( i = 0; mods->sml_nvalues[i].bv_val; i++ ) {
546                                         ber_dupbv( &attr->a_nvals[i], &mods->sml_nvalues[i] );
547                                 }
548                                 BER_BVZERO( &attr->a_nvals[i] );
549                         } else {
550                                 attr->a_nvals = mods->sml_nvalues;
551                                 mods->sml_nvalues = NULL;
552                         }
553                 } else {
554                         attr->a_nvals = attr->a_vals;
555                 }
556
557                 *tail = attr;
558                 tail = &attr->a_next;
559         }
560
561         *text = NULL;
562
563         return LDAP_SUCCESS;
564 }
565
566 int
567 slap_entry2mods(
568         Entry *e,
569         Modifications **mods,
570         const char **text,
571         char *textbuf, size_t textlen )
572 {
573         Modifications   *modhead = NULL;
574         Modifications   *mod;
575         Modifications   **modtail = &modhead;
576         Attribute               *a_new;
577         AttributeDescription    *a_new_desc;
578         int                             i, count;
579
580         a_new = e->e_attrs;
581
582         while ( a_new != NULL ) {
583                 a_new_desc = a_new->a_desc;
584                 mod = (Modifications *) malloc( sizeof( Modifications ));
585                 
586                 mod->sml_op = LDAP_MOD_REPLACE;
587                 mod->sml_flags = 0;
588
589                 mod->sml_type = a_new_desc->ad_cname;
590
591                 for ( count = 0; a_new->a_vals[count].bv_val; count++ ) /* EMPTY */;
592
593                 mod->sml_values = (struct berval*) malloc(
594                         (count+1) * sizeof( struct berval) );
595
596                 /* see slap_mods_check() comments...
597                  * if a_vals == a_nvals, there is no normalizer.
598                  * in this case, mod->sml_nvalues must be left NULL.
599                  */
600                 if ( a_new->a_vals != a_new->a_nvals ) {
601                         mod->sml_nvalues = (struct berval*) malloc(
602                                 (count+1) * sizeof( struct berval) );
603                 } else {
604                         mod->sml_nvalues = NULL;
605                 }
606
607                 for ( i = 0; i < count; i++ ) {
608                         ber_dupbv(mod->sml_values+i, a_new->a_vals+i); 
609                         if ( mod->sml_nvalues ) {
610                                 ber_dupbv( mod->sml_nvalues+i, a_new->a_nvals+i ); 
611                         } 
612                 }
613
614                 mod->sml_values[count].bv_val = NULL; 
615                 mod->sml_values[count].bv_len = 0; 
616
617                 if ( mod->sml_nvalues ) {
618                         mod->sml_nvalues[count].bv_val = NULL; 
619                         mod->sml_nvalues[count].bv_len = 0; 
620                 }
621
622                 mod->sml_desc = a_new_desc;
623                 mod->sml_next =NULL;
624                 *modtail = mod;
625                 modtail = &mod->sml_next;
626                 a_new = a_new->a_next; 
627         }
628
629         *mods = modhead;
630
631         return LDAP_SUCCESS;
632 }
633