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