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