]> git.sur5r.net Git - openldap/blob - servers/slapd/add.c
3b20cde293553700fd9554145957ff9eb847f6d8
[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         char *text;
52 #endif
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 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 ) ;
185
186         if( rc != LDAP_SUCCESS ) {
187                 send_ldap_result( conn, op, rc,
188                         NULL, NULL, 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, "database 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,
242                                                 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, "Function not implemented", 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         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