]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/log.c
pass Operation instead of private info
[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 ( monitor_ad_desc->ad_type->sat_equality->smr_normalize ) {
120                         int     rc;
121
122                         rc = (*monitor_ad_desc->ad_type->sat_equality->smr_normalize)(
123                                         0,
124                                         monitor_ad_desc->ad_type->sat_syntax,
125                                         monitor_ad_desc->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, monitor_ad_desc,
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         Modifications           *modlist
150 )
151 {
152         int             rc = LDAP_OTHER;
153         int             newlevel = ldap_syslog;
154         Attribute       *save_attrs;
155         Modifications   *ml;
156
157         ldap_pvt_thread_mutex_lock( &monitor_log_mutex );
158
159         save_attrs = e->e_attrs;
160         e->e_attrs = attrs_dup( e->e_attrs );
161
162         for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
163                 Modification    *mod = &ml->sml_mod;
164
165                 /*
166                  * accept all operational attributes
167                  */
168                 if ( is_at_operational( mod->sm_desc->ad_type ) ) {
169                         ( void ) attr_delete( &e->e_attrs, mod->sm_desc );
170                         rc = attr_merge( e, mod->sm_desc, mod->sm_bvalues,
171                                         mod->sm_nvalues );
172                         if ( rc != 0 ) {
173                                 rc = LDAP_OTHER;
174                                 break;
175                         }
176                         continue;
177
178                 /*
179                  * only the monitor description attribute can be modified
180                  */
181                 } else if ( mod->sm_desc != monitor_ad_desc ) {
182                         rc = LDAP_UNWILLING_TO_PERFORM;
183                         break;
184                 }
185
186                 switch ( mod->sm_op ) {
187                 case LDAP_MOD_ADD:
188                         rc = add_values( e, mod, &newlevel );
189                         break;
190                         
191                 case LDAP_MOD_DELETE:
192                         rc = delete_values( e, mod, &newlevel );
193                         break;
194
195                 case LDAP_MOD_REPLACE:
196                         rc = replace_values( e, mod, &newlevel );
197                         break;
198
199                 default:
200                         rc = LDAP_OTHER;
201                         break;
202                 }
203
204                 if ( rc != LDAP_SUCCESS ) {
205                         break;
206                 }
207         }
208
209         /* set the new debug level */
210         if ( rc == LDAP_SUCCESS ) {
211                 const char *text;
212                 static char textbuf[1024];
213
214                 /* check for abandon */
215                 if ( op->o_abandon ) {
216                         rc = SLAPD_ABANDON;
217
218                         goto cleanup;
219                 }
220
221                 /* check that the entry still obeys the schema */
222                 rc = entry_schema_check( be_monitor, e, save_attrs, 
223                                 &text, textbuf, sizeof( textbuf ) );
224                 if ( rc != LDAP_SUCCESS ) {
225                         goto cleanup;
226                 }
227
228                 /*
229                  * Do we need to protect this with a mutex?
230                  */
231                 ldap_syslog = newlevel;
232
233 #if 0   /* debug rather than log */
234                 slap_debug = newlevel;
235                 lutil_set_debug_level( "slapd", slap_debug );
236                 ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
237                 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
238                 ldif_debug = slap_debug;
239 #endif
240         }
241
242 cleanup:;
243         if ( rc == LDAP_SUCCESS ) {
244                 attrs_free( save_attrs );
245
246         } else {
247                 attrs_free( e->e_attrs );
248                 e->e_attrs = save_attrs;
249         }
250         
251         ldap_pvt_thread_mutex_unlock( &monitor_log_mutex );
252
253         return( rc );
254 }
255
256 static int
257 loglevel2int( struct berval *l )
258 {
259         int             i;
260         
261         for ( i = 0; int_2_level[ i ].i != 0; i++ ) {
262                 if ( l->bv_len != int_2_level[ i ].s.bv_len ) {
263                         continue;
264                 }
265
266                 if ( strcasecmp( l->bv_val, int_2_level[ i ].s.bv_val ) == 0 ) {
267                         return int_2_level[ i ].i;
268                 }
269         }
270
271         return 0;
272 }
273
274 static int
275 int2loglevel( int n )
276 {
277         int             i;
278         
279         for ( i = 0; int_2_level[ i ].i != 0; i++ ) {
280                 if ( int_2_level[ i ].i == n ) {
281                         return i;
282                 }
283         }
284
285         return -1;
286 }
287
288 static int
289 check_constraints( Modification *mod, int *newlevel )
290 {
291         int             i;
292
293         for ( i = 0; mod->sm_bvalues && mod->sm_bvalues[i].bv_val != NULL; i++ ) {
294                 int             l;
295                 
296                 l = loglevel2int( &mod->sm_bvalues[i] );
297                 if ( !l ) {
298                         return LDAP_CONSTRAINT_VIOLATION;
299                 }
300
301                 if ( ( l = int2loglevel( l ) ) == -1 ) {
302                         return LDAP_OTHER;
303                 }
304
305                 assert( int_2_level[ l ].s.bv_len
306                                 == mod->sm_bvalues[i].bv_len );
307                 
308                 AC_MEMCPY( mod->sm_bvalues[i].bv_val,
309                                 int_2_level[ l ].s.bv_val,
310                                 int_2_level[ l ].s.bv_len );
311
312                 AC_MEMCPY( mod->sm_nvalues[i].bv_val,
313                                 int_2_level[ l ].n.bv_val,
314                                 int_2_level[ l ].n.bv_len );
315
316                 *newlevel |= l;
317         }
318
319         return LDAP_SUCCESS;
320 }       
321
322 static int 
323 add_values( Entry *e, Modification *mod, int *newlevel )
324 {
325         Attribute       *a;
326         int             i, rc;
327         MatchingRule    *mr = mod->sm_desc->ad_type->sat_equality;
328
329         rc = check_constraints( mod, newlevel );
330         if ( rc != LDAP_SUCCESS ) {
331                 return rc;
332         }
333
334         a = attr_find( e->e_attrs, mod->sm_desc );
335
336         if ( a != NULL ) {
337                 /* "description" SHOULD have appropriate rules ... */
338                 if ( mr == NULL || !mr->smr_match ) {
339                         return LDAP_INAPPROPRIATE_MATCHING;
340                 }
341
342                 for ( i = 0; mod->sm_bvalues[i].bv_val != NULL; i++ ) {
343                         int rc;
344                         int j;
345                         const char *text = NULL;
346                         struct berval asserted;
347
348                         rc = asserted_value_validate_normalize(
349                                 mod->sm_desc, mr, SLAP_MR_EQUALITY,
350                                 &mod->sm_bvalues[i], &asserted, &text, NULL );
351
352                         if ( rc != LDAP_SUCCESS ) {
353                                 return rc;
354                         }
355
356                         for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) {
357                                 int match;
358                                 int rc = value_match( &match, mod->sm_desc, mr,
359                                         0,
360                                         &a->a_vals[j], &asserted, &text );
361
362                                 if ( rc == LDAP_SUCCESS && match == 0 ) {
363                                         free( asserted.bv_val );
364                                         return LDAP_TYPE_OR_VALUE_EXISTS;
365                                 }
366                         }
367
368                         free( asserted.bv_val );
369                 }
370         }
371
372         /* no - add them */
373         rc = attr_merge( e, mod->sm_desc, mod->sm_bvalues, mod->sm_nvalues );
374         if ( rc != LDAP_SUCCESS ) {
375                 /* this should return result of attr_mergeit */
376                 return rc;
377         }
378
379         return LDAP_SUCCESS;
380 }
381
382 static int
383 delete_values( Entry *e, Modification *mod, int *newlevel )
384 {
385         int             i, j, k, found, rc, nl = 0;
386         Attribute       *a;
387         MatchingRule    *mr = mod->sm_desc->ad_type->sat_equality;
388
389         rc = check_constraints( mod, &nl );
390         if ( rc != LDAP_SUCCESS ) {
391                 return rc;
392         }
393
394         *newlevel &= ~nl;
395
396         /* delete the entire attribute */
397         if ( mod->sm_bvalues == NULL ) {
398                 int rc = attr_delete( &e->e_attrs, mod->sm_desc );
399
400                 if ( rc ) {
401                         rc = LDAP_NO_SUCH_ATTRIBUTE;
402                 } else {
403                         *newlevel = 0;
404                         rc = LDAP_SUCCESS;
405                 }
406                 return rc;
407         }
408
409         if ( mr == NULL || !mr->smr_match ) {
410                 /* disallow specific attributes from being deleted if
411                  * no equality rule */
412                 return LDAP_INAPPROPRIATE_MATCHING;
413         }
414
415         /* delete specific values - find the attribute first */
416         if ( (a = attr_find( e->e_attrs, mod->sm_desc )) == NULL ) {
417                 return( LDAP_NO_SUCH_ATTRIBUTE );
418         }
419
420         /* find each value to delete */
421         for ( i = 0; mod->sm_bvalues[i].bv_val != NULL; i++ ) {
422                 int rc;
423                 const char *text = NULL;
424
425                 struct berval asserted;
426
427                 rc = asserted_value_validate_normalize(
428                                 mod->sm_desc, mr, SLAP_MR_EQUALITY,
429                                 &mod->sm_bvalues[i], &asserted, &text, NULL );
430
431                 if( rc != LDAP_SUCCESS ) return rc;
432
433                 found = 0;
434                 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) {
435                         int match;
436                         int rc = value_match( &match, mod->sm_desc, mr,
437                                 0,
438                                 &a->a_vals[j], &asserted, &text );
439
440                         if( rc == LDAP_SUCCESS && match != 0 ) {
441                                 continue;
442                         }
443
444                         /* found a matching value */
445                         found = 1;
446
447                         /* delete it */
448                         free( a->a_vals[j].bv_val );
449                         for ( k = j + 1; a->a_vals[k].bv_val != NULL; k++ ) {
450                                 a->a_vals[k - 1] = a->a_vals[k];
451                         }
452                         a->a_vals[k - 1].bv_val = NULL;
453
454                         break;
455                 }
456
457                 free( asserted.bv_val );
458
459                 /* looked through them all w/o finding it */
460                 if ( ! found ) {
461                         return LDAP_NO_SUCH_ATTRIBUTE;
462                 }
463         }
464
465         /* if no values remain, delete the entire attribute */
466         if ( a->a_vals[0].bv_val == NULL ) {
467                 /* should already be zero */
468                 *newlevel = 0;
469                 
470                 if ( attr_delete( &e->e_attrs, mod->sm_desc ) ) {
471                         return LDAP_NO_SUCH_ATTRIBUTE;
472                 }
473         }
474
475         return LDAP_SUCCESS;
476 }
477
478 static int
479 replace_values( Entry *e, Modification *mod, int *newlevel )
480 {
481         int rc;
482
483         *newlevel = 0;
484         rc = check_constraints( mod, newlevel );
485         if ( rc != LDAP_SUCCESS ) {
486                 return rc;
487         }
488
489         rc = attr_delete( &e->e_attrs, mod->sm_desc );
490
491         if ( rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_ATTRIBUTE ) {
492                 return rc;
493         }
494
495         if ( mod->sm_bvalues != NULL ) {
496                 rc = attr_merge( e, mod->sm_desc, mod->sm_bvalues,
497                        mod->sm_nvalues );
498                 if ( rc != LDAP_SUCCESS ) {
499                         return rc;
500                 }
501         }
502
503         return LDAP_SUCCESS;
504 }
505