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