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