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