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