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