]> git.sur5r.net Git - openldap/blob - servers/slapd/modify.c
Apply Hallvard's modtail twice freed bug fix from -devel.
[openldap] / servers / slapd / modify.c
1 /*
2  * Copyright (c) 1995 Regents of the University of Michigan.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of Michigan at Ann Arbor. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  */
12
13 #include "portable.h"
14
15 #include <stdio.h>
16
17 #include <ac/socket.h>
18 #include <ac/string.h>
19 #include <ac/time.h>
20
21 #include "slap.h"
22
23 static void     modlist_free(LDAPMod *mods);
24 static void     add_lastmods(Operation *op, LDAPMod **mods);
25
26
27 void
28 do_modify(
29     Connection  *conn,
30     Operation   *op
31 )
32 {
33         char            *dn, *odn;
34         char            *last;
35         unsigned long   tag, len;
36         LDAPMod         *mods, *tmp;
37         LDAPMod         **modtail;
38         Backend         *be;
39
40         Debug( LDAP_DEBUG_TRACE, "do_modify\n", 0, 0, 0 );
41
42         /*
43          * Parse the modify request.  It looks like this:
44          *
45          *      ModifyRequest := [APPLICATION 6] SEQUENCE {
46          *              name    DistinguishedName,
47          *              mods    SEQUENCE OF SEQUENCE {
48          *                      operation       ENUMERATED {
49          *                              add     (0),
50          *                              delete  (1),
51          *                              replace (2)
52          *                      },
53          *                      modification    SEQUENCE {
54          *                              type    AttributeType,
55          *                              values  SET OF AttributeValue
56          *                      }
57          *              }
58          *      }
59          */
60
61         if ( ber_scanf( op->o_ber, "{a", &dn ) == LBER_ERROR ) {
62                 Debug( LDAP_DEBUG_ANY, "ber_scanf failed\n", 0, 0, 0 );
63                 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL, "" );
64                 return;
65         }
66         odn = ch_strdup( dn );
67         dn_normalize( dn );
68
69         Debug( LDAP_DEBUG_ARGS, "do_modify: dn (%s)\n", dn, 0, 0 );
70
71         /* collect modifications & save for later */
72         mods = NULL;
73         modtail = &mods;
74         for ( tag = ber_first_element( op->o_ber, &len, &last );
75             tag != LBER_DEFAULT;
76             tag = ber_next_element( op->o_ber, &len, last ) )
77         {
78                 (*modtail) = (LDAPMod *) ch_calloc( 1, sizeof(LDAPMod) );
79
80                 if ( ber_scanf( op->o_ber, "{i{a[V]}}", &(*modtail)->mod_op,
81                     &(*modtail)->mod_type, &(*modtail)->mod_bvalues )
82                     == LBER_ERROR )
83                 {
84                         send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
85                             "decoding error" );
86                         free( dn );
87                         free( odn );
88                         free( *modtail );
89                         *modtail = NULL;
90                         modlist_free( mods );
91                         return;
92                 }
93
94                 if ( (*modtail)->mod_op != LDAP_MOD_ADD &&
95                     (*modtail)->mod_op != LDAP_MOD_DELETE &&
96                     (*modtail)->mod_op != LDAP_MOD_REPLACE )
97                 {
98                         send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
99                             "unrecognized modify operation" );
100                         free( dn );
101                         free( odn );
102                         modlist_free( mods );
103                         return;
104                 }
105
106                 if ( (*modtail)->mod_bvalues == NULL && (*modtail)->mod_op
107                   != LDAP_MOD_DELETE ) {
108                         send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
109                             "no values given" );
110                         free( dn );
111                         free( odn );
112                         modlist_free( mods );
113                         return;
114                 }
115                 attr_normalize( (*modtail)->mod_type );
116
117                 modtail = &(*modtail)->mod_next;
118         }
119         *modtail = NULL;
120
121 #ifdef LDAP_DEBUG
122         Debug( LDAP_DEBUG_ARGS, "modifications:\n", 0, 0, 0 );
123         for ( tmp = mods; tmp != NULL; tmp = tmp->mod_next ) {
124                 Debug( LDAP_DEBUG_ARGS, "\t%s: %s\n", tmp->mod_op
125                     == LDAP_MOD_ADD ? "add" : (tmp->mod_op == LDAP_MOD_DELETE ?
126                     "delete" : "replace"), tmp->mod_type, 0 );
127         }
128 #endif
129
130         Statslog( LDAP_DEBUG_STATS, "conn=%d op=%d MOD dn=\"%s\"\n",
131             conn->c_connid, op->o_opid, dn, 0, 0 );
132
133         /*
134          * We could be serving multiple database backends.  Select the
135          * appropriate one, or send a referral to our "referral server"
136          * if we don't hold it.
137          */
138         if ( (be = select_backend( dn )) == NULL ) {
139                 free( dn );
140                 free( odn );
141                 modlist_free( mods );
142                 send_ldap_result( conn, op, LDAP_PARTIAL_RESULTS, NULL,
143                     default_referral );
144                 return;
145         }
146
147         /* alias suffix if approp */
148         dn = suffixAlias ( dn, op, be );
149
150         /*
151          * do the modify if 1 && (2 || 3)
152          * 1) there is a modify function implemented in this backend;
153          * 2) this backend is master for what it holds;
154          * 3) it's a replica and the dn supplied is the updatedn.
155          */
156         if ( be->be_modify != NULL ) {
157                 /* do the update here */
158                 if ( be->be_updatedn == NULL ||
159                         strcasecmp( be->be_updatedn, op->o_dn ) == 0 ) {
160
161                         if ( (be->be_lastmod == ON || ( be->be_lastmod == UNDEFINED &&
162                                 global_lastmod == ON ) ) && be->be_updatedn == NULL ) {
163                                 add_lastmods( op, &mods );
164                         }
165                         if ( (*be->be_modify)( be, conn, op, odn, mods ) == 0 ) {
166                                 replog( be, LDAP_REQ_MODIFY, dn, mods, 0 );
167                         }
168
169                 /* send a referral */
170                 } else {
171                         send_ldap_result( conn, op, LDAP_PARTIAL_RESULTS, NULL,
172                             default_referral );
173                 }
174         } else {
175                 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM, NULL,
176                     "Function not implemented" );
177         }
178
179         free( dn );
180         free( odn );
181         modlist_free( mods );
182 }
183
184 static void
185 modlist_free(
186     LDAPMod     *mods
187 )
188 {
189         LDAPMod *next;
190
191         for ( ; mods != NULL; mods = next ) {
192                 next = mods->mod_next;
193                 free( mods->mod_type );
194                 if ( mods->mod_bvalues != NULL )
195                         ber_bvecfree( mods->mod_bvalues );
196                 free( mods );
197         }
198 }
199
200 static void
201 add_lastmods( Operation *op, LDAPMod **mods )
202 {
203         char            buf[22];
204         struct berval   bv;
205         struct berval   *bvals[2];
206         LDAPMod         **m;
207         LDAPMod         *tmp;
208         struct tm       *ltm;
209
210         Debug( LDAP_DEBUG_TRACE, "add_lastmods\n", 0, 0, 0 );
211
212         bvals[0] = &bv;
213         bvals[1] = NULL;
214
215         /* remove any attempts by the user to modify these attrs */
216         for ( m = mods; *m != NULL; m = &(*m)->mod_next ) {
217             if ( strcasecmp( (*m)->mod_type, "modifytimestamp" ) == 0 || 
218                                 strcasecmp( (*m)->mod_type, "modifiersname" ) == 0 ||
219                                 strcasecmp( (*m)->mod_type, "createtimestamp" ) == 0 || 
220                                 strcasecmp( (*m)->mod_type, "creatorsname" ) == 0 ) {
221
222                 Debug( LDAP_DEBUG_TRACE,
223                                         "add_lastmods: found lastmod attr: %s\n",
224                                         (*m)->mod_type, 0, 0 );
225                 tmp = *m;
226                 *m = (*m)->mod_next;
227                 free( tmp->mod_type );
228                 if ( tmp->mod_bvalues != NULL ) {
229                     ber_bvecfree( tmp->mod_bvalues );
230                 }
231                 free( tmp );
232                 if (!*m)
233                     break;
234             }
235         }
236
237         if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
238                 bv.bv_val = "NULLDN";
239                 bv.bv_len = strlen( bv.bv_val );
240         } else {
241                 bv.bv_val = op->o_dn;
242                 bv.bv_len = strlen( bv.bv_val );
243         }
244         tmp = (LDAPMod *) ch_calloc( 1, sizeof(LDAPMod) );
245         tmp->mod_type = ch_strdup( "modifiersname" );
246         tmp->mod_op = LDAP_MOD_REPLACE;
247         tmp->mod_bvalues = (struct berval **) ch_calloc( 1,
248             2 * sizeof(struct berval *) );
249         tmp->mod_bvalues[0] = ber_bvdup( &bv );
250         tmp->mod_next = *mods;
251         *mods = tmp;
252
253         pthread_mutex_lock( &currenttime_mutex );
254 #ifndef LDAP_LOCALTIME
255         ltm = gmtime( &currenttime );
256         strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
257 #else
258         ltm = localtime( &currenttime );
259         strftime( buf, sizeof(buf), "%y%m%d%H%M%SZ", ltm );
260 #endif
261         pthread_mutex_unlock( &currenttime_mutex );
262         bv.bv_val = buf;
263         bv.bv_len = strlen( bv.bv_val );
264         tmp = (LDAPMod *) ch_calloc( 1, sizeof(LDAPMod) );
265         tmp->mod_type = ch_strdup( "modifytimestamp" );
266         tmp->mod_op = LDAP_MOD_REPLACE;
267         tmp->mod_bvalues = (struct berval **) ch_calloc( 1, 2 * sizeof(struct berval *) );
268         tmp->mod_bvalues[0] = ber_bvdup( &bv );
269         tmp->mod_next = *mods;
270         *mods = tmp;
271 }