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