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