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