]> git.sur5r.net Git - openldap/blob - servers/slapd/add.c
Plug memory leak
[openldap] / servers / slapd / add.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 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 #include "slapi_common.h"
20
21 #include <stdio.h>
22 #include <ac/string.h>
23 #include <ac/time.h>
24 #include <ac/socket.h>
25
26 #include "ldap_pvt.h"
27 #include "slap.h"
28 #include "slapi.h"
29
30 int
31 do_add( Connection *conn, Operation *op )
32 {
33         BerElement      *ber = op->o_ber;
34         char            *last;
35         struct berval dn = { 0, NULL };
36         ber_len_t       len;
37         ber_tag_t       tag;
38         Entry           *e;
39         Backend         *be;
40         Modifications   *modlist = NULL;
41         Modifications   **modtail = &modlist;
42         Modifications   tmp;
43         const char *text;
44         int                     rc = LDAP_SUCCESS;
45         int     manageDSAit;
46
47         Slapi_PBlock *pb = op->o_pb;
48
49 #ifdef NEW_LOGGING
50         LDAP_LOG( OPERATION, ENTRY, "do_add: conn %d enter\n", conn->c_connid,0,0 );
51 #else
52         Debug( LDAP_DEBUG_TRACE, "do_add\n", 0, 0, 0 );
53 #endif
54         /*
55          * Parse the add request.  It looks like this:
56          *
57          *      AddRequest := [APPLICATION 14] SEQUENCE {
58          *              name    DistinguishedName,
59          *              attrs   SEQUENCE OF SEQUENCE {
60          *                      type    AttributeType,
61          *                      values  SET OF AttributeValue
62          *              }
63          *      }
64          */
65
66         /* get the name */
67         if ( ber_scanf( ber, "{m", /*}*/ &dn ) == LBER_ERROR ) {
68 #ifdef NEW_LOGGING
69                 LDAP_LOG( OPERATION, ERR, 
70                         "do_add: conn %d ber_scanf failed\n", conn->c_connid,0,0 );
71 #else
72                 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
73 #endif
74                 send_ldap_disconnect( conn, op,
75                         LDAP_PROTOCOL_ERROR, "decoding error" );
76                 return -1;
77         }
78
79         e = (Entry *) ch_calloc( 1, sizeof(Entry) );
80
81         rc = dnPrettyNormal( NULL, &dn, &e->e_name, &e->e_nname );
82
83         if( rc != LDAP_SUCCESS ) {
84 #ifdef NEW_LOGGING
85                 LDAP_LOG( OPERATION, ERR, 
86                         "do_add: conn %d invalid dn (%s)\n", conn->c_connid, dn.bv_val, 0 );
87 #else
88                 Debug( LDAP_DEBUG_ANY, "do_add: invalid dn (%s)\n", dn.bv_val, 0, 0 );
89 #endif
90                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
91                             "invalid DN", NULL, NULL );
92                 goto done;
93         }
94
95 #ifdef NEW_LOGGING
96         LDAP_LOG( OPERATION, ARGS, 
97                 "do_add: conn %d  dn (%s)\n", conn->c_connid, e->e_dn, 0 );
98 #else
99         Debug( LDAP_DEBUG_ARGS, "do_add: dn (%s)\n", e->e_dn, 0, 0 );
100 #endif
101
102         /* get the attrs */
103         for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
104             tag = ber_next_element( ber, &len, last ) )
105         {
106                 Modifications *mod;
107                 ber_tag_t rtag;
108
109                 rtag = ber_scanf( ber, "{m{W}}", &tmp.sml_type, &tmp.sml_bvalues );
110
111                 if ( rtag == LBER_ERROR ) {
112 #ifdef NEW_LOGGING
113                         LDAP_LOG( OPERATION, ERR, 
114                                    "do_add: conn %d      decoding error \n", conn->c_connid, 0, 0 );
115 #else
116                         Debug( LDAP_DEBUG_ANY, "do_add: decoding error\n", 0, 0, 0 );
117 #endif
118                         send_ldap_disconnect( conn, op,
119                                 LDAP_PROTOCOL_ERROR, "decoding error" );
120                         rc = -1;
121                         goto done;
122                 }
123
124                 if ( tmp.sml_bvalues == NULL ) {
125 #ifdef NEW_LOGGING
126                         LDAP_LOG( OPERATION, INFO, 
127                                 "do_add: conn %d         no values for type %s\n",
128                                 conn->c_connid, tmp.sml_type.bv_val, 0 );
129 #else
130                         Debug( LDAP_DEBUG_ANY, "no values for type %s\n",
131                                 tmp.sml_type.bv_val, 0, 0 );
132 #endif
133                         send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
134                                 NULL, "no values for attribute type", NULL, NULL );
135                         goto done;
136                 }
137                 mod  = (Modifications *) ch_malloc( sizeof(Modifications) );
138                 
139                 mod->sml_op = LDAP_MOD_ADD;
140                 mod->sml_next = NULL;
141                 mod->sml_desc = NULL;
142                 mod->sml_type = tmp.sml_type;
143                 mod->sml_bvalues = tmp.sml_bvalues;
144
145                 *modtail = mod;
146                 modtail = &mod->sml_next;
147         }
148
149         if ( ber_scanf( ber, /*{*/ "}") == LBER_ERROR ) {
150 #ifdef NEW_LOGGING
151                 LDAP_LOG( OPERATION, ERR, 
152                         "do_add: conn %d ber_scanf failed\n", conn->c_connid, 0, 0 );
153 #else
154                 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
155 #endif
156                 send_ldap_disconnect( conn, op,
157                         LDAP_PROTOCOL_ERROR, "decoding error" );
158                 rc = -1;
159                 goto done;
160         }
161
162         if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
163 #ifdef NEW_LOGGING
164                 LDAP_LOG( OPERATION, INFO, 
165                         "do_add: conn %d get_ctrls failed\n", conn->c_connid, 0, 0 );
166 #else
167                 Debug( LDAP_DEBUG_ANY, "do_add: get_ctrls failed\n", 0, 0, 0 );
168 #endif
169                 goto done;
170         } 
171
172         if ( modlist == NULL ) {
173                 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
174                         NULL, "no attributes provided", NULL, NULL );
175                 goto done;
176         }
177
178         Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu ADD dn=\"%s\"\n",
179             op->o_connid, op->o_opid, e->e_dn, 0, 0 );
180
181         if( e->e_nname.bv_len == 0 ) {
182                 /* protocolError may be a more appropriate error */
183                 send_ldap_result( conn, op, rc = LDAP_ALREADY_EXISTS,
184                         NULL, "root DSE already exists",
185                         NULL, NULL );
186                 goto done;
187
188         } else if ( bvmatch( &e->e_nname, &global_schemandn ) ) {
189                 send_ldap_result( conn, op, rc = LDAP_ALREADY_EXISTS,
190                         NULL, "subschema subentry already exists",
191                         NULL, NULL );
192                 goto done;
193         }
194
195         manageDSAit = get_manageDSAit( op );
196
197         /*
198          * We could be serving multiple database backends.  Select the
199          * appropriate one, or send a referral to our "referral server"
200          * if we don't hold it.
201          */
202         be = select_backend( &e->e_nname, manageDSAit, 0 );
203         if ( be == NULL ) {
204                 BerVarray ref = referral_rewrite( default_referral,
205                         NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
206
207                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
208                         NULL, NULL, ref ? ref : default_referral, NULL );
209
210                 if ( ref ) ber_bvarray_free( ref );
211                 goto done;
212         }
213
214         /* check restrictions */
215         rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
216         if( rc != LDAP_SUCCESS ) {
217                 send_ldap_result( conn, op, rc,
218                         NULL, text, NULL, NULL );
219                 goto done;
220         }
221
222         /* check for referrals */
223         rc = backend_check_referrals( be, conn, op, &e->e_name, &e->e_nname );
224         if ( rc != LDAP_SUCCESS ) {
225                 goto done;
226         }
227
228 #if defined( LDAP_SLAPI )
229         slapi_pblock_set( pb, SLAPI_BACKEND, (void *)be );
230         slapi_pblock_set( pb, SLAPI_CONNECTION, (void *)conn );
231         slapi_pblock_set( pb, SLAPI_OPERATION, (void *)op );
232         slapi_pblock_set( pb, SLAPI_ADD_ENTRY, (void *)e );
233         slapi_pblock_set( pb, SLAPI_ADD_TARGET, (void *)dn.bv_val );
234         slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)(1) );
235         slapi_pblock_set( pb, SLAPI_REQCONTROLS, (void *)op->o_ctrls );
236
237         rc = doPluginFNs( be, SLAPI_PLUGIN_PRE_ADD_FN, pb );
238         if ( rc != 0 && rc != LDAP_OTHER ) {
239                 /*
240                  * either there is no preOp (add) plugins
241                  * or a plugin failed. Just log it
242                  * 
243                  * FIXME: is this correct?
244                  */
245 #ifdef NEW_LOGGING
246                 LDAP_LOG(( "operation", LDAP_LEVEL_INFO, "do_add: add preOps failed\n"));
247 #else
248                 Debug (LDAP_DEBUG_TRACE, " add preOps failed.\n", 0, 0, 0);
249 #endif
250         }
251 #endif /* defined( LDAP_SLAPI ) */
252
253         /*
254          * do the add if 1 && (2 || 3)
255          * 1) there is an add function implemented in this backend;
256          * 2) this backend is master for what it holds;
257          * 3) it's a replica and the dn supplied is the updatedn.
258          */
259         if ( be->be_add ) {
260                 /* do the update here */
261                 int repl_user = be_isupdate(be, &op->o_ndn );
262 #ifndef SLAPD_MULTIMASTER
263                 if ( !be->be_update_ndn.bv_len || repl_user )
264 #endif
265                 {
266                         int update = be->be_update_ndn.bv_len;
267                         char textbuf[SLAP_TEXT_BUFLEN];
268                         size_t textlen = sizeof textbuf;
269
270                         rc = slap_mods_check( modlist, update, &text,
271                                 textbuf, textlen );
272
273                         if( rc != LDAP_SUCCESS ) {
274                                 send_ldap_result( conn, op, rc,
275                                         NULL, text, NULL, NULL );
276                                 goto done;
277                         }
278
279                         if ( !repl_user ) {
280                                 for( modtail = &modlist;
281                                         *modtail != NULL;
282                                         modtail = &(*modtail)->sml_next )
283                                 {
284                                         assert( (*modtail)->sml_op == LDAP_MOD_ADD );
285                                         assert( (*modtail)->sml_desc != NULL );
286                                 }
287                                 rc = slap_mods_opattrs( be, op, modlist, modtail, &text,
288                                         textbuf, textlen );
289                                 if( rc != LDAP_SUCCESS ) {
290                                         send_ldap_result( conn, op, rc,
291                                                 NULL, text, NULL, NULL );
292                                         goto done;
293                                 }
294                         }
295
296                         rc = slap_mods2entry( modlist, &e, repl_user, &text,
297                                 textbuf, textlen );
298                         if( rc != LDAP_SUCCESS ) {
299                                 send_ldap_result( conn, op, rc,
300                                         NULL, text, NULL, NULL );
301                                 goto done;
302                         }
303
304                         if ( (*be->be_add)( be, conn, op, e ) == 0 ) {
305 #ifdef SLAPD_MULTIMASTER
306                                 if ( !repl_user )
307 #endif
308                                 {
309                                         replog( be, op, &e->e_name, &e->e_nname, e );
310                                 }
311                                 be_entry_release_w( be, conn, op, e );
312                                 e = NULL;
313                         }
314
315 #ifndef SLAPD_MULTIMASTER
316                 } else {
317                         BerVarray defref = be->be_update_refs
318                                 ? be->be_update_refs : default_referral;
319                         BerVarray ref = referral_rewrite( defref,
320                                 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
321
322                         send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
323                                 ref ? ref : defref, NULL );
324
325                         if ( ref ) ber_bvarray_free( ref );
326 #endif
327                 }
328         } else {
329 #ifdef NEW_LOGGING
330             LDAP_LOG( OPERATION, INFO, 
331                        "do_add: conn %d  no backend support\n", conn->c_connid, 0, 0 );
332 #else
333             Debug( LDAP_DEBUG_ARGS, "    do_add: no backend support\n", 0, 0, 0 );
334 #endif
335             send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
336                               NULL, "operation not supported within namingContext", NULL, NULL );
337         }
338
339 #if defined( LDAP_SLAPI )
340         rc = doPluginFNs( be, SLAPI_PLUGIN_POST_ADD_FN, pb );
341         if ( rc != 0 && rc != LDAP_OTHER ) {
342                 /*
343                  * either there is no postOp (Add) plugins
344                  * or a plugin failed. Just log it
345                  *
346                  * FIXME: is this correct?
347                  */
348 #ifdef NEW_LOGGING
349                 LDAP_LOG(( "operation", LDAP_LEVEL_INFO, "do_add: Add postOps failed\n"));
350 #else
351                 Debug (LDAP_DEBUG_TRACE, " Add postOps failed.\n", 0, 0, 0);
352 #endif
353         }
354 #endif /* defined( LDAP_SLAPI ) */
355
356 done:
357         if( modlist != NULL ) {
358                 slap_mods_free( modlist );
359         }
360         if( e != NULL ) {
361                 entry_free( e );
362         }
363
364         return rc;
365 }
366
367 int
368 slap_mods2entry(
369         Modifications *mods,
370         Entry **e,
371         int repl_user,
372         const char **text,
373         char *textbuf, size_t textlen )
374 {
375         Attribute **tail = &(*e)->e_attrs;
376         assert( *tail == NULL );
377
378         *text = textbuf;
379
380         for( ; mods != NULL; mods = mods->sml_next ) {
381                 Attribute *attr;
382
383                 assert( mods->sml_op == LDAP_MOD_ADD );
384                 assert( mods->sml_desc != NULL );
385
386                 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
387
388                 if( attr != NULL ) {
389 #define SLURPD_FRIENDLY
390 #ifdef SLURPD_FRIENDLY
391                         ber_len_t i,j;
392
393                         if( !repl_user ) {
394                                 snprintf( textbuf, textlen,
395                                         "attribute '%s' provided more than once",
396                                         mods->sml_desc->ad_cname.bv_val );
397                                 return LDAP_TYPE_OR_VALUE_EXISTS;
398                         }
399
400                         for( i=0; attr->a_vals[i].bv_val; i++ ) {
401                                 /* count them */
402                         }
403                         for( j=0; mods->sml_bvalues[j].bv_val; j++ ) {
404                                 /* count them */
405                         }
406                         j++;    /* NULL */
407                         
408                         attr->a_vals = ch_realloc( attr->a_vals,
409                                 sizeof( struct berval ) * (i+j) );
410
411                         /* should check for duplicates */
412
413                         AC_MEMCPY( &attr->a_vals[i], mods->sml_bvalues,
414                                 sizeof( struct berval ) * j );
415
416                         /* trim the mods array */
417                         ch_free( mods->sml_bvalues );
418                         mods->sml_bvalues = NULL;
419
420                         continue;
421 #else
422                         snprintf( textbuf, textlen,
423                                 "attribute '%s' provided more than once",
424                                 mods->sml_desc->ad_cname.bv_val );
425                         return LDAP_TYPE_OR_VALUE_EXISTS;
426 #endif
427                 }
428
429                 if( mods->sml_bvalues[1].bv_val != NULL ) {
430                         /* check for duplicates */
431                         int             i, j;
432                         MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
433
434                         /* check if the values we're adding already exist */
435                         if( mr == NULL || !mr->smr_match ) {
436                                 for ( i = 0; mods->sml_bvalues[i].bv_val != NULL; i++ ) {
437                                         /* test asserted values against themselves */
438                                         for( j = 0; j < i; j++ ) {
439                                                 if ( bvmatch( &mods->sml_bvalues[i],
440                                                         &mods->sml_bvalues[j] ) ) {
441                                                         /* value exists already */
442                                                         snprintf( textbuf, textlen,
443                                                                 "%s: value #%d provided more than once",
444                                                                 mods->sml_desc->ad_cname.bv_val, j );
445                                                         return LDAP_TYPE_OR_VALUE_EXISTS;
446                                                 }
447                                         }
448                                 }
449
450                         } else {
451                                 int             rc;
452                                 const char      *text = NULL;
453                                 char            textbuf[ SLAP_TEXT_BUFLEN ]  = { '\0' };
454                                 
455                                 rc = modify_check_duplicates( mods->sml_desc, mr,
456                                                 NULL, mods->sml_bvalues,
457                                                 &text, textbuf, sizeof( textbuf ) );
458
459                                 if ( rc != LDAP_SUCCESS ) {
460                                         return rc;
461                                 }
462                         }
463                 }
464
465                 attr = ch_calloc( 1, sizeof(Attribute) );
466
467                 /* move ad to attr structure */
468                 attr->a_desc = mods->sml_desc;
469                 mods->sml_desc = NULL;
470
471                 /* move values to attr structure */
472                 /*      should check for duplicates */
473                 attr->a_vals = mods->sml_bvalues;
474                 mods->sml_bvalues = NULL;
475
476                 *tail = attr;
477                 tail = &attr->a_next;
478         }
479
480         return LDAP_SUCCESS;
481 }