]> git.sur5r.net Git - openldap/blob - servers/slapd/add.c
Fix up duplicate error reporting
[openldap] / servers / slapd / add.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*
7  * Copyright (c) 1995 Regents of the University of Michigan.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms are permitted
11  * provided that this notice is preserved and that due credit is given
12  * to the University of Michigan at Ann Arbor. The name of the University
13  * may not be used to endorse or promote products derived from this
14  * software without specific prior written permission. This software
15  * is provided ``as is'' without express or implied warranty.
16  */
17
18 #include "portable.h"
19
20 #include <stdio.h>
21 #include <ac/string.h>
22 #include <ac/time.h>
23 #include <ac/socket.h>
24
25 #include "ldap_pvt.h"
26 #include "slap.h"
27
28 static int slap_mods2entry(
29         Modifications *mods,
30         Entry **e,
31         int repl_user,
32         const char **text,
33         char *textbuf, size_t textlen );
34
35 int
36 do_add( Connection *conn, Operation *op )
37 {
38         BerElement      *ber = op->o_ber;
39         char            *last;
40         struct berval dn = { 0, NULL };
41         ber_len_t       len;
42         ber_tag_t       tag;
43         Entry           *e;
44         Backend         *be;
45         Modifications   *modlist = NULL;
46         Modifications   **modtail = &modlist;
47         Modifications   tmp;
48         const char *text;
49         int                     rc = LDAP_SUCCESS;
50         int     manageDSAit;
51
52 #ifdef NEW_LOGGING
53         LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY,
54                    "do_add: conn %d enter\n", conn->c_connid ));
55 #else
56         Debug( LDAP_DEBUG_TRACE, "do_add\n", 0, 0, 0 );
57 #endif
58         /*
59          * Parse the add request.  It looks like this:
60          *
61          *      AddRequest := [APPLICATION 14] SEQUENCE {
62          *              name    DistinguishedName,
63          *              attrs   SEQUENCE OF SEQUENCE {
64          *                      type    AttributeType,
65          *                      values  SET OF AttributeValue
66          *              }
67          *      }
68          */
69
70         /* get the name */
71         if ( ber_scanf( ber, "{m", /*}*/ &dn ) == LBER_ERROR ) {
72 #ifdef NEW_LOGGING
73                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
74                         "do_add: conn %d ber_scanf failed\n", conn->c_connid ));
75 #else
76                 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
77 #endif
78                 send_ldap_disconnect( conn, op,
79                         LDAP_PROTOCOL_ERROR, "decoding error" );
80                 return -1;
81         }
82
83         e = (Entry *) ch_calloc( 1, sizeof(Entry) );
84         e->e_name.bv_val = NULL;
85         e->e_name.bv_len = 0;
86         e->e_nname.bv_val = NULL;
87         e->e_nname.bv_len = 0;
88         e->e_attrs = NULL;
89         e->e_private = NULL;
90
91         rc = dnPrettyNormal( NULL, &dn, &e->e_name, &e->e_nname );
92
93         if( rc != LDAP_SUCCESS ) {
94 #ifdef NEW_LOGGING
95                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
96                         "do_add: conn %d invalid dn (%s)\n", conn->c_connid,
97                                 dn.bv_val ));
98 #else
99                 Debug( LDAP_DEBUG_ANY, "do_add: invalid dn (%s)\n", dn.bv_val, 0, 0 );
100 #endif
101                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
102                             "invalid DN", NULL, NULL );
103                 goto done;
104         }
105
106 #ifdef NEW_LOGGING
107         LDAP_LOG(( "operation", LDAP_LEVEL_ARGS,
108                 "do_add: conn %d  dn (%s)\n", conn->c_connid, e->e_dn ));
109 #else
110         Debug( LDAP_DEBUG_ARGS, "do_add: dn (%s)\n", e->e_dn, 0, 0 );
111 #endif
112
113         /* get the attrs */
114         for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
115             tag = ber_next_element( ber, &len, last ) )
116         {
117                 Modifications *mod;
118
119                 rc = ber_scanf( ber, "{m{W}}", &tmp.sml_type, &tmp.sml_bvalues );
120
121                 if ( rc == LBER_ERROR ) {
122 #ifdef NEW_LOGGING
123                         LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
124                                    "do_add: conn %d      decoding error \n", conn->c_connid ));
125 #else
126                         Debug( LDAP_DEBUG_ANY, "do_add: decoding error\n", 0, 0, 0 );
127 #endif
128                         send_ldap_disconnect( conn, op,
129                                 LDAP_PROTOCOL_ERROR, "decoding error" );
130                         rc = -1;
131                         goto done;
132                 }
133
134                 if ( tmp.sml_bvalues == NULL ) {
135 #ifdef NEW_LOGGING
136                         LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
137                                 "do_add: conn %d         no values for type %s\n",
138                                 conn->c_connid, tmp.sml_type.bv_val ));
139 #else
140                         Debug( LDAP_DEBUG_ANY, "no values for type %s\n",
141                                 tmp.sml_type.bv_val, 0, 0 );
142 #endif
143                         send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
144                                 NULL, "no values for attribute type", NULL, NULL );
145                         free( tmp.sml_type.bv_val );
146                         goto done;
147                 }
148                 mod  = (Modifications *) ch_malloc( sizeof(Modifications) );
149                 
150                 mod->sml_op = LDAP_MOD_ADD;
151                 mod->sml_next = NULL;
152                 mod->sml_desc = NULL;
153                 mod->sml_type = tmp.sml_type;
154                 mod->sml_bvalues = tmp.sml_bvalues;
155
156                 *modtail = mod;
157                 modtail = &mod->sml_next;
158         }
159
160         if ( ber_scanf( ber, /*{*/ "}") == LBER_ERROR ) {
161 #ifdef NEW_LOGGING
162                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
163                         "do_add: conn %d ber_scanf failed\n", conn->c_connid ));
164 #else
165                 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
166 #endif
167                 send_ldap_disconnect( conn, op,
168                         LDAP_PROTOCOL_ERROR, "decoding error" );
169                 rc = -1;
170                 goto done;
171         }
172
173         if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
174 #ifdef NEW_LOGGING
175                 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
176                         "do_add: conn %d get_ctrls failed\n", conn->c_connid ));
177 #else
178                 Debug( LDAP_DEBUG_ANY, "do_add: get_ctrls failed\n", 0, 0, 0 );
179 #endif
180                 goto done;
181         } 
182
183         if ( modlist == NULL ) {
184                 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
185                         NULL, "no attributes provided", NULL, NULL );
186                 goto done;
187         }
188
189         Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d ADD dn=\"%s\"\n",
190             op->o_connid, op->o_opid, e->e_dn, 0, 0 );
191
192         if( e->e_nname.bv_len == 0 ) {
193                 /* protocolError may be a more appropriate error */
194                 send_ldap_result( conn, op, rc = LDAP_ALREADY_EXISTS,
195                         NULL, "root DSE already exists",
196                         NULL, NULL );
197                 goto done;
198
199 #if defined( SLAPD_SCHEMA_DN )
200         } else if ( strcasecmp( e->e_ndn, SLAPD_SCHEMA_DN ) == 0 ) {
201                 send_ldap_result( conn, op, rc = LDAP_ALREADY_EXISTS,
202                         NULL, "subschema subentry already exists",
203                         NULL, NULL );
204                 goto done;
205 #endif
206         }
207
208         manageDSAit = get_manageDSAit( op );
209
210         /*
211          * We could be serving multiple database backends.  Select the
212          * appropriate one, or send a referral to our "referral server"
213          * if we don't hold it.
214          */
215         be = select_backend( &e->e_nname, manageDSAit, 0 );
216         if ( be == NULL ) {
217                 BerVarray ref = referral_rewrite( default_referral,
218                         NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
219
220                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
221                         NULL, NULL, ref ? ref : default_referral, NULL );
222
223                 if ( ref ) ber_bvarray_free( ref );
224                 goto done;
225         }
226
227         /* check restrictions */
228         rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
229         if( rc != LDAP_SUCCESS ) {
230                 send_ldap_result( conn, op, rc,
231                         NULL, text, NULL, NULL );
232                 goto done;
233         }
234
235         /* check for referrals */
236         rc = backend_check_referrals( be, conn, op, &e->e_name, &e->e_nname );
237         if ( rc != LDAP_SUCCESS ) {
238                 goto done;
239         }
240
241         /*
242          * do the add if 1 && (2 || 3)
243          * 1) there is an add function implemented in this backend;
244          * 2) this backend is master for what it holds;
245          * 3) it's a replica and the dn supplied is the updatedn.
246          */
247         if ( be->be_add ) {
248                 /* do the update here */
249                 int repl_user = be_isupdate(be, &op->o_ndn );
250 #ifndef SLAPD_MULTIMASTER
251                 if ( !be->be_update_ndn.bv_len || repl_user )
252 #endif
253                 {
254                         int update = be->be_update_ndn.bv_len;
255                         char textbuf[SLAP_TEXT_BUFLEN];
256                         size_t textlen = sizeof textbuf;
257
258                         rc = slap_mods_check( modlist, update, &text,
259                                 textbuf, textlen );
260
261                         if( rc != LDAP_SUCCESS ) {
262                                 send_ldap_result( conn, op, rc,
263                                         NULL, text, NULL, NULL );
264                                 goto done;
265                         }
266
267                         if ( SLAP_LASTMOD(be) && !repl_user ) {
268                                 for( modtail = &modlist;
269                                         *modtail != NULL;
270                                         modtail = &(*modtail)->sml_next )
271                                 {
272                                         assert( (*modtail)->sml_op == LDAP_MOD_ADD );
273                                         assert( (*modtail)->sml_desc != NULL );
274                                 }
275                                 rc = slap_mods_opattrs( op, modlist, modtail, &text,
276                                         textbuf, textlen );
277                                 if( rc != LDAP_SUCCESS ) {
278                                         send_ldap_result( conn, op, rc,
279                                                 NULL, text, NULL, NULL );
280                                         goto done;
281                                 }
282                         }
283
284                         rc = slap_mods2entry( modlist, &e, repl_user, &text,
285                                 textbuf, textlen );
286                         if( rc != LDAP_SUCCESS ) {
287                                 send_ldap_result( conn, op, rc,
288                                         NULL, text, NULL, NULL );
289                                 goto done;
290                         }
291
292                         if ( (*be->be_add)( be, conn, op, e ) == 0 ) {
293 #ifdef SLAPD_MULTIMASTER
294                                 if ( !repl_user )
295 #endif
296                                 {
297                                         replog( be, op, &e->e_name, &e->e_nname, e );
298                                 }
299                                 be_entry_release_w( be, conn, op, e );
300                                 e = NULL;
301                         }
302
303 #ifndef SLAPD_MULTIMASTER
304                 } else {
305                         BerVarray defref = be->be_update_refs
306                                 ? be->be_update_refs : default_referral;
307                         BerVarray ref = referral_rewrite( defref,
308                                 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
309
310                         send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
311                                 ref ? ref : defref, NULL );
312
313                         if ( ref ) ber_bvarray_free( ref );
314 #endif
315                 }
316         } else {
317 #ifdef NEW_LOGGING
318             LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
319                        "do_add: conn %d  no backend support\n", conn->c_connid ));
320 #else
321             Debug( LDAP_DEBUG_ARGS, "    do_add: no backend support\n", 0, 0, 0 );
322 #endif
323             send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
324                               NULL, "operation not supported within namingContext", NULL, NULL );
325         }
326
327 done:
328         if( modlist != NULL ) {
329                 slap_mods_free( modlist );
330         }
331         if( e != NULL ) {
332                 entry_free( e );
333         }
334
335         return rc;
336 }
337
338 static int slap_mods2entry(
339         Modifications *mods,
340         Entry **e,
341         int repl_user,
342         const char **text,
343         char *textbuf, size_t textlen )
344 {
345         Attribute **tail = &(*e)->e_attrs;
346         assert( *tail == NULL );
347
348         *text = textbuf;
349
350         for( ; mods != NULL; mods = mods->sml_next ) {
351                 Attribute *attr;
352
353                 assert( mods->sml_op == LDAP_MOD_ADD );
354                 assert( mods->sml_desc != NULL );
355
356                 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
357
358                 if( attr != NULL ) {
359 #define SLURPD_FRIENDLY
360 #ifdef SLURPD_FRIENDLY
361                         ber_len_t i,j;
362
363                         if( !repl_user ) {
364                                 snprintf( textbuf, textlen,
365                                         "attribute '%s' provided more than once",
366                                         mods->sml_desc->ad_cname.bv_val );
367                                 return LDAP_TYPE_OR_VALUE_EXISTS;
368                         }
369
370                         for( i=0; attr->a_vals[i].bv_val; i++ ) {
371                                 /* count them */
372                         }
373                         for( j=0; mods->sml_bvalues[j].bv_val; j++ ) {
374                                 /* count them */
375                         }
376                         j++;    /* NULL */
377                         
378                         attr->a_vals = ch_realloc( attr->a_vals,
379                                 sizeof( struct berval ) * (i+j) );
380
381                         /* should check for duplicates */
382
383                         AC_MEMCPY( &attr->a_vals[i], mods->sml_bvalues,
384                                 sizeof( struct berval ) * j );
385
386                         /* trim the mods array */
387                         ch_free( mods->sml_bvalues );
388                         mods->sml_bvalues = NULL;
389
390                         continue;
391 #else
392                         snprintf( textbuf, textlen,
393                                 "attribute '%s' provided more than once",
394                                 mods->sml_desc->ad_cname.bv_val );
395                         return LDAP_TYPE_OR_VALUE_EXISTS;
396 #endif
397                 }
398
399                 if( mods->sml_bvalues[1].bv_val != NULL ) {
400                         /* check for duplicates */
401                         int             i, j;
402                         MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
403
404                         /* check if the values we're adding already exist */
405                         if( mr == NULL || !mr->smr_match ) {
406                                 for ( i = 0; mods->sml_bvalues[i].bv_val != NULL; i++ ) {
407                                         /* test asserted values against themselves */
408                                         for( j = 0; j < i; j++ ) {
409                                                 int rc = ber_bvcmp( &mods->sml_bvalues[i],
410                                                         &mods->sml_bvalues[j] );
411
412                                                 if( rc == 0 ) {
413                                                         /* value exists already */
414                                                         snprintf( textbuf, textlen,
415                                                                 "%s: value #%d provided more than once",
416                                                                 mods->sml_desc->ad_cname.bv_val, j );
417                                                         return LDAP_TYPE_OR_VALUE_EXISTS;
418                                                 }
419                                         }
420                                 }
421
422                         } else {
423                                 for ( i = 0; mods->sml_bvalues[i].bv_val != NULL; i++ ) {
424                                         int rc, match;
425                                         const char *text = NULL;
426                                         struct berval asserted;
427
428                                         rc = value_normalize( mods->sml_desc,
429                                                 SLAP_MR_EQUALITY,
430                                                 &mods->sml_bvalues[i],
431                                                 &asserted,
432                                                 &text );
433
434                                         if( rc != LDAP_SUCCESS ) return rc;
435
436                                         for ( j = 0; j < i; j++ ) {
437                                                 int rc = value_match( &match, mods->sml_desc, mr,
438                                                         SLAP_MR_VALUE_SYNTAX_MATCH,
439                                                         &mods->sml_bvalues[j], &asserted, &text );
440
441                                                 if( rc == LDAP_SUCCESS && match == 0 ) {
442                                                         free( asserted.bv_val );
443                                                         snprintf( textbuf, textlen,
444                                                                 "%s: value #%d provided more than once",
445                                                                 mods->sml_desc->ad_cname.bv_val, j );
446                                                         return LDAP_TYPE_OR_VALUE_EXISTS;
447                                                 }
448                                         }
449
450                                         free( asserted.bv_val );
451                                 }
452                         }
453                 }
454
455                 attr = ch_calloc( 1, sizeof(Attribute) );
456
457                 /* move ad to attr structure */
458                 attr->a_desc = mods->sml_desc;
459                 mods->sml_desc = NULL;
460
461                 /* move values to attr structure */
462                 /*      should check for duplicates */
463                 attr->a_vals = mods->sml_bvalues;
464                 mods->sml_bvalues = NULL;
465
466                 *tail = attr;
467                 tail = &attr->a_next;
468         }
469
470         return LDAP_SUCCESS;
471 }