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