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