]> git.sur5r.net Git - openldap/blob - servers/slapd/add.c
1b7333f5cff4e4a5c55473db1d3143ffb78ea43f
[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
22 #include <ac/string.h>
23 #include <ac/time.h>
24 #include <ac/socket.h>
25
26 #include "ldap_pvt.h"
27 #include "slap.h"
28
29 static int slap_mods2entry(
30         Modifications *mods,
31         Entry **e,
32         const char **text );
33
34 int
35 do_add( Connection *conn, Operation *op )
36 {
37         BerElement      *ber = op->o_ber;
38         char            *dn, *ndn, *last;
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
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         {
139                 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
140                         NULL, "no attributes provided", NULL, NULL );
141                 goto done;
142         }
143
144         Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d ADD dn=\"%s\"\n",
145             op->o_connid, op->o_opid, e->e_ndn, 0, 0 );
146
147         /*
148          * We could be serving multiple database backends.  Select the
149          * appropriate one, or send a referral to our "referral server"
150          * if we don't hold it.
151          */
152         be = select_backend( e->e_ndn );
153         if ( be == NULL ) {
154                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
155                         NULL, NULL, default_referral, NULL );
156                 goto done;
157         }
158
159         /* make sure this backend recongizes critical controls */
160         rc = backend_check_controls( be, conn, op, &text ) ;
161
162         if( rc != LDAP_SUCCESS ) {
163                 send_ldap_result( conn, op, rc,
164                         NULL, text, NULL, NULL );
165                 goto done;
166         }
167
168         if ( global_readonly || be->be_readonly ) {
169                 Debug( LDAP_DEBUG_ANY, "do_add: database is read-only\n",
170                        0, 0, 0 );
171                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
172                         NULL, "directory is read-only", NULL, NULL );
173                 goto done;
174         }
175
176         /*
177          * do the add if 1 && (2 || 3)
178          * 1) there is an add function implemented in this backend;
179          * 2) this backend is master for what it holds;
180          * 3) it's a replica and the dn supplied is the updatedn.
181          */
182         if ( be->be_add ) {
183                 /* do the update here */
184 #ifdef SLAPD_MULTIMASTER
185                 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
186                         global_lastmod == ON)) && (be->be_update_ndn == NULL ||
187                         strcmp( be->be_update_ndn, op->o_ndn )) )
188 #else
189                 if ( be->be_update_ndn == NULL ||
190                         strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
191 #endif
192                 {
193                         int update = be->be_update_ndn != NULL;
194
195                         rc = slap_modlist2mods( modlist, update, &mods, &text );
196                         if( rc != LDAP_SUCCESS ) {
197                                 send_ldap_result( conn, op, rc,
198                                         NULL, text, NULL, NULL );
199                                 goto done;
200                         }
201
202 #ifndef SLAPD_MULTIMASTER
203                         if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
204                                 global_lastmod == ON)) && !update )
205 #endif
206                         {
207                                 Modifications **modstail;
208                                 for( modstail = &mods;
209                                         *modstail != NULL;
210                                         modstail = &(*modstail)->sml_next )
211                                 {
212                                         assert( (*modstail)->sml_op == LDAP_MOD_ADD );
213                                         assert( (*modstail)->sml_desc != NULL );
214                                 }
215                                 rc = slap_mods_opattrs( op, modstail, &text );
216                                 if( rc != LDAP_SUCCESS ) {
217                                         send_ldap_result( conn, op, rc,
218                                                 NULL, text, NULL, NULL );
219                                         goto done;
220                                 }
221                         }
222
223                         rc = slap_mods2entry( mods, &e, &text );
224                         if( rc != LDAP_SUCCESS ) {
225                                 send_ldap_result( conn, op, rc,
226                                         NULL, text, NULL, NULL );
227                                 goto done;
228                         }
229
230                         if ( (*be->be_add)( be, conn, op, e ) == 0 ) {
231 #ifdef SLAPD_MULTIMASTER
232                                 if (be->be_update_ndn == NULL ||
233                                         strcmp( be->be_update_ndn, op->o_ndn ))
234 #endif
235                                 {
236                                         replog( be, op, e->e_dn, e );
237                                 }
238                                 be_entry_release_w( be, e );
239                                 e = NULL;
240                         }
241
242 #ifndef SLAPD_MULTIMASTER
243                 } else {
244                         send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
245                                 be->be_update_refs ? be->be_update_refs : default_referral, NULL );
246 #endif
247                 }
248         } else {
249             Debug( LDAP_DEBUG_ARGS, "    do_add: no backend support\n", 0, 0, 0 );
250                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
251                         NULL, "operation not supported within namingContext", NULL, NULL );
252         }
253
254 done:
255         if( modlist != NULL ) {
256                 slap_modlist_free( modlist );
257         }
258         if( mods != NULL ) {
259                 slap_mods_free( mods );
260         }
261         if( e != NULL ) {
262                 entry_free( e );
263         }
264
265         return rc;
266 }
267
268 static int slap_mods2entry(
269         Modifications *mods,
270         Entry **e,
271         const char **text )
272 {
273         Attribute **tail = &(*e)->e_attrs;
274         assert( *tail == NULL );
275
276         for( ; mods != NULL; mods = mods->sml_next ) {
277                 Attribute *attr;
278
279                 assert( mods->sml_op == LDAP_MOD_ADD );
280                 assert( mods->sml_desc != NULL );
281
282                 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
283
284                 if( attr != NULL ) {
285                         *text = "attribute provided more than once";
286                         return LDAP_OPERATIONS_ERROR;
287                 }
288
289                 attr = ch_calloc( 1, sizeof(Attribute) );
290
291                 /* move ad to attr structure */
292                 attr->a_desc = mods->sml_desc;
293                 mods->sml_desc = NULL;
294
295                 /* move values to attr structure */
296                 /*      should check for duplicates */
297                 attr->a_vals = mods->sml_bvalues;
298                 mods->sml_bvalues = NULL;
299
300                 *tail = attr;
301                 tail = &attr->a_next;
302         }
303
304         return LDAP_SUCCESS;
305 }
306