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