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