]> git.sur5r.net Git - openldap/blob - servers/slapd/modify.c
Import Hallvards idl memory plugs 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 add_lastmods( Operation *op, LDAPMod **mods );
24 static void     modlist_free(LDAPMod *mods);
25
26
27 void
28 do_modify(
29     Connection  *conn,
30     Operation   *op
31 )
32 {
33         char            *ndn;
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" /*}*/, &ndn ) == 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
67         Debug( LDAP_DEBUG_ARGS, "do_modify: dn (%s)\n", ndn, 0, 0 );
68
69         (void) dn_normalize_case( ndn );
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( ndn );
87                         free( *modtail );
88                         *modtail = NULL;
89                         modlist_free( mods );
90                         return;
91                 }
92
93                 if ( (*modtail)->mod_op != LDAP_MOD_ADD &&
94                     (*modtail)->mod_op != LDAP_MOD_DELETE &&
95                     (*modtail)->mod_op != LDAP_MOD_REPLACE )
96                 {
97                         send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
98                             "unrecognized modify operation" );
99                         free( ndn );
100                         modlist_free( mods );
101                         return;
102                 }
103
104                 if ( (*modtail)->mod_bvalues == NULL && (*modtail)->mod_op
105                   != LDAP_MOD_DELETE ) {
106                         send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, NULL,
107                             "no values given" );
108                         free( ndn );
109                         modlist_free( mods );
110                         return;
111                 }
112                 attr_normalize( (*modtail)->mod_type );
113
114                 modtail = &(*modtail)->mod_next;
115         }
116         *modtail = NULL;
117
118 #ifdef LDAP_DEBUG
119         Debug( LDAP_DEBUG_ARGS, "modifications:\n", 0, 0, 0 );
120         for ( tmp = mods; tmp != NULL; tmp = tmp->mod_next ) {
121                 Debug( LDAP_DEBUG_ARGS, "\t%s: %s\n", tmp->mod_op
122                     == LDAP_MOD_ADD ? "add" : (tmp->mod_op == LDAP_MOD_DELETE ?
123                     "delete" : "replace"), tmp->mod_type, 0 );
124         }
125 #endif
126
127         Statslog( LDAP_DEBUG_STATS, "conn=%d op=%d MOD dn=\"%s\"\n",
128             conn->c_connid, op->o_opid, ndn, 0, 0 );
129
130         /*
131          * We could be serving multiple database backends.  Select the
132          * appropriate one, or send a referral to our "referral server"
133          * if we don't hold it.
134          */
135         if ( (be = select_backend( ndn )) == NULL ) {
136                 free( ndn );
137                 modlist_free( mods );
138                 send_ldap_result( conn, op, LDAP_PARTIAL_RESULTS, NULL,
139                     default_referral );
140                 return;
141         }
142
143         /* alias suffix if approp */
144         ndn = suffixAlias ( ndn, op, be );
145
146         /*
147          * do the modify if 1 && (2 || 3)
148          * 1) there is a modify function implemented in this backend;
149          * 2) this backend is master for what it holds;
150          * 3) it's a replica and the dn supplied is the update_ndn.
151          */
152         if ( be->be_modify != NULL ) {
153                 /* do the update here */
154                 if ( be->be_update_ndn == NULL ||
155                         strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
156                 {
157                         if(( be->be_lastmod == ON || (be->be_lastmod == UNDEFINED
158                                 && global_lastmod == ON )) && be->be_update_ndn == NULL )
159                         {
160                                 add_lastmods( op, &mods );
161                         }
162
163                         if ( (*be->be_modify)( be, conn, op, ndn, mods ) == 0 ) {
164                                 replog( be, LDAP_REQ_MODIFY, ndn, mods, 0 );
165                         }
166
167                 /* send a referral */
168                 } else {
169                         send_ldap_result( conn, op, LDAP_PARTIAL_RESULTS, NULL,
170                             default_referral );
171                 }
172         } else {
173                 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM, NULL,
174                     "Function not implemented" );
175         }
176
177         free( ndn );
178         modlist_free( mods );
179 }
180
181 static void
182 add_lastmods( Operation *op, LDAPMod **mods )
183 {
184         char            buf[22];
185         struct berval   bv;
186         struct berval   *bvals[2];
187         LDAPMod         **m;
188         LDAPMod         *tmp;
189         struct tm       *ltm;
190
191         Debug( LDAP_DEBUG_TRACE, "add_lastmods\n", 0, 0, 0 );
192
193         bvals[0] = &bv;
194         bvals[1] = NULL;
195
196         /* remove any attempts by the user to modify these attrs */
197         for ( m = mods; *m != NULL; m = &(*m)->mod_next ) {
198             if ( strcasecmp( (*m)->mod_type, "modifytimestamp" ) == 0 || 
199                                 strcasecmp( (*m)->mod_type, "modifiersname" ) == 0 ||
200                                 strcasecmp( (*m)->mod_type, "createtimestamp" ) == 0 || 
201                                 strcasecmp( (*m)->mod_type, "creatorsname" ) == 0 ) {
202
203                 Debug( LDAP_DEBUG_TRACE,
204                                         "add_lastmods: found lastmod attr: %s\n",
205                                         (*m)->mod_type, 0, 0 );
206                 tmp = *m;
207                 *m = (*m)->mod_next;
208                 free( tmp->mod_type );
209                 if ( tmp->mod_bvalues != NULL ) {
210                     ber_bvecfree( tmp->mod_bvalues );
211                 }
212                 free( tmp );
213                 if (!*m)
214                     break;
215             }
216         }
217
218         if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
219                 bv.bv_val = "NULLDN";
220                 bv.bv_len = strlen( bv.bv_val );
221         } else {
222                 bv.bv_val = op->o_dn;
223                 bv.bv_len = strlen( bv.bv_val );
224         }
225         tmp = (LDAPMod *) ch_calloc( 1, sizeof(LDAPMod) );
226         tmp->mod_type = ch_strdup( "modifiersname" );
227         tmp->mod_op = LDAP_MOD_REPLACE;
228         tmp->mod_bvalues = (struct berval **) ch_calloc( 1,
229             2 * sizeof(struct berval *) );
230         tmp->mod_bvalues[0] = ber_bvdup( &bv );
231         tmp->mod_next = *mods;
232         *mods = tmp;
233
234         ldap_pvt_thread_mutex_lock( &currenttime_mutex );
235 #ifndef LDAP_LOCALTIME
236         ltm = gmtime( &currenttime );
237         strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
238 #else
239         ltm = localtime( &currenttime );
240         strftime( buf, sizeof(buf), "%y%m%d%H%M%SZ", ltm );
241 #endif
242         ldap_pvt_thread_mutex_unlock( &currenttime_mutex );
243         bv.bv_val = buf;
244         bv.bv_len = strlen( bv.bv_val );
245         tmp = (LDAPMod *) ch_calloc( 1, sizeof(LDAPMod) );
246         tmp->mod_type = ch_strdup( "modifytimestamp" );
247         tmp->mod_op = LDAP_MOD_REPLACE;
248         tmp->mod_bvalues = (struct berval **) ch_calloc( 1, 2 * sizeof(struct berval *) );
249         tmp->mod_bvalues[0] = ber_bvdup( &bv );
250         tmp->mod_next = *mods;
251         *mods = tmp;
252 }
253
254
255 static void
256 modlist_free(
257     LDAPMod     *mods
258 )
259 {
260         LDAPMod *next;
261
262         for ( ; mods != NULL; mods = next ) {
263                 next = mods->mod_next;
264                 free( mods->mod_type );
265                 if ( mods->mod_bvalues != NULL )
266                         ber_bvecfree( mods->mod_bvalues );
267                 free( mods );
268         }
269 }