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