]> git.sur5r.net Git - openldap/blob - servers/slapd/modify.c
7a61fc1cd63a146dd92a44bbb76090e9cba45cb4
[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;
43         LDAPModList     **modtail;
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         if(     dn_normalize( dn ) == NULL ) {
89                 Debug( LDAP_DEBUG_ANY, "do_modify: invalid dn (%s)\n", dn, 0, 0 );
90                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
91                     "invalid DN", NULL, NULL );
92                 free( dn );
93                 return rc;
94         }
95
96         /* collect modifications & save for later */
97         modlist = NULL;
98         modtail = &modlist;
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) = (LDAPModList *) ch_calloc( 1, sizeof(LDAPModList) );
107
108                 if ( ber_scanf( op->o_ber, "{i{a[V]}}", &mop,
109                     &(*modtail)->ml_type, &(*modtail)->ml_bvalues )
110                     == LBER_ERROR )
111                 {
112                         send_ldap_disconnect( conn, op,
113                                 LDAP_PROTOCOL_ERROR, "decoding modlist error" );
114                         free( dn );
115                         free( *modtail );
116                         *modtail = NULL;
117                         modlist_free( modlist );
118                         return -1;
119                 }
120
121                 (*modtail)->ml_op = mop;
122                 
123                 if ( (*modtail)->ml_op != LDAP_MOD_ADD &&
124                     (*modtail)->ml_op != LDAP_MOD_DELETE &&
125                     (*modtail)->ml_op != LDAP_MOD_REPLACE )
126                 {
127                         Debug( LDAP_DEBUG_ANY,
128                                 "do_modify: invalid modify operation (%ld)\n",
129                                 (long) (*modtail)->ml_op, 0, 0 );
130                         send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
131                             NULL, "unrecognized modify operation", NULL, NULL );
132                         free( dn );
133                         modlist_free( modlist );
134                         return LDAP_PROTOCOL_ERROR;
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                         free( dn );
148                         modlist_free( modlist );
149                         return LDAP_PROTOCOL_ERROR;
150                 }
151                 attr_normalize( (*modtail)->ml_type );
152
153                 modtail = &(*modtail)->ml_next;
154         }
155         *modtail = NULL;
156
157 #ifdef LDAP_DEBUG
158         Debug( LDAP_DEBUG_ARGS, "modifications:\n", 0, 0, 0 );
159         for ( tmp = modlist; tmp != NULL; tmp = tmp->ml_next ) {
160                 Debug( LDAP_DEBUG_ARGS, "\t%s: %s\n",
161                         tmp->ml_op == LDAP_MOD_ADD
162                                 ? "add" : (tmp->ml_op == LDAP_MOD_DELETE
163                                         ? "delete" : "replace"), tmp->ml_type, 0 );
164         }
165 #endif
166
167         if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
168                 free( dn );
169                 modlist_free( modlist );
170                 Debug( LDAP_DEBUG_ANY, "do_modify: get_ctrls failed\n", 0, 0, 0 );
171                 return rc;
172         } 
173
174         Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d MOD dn=\"%s\"\n",
175             op->o_connid, op->o_opid, dn, 0, 0 );
176
177         ndn = ch_strdup( ndn );
178         ldap_pvt_str2upper( ndn );
179
180         /*
181          * We could be serving multiple database backends.  Select the
182          * appropriate one, or send a referral to our "referral server"
183          * if we don't hold it.
184          */
185         if ( (be = select_backend( ndn )) == NULL ) {
186                 free( dn );
187                 free( ndn );
188                 modlist_free( modlist );
189                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
190                         NULL, NULL, default_referral, NULL );
191                 return rc;
192         }
193
194         if ( global_readonly || be->be_readonly ) {
195                 Debug( LDAP_DEBUG_ANY, "do_modify: database is read-only\n",
196                        0, 0, 0 );
197                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
198                                   NULL, "database is read-only", NULL, NULL );
199                 goto done;
200         }
201
202         /* deref suffix alias if appropriate */
203         ndn = suffix_alias( be, ndn );
204
205         /*
206          * do the modify if 1 && (2 || 3)
207          * 1) there is a modify function implemented in this backend;
208          * 2) this backend is master for what it holds;
209          * 3) it's a replica and the dn supplied is the update_ndn.
210          */
211         if ( be->be_modify ) {
212                 /* do the update here */
213 #ifndef SLAPD_MULTIMASTER
214                 /* we don't have to check for replicator dn
215                  * because we accept each modify request
216                  */
217                 if ( be->be_update_ndn == NULL ||
218                         strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
219 #endif
220                 {
221                         if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
222                                 global_lastmod == ON)) && be->be_update_ndn == NULL )
223                         {
224                                 rc = add_modified_attrs( op, &modlist );
225
226                                 if( rc != LDAP_SUCCESS ) {
227                                         free( dn );
228                                         free( ndn );
229                                         modlist_free( modlist );
230                                         send_ldap_result( conn, op, rc,
231                                                 NULL, "no-user-modification attribute type",
232                                                 NULL, NULL );
233                                         return rc;
234                                 }
235                         }
236
237                         if ( (*be->be_modify)( be, conn, op, dn, ndn, modlist ) == 0 
238 #ifdef SLAPD_MULTIMASTER
239                                 && ( be->be_update_ndn == NULL ||
240                                         strcmp( be->be_update_ndn, op->o_ndn ) != 0 )
241 #endif
242                         ) {
243                                 /* but we log only the ones not from a replicator user */
244                                 replog( be, op, dn, modlist );
245                         }
246
247 #ifndef SLAPD_MULTIMASTER
248                 /* send a referral */
249                 } else {
250                         send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
251                                 be->be_update_refs ? be->be_update_refs : default_referral,
252                                 NULL );
253 #endif
254                 }
255         } else {
256                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
257                     NULL, "Function not implemented", NULL, NULL );
258         }
259
260 done:
261         free( dn );
262         free( ndn );
263         modlist_free( modlist );
264         return rc;
265 }
266
267 static int
268 add_modified_attrs( Operation *op, LDAPModList **modlist )
269 {
270         char            buf[22];
271         struct berval   bv;
272         struct berval   *bvals[2];
273         LDAPModList             *m;
274         struct tm       *ltm;
275         time_t          currenttime;
276
277         bvals[0] = &bv;
278         bvals[1] = NULL;
279
280         /* remove any attempts by the user to modify these attrs */
281         for ( m = *modlist; m != NULL; m = m->ml_next ) {
282                 if ( oc_check_no_usermod_attr( m->ml_type ) ) {
283                         return LDAP_CONSTRAINT_VIOLATION;
284                 }
285         }
286
287         if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
288                 bv.bv_val = "NULLDN";
289                 bv.bv_len = strlen( bv.bv_val );
290         } else {
291                 bv.bv_val = op->o_dn;
292                 bv.bv_len = strlen( bv.bv_val );
293         }
294         m = (LDAPModList *) ch_calloc( 1, sizeof(LDAPModList) );
295         m->ml_type = ch_strdup( "modifiersname" );
296         m->ml_op = LDAP_MOD_REPLACE;
297         m->ml_bvalues = (struct berval **) ch_calloc(2, sizeof(struct berval *));
298         m->ml_bvalues[0] = ber_bvdup( &bv );
299         m->ml_next = *modlist;
300         *modlist = m;
301
302         currenttime = slap_get_time();
303         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
304 #ifndef LDAP_LOCALTIME
305         ltm = gmtime( &currenttime );
306         strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
307 #else
308         ltm = localtime( &currenttime );
309         strftime( buf, sizeof(buf), "%y%m%d%H%M%SZ", ltm );
310 #endif
311         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
312
313         bv.bv_val = buf;
314         bv.bv_len = strlen( bv.bv_val );
315         m = (LDAPModList *) ch_calloc( 1, sizeof(LDAPModList) );
316         m->ml_type = ch_strdup( "modifytimestamp" );
317         m->ml_op = LDAP_MOD_REPLACE;
318         m->ml_bvalues = (struct berval **) ch_calloc(2, sizeof(struct berval *));
319         m->ml_bvalues[0] = ber_bvdup( &bv );
320         m->ml_next = *modlist;
321         *modlist = m;
322
323         return LDAP_SUCCESS;
324 }
325
326 static void
327 modlist_free(
328     LDAPModList *ml
329 )
330 {
331         LDAPModList *next;
332
333         for ( ; ml != NULL; ml = next ) {
334                 next = ml->ml_next;
335
336                 free( ml->ml_type );
337                 if ( ml->ml_bvalues != NULL )
338                         ber_bvecfree( ml->ml_bvalues );
339
340                 free( ml );
341         }
342 }