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