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