]> git.sur5r.net Git - openldap/blob - servers/slapd/add.c
Install *.schema only
[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                                 Modifications **modstail;
237                                 for( modstail = &mods;
238                                         *modstail != NULL;
239                                         modstail = &(*modstail)->sml_next )
240                                 {
241                                         assert( (*modstail)->sml_op == LDAP_MOD_ADD );
242                                         assert( (*modstail)->sml_desc != NULL );
243                                 }
244                                 rc = slap_mods_opattrs( op, modstail, &text );
245 #else
246                                 char *text = "no-user-modification attribute type";
247                                 rc = add_created_attrs( op, e );
248 #endif
249                                 if( rc != LDAP_SUCCESS ) {
250                                         send_ldap_result( conn, op, rc,
251                                                 NULL, text, NULL, NULL );
252                                         goto done;
253                                 }
254                         }
255
256 #ifdef SLAPD_SCHEMA_NOT_COMPAT
257                         rc = slap_mods2entry( mods, &e, &text );
258                         if( rc != LDAP_SUCCESS ) {
259                                 send_ldap_result( conn, op, rc,
260                                         NULL, text, NULL, NULL );
261                                 goto done;
262                         }
263 #endif
264
265                         if ( (*be->be_add)( be, conn, op, e ) == 0 ) {
266 #ifdef SLAPD_MULTIMASTER
267                                 if (be->be_update_ndn == NULL ||
268                                         strcmp( be->be_update_ndn, op->o_ndn ))
269 #endif
270                                 {
271                                         replog( be, op, e->e_dn, e );
272                                 }
273                                 be_entry_release_w( be, e );
274                                 e = NULL;
275                         }
276
277 #ifndef SLAPD_MULTIMASTER
278                 } else {
279                         send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
280                                 be->be_update_refs ? be->be_update_refs : default_referral, NULL );
281 #endif
282                 }
283         } else {
284             Debug( LDAP_DEBUG_ARGS, "    do_add: no backend support\n", 0, 0, 0 );
285                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
286                         NULL, "operation not supported within namingContext", NULL, NULL );
287         }
288
289 done:
290 #ifdef SLAPD_SCHEMA_NOT_COMPAT
291         if( modlist != NULL ) {
292                 slap_modlist_free( modlist );
293         }
294         if( mods != NULL ) {
295                 slap_mods_free( mods );
296         }
297 #endif
298         if( e != NULL ) {
299                 entry_free( e );
300         }
301
302         return rc;
303 }
304
305 #ifdef SLAPD_SCHEMA_NOT_COMPAT
306 static int slap_mods2entry(
307         Modifications *mods,
308         Entry **e,
309         const char **text )
310 {
311         Attribute **tail = &(*e)->e_attrs;
312         assert( *tail == NULL );
313
314         for( ; mods != NULL; mods = mods->sml_next ) {
315                 Attribute *attr;
316
317                 assert( mods->sml_op == LDAP_MOD_ADD );
318                 assert( mods->sml_desc != NULL );
319
320                 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
321
322                 if( attr != NULL ) {
323                         *text = "attribute provided more than once";
324                         return LDAP_OPERATIONS_ERROR;
325                 }
326
327                 attr = ch_calloc( 1, sizeof(Attribute) );
328
329                 /* move ad to attr structure */
330                 attr->a_desc = mods->sml_desc;
331                 mods->sml_desc = NULL;
332
333                 /* move values to attr structure */
334                 /*      should check for duplicates */
335                 attr->a_vals = mods->sml_bvalues;
336                 mods->sml_bvalues = NULL;
337
338                 *tail = attr;
339                 tail = &attr->a_next;
340         }
341
342         return LDAP_SUCCESS;
343 }
344
345 #else
346 static int
347 add_created_attrs( Operation *op, Entry *e )
348 {
349         char            buf[22];
350         struct berval   bv;
351         struct berval   *bvals[2];
352         Attribute       *a;
353         struct tm       *ltm;
354         time_t          currenttime;
355
356         Debug( LDAP_DEBUG_TRACE, "add_created_attrs\n", 0, 0, 0 );
357
358         bvals[0] = &bv;
359         bvals[1] = NULL;
360
361         /* return error on any attempts by the user to add these attrs */
362         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
363                 if ( oc_check_op_no_usermod_attr( a->a_type ) ) {
364                         return LDAP_CONSTRAINT_VIOLATION;
365                 }
366         }
367
368         if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
369                 bv.bv_val = SLAPD_ANONYMOUS;
370                 bv.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
371 ;
372         } else {
373                 bv.bv_val = op->o_dn;
374                 bv.bv_len = strlen( bv.bv_val );
375         }
376         attr_merge( e, "creatorsname", bvals );
377         attr_merge( e, "modifiersname", bvals );
378
379         currenttime = slap_get_time();
380         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
381         ltm = gmtime( &currenttime );
382         strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
383         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
384
385         bv.bv_val = buf;
386         bv.bv_len = strlen( bv.bv_val );
387         attr_merge( e, "createtimestamp", bvals );
388         attr_merge( e, "modifytimestamp", bvals );
389
390         return LDAP_SUCCESS;
391 }
392 #endif