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