]> git.sur5r.net Git - openldap/blob - servers/slapd/modify.c
f1e1a52a435e98c93e1d1787eefbc8b046ba1637
[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         if ( global_readonly || be->be_readonly ) {
182                 Debug( LDAP_DEBUG_ANY, "do_modify: database is read-only\n",
183                        0, 0, 0 );
184                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
185                                   NULL, "database is read-only", NULL, NULL );
186                 goto done;
187         }
188
189         /* deref suffix alias if appropriate */
190         ndn = suffix_alias( be, ndn );
191
192         /*
193          * do the modify if 1 && (2 || 3)
194          * 1) there is a modify function implemented in this backend;
195          * 2) this backend is master for what it holds;
196          * 3) it's a replica and the dn supplied is the update_ndn.
197          */
198         if ( be->be_modify ) {
199                 /* do the update here */
200 #ifndef SLAPD_MULTIMASTER
201                 /* we don't have to check for replicator dn
202                  * because we accept each modify request
203                  */
204                 if ( be->be_update_ndn == NULL ||
205                         strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
206 #endif
207                 {
208                         if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
209                                 global_lastmod == ON)) && be->be_update_ndn == NULL )
210                         {
211                                 rc = add_modified_attrs( op, &modlist );
212
213                                 if( rc != LDAP_SUCCESS ) {
214                                         free( ndn );
215                                         modlist_free( modlist );
216                                         send_ldap_result( conn, op, rc,
217                                                 NULL, "no-user-modification attribute type",
218                                                 NULL, NULL );
219                                         return rc;
220                                 }
221                         }
222
223                         if ( (*be->be_modify)( be, conn, op, ndn, modlist ) == 0 
224 #ifdef SLAPD_MULTIMASTER
225                                 && ( be->be_update_ndn == NULL ||
226                                         strcmp( be->be_update_ndn, op->o_ndn ) != 0 )
227 #endif
228                         ) {
229                                 /* but we log only the ones not from a replicator user */
230                                 replog( be, op, ndn, modlist );
231                         }
232
233 #ifndef SLAPD_MULTIMASTER
234                 /* send a referral */
235                 } else {
236                         send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
237                                 be->be_update_refs ? be->be_update_refs : default_referral,
238                                 NULL );
239 #endif
240                 }
241         } else {
242                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
243                     NULL, "Function not implemented", NULL, NULL );
244         }
245
246 done:
247         free( ndn );
248         modlist_free( modlist );
249         return rc;
250 }
251
252 static int
253 add_modified_attrs( Operation *op, LDAPModList **modlist )
254 {
255         char            buf[22];
256         struct berval   bv;
257         struct berval   *bvals[2];
258         LDAPModList             *m;
259         struct tm       *ltm;
260         time_t          currenttime;
261
262         bvals[0] = &bv;
263         bvals[1] = NULL;
264
265         /* remove any attempts by the user to modify these attrs */
266         for ( m = *modlist; m != NULL; m = m->ml_next ) {
267                 if ( oc_check_no_usermod_attr( m->ml_type ) ) {
268                         return LDAP_CONSTRAINT_VIOLATION;
269                 }
270         }
271
272         if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
273                 bv.bv_val = "NULLDN";
274                 bv.bv_len = strlen( bv.bv_val );
275         } else {
276                 bv.bv_val = op->o_dn;
277                 bv.bv_len = strlen( bv.bv_val );
278         }
279         m = (LDAPModList *) ch_calloc( 1, sizeof(LDAPModList) );
280         m->ml_type = ch_strdup( "modifiersname" );
281         m->ml_op = LDAP_MOD_REPLACE;
282         m->ml_bvalues = (struct berval **) ch_calloc(2, sizeof(struct berval *));
283         m->ml_bvalues[0] = ber_bvdup( &bv );
284         m->ml_next = *modlist;
285         *modlist = m;
286
287         currenttime = slap_get_time();
288         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
289 #ifndef LDAP_LOCALTIME
290         ltm = gmtime( &currenttime );
291         strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
292 #else
293         ltm = localtime( &currenttime );
294         strftime( buf, sizeof(buf), "%y%m%d%H%M%SZ", ltm );
295 #endif
296         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
297
298         bv.bv_val = buf;
299         bv.bv_len = strlen( bv.bv_val );
300         m = (LDAPModList *) ch_calloc( 1, sizeof(LDAPModList) );
301         m->ml_type = ch_strdup( "modifytimestamp" );
302         m->ml_op = LDAP_MOD_REPLACE;
303         m->ml_bvalues = (struct berval **) ch_calloc(2, sizeof(struct berval *));
304         m->ml_bvalues[0] = ber_bvdup( &bv );
305         m->ml_next = *modlist;
306         *modlist = m;
307
308         return LDAP_SUCCESS;
309 }
310
311 static void
312 modlist_free(
313     LDAPModList *ml
314 )
315 {
316         LDAPModList *next;
317
318         for ( ; ml != NULL; ml = next ) {
319                 next = ml->ml_next;
320
321                 free( ml->ml_type );
322                 if ( ml->ml_bvalues != NULL )
323                         ber_bvecfree( ml->ml_bvalues );
324
325                 free( ml );
326         }
327 }