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