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