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