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