]> git.sur5r.net Git - openldap/blob - servers/slapd/add.c
Add hacks to allow debugging with CSRI malloc.
[openldap] / servers / slapd / add.c
1 /*
2  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5 /*
6  * Copyright (c) 1995 Regents of the University of Michigan.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms are permitted
10  * provided that this notice is preserved and that due credit is given
11  * to the University of Michigan at Ann Arbor. The name of the University
12  * may not be used to endorse or promote products derived from this
13  * software without specific prior written permission. This software
14  * is provided ``as is'' without express or implied warranty.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20
21 #include <ac/string.h>
22 #include <ac/time.h>
23 #include <ac/socket.h>
24
25 #include "slap.h"
26
27 static int      add_created_attrs(Operation *op, Entry *e);
28
29 int
30 do_add( Connection *conn, Operation *op )
31 {
32         BerElement      *ber = op->o_ber;
33         char            *dn, *ndn, *last;
34         ber_len_t       len;
35         ber_tag_t       tag;
36         Entry           *e;
37         Backend         *be;
38         int                     rc = LDAP_SUCCESS;
39
40         Debug( LDAP_DEBUG_TRACE, "do_add\n", 0, 0, 0 );
41
42         if( op->o_bind_in_progress ) {
43                 Debug( LDAP_DEBUG_ANY, "do_add: SASL bind in progress.\n", 0, 0, 0 );
44                 send_ldap_result( conn, op, LDAP_SASL_BIND_IN_PROGRESS, NULL,
45                     "SASL bind in progress", NULL, NULL );
46                 return LDAP_SASL_BIND_IN_PROGRESS;
47         }
48
49         /*
50          * Parse the add request.  It looks like this:
51          *
52          *      AddRequest := [APPLICATION 14] SEQUENCE {
53          *              name    DistinguishedName,
54          *              attrs   SEQUENCE OF SEQUENCE {
55          *                      type    AttributeType,
56          *                      values  SET OF AttributeValue
57          *              }
58          *      }
59          */
60
61         /* get the name */
62         if ( ber_scanf( ber, "{a", /*}*/ &dn ) == LBER_ERROR ) {
63                 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
64                 send_ldap_disconnect( conn, op,
65                         LDAP_PROTOCOL_ERROR, "decoding error" );
66                 return -1;
67         }
68
69         ndn = ch_strdup( dn );
70
71         if ( dn_normalize_case( ndn ) == NULL ) {
72                 Debug( LDAP_DEBUG_ANY, "do_add: invalid dn (%s)\n", dn, 0, 0 );
73                 send_ldap_result( conn, op, LDAP_INVALID_DN_SYNTAX, NULL,
74                     "invalid DN", NULL, NULL );
75                 free( dn );
76                 free( ndn );
77                 return LDAP_INVALID_DN_SYNTAX;
78         }
79
80         e = (Entry *) ch_calloc( 1, sizeof(Entry) );
81
82         e->e_dn = dn;
83         e->e_ndn = ndn;
84         e->e_private = NULL;
85
86         Debug( LDAP_DEBUG_ARGS, "    do_add: ndn (%s)\n", e->e_ndn, 0, 0 );
87
88         /* get the attrs */
89         e->e_attrs = NULL;
90         for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
91             tag = ber_next_element( ber, &len, last ) ) {
92                 char            *type;
93                 struct berval   **vals;
94
95                 if ( ber_scanf( ber, "{a{V}}", &type, &vals ) == LBER_ERROR ) {
96                         send_ldap_disconnect( conn, op,
97                                 LDAP_PROTOCOL_ERROR, "decoding error" );
98                         entry_free( e );
99                         return -1;
100                 }
101
102                 if ( vals == NULL ) {
103                         Debug( LDAP_DEBUG_ANY, "no values for type %s\n", type,
104                             0, 0 );
105                         send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
106                                 NULL, "no values for type", NULL, NULL );
107                         free( type );
108                         entry_free( e );
109                         return LDAP_PROTOCOL_ERROR;
110                 }
111
112                 attr_merge( e, type, vals );
113
114                 free( type );
115                 ber_bvecfree( vals );
116         }
117
118         if ( ber_scanf( ber, /*{*/ "}") == LBER_ERROR ) {
119                 entry_free( e );
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                 return -1;
124         }
125
126         if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
127                 entry_free( e );
128                 Debug( LDAP_DEBUG_ANY, "do_add: get_ctrls failed\n", 0, 0, 0 );
129                 return rc;
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                 entry_free( e );
143                 send_ldap_result( conn, op, LDAP_REFERRAL, NULL,
144                     NULL, default_referral, NULL );
145                 return rc;
146         }
147
148         /*
149          * do the add if 1 && (2 || 3)
150          * 1) there is an add function implemented in this backend;
151          * 2) this backend is master for what it holds;
152          * 3) it's a replica and the dn supplied is the updatedn.
153          */
154         if ( be->be_add ) {
155                 /* do the update here */
156 #ifdef SLAPD_MULTIMASTER
157                 if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
158                         global_lastmod == ON)) && (be->be_update_ndn == NULL ||
159                         strcmp( be->be_update_ndn, op->o_ndn )) )
160 #else
161                 if ( be->be_update_ndn == NULL ||
162                         strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
163 #endif
164                 {
165 #ifndef SLAPD_MULTIMASTER
166                         if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
167                                 global_lastmod == ON)) && be->be_update_ndn == NULL )
168 #endif
169                         {
170                                 rc = add_created_attrs( op, e );
171
172                                 if( rc != LDAP_SUCCESS ) {
173                                         entry_free( e );
174                                         send_ldap_result( conn, op, rc,
175                                                 NULL, "no-user-modification attribute type",
176                                                 NULL, NULL );
177                                         return rc;
178                                 }
179                         }
180
181                         if ( (*be->be_add)( be, conn, op, e ) == 0 ) {
182 #ifdef SLAPD_MULTIMASTER
183                                 if (be->be_update_ndn == NULL ||
184                                         strcmp( be->be_update_ndn, op->o_ndn ))
185 #endif
186                                 {
187                                         replog( be, op, e->e_dn, e );
188                                 }
189                                 be_entry_release_w( be, e );
190                         }
191
192 #ifndef SLAPD_MULTIMASTER
193                 } else {
194                         entry_free( e );
195                         send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
196                                 be->be_update_refs ? be->be_update_refs : default_referral, NULL );
197 #endif
198                 }
199         } else {
200             Debug( LDAP_DEBUG_ARGS, "    do_add: HHH\n", 0, 0, 0 );
201                 entry_free( e );
202                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
203                         NULL, "Function not implemented", NULL, NULL );
204         }
205
206         return rc;
207 }
208
209 static int
210 add_created_attrs( Operation *op, Entry *e )
211 {
212         char            buf[22];
213         struct berval   bv;
214         struct berval   *bvals[2];
215         Attribute       *a;
216         struct tm       *ltm;
217         time_t          currenttime;
218
219         Debug( LDAP_DEBUG_TRACE, "add_created_attrs\n", 0, 0, 0 );
220
221         bvals[0] = &bv;
222         bvals[1] = NULL;
223
224         /* return error on any attempts by the user to add these attrs */
225         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
226                 if ( oc_check_no_usermod_attr( a->a_type ) ) {
227                         return LDAP_CONSTRAINT_VIOLATION;
228                 }
229         }
230
231         if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
232                 bv.bv_val = "<anonymous>";
233                 bv.bv_len = strlen( bv.bv_val );
234         } else {
235                 bv.bv_val = op->o_dn;
236                 bv.bv_len = strlen( bv.bv_val );
237         }
238         attr_merge( e, "creatorsname", bvals );
239
240         currenttime = slap_get_time();
241         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
242 #ifndef LDAP_LOCALTIME
243         ltm = gmtime( &currenttime );
244         strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
245 #else
246         ltm = localtime( &currenttime );
247         strftime( buf, sizeof(buf), "%y%m%d%H%M%SZ", ltm );
248 #endif
249         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
250
251         bv.bv_val = buf;
252         bv.bv_len = strlen( bv.bv_val );
253         attr_merge( e, "createtimestamp", bvals );
254
255         return LDAP_SUCCESS;
256 }