]> git.sur5r.net Git - openldap/blob - servers/slapd/add.c
7df87348aa1c7b1b5b8740e63044c4b5e5b24230
[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, 0 );
199         }
200
201         if ( op->ora_e != NULL ) {
202                 entry_free( op->ora_e );
203         }
204         op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
205         op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
206
207         return rc;
208 }
209
210 int
211 fe_op_add( Operation *op, SlapReply *rs )
212 {
213         int             manageDSAit;
214         Modifications   *modlist = op->ora_modlist;
215         Modifications   **modtail = &modlist;
216         int             rc = 0;
217         BackendDB *op_be;
218         char            textbuf[ SLAP_TEXT_BUFLEN ];
219         size_t          textlen = sizeof( textbuf );
220
221         manageDSAit = get_manageDSAit( op );
222
223         /*
224          * We could be serving multiple database backends.  Select the
225          * appropriate one, or send a referral to our "referral server"
226          * if we don't hold it.
227          */
228         op->o_bd = select_backend( &op->ora_e->e_nname, manageDSAit, 1 );
229         if ( op->o_bd == NULL ) {
230                 rs->sr_ref = referral_rewrite( default_referral,
231                         NULL, &op->ora_e->e_name, LDAP_SCOPE_DEFAULT );
232                 if ( !rs->sr_ref ) rs->sr_ref = default_referral;
233                 if ( rs->sr_ref ) {
234                         rs->sr_err = LDAP_REFERRAL;
235                         op->o_bd = frontendDB;
236                         send_ldap_result( op, rs );
237                         op->o_bd = NULL;
238
239                         if ( rs->sr_ref != default_referral ) {
240                                 ber_bvarray_free( rs->sr_ref );
241                         }
242                 } else {
243                         op->o_bd = frontendDB;
244                         send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
245                                 "no global superior knowledge" );
246                         op->o_bd = NULL;
247                 }
248                 goto done;
249         }
250
251         /* If we've got a glued backend, check the real backend */
252         op_be = op->o_bd;
253         if ( SLAP_GLUE_INSTANCE( op->o_bd )) {
254                 op->o_bd = select_backend( &op->ora_e->e_nname, manageDSAit, 0 );
255         }
256
257         /* check restrictions */
258         if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
259                 send_ldap_result( op, rs );
260                 goto done;
261         }
262
263         /* check for referrals */
264         if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
265                 goto done;
266         }
267
268         rs->sr_err = slap_mods_obsolete_check( op, modlist,
269                 &rs->sr_text, textbuf, textlen );
270
271         if ( rs->sr_err != LDAP_SUCCESS ) {
272                 send_ldap_result( op, rs );
273                 goto done;
274         }
275
276         /*
277          * do the add if 1 && (2 || 3)
278          * 1) there is an add function implemented in this backend;
279          * 2) this backend is master for what it holds;
280          * 3) it's a replica and the dn supplied is the updatedn.
281          */
282         if ( op->o_bd->be_add ) {
283                 /* do the update here */
284                 int repl_user = be_isupdate( op );
285 #ifndef SLAPD_MULTIMASTER
286                 if ( !SLAP_SHADOW(op->o_bd) || repl_user )
287 #endif
288                 {
289                         int             update = !BER_BVISEMPTY( &op->o_bd->be_update_ndn );
290                         slap_callback   cb = { NULL, slap_replog_cb, NULL, NULL };
291
292                         op->o_bd = op_be;
293
294                         if ( !update ) {
295                                 rs->sr_err = slap_mods_no_user_mod_check( op, modlist,
296                                         &rs->sr_text, textbuf, textlen );
297
298                                 if ( rs->sr_err != LDAP_SUCCESS ) {
299                                         send_ldap_result( op, rs );
300                                         goto done;
301                                 }
302                         }
303
304                         if ( !repl_user ) {
305                                 /* go to the last mod */
306                                 for ( modtail = &modlist;
307                                                 *modtail != NULL;
308                                                 modtail = &(*modtail)->sml_next )
309                                 {
310                                         assert( (*modtail)->sml_op == LDAP_MOD_ADD );
311                                         assert( (*modtail)->sml_desc != NULL );
312                                 }
313
314
315                                 rs->sr_err = slap_mods_opattrs( op, modlist,
316                                                 modtail, &rs->sr_text,
317                                                 textbuf, textlen, 1 );
318                                 if ( rs->sr_err != LDAP_SUCCESS ) {
319                                         send_ldap_result( op, rs );
320                                         goto done;
321                                 }
322
323                                 /* check for duplicate values */
324                                 rs->sr_err = slap_mods_no_repl_user_mod_check( op,
325                                         modlist, &rs->sr_text, textbuf, textlen );
326                                 if ( rs->sr_err != LDAP_SUCCESS ) {
327                                         send_ldap_result( op, rs );
328                                         goto done;
329                                 }
330
331                                 rs->sr_err = slap_mods2entry( *modtail, &op->ora_e,
332                                         0, 0, &rs->sr_text, textbuf, textlen );
333                                 if ( rs->sr_err != LDAP_SUCCESS ) {
334                                         send_ldap_result( op, rs );
335                                         goto done;
336                                 }
337                         }
338
339 #ifdef SLAPD_MULTIMASTER
340                         if ( !repl_user )
341 #endif
342                         {
343                                 cb.sc_next = op->o_callback;
344                                 op->o_callback = &cb;
345                         }
346                         rc = op->o_bd->be_add( op, rs );
347                         if ( rc == LDAP_SUCCESS ) {
348                                 /* NOTE: be_entry_release_w() is
349                                  * called by do_add(), so that global
350                                  * overlays on the way back can
351                                  * at least read the entry */
352                                 op->o_private = op->o_bd;
353                         }
354
355 #ifndef SLAPD_MULTIMASTER
356                 } else {
357                         BerVarray defref = NULL;
358
359                         defref = op->o_bd->be_update_refs
360                                 ? op->o_bd->be_update_refs : default_referral;
361
362                         if ( defref != NULL ) {
363                                 rs->sr_ref = referral_rewrite( defref,
364                                         NULL, &op->ora_e->e_name, LDAP_SCOPE_DEFAULT );
365                                 if ( rs->sr_ref == NULL ) rs->sr_ref = defref;
366                                 rs->sr_err = LDAP_REFERRAL;
367                                 if (!rs->sr_ref) rs->sr_ref = default_referral;
368                                 send_ldap_result( op, rs );
369
370                                 if ( rs->sr_ref != default_referral ) {
371                                         ber_bvarray_free( rs->sr_ref );
372                                 }
373                         } else {
374                                 send_ldap_error( op, rs,
375                                         LDAP_UNWILLING_TO_PERFORM,
376                                         "shadow context; no update referral" );
377                         }
378 #endif /* SLAPD_MULTIMASTER */
379                 }
380         } else {
381             Debug( LDAP_DEBUG_ARGS, "    do_add: no backend support\n", 0, 0, 0 );
382             send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
383                         "operation not supported within namingContext" );
384         }
385
386 done:;
387         return rc;
388 }
389
390 int
391 slap_mods2entry(
392         Modifications *mods,
393         Entry **e,
394         int initial,
395         int dup,
396         const char **text,
397         char *textbuf, size_t textlen )
398 {
399         Attribute **tail;
400
401         if ( initial ) {
402                 assert( (*e)->e_attrs == NULL );
403         }
404
405         for ( tail = &(*e)->e_attrs; *tail != NULL; tail = &(*tail)->a_next )
406                 ;
407
408         *text = textbuf;
409
410         for( ; mods != NULL; mods = mods->sml_next ) {
411                 Attribute *attr;
412
413                 assert( mods->sml_desc != NULL );
414
415                 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
416
417                 if( attr != NULL ) {
418 #define SLURPD_FRIENDLY
419 #ifdef SLURPD_FRIENDLY
420                         ber_len_t i,j;
421
422                         if ( !initial ) {
423                                 /*      
424                                  * This check allows overlays to override operational
425                                  * attributes by setting them directly in the entry.
426                                  * We assume slap_mods_no_user_mod_check() was called
427                                  * with the user modifications.
428                                  */
429                                 *text = NULL;
430                                 return LDAP_SUCCESS;
431                         }
432
433                         for( i=0; attr->a_vals[i].bv_val; i++ ) {
434                                 /* count them */
435                         }
436                         for( j=0; mods->sml_values[j].bv_val; j++ ) {
437                                 /* count them */
438                         }
439                         j++;    /* NULL */
440                         
441                         attr->a_vals = ch_realloc( attr->a_vals,
442                                 sizeof( struct berval ) * (i+j) );
443
444                         /* should check for duplicates */
445
446                         if ( dup ) {
447                                 for ( j = 0; mods->sml_values[j].bv_val; j++ ) {
448                                         ber_dupbv( &attr->a_vals[i+j], &mods->sml_values[j] );
449                                 }
450                                 BER_BVZERO( &attr->a_vals[i+j] );
451                                 j++;
452                         } else {
453                                 AC_MEMCPY( &attr->a_vals[i], mods->sml_values,
454                                         sizeof( struct berval ) * j );
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                                 }
469                         } else {
470                                 attr->a_nvals = attr->a_vals;
471                         }
472
473                         continue;
474 #else
475                         snprintf( textbuf, textlen,
476                                 "attribute '%s' provided more than once",
477                                 mods->sml_desc->ad_cname.bv_val );
478                         return LDAP_TYPE_OR_VALUE_EXISTS;
479 #endif
480                 }
481
482                 if( mods->sml_values[1].bv_val != NULL ) {
483                         /* check for duplicates */
484                         int             i, j, rc, match;
485                         MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
486
487                         for ( i = 1; mods->sml_values[i].bv_val != NULL; i++ ) {
488                                 /* test asserted values against themselves */
489                                 for( j = 0; j < i; j++ ) {
490                                         rc = ordered_value_match( &match, mods->sml_desc, mr,
491                                                 SLAP_MR_EQUALITY
492                                                 | SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX
493                                                 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
494                                                 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
495                                                 mods->sml_nvalues
496                                                         ? &mods->sml_nvalues[i]
497                                                         : &mods->sml_values[i],
498                                                 mods->sml_nvalues
499                                                         ? &mods->sml_nvalues[j]
500                                                         : &mods->sml_values[j],
501                                                 text );
502
503                                         if ( rc == LDAP_SUCCESS && match == 0 ) {
504                                                 /* value exists already */
505                                                 snprintf( textbuf, textlen,
506                                                         "%s: value #%d provided more than once",
507                                                         mods->sml_desc->ad_cname.bv_val, j );
508                                                 return LDAP_TYPE_OR_VALUE_EXISTS;
509
510                                         } else if ( rc != LDAP_SUCCESS ) {
511                                                 return rc;
512                                         }
513                                 }
514                         }
515                 }
516
517                 attr = ch_calloc( 1, sizeof(Attribute) );
518
519                 /* move ad to attr structure */
520                 attr->a_desc = mods->sml_desc;
521
522                 /* move values to attr structure */
523                 /*      should check for duplicates */
524                 if ( dup ) { 
525                         int i;
526                         for ( i = 0; mods->sml_values[i].bv_val; i++ ) /* EMPTY */;
527                         attr->a_vals = (BerVarray) ch_calloc( i+1, sizeof( BerValue ));
528                         for ( i = 0; mods->sml_values[i].bv_val; i++ ) {
529                                 ber_dupbv( &attr->a_vals[i], &mods->sml_values[i] );
530                         }
531                         BER_BVZERO( &attr->a_vals[i] );
532                 } else {
533                         attr->a_vals = mods->sml_values;
534                 }
535
536                 if ( mods->sml_nvalues ) {
537                         if ( dup ) {
538                                 int i;
539                                 for ( i = 0; mods->sml_nvalues[i].bv_val; i++ ) /* EMPTY */;
540                                 attr->a_nvals = (BerVarray) ch_calloc( i+1, sizeof( BerValue ));
541                                 for ( i = 0; mods->sml_nvalues[i].bv_val; i++ ) {
542                                         ber_dupbv( &attr->a_nvals[i], &mods->sml_nvalues[i] );
543                                 }
544                                 BER_BVZERO( &attr->a_nvals[i] );
545                         } else {
546                                 attr->a_nvals = mods->sml_nvalues;
547                         }
548                 } else {
549                         attr->a_nvals = attr->a_vals;
550                 }
551
552                 *tail = attr;
553                 tail = &attr->a_next;
554         }
555
556         *text = NULL;
557
558         return LDAP_SUCCESS;
559 }
560
561 int
562 slap_entry2mods(
563         Entry *e,
564         Modifications **mods,
565         const char **text,
566         char *textbuf, size_t textlen )
567 {
568         Modifications   *modhead = NULL;
569         Modifications   *mod;
570         Modifications   **modtail = &modhead;
571         Attribute               *a_new;
572         AttributeDescription    *a_new_desc;
573         int                             i, count;
574
575         a_new = e->e_attrs;
576
577         while ( a_new != NULL ) {
578                 a_new_desc = a_new->a_desc;
579                 mod = (Modifications *) malloc( sizeof( Modifications ));
580                 
581                 mod->sml_op = LDAP_MOD_REPLACE;
582                 mod->sml_flags = 0;
583
584                 mod->sml_type = a_new_desc->ad_cname;
585
586                 for ( count = 0; a_new->a_vals[count].bv_val; count++ ) /* EMPTY */;
587
588                 mod->sml_values = (struct berval*) malloc(
589                         (count+1) * sizeof( struct berval) );
590
591                 /* see slap_mods_check() comments...
592                  * if a_vals == a_nvals, there is no normalizer.
593                  * in this case, mod->sml_nvalues must be left NULL.
594                  */
595                 if ( a_new->a_vals != a_new->a_nvals ) {
596                         mod->sml_nvalues = (struct berval*) malloc(
597                                 (count+1) * sizeof( struct berval) );
598                 } else {
599                         mod->sml_nvalues = NULL;
600                 }
601
602                 for ( i = 0; i < count; i++ ) {
603                         ber_dupbv(mod->sml_values+i, a_new->a_vals+i); 
604                         if ( mod->sml_nvalues ) {
605                                 ber_dupbv( mod->sml_nvalues+i, a_new->a_nvals+i ); 
606                         } 
607                 }
608
609                 mod->sml_values[count].bv_val = NULL; 
610                 mod->sml_values[count].bv_len = 0; 
611
612                 if ( mod->sml_nvalues ) {
613                         mod->sml_nvalues[count].bv_val = NULL; 
614                         mod->sml_nvalues[count].bv_len = 0; 
615                 }
616
617                 mod->sml_desc = a_new_desc;
618                 mod->sml_next =NULL;
619                 *modtail = mod;
620                 modtail = &mod->sml_next;
621                 a_new = a_new->a_next; 
622         }
623
624         *mods = modhead;
625
626         return LDAP_SUCCESS;
627 }
628