]> git.sur5r.net Git - openldap/blob - servers/slapd/add.c
Conform SLAPI to Netscape, iPlanet and Sun ONE Directory Server
[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_backend_set_pb( pb, be );
230         slapi_connection_set_pb( pb, conn );
231         slapi_operation_set_pb( pb, 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
236         rc = doPluginFNs( be, SLAPI_PLUGIN_PRE_ADD_FN, pb );
237         if ( rc != 0 ) {
238                 /*
239                  * A preoperation plugin failure will abort the
240                  * entire operation.
241                  */
242 #ifdef NEW_LOGGING
243                 LDAP_LOG( OPERATION, INFO, "do_add: add preoperation plugin failed\n",
244                                 0, 0, 0);
245 #else
246                 Debug(LDAP_DEBUG_TRACE, "do_add: add preoperation plugin failed.\n",
247                                 0, 0, 0);
248                 if ( slapi_pblock_get( pb, SLAPI_RESULT_CODE, (void *)&rc ) != 0 )
249                         rc = LDAP_OPERATIONS_ERROR;
250                 goto done;
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         /*
343          * Postoperation errors are silently ignored; the work has
344          * been done.
345          */
346         if ( doPluginFNs( be, SLAPI_PLUGIN_POST_ADD_FN, pb ) != 0) {
347 #ifdef NEW_LOGGING
348                 LDAP_LOG( OPERATION, INFO, "do_add: Add postoperation plugins failed\n",
349                                 0, 0, 0);
350 #else
351                 Debug(LDAP_DEBUG_TRACE, "do_add: Add postoperation plugins failed.\n",
352                                 0, 0, 0);
353 #endif
354         }
355 #endif /* defined( LDAP_SLAPI ) */
356
357 done:
358         if( modlist != NULL ) {
359                 slap_mods_free( modlist );
360         }
361         if( e != NULL ) {
362                 entry_free( e );
363         }
364
365         return rc;
366 }
367
368 int
369 slap_mods2entry(
370         Modifications *mods,
371         Entry **e,
372         int repl_user,
373         const char **text,
374         char *textbuf, size_t textlen )
375 {
376         Attribute **tail = &(*e)->e_attrs;
377         assert( *tail == NULL );
378
379         *text = textbuf;
380
381         for( ; mods != NULL; mods = mods->sml_next ) {
382                 Attribute *attr;
383
384                 assert( mods->sml_op == LDAP_MOD_ADD );
385                 assert( mods->sml_desc != NULL );
386
387                 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
388
389                 if( attr != NULL ) {
390 #define SLURPD_FRIENDLY
391 #ifdef SLURPD_FRIENDLY
392                         ber_len_t i,j;
393
394                         if( !repl_user ) {
395                                 snprintf( textbuf, textlen,
396                                         "attribute '%s' provided more than once",
397                                         mods->sml_desc->ad_cname.bv_val );
398                                 return LDAP_TYPE_OR_VALUE_EXISTS;
399                         }
400
401                         for( i=0; attr->a_vals[i].bv_val; i++ ) {
402                                 /* count them */
403                         }
404                         for( j=0; mods->sml_bvalues[j].bv_val; j++ ) {
405                                 /* count them */
406                         }
407                         j++;    /* NULL */
408                         
409                         attr->a_vals = ch_realloc( attr->a_vals,
410                                 sizeof( struct berval ) * (i+j) );
411
412                         /* should check for duplicates */
413
414                         AC_MEMCPY( &attr->a_vals[i], mods->sml_bvalues,
415                                 sizeof( struct berval ) * j );
416
417                         /* trim the mods array */
418                         ch_free( mods->sml_bvalues );
419                         mods->sml_bvalues = NULL;
420
421                         continue;
422 #else
423                         snprintf( textbuf, textlen,
424                                 "attribute '%s' provided more than once",
425                                 mods->sml_desc->ad_cname.bv_val );
426                         return LDAP_TYPE_OR_VALUE_EXISTS;
427 #endif
428                 }
429
430                 if( mods->sml_bvalues[1].bv_val != NULL ) {
431                         /* check for duplicates */
432                         int             i, j;
433                         MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
434
435                         /* check if the values we're adding already exist */
436                         if( mr == NULL || !mr->smr_match ) {
437                                 for ( i = 0; mods->sml_bvalues[i].bv_val != NULL; i++ ) {
438                                         /* test asserted values against themselves */
439                                         for( j = 0; j < i; j++ ) {
440                                                 if ( bvmatch( &mods->sml_bvalues[i],
441                                                         &mods->sml_bvalues[j] ) ) {
442                                                         /* value exists already */
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
451                         } else {
452                                 int             rc;
453                                 const char      *text = NULL;
454                                 char            textbuf[ SLAP_TEXT_BUFLEN ]  = { '\0' };
455                                 
456                                 rc = modify_check_duplicates( mods->sml_desc, mr,
457                                                 NULL, mods->sml_bvalues,
458                                                 &text, textbuf, sizeof( textbuf ) );
459
460                                 if ( rc != LDAP_SUCCESS ) {
461                                         return rc;
462                                 }
463                         }
464                 }
465
466                 attr = ch_calloc( 1, sizeof(Attribute) );
467
468                 /* move ad to attr structure */
469                 attr->a_desc = mods->sml_desc;
470                 mods->sml_desc = NULL;
471
472                 /* move values to attr structure */
473                 /*      should check for duplicates */
474                 attr->a_vals = mods->sml_bvalues;
475                 mods->sml_bvalues = NULL;
476
477                 *tail = attr;
478                 tail = &attr->a_next;
479         }
480
481         return LDAP_SUCCESS;
482 }