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