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