]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/init.c
Memory cleanup
[openldap] / servers / slapd / back-monitor / init.c
1 /* init.c - initialize monitor backend */
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 #include <ac/string.h>
26
27 #include <lutil.h>
28 #include "slap.h"
29 #include "lber_pvt.h"
30 #include "back-monitor.h"
31
32 #undef INTEGRATE_CORE_SCHEMA
33
34 /*
35  * used by many functions to add description to entries
36  *
37  * WARNING: be_monitor may change as new databases are added,
38  * so it should not be used outside monitor_back_db_init()
39  * until monitor_back_db_open is called.
40  */
41 BackendDB                       *be_monitor;
42
43 static struct monitor_subsys_t  **monitor_subsys;
44 static int                      monitor_subsys_opened;
45 static monitor_info_t           monitor_info;
46
47 /*
48  * subsystem data
49  *
50  * the known subsystems are added to the subsystems
51  * array at backend initialization; other subsystems
52  * may be added by calling monitor_back_register_subsys()
53  * before the database is opened (e.g. by other backends
54  * or by overlays or modules).
55  */
56 static struct monitor_subsys_t known_monitor_subsys[] = {
57         { 
58                 SLAPD_MONITOR_BACKEND_NAME, 
59                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
60                 { BER_BVC( "This subsystem contains information about available backends." ),
61                         BER_BVNULL },
62                 MONITOR_F_PERSISTENT_CH,
63                 monitor_subsys_backend_init,
64                 NULL,   /* update */
65                 NULL,   /* create */
66                 NULL    /* modify */
67         }, { 
68                 SLAPD_MONITOR_CONN_NAME,
69                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
70                 { BER_BVC( "This subsystem contains information about connections." ),
71                         BER_BVNULL },
72                 MONITOR_F_VOLATILE_CH,
73                 monitor_subsys_conn_init,
74                 monitor_subsys_conn_update,
75                 monitor_subsys_conn_create,
76                 NULL    /* modify */
77         }, { 
78                 SLAPD_MONITOR_DATABASE_NAME,    
79                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
80                 { BER_BVC( "This subsystem contains information about configured databases." ),
81                         BER_BVNULL },
82                 MONITOR_F_PERSISTENT_CH,
83                 monitor_subsys_database_init,
84                 NULL,   /* update */
85                 NULL,   /* create */
86                 monitor_subsys_database_modify
87         }, { 
88                 SLAPD_MONITOR_LISTENER_NAME,    
89                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
90                 { BER_BVC( "This subsystem contains information about active listeners." ),
91                         BER_BVNULL },
92                 MONITOR_F_PERSISTENT_CH,
93                 monitor_subsys_listener_init,
94                 NULL,   /* update */
95                 NULL,   /* create */
96                 NULL    /* modify */
97         }, { 
98                 SLAPD_MONITOR_LOG_NAME,
99                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
100                 { BER_BVC( "This subsystem contains information about logging." ),
101                         BER_BVC( "Set the attribute \"managedInfo\" to the desired log levels." ),
102                         BER_BVNULL },
103                 MONITOR_F_NONE,
104                 monitor_subsys_log_init,
105                 NULL,   /* update */
106                 NULL,   /* create */
107                 monitor_subsys_log_modify
108         }, { 
109                 SLAPD_MONITOR_OPS_NAME,
110                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
111                 { BER_BVC( "This subsystem contains information about performed operations." ),
112                         BER_BVNULL },
113                 MONITOR_F_PERSISTENT_CH,
114                 monitor_subsys_ops_init,
115                 monitor_subsys_ops_update,
116                 NULL,   /* create */
117                 NULL,   /* modify */
118         }, { 
119                 SLAPD_MONITOR_OVERLAY_NAME,
120                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
121                 { BER_BVC( "This subsystem contains information about available overlays." ),
122                         BER_BVNULL },
123                 MONITOR_F_PERSISTENT_CH,
124                 monitor_subsys_overlay_init,
125                 NULL,   /* update */
126                 NULL,   /* create */
127                 NULL,   /* modify */
128         }, { 
129                 SLAPD_MONITOR_SASL_NAME,        
130                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
131                 { BER_BVC( "This subsystem contains information about SASL." ),
132                         BER_BVNULL },
133                 MONITOR_F_NONE,
134                 NULL,   /* init */
135                 NULL,   /* update */
136                 NULL,   /* create */
137                 NULL    /* modify */
138         }, { 
139                 SLAPD_MONITOR_SENT_NAME,
140                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
141                 { BER_BVC( "This subsystem contains statistics." ),
142                         BER_BVNULL },
143                 MONITOR_F_PERSISTENT_CH,
144                 monitor_subsys_sent_init,
145                 monitor_subsys_sent_update,
146                 NULL,   /* create */
147                 NULL,   /* modify */
148         }, { 
149                 SLAPD_MONITOR_THREAD_NAME,      
150                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
151                 { BER_BVC( "This subsystem contains information about threads." ),
152                         BER_BVNULL },
153                 MONITOR_F_PERSISTENT_CH,
154                 monitor_subsys_thread_init,
155                 monitor_subsys_thread_update,
156                 NULL,   /* create */
157                 NULL    /* modify */
158         }, { 
159                 SLAPD_MONITOR_TIME_NAME,
160                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
161                 { BER_BVC( "This subsystem contains information about time." ),
162                         BER_BVNULL },
163                 MONITOR_F_PERSISTENT_CH,
164                 monitor_subsys_time_init,
165                 monitor_subsys_time_update,
166                 NULL,   /* create */
167                 NULL,   /* modify */
168         }, { 
169                 SLAPD_MONITOR_TLS_NAME,
170                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
171                 { BER_BVC( "This subsystem contains information about TLS." ),
172                         BER_BVNULL },
173                 MONITOR_F_NONE,
174                 NULL,   /* init */
175                 NULL,   /* update */
176                 NULL,   /* create */
177                 NULL    /* modify */
178         }, { 
179                 SLAPD_MONITOR_RWW_NAME,
180                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
181                 { BER_BVC( "This subsystem contains information about read/write waiters." ),
182                         BER_BVNULL },
183                 MONITOR_F_PERSISTENT_CH,
184                 monitor_subsys_rww_init,
185                 monitor_subsys_rww_update,
186                 NULL,   /* create */
187                 NULL    /* modify */
188         }, { NULL }
189 };
190
191 int
192 monitor_back_register_subsys(
193         monitor_subsys_t        *ms )
194 {
195         int     i = 0;
196
197         if ( monitor_subsys ) {
198                 for ( ; monitor_subsys[ i ] != NULL; i++ )
199                         /* just count'em */ ;
200         }
201
202         monitor_subsys = ch_realloc( monitor_subsys,
203                         ( 2 + i ) * sizeof( monitor_subsys_t * ) );
204
205         if ( monitor_subsys == NULL ) {
206                 return -1;
207         }
208
209         monitor_subsys[ i ] = ms;
210         monitor_subsys[ i + 1 ] = NULL;
211
212         /* if a subsystem is registered __AFTER__ subsystem 
213          * initialization (depending on the sequence the databases
214          * are listed in slapd.conf), init it */
215         if ( monitor_subsys_opened ) {
216
217                 /* FIXME: this should only be possible
218                  * if be_monitor is already initialized */
219                 assert( be_monitor != NULL );
220
221                 if ( ms->mss_open && ( *ms->mss_open )( be_monitor, ms ) ) {
222                         return -1;
223                 }
224
225                 ms->mss_flags |= MONITOR_F_OPENED;
226         }
227
228         return 0;
229 }
230
231 enum {
232         LIMBO_ENTRY,
233         LIMBO_ENTRY_PARENT,
234         LIMBO_ATTRS,
235         LIMBO_CB
236 };
237
238 typedef struct entry_limbo_t {
239         int                     el_type;
240         Entry                   *el_e;
241         Attribute               *el_a;
242         struct berval           el_ndn;
243         struct berval           el_base;
244         int                     el_scope;
245         struct berval           el_filter;
246         monitor_callback_t      *el_cb;
247         struct entry_limbo_t    *el_next;
248 } entry_limbo_t;
249
250 int
251 monitor_back_register_entry(
252         Entry                   *e,
253         monitor_callback_t      *cb )
254 {
255         monitor_info_t  *mi = ( monitor_info_t * )be_monitor->be_private;
256
257         assert( mi != NULL );
258         assert( e != NULL );
259         assert( e->e_private == NULL );
260         
261         if ( monitor_subsys_opened ) {
262                 Entry           *e_parent = NULL,
263                                 *e_new = NULL,
264                                 **ep = NULL;
265                 struct berval   pdn = BER_BVNULL;
266                 monitor_entry_t *mp = NULL,
267                                 *mp_parent = NULL;
268                 int             rc = 0;
269
270                 if ( monitor_cache_get( mi, &e->e_nname, &e_parent ) == 0 ) {
271                         /* entry exists */
272                         Debug( LDAP_DEBUG_ANY,
273                                 "monitor_back_register_entry(\"%s\"): "
274                                 "entry exists\n",
275                                 e->e_name.bv_val, 0, 0 );
276                         monitor_cache_release( mi, e_parent );
277                         return -1;
278                 }
279
280                 dnParent( &e->e_nname, &pdn );
281                 if ( monitor_cache_get( mi, &pdn, &e_parent ) != 0 ) {
282                         /* parent does not exist */
283                         Debug( LDAP_DEBUG_ANY,
284                                 "monitor_back_register_entry(\"%s\"): "
285                                 "parent \"%s\" not found\n",
286                                 e->e_name.bv_val, pdn.bv_val, 0 );
287                         return -1;
288                 }
289
290                 assert( e_parent->e_private != NULL );
291                 mp_parent = ( monitor_entry_t * )e_parent->e_private;
292
293                 if ( mp_parent->mp_flags & MONITOR_F_VOLATILE ) {
294                         /* entry is volatile; cannot append children */
295                         Debug( LDAP_DEBUG_ANY,
296                                 "monitor_back_register_entry(\"%s\"): "
297                                 "parent \"%s\" is volatile\n",
298                                 e->e_name.bv_val, e_parent->e_name.bv_val, 0 );
299                         rc = -1;
300                         goto done;
301                 }
302
303                 mp = monitor_entrypriv_create();
304                 if ( mp == NULL ) {
305                         Debug( LDAP_DEBUG_ANY,
306                                 "monitor_back_register_entry(\"%s\"): "
307                                 "monitor_entrypriv_create() failed\n",
308                                 e->e_name.bv_val, 0, 0 );
309                         rc = -1;
310                         goto done;
311                 }
312
313                 e_new = entry_dup( e );
314                 if ( e_new == NULL ) {
315                         Debug( LDAP_DEBUG_ANY,
316                                 "monitor_back_register_entry(\"%s\"): "
317                                 "entry_dup() failed\n",
318                                 e->e_name.bv_val, 0, 0 );
319                         rc = -1;
320                         goto done;
321                 }
322                 
323                 e_new->e_private = ( void * )mp;
324                 mp->mp_info = mp_parent->mp_info;
325                 mp->mp_flags = mp_parent->mp_flags | MONITOR_F_SUB;
326                 mp->mp_cb = cb;
327
328                 ep = &mp_parent->mp_children;
329                 for ( ; *ep; ) {
330                         mp_parent = ( monitor_entry_t * )(*ep)->e_private;
331                         ep = &mp_parent->mp_next;
332                 }
333                 *ep = e_new;
334
335                 if ( monitor_cache_add( mi, e_new ) ) {
336                         Debug( LDAP_DEBUG_ANY,
337                                 "monitor_back_register_entry(\"%s\"): "
338                                 "unable to add entry\n",
339                                 e->e_name.bv_val, 0, 0 );
340                         rc = -1;
341                         goto done;
342                 }
343
344 done:;
345                 if ( rc ) {
346                         if ( mp ) {
347                                 ch_free( mp );
348                         }
349                         if ( e_new ) {
350                                 e_new->e_private = NULL;
351                                 entry_free( e_new );
352                         }
353                 }
354
355                 if ( e_parent ) {
356                         monitor_cache_release( mi, e_parent );
357                 }
358
359         } else {
360                 entry_limbo_t   **elpp, el = { 0 };
361
362                 el.el_type = LIMBO_ENTRY;
363
364                 el.el_e = entry_dup( e );
365                 if ( el.el_e == NULL ) {
366                         Debug( LDAP_DEBUG_ANY,
367                                 "monitor_back_register_entry(\"%s\"): "
368                                 "entry_dup() failed\n",
369                                 e->e_name.bv_val, 0, 0 );
370                         return -1;
371                 }
372                 
373                 el.el_cb = cb;
374
375                 for ( elpp = (entry_limbo_t **)&mi->mi_entry_limbo;
376                                 *elpp;
377                                 elpp = &(*elpp)->el_next )
378                         /* go to last */;
379
380                 *elpp = (entry_limbo_t *)ch_malloc( sizeof( entry_limbo_t ) );
381                 if ( *elpp == NULL ) {
382                         el.el_e->e_private = NULL;
383                         entry_free( el.el_e );
384                         return -1;
385                 }
386
387                 el.el_next = NULL;
388                 **elpp = el;
389         }
390
391         return 0;
392 }
393
394 int
395 monitor_back_register_entry_parent(
396         Entry                   *e,
397         monitor_callback_t      *cb,
398         struct berval           *base,
399         int                     scope,
400         struct berval           *filter )
401 {
402         monitor_info_t  *mi = ( monitor_info_t * )be_monitor->be_private;
403         struct berval   ndn = BER_BVNULL;
404
405         assert( mi != NULL );
406         assert( e != NULL );
407         assert( e->e_private == NULL );
408
409         if ( BER_BVISNULL( filter ) ) {
410                 /* need a filter */
411                 Debug( LDAP_DEBUG_ANY,
412                         "monitor_back_register_entry_parent(\"\"): "
413                         "need a valid filter\n",
414                         0, 0, 0 );
415                 return -1;
416         }
417
418         if ( monitor_subsys_opened ) {
419                 Entry           *e_parent = NULL,
420                                 *e_new = NULL,
421                                 **ep = NULL;
422                 struct berval   e_name = BER_BVNULL,
423                                 e_nname = BER_BVNULL;
424                 monitor_entry_t *mp = NULL,
425                                 *mp_parent = NULL;
426                 int             rc = 0;
427
428                 if ( monitor_filter2ndn( base, scope, filter, &ndn ) ) {
429                         /* entry does not exist */
430                         Debug( LDAP_DEBUG_ANY,
431                                 "monitor_back_register_entry_parent(\"\"): "
432                                 "base=%s scope=%d filter=%s : "
433                                 "unable to find entry\n",
434                                 base->bv_val ? base->bv_val : "\"\"",
435                                 scope, filter->bv_val );
436                         return -1;
437                 }
438
439                 if ( monitor_cache_get( mi, &ndn, &e_parent ) != 0 ) {
440                         /* entry does not exist */
441                         Debug( LDAP_DEBUG_ANY,
442                                 "monitor_back_register_entry_parent(\"%s\"): "
443                                 "parent entry does not exist\n",
444                                 ndn.bv_val, 0, 0 );
445                         rc = -1;
446                         goto done;
447                 }
448
449                 assert( e_parent->e_private != NULL );
450                 mp_parent = ( monitor_entry_t * )e_parent->e_private;
451
452                 if ( mp_parent->mp_flags & MONITOR_F_VOLATILE ) {
453                         /* entry is volatile; cannot append callback */
454                         Debug( LDAP_DEBUG_ANY,
455                                 "monitor_back_register_entry_parent(\"%s\"): "
456                                 "entry is volatile\n",
457                                 e_parent->e_name.bv_val, 0, 0 );
458                         rc = -1;
459                         goto done;
460                 }
461
462                 build_new_dn( &e_name, &e_parent->e_name, &e->e_name, NULL );
463                 build_new_dn( &e_nname, &e_parent->e_nname, &e->e_nname, NULL );
464
465                 if ( monitor_cache_get( mi, &e_nname, &e_new ) == 0 ) {
466                         /* entry already exists */
467                         Debug( LDAP_DEBUG_ANY,
468                                 "monitor_back_register_entry_parent(\"%s\"): "
469                                 "entry already exists\n",
470                                 e_name.bv_val, 0, 0 );
471                         monitor_cache_release( mi, e_new );
472                         rc = -1;
473                         goto done;
474                 }
475
476                 mp = monitor_entrypriv_create();
477                 if ( mp == NULL ) {
478                         Debug( LDAP_DEBUG_ANY,
479                                 "monitor_back_register_entry_parent(\"%s\"): "
480                                 "monitor_entrypriv_create() failed\n",
481                                 e->e_name.bv_val, 0, 0 );
482                         rc = -1;
483                         goto done;
484                 }
485
486                 e_new = entry_dup( e );
487                 if ( e_new == NULL ) {
488                         Debug( LDAP_DEBUG_ANY,
489                                 "monitor_back_register_entry(\"%s\"): "
490                                 "entry_dup() failed\n",
491                                 e->e_name.bv_val, 0, 0 );
492                         rc = -1;
493                         goto done;
494                 }
495                 ch_free( e_new->e_name.bv_val );
496                 ch_free( e_new->e_nname.bv_val );
497                 e_new->e_name = e_name;
498                 e_new->e_nname = e_nname;
499                 
500                 e_new->e_private = ( void * )mp;
501                 mp->mp_info = mp_parent->mp_info;
502                 mp->mp_flags = mp_parent->mp_flags | MONITOR_F_SUB;
503                 mp->mp_cb = cb;
504
505                 ep = &mp_parent->mp_children;
506                 for ( ; *ep; ) {
507                         mp_parent = ( monitor_entry_t * )(*ep)->e_private;
508                         ep = &mp_parent->mp_next;
509                 }
510                 *ep = e_new;
511
512                 if ( monitor_cache_add( mi, e_new ) ) {
513                         Debug( LDAP_DEBUG_ANY,
514                                 "monitor_back_register_entry(\"%s\"): "
515                                 "unable to add entry\n",
516                                 e->e_name.bv_val, 0, 0 );
517                         rc = -1;
518                         goto done;
519                 }
520
521 done:;
522                 if ( !BER_BVISNULL( &ndn ) ) {
523                         ch_free( ndn.bv_val );
524                 }
525
526                 if ( rc ) {
527                         if ( mp ) {
528                                 ch_free( mp );
529                         }
530                         if ( e_new ) {
531                                 e_new->e_private = NULL;
532                                 entry_free( e_new );
533                         }
534                 }
535
536                 if ( e_parent ) {
537                         monitor_cache_release( mi, e_parent );
538                 }
539
540         } else {
541                 entry_limbo_t   **elpp, el = { 0 };
542
543                 el.el_type = LIMBO_ENTRY_PARENT;
544
545                 el.el_e = entry_dup( e );
546                 if ( el.el_e == NULL ) {
547                         Debug( LDAP_DEBUG_ANY,
548                                 "monitor_back_register_entry(\"%s\"): "
549                                 "entry_dup() failed\n",
550                                 e->e_name.bv_val, 0, 0 );
551                         return -1;
552                 }
553                 
554                 if ( !BER_BVISNULL( base ) ) {
555                         ber_dupbv( &el.el_base, base );
556                 }
557                 el.el_scope = scope;
558                 if ( !BER_BVISNULL( filter ) ) {
559                         ber_dupbv( &el.el_filter, filter );
560                 }
561
562                 el.el_cb = cb;
563
564                 for ( elpp = (entry_limbo_t **)&mi->mi_entry_limbo;
565                                 *elpp;
566                                 elpp = &(*elpp)->el_next )
567                         /* go to last */;
568
569                 *elpp = (entry_limbo_t *)ch_malloc( sizeof( entry_limbo_t ) );
570                 if ( *elpp == NULL ) {
571                         el.el_e->e_private = NULL;
572                         entry_free( el.el_e );
573                         return -1;
574                 }
575
576                 el.el_next = NULL;
577                 **elpp = el;
578         }
579
580         return 0;
581 }
582
583 static int
584 monitor_filter2ndn_cb( Operation *op, SlapReply *rs )
585 {
586         if ( rs->sr_type == REP_SEARCH ) {
587                 struct berval   *ndn = op->o_callback->sc_private;
588                 
589                 ber_dupbv( ndn, &rs->sr_entry->e_nname );
590         }
591
592         return 0;
593 }
594
595 int
596 monitor_filter2ndn(
597         struct berval   *base,
598         int             scope,
599         struct berval   *filter,
600         struct berval   *ndn )
601 {
602         Connection      conn = { 0 };
603         char            opbuf[OPERATION_BUFFER_SIZE];
604         Operation       *op;
605         SlapReply       rs = { 0 };
606         slap_callback   cb = { NULL, monitor_filter2ndn_cb, NULL, NULL };
607         int             rc;
608
609         BER_BVZERO( ndn );
610
611         if ( be_monitor == NULL ) {
612                 return -1;
613         }
614
615         op = (Operation *)opbuf;
616         connection_fake_init( &conn, op, &conn );
617
618         op->o_tag = LDAP_REQ_SEARCH;
619
620         /* use global malloc for now */
621         if ( op->o_tmpmemctx ) {
622                 /* FIXME: connection_fake_init() calls slap_sl_mem_create, so we destroy it for now */
623                 slap_sl_mem_destroy( NULL, op->o_tmpmemctx );
624                 op->o_tmpmemctx = NULL;
625         }
626         op->o_tmpmfuncs = &ch_mfuncs;
627
628         op->o_bd = be_monitor;
629         if ( base == NULL || BER_BVISNULL( base ) ) {
630                 ber_dupbv_x( &op->o_req_dn, &op->o_bd->be_suffix[ 0 ],
631                                 op->o_tmpmemctx );
632                 ber_dupbv_x( &op->o_req_ndn, &op->o_bd->be_nsuffix[ 0 ],
633                                 op->o_tmpmemctx );
634
635         } else {
636                 if ( dnPrettyNormal( NULL, base, &op->o_req_dn, &op->o_req_ndn,
637                                         op->o_tmpmemctx ) ) {
638                         return -1;
639                 }
640         }
641
642         op->o_callback = &cb;
643         cb.sc_private = (void *)ndn;
644
645         op->ors_scope = scope;
646         ber_dupbv_x( &op->ors_filterstr, filter, op->o_tmpmemctx );
647         op->ors_filter = str2filter_x( op, filter->bv_val );
648         op->ors_attrs = slap_anlist_no_attrs;
649         op->ors_attrsonly = 0;
650         op->ors_tlimit = SLAP_NO_LIMIT;
651         op->ors_slimit = 1;
652         op->ors_limit = NULL;
653         op->ors_deref = LDAP_DEREF_NEVER;
654
655         op->o_nocaching = 1;
656         op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
657
658         op->o_dn = be_monitor->be_rootdn;
659         op->o_ndn = be_monitor->be_rootndn;
660
661         rc = op->o_bd->be_search( op, &rs );
662
663         filter_free_x( op, op->ors_filter );
664         op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
665         op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
666         op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
667
668         if ( rc != 0 ) {
669                 return rc;
670         }
671
672         switch ( rs.sr_err ) {
673         case LDAP_SUCCESS:
674                 if ( BER_BVISNULL( ndn ) ) {
675                         rc = -1;
676                 }
677                 break;
678                         
679         case LDAP_SIZELIMIT_EXCEEDED:
680         default:
681                 if ( !BER_BVISNULL( ndn ) ) {
682                         ber_memfree( ndn->bv_val );
683                         BER_BVZERO( ndn );
684                 }
685                 rc = -1;
686                 break;
687         }
688
689         return rc;
690 }
691
692 int
693 monitor_back_register_entry_attrs(
694         struct berval           *ndn_in,
695         Attribute               *a,
696         monitor_callback_t      *cb,
697         struct berval           *base,
698         int                     scope,
699         struct berval           *filter )
700 {
701         monitor_info_t  *mi = ( monitor_info_t * )be_monitor->be_private;
702         struct berval   ndn = BER_BVNULL;
703
704         assert( mi != NULL );
705
706         if ( ndn_in != NULL ) {
707                 ndn = *ndn_in;
708         }
709
710         if ( a == NULL && cb == NULL ) {
711                 /* nothing to do */
712                 return -1;
713         }
714
715         if ( ( ndn_in == NULL || BER_BVISNULL( &ndn ) )
716                         && BER_BVISNULL( filter ) )
717         {
718                 /* need a filter */
719                 Debug( LDAP_DEBUG_ANY,
720                         "monitor_back_register_entry_*(\"\"): "
721                         "need a valid filter\n",
722                         0, 0, 0 );
723                 return -1;
724         }
725
726         if ( monitor_subsys_opened ) {
727                 Entry                   *e = NULL;
728                 Attribute               **atp = NULL;
729                 monitor_entry_t         *mp = NULL;
730                 monitor_callback_t      **mcp = NULL;
731                 int                     rc = 0;
732                 int                     freeit = 0;
733
734                 if ( BER_BVISNULL( &ndn ) ) {
735                         if ( monitor_filter2ndn( base, scope, filter, &ndn ) ) {
736                                 /* entry does not exist */
737                                 Debug( LDAP_DEBUG_ANY,
738                                         "monitor_back_register_entry_*(\"\"): "
739                                         "base=%s scope=%d filter=%s : "
740                                         "unable to find entry\n",
741                                         base->bv_val ? base->bv_val : "\"\"",
742                                         scope, filter->bv_val );
743                                 return -1;
744                         }
745
746                         freeit = 1;
747                 }
748
749                 if ( monitor_cache_get( mi, &ndn, &e ) != 0 ) {
750                         /* entry does not exist */
751                         Debug( LDAP_DEBUG_ANY,
752                                 "monitor_back_register_entry_*(\"%s\"): "
753                                 "entry does not exist\n",
754                                 ndn.bv_val, 0, 0 );
755                         rc = -1;
756                         goto done;
757                 }
758
759                 assert( e->e_private != NULL );
760                 mp = ( monitor_entry_t * )e->e_private;
761
762                 if ( mp->mp_flags & MONITOR_F_VOLATILE ) {
763                         /* entry is volatile; cannot append callback */
764                         Debug( LDAP_DEBUG_ANY,
765                                 "monitor_back_register_entry_*(\"%s\"): "
766                                 "entry is volatile\n",
767                                 e->e_name.bv_val, 0, 0 );
768                         rc = -1;
769                         goto done;
770                 }
771
772                 if ( a ) {
773                         for ( atp = &e->e_attrs; *atp; atp = &(*atp)->a_next )
774                                 /* just get to last */ ;
775
776                         *atp = attrs_dup( a );
777                         if ( *atp == NULL ) {
778                                 Debug( LDAP_DEBUG_ANY,
779                                         "monitor_back_register_entry_*(\"%s\"): "
780                                         "attrs_dup() failed\n",
781                                         e->e_name.bv_val, 0, 0 );
782                                 rc = -1;
783                                 goto done;
784                         }
785                 }
786
787                 if ( cb ) {
788                         for ( mcp = &mp->mp_cb; *mcp; mcp = &(*mcp)->mc_next )
789                                 /* go to tail */ ;
790                 
791                         /* NOTE: we do not clear cb->mc_next, so this function
792                          * can be used to append a list of callbacks */
793                         (*mcp) = cb;
794                 }
795
796 done:;
797                 if ( rc ) {
798                         if ( *atp ) {
799                                 attrs_free( *atp );
800                                 *atp = NULL;
801                         }
802                 }
803
804                 if ( freeit ) {
805                         ber_memfree( ndn.bv_val );
806                 }
807
808                 if ( e ) {
809                         monitor_cache_release( mi, e );
810                 }
811
812         } else {
813                 entry_limbo_t   **elpp, el = { 0 };
814
815                 el.el_type = LIMBO_ATTRS;
816                 if ( !BER_BVISNULL( &ndn ) ) {
817                         ber_dupbv( &el.el_ndn, &ndn );
818                 }
819                 if ( !BER_BVISNULL( base ) ) {
820                         ber_dupbv( &el.el_base, base);
821                 }
822                 el.el_scope = scope;
823                 if ( !BER_BVISNULL( filter ) ) {
824                         ber_dupbv( &el.el_filter, filter );
825                 }
826
827                 el.el_a = attrs_dup( a );
828                 el.el_cb = cb;
829
830                 for ( elpp = (entry_limbo_t **)&mi->mi_entry_limbo;
831                                 *elpp;
832                                 elpp = &(*elpp)->el_next )
833                         /* go to last */;
834
835                 *elpp = (entry_limbo_t *)ch_malloc( sizeof( entry_limbo_t ) );
836                 if ( *elpp == NULL ) {
837                         el.el_e->e_private = NULL;
838                         entry_free( el.el_e );
839                         return -1;
840                 }
841
842                 el.el_next = NULL;
843                 **elpp = el;
844         }
845
846         return 0;
847 }
848
849 int
850 monitor_back_register_entry_callback(
851         struct berval           *ndn,
852         monitor_callback_t      *cb,
853         struct berval           *base,
854         int                     scope,
855         struct berval           *filter )
856 {
857         return monitor_back_register_entry_attrs( ndn, NULL, cb,
858                         base, scope, filter );
859 }
860
861 monitor_subsys_t *
862 monitor_back_get_subsys( const char *name )
863 {
864         if ( monitor_subsys != NULL ) {
865                 int     i;
866                 
867                 for ( i = 0; monitor_subsys[ i ] != NULL; i++ ) {
868                         if ( strcasecmp( monitor_subsys[ i ]->mss_name, name ) == 0 ) {
869                                 return monitor_subsys[ i ];
870                         }
871                 }
872         }
873
874         return NULL;
875 }
876
877 monitor_subsys_t *
878 monitor_back_get_subsys_by_dn(
879         struct berval   *ndn,
880         int             sub )
881 {
882         if ( monitor_subsys != NULL ) {
883                 int     i;
884
885                 if ( sub ) {
886                         for ( i = 0; monitor_subsys[ i ] != NULL; i++ ) {
887                                 if ( dnIsSuffix( ndn, &monitor_subsys[ i ]->mss_ndn ) ) {
888                                         return monitor_subsys[ i ];
889                                 }
890                         }
891
892                 } else {
893                         for ( i = 0; monitor_subsys[ i ] != NULL; i++ ) {
894                                 if ( dn_match( ndn, &monitor_subsys[ i ]->mss_ndn ) ) {
895                                         return monitor_subsys[ i ];
896                                 }
897                         }
898                 }
899         }
900
901         return NULL;
902 }
903
904 int
905 monitor_back_initialize(
906         BackendInfo     *bi )
907 {
908         monitor_subsys_t        *ms;
909         static char             *controls[] = {
910                 LDAP_CONTROL_MANAGEDSAIT,
911                 NULL
912         };
913         monitor_info_t          *mi = &monitor_info;
914         int                     i;
915         const char              *text;
916
917         struct m_s {
918                 char    *name;
919                 char    *schema;
920                 slap_mask_t flags;
921                 int     offset;
922         } moc[] = {
923                 { "monitor", "( 1.3.6.1.4.1.4203.666.3.2 "
924                         "NAME 'monitor' "
925                         "DESC 'OpenLDAP system monitoring' "
926                         "SUP top STRUCTURAL "
927                         "MUST cn "
928                         "MAY ( "
929                                 "description "
930                                 "$ seeAlso "
931                                 "$ labeledURI "
932                                 "$ monitoredInfo "
933                                 "$ managedInfo "
934                                 "$ monitorOverlay "
935                         ") )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
936                         offsetof(monitor_info_t, mi_oc_monitor) },
937                 { "monitorServer", "( 1.3.6.1.4.1.4203.666.3.7 "
938                         "NAME 'monitorServer' "
939                         "DESC 'Server monitoring root entry' "
940                         "SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
941                         offsetof(monitor_info_t, mi_oc_monitorServer) },
942                 { "monitorContainer", "( 1.3.6.1.4.1.4203.666.3.8 "
943                         "NAME 'monitorContainer' "
944                         "DESC 'monitor container class' "
945                         "SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
946                         offsetof(monitor_info_t, mi_oc_monitorContainer) },
947                 { "monitorCounterObject", "( 1.3.6.1.4.1.4203.666.3.9 "
948                         "NAME 'monitorCounterObject' "
949                         "DESC 'monitor counter class' "
950                         "SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
951                         offsetof(monitor_info_t, mi_oc_monitorCounterObject) },
952                 { "monitorOperation", "( 1.3.6.1.4.1.4203.666.3.10 "
953                         "NAME 'monitorOperation' "
954                         "DESC 'monitor operation class' "
955                         "SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
956                         offsetof(monitor_info_t, mi_oc_monitorOperation) },
957                 { "monitorConnection", "( 1.3.6.1.4.1.4203.666.3.11 "
958                         "NAME 'monitorConnection' "
959                         "DESC 'monitor connection class' "
960                         "SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
961                         offsetof(monitor_info_t, mi_oc_monitorConnection) },
962                 { "managedObject", "( 1.3.6.1.4.1.4203.666.3.12 "
963                         "NAME 'managedObject' "
964                         "DESC 'monitor managed entity class' "
965                         "SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
966                         offsetof(monitor_info_t, mi_oc_managedObject) },
967                 { "monitoredObject", "( 1.3.6.1.4.1.4203.666.3.14 "
968                         "NAME 'monitoredObject' "
969                         "DESC 'monitor monitored entity class' "
970                         "SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
971                         offsetof(monitor_info_t, mi_oc_monitoredObject) },
972                 { NULL, NULL, 0, -1 }
973         }, mat[] = {
974                 { "monitoredInfo", "( 1.3.6.1.4.1.4203.666.1.14 "
975                         "NAME 'monitoredInfo' "
976                         "DESC 'monitored info' "
977                         /* "SUP name " */
978                         "EQUALITY caseIgnoreMatch "
979                         "SUBSTR caseIgnoreSubstringsMatch "
980                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15{32768} "
981                         "NO-USER-MODIFICATION "
982                         "USAGE directoryOperation )", SLAP_AT_HIDE,
983                         offsetof(monitor_info_t, mi_ad_monitoredInfo) },
984                 { "managedInfo", "( 1.3.6.1.4.1.4203.666.1.15 "
985                         "NAME 'managedInfo' "
986                         "DESC 'monitor managed info' "
987                         "SUP name )", SLAP_AT_HIDE,
988                         offsetof(monitor_info_t, mi_ad_managedInfo) },
989                 { "monitorCounter", "( 1.3.6.1.4.1.4203.666.1.16 "
990                         "NAME 'monitorCounter' "
991                         "DESC 'monitor counter' "
992                         "EQUALITY integerMatch "
993                         "ORDERING integerOrderingMatch "
994                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 "
995                         "NO-USER-MODIFICATION "
996                         "USAGE directoryOperation )", SLAP_AT_HIDE,
997                         offsetof(monitor_info_t, mi_ad_monitorCounter) },
998                 { "monitorOpCompleted", "( 1.3.6.1.4.1.4203.666.1.17 "
999                         "NAME 'monitorOpCompleted' "
1000                         "DESC 'monitor completed operations' "
1001                         "SUP monitorCounter "
1002                         "NO-USER-MODIFICATION "
1003                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1004                         offsetof(monitor_info_t, mi_ad_monitorOpCompleted) },
1005                 { "monitorOpInitiated", "( 1.3.6.1.4.1.4203.666.1.18 "
1006                         "NAME 'monitorOpInitiated' "
1007                         "DESC 'monitor initiated operations' "
1008                         "SUP monitorCounter "
1009                         "NO-USER-MODIFICATION "
1010                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1011                         offsetof(monitor_info_t, mi_ad_monitorOpInitiated) },
1012                 { "monitorConnectionNumber", "( 1.3.6.1.4.1.4203.666.1.19 "
1013                         "NAME 'monitorConnectionNumber' "
1014                         "DESC 'monitor connection number' "
1015                         "SUP monitorCounter "
1016                         "NO-USER-MODIFICATION "
1017                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1018                         offsetof(monitor_info_t, mi_ad_monitorConnectionNumber) },
1019                 { "monitorConnectionAuthzDN", "( 1.3.6.1.4.1.4203.666.1.20 "
1020                         "NAME 'monitorConnectionAuthzDN' "
1021                         "DESC 'monitor connection authorization DN' "
1022                         /* "SUP distinguishedName " */
1023                         "EQUALITY distinguishedNameMatch "
1024                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.12 "
1025                         "NO-USER-MODIFICATION "
1026                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1027                         offsetof(monitor_info_t, mi_ad_monitorConnectionAuthzDN) },
1028                 { "monitorConnectionLocalAddress", "( 1.3.6.1.4.1.4203.666.1.21 "
1029                         "NAME 'monitorConnectionLocalAddress' "
1030                         "DESC 'monitor connection local address' "
1031                         "SUP monitoredInfo "
1032                         "NO-USER-MODIFICATION "
1033                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1034                         offsetof(monitor_info_t, mi_ad_monitorConnectionLocalAddress) },
1035                 { "monitorConnectionPeerAddress", "( 1.3.6.1.4.1.4203.666.1.22 "
1036                         "NAME 'monitorConnectionPeerAddress' "
1037                         "DESC 'monitor connection peer address' "
1038                         "SUP monitoredInfo "
1039                         "NO-USER-MODIFICATION "
1040                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1041                         offsetof(monitor_info_t, mi_ad_monitorConnectionPeerAddress) },
1042                 { "monitorTimestamp", "( 1.3.6.1.4.1.4203.666.1.24 "
1043                         "NAME 'monitorTimestamp' "
1044                         "DESC 'monitor timestamp' "
1045                         "EQUALITY generalizedTimeMatch "
1046                         "ORDERING generalizedTimeOrderingMatch "
1047                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
1048                         "SINGLE-VALUE "
1049                         "NO-USER-MODIFICATION "
1050                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1051                         offsetof(monitor_info_t, mi_ad_monitorTimestamp) },
1052                 { "monitorOverlay", "( 1.3.6.1.4.1.4203.666.1.27 "
1053                         "NAME 'monitorOverlay' "
1054                         "DESC 'name of overlays defined for a given database' "
1055                         "SUP monitoredInfo "
1056                         "NO-USER-MODIFICATION "
1057                         "USAGE directoryOperation )", SLAP_AT_HIDE,
1058                         offsetof(monitor_info_t, mi_ad_monitorOverlay) },
1059                 { "readOnly", "( 1.3.6.1.4.1.4203.666.1.31 "
1060                         "NAME 'readOnly' "
1061                         "DESC 'read/write status of a given database' "
1062                         "EQUALITY booleanMatch "
1063                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.7 "
1064                         "SINGLE-VALUE "
1065                         "USAGE directoryOperation )", SLAP_AT_HIDE,
1066                         offsetof(monitor_info_t, mi_ad_readOnly) },
1067                 { "restrictedOperation", "( 1.3.6.1.4.1.4203.666.1.32 "
1068                         "NAME 'restrictedOperation' "
1069                         "DESC 'name of restricted operation for a given database' "
1070                         "SUP managedInfo )", SLAP_AT_HIDE,
1071                         offsetof(monitor_info_t, mi_ad_restrictedOperation ) },
1072                 { "monitorConnectionProtocol", "( 1.3.6.1.4.1.4203.666.1.39 "
1073                         "NAME 'monitorConnectionProtocol' "
1074                         "DESC 'monitor connection protocol' "
1075                         "SUP monitoredInfo "
1076                         "NO-USER-MODIFICATION "
1077                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1078                         offsetof(monitor_info_t, mi_ad_monitorConnectionProtocol) },
1079                 { "monitorConnectionOpsReceived", "( 1.3.6.1.4.1.4203.666.1.40 "
1080                         "NAME 'monitorConnectionOpsReceived' "
1081                         "DESC 'monitor number of operations received by the connection' "
1082                         "SUP monitorCounter "
1083                         "NO-USER-MODIFICATION "
1084                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1085                         offsetof(monitor_info_t, mi_ad_monitorConnectionOpsReceived) },
1086                 { "monitorConnectionOpsExecuting", "( 1.3.6.1.4.1.4203.666.1.41 "
1087                         "NAME 'monitorConnectionOpsExecuting' "
1088                         "DESC 'monitor number of operations in execution within the connection' "
1089                         "SUP monitorCounter "
1090                         "NO-USER-MODIFICATION "
1091                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1092                         offsetof(monitor_info_t, mi_ad_monitorConnectionOpsExecuting) },
1093                 { "monitorConnectionOpsPending", "( 1.3.6.1.4.1.4203.666.1.42 "
1094                         "NAME 'monitorConnectionOpsPending' "
1095                         "DESC 'monitor number of pending operations within the connection' "
1096                         "SUP monitorCounter "
1097                         "NO-USER-MODIFICATION "
1098                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1099                         offsetof(monitor_info_t, mi_ad_monitorConnectionOpsPending) },
1100                 { "monitorConnectionOpsCompleted", "( 1.3.6.1.4.1.4203.666.1.43 "
1101                         "NAME 'monitorConnectionOpsCompleted' "
1102                         "DESC 'monitor number of operations completed within the connection' "
1103                         "SUP monitorCounter "
1104                         "NO-USER-MODIFICATION "
1105                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1106                         offsetof(monitor_info_t, mi_ad_monitorConnectionOpsCompleted) },
1107                 { "monitorConnectionGet", "( 1.3.6.1.4.1.4203.666.1.44 "
1108                         "NAME 'monitorConnectionGet' "
1109                         "DESC 'number of times connection_get() was called so far' "
1110                         "SUP monitorCounter "
1111                         "NO-USER-MODIFICATION "
1112                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1113                         offsetof(monitor_info_t, mi_ad_monitorConnectionGet) },
1114                 { "monitorConnectionRead", "( 1.3.6.1.4.1.4203.666.1.45 "
1115                         "NAME 'monitorConnectionRead' "
1116                         "DESC 'number of times connection_read() was called so far' "
1117                         "SUP monitorCounter "
1118                         "NO-USER-MODIFICATION "
1119                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1120                         offsetof(monitor_info_t, mi_ad_monitorConnectionRead) },
1121                 { "monitorConnectionWrite", "( 1.3.6.1.4.1.4203.666.1.46 "
1122                         "NAME 'monitorConnectionWrite' "
1123                         "DESC 'number of times connection_write() was called so far' "
1124                         "SUP monitorCounter "
1125                         "NO-USER-MODIFICATION "
1126                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1127                         offsetof(monitor_info_t, mi_ad_monitorConnectionWrite) },
1128                 { "monitorConnectionMask", "( 1.3.6.1.4.1.4203.666.1.47 "
1129                         "NAME 'monitorConnectionMask' "
1130                         "DESC 'monitor connection mask' "
1131                         "SUP monitoredInfo "
1132                         "NO-USER-MODIFICATION "
1133                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1134                         offsetof(monitor_info_t, mi_ad_monitorConnectionMask) },
1135                 { "monitorConnectionListener", "( 1.3.6.1.4.1.4203.666.1.48 "
1136                         "NAME 'monitorConnectionListener' "
1137                         "DESC 'monitor connection listener' "
1138                         "SUP monitoredInfo "
1139                         "NO-USER-MODIFICATION "
1140                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1141                         offsetof(monitor_info_t, mi_ad_monitorConnectionListener) },
1142                 { "monitorConnectionPeerDomain", "( 1.3.6.1.4.1.4203.666.1.49 "
1143                         "NAME 'monitorConnectionPeerDomain' "
1144                         "DESC 'monitor connection peer domain' "
1145                         "SUP monitoredInfo "
1146                         "NO-USER-MODIFICATION "
1147                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1148                         offsetof(monitor_info_t, mi_ad_monitorConnectionPeerDomain) },
1149                 { "monitorConnectionStartTime", "( 1.3.6.1.4.1.4203.666.1.50 "
1150                         "NAME 'monitorConnectionStartTime' "
1151                         "DESC 'monitor connection start time' "
1152                         "SUP monitorTimestamp "
1153                         "SINGLE-VALUE "
1154                         "NO-USER-MODIFICATION "
1155                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1156                         offsetof(monitor_info_t, mi_ad_monitorConnectionStartTime) },
1157                 { "monitorConnectionActivityTime", "( 1.3.6.1.4.1.4203.666.1.51 "
1158                         "NAME 'monitorConnectionActivityTime' "
1159                         "DESC 'monitor connection activity time' "
1160                         "SUP monitorTimestamp "
1161                         "SINGLE-VALUE "
1162                         "NO-USER-MODIFICATION "
1163                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
1164                         offsetof(monitor_info_t, mi_ad_monitorConnectionActivityTime) },
1165                 { "monitorIsShadow", "( 1.3.6.1.4.1.4203.666.1.52 "
1166                         "NAME 'monitorIsShadow' "
1167                         "DESC 'TRUE if the database is shadow' "
1168                         "EQUALITY booleanMatch "
1169                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.7 "
1170                         "SINGLE-VALUE "
1171                         "USAGE directoryOperation )", SLAP_AT_HIDE,
1172                         offsetof(monitor_info_t, mi_ad_monitorIsShadow) },
1173                 { "monitorUpdateRef", "( 1.3.6.1.4.1.4203.666.1.53 "
1174                         "NAME 'monitorUpdateRef' "
1175                         "DESC 'update referral for shadow databases' "
1176                         "SUP monitoredInfo "
1177                         "SINGLE-VALUE "
1178                         "USAGE directoryOperation )", SLAP_AT_HIDE,
1179                         offsetof(monitor_info_t, mi_ad_monitorUpdateRef) },
1180                 { NULL, NULL, 0, -1 }
1181         };
1182         
1183         bi->bi_controls = controls;
1184
1185         bi->bi_init = 0;
1186         bi->bi_open = 0;
1187         bi->bi_config = monitor_back_config;
1188         bi->bi_close = 0;
1189         bi->bi_destroy = 0;
1190
1191         bi->bi_db_init = monitor_back_db_init;
1192         bi->bi_db_config = monitor_back_db_config;
1193         bi->bi_db_open = monitor_back_db_open;
1194         bi->bi_db_close = 0;
1195         bi->bi_db_destroy = monitor_back_db_destroy;
1196
1197         bi->bi_op_bind = monitor_back_bind;
1198         bi->bi_op_unbind = 0;
1199         bi->bi_op_search = monitor_back_search;
1200         bi->bi_op_compare = monitor_back_compare;
1201         bi->bi_op_modify = monitor_back_modify;
1202         bi->bi_op_modrdn = 0;
1203         bi->bi_op_add = 0;
1204         bi->bi_op_delete = 0;
1205         bi->bi_op_abandon = 0;
1206
1207         bi->bi_extended = 0;
1208
1209         bi->bi_entry_release_rw = 0;
1210         bi->bi_chk_referrals = 0;
1211         bi->bi_operational = monitor_back_operational;
1212
1213         /*
1214          * hooks for slap tools
1215          */
1216         bi->bi_tool_entry_open = 0;
1217         bi->bi_tool_entry_close = 0;
1218         bi->bi_tool_entry_first = 0;
1219         bi->bi_tool_entry_next = 0;
1220         bi->bi_tool_entry_get = 0;
1221         bi->bi_tool_entry_put = 0;
1222         bi->bi_tool_entry_reindex = 0;
1223         bi->bi_tool_sync = 0;
1224         bi->bi_tool_dn2id_get = 0;
1225         bi->bi_tool_id2entry_get = 0;
1226         bi->bi_tool_entry_modify = 0;
1227
1228         bi->bi_connection_init = 0;
1229         bi->bi_connection_destroy = 0;
1230
1231         for ( ms = known_monitor_subsys; ms->mss_name != NULL; ms++ ) {
1232                 if ( monitor_back_register_subsys( ms ) ) {
1233                         return -1;
1234                 }
1235         }
1236
1237         /* schema integration */
1238         for ( i = 0; mat[ i ].name; i++ ) {
1239                 LDAPAttributeType       *at;
1240                 int                     code;
1241                 const char              *err;
1242                 AttributeDescription    **ad;
1243
1244                 at = ldap_str2attributetype( mat[ i ].schema, &code,
1245                         &err, LDAP_SCHEMA_ALLOW_ALL );
1246                 if ( !at ) {
1247                         Debug( LDAP_DEBUG_ANY, "monitor_back_db_init: "
1248                                 "in AttributeType \"%s\" %s before %s\n",
1249                                 mat[ i ].name, ldap_scherr2str(code), err );
1250                         return -1;
1251                 }
1252
1253                 if ( at->at_oid == NULL ) {
1254                         Debug( LDAP_DEBUG_ANY, "monitor_back_db_init: "
1255                                 "null OID for attributeType \"%s\"\n",
1256                                 mat[ i ].name, 0, 0 );
1257                         return -1;
1258                 }
1259
1260                 code = at_add(at, 0, NULL, &err);
1261                 if ( code ) {
1262                         Debug( LDAP_DEBUG_ANY, "monitor_back_db_init: "
1263                                 "%s in attributeType \"%s\"\n",
1264                                 scherr2str(code), mat[ i ].name, 0 );
1265                         return -1;
1266                 }
1267                 ldap_memfree(at);
1268
1269                 ad = ((AttributeDescription **)&(((char *)mi)[ mat[ i ].offset ]));
1270                 ad[ 0 ] = NULL;
1271                 if ( slap_str2ad( mat[ i ].name, ad, &text ) ) {
1272                         Debug( LDAP_DEBUG_ANY,
1273                                 "monitor_back_db_init: %s\n", text, 0, 0 );
1274                         return -1;
1275                 }
1276
1277                 (*ad)->ad_type->sat_flags |= mat[ i ].flags;
1278         }
1279
1280         for ( i = 0; moc[ i ].name; i++ ) {
1281                 LDAPObjectClass         *oc;
1282                 int                     code;
1283                 const char              *err;
1284                 ObjectClass             *Oc;
1285
1286                 oc = ldap_str2objectclass(moc[ i ].schema, &code, &err,
1287                                 LDAP_SCHEMA_ALLOW_ALL );
1288                 if ( !oc ) {
1289                         Debug( LDAP_DEBUG_ANY,
1290                                 "unable to parse monitor objectclass \"%s\": "
1291                                 "%s before %s\n" , moc[ i ].name,
1292                                 ldap_scherr2str(code), err );
1293                         return -1;
1294                 }
1295
1296                 if ( oc->oc_oid == NULL ) {
1297                         Debug( LDAP_DEBUG_ANY,
1298                                 "objectclass \"%s\" has no OID\n" ,
1299                                 moc[ i ].name, 0, 0 );
1300                         return -1;
1301                 }
1302
1303                 code = oc_add(oc, 0, NULL, &err);
1304                 if ( code ) {
1305                         Debug( LDAP_DEBUG_ANY,
1306                                 "objectclass \"%s\": %s \"%s\"\n" ,
1307                                 moc[ i ].name, scherr2str(code), err );
1308                         return -1;
1309                 }
1310
1311                 ldap_memfree(oc);
1312
1313                 Oc = oc_find( moc[ i ].name );
1314                 if ( Oc == NULL ) {
1315                         Debug( LDAP_DEBUG_ANY, "monitor_back_db_init: "
1316                                         "unable to find objectClass %s "
1317                                         "(just added)\n", moc[ i ].name, 0, 0 );
1318                         return -1;
1319                 }
1320
1321                 Oc->soc_flags |= moc[ i ].flags;
1322
1323                 ((ObjectClass **)&(((char *)mi)[ moc[ i ].offset ]))[ 0 ] = Oc;
1324         }
1325
1326         return 0;
1327 }
1328
1329 int
1330 monitor_back_db_init(
1331         BackendDB       *be )
1332 {
1333         monitor_info_t  *mi;
1334         int             rc;
1335         struct berval   dn, ndn;
1336         struct berval   bv;
1337
1338         /*
1339          * database monitor can be defined once only
1340          */
1341         if ( be_monitor != NULL ) {
1342                 Debug( LDAP_DEBUG_ANY,
1343                         "only one monitor database is allowed\n", 0, 0, 0 );
1344                 return( -1 );
1345         }
1346         be_monitor = be;
1347
1348         /* indicate system schema supported */
1349         SLAP_BFLAGS(be) |= SLAP_BFLAG_MONITOR;
1350
1351         dn.bv_val = SLAPD_MONITOR_DN;
1352         dn.bv_len = sizeof( SLAPD_MONITOR_DN ) - 1;
1353
1354         rc = dnNormalize( 0, NULL, NULL, &dn, &ndn, NULL );
1355         if( rc != LDAP_SUCCESS ) {
1356                 Debug( LDAP_DEBUG_ANY,
1357                         "unable to normalize monitor DN \"%s\"\n",
1358                         SLAPD_MONITOR_DN, 0, 0 );
1359                 return -1;
1360         }
1361
1362         ber_dupbv( &bv, &dn );
1363         ber_bvarray_add( &be->be_suffix, &bv );
1364         ber_bvarray_add( &be->be_nsuffix, &ndn );
1365
1366         /* NOTE: only one monitor database is allowed,
1367          * so we use static storage */
1368         mi = &monitor_info;
1369
1370         ldap_pvt_thread_mutex_init( &mi->mi_cache_mutex );
1371
1372         be->be_private = mi;
1373
1374         return 0;
1375 }
1376
1377 int
1378 monitor_back_db_open(
1379         BackendDB       *be )
1380 {
1381         monitor_info_t          *mi = (monitor_info_t *)be->be_private;
1382         struct monitor_subsys_t **ms;
1383         Entry                   *e, **ep;
1384         monitor_entry_t         *mp;
1385         int                     i;
1386         char                    buf[ BACKMONITOR_BUFSIZE ];
1387         struct berval           bv;
1388         struct tm               *tms;
1389 #ifdef HAVE_GMTIME_R
1390         struct tm               tm_buf;
1391 #endif
1392         static char             tmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
1393
1394         assert( be_monitor != NULL );
1395         if ( be != be_monitor ) {
1396                 be_monitor = be;
1397         }
1398
1399         /*
1400          * Start
1401          */
1402 #ifndef HAVE_GMTIME_R
1403         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
1404 #endif
1405 #ifdef HACK_LOCAL_TIME
1406 # ifdef HAVE_LOCALTIME_R
1407         tms = localtime_r( &starttime, &tm_buf );
1408 # else
1409         tms = localtime( &starttime );
1410 # endif /* HAVE_LOCALTIME_R */
1411         lutil_localtime( tmbuf, sizeof(tmbuf), tms, -timezone );
1412 #else /* !HACK_LOCAL_TIME */
1413 # ifdef HAVE_GMTIME_R
1414         tms = gmtime_r( &starttime, &tm_buf );
1415 # else
1416         tms = gmtime( &starttime );
1417 # endif /* HAVE_GMTIME_R */
1418         lutil_gentime( tmbuf, sizeof(tmbuf), tms );
1419 #endif /* !HACK_LOCAL_TIME */
1420 #ifndef HAVE_GMTIME_R
1421         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
1422 #endif
1423
1424         mi->mi_startTime.bv_val = tmbuf;
1425         mi->mi_startTime.bv_len = strlen( tmbuf );
1426
1427         if ( BER_BVISEMPTY( &be->be_rootdn ) ) {
1428                 BER_BVSTR( &mi->mi_creatorsName, SLAPD_ANONYMOUS );
1429         } else {
1430                 mi->mi_creatorsName = be->be_rootdn;
1431         }
1432
1433         /*
1434          * creates the "cn=Monitor" entry 
1435          */
1436         snprintf( buf, sizeof( buf ), 
1437                 "dn: %s\n"
1438                 "objectClass: %s\n"
1439                 "structuralObjectClass: %s\n"
1440                 "cn: Monitor\n"
1441                 "description: This subtree contains monitoring/managing objects.\n"
1442                 "description: This object contains information about this server.\n"
1443                 "description: Most of the information is held in operational"
1444                 " attributeTypes, which must be explicitly requested.\n"
1445                 "creatorsName: %s\n"
1446                 "modifiersName: %s\n"
1447                 "createTimestamp: %s\n"
1448                 "modifyTimestamp: %s\n",
1449                 SLAPD_MONITOR_DN,
1450                 mi->mi_oc_monitorServer->soc_cname.bv_val,
1451                 mi->mi_oc_monitorServer->soc_cname.bv_val,
1452                 mi->mi_creatorsName.bv_val,
1453                 mi->mi_creatorsName.bv_val,
1454                 mi->mi_startTime.bv_val,
1455                 mi->mi_startTime.bv_val );
1456
1457         e = str2entry( buf );
1458         if ( e == NULL) {
1459                 Debug( LDAP_DEBUG_ANY,
1460                         "unable to create \"%s\" entry\n",
1461                         SLAPD_MONITOR_DN, 0, 0 );
1462                 return( -1 );
1463         }
1464
1465         bv.bv_val = strchr( (char *) Versionstr, '$' );
1466         if ( bv.bv_val != NULL ) {
1467                 char    *end;
1468
1469                 bv.bv_val++;
1470                 for ( ; bv.bv_val[ 0 ] == ' '; bv.bv_val++ )
1471                         ;
1472
1473                 end = strchr( bv.bv_val, '$' );
1474                 if ( end != NULL ) {
1475                         end--;
1476
1477                         for ( ; end > bv.bv_val && end[ 0 ] == ' '; end-- )
1478                                 ;
1479
1480                         end++;
1481
1482                         bv.bv_len = end - bv.bv_val;
1483
1484                 } else {
1485                         bv.bv_len = strlen( bv.bv_val );
1486                 }
1487
1488                 if ( attr_merge_normalize_one( e, mi->mi_ad_monitoredInfo,
1489                                         &bv, NULL ) ) {
1490                         Debug( LDAP_DEBUG_ANY,
1491                                 "unable to add monitoredInfo to \"%s\" entry\n",
1492                                 SLAPD_MONITOR_DN, 0, 0 );
1493                         return( -1 );
1494                 }
1495         }
1496
1497         mp = monitor_entrypriv_create();
1498         if ( mp == NULL ) {
1499                 return -1;
1500         }
1501         e->e_private = ( void * )mp;
1502         ep = &mp->mp_children;
1503
1504         if ( monitor_cache_add( mi, e ) ) {
1505                 Debug( LDAP_DEBUG_ANY,
1506                         "unable to add entry \"%s\" to cache\n",
1507                         SLAPD_MONITOR_DN, 0, 0 );
1508                 return -1;
1509         }
1510
1511         /*      
1512          * Create all the subsystem specific entries
1513          */
1514         for ( i = 0; monitor_subsys[ i ] != NULL; i++ ) {
1515                 int             len = strlen( monitor_subsys[ i ]->mss_name );
1516                 struct berval   dn;
1517                 int             rc;
1518
1519                 dn.bv_len = len + sizeof( "cn=" ) - 1;
1520                 dn.bv_val = ch_calloc( sizeof( char ), dn.bv_len + 1 );
1521                 strcpy( dn.bv_val, "cn=" );
1522                 strcat( dn.bv_val, monitor_subsys[ i ]->mss_name );
1523                 rc = dnPretty( NULL, &dn, &monitor_subsys[ i ]->mss_rdn, NULL );
1524                 free( dn.bv_val );
1525                 if ( rc != LDAP_SUCCESS ) {
1526                         Debug( LDAP_DEBUG_ANY,
1527                                 "monitor RDN \"%s\" is invalid\n", 
1528                                 dn.bv_val, 0, 0 );
1529                         return( -1 );
1530                 }
1531
1532                 dn.bv_len += sizeof( SLAPD_MONITOR_DN ); /* 1 for the , */
1533                 dn.bv_val = ch_malloc( dn.bv_len + 1 );
1534                 strcpy( dn.bv_val , monitor_subsys[ i ]->mss_rdn.bv_val );
1535                 strcat( dn.bv_val, "," SLAPD_MONITOR_DN );
1536                 rc = dnPrettyNormal( NULL, &dn, &monitor_subsys[ i ]->mss_dn,
1537                         &monitor_subsys[ i ]->mss_ndn, NULL );
1538                 free( dn.bv_val );
1539                 if ( rc != LDAP_SUCCESS ) {
1540                         Debug( LDAP_DEBUG_ANY,
1541                                 "monitor DN \"%s\" is invalid\n", 
1542                                 dn.bv_val, 0, 0 );
1543                         return( -1 );
1544                 }
1545
1546                 snprintf( buf, sizeof( buf ),
1547                                 "dn: %s\n"
1548                                 "objectClass: %s\n"
1549                                 "structuralObjectClass: %s\n"
1550                                 "cn: %s\n"
1551                                 "creatorsName: %s\n"
1552                                 "modifiersName: %s\n"
1553                                 "createTimestamp: %s\n"
1554                                 "modifyTimestamp: %s\n",
1555                                 monitor_subsys[ i ]->mss_dn.bv_val,
1556                                 mi->mi_oc_monitorContainer->soc_cname.bv_val,
1557                                 mi->mi_oc_monitorContainer->soc_cname.bv_val,
1558                                 monitor_subsys[ i ]->mss_name,
1559                                 mi->mi_creatorsName.bv_val,
1560                                 mi->mi_creatorsName.bv_val,
1561                                 mi->mi_startTime.bv_val,
1562                                 mi->mi_startTime.bv_val );
1563                 
1564                 e = str2entry( buf );
1565                 
1566                 if ( e == NULL) {
1567                         Debug( LDAP_DEBUG_ANY,
1568                                 "unable to create \"%s\" entry\n", 
1569                                 monitor_subsys[ i ]->mss_dn.bv_val, 0, 0 );
1570                         return( -1 );
1571                 }
1572
1573                 if ( !BER_BVISNULL( &monitor_subsys[ i ]->mss_desc[ 0 ] ) ) {
1574                         attr_merge_normalize( e, slap_schema.si_ad_description,
1575                                         monitor_subsys[ i ]->mss_desc, NULL );
1576                 }
1577
1578                 mp = monitor_entrypriv_create();
1579                 if ( mp == NULL ) {
1580                         return -1;
1581                 }
1582                 e->e_private = ( void * )mp;
1583                 mp->mp_info = monitor_subsys[ i ];
1584                 mp->mp_flags = monitor_subsys[ i ]->mss_flags;
1585
1586                 if ( monitor_cache_add( mi, e ) ) {
1587                         Debug( LDAP_DEBUG_ANY,
1588                                 "unable to add entry \"%s\" to cache\n",
1589                                 monitor_subsys[ i ]->mss_dn.bv_val, 0, 0 );
1590                         return -1;
1591                 }
1592
1593                 *ep = e;
1594                 ep = &mp->mp_next;
1595         }
1596
1597         assert( be != NULL );
1598
1599         be->be_private = mi;
1600         
1601         /*
1602          * opens the monitor backend subsystems
1603          */
1604         for ( ms = monitor_subsys; ms[ 0 ] != NULL; ms++ ) {
1605                 if ( ms[ 0 ]->mss_open && ( *ms[ 0 ]->mss_open )( be, ms[ 0 ] ) )
1606                 {
1607                         return( -1 );
1608                 }
1609                 ms[ 0 ]->mss_flags |= MONITOR_F_OPENED;
1610         }
1611
1612         monitor_subsys_opened = 1;
1613
1614         if ( mi->mi_entry_limbo ) {
1615                 entry_limbo_t   *el = (entry_limbo_t *)mi->mi_entry_limbo;
1616
1617                 for ( ; el; ) {
1618                         entry_limbo_t   *tmp;
1619
1620                         switch ( el->el_type ) {
1621                         case LIMBO_ENTRY:
1622                                 monitor_back_register_entry(
1623                                                 el->el_e,
1624                                                 el->el_cb );
1625                                 break;
1626
1627                         case LIMBO_ENTRY_PARENT:
1628                                 monitor_back_register_entry_parent(
1629                                                 el->el_e,
1630                                                 el->el_cb,
1631                                                 &el->el_base,
1632                                                 el->el_scope,
1633                                                 &el->el_filter );
1634                                 break;
1635                                 
1636
1637                         case LIMBO_ATTRS:
1638                                 monitor_back_register_entry_attrs(
1639                                                 &el->el_ndn,
1640                                                 el->el_a,
1641                                                 el->el_cb,
1642                                                 &el->el_base,
1643                                                 el->el_scope,
1644                                                 &el->el_filter );
1645                                 break;
1646
1647                         case LIMBO_CB:
1648                                 monitor_back_register_entry_callback(
1649                                                 &el->el_ndn,
1650                                                 el->el_cb,
1651                                                 &el->el_base,
1652                                                 el->el_scope,
1653                                                 &el->el_filter );
1654                                 break;
1655
1656                         default:
1657                                 assert( 0 );
1658                         }
1659
1660                         if ( el->el_e ) {
1661                                 entry_free( el->el_e );
1662                         }
1663                         if ( el->el_a ) {
1664                                 attrs_free( el->el_a );
1665                         }
1666                         if ( !BER_BVISNULL( &el->el_ndn ) ) {
1667                                 ber_memfree( el->el_ndn.bv_val );
1668                         }
1669                         if ( !BER_BVISNULL( &el->el_base ) ) {
1670                                 ber_memfree( el->el_base.bv_val );
1671                         }
1672                         if ( !BER_BVISNULL( &el->el_filter ) ) {
1673                                 ber_memfree( el->el_filter.bv_val );
1674                         }
1675
1676                         tmp = el;
1677                         el = el->el_next;
1678                         ch_free( tmp );
1679                 }
1680
1681                 mi->mi_entry_limbo = NULL;
1682         }
1683
1684         return( 0 );
1685 }
1686
1687 int
1688 monitor_back_config(
1689         BackendInfo     *bi,
1690         const char      *fname,
1691         int             lineno,
1692         int             argc,
1693         char            **argv )
1694 {
1695         /*
1696          * eventually, will hold backend specific configuration parameters
1697          */
1698         return SLAP_CONF_UNKNOWN;
1699 }
1700
1701 int
1702 monitor_back_db_config(
1703         Backend     *be,
1704         const char  *fname,
1705         int         lineno,
1706         int         argc,
1707         char        **argv )
1708 {
1709         monitor_info_t  *mi = ( monitor_info_t * )be->be_private;
1710
1711         /*
1712          * eventually, will hold database specific configuration parameters
1713          */
1714         return SLAP_CONF_UNKNOWN;
1715 }
1716
1717 int
1718 monitor_back_db_destroy(
1719         BackendDB       *be )
1720 {
1721         monitor_info_t  *mi = ( monitor_info_t * )be->be_private;
1722
1723         /*
1724          * FIXME: destroys all the data
1725          */
1726         /* NOTE: mi points to static storage; don't free it */
1727         
1728         return 0;
1729 }
1730
1731 #if SLAPD_MONITOR == SLAPD_MOD_DYNAMIC
1732
1733 /* conditionally define the init_module() function */
1734 SLAP_BACKEND_INIT_MODULE( monitor )
1735
1736 #endif /* SLAPD_MONITOR == SLAPD_MOD_DYNAMIC */
1737