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