]> git.sur5r.net Git - openldap/blob - servers/slapd/modify.c
2d9958362b564a356bd653c967b748a9e170c643
[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 <stdio.h>
14 #include <string.h>
15 #include <time.h>
16 #include <string.h>
17 #include <sys/types.h>
18 #include <sys/socket.h>
19 #include "slap.h"
20
21 extern Backend  *select_backend();
22
23 extern char             *default_referral;
24 extern time_t           currenttime;
25 extern pthread_mutex_t  currenttime_mutex;
26 extern int              global_lastmod;
27
28 static void     modlist_free();
29 static void     add_lastmods();
30 extern char     *suffixAlias();
31
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         /* alias suffix if approp */
153         dn = suffixAlias ( dn, op, be );
154
155         /*
156          * do the modify if 1 && (2 || 3)
157          * 1) there is a modify function implemented in this backend;
158          * 2) this backend is master for what it holds;
159          * 3) it's a replica and the dn supplied is the updatedn.
160          */
161         if ( be->be_modify != NULL ) {
162                 /* do the update here */
163                 if ( be->be_updatedn == NULL ||
164                         strcasecmp( be->be_updatedn, op->o_dn ) == 0 ) {
165
166                         if ( (be->be_lastmod == ON || ( be->be_lastmod == UNDEFINED &&
167                                 global_lastmod == ON ) ) && be->be_updatedn == NULL ) {
168                                 add_lastmods( op, &mods );
169                         }
170                         if ( (*be->be_modify)( be, conn, op, odn, mods ) == 0 ) {
171                                 replog( be, LDAP_REQ_MODIFY, dn, mods, 0 );
172                         }
173
174                 /* send a referral */
175                 } else {
176                         send_ldap_result( conn, op, LDAP_PARTIAL_RESULTS, NULL,
177                             default_referral );
178                 }
179         } else {
180                 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM, NULL,
181                     "Function not implemented" );
182         }
183
184         free( dn );
185         free( odn );
186         modlist_free( mods );
187 }
188
189 static void
190 modlist_free(
191     LDAPMod     *mods
192 )
193 {
194         LDAPMod *next;
195
196         for ( ; mods != NULL; mods = next ) {
197                 next = mods->mod_next;
198                 free( mods->mod_type );
199                 if ( mods->mod_bvalues != NULL )
200                         ber_bvecfree( mods->mod_bvalues );
201                 free( mods );
202         }
203 }
204
205 static void
206 add_lastmods( Operation *op, LDAPMod **mods )
207 {
208         char            buf[22];
209         struct berval   bv;
210         struct berval   *bvals[2];
211         LDAPMod         **m;
212         LDAPMod         *tmp;
213         struct tm       *ltm;
214
215         Debug( LDAP_DEBUG_TRACE, "add_lastmods\n", 0, 0, 0 );
216
217         bvals[0] = &bv;
218         bvals[1] = NULL;
219
220         /* remove any attempts by the user to modify these attrs */
221         for ( m = mods; *m != NULL; m = &(*m)->mod_next ) {
222             if ( strcasecmp( (*m)->mod_type, "modifytimestamp" ) == 0 || 
223                                 strcasecmp( (*m)->mod_type, "modifiersname" ) == 0 ||
224                                 strcasecmp( (*m)->mod_type, "createtimestamp" ) == 0 || 
225                                 strcasecmp( (*m)->mod_type, "creatorsname" ) == 0 ) {
226
227                 Debug( LDAP_DEBUG_TRACE,
228                                         "add_lastmods: found lastmod attr: %s\n",
229                                         (*m)->mod_type, 0, 0 );
230                 tmp = *m;
231                 *m = (*m)->mod_next;
232                 free( tmp->mod_type );
233                 if ( tmp->mod_bvalues != NULL ) {
234                     ber_bvecfree( tmp->mod_bvalues );
235                 }
236                 free( tmp );
237                 if (!*m)
238                     break;
239             }
240         }
241
242         if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
243                 bv.bv_val = "NULLDN";
244                 bv.bv_len = strlen( bv.bv_val );
245         } else {
246                 bv.bv_val = op->o_dn;
247                 bv.bv_len = strlen( bv.bv_val );
248         }
249         tmp = (LDAPMod *) ch_calloc( 1, sizeof(LDAPMod) );
250         tmp->mod_type = strdup( "modifiersname" );
251         tmp->mod_op = LDAP_MOD_REPLACE;
252         tmp->mod_bvalues = (struct berval **) ch_calloc( 1,
253             2 * sizeof(struct berval *) );
254         tmp->mod_bvalues[0] = ber_bvdup( &bv );
255         tmp->mod_next = *mods;
256         *mods = tmp;
257
258         pthread_mutex_lock( &currenttime_mutex );
259 #ifndef LDAP_LOCALTIME
260         ltm = gmtime( &currenttime );
261         strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
262 #else
263         ltm = localtime( &currenttime );
264         strftime( buf, sizeof(buf), "%y%m%d%H%M%SZ", ltm );
265 #endif
266         pthread_mutex_unlock( &currenttime_mutex );
267         bv.bv_val = buf;
268         bv.bv_len = strlen( bv.bv_val );
269         tmp = (LDAPMod *) ch_calloc( 1, sizeof(LDAPMod) );
270         tmp->mod_type = strdup( "modifytimestamp" );
271         tmp->mod_op = LDAP_MOD_REPLACE;
272         tmp->mod_bvalues = (struct berval **) ch_calloc( 1, 2 * sizeof(struct berval *) );
273         tmp->mod_bvalues[0] = ber_bvdup( &bv );
274         tmp->mod_next = *mods;
275         *mods = tmp;
276 }