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