]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/modify.c
Add sasl "mech" argument to backend bind routines.
[openldap] / servers / slapd / back-ldbm / modify.c
1 /* modify.c - ldbm backend modify routine */
2
3 #include "portable.h"
4
5 #include <stdio.h>
6
7 #include <ac/string.h>
8 #include <ac/socket.h>
9 #include <ac/time.h>
10
11 #include "slap.h"
12 #include "back-ldbm.h"
13 #include "proto-back-ldbm.h"
14
15 static void     add_lastmods(Operation *op, LDAPModList **ml);
16
17
18 static void
19 add_lastmods( Operation *op, LDAPModList **modlist )
20 {
21         char            buf[22];
22         struct berval   bv;
23         struct berval   *bvals[2];
24         LDAPModList             **m;
25         LDAPModList             *tmp;
26         struct tm       *ltm;
27         time_t          currenttime;
28
29         Debug( LDAP_DEBUG_TRACE, "add_lastmods\n", 0, 0, 0 );
30
31         bvals[0] = &bv;
32         bvals[1] = NULL;
33
34         /* remove any attempts by the user to modify these attrs */
35         for ( m = modlist; *m != NULL; m = &(*m)->ml_next ) {
36             if ( strcasecmp( (*m)->ml_type, "modifytimestamp" ) == 0 || 
37                                 strcasecmp( (*m)->ml_type, "modifiersname" ) == 0 ||
38                                 strcasecmp( (*m)->ml_type, "createtimestamp" ) == 0 || 
39                                 strcasecmp( (*m)->ml_type, "creatorsname" ) == 0 ) {
40
41                 Debug( LDAP_DEBUG_TRACE,
42                                         "add_lastmods: found lastmod attr: %s\n",
43                                         (*m)->ml_type, 0, 0 );
44                 tmp = *m;
45                 *m = (*m)->ml_next;
46                 free( tmp->ml_type );
47                 if ( tmp->ml_bvalues != NULL ) {
48                     ber_bvecfree( tmp->ml_bvalues );
49                 }
50                 free( tmp );
51                 if (!*m)
52                     break;
53             }
54         }
55
56         if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
57                 bv.bv_val = "NULLDN";
58                 bv.bv_len = strlen( bv.bv_val );
59         } else {
60                 bv.bv_val = op->o_dn;
61                 bv.bv_len = strlen( bv.bv_val );
62         }
63         tmp = (LDAPModList *) ch_calloc( 1, sizeof(LDAPModList) );
64         tmp->ml_type = ch_strdup( "modifiersname" );
65         tmp->ml_op = LDAP_MOD_REPLACE;
66         tmp->ml_bvalues = (struct berval **) ch_calloc(2, sizeof(struct berval *));
67         tmp->ml_bvalues[0] = ber_bvdup( &bv );
68         tmp->ml_next = *modlist;
69         *modlist = tmp;
70
71         currenttime = slap_get_time();
72         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
73 #ifndef LDAP_LOCALTIME
74         ltm = gmtime( &currenttime );
75         strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
76 #else
77         ltm = localtime( &currenttime );
78         strftime( buf, sizeof(buf), "%y%m%d%H%M%SZ", ltm );
79 #endif
80         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
81
82         bv.bv_val = buf;
83         bv.bv_len = strlen( bv.bv_val );
84         tmp = (LDAPModList *) ch_calloc( 1, sizeof(LDAPModList) );
85         tmp->ml_type = ch_strdup( "modifytimestamp" );
86         tmp->ml_op = LDAP_MOD_REPLACE;
87         tmp->ml_bvalues = (struct berval **) ch_calloc(2, sizeof(struct berval *));
88         tmp->ml_bvalues[0] = ber_bvdup( &bv );
89         tmp->ml_next = *modlist;
90         *modlist = tmp;
91
92 }
93
94 /* We need this function because of LDAP modrdn. If we do not 
95  * add this there would be a bunch of code replication here 
96  * and there and of course the likelihood of bugs increases.
97  * Juan C. Gomez (gomez@engr.sgi.com) 05/18/99
98  */ 
99
100 int ldbm_modify_internal(
101     Backend     *be,
102     Connection  *conn,
103     Operation   *op,
104     char        *dn,
105     LDAPModList *modlist,
106     Entry       *e 
107 )
108 {
109         int err;
110         LDAPMod         *mod;
111         LDAPModList     *ml;
112         Attribute       *a;
113
114         if ( ((be->be_lastmod == ON)
115               || ((be->be_lastmod == UNDEFINED)&&(global_lastmod == ON)))
116              && (be->be_update_ndn == NULL)) {
117
118                 /* XXX: It may be wrong, it changes mod time even if 
119                  * mod fails!
120                  */
121                 add_lastmods( op, &modlist );
122
123         }
124
125
126         if ( (err = acl_check_modlist( be, conn, op, e, modlist ))
127              != LDAP_SUCCESS ) {
128                 send_ldap_result( conn, op, err, NULL, NULL );
129                 return -1;
130         }
131
132         for ( ml = modlist; ml != NULL; ml = ml->ml_next ) {
133
134                 mod = &ml->ml_mod;
135
136                 switch ( mod->mod_op & ~LDAP_MOD_BVALUES ) {
137                 case LDAP_MOD_ADD:
138                         err = add_values( e, mod, op->o_ndn );
139                         break;
140
141                 case LDAP_MOD_DELETE:
142                         err = delete_values( e, mod, op->o_ndn );
143                         break;
144
145                 case LDAP_MOD_REPLACE:
146                         /* Need to remove all values from indexes before they
147                          * are lost.
148                          */
149                         if( e->e_attrs
150                             && ((a = attr_find( e->e_attrs, mod->mod_type ))
151                            != NULL) ) {
152
153                             (void) index_change_values( be,
154                                                         mod->mod_type,
155                                                         a->a_vals,
156                                                         e->e_id,
157                                                         __INDEX_DELETE_OP);
158                         }
159
160                         err = replace_values( e, mod, op->o_ndn );
161                         break;
162
163                 case LDAP_MOD_SOFTADD:
164                         /* Avoid problems in index_add_mods()
165                          * We need to add index if necessary.
166                          */
167                         mod->mod_op = LDAP_MOD_ADD;
168                         if ( (err = add_values( e, mod, op->o_ndn ))
169                                 ==  LDAP_TYPE_OR_VALUE_EXISTS ) {
170  
171                                 err = LDAP_SUCCESS;
172                                 mod->mod_op = LDAP_MOD_SOFTADD;
173  
174                         }
175                         break;
176                 }
177
178                 if ( err != LDAP_SUCCESS ) {
179                         /* unlock entry, delete from cache */
180                         send_ldap_result( conn, op, err, NULL, NULL );
181                         return -1;
182                 }
183         }
184
185         /* check that the entry still obeys the schema */
186         if ( global_schemacheck && oc_schema_check( e ) != 0 ) {
187                 Debug( LDAP_DEBUG_ANY, "entry failed schema check\n", 0, 0, 0 );
188                 send_ldap_result( conn, op, LDAP_OBJECT_CLASS_VIOLATION, NULL, NULL );
189                 return -1;
190         }
191
192         /* check for abandon */
193         ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
194         if ( op->o_abandon ) {
195                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
196                 return -1;
197         }
198         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
199
200         /* modify indexes */
201         if ( index_add_mods( be, modlist, e->e_id ) != 0 ) {
202                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL, NULL );
203                 return -1;
204         }
205
206         /* check for abandon */
207         ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
208         if ( op->o_abandon ) {
209                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
210                 return -1;
211         }
212         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
213
214         return 0;
215
216 }/* int ldbm_modify_internal() */
217
218
219 int
220 ldbm_back_modify(
221     Backend     *be,
222     Connection  *conn,
223     Operation   *op,
224     char        *dn,
225     LDAPModList *modlist
226 )
227 {
228         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
229         char            *matched;
230         Entry           *e;
231
232         Debug(LDAP_DEBUG_ARGS, "ldbm_back_modify:\n", 0, 0, 0);
233
234         /* acquire and lock entry */
235         if ( (e = dn2entry_w( be, dn, &matched )) == NULL ) {
236                 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT, matched,
237                     NULL );
238                 if ( matched != NULL ) {
239                         free( matched );
240                 }
241                 return( -1 );
242         }
243
244         /* Modify the entry */
245         if ( ldbm_modify_internal( be, conn, op, dn, modlist, e ) != 0 ) {
246
247                 goto error_return;
248
249         }
250
251         /* change the entry itself */
252         if ( id2entry_add( be, e ) != 0 ) {
253                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL, NULL );
254                 goto error_return;
255         }
256
257         send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
258         cache_return_entry_w( &li->li_cache, e );
259         return( 0 );
260
261 error_return:;
262         cache_return_entry_w( &li->li_cache, e );
263         return( -1 );
264 }
265
266 int
267 add_values(
268     Entry       *e,
269     LDAPMod     *mod,
270     char        *dn
271 )
272 {
273         int             i;
274         Attribute       *a;
275
276         /* check if the values we're adding already exist */
277         if ( (a = attr_find( e->e_attrs, mod->mod_type )) != NULL ) {
278                 for ( i = 0; mod->mod_bvalues[i] != NULL; i++ ) {
279                         if ( value_find( a->a_vals, mod->mod_bvalues[i],
280                             a->a_syntax, 3 ) == 0 ) {
281                                 return( LDAP_TYPE_OR_VALUE_EXISTS );
282                         }
283                 }
284         }
285
286         /* no - add them */
287         if( attr_merge( e, mod->mod_type, mod->mod_bvalues ) != 0 ) {
288                 return( LDAP_CONSTRAINT_VIOLATION );
289         }
290
291         return( LDAP_SUCCESS );
292 }
293
294 int
295 delete_values(
296     Entry       *e,
297     LDAPMod     *mod,
298     char        *dn
299 )
300 {
301         int             i, j, k, found;
302         Attribute       *a;
303
304         /* delete the entire attribute */
305         if ( mod->mod_bvalues == NULL ) {
306                 Debug( LDAP_DEBUG_ARGS, "removing entire attribute %s\n",
307                     mod->mod_type, 0, 0 );
308                 return( attr_delete( &e->e_attrs, mod->mod_type ) ?
309                     LDAP_NO_SUCH_ATTRIBUTE : LDAP_SUCCESS );
310         }
311
312         /* delete specific values - find the attribute first */
313         if ( (a = attr_find( e->e_attrs, mod->mod_type )) == NULL ) {
314                 Debug( LDAP_DEBUG_ARGS, "could not find attribute %s\n",
315                     mod->mod_type, 0, 0 );
316                 return( LDAP_NO_SUCH_ATTRIBUTE );
317         }
318
319         /* find each value to delete */
320         for ( i = 0; mod->mod_bvalues[i] != NULL; i++ ) {
321                 found = 0;
322                 for ( j = 0; a->a_vals[j] != NULL; j++ ) {
323                         if ( value_cmp( mod->mod_bvalues[i], a->a_vals[j],
324                             a->a_syntax, 3 ) != 0 ) {
325                                 continue;
326                         }
327                         found = 1;
328
329                         /* found a matching value - delete it */
330                         ber_bvfree( a->a_vals[j] );
331                         for ( k = j + 1; a->a_vals[k] != NULL; k++ ) {
332                                 a->a_vals[k - 1] = a->a_vals[k];
333                         }
334                         a->a_vals[k - 1] = NULL;
335                         break;
336                 }
337
338                 /* looked through them all w/o finding it */
339                 if ( ! found ) {
340                         Debug( LDAP_DEBUG_ARGS,
341                             "could not find value for attr %s\n",
342                             mod->mod_type, 0, 0 );
343                         return( LDAP_NO_SUCH_ATTRIBUTE );
344                 }
345         }
346
347         return( LDAP_SUCCESS );
348 }
349
350 int
351 replace_values(
352     Entry       *e,
353     LDAPMod     *mod,
354     char        *dn
355 )
356 {
357
358         /* XXX: BEFORE YOU GET RID OF PREVIOUS VALUES REMOVE FROM INDEX
359          * FILES
360          */
361
362         (void) attr_delete( &e->e_attrs, mod->mod_type );
363
364         if ( attr_merge( e, mod->mod_type, mod->mod_bvalues ) != 0 ) {
365                 return( LDAP_CONSTRAINT_VIOLATION );
366         }
367
368         return( LDAP_SUCCESS );
369 }