]> git.sur5r.net Git - openldap/blob - servers/slapd/modify.c
c5556a33dc270ceafe5763c6ce1d80e4a5fb1d58
[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            *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" /*}*/, &ndn ) == 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", ndn, 0, 0 );
87
88         if(     dn_normalize_case( ndn ) == NULL ) {
89                 Debug( LDAP_DEBUG_ANY, "do_modify: invalid dn (%s)\n", ndn, 0, 0 );
90                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
91                     "invalid DN", NULL, NULL );
92                 free( ndn );
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( ndn );
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                         send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
128                             NULL, "unrecognized modify operation", NULL, NULL );
129                         free( ndn );
130                         modlist_free( modlist );
131                         return LDAP_PROTOCOL_ERROR;
132                 }
133
134                 if ( (*modtail)->ml_bvalues == NULL
135                         && (*modtail)->ml_op != LDAP_MOD_DELETE )
136                 {
137                         send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
138                             NULL, "unrecognized modify operation", NULL, NULL );
139                         free( ndn );
140                         modlist_free( modlist );
141                         return LDAP_PROTOCOL_ERROR;
142                 }
143                 attr_normalize( (*modtail)->ml_type );
144
145                 modtail = &(*modtail)->ml_next;
146         }
147         *modtail = NULL;
148
149 #ifdef LDAP_DEBUG
150         Debug( LDAP_DEBUG_ARGS, "modifications:\n", 0, 0, 0 );
151         for ( tmp = modlist; tmp != NULL; tmp = tmp->ml_next ) {
152                 Debug( LDAP_DEBUG_ARGS, "\t%s: %s\n",
153                         tmp->ml_op == LDAP_MOD_ADD
154                                 ? "add" : (tmp->ml_op == LDAP_MOD_DELETE
155                                         ? "delete" : "replace"), tmp->ml_type, 0 );
156         }
157 #endif
158
159         if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
160                 free( ndn );
161                 modlist_free( modlist );
162                 Debug( LDAP_DEBUG_ANY, "do_modify: get_ctrls failed\n", 0, 0, 0 );
163                 return rc;
164         } 
165
166         Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d MOD dn=\"%s\"\n",
167             op->o_connid, op->o_opid, ndn, 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                 free( ndn );
176                 modlist_free( modlist );
177                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
178                         NULL, NULL, default_referral, NULL );
179                 return rc;
180         }
181
182         if ( global_readonly || be->be_readonly ) {
183                 Debug( LDAP_DEBUG_ANY, "do_modify: database is read-only\n",
184                        0, 0, 0 );
185                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
186                                   NULL, "database is read-only", NULL, NULL );
187                 goto done;
188         }
189
190         /* deref suffix alias if appropriate */
191         ndn = suffix_alias( be, ndn );
192
193         /*
194          * do the modify if 1 && (2 || 3)
195          * 1) there is a modify function implemented in this backend;
196          * 2) this backend is master for what it holds;
197          * 3) it's a replica and the dn supplied is the update_ndn.
198          */
199         if ( be->be_modify ) {
200                 /* do the update here */
201 #ifndef SLAPD_MULTIMASTER
202                 /* we don't have to check for replicator dn
203                  * because we accept each modify request
204                  */
205                 if ( be->be_update_ndn == NULL ||
206                         strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
207 #endif
208                 {
209                         if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
210                                 global_lastmod == ON)) && be->be_update_ndn == NULL )
211                         {
212                                 rc = add_modified_attrs( op, &modlist );
213
214                                 if( rc != LDAP_SUCCESS ) {
215                                         free( ndn );
216                                         modlist_free( modlist );
217                                         send_ldap_result( conn, op, rc,
218                                                 NULL, "no-user-modification attribute type",
219                                                 NULL, NULL );
220                                         return rc;
221                                 }
222                         }
223
224                         if ( (*be->be_modify)( be, conn, op, ndn, modlist ) == 0 
225 #ifdef SLAPD_MULTIMASTER
226                                 && ( be->be_update_ndn == NULL ||
227                                         strcmp( be->be_update_ndn, op->o_ndn ) != 0 )
228 #endif
229                         ) {
230                                 /* but we log only the ones not from a replicator user */
231                                 replog( be, op, ndn, modlist );
232                         }
233
234 #ifndef SLAPD_MULTIMASTER
235                 /* send a referral */
236                 } else {
237                         send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
238                                 be->be_update_refs ? be->be_update_refs : default_referral,
239                                 NULL );
240 #endif
241                 }
242         } else {
243                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
244                     NULL, "Function not implemented", NULL, NULL );
245         }
246
247 done:
248         free( ndn );
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 }