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