]> git.sur5r.net Git - openldap/blob - servers/slapd/add.c
ad397914d75223d73f057eed2259a5dd8d05f804
[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 #ifdef SLAPD_SCHEMA_NOT_COMPAT
30 static int slap_mods2entry(
31         Modifications *mods,
32         Entry **e,
33         const char **text );
34 #else
35 static int      add_created_attrs(Operation *op, Entry *e);
36 #endif
37
38 int
39 do_add( Connection *conn, Operation *op )
40 {
41         BerElement      *ber = op->o_ber;
42         char            *dn, *ndn, *last;
43         ber_len_t       len;
44         ber_tag_t       tag;
45         Entry           *e;
46         Backend         *be;
47 #ifdef SLAPD_SCHEMA_NOT_COMPAT
48         LDAPModList     *modlist = NULL;
49         LDAPModList     **modtail = &modlist;
50         Modifications *mods = NULL;
51 #endif
52         const char *text;
53         int                     rc = LDAP_SUCCESS;
54
55         Debug( LDAP_DEBUG_TRACE, "do_add\n", 0, 0, 0 );
56
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, "{a", /*}*/ &dn ) == LBER_ERROR ) {
71                 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
72                 send_ldap_disconnect( conn, op,
73                         LDAP_PROTOCOL_ERROR, "decoding error" );
74                 return -1;
75         }
76
77         ndn = ch_strdup( dn );
78
79         if ( dn_normalize( ndn ) == NULL ) {
80                 Debug( LDAP_DEBUG_ANY, "do_add: invalid dn (%s)\n", dn, 0, 0 );
81                 send_ldap_result( conn, op, LDAP_INVALID_DN_SYNTAX, NULL,
82                     "invalid DN", NULL, NULL );
83                 free( dn );
84                 free( ndn );
85                 return LDAP_INVALID_DN_SYNTAX;
86         }
87
88         e = (Entry *) ch_calloc( 1, sizeof(Entry) );
89
90         e->e_dn = dn;
91         e->e_ndn = ndn;
92         e->e_attrs = NULL;
93         e->e_private = NULL;
94
95         Debug( LDAP_DEBUG_ARGS, "    do_add: ndn (%s)\n", e->e_ndn, 0, 0 );
96
97         /* get the attrs */
98         for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
99             tag = ber_next_element( ber, &len, last ) )
100         {
101 #ifdef SLAPD_SCHEMA_NOT_COMPAT
102                 LDAPModList *mod = (LDAPModList *) ch_malloc( sizeof(LDAPModList) );
103 #else
104                 LDAPModList tmpmod;
105                 LDAPModList *mod = &tmpmod;
106 #endif
107                 mod->ml_op = LDAP_MOD_ADD;
108                 mod->ml_next = NULL;
109
110                 rc = ber_scanf( ber, "{a{V}}", &mod->ml_type, &mod->ml_bvalues );
111
112                 if ( rc == LBER_ERROR ) {
113                         send_ldap_disconnect( conn, op,
114                                 LDAP_PROTOCOL_ERROR, "decoding error" );
115                         rc = -1;
116 #ifdef SLAPD_SCHEMA_NOT_COMPAT
117                         free( mod );
118 #endif
119                         goto done;
120                 }
121
122                 if ( mod->ml_bvalues == NULL ) {
123                         Debug( LDAP_DEBUG_ANY, "no values for type %s\n",
124                                 mod->ml_type, 0, 0 );
125                         send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
126                                 NULL, "no values for attribute type", NULL, NULL );
127                         free( mod->ml_type );
128 #ifdef SLAPD_SCHEMA_NOT_COMPAT
129                         free( mod );
130 #endif
131                         goto done;
132                 }
133
134 #ifdef SLAPD_SCHEMA_NOT_COMPAT
135                 *modtail = mod;
136                 modtail = &mod->ml_next;
137 #else
138                 attr_merge( e, mod->ml_type, mod->ml_bvalues );
139
140                 free( mod->ml_type );
141                 ber_bvecfree( mod->ml_bvalues );
142 #endif
143         }
144
145         if ( ber_scanf( ber, /*{*/ "}") == LBER_ERROR ) {
146                 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
147                 send_ldap_disconnect( conn, op,
148                         LDAP_PROTOCOL_ERROR, "decoding error" );
149                 rc = -1;
150                 goto done;
151         }
152
153         if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
154                 Debug( LDAP_DEBUG_ANY, "do_add: get_ctrls failed\n", 0, 0, 0 );
155                 goto done;
156         } 
157
158 #ifdef SLAPD_SCHEMA_NOT_COMPAT
159         if ( modlist == NULL )
160 #else
161         if ( e->e_attrs == NULL )
162 #endif
163         {
164                 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
165                         NULL, "no attributes provided", NULL, NULL );
166                 goto done;
167         }
168
169         Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d ADD dn=\"%s\"\n",
170             op->o_connid, op->o_opid, e->e_ndn, 0, 0 );
171
172         /*
173          * We could be serving multiple database backends.  Select the
174          * appropriate one, or send a referral to our "referral server"
175          * if we don't hold it.
176          */
177         be = select_backend( e->e_ndn );
178         if ( be == NULL ) {
179                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
180                         NULL, NULL, default_referral, NULL );
181                 goto done;
182         }
183
184         /* make sure this backend recongizes critical controls */
185         rc = backend_check_controls( be, conn, op, &text ) ;
186
187         if( rc != LDAP_SUCCESS ) {
188                 send_ldap_result( conn, op, rc,
189                         NULL, text, NULL, NULL );
190                 goto done;
191         }
192
193         if ( global_readonly || be->be_readonly ) {
194                 Debug( LDAP_DEBUG_ANY, "do_add: database is read-only\n",
195                        0, 0, 0 );
196                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
197                         NULL, "directory is read-only", NULL, NULL );
198                 goto done;
199         }
200
201         /*
202          * do the add if 1 && (2 || 3)
203          * 1) there is an add function implemented in this backend;
204          * 2) this backend is master for what it holds;
205          * 3) it's a replica and the dn supplied is the updatedn.
206          */
207         if ( be->be_add ) {
208                 /* do the update here */
209 #ifdef SLAPD_MULTIMASTER
210                 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
211                         global_lastmod == ON)) && (be->be_update_ndn == NULL ||
212                         strcmp( be->be_update_ndn, op->o_ndn )) )
213 #else
214                 if ( be->be_update_ndn == NULL ||
215                         strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
216 #endif
217                 {
218                         int update = be->be_update_ndn != NULL;
219
220 #ifdef SLAPD_SCHEMA_NOT_COMPAT
221                         rc = slap_modlist2mods( modlist, update, &mods, &text );
222                         if( rc != LDAP_SUCCESS ) {
223                                 send_ldap_result( conn, op, rc,
224                                         NULL, text, NULL, NULL );
225                                 goto done;
226                         }
227
228 #endif
229 #ifndef SLAPD_MULTIMASTER
230                         if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
231                                 global_lastmod == ON)) && !update )
232 #endif
233                         {
234 #ifdef SLAPD_SCHEMA_NOT_COMPAT
235                                 rc = slap_mods_opattrs( op, &mods, &text );
236 #else
237                                 char *text = "no-user-modification attribute type";
238                                 rc = add_created_attrs( op, e );
239 #endif
240                                 if( rc != LDAP_SUCCESS ) {
241                                         send_ldap_result( conn, op, rc,
242                                                 NULL, text, NULL, NULL );
243                                         goto done;
244                                 }
245                         }
246
247 #ifdef SLAPD_SCHEMA_NOT_COMPAT
248                         rc = slap_mods2entry( mods, &e, &text );
249                         if( rc != LDAP_SUCCESS ) {
250                                 send_ldap_result( conn, op, rc,
251                                         NULL, text, NULL, NULL );
252                                 goto done;
253                         }
254 #endif
255
256                         if ( (*be->be_add)( be, conn, op, e ) == 0 ) {
257 #ifdef SLAPD_MULTIMASTER
258                                 if (be->be_update_ndn == NULL ||
259                                         strcmp( be->be_update_ndn, op->o_ndn ))
260 #endif
261                                 {
262                                         replog( be, op, e->e_dn, e );
263                                 }
264                                 be_entry_release_w( be, e );
265                                 e = NULL;
266                         }
267
268 #ifndef SLAPD_MULTIMASTER
269                 } else {
270                         send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
271                                 be->be_update_refs ? be->be_update_refs : default_referral, NULL );
272 #endif
273                 }
274         } else {
275             Debug( LDAP_DEBUG_ARGS, "    do_add: HHH\n", 0, 0, 0 );
276                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
277                         NULL, "operation not supported within namingContext", NULL, NULL );
278         }
279
280 done:
281 #ifdef SLAPD_SCHEMA_NOT_COMPAT
282         if( modlist != NULL ) {
283                 slap_modlist_free( modlist );
284         }
285         if( mods != NULL ) {
286                 slap_mods_free( mods );
287         }
288 #endif
289         if( e != NULL ) {
290                 entry_free( e );
291         }
292
293         return rc;
294 }
295
296 #ifdef SLAPD_SCHEMA_NOT_COMPAT
297 static int slap_mods2entry(
298         Modifications *mods,
299         Entry **e,
300         const char **text )
301 {
302         Attribute **tail = &(*e)->e_attrs;
303         assert( *tail == NULL );
304
305         for( ; mods != NULL; mods = mods->sml_next ) {
306                 Attribute *attr;
307
308                 assert( mods->sml_op == LDAP_MOD_ADD );
309
310                 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
311
312                 if( attr != NULL ) {
313                         *text = "Attribute provided more than once";
314                         return LDAP_OPERATIONS_ERROR;
315                 }
316
317                 attr = ch_calloc( 1, sizeof(Attribute) );
318
319                 /* should check for duplicates */
320                 attr->a_vals = mods->sml_bvalues;
321                 mods->sml_bvalues = NULL;
322
323                 *tail = attr;
324                 tail = &attr->a_next;
325         }
326
327         return LDAP_SUCCESS;
328 }
329
330 #else
331 static int
332 add_created_attrs( Operation *op, Entry *e )
333 {
334         char            buf[22];
335         struct berval   bv;
336         struct berval   *bvals[2];
337         Attribute       *a;
338         struct tm       *ltm;
339         time_t          currenttime;
340
341         Debug( LDAP_DEBUG_TRACE, "add_created_attrs\n", 0, 0, 0 );
342
343         bvals[0] = &bv;
344         bvals[1] = NULL;
345
346         /* return error on any attempts by the user to add these attrs */
347         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
348 #ifdef SLAPD_SCHEMA_NOT_COMPAT
349                 if ( is_at_no_user_mod( a->a_desc.ad_type ))
350 #else
351                 if ( oc_check_op_no_usermod_attr( a->a_type ) )
352 #endif
353                 {
354                         return LDAP_CONSTRAINT_VIOLATION;
355                 }
356         }
357
358         if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
359                 bv.bv_val = "<anonymous>";
360                 bv.bv_len = sizeof("<anonymous>")-1;
361 ;
362         } else {
363                 bv.bv_val = op->o_dn;
364                 bv.bv_len = strlen( bv.bv_val );
365         }
366         attr_merge( e, "creatorsname", bvals );
367         attr_merge( e, "modifiersname", bvals );
368
369         currenttime = slap_get_time();
370         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
371         ltm = gmtime( &currenttime );
372         strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
373         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
374
375         bv.bv_val = buf;
376         bv.bv_len = strlen( bv.bv_val );
377         attr_merge( e, "createtimestamp", bvals );
378         attr_merge( e, "modifytimestamp", bvals );
379
380         return LDAP_SUCCESS;
381 }
382 #endif