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