]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/log.c
Destroy monitor_info.mi_cache_mutex
[openldap] / servers / slapd / back-monitor / log.c
1 /* log.c - deal with log subsystem */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2001-2005 The OpenLDAP Foundation.
6  * Portions Copyright 2001-2003 Pierangelo Masarati.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17 /* ACKNOWLEDGEMENTS:
18  * This work was initially developed by Pierangelo Masarati for inclusion
19  * in OpenLDAP Software.
20  */
21
22 #include "portable.h"
23
24 #include <stdio.h>
25
26 #include <ac/string.h>
27
28 #include "slap.h"
29 #include <lber_pvt.h>
30 #include "lutil.h"
31 #include "ldif.h"
32 #include "back-monitor.h"
33
34 static int
35 monitor_subsys_log_destroy(
36         BackendDB               *be,
37         monitor_subsys_t        *ms );
38
39 static int 
40 monitor_subsys_log_modify( 
41         Operation               *op,
42         SlapReply               *rs,
43         Entry                   *e );
44
45 /*
46  * log mutex
47  */
48 ldap_pvt_thread_mutex_t         monitor_log_mutex;
49
50 static struct {
51         int i;
52         struct berval s;
53         struct berval n;
54 } int_2_level[] = {
55         { LDAP_DEBUG_TRACE,     BER_BVC("Trace"),       BER_BVNULL },
56         { LDAP_DEBUG_PACKETS,   BER_BVC("Packets"),     BER_BVNULL },
57         { LDAP_DEBUG_ARGS,      BER_BVC("Args"),        BER_BVNULL },
58         { LDAP_DEBUG_CONNS,     BER_BVC("Conns"),       BER_BVNULL },
59         { LDAP_DEBUG_BER,       BER_BVC("BER"), BER_BVNULL },
60         { LDAP_DEBUG_FILTER,    BER_BVC("Filter"),      BER_BVNULL },
61         { LDAP_DEBUG_CONFIG,    BER_BVC("Config"),      BER_BVNULL },   /* useless */
62         { LDAP_DEBUG_ACL,       BER_BVC("ACL"), BER_BVNULL },
63         { LDAP_DEBUG_STATS,     BER_BVC("Stats"),       BER_BVNULL },
64         { LDAP_DEBUG_STATS2,    BER_BVC("Stats2"),      BER_BVNULL },
65         { LDAP_DEBUG_SHELL,     BER_BVC("Shell"),       BER_BVNULL },
66         { LDAP_DEBUG_PARSE,     BER_BVC("Parse"),       BER_BVNULL },
67         { LDAP_DEBUG_CACHE,     BER_BVC("Cache"),       BER_BVNULL },
68         { LDAP_DEBUG_INDEX,     BER_BVC("Index"),       BER_BVNULL },
69         { 0,                    BER_BVNULL,     BER_BVNULL }
70 };
71
72 static int loglevel2int( struct berval *l );
73 static int int2loglevel( int n );
74
75 static int add_values( Entry *e, Modification *mod, int *newlevel );
76 static int delete_values( Entry *e, Modification *mod, int *newlevel );
77 static int replace_values( Entry *e, Modification *mod, int *newlevel );
78
79 /*
80  * initializes log subentry
81  */
82 int
83 monitor_subsys_log_init(
84         BackendDB               *be,
85         monitor_subsys_t        *ms )
86 {
87         monitor_info_t  *mi;
88         Entry           *e;
89         int             i;
90
91         ms->mss_destroy = monitor_subsys_log_destroy;
92         ms->mss_modify = monitor_subsys_log_modify;
93
94         ldap_pvt_thread_mutex_init( &monitor_log_mutex );
95
96         mi = ( monitor_info_t * )be->be_private;
97
98         if ( monitor_cache_get( mi, &ms->mss_ndn, 
99                                 &e ) ) {
100                 Debug( LDAP_DEBUG_ANY,
101                         "monitor_subsys_log_init: "
102                         "unable to get entry \"%s\"\n",
103                         ms->mss_ndn.bv_val, 0, 0 );
104                 return( -1 );
105         }
106
107         /* initialize the debug level(s) */
108         for ( i = 0; int_2_level[ i ].i != 0; i++ ) {
109                 if ( mi->mi_ad_managedInfo->ad_type->sat_equality->smr_normalize ) {
110                         int     rc;
111
112                         rc = (*mi->mi_ad_managedInfo->ad_type->sat_equality->smr_normalize)(
113                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
114                                         mi->mi_ad_managedInfo->ad_type->sat_syntax,
115                                         mi->mi_ad_managedInfo->ad_type->sat_equality,
116                                         &int_2_level[ i ].s,
117                                         &int_2_level[ i ].n, NULL );
118                         if ( rc ) {
119                                 return( -1 );
120                         }
121                 }
122
123                 if ( int_2_level[ i ].i & ldap_syslog ) {
124                         attr_merge_one( e, mi->mi_ad_managedInfo,
125                                         &int_2_level[ i ].s,
126                                         &int_2_level[ i ].n );
127                 }
128         }
129
130         monitor_cache_release( mi, e );
131
132         return( 0 );
133 }
134
135 static int
136 monitor_subsys_log_destroy(
137         BackendDB               *be,
138         monitor_subsys_t        *ms )
139 {
140         int             i;
141
142         for ( i = 0; int_2_level[ i ].i != 0; i++ ) {
143                 if ( !BER_BVISNULL( &int_2_level[ i ].n ) ) {
144                         ch_free( int_2_level[ i ].n.bv_val );
145                 }
146         }
147
148         return 0;
149 }
150
151 static int 
152 monitor_subsys_log_modify( 
153         Operation               *op,
154         SlapReply               *rs,
155         Entry                   *e )
156 {
157         monitor_info_t  *mi = ( monitor_info_t * )op->o_bd->be_private;
158         int             rc = LDAP_OTHER;
159         int             newlevel = ldap_syslog;
160         Attribute       *save_attrs;
161         Modifications   *modlist = op->oq_modify.rs_modlist;
162         Modifications   *ml;
163
164         ldap_pvt_thread_mutex_lock( &monitor_log_mutex );
165
166         save_attrs = e->e_attrs;
167         e->e_attrs = attrs_dup( e->e_attrs );
168
169         for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
170                 Modification    *mod = &ml->sml_mod;
171
172                 /*
173                  * accept all operational attributes;
174                  * this includes modifersName and modifyTimestamp
175                  * if lastmod is "on"
176                  */
177                 if ( is_at_operational( mod->sm_desc->ad_type ) ) {
178                         ( void ) attr_delete( &e->e_attrs, mod->sm_desc );
179                         rc = rs->sr_err = attr_merge( e, mod->sm_desc, mod->sm_values, mod->sm_nvalues );
180                         if ( rc != LDAP_SUCCESS ) {
181                                 break;
182                         }
183                         continue;
184
185                 /*
186                  * only the "managedInfo" attribute can be modified
187                  */
188                 } else if ( mod->sm_desc != mi->mi_ad_managedInfo ) {
189                         rc = rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
190                         break;
191                 }
192
193                 switch ( mod->sm_op ) {
194                 case LDAP_MOD_ADD:
195                         rc = add_values( e, mod, &newlevel );
196                         break;
197                         
198                 case LDAP_MOD_DELETE:
199                         rc = delete_values( e, mod, &newlevel );
200                         break;
201
202                 case LDAP_MOD_REPLACE:
203                         rc = replace_values( e, mod, &newlevel );
204                         break;
205
206                 default:
207                         rc = LDAP_OTHER;
208                         break;
209                 }
210
211                 if ( rc != LDAP_SUCCESS ) {
212                         rs->sr_err = rc;
213                         break;
214                 }
215         }
216
217         /* set the new debug level */
218         if ( rc == LDAP_SUCCESS ) {
219                 const char      *text;
220                 static char     textbuf[ BACKMONITOR_BUFSIZE ];
221
222                 /* check for abandon */
223                 if ( op->o_abandon ) {
224                         rc = rs->sr_err = SLAPD_ABANDON;
225
226                         goto cleanup;
227                 }
228
229                 /* check that the entry still obeys the schema */
230                 rc = entry_schema_check( op, e, save_attrs, 0,
231                         &text, textbuf, sizeof( textbuf ) );
232                 if ( rc != LDAP_SUCCESS ) {
233                         rs->sr_err = rc;
234                         goto cleanup;
235                 }
236
237                 /*
238                  * Do we need to protect this with a mutex?
239                  */
240                 ldap_syslog = newlevel;
241
242 #if 0   /* debug rather than log */
243                 slap_debug = newlevel;
244                 lutil_set_debug_level( "slapd", slap_debug );
245                 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
246                 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
247                 ldif_debug = slap_debug;
248 #endif
249         }
250
251 cleanup:;
252         if ( rc == LDAP_SUCCESS ) {
253                 attrs_free( save_attrs );
254
255         } else {
256                 attrs_free( e->e_attrs );
257                 e->e_attrs = save_attrs;
258         }
259         
260         ldap_pvt_thread_mutex_unlock( &monitor_log_mutex );
261
262         if ( rc == LDAP_SUCCESS ) {
263                 rc = SLAP_CB_CONTINUE;
264         }
265
266         return rc;
267 }
268
269 static int
270 loglevel2int( struct berval *l )
271 {
272         int             i;
273         
274         for ( i = 0; int_2_level[ i ].i != 0; i++ ) {
275                 if ( l->bv_len != int_2_level[ i ].s.bv_len ) {
276                         continue;
277                 }
278
279                 if ( strcasecmp( l->bv_val, int_2_level[ i ].s.bv_val ) == 0 ) {
280                         return int_2_level[ i ].i;
281                 }
282         }
283
284         return 0;
285 }
286
287 static int
288 int2loglevel( int n )
289 {
290         int             i;
291         
292         for ( i = 0; int_2_level[ i ].i != 0; i++ ) {
293                 if ( int_2_level[ i ].i == n ) {
294                         return i;
295                 }
296         }
297
298         return -1;
299 }
300
301 static int
302 check_constraints( Modification *mod, int *newlevel )
303 {
304         int             i;
305
306         for ( i = 0; mod->sm_values && !BER_BVISNULL( &mod->sm_values[ i ] ); i++ ) {
307                 int             l;
308                 
309                 l = loglevel2int( &mod->sm_values[ i ] );
310                 if ( !l ) {
311                         return LDAP_CONSTRAINT_VIOLATION;
312                 }
313
314                 if ( ( l = int2loglevel( l ) ) == -1 ) {
315                         return LDAP_OTHER;
316                 }
317
318                 assert( int_2_level[ l ].s.bv_len
319                                 == mod->sm_values[ i ].bv_len );
320                 
321                 AC_MEMCPY( mod->sm_values[ i ].bv_val,
322                                 int_2_level[ l ].s.bv_val,
323                                 int_2_level[ l ].s.bv_len );
324
325                 AC_MEMCPY( mod->sm_nvalues[ i ].bv_val,
326                                 int_2_level[ l ].n.bv_val,
327                                 int_2_level[ l ].n.bv_len );
328
329                 *newlevel |= l;
330         }
331
332         return LDAP_SUCCESS;
333 }       
334
335 static int 
336 add_values( Entry *e, Modification *mod, int *newlevel )
337 {
338         Attribute       *a;
339         int             i, rc;
340         MatchingRule    *mr = mod->sm_desc->ad_type->sat_equality;
341
342         rc = check_constraints( mod, newlevel );
343         if ( rc != LDAP_SUCCESS ) {
344                 return rc;
345         }
346
347         a = attr_find( e->e_attrs, mod->sm_desc );
348
349         if ( a != NULL ) {
350                 /* "managedInfo" SHOULD have appropriate rules ... */
351                 if ( mr == NULL || !mr->smr_match ) {
352                         return LDAP_INAPPROPRIATE_MATCHING;
353                 }
354
355                 for ( i = 0; !BER_BVISNULL( &mod->sm_values[ i ] ); i++ ) {
356                         int rc;
357                         int j;
358                         const char *text = NULL;
359                         struct berval asserted;
360
361                         rc = asserted_value_validate_normalize(
362                                 mod->sm_desc, mr, SLAP_MR_EQUALITY,
363                                 &mod->sm_values[ i ], &asserted, &text, NULL );
364
365                         if ( rc != LDAP_SUCCESS ) {
366                                 return rc;
367                         }
368
369                         for ( j = 0; !BER_BVISNULL( &a->a_vals[ j ] ); j++ ) {
370                                 int match;
371                                 int rc = value_match( &match, mod->sm_desc, mr,
372                                         0, &a->a_vals[ j ], &asserted, &text );
373
374                                 if ( rc == LDAP_SUCCESS && match == 0 ) {
375                                         free( asserted.bv_val );
376                                         return LDAP_TYPE_OR_VALUE_EXISTS;
377                                 }
378                         }
379
380                         free( asserted.bv_val );
381                 }
382         }
383
384         /* no - add them */
385         rc = attr_merge( e, mod->sm_desc, mod->sm_values, mod->sm_nvalues );
386         if ( rc != LDAP_SUCCESS ) {
387                 /* this should return result of attr_mergeit */
388                 return rc;
389         }
390
391         return LDAP_SUCCESS;
392 }
393
394 static int
395 delete_values( Entry *e, Modification *mod, int *newlevel )
396 {
397         int             i, j, k, found, rc, nl = 0;
398         Attribute       *a;
399         MatchingRule    *mr = mod->sm_desc->ad_type->sat_equality;
400
401         rc = check_constraints( mod, &nl );
402         if ( rc != LDAP_SUCCESS ) {
403                 return rc;
404         }
405
406         *newlevel &= ~nl;
407
408         /* delete the entire attribute */
409         if ( mod->sm_values == NULL ) {
410                 int rc = attr_delete( &e->e_attrs, mod->sm_desc );
411
412                 if ( rc ) {
413                         rc = LDAP_NO_SUCH_ATTRIBUTE;
414                 } else {
415                         *newlevel = 0;
416                         rc = LDAP_SUCCESS;
417                 }
418                 return rc;
419         }
420
421         if ( mr == NULL || !mr->smr_match ) {
422                 /* disallow specific attributes from being deleted if
423                  * no equality rule */
424                 return LDAP_INAPPROPRIATE_MATCHING;
425         }
426
427         /* delete specific values - find the attribute first */
428         if ( (a = attr_find( e->e_attrs, mod->sm_desc )) == NULL ) {
429                 return( LDAP_NO_SUCH_ATTRIBUTE );
430         }
431
432         /* find each value to delete */
433         for ( i = 0; !BER_BVISNULL( &mod->sm_values[ i ] ); i++ ) {
434                 int rc;
435                 const char *text = NULL;
436
437                 struct berval asserted;
438
439                 rc = asserted_value_validate_normalize(
440                                 mod->sm_desc, mr, SLAP_MR_EQUALITY,
441                                 &mod->sm_values[ i ], &asserted, &text, NULL );
442
443                 if( rc != LDAP_SUCCESS ) return rc;
444
445                 found = 0;
446                 for ( j = 0; !BER_BVISNULL( &a->a_vals[ j ] ); j++ ) {
447                         int match;
448                         int rc = value_match( &match, mod->sm_desc, mr,
449                                 0,
450                                 &a->a_vals[ j ], &asserted, &text );
451
452                         if( rc == LDAP_SUCCESS && match != 0 ) {
453                                 continue;
454                         }
455
456                         /* found a matching value */
457                         found = 1;
458
459                         /* delete it */
460                         free( a->a_vals[ j ].bv_val );
461                         for ( k = j + 1; !BER_BVISNULL( &a->a_vals[ k ] ); k++ ) {
462                                 a->a_vals[ k - 1 ] = a->a_vals[ k ];
463                         }
464                         BER_BVZERO( &a->a_vals[ k - 1 ] );
465
466                         break;
467                 }
468
469                 free( asserted.bv_val );
470
471                 /* looked through them all w/o finding it */
472                 if ( ! found ) {
473                         return LDAP_NO_SUCH_ATTRIBUTE;
474                 }
475         }
476
477         /* if no values remain, delete the entire attribute */
478         if ( BER_BVISNULL( &a->a_vals[ 0 ] ) ) {
479                 /* should already be zero */
480                 *newlevel = 0;
481                 
482                 if ( attr_delete( &e->e_attrs, mod->sm_desc ) ) {
483                         return LDAP_NO_SUCH_ATTRIBUTE;
484                 }
485         }
486
487         return LDAP_SUCCESS;
488 }
489
490 static int
491 replace_values( Entry *e, Modification *mod, int *newlevel )
492 {
493         int rc;
494
495         *newlevel = 0;
496         rc = check_constraints( mod, newlevel );
497         if ( rc != LDAP_SUCCESS ) {
498                 return rc;
499         }
500
501         rc = attr_delete( &e->e_attrs, mod->sm_desc );
502
503         if ( rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_ATTRIBUTE ) {
504                 return rc;
505         }
506
507         if ( mod->sm_values != NULL ) {
508                 rc = attr_merge( e, mod->sm_desc, mod->sm_values, mod->sm_nvalues );
509                 if ( rc != LDAP_SUCCESS ) {
510                         return rc;
511                 }
512         }
513
514         return LDAP_SUCCESS;
515 }
516