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