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