]> git.sur5r.net Git - openldap/blob - servers/slapd/add.c
3db0bf9d025af996c7e6a8bf04ab70cf98748a2b
[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, *ndn, *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         struct berval **urls = NULL;
48
49         Debug( LDAP_DEBUG_TRACE, "do_add\n", 0, 0, 0 );
50
51         /*
52          * Parse the add request.  It looks like this:
53          *
54          *      AddRequest := [APPLICATION 14] SEQUENCE {
55          *              name    DistinguishedName,
56          *              attrs   SEQUENCE OF SEQUENCE {
57          *                      type    AttributeType,
58          *                      values  SET OF AttributeValue
59          *              }
60          *      }
61          */
62
63         /* get the name */
64         if ( ber_scanf( ber, "{a", /*}*/ &dn ) == LBER_ERROR ) {
65                 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
66                 send_ldap_disconnect( conn, op,
67                         LDAP_PROTOCOL_ERROR, "decoding error" );
68                 return -1;
69         }
70
71         ndn = ch_strdup( dn );
72
73         if ( dn_normalize( ndn ) == NULL ) {
74                 Debug( LDAP_DEBUG_ANY, "do_add: invalid dn (%s)\n", dn, 0, 0 );
75                 send_ldap_result( conn, op, LDAP_INVALID_DN_SYNTAX, NULL,
76                     "invalid DN", NULL, NULL );
77                 free( dn );
78                 free( ndn );
79                 return LDAP_INVALID_DN_SYNTAX;
80         }
81
82         e = (Entry *) ch_calloc( 1, sizeof(Entry) );
83
84         e->e_dn = dn;
85         e->e_ndn = ndn;
86         e->e_attrs = NULL;
87         e->e_private = NULL;
88
89         Debug( LDAP_DEBUG_ARGS, "do_add: ndn (%s)\n", e->e_ndn, 0, 0 );
90
91         /* get the attrs */
92         for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
93             tag = ber_next_element( ber, &len, last ) )
94         {
95                 LDAPModList *mod = (LDAPModList *) ch_malloc( sizeof(LDAPModList) );
96                 mod->ml_op = LDAP_MOD_ADD;
97                 mod->ml_next = NULL;
98
99                 rc = ber_scanf( ber, "{a{V}}", &mod->ml_type, &mod->ml_bvalues );
100
101                 if ( rc == LBER_ERROR ) {
102                         Debug( LDAP_DEBUG_ANY, "do_add: decoding error\n", 0, 0, 0 );
103                         send_ldap_disconnect( conn, op,
104                                 LDAP_PROTOCOL_ERROR, "decoding error" );
105                         rc = -1;
106                         free( mod );
107                         goto done;
108                 }
109
110                 if ( mod->ml_bvalues == NULL ) {
111                         Debug( LDAP_DEBUG_ANY, "no values for type %s\n",
112                                 mod->ml_type, 0, 0 );
113                         send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
114                                 NULL, "no values for attribute type", NULL, NULL );
115                         free( mod->ml_type );
116                         free( mod );
117                         goto done;
118                 }
119
120                 *modtail = mod;
121                 modtail = &mod->ml_next;
122         }
123
124         if ( ber_scanf( ber, /*{*/ "}") == LBER_ERROR ) {
125                 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
126                 send_ldap_disconnect( conn, op,
127                         LDAP_PROTOCOL_ERROR, "decoding error" );
128                 rc = -1;
129                 goto done;
130         }
131
132         if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
133                 Debug( LDAP_DEBUG_ANY, "do_add: get_ctrls failed\n", 0, 0, 0 );
134                 goto done;
135         } 
136
137         if ( modlist == NULL ) {
138                 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
139                         NULL, "no attributes provided", NULL, NULL );
140                 goto done;
141         }
142
143         Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d ADD dn=\"%s\"\n",
144             op->o_connid, op->o_opid, e->e_ndn, 0, 0 );
145
146         /*
147          * We could be serving multiple database backends.  Select the
148          * appropriate one, or send a referral to our "referral server"
149          * if we don't hold it.
150          */
151         be = select_backend( e->e_ndn );
152         if ( be == NULL ) {
153                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
154                         NULL, NULL, default_referral, NULL );
155                 goto done;
156         }
157
158         /* make sure this backend recongizes critical controls */
159         rc = backend_check_controls( be, conn, op, &text ) ;
160         if( rc != LDAP_SUCCESS ) {
161                 send_ldap_result( conn, op, rc,
162                         NULL, text, NULL, NULL );
163                 goto done;
164         }
165
166         /* check for referrals */
167         rc = backend_check_referrals( be, conn, op, &urls, &text );
168         if ( rc != LDAP_SUCCESS ) {
169                 send_ldap_result( conn, op, rc,
170                         NULL, text, urls, NULL );
171                 ber_bvecfree( urls );
172                 goto done;
173         }
174
175         if ( global_readonly || be->be_readonly ) {
176                 Debug( LDAP_DEBUG_ANY, "do_add: database is read-only\n",
177                        0, 0, 0 );
178                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
179                         NULL, "directory is read-only", NULL, NULL );
180                 goto done;
181         }
182
183         /*
184          * do the add if 1 && (2 || 3)
185          * 1) there is an add function implemented in this backend;
186          * 2) this backend is master for what it holds;
187          * 3) it's a replica and the dn supplied is the updatedn.
188          */
189         if ( be->be_add ) {
190                 /* do the update here */
191 #ifdef SLAPD_MULTIMASTER
192                 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
193                         global_lastmod == ON)) && (be->be_update_ndn == NULL ||
194                         strcmp( be->be_update_ndn, op->o_ndn )) )
195 #else
196                 if ( be->be_update_ndn == NULL ||
197                         strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
198 #endif
199                 {
200                         int update = be->be_update_ndn != NULL;
201
202                         rc = slap_modlist2mods( modlist, update, &mods, &text );
203                         if( rc != LDAP_SUCCESS ) {
204                                 send_ldap_result( conn, op, rc,
205                                         NULL, text, NULL, NULL );
206                                 goto done;
207                         }
208
209 #ifndef SLAPD_MULTIMASTER
210                         if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
211                                 global_lastmod == ON)) && !update )
212 #endif
213                         {
214                                 Modifications **modstail;
215                                 for( modstail = &mods;
216                                         *modstail != NULL;
217                                         modstail = &(*modstail)->sml_next )
218                                 {
219                                         assert( (*modstail)->sml_op == LDAP_MOD_ADD );
220                                         assert( (*modstail)->sml_desc != NULL );
221                                 }
222                                 rc = slap_mods_opattrs( op, modstail, &text );
223                                 if( rc != LDAP_SUCCESS ) {
224                                         send_ldap_result( conn, op, rc,
225                                                 NULL, text, NULL, NULL );
226                                         goto done;
227                                 }
228                         }
229
230                         rc = slap_mods2entry( mods, &e, &text );
231                         if( rc != LDAP_SUCCESS ) {
232                                 send_ldap_result( conn, op, rc,
233                                         NULL, text, NULL, NULL );
234                                 goto done;
235                         }
236
237                         if ( (*be->be_add)( be, conn, op, e ) == 0 ) {
238 #ifdef SLAPD_MULTIMASTER
239                                 if (be->be_update_ndn == NULL ||
240                                         strcmp( be->be_update_ndn, op->o_ndn ))
241 #endif
242                                 {
243                                         replog( be, op, e->e_dn, e );
244                                 }
245                                 be_entry_release_w( be, e );
246                                 e = NULL;
247                         }
248
249 #ifndef SLAPD_MULTIMASTER
250                 } else {
251                         send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
252                                 be->be_update_refs ? be->be_update_refs : default_referral, NULL );
253 #endif
254                 }
255         } else {
256             Debug( LDAP_DEBUG_ARGS, "    do_add: no backend support\n", 0, 0, 0 );
257                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
258                         NULL, "operation not supported within namingContext", NULL, NULL );
259         }
260
261 done:
262         if( modlist != NULL ) {
263                 slap_modlist_free( modlist );
264         }
265         if( mods != NULL ) {
266                 slap_mods_free( mods );
267         }
268         if( e != NULL ) {
269                 entry_free( e );
270         }
271
272         return rc;
273 }
274
275 static int slap_mods2entry(
276         Modifications *mods,
277         Entry **e,
278         const char **text )
279 {
280         Attribute **tail = &(*e)->e_attrs;
281         assert( *tail == NULL );
282
283         for( ; mods != NULL; mods = mods->sml_next ) {
284                 Attribute *attr;
285
286                 assert( mods->sml_op == LDAP_MOD_ADD );
287                 assert( mods->sml_desc != NULL );
288
289                 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
290
291                 if( attr != NULL ) {
292                         *text = "attribute provided more than once";
293                         return LDAP_OPERATIONS_ERROR;
294                 }
295
296                 attr = ch_calloc( 1, sizeof(Attribute) );
297
298                 /* move ad to attr structure */
299                 attr->a_desc = mods->sml_desc;
300                 mods->sml_desc = NULL;
301
302                 /* move values to attr structure */
303                 /*      should check for duplicates */
304                 attr->a_vals = mods->sml_bvalues;
305                 mods->sml_bvalues = NULL;
306
307                 *tail = attr;
308                 tail = &attr->a_next;
309         }
310
311         return LDAP_SUCCESS;
312 }
313