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