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