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