]> git.sur5r.net Git - openldap/blob - servers/slapd/add.c
Suck in HEAD changes since 2.1alpha
[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
85         rc = dnPrettyNormal( NULL, &dn, &e->e_name, &e->e_nname );
86
87         if( rc != LDAP_SUCCESS ) {
88 #ifdef NEW_LOGGING
89                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
90                         "do_add: conn %d invalid dn (%s)\n", conn->c_connid,
91                                 dn.bv_val ));
92 #else
93                 Debug( LDAP_DEBUG_ANY, "do_add: invalid dn (%s)\n", dn.bv_val, 0, 0 );
94 #endif
95                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
96                             "invalid DN", NULL, NULL );
97                 goto done;
98         }
99
100 #ifdef NEW_LOGGING
101         LDAP_LOG(( "operation", LDAP_LEVEL_ARGS,
102                 "do_add: conn %d  dn (%s)\n", conn->c_connid, e->e_dn ));
103 #else
104         Debug( LDAP_DEBUG_ARGS, "do_add: dn (%s)\n", e->e_dn, 0, 0 );
105 #endif
106
107         /* get the attrs */
108         for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
109             tag = ber_next_element( ber, &len, last ) )
110         {
111                 Modifications *mod;
112                 ber_tag_t rtag;
113
114                 rtag = ber_scanf( ber, "{m{W}}", &tmp.sml_type, &tmp.sml_bvalues );
115
116                 if ( rtag == LBER_ERROR ) {
117 #ifdef NEW_LOGGING
118                         LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
119                                    "do_add: conn %d      decoding error \n", conn->c_connid ));
120 #else
121                         Debug( LDAP_DEBUG_ANY, "do_add: decoding error\n", 0, 0, 0 );
122 #endif
123                         send_ldap_disconnect( conn, op,
124                                 LDAP_PROTOCOL_ERROR, "decoding error" );
125                         rc = -1;
126                         goto done;
127                 }
128
129                 if ( tmp.sml_bvalues == NULL ) {
130 #ifdef NEW_LOGGING
131                         LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
132                                 "do_add: conn %d         no values for type %s\n",
133                                 conn->c_connid, tmp.sml_type.bv_val ));
134 #else
135                         Debug( LDAP_DEBUG_ANY, "no values for type %s\n",
136                                 tmp.sml_type.bv_val, 0, 0 );
137 #endif
138                         send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
139                                 NULL, "no values for attribute type", NULL, NULL );
140                         free( tmp.sml_type.bv_val );
141                         goto done;
142                 }
143                 mod  = (Modifications *) ch_malloc( sizeof(Modifications) );
144                 
145                 mod->sml_op = LDAP_MOD_ADD;
146                 mod->sml_next = NULL;
147                 mod->sml_desc = NULL;
148                 mod->sml_type = tmp.sml_type;
149                 mod->sml_bvalues = tmp.sml_bvalues;
150
151                 *modtail = mod;
152                 modtail = &mod->sml_next;
153         }
154
155         if ( ber_scanf( ber, /*{*/ "}") == LBER_ERROR ) {
156 #ifdef NEW_LOGGING
157                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
158                         "do_add: conn %d ber_scanf failed\n", conn->c_connid ));
159 #else
160                 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
161 #endif
162                 send_ldap_disconnect( conn, op,
163                         LDAP_PROTOCOL_ERROR, "decoding error" );
164                 rc = -1;
165                 goto done;
166         }
167
168         if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
169 #ifdef NEW_LOGGING
170                 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
171                         "do_add: conn %d get_ctrls failed\n", conn->c_connid ));
172 #else
173                 Debug( LDAP_DEBUG_ANY, "do_add: get_ctrls failed\n", 0, 0, 0 );
174 #endif
175                 goto done;
176         } 
177
178         if ( modlist == NULL ) {
179                 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
180                         NULL, "no attributes provided", NULL, NULL );
181                 goto done;
182         }
183
184         Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu ADD dn=\"%s\"\n",
185             op->o_connid, op->o_opid, e->e_dn, 0, 0 );
186
187         if( e->e_nname.bv_len == 0 ) {
188                 /* protocolError may be a more appropriate error */
189                 send_ldap_result( conn, op, rc = LDAP_ALREADY_EXISTS,
190                         NULL, "root DSE already exists",
191                         NULL, NULL );
192                 goto done;
193
194 #if defined( SLAPD_SCHEMA_DN )
195         } else if ( strcasecmp( e->e_ndn, SLAPD_SCHEMA_DN ) == 0 ) {
196                 send_ldap_result( conn, op, rc = LDAP_ALREADY_EXISTS,
197                         NULL, "subschema subentry already exists",
198                         NULL, NULL );
199                 goto done;
200 #endif
201         }
202
203         manageDSAit = get_manageDSAit( op );
204
205         /*
206          * We could be serving multiple database backends.  Select the
207          * appropriate one, or send a referral to our "referral server"
208          * if we don't hold it.
209          */
210         be = select_backend( &e->e_nname, manageDSAit, 0 );
211         if ( be == NULL ) {
212                 BerVarray ref = referral_rewrite( default_referral,
213                         NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
214
215                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
216                         NULL, NULL, ref ? ref : default_referral, NULL );
217
218                 if ( ref ) ber_bvarray_free( ref );
219                 goto done;
220         }
221
222         /* check restrictions */
223         rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
224         if( rc != LDAP_SUCCESS ) {
225                 send_ldap_result( conn, op, rc,
226                         NULL, text, NULL, NULL );
227                 goto done;
228         }
229
230         /* check for referrals */
231         rc = backend_check_referrals( be, conn, op, &e->e_name, &e->e_nname );
232         if ( rc != LDAP_SUCCESS ) {
233                 goto done;
234         }
235
236         /*
237          * do the add if 1 && (2 || 3)
238          * 1) there is an add function implemented in this backend;
239          * 2) this backend is master for what it holds;
240          * 3) it's a replica and the dn supplied is the updatedn.
241          */
242         if ( be->be_add ) {
243                 /* do the update here */
244                 int repl_user = be_isupdate(be, &op->o_ndn );
245 #ifndef SLAPD_MULTIMASTER
246                 if ( !be->be_update_ndn.bv_len || repl_user )
247 #endif
248                 {
249                         int update = be->be_update_ndn.bv_len;
250                         char textbuf[SLAP_TEXT_BUFLEN];
251                         size_t textlen = sizeof textbuf;
252
253                         rc = slap_mods_check( modlist, update, &text,
254                                 textbuf, textlen );
255
256                         if( rc != LDAP_SUCCESS ) {
257                                 send_ldap_result( conn, op, rc,
258                                         NULL, text, NULL, NULL );
259                                 goto done;
260                         }
261
262                         if ( SLAP_LASTMOD(be) && !repl_user ) {
263                                 for( modtail = &modlist;
264                                         *modtail != NULL;
265                                         modtail = &(*modtail)->sml_next )
266                                 {
267                                         assert( (*modtail)->sml_op == LDAP_MOD_ADD );
268                                         assert( (*modtail)->sml_desc != NULL );
269                                 }
270                                 rc = slap_mods_opattrs( op, modlist, modtail, &text,
271                                         textbuf, textlen );
272                                 if( rc != LDAP_SUCCESS ) {
273                                         send_ldap_result( conn, op, rc,
274                                                 NULL, text, NULL, NULL );
275                                         goto done;
276                                 }
277                         }
278
279                         rc = slap_mods2entry( modlist, &e, repl_user, &text,
280                                 textbuf, textlen );
281                         if( rc != LDAP_SUCCESS ) {
282                                 send_ldap_result( conn, op, rc,
283                                         NULL, text, NULL, NULL );
284                                 goto done;
285                         }
286
287                         if ( (*be->be_add)( be, conn, op, e ) == 0 ) {
288 #ifdef SLAPD_MULTIMASTER
289                                 if ( !repl_user )
290 #endif
291                                 {
292                                         replog( be, op, &e->e_name, &e->e_nname, e );
293                                 }
294                                 be_entry_release_w( be, conn, op, e );
295                                 e = NULL;
296                         }
297
298 #ifndef SLAPD_MULTIMASTER
299                 } else {
300                         BerVarray defref = be->be_update_refs
301                                 ? be->be_update_refs : default_referral;
302                         BerVarray ref = referral_rewrite( defref,
303                                 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
304
305                         send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
306                                 ref ? ref : defref, NULL );
307
308                         if ( ref ) ber_bvarray_free( ref );
309 #endif
310                 }
311         } else {
312 #ifdef NEW_LOGGING
313             LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
314                        "do_add: conn %d  no backend support\n", conn->c_connid ));
315 #else
316             Debug( LDAP_DEBUG_ARGS, "    do_add: no backend support\n", 0, 0, 0 );
317 #endif
318             send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
319                               NULL, "operation not supported within namingContext", NULL, NULL );
320         }
321
322 done:
323         if( modlist != NULL ) {
324                 slap_mods_free( modlist );
325         }
326         if( e != NULL ) {
327                 entry_free( e );
328         }
329
330         return rc;
331 }
332
333 static int slap_mods2entry(
334         Modifications *mods,
335         Entry **e,
336         int repl_user,
337         const char **text,
338         char *textbuf, size_t textlen )
339 {
340         Attribute **tail = &(*e)->e_attrs;
341         assert( *tail == NULL );
342
343         *text = textbuf;
344
345         for( ; mods != NULL; mods = mods->sml_next ) {
346                 Attribute *attr;
347
348                 assert( mods->sml_op == LDAP_MOD_ADD );
349                 assert( mods->sml_desc != NULL );
350
351                 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
352
353                 if( attr != NULL ) {
354 #define SLURPD_FRIENDLY
355 #ifdef SLURPD_FRIENDLY
356                         ber_len_t i,j;
357
358                         if( !repl_user ) {
359                                 snprintf( textbuf, textlen,
360                                         "attribute '%s' provided more than once",
361                                         mods->sml_desc->ad_cname.bv_val );
362                                 return LDAP_TYPE_OR_VALUE_EXISTS;
363                         }
364
365                         for( i=0; attr->a_vals[i].bv_val; i++ ) {
366                                 /* count them */
367                         }
368                         for( j=0; mods->sml_bvalues[j].bv_val; j++ ) {
369                                 /* count them */
370                         }
371                         j++;    /* NULL */
372                         
373                         attr->a_vals = ch_realloc( attr->a_vals,
374                                 sizeof( struct berval ) * (i+j) );
375
376                         /* should check for duplicates */
377
378                         AC_MEMCPY( &attr->a_vals[i], mods->sml_bvalues,
379                                 sizeof( struct berval ) * j );
380
381                         /* trim the mods array */
382                         ch_free( mods->sml_bvalues );
383                         mods->sml_bvalues = NULL;
384
385                         continue;
386 #else
387                         snprintf( textbuf, textlen,
388                                 "attribute '%s' provided more than once",
389                                 mods->sml_desc->ad_cname.bv_val );
390                         return LDAP_TYPE_OR_VALUE_EXISTS;
391 #endif
392                 }
393
394                 if( mods->sml_bvalues[1].bv_val != NULL ) {
395                         /* check for duplicates */
396                         int             i, j;
397                         MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
398
399                         /* check if the values we're adding already exist */
400                         if( mr == NULL || !mr->smr_match ) {
401                                 for ( i = 0; mods->sml_bvalues[i].bv_val != NULL; i++ ) {
402                                         /* test asserted values against themselves */
403                                         for( j = 0; j < i; j++ ) {
404                                                 int rc = ber_bvcmp( &mods->sml_bvalues[i],
405                                                         &mods->sml_bvalues[j] );
406
407                                                 if( rc == 0 ) {
408                                                         /* value exists already */
409                                                         snprintf( textbuf, textlen,
410                                                                 "%s: value #%d provided more than once",
411                                                                 mods->sml_desc->ad_cname.bv_val, j );
412                                                         return LDAP_TYPE_OR_VALUE_EXISTS;
413                                                 }
414                                         }
415                                 }
416
417                         } else {
418                                 for ( i = 0; mods->sml_bvalues[i].bv_val != NULL; i++ ) {
419                                         int rc, match;
420                                         const char *text = NULL;
421                                         struct berval asserted;
422
423                                         rc = value_normalize( mods->sml_desc,
424                                                 SLAP_MR_EQUALITY,
425                                                 &mods->sml_bvalues[i],
426                                                 &asserted,
427                                                 &text );
428
429                                         if( rc != LDAP_SUCCESS ) return rc;
430
431                                         for ( j = 0; j < i; j++ ) {
432                                                 int rc = value_match( &match, mods->sml_desc, mr,
433                                                         SLAP_MR_VALUE_SYNTAX_MATCH,
434                                                         &mods->sml_bvalues[j], &asserted, &text );
435
436                                                 if( rc == LDAP_SUCCESS && match == 0 ) {
437                                                         free( asserted.bv_val );
438                                                         snprintf( textbuf, textlen,
439                                                                 "%s: value #%d provided more than once",
440                                                                 mods->sml_desc->ad_cname.bv_val, j );
441                                                         return LDAP_TYPE_OR_VALUE_EXISTS;
442                                                 }
443                                         }
444
445                                         free( asserted.bv_val );
446                                 }
447                         }
448                 }
449
450                 attr = ch_calloc( 1, sizeof(Attribute) );
451
452                 /* move ad to attr structure */
453                 attr->a_desc = mods->sml_desc;
454                 mods->sml_desc = NULL;
455
456                 /* move values to attr structure */
457                 /*      should check for duplicates */
458                 attr->a_vals = mods->sml_bvalues;
459                 mods->sml_bvalues = NULL;
460
461                 *tail = attr;
462                 tail = &attr->a_next;
463         }
464
465         return LDAP_SUCCESS;
466 }