]> git.sur5r.net Git - openldap/blob - servers/slapd/modify.c
Import nextid cleanup 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            *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                                 add_lastmods( op, &mods );
160                         }
161                         if ( (*be->be_modify)( be, conn, op, ndn, mods ) == 0 ) {
162                                 replog( be, LDAP_REQ_MODIFY, ndn, mods, 0 );
163                         }
164
165                 /* send a referral */
166                 } else {
167                         send_ldap_result( conn, op, LDAP_PARTIAL_RESULTS, NULL,
168                             default_referral );
169                 }
170         } else {
171                 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM, NULL,
172                     "Function not implemented" );
173         }
174
175         free( ndn );
176         modlist_free( mods );
177 }
178
179 static void
180 modlist_free(
181     LDAPMod     *mods
182 )
183 {
184         LDAPMod *next;
185
186         for ( ; mods != NULL; mods = next ) {
187                 next = mods->mod_next;
188                 free( mods->mod_type );
189                 if ( mods->mod_bvalues != NULL )
190                         ber_bvecfree( mods->mod_bvalues );
191                 free( mods );
192         }
193 }
194
195 static void
196 add_lastmods( Operation *op, LDAPMod **mods )
197 {
198         char            buf[22];
199         struct berval   bv;
200         struct berval   *bvals[2];
201         LDAPMod         **m;
202         LDAPMod         *tmp;
203         struct tm       *ltm;
204
205         Debug( LDAP_DEBUG_TRACE, "add_lastmods\n", 0, 0, 0 );
206
207         bvals[0] = &bv;
208         bvals[1] = NULL;
209
210         /* remove any attempts by the user to modify these attrs */
211         for ( m = mods; *m != NULL; m = &(*m)->mod_next ) {
212             if ( strcasecmp( (*m)->mod_type, "modifytimestamp" ) == 0 || 
213                                 strcasecmp( (*m)->mod_type, "modifiersname" ) == 0 ||
214                                 strcasecmp( (*m)->mod_type, "createtimestamp" ) == 0 || 
215                                 strcasecmp( (*m)->mod_type, "creatorsname" ) == 0 ) {
216
217                 Debug( LDAP_DEBUG_TRACE,
218                                         "add_lastmods: found lastmod attr: %s\n",
219                                         (*m)->mod_type, 0, 0 );
220                 tmp = *m;
221                 *m = (*m)->mod_next;
222                 free( tmp->mod_type );
223                 if ( tmp->mod_bvalues != NULL ) {
224                     ber_bvecfree( tmp->mod_bvalues );
225                 }
226                 free( tmp );
227                 if (!*m)
228                     break;
229             }
230         }
231
232         if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
233                 bv.bv_val = "NULLDN";
234                 bv.bv_len = strlen( bv.bv_val );
235         } else {
236                 bv.bv_val = op->o_dn;
237                 bv.bv_len = strlen( bv.bv_val );
238         }
239         tmp = (LDAPMod *) ch_calloc( 1, sizeof(LDAPMod) );
240         tmp->mod_type = ch_strdup( "modifiersname" );
241         tmp->mod_op = LDAP_MOD_REPLACE;
242         tmp->mod_bvalues = (struct berval **) ch_calloc( 1,
243             2 * sizeof(struct berval *) );
244         tmp->mod_bvalues[0] = ber_bvdup( &bv );
245         tmp->mod_next = *mods;
246         *mods = tmp;
247
248         pthread_mutex_lock( &currenttime_mutex );
249 #ifndef LDAP_LOCALTIME
250         ltm = gmtime( &currenttime );
251         strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
252 #else
253         ltm = localtime( &currenttime );
254         strftime( buf, sizeof(buf), "%y%m%d%H%M%SZ", ltm );
255 #endif
256         pthread_mutex_unlock( &currenttime_mutex );
257         bv.bv_val = buf;
258         bv.bv_len = strlen( bv.bv_val );
259         tmp = (LDAPMod *) ch_calloc( 1, sizeof(LDAPMod) );
260         tmp->mod_type = ch_strdup( "modifytimestamp" );
261         tmp->mod_op = LDAP_MOD_REPLACE;
262         tmp->mod_bvalues = (struct berval **) ch_calloc( 1, 2 * sizeof(struct berval *) );
263         tmp->mod_bvalues[0] = ber_bvdup( &bv );
264         tmp->mod_next = *mods;
265         *mods = tmp;
266 }