]> git.sur5r.net Git - openldap/blob - servers/slapd/add.c
Fix dnValidate
[openldap] / servers / slapd / add.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 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         const char **text );
32
33 int
34 do_add( Connection *conn, Operation *op )
35 {
36         BerElement      *ber = op->o_ber;
37         char            *last;
38         struct berval dn = { 0, NULL };
39         ber_len_t       len;
40         ber_tag_t       tag;
41         Entry           *e;
42         Backend         *be;
43         LDAPModList     *modlist = NULL;
44         LDAPModList     **modtail = &modlist;
45         Modifications *mods = NULL;
46         const char *text;
47         int                     rc = LDAP_SUCCESS;
48         int     manageDSAit;
49
50 #ifdef NEW_LOGGING
51         LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY,
52                    "do_add: conn %d enter\n", conn->c_connid ));
53 #else
54         Debug( LDAP_DEBUG_TRACE, "do_add\n", 0, 0, 0 );
55 #endif
56         /*
57          * Parse the add request.  It looks like this:
58          *
59          *      AddRequest := [APPLICATION 14] SEQUENCE {
60          *              name    DistinguishedName,
61          *              attrs   SEQUENCE OF SEQUENCE {
62          *                      type    AttributeType,
63          *                      values  SET OF AttributeValue
64          *              }
65          *      }
66          */
67
68         /* get the name */
69         if ( ber_scanf( ber, "{o", /*}*/ &dn ) == LBER_ERROR ) {
70 #ifdef NEW_LOGGING
71                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
72                         "do_add: conn %d ber_scanf failed\n", conn->c_connid ));
73 #else
74                 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
75 #endif
76                 send_ldap_disconnect( conn, op,
77                         LDAP_PROTOCOL_ERROR, "decoding error" );
78                 return -1;
79         }
80
81         e = (Entry *) ch_calloc( 1, sizeof(Entry) );
82         e->e_name.bv_val = NULL;
83         e->e_name.bv_len = 0;
84         e->e_nname.bv_val = NULL;
85         e->e_nname.bv_len = 0;
86         e->e_attrs = NULL;
87         e->e_private = NULL;
88
89         rc = dnPrettyNormal( NULL, &dn, &e->e_name, &e->e_nname );
90
91         if( rc != LDAP_SUCCESS ) {
92 #ifdef NEW_LOGGING
93                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
94                         "do_add: conn %d invalid dn (%s)\n", conn->c_connid,
95                                 dn.bv_val ));
96 #else
97                 Debug( LDAP_DEBUG_ANY, "do_add: invalid dn (%s)\n", dn.bv_val, 0, 0 );
98 #endif
99                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
100                             "invalid DN", NULL, NULL );
101                 goto done;
102         }
103
104 #ifdef NEW_LOGGING
105         LDAP_LOG(( "operation", LDAP_LEVEL_ARGS,
106                 "do_add: conn %d  dn (%s)\n", conn->c_connid, e->e_dn ));
107 #else
108         Debug( LDAP_DEBUG_ARGS, "do_add: dn (%s)\n", e->e_dn, 0, 0 );
109 #endif
110
111         /* get the attrs */
112         for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
113             tag = ber_next_element( ber, &len, last ) )
114         {
115                 LDAPModList *mod = (LDAPModList *) ch_malloc( sizeof(LDAPModList) );
116                 mod->ml_op = LDAP_MOD_ADD;
117                 mod->ml_next = NULL;
118
119                 rc = ber_scanf( ber, "{a{V}}", &mod->ml_type, &mod->ml_bvalues );
120
121                 if ( rc == LBER_ERROR ) {
122 #ifdef NEW_LOGGING
123                         LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
124                                    "do_add: conn %d      decoding error \n", conn->c_connid ));
125 #else
126                         Debug( LDAP_DEBUG_ANY, "do_add: decoding error\n", 0, 0, 0 );
127 #endif
128                         send_ldap_disconnect( conn, op,
129                                 LDAP_PROTOCOL_ERROR, "decoding error" );
130                         rc = -1;
131                         free( mod );
132                         goto done;
133                 }
134
135                 if ( mod->ml_bvalues == NULL ) {
136 #ifdef NEW_LOGGING
137                         LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
138                                 "do_add: conn %d         no values for type %s\n",
139                                 conn->c_connid, mod->ml_type ));
140 #else
141                         Debug( LDAP_DEBUG_ANY, "no values for type %s\n",
142                                 mod->ml_type, 0, 0 );
143 #endif
144                         send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
145                                 NULL, "no values for attribute type", NULL, NULL );
146                         free( mod->ml_type );
147                         free( mod );
148                         goto done;
149                 }
150
151                 *modtail = mod;
152                 modtail = &mod->ml_next;
153         }
154
155         if ( ber_scanf( ber, /*{*/ "}") == LBER_ERROR ) {
156 #ifdef NEW_LOGGING
157                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
158                         "do_add: conn %d ber_scanf failed\n", conn->c_connid ));
159 #else
160                 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
161 #endif
162                 send_ldap_disconnect( conn, op,
163                         LDAP_PROTOCOL_ERROR, "decoding error" );
164                 rc = -1;
165                 goto done;
166         }
167
168         if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
169 #ifdef NEW_LOGGING
170                 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
171                         "do_add: conn %d get_ctrls failed\n", conn->c_connid ));
172 #else
173                 Debug( LDAP_DEBUG_ANY, "do_add: get_ctrls failed\n", 0, 0, 0 );
174 #endif
175                 goto done;
176         } 
177
178         if ( modlist == NULL ) {
179                 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
180                         NULL, "no attributes provided", NULL, NULL );
181                 goto done;
182         }
183
184         Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d ADD dn=\"%s\"\n",
185             op->o_connid, op->o_opid, e->e_dn, 0, 0 );
186
187         if( e->e_ndn == NULL || *e->e_ndn == '\0' ) {
188                 /* protocolError may be a more appropriate error */
189                 send_ldap_result( conn, op, rc = LDAP_ALREADY_EXISTS,
190                         NULL, "root DSE already exists",
191                         NULL, NULL );
192                 goto done;
193
194 #if defined( SLAPD_SCHEMA_DN )
195         } else if ( strcasecmp( e->e_ndn, SLAPD_SCHEMA_DN ) == 0 ) {
196                 send_ldap_result( conn, op, rc = LDAP_ALREADY_EXISTS,
197                         NULL, "subschema subentry already exists",
198                         NULL, NULL );
199                 goto done;
200 #endif
201         }
202
203         manageDSAit = get_manageDSAit( op );
204
205         /*
206          * We could be serving multiple database backends.  Select the
207          * appropriate one, or send a referral to our "referral server"
208          * if we don't hold it.
209          */
210         be = select_backend( &e->e_nname, manageDSAit, 0 );
211         if ( be == NULL ) {
212                 struct berval **ref = referral_rewrite( default_referral,
213                         NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
214
215                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
216                         NULL, NULL, ref ? ref : default_referral, NULL );
217
218                 ber_bvecfree( ref );
219                 goto done;
220         }
221
222         /* check restrictions */
223         rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
224         if( rc != LDAP_SUCCESS ) {
225                 send_ldap_result( conn, op, rc,
226                         NULL, text, NULL, NULL );
227                 goto done;
228         }
229
230         /* check for referrals */
231         rc = backend_check_referrals( be, conn, op, &e->e_name, &e->e_nname );
232         if ( rc != LDAP_SUCCESS ) {
233                 goto done;
234         }
235
236         /*
237          * do the add if 1 && (2 || 3)
238          * 1) there is an add function implemented in this backend;
239          * 2) this backend is master for what it holds;
240          * 3) it's a replica and the dn supplied is the updatedn.
241          */
242         if ( be->be_add ) {
243                 /* do the update here */
244                 int repl_user = be_isupdate(be, &op->o_ndn );
245 #ifndef SLAPD_MULTIMASTER
246                 if ( !be->be_update_ndn.bv_len || repl_user )
247 #endif
248                 {
249                         int update = be->be_update_ndn.bv_len;
250                         char textbuf[SLAP_TEXT_BUFLEN];
251                         size_t textlen = sizeof textbuf;
252
253                         rc = slap_modlist2mods( modlist, update, &mods, &text,
254                                 textbuf, textlen );
255
256                         if( rc != LDAP_SUCCESS ) {
257                                 send_ldap_result( conn, op, rc,
258                                         NULL, text, NULL, NULL );
259                                 goto done;
260                         }
261
262                         if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
263                                 global_lastmod == ON)) && !repl_user )
264                         {
265                                 Modifications **modstail;
266                                 for( modstail = &mods;
267                                         *modstail != NULL;
268                                         modstail = &(*modstail)->sml_next )
269                                 {
270                                         assert( (*modstail)->sml_op == LDAP_MOD_ADD );
271                                         assert( (*modstail)->sml_desc != NULL );
272                                 }
273                                 rc = slap_mods_opattrs( op, mods, modstail, &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( mods, &e, &text );
283                         if( rc != LDAP_SUCCESS ) {
284                                 send_ldap_result( conn, op, rc,
285                                         NULL, text, NULL, NULL );
286                                 goto done;
287                         }
288
289                         if ( (*be->be_add)( be, conn, op, e ) == 0 ) {
290 #ifdef SLAPD_MULTIMASTER
291                                 if ( !repl_user )
292 #endif
293                                 {
294                                         replog( be, op, &e->e_name, &e->e_nname, e );
295                                 }
296                                 be_entry_release_w( be, conn, op, e );
297                                 e = NULL;
298                         }
299
300 #ifndef SLAPD_MULTIMASTER
301                 } else {
302                         struct berval **defref = be->be_update_refs
303                                 ? be->be_update_refs : default_referral;
304                         struct berval **ref = referral_rewrite( defref,
305                                 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
306
307                         send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
308                                 ref ? ref : defref, NULL );
309
310                         ber_bvecfree( ref );
311 #endif
312                 }
313         } else {
314 #ifdef NEW_LOGGING
315             LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
316                        "do_add: conn %d  no backend support\n", conn->c_connid ));
317 #else
318             Debug( LDAP_DEBUG_ARGS, "    do_add: no backend support\n", 0, 0, 0 );
319 #endif
320             send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
321                               NULL, "operation not supported within namingContext", NULL, NULL );
322         }
323
324 done:
325         free( dn.bv_val );
326
327         if( modlist != NULL ) {
328                 slap_modlist_free( modlist );
329         }
330         if( mods != NULL ) {
331                 slap_mods_free( mods );
332         }
333         if( e != NULL ) {
334                 entry_free( e );
335         }
336
337         return rc;
338 }
339
340 static int slap_mods2entry(
341         Modifications *mods,
342         Entry **e,
343         const char **text )
344 {
345         Attribute **tail = &(*e)->e_attrs;
346         assert( *tail == NULL );
347
348         for( ; mods != NULL; mods = mods->sml_next ) {
349                 Attribute *attr;
350
351                 assert( mods->sml_op == LDAP_MOD_ADD );
352                 assert( mods->sml_desc != NULL );
353
354                 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
355
356                 if( attr != NULL ) {
357 #define SLURPD_FRIENDLY
358 #ifdef SLURPD_FRIENDLY
359                         ber_len_t i,j;
360
361                         for( i=0; attr->a_vals[i]; i++ ) {
362                                 /* count them */
363                         }
364                         for( j=0; mods->sml_bvalues[j]; j++ ) {
365                                 /* count them */
366                         }
367                         j++;    /* NULL */
368                         
369                         attr->a_vals = ch_realloc( attr->a_vals,
370                                 sizeof( struct berval * ) * (i+j) );
371
372                         /* should check for duplicates */
373                         AC_MEMCPY( &attr->a_vals[i], mods->sml_bvalues,
374                                 sizeof( struct berval * ) * j );
375
376                         /* trim the mods array */
377                         ch_free( mods->sml_bvalues );
378                         mods->sml_bvalues = NULL;
379
380                         continue;
381 #else
382                         *text = "attribute provided more than once";
383                         return LDAP_TYPE_OR_VALUE_EXISTS;
384 #endif
385                 }
386
387                 attr = ch_calloc( 1, sizeof(Attribute) );
388
389                 /* move ad to attr structure */
390                 attr->a_desc = mods->sml_desc;
391                 mods->sml_desc = NULL;
392
393                 /* move values to attr structure */
394                 /*      should check for duplicates */
395                 attr->a_vals = mods->sml_bvalues;
396                 mods->sml_bvalues = NULL;
397
398                 *tail = attr;
399                 tail = &attr->a_next;
400         }
401
402         return LDAP_SUCCESS;
403 }