]> git.sur5r.net Git - openldap/blob - servers/slapd/modify.c
Eliminate second session protocol version field.
[openldap] / servers / slapd / modify.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/socket.h>
23 #include <ac/string.h>
24 #include <ac/time.h>
25
26 #include "ldap_pvt.h"
27 #include "slap.h"
28
29 static void     modlist_free(LDAPModList *ml);
30
31 static int add_modified_attrs( Operation *op, Modifications **modlist );
32
33 int
34 do_modify(
35     Connection  *conn,
36     Operation   *op
37 )
38 {
39         char            *dn, *ndn;
40         char            *last;
41         ber_tag_t       tag;
42         ber_len_t       len;
43         LDAPModList     *modlist = NULL;
44         LDAPModList     **modtail = &modlist;
45         Modifications *mods = NULL;
46 #ifdef LDAP_DEBUG
47         Modifications *tmp;
48 #endif
49         Backend         *be;
50         int rc;
51
52         Debug( LDAP_DEBUG_TRACE, "do_modify\n", 0, 0, 0 );
53
54         if( op->o_bind_in_progress ) {
55                 Debug( LDAP_DEBUG_ANY, "do_modify: SASL bind in progress.\n",
56                         0, 0, 0 );
57                 send_ldap_result( conn, op, LDAP_SASL_BIND_IN_PROGRESS,
58                         NULL, "SASL bind in progress", NULL, NULL );
59                 return LDAP_SASL_BIND_IN_PROGRESS;
60         }
61
62         /*
63          * Parse the modify request.  It looks like this:
64          *
65          *      ModifyRequest := [APPLICATION 6] SEQUENCE {
66          *              name    DistinguishedName,
67          *              mods    SEQUENCE OF SEQUENCE {
68          *                      operation       ENUMERATED {
69          *                              add     (0),
70          *                              delete  (1),
71          *                              replace (2)
72          *                      },
73          *                      modification    SEQUENCE {
74          *                              type    AttributeType,
75          *                              values  SET OF AttributeValue
76          *                      }
77          *              }
78          *      }
79          */
80
81         if ( ber_scanf( op->o_ber, "{a" /*}*/, &dn ) == LBER_ERROR ) {
82                 Debug( LDAP_DEBUG_ANY, "do_modify: ber_scanf failed\n", 0, 0, 0 );
83                 send_ldap_disconnect( conn, op,
84                         LDAP_PROTOCOL_ERROR, "decoding error" );
85                 return -1;
86         }
87
88         Debug( LDAP_DEBUG_ARGS, "do_modify: dn (%s)\n", dn, 0, 0 );
89
90         ndn = ch_strdup( dn );
91
92         if(     dn_normalize( ndn ) == NULL ) {
93                 Debug( LDAP_DEBUG_ANY, "do_modify: invalid dn (%s)\n", dn, 0, 0 );
94                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
95                     "invalid DN", NULL, NULL );
96                 goto cleanup;
97         }
98
99         /* collect modifications & save for later */
100
101         for ( tag = ber_first_element( op->o_ber, &len, &last );
102             tag != LBER_DEFAULT;
103             tag = ber_next_element( op->o_ber, &len, last ) )
104         {
105                 ber_int_t mop;
106
107                 (*modtail) = (LDAPModList *) ch_calloc( 1, sizeof(LDAPModList) );
108
109                 if ( ber_scanf( op->o_ber, "{i{a[V]}}", &mop,
110                     &(*modtail)->ml_type, &(*modtail)->ml_bvalues )
111                     == LBER_ERROR )
112                 {
113                         send_ldap_disconnect( conn, op,
114                                 LDAP_PROTOCOL_ERROR, "decoding modlist error" );
115                         rc = -1;
116                         goto cleanup;
117                 }
118
119                 (*modtail)->ml_op = mop;
120                 
121                 if ( (*modtail)->ml_op != LDAP_MOD_ADD &&
122                     (*modtail)->ml_op != LDAP_MOD_DELETE &&
123                     (*modtail)->ml_op != LDAP_MOD_REPLACE )
124                 {
125                         Debug( LDAP_DEBUG_ANY,
126                                 "do_modify: invalid modify operation (%ld)\n",
127                                 (long) (*modtail)->ml_op, 0, 0 );
128                         send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
129                             NULL, "unrecognized modify operation", NULL, NULL );
130                         rc = LDAP_PROTOCOL_ERROR;
131                         goto cleanup;
132                 }
133
134                 if ( (*modtail)->ml_bvalues == NULL && (
135                         (*modtail)->ml_op != LDAP_MOD_REPLACE &&
136                         (*modtail)->ml_op != LDAP_MOD_DELETE ) )
137                 {
138                         Debug( LDAP_DEBUG_ANY,
139                                 "do_modify: invalid modify operation (%ld) without values\n",
140                                 (long) (*modtail)->ml_op, 0, 0 );
141                         send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
142                             NULL, "unrecognized modify operation without values",
143                                 NULL, NULL );
144                         rc = LDAP_PROTOCOL_ERROR;
145                         goto cleanup;
146                 }
147
148 #ifndef SLAPD_SCHEMA_NOT_COMPAT
149                 attr_normalize( (*modtail)->ml_type );
150 #endif
151
152                 modtail = &(*modtail)->ml_next;
153         }
154         *modtail = NULL;
155
156         if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
157                 Debug( LDAP_DEBUG_ANY, "do_modify: get_ctrls failed\n", 0, 0, 0 );
158                 goto cleanup;
159         }
160
161 #ifdef SLAPD_SCHEMA_NOT_COMPAT
162         /* not yet implemented */
163 #else
164         mods = modlist;
165 #endif
166
167 #ifdef LDAP_DEBUG
168         Debug( LDAP_DEBUG_ARGS, "modifications:\n", 0, 0, 0 );
169         for ( tmp = mods; tmp != NULL; tmp = tmp->sml_next ) {
170 #ifdef SLAPD_SCHEMA_NOT_COMPAT
171                 char *type = tmp->sml_desc.ad_cname->bv_val;
172 #else
173                 char *type = tmp->sml_type;
174 #endif
175                 Debug( LDAP_DEBUG_ARGS, "\t%s: %s\n",
176                         tmp->sml_op == LDAP_MOD_ADD
177                                 ? "add" : (tmp->sml_op == LDAP_MOD_DELETE
178                                         ? "delete" : "replace"), type, 0 );
179         }
180 #endif
181
182
183
184         Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d MOD dn=\"%s\"\n",
185             op->o_connid, op->o_opid, dn, 0, 0 );
186
187         /*
188          * We could be serving multiple database backends.  Select the
189          * appropriate one, or send a referral to our "referral server"
190          * if we don't hold it.
191          */
192         if ( (be = select_backend( ndn )) == NULL ) {
193                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
194                         NULL, NULL, default_referral, NULL );
195                 goto cleanup;
196         }
197
198         /* make sure this backend recongizes critical controls */
199         rc = backend_check_controls( be, conn, op ) ;
200
201         if( rc != LDAP_SUCCESS ) {
202                 send_ldap_result( conn, op, rc,
203                         NULL, NULL, NULL, NULL );
204                 goto cleanup;
205         }
206
207         if ( global_readonly || be->be_readonly ) {
208                 Debug( LDAP_DEBUG_ANY, "do_modify: database is read-only\n",
209                        0, 0, 0 );
210                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
211                                   NULL, "database is read-only", NULL, NULL );
212                 goto cleanup;
213         }
214
215         /* deref suffix alias if appropriate */
216         ndn = suffix_alias( be, ndn );
217
218         /*
219          * do the modify if 1 && (2 || 3)
220          * 1) there is a modify function implemented in this backend;
221          * 2) this backend is master for what it holds;
222          * 3) it's a replica and the dn supplied is the update_ndn.
223          */
224         if ( be->be_modify ) {
225                 /* do the update here */
226 #ifndef SLAPD_MULTIMASTER
227                 /* we don't have to check for replicator dn
228                  * because we accept each modify request
229                  */
230                 if ( be->be_update_ndn == NULL ||
231                         strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
232 #endif
233                 {
234                         if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
235                                 global_lastmod == ON)) && be->be_update_ndn == NULL )
236                         {
237                                 rc = add_modified_attrs( op, &mods );
238
239                                 if( rc != LDAP_SUCCESS ) {
240                                         send_ldap_result( conn, op, rc,
241                                                 NULL, "no-user-modification attribute type",
242                                                 NULL, NULL );
243                                         goto cleanup;
244                                 }
245                         }
246
247                         if ( (*be->be_modify)( be, conn, op, dn, ndn, mods ) == 0 
248 #ifdef SLAPD_MULTIMASTER
249                                 && ( be->be_update_ndn == NULL ||
250                                         strcmp( be->be_update_ndn, op->o_ndn ) != 0 )
251 #endif
252                         ) {
253                                 /* but we log only the ones not from a replicator user */
254                                 replog( be, op, dn, mods );
255                         }
256
257 #ifndef SLAPD_MULTIMASTER
258                 /* send a referral */
259                 } else {
260                         send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
261                                 be->be_update_refs ? be->be_update_refs : default_referral,
262                                 NULL );
263 #endif
264                 }
265         } else {
266                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
267                     NULL, "Function not implemented", NULL, NULL );
268         }
269
270 cleanup:
271         free( dn );
272         free( ndn );
273         if ( modlist != NULL )
274                 modlist_free( modlist );
275         return rc;
276 }
277
278 static int
279 add_modified_attrs( Operation *op, Modifications **modlist )
280 {
281         char            buf[22];
282         struct berval   bv;
283         struct berval   *bvals[2];
284         Modifications           *m;
285         struct tm       *ltm;
286         time_t          currenttime;
287
288         bvals[0] = &bv;
289         bvals[1] = NULL;
290
291 #ifdef SLAPD_SCHEMA_NOT_COMPAT
292         /* not yet implemented */
293 #else
294         /* remove any attempts by the user to modify these attrs */
295         for ( m = *modlist; m != NULL; m = m->ml_next ) {
296                 if ( oc_check_op_no_usermod_attr( m->ml_type ) ) {
297                         return LDAP_CONSTRAINT_VIOLATION;
298                 }
299         }
300
301         if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
302                 bv.bv_val = "<anonymous>";
303                 bv.bv_len = strlen( bv.bv_val );
304         } else {
305                 bv.bv_val = op->o_dn;
306                 bv.bv_len = strlen( bv.bv_val );
307         }
308         m = (Modifications *) ch_calloc( 1, sizeof(Modifications) );
309         m->ml_type = ch_strdup( "modifiersname" );
310         m->ml_op = LDAP_MOD_REPLACE;
311         m->ml_bvalues = (struct berval **) ch_calloc(2, sizeof(struct berval *));
312         m->ml_bvalues[0] = ber_bvdup( &bv );
313         m->ml_next = *modlist;
314         *modlist = m;
315
316         currenttime = slap_get_time();
317         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
318         ltm = gmtime( &currenttime );
319         strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
320         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
321
322         bv.bv_val = buf;
323         bv.bv_len = strlen( bv.bv_val );
324         m = (Modifications *) ch_calloc( 1, sizeof(Modifications) );
325         m->ml_type = ch_strdup( "modifytimestamp" );
326         m->ml_op = LDAP_MOD_REPLACE;
327         m->ml_bvalues = (struct berval **) ch_calloc(2, sizeof(struct berval *));
328         m->ml_bvalues[0] = ber_bvdup( &bv );
329         m->ml_next = *modlist;
330         *modlist = m;
331 #endif
332
333         return LDAP_SUCCESS;
334 }
335
336 static void
337 modlist_free(
338     LDAPModList *ml
339 )
340 {
341         LDAPModList *next;
342
343         for ( ; ml != NULL; ml = next ) {
344                 next = ml->ml_next;
345
346                 if (ml->ml_type)
347                         free( ml->ml_type );
348
349                 if ( ml->ml_bvalues != NULL )
350                         ber_bvecfree( ml->ml_bvalues );
351
352                 free( ml );
353         }
354 }