]> git.sur5r.net Git - openldap/blob - servers/slapd/add.c
Use common entry cleanup code.
[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 static int      add_created_attrs(Operation *op, Entry *e);
30
31 int
32 do_add( Connection *conn, Operation *op )
33 {
34         BerElement      *ber = op->o_ber;
35         char            *dn, *ndn, *last;
36         ber_len_t       len;
37         ber_tag_t       tag;
38         Entry           *e;
39         Backend         *be;
40         int                     rc = LDAP_SUCCESS;
41
42         Debug( LDAP_DEBUG_TRACE, "do_add\n", 0, 0, 0 );
43
44         if( op->o_bind_in_progress ) {
45                 Debug( LDAP_DEBUG_ANY, "do_add: SASL bind in progress.\n", 0, 0, 0 );
46                 send_ldap_result( conn, op, LDAP_SASL_BIND_IN_PROGRESS, NULL,
47                     "SASL bind in progress", NULL, NULL );
48                 return LDAP_SASL_BIND_IN_PROGRESS;
49         }
50
51         /*
52          * Parse the add request.  It looks like this:
53          *
54          *      AddRequest := [APPLICATION 14] SEQUENCE {
55          *              name    DistinguishedName,
56          *              attrs   SEQUENCE OF SEQUENCE {
57          *                      type    AttributeType,
58          *                      values  SET OF AttributeValue
59          *              }
60          *      }
61          */
62
63         /* get the name */
64         if ( ber_scanf( ber, "{a", /*}*/ &dn ) == LBER_ERROR ) {
65                 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
66                 send_ldap_disconnect( conn, op,
67                         LDAP_PROTOCOL_ERROR, "decoding error" );
68                 return -1;
69         }
70
71         ndn = ch_strdup( dn );
72
73         if ( dn_normalize( ndn ) == NULL ) {
74                 Debug( LDAP_DEBUG_ANY, "do_add: invalid dn (%s)\n", dn, 0, 0 );
75                 send_ldap_result( conn, op, LDAP_INVALID_DN_SYNTAX, NULL,
76                     "invalid DN", NULL, NULL );
77                 free( dn );
78                 free( ndn );
79                 return LDAP_INVALID_DN_SYNTAX;
80         }
81
82         e = (Entry *) ch_calloc( 1, sizeof(Entry) );
83
84         e->e_dn = dn;
85         e->e_ndn = ndn;
86         e->e_attrs = NULL;
87         e->e_private = NULL;
88
89         Debug( LDAP_DEBUG_ARGS, "    do_add: ndn (%s)\n", e->e_ndn, 0, 0 );
90
91         /* get the attrs */
92         for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
93             tag = ber_next_element( ber, &len, last ) ) {
94                 char            *type;
95                 struct berval   **vals;
96
97                 if ( ber_scanf( ber, "{a{V}}", &type, &vals ) == LBER_ERROR ) {
98                         send_ldap_disconnect( conn, op,
99                                 LDAP_PROTOCOL_ERROR, "decoding error" );
100                         rc = -1;
101                         goto done;
102                 }
103
104                 if ( vals == NULL ) {
105                         Debug( LDAP_DEBUG_ANY, "no values for type %s\n", type,
106                             0, 0 );
107                         send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
108                                 NULL, "no values for type", NULL, NULL );
109                         free( type );
110                         goto done;
111                 }
112
113                 attr_merge( e, type, vals );
114
115                 free( type );
116                 ber_bvecfree( vals );
117         }
118
119         if ( ber_scanf( ber, /*{*/ "}") == LBER_ERROR ) {
120                 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
121                 send_ldap_disconnect( conn, op,
122                         LDAP_PROTOCOL_ERROR, "decoding error" );
123                 rc = -1;
124                 goto done;
125         }
126
127         if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
128                 Debug( LDAP_DEBUG_ANY, "do_add: get_ctrls failed\n", 0, 0, 0 );
129                 goto done;
130         } 
131
132         Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d ADD dn=\"%s\"\n",
133             op->o_connid, op->o_opid, e->e_ndn, 0, 0 );
134
135         /*
136          * We could be serving multiple database backends.  Select the
137          * appropriate one, or send a referral to our "referral server"
138          * if we don't hold it.
139          */
140         be = select_backend( e->e_ndn );
141         if ( be == NULL ) {
142                 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL,
143                     NULL, default_referral, NULL );
144                 goto done;
145         }
146
147         /* make sure this backend recongizes critical controls */
148         rc = backend_check_controls( be, conn, op ) ;
149
150         if( rc != LDAP_SUCCESS ) {
151                 send_ldap_result( conn, op, rc,
152                         NULL, NULL, NULL, NULL );
153                 goto done;
154         }
155
156         if ( global_readonly || be->be_readonly ) {
157                 Debug( LDAP_DEBUG_ANY, "do_add: database is read-only\n",
158                        0, 0, 0 );
159                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
160                                   NULL, "database is read-only", NULL, NULL );
161                 goto done;
162         }
163
164         /*
165          * do the add if 1 && (2 || 3)
166          * 1) there is an add function implemented in this backend;
167          * 2) this backend is master for what it holds;
168          * 3) it's a replica and the dn supplied is the updatedn.
169          */
170         if ( be->be_add ) {
171                 /* do the update here */
172 #ifdef SLAPD_MULTIMASTER
173                 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
174                         global_lastmod == ON)) && (be->be_update_ndn == NULL ||
175                         strcmp( be->be_update_ndn, op->o_ndn )) )
176 #else
177                 if ( be->be_update_ndn == NULL ||
178                         strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
179 #endif
180                 {
181 #ifndef SLAPD_MULTIMASTER
182                         if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
183                                 global_lastmod == ON)) && be->be_update_ndn == NULL )
184 #endif
185                         {
186                                 rc = add_created_attrs( op, e );
187
188                                 if( rc != LDAP_SUCCESS ) {
189                                         send_ldap_result( conn, op, rc,
190                                                 NULL, "no-user-modification attribute type",
191                                                 NULL, NULL );
192                                         goto done;
193                                 }
194                         }
195
196                         if ( (*be->be_add)( be, conn, op, e ) == 0 ) {
197 #ifdef SLAPD_MULTIMASTER
198                                 if (be->be_update_ndn == NULL ||
199                                         strcmp( be->be_update_ndn, op->o_ndn ))
200 #endif
201                                 {
202                                         replog( be, op, e->e_dn, e );
203                                 }
204                                 be_entry_release_w( be, e );
205                                 e = NULL;
206                         }
207
208 #ifndef SLAPD_MULTIMASTER
209                 } else {
210                         send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
211                                 be->be_update_refs ? be->be_update_refs : default_referral, NULL );
212 #endif
213                 }
214         } else {
215             Debug( LDAP_DEBUG_ARGS, "    do_add: HHH\n", 0, 0, 0 );
216                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
217                         NULL, "Function not implemented", NULL, NULL );
218         }
219
220 done:
221         if( e ) {
222                 entry_free( e );
223         }
224
225         return rc;
226 }
227
228 static int
229 add_created_attrs( Operation *op, Entry *e )
230 {
231         char            buf[22];
232         struct berval   bv;
233         struct berval   *bvals[2];
234         Attribute       *a;
235         struct tm       *ltm;
236         time_t          currenttime;
237
238         Debug( LDAP_DEBUG_TRACE, "add_created_attrs\n", 0, 0, 0 );
239
240         bvals[0] = &bv;
241         bvals[1] = NULL;
242
243         /* return error on any attempts by the user to add these attrs */
244         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
245 #ifdef SLAPD_SCHEMA_NOT_COMPAT
246                 if ( is_at_no_user_mod( a->a_desc.ad_type ))
247 #else
248                 if ( oc_check_op_no_usermod_attr( a->a_type ) )
249 #endif
250                 {
251                         return LDAP_CONSTRAINT_VIOLATION;
252                 }
253         }
254
255         if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
256                 bv.bv_val = "<anonymous>";
257                 bv.bv_len = sizeof("<anonymous>")-1;
258 ;
259         } else {
260                 bv.bv_val = op->o_dn;
261                 bv.bv_len = strlen( bv.bv_val );
262         }
263         attr_merge( e, "creatorsname", bvals );
264         attr_merge( e, "modifiersname", bvals );
265
266         currenttime = slap_get_time();
267         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
268         ltm = gmtime( &currenttime );
269         strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
270         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
271
272         bv.bv_val = buf;
273         bv.bv_len = strlen( bv.bv_val );
274         attr_merge( e, "createtimestamp", bvals );
275         attr_merge( e, "modifytimestamp", bvals );
276
277         return LDAP_SUCCESS;
278 }