]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/init.c
allow to register entries and entry callbacks; radical namespace 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-2004 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 BackendDB *be_monitor = NULL;
38
39 static struct monitor_subsys_t  **monitor_subsys = NULL;
40 static int                      monitor_subsys_opened = 0;
41
42 /*
43  * subsystem data
44  *
45  * the known subsystems are added to the subsystems
46  * array at backend initialization; other subsystems
47  * may be added by calling monitor_back_register_subsys()
48  * before the database is opened (e.g. by other backends
49  * or by overlays or modules).
50  */
51 static struct monitor_subsys_t known_monitor_subsys[] = {
52         { 
53                 SLAPD_MONITOR_BACKEND_NAME, 
54                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
55                 MONITOR_F_PERSISTENT_CH,
56                 monitor_subsys_backend_init,
57                 NULL,   /* update */
58                 NULL,   /* create */
59                 NULL    /* modify */
60         }, { 
61                 SLAPD_MONITOR_CONN_NAME,
62                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
63                 MONITOR_F_VOLATILE_CH,
64                 monitor_subsys_conn_init,
65                 monitor_subsys_conn_update,
66                 monitor_subsys_conn_create,
67                 NULL    /* modify */
68         }, { 
69                 SLAPD_MONITOR_DATABASE_NAME,    
70                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
71                 MONITOR_F_PERSISTENT_CH,
72                 monitor_subsys_database_init,
73                 NULL,   /* update */
74                 NULL,   /* create */
75                 monitor_subsys_database_modify
76         }, { 
77                 SLAPD_MONITOR_LISTENER_NAME,    
78                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
79                 MONITOR_F_PERSISTENT_CH,
80                 monitor_subsys_listener_init,
81                 NULL,   /* update */
82                 NULL,   /* create */
83                 NULL    /* modify */
84         }, { 
85                 SLAPD_MONITOR_LOG_NAME,
86                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
87                 MONITOR_F_NONE,
88                 monitor_subsys_log_init,
89                 NULL,   /* update */
90                 NULL,   /* create */
91                 monitor_subsys_log_modify
92         }, { 
93                 SLAPD_MONITOR_OPS_NAME,
94                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
95                 MONITOR_F_PERSISTENT_CH,
96                 monitor_subsys_ops_init,
97                 monitor_subsys_ops_update,
98                 NULL,   /* create */
99                 NULL,   /* modify */
100         }, { 
101                 SLAPD_MONITOR_OVERLAY_NAME,
102                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
103                 MONITOR_F_PERSISTENT_CH,
104                 monitor_subsys_overlay_init,
105                 NULL,   /* update */
106                 NULL,   /* create */
107                 NULL,   /* modify */
108         }, { 
109                 SLAPD_MONITOR_SASL_NAME,        
110                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
111                 MONITOR_F_NONE,
112                 NULL,   /* init */
113                 NULL,   /* update */
114                 NULL,   /* create */
115                 NULL    /* modify */
116         }, { 
117                 SLAPD_MONITOR_SENT_NAME,
118                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
119                 MONITOR_F_PERSISTENT_CH,
120                 monitor_subsys_sent_init,
121                 monitor_subsys_sent_update,
122                 NULL,   /* create */
123                 NULL,   /* modify */
124         }, { 
125                 SLAPD_MONITOR_THREAD_NAME,      
126                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
127                 MONITOR_F_PERSISTENT_CH,
128                 monitor_subsys_thread_init,
129                 monitor_subsys_thread_update,
130                 NULL,   /* create */
131                 NULL    /* modify */
132         }, { 
133                 SLAPD_MONITOR_TIME_NAME,
134                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
135                 MONITOR_F_PERSISTENT_CH,
136                 monitor_subsys_time_init,
137                 monitor_subsys_time_update,
138                 NULL,   /* create */
139                 NULL,   /* modify */
140         }, { 
141                 SLAPD_MONITOR_TLS_NAME,
142                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
143                 MONITOR_F_NONE,
144                 NULL,   /* init */
145                 NULL,   /* update */
146                 NULL,   /* create */
147                 NULL    /* modify */
148         }, { 
149                 SLAPD_MONITOR_RWW_NAME,
150                 BER_BVNULL, BER_BVNULL, BER_BVNULL,
151                 MONITOR_F_PERSISTENT_CH,
152                 monitor_subsys_rww_init,
153                 monitor_subsys_rww_update,
154                 NULL,   /* create */
155                 NULL    /* modify */
156         }, { NULL }
157 };
158
159 #if SLAPD_MONITOR == SLAPD_MOD_DYNAMIC
160
161 int
162 init_module( int argc, char *argv[] )
163 {
164         BackendInfo bi;
165
166         memset( &bi, '\0', sizeof(bi) );
167         bi.bi_type = "monitor";
168         bi.bi_init = monitor_back_initialize;
169         backend_add( &bi );
170         return 0;
171 }
172
173 #endif /* SLAPD_MONITOR */
174
175 int
176 monitor_back_register_subsys( monitor_subsys_t *ms )
177 {
178         int     i = 0;
179
180         if ( monitor_subsys ) {
181                 for ( ; monitor_subsys[ i ] != NULL; i++ )
182                         /* just count'em */ ;
183         }
184
185         monitor_subsys = ch_realloc( monitor_subsys,
186                         ( 2 + i ) * sizeof( monitor_subsys_t * ) );
187
188         if ( monitor_subsys == NULL ) {
189                 return -1;
190         }
191
192         monitor_subsys[ i ] = ms;
193         monitor_subsys[ i + 1 ] = NULL;
194
195         /* if a subsystem is registered __AFTER__ subsystem 
196          * initialization (depending on the sequence the databases
197          * are listed in slapd.conf), init it */
198         if ( monitor_subsys_opened ) {
199
200                 /* FIXME: this should only be possible
201                  * if be_monitor is already initialized */
202                 assert( be_monitor );
203
204                 if ( ms->mss_open && ( *ms->mss_open )( be_monitor, ms ) ) {
205                         return -1;
206                 }
207
208                 ms->mss_flags |= MONITOR_F_OPENED;
209         }
210
211         return 0;
212 }
213
214 typedef struct entry_limbo_t {
215         struct berval           el_ndn;
216         Entry                   *el_e;
217         monitor_callback_t      *el_cb;
218         void                    *el_private;
219         struct entry_limbo_t    *el_next;
220 } entry_limbo_t;
221
222 int
223 monitor_back_register_entry(
224                 Entry                   *e,
225                 monitor_callback_t      *cb,
226                 void                    *private )
227 {
228         monitor_info_t  *mi = ( monitor_info_t * )be_monitor->be_private;
229
230         assert( mi != NULL );
231         assert( e != NULL );
232         assert( e->e_private == NULL );
233         
234         if ( monitor_subsys_opened ) {
235                 Entry                   *e2 = NULL,
236                                         **ep = NULL;
237                 struct berval           pdn = BER_BVNULL;
238                 monitor_entry_t *mp = NULL,
239                                         *pmp = NULL;
240
241                 if ( monitor_cache_get( mi, &e->e_nname, &e2 ) == 0 ) {
242                         /* entry exists */
243                         Debug( LDAP_DEBUG_ANY,
244                                 "monitor_back_register_entry(\"%s\"): "
245                                 "entry exists\n",
246                                 e->e_name.bv_val, 0, 0 );
247                         monitor_cache_release( mi, e2 );
248                         return -1;
249                 }
250
251                 dnParent( &e->e_nname, &pdn );
252                 if ( monitor_cache_get( mi, &pdn, &e2 ) != 0 ) {
253                         /* parent does not exist */
254                         Debug( LDAP_DEBUG_ANY,
255                                 "monitor_back_register_entry(\"%s\"): "
256                                 "parent \"%s\" not found\n",
257                                 e->e_name.bv_val, pdn.bv_val, 0 );
258                         return -1;
259                 }
260
261                 assert( e2->e_private != NULL );
262                 pmp = ( monitor_entry_t * )e2->e_private;
263
264                 if ( pmp->mp_flags & MONITOR_F_VOLATILE ) {
265                         /* entry is volatile; cannot append children */
266                         Debug( LDAP_DEBUG_ANY,
267                                 "monitor_back_register_entry(\"%s\"): "
268                                 "parent \"%s\" is volatile\n",
269                                 e->e_name.bv_val, e2->e_name.bv_val, 0 );
270                         monitor_cache_release( mi, e2 );
271                         return -1;
272                 }
273
274                 mp = monitor_entrypriv_create();
275                 if ( mp == NULL ) {
276                         Debug( LDAP_DEBUG_ANY,
277                                 "monitor_back_register_entry(\"%s\"): "
278                                 "monitor_entrypriv_create failed\n",
279                                 e->e_name.bv_val, 0, 0 );
280                         return -1;
281                 }
282                 e->e_private = ( void * )mp;
283                 mp->mp_info = pmp->mp_info;
284                 mp->mp_flags = pmp->mp_flags | MONITOR_F_SUB;
285
286                 ep = &pmp->mp_children;
287                 for ( ; *ep; ) {
288                         pmp = ( monitor_entry_t * )(*ep)->e_private;
289                         ep = &pmp->mp_next;
290                 }
291                 *ep = e;
292
293                 if ( monitor_cache_add( mi, e ) ) {
294                         Debug( LDAP_DEBUG_ANY,
295                                 "monitor_back_register_entry(\"%s\"): "
296                                 "unable to add entry\n",
297                                 e->e_name.bv_val, 0, 0 );
298                         ch_free( e->e_private );
299                         e->e_private = NULL;
300                         return( -1 );
301                 }
302
303         } else {
304                 entry_limbo_t           *el;
305
306                 el = (entry_limbo_t *)ch_malloc( sizeof( entry_limbo_t ) );
307
308                 BER_BVZERO( &el->el_ndn );
309                 el->el_e = e;
310                 el->el_cb = cb;
311                 el->el_private = private;
312                 el->el_next = (entry_limbo_t *)mi->mi_entry_limbo;
313         }
314
315         return 0;
316 }
317
318 int
319 monitor_back_register_entry_callback(
320                 struct berval           *ndn,
321                 struct monitor_callback_t       *cb,
322                 void                    *private )
323 {
324         monitor_info_t  *mi = ( monitor_info_t * )be_monitor->be_private;
325
326         assert( mi != NULL );
327         assert( ndn != NULL );
328         assert( !BER_BVISNULL( ndn ) );
329         
330         if ( monitor_subsys_opened ) {
331                 Entry                   *e = NULL;
332                 monitor_entry_t *mp = NULL;
333                 struct monitor_callback_t       **mcp = NULL;
334
335                 if ( monitor_cache_get( mi, ndn, &e ) != 0 ) {
336                         /* entry does not exist */
337                         Debug( LDAP_DEBUG_ANY,
338                                 "monitor_back_register_entry_callback(\"%s\"): "
339                                 "entry does not exist\n",
340                                 e->e_name.bv_val, 0, 0 );
341                         return -1;
342                 }
343
344                 assert( e->e_private != NULL );
345                 mp = ( monitor_entry_t * )e->e_private;
346
347                 if ( mp->mp_flags & MONITOR_F_VOLATILE ) {
348                         /* entry is volatile; cannot append callback */
349                         Debug( LDAP_DEBUG_ANY,
350                                 "monitor_back_register_entry_callback(\"%s\"): "
351                                 "entry is volatile\n",
352                                 e->e_name.bv_val, 0, 0 );
353                         monitor_cache_release( mi, e );
354                         return -1;
355                 }
356
357                 for ( mcp = &mp->mp_cb; *mcp; mcp = &(*mcp)->mc_next )
358                         /* go to tail */ ;
359                 
360                 /* NOTE: we do not clear cb->mc_next, so this function
361                  * can be used to append a list of callbacks */
362                 (*mcp) = cb;
363
364                 monitor_cache_release( mi, e );
365
366         } else {
367                 entry_limbo_t           *el;
368
369                 el = (entry_limbo_t *)ch_malloc( sizeof( entry_limbo_t ) );
370
371                 ber_dupbv( &el->el_ndn, ndn );
372                 el->el_e = NULL;
373                 el->el_cb = cb;
374                 el->el_private = private;
375                 el->el_next = (entry_limbo_t *)mi->mi_entry_limbo;
376         }
377
378         return 0;
379 }
380
381 monitor_subsys_t *
382 monitor_back_get_subsys( const char *name )
383 {
384         if ( monitor_subsys != NULL ) {
385                 int     i;
386                 
387                 for ( i = 0; monitor_subsys[ i ] != NULL; i++ ) {
388                         if ( strcasecmp( monitor_subsys[ i ]->mss_name, name ) == 0 ) {
389                                 return monitor_subsys[ i ];
390                         }
391                 }
392         }
393
394         return NULL;
395 }
396
397 monitor_subsys_t *
398 monitor_back_get_subsys_by_dn( struct berval *ndn, int sub )
399 {
400         if ( monitor_subsys != NULL ) {
401                 int     i;
402
403                 if ( sub ) {
404                         for ( i = 0; monitor_subsys[ i ] != NULL; i++ ) {
405                                 if ( dnIsSuffix( ndn, &monitor_subsys[ i ]->mss_ndn ) ) {
406                                         return monitor_subsys[ i ];
407                                 }
408                         }
409
410                 } else {
411                         for ( i = 0; monitor_subsys[ i ] != NULL; i++ ) {
412                                 if ( dn_match( ndn, &monitor_subsys[ i ]->mss_ndn ) ) {
413                                         return monitor_subsys[ i ];
414                                 }
415                         }
416                 }
417         }
418
419         return NULL;
420 }
421
422 int
423 monitor_back_initialize(
424         BackendInfo     *bi
425 )
426 {
427         monitor_subsys_t        *ms;
428         static char             *controls[] = {
429                 LDAP_CONTROL_MANAGEDSAIT,
430                 LDAP_CONTROL_VALUESRETURNFILTER,
431                 NULL
432         };
433
434         bi->bi_controls = controls;
435
436         bi->bi_init = 0;
437         bi->bi_open = 0;
438         bi->bi_config = monitor_back_config;
439         bi->bi_close = 0;
440         bi->bi_destroy = 0;
441
442         bi->bi_db_init = monitor_back_db_init;
443         bi->bi_db_config = monitor_back_db_config;
444         bi->bi_db_open = monitor_back_db_open;
445         bi->bi_db_close = 0;
446         bi->bi_db_destroy = monitor_back_db_destroy;
447
448         bi->bi_op_bind = monitor_back_bind;
449         bi->bi_op_unbind = 0;
450         bi->bi_op_search = monitor_back_search;
451         bi->bi_op_compare = monitor_back_compare;
452         bi->bi_op_modify = monitor_back_modify;
453         bi->bi_op_modrdn = 0;
454         bi->bi_op_add = 0;
455         bi->bi_op_delete = 0;
456         bi->bi_op_abandon = 0;
457
458         bi->bi_extended = 0;
459
460         bi->bi_entry_release_rw = 0;
461         bi->bi_chk_referrals = 0;
462         bi->bi_operational = monitor_back_operational;
463
464         /*
465          * hooks for slap tools
466          */
467         bi->bi_tool_entry_open = 0;
468         bi->bi_tool_entry_close = 0;
469         bi->bi_tool_entry_first = 0;
470         bi->bi_tool_entry_next = 0;
471         bi->bi_tool_entry_get = 0;
472         bi->bi_tool_entry_put = 0;
473         bi->bi_tool_entry_reindex = 0;
474         bi->bi_tool_sync = 0;
475         bi->bi_tool_dn2id_get = 0;
476         bi->bi_tool_id2entry_get = 0;
477         bi->bi_tool_entry_modify = 0;
478
479         bi->bi_connection_init = 0;
480         bi->bi_connection_destroy = 0;
481
482         for ( ms = known_monitor_subsys; ms->mss_name != NULL; ms++ ) {
483                 if ( monitor_back_register_subsys( ms ) ) {
484                         return -1;
485                 }
486         }
487
488         return 0;
489 }
490
491 int
492 monitor_back_db_init(
493         BackendDB       *be
494 )
495 {
496         monitor_info_t  *mi;
497         int             i, rc;
498         struct berval   dn, ndn;
499         struct berval   bv;
500         const char      *text;
501
502         struct m_s {
503                 char    *name;
504                 char    *schema;
505                 slap_mask_t flags;
506                 int     offset;
507         } moc[] = {
508                 { "monitor", "( 1.3.6.1.4.1.4203.666.3.2 "
509                         "NAME 'monitor' "
510                         "DESC 'OpenLDAP system monitoring' "
511                         "SUP top STRUCTURAL "
512                         "MUST cn "
513                         "MAY ( "
514                                 "description "
515                                 "$ l "
516 #if 0   /* temporarily disabled */
517                                 "$ st "
518                                 "$ street "
519                                 "$ postalAddress "
520                                 "$ postalCode "
521 #endif
522                                 "$ seeAlso "
523                                 "$ labeledURI "
524                                 "$ monitoredInfo "
525                                 "$ managedInfo "
526                                 "$ monitorOverlay "
527                         ") )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
528                         offsetof(monitor_info_t, mi_oc_monitor) },
529                 { "monitorServer", "( 1.3.6.1.4.1.4203.666.3.7 "
530                         "NAME 'monitorServer' "
531                         "DESC 'Server monitoring root entry' "
532                         "SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
533                         offsetof(monitor_info_t, mi_oc_monitorServer) },
534                 { "monitorContainer", "( 1.3.6.1.4.1.4203.666.3.8 "
535                         "NAME 'monitorContainer' "
536                         "DESC 'monitor container class' "
537                         "SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
538                         offsetof(monitor_info_t, mi_oc_monitorContainer) },
539                 { "monitorCounterObject", "( 1.3.6.1.4.1.4203.666.3.9 "
540                         "NAME 'monitorCounterObject' "
541                         "DESC 'monitor counter class' "
542                         "SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
543                         offsetof(monitor_info_t, mi_oc_monitorCounterObject) },
544                 { "monitorOperation", "( 1.3.6.1.4.1.4203.666.3.10 "
545                         "NAME 'monitorOperation' "
546                         "DESC 'monitor operation class' "
547                         "SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
548                         offsetof(monitor_info_t, mi_oc_monitorOperation) },
549                 { "monitorConnection", "( 1.3.6.1.4.1.4203.666.3.11 "
550                         "NAME 'monitorConnection' "
551                         "DESC 'monitor connection class' "
552                         "SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
553                         offsetof(monitor_info_t, mi_oc_monitorConnection) },
554                 { "managedObject", "( 1.3.6.1.4.1.4203.666.3.12 "
555                         "NAME 'managedObject' "
556                         "DESC 'monitor managed entity class' "
557                         "SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
558                         offsetof(monitor_info_t, mi_oc_managedObject) },
559                 { "monitoredObject", "( 1.3.6.1.4.1.4203.666.3.13 "
560                         "NAME 'monitoredObject' "
561                         "DESC 'monitor monitored entity class' "
562                         "SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
563                         offsetof(monitor_info_t, mi_oc_monitoredObject) },
564                 { NULL, NULL, 0, -1 }
565         }, mat[] = {
566                 { "monitoredInfo", "( 1.3.6.1.4.1.4203.666.1.14 "
567                         "NAME 'monitoredInfo' "
568                         "DESC 'monitored info' "
569                         /* "SUP name " */
570                         "EQUALITY caseIgnoreMatch "
571                         "SUBSTR caseIgnoreSubstringsMatch "
572                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15{32768} "
573                         "NO-USER-MODIFICATION "
574                         "USAGE directoryOperation )", SLAP_AT_HIDE,
575                         offsetof(monitor_info_t, mi_ad_monitoredInfo) },
576                 { "managedInfo", "( 1.3.6.1.4.1.4203.666.1.15 "
577                         "NAME 'managedInfo' "
578                         "DESC 'monitor managed info' "
579                         "SUP name )", SLAP_AT_HIDE,
580                         offsetof(monitor_info_t, mi_ad_managedInfo) },
581                 { "monitorCounter", "( 1.3.6.1.4.1.4203.666.1.16 "
582                         "NAME 'monitorCounter' "
583                         "DESC 'monitor counter' "
584                         "EQUALITY integerMatch "
585                         "ORDERING integerOrderingMatch "
586                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 "
587                         "NO-USER-MODIFICATION "
588                         "USAGE directoryOperation )", SLAP_AT_HIDE,
589                         offsetof(monitor_info_t, mi_ad_monitorCounter) },
590                 { "monitorOpCompleted", "( 1.3.6.1.4.1.4203.666.1.17 "
591                         "NAME 'monitorOpCompleted' "
592                         "DESC 'monitor completed operations' "
593                         "SUP monitorCounter "
594                         "NO-USER-MODIFICATION "
595                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
596                         offsetof(monitor_info_t, mi_ad_monitorOpCompleted) },
597                 { "monitorOpInitiated", "( 1.3.6.1.4.1.4203.666.1.18 "
598                         "NAME 'monitorOpInitiated' "
599                         "DESC 'monitor initiated operations' "
600                         "SUP monitorCounter "
601                         "NO-USER-MODIFICATION "
602                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
603                         offsetof(monitor_info_t, mi_ad_monitorOpInitiated) },
604                 { "monitorConnectionNumber", "( 1.3.6.1.4.1.4203.666.1.19 "
605                         "NAME 'monitorConnectionNumber' "
606                         "DESC 'monitor connection number' "
607                         "SUP monitorCounter "
608                         "NO-USER-MODIFICATION "
609                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
610                         offsetof(monitor_info_t, mi_ad_monitorConnectionNumber) },
611                 { "monitorConnectionAuthzDN", "( 1.3.6.1.4.1.4203.666.1.20 "
612                         "NAME 'monitorConnectionAuthzDN' "
613                         "DESC 'monitor connection authorization DN' "
614                         /* "SUP distinguishedName " */
615                         "EQUALITY distinguishedNameMatch "
616                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.12 "
617                         "NO-USER-MODIFICATION "
618                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
619                         offsetof(monitor_info_t, mi_ad_monitorConnectionAuthzDN) },
620                 { "monitorConnectionLocalAddress", "( 1.3.6.1.4.1.4203.666.1.21 "
621                         "NAME 'monitorConnectionLocalAddress' "
622                         "DESC 'monitor connection local address' "
623                         "SUP monitoredInfo "
624                         "NO-USER-MODIFICATION "
625                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
626                         offsetof(monitor_info_t, mi_ad_monitorConnectionLocalAddress) },
627                 { "monitorConnectionPeerAddress", "( 1.3.6.1.4.1.4203.666.1.22 "
628                         "NAME 'monitorConnectionPeerAddress' "
629                         "DESC 'monitor connection peer address' "
630                         "SUP monitoredInfo "
631                         "NO-USER-MODIFICATION "
632                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
633                         offsetof(monitor_info_t, mi_ad_monitorConnectionPeerAddress) },
634                 { "monitorTimestamp", "( 1.3.6.1.4.1.4203.666.1.24 "
635                         "NAME 'monitorTimestamp' "
636                         "DESC 'monitor timestamp' "
637                         "EQUALITY generalizedTimeMatch "
638                         "ORDERING generalizedTimeOrderingMatch "
639                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
640                         "SINGLE-VALUE "
641                         "NO-USER-MODIFICATION "
642                         "USAGE directoryOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
643                         offsetof(monitor_info_t, mi_ad_monitorTimestamp) },
644                 { "monitorOverlay", "( 1.3.6.1.4.1.4203.666.1.27 "
645                         "NAME 'monitorOverlay' "
646                         "DESC 'name of overlays defined for a give database' "
647                         "SUP monitoredInfo "
648                         "NO-USER-MODIFICATION "
649                         "USAGE directoryOperation )", SLAP_AT_HIDE,
650                         offsetof(monitor_info_t, mi_ad_monitorOverlay) },
651                 { "readOnly", "( 1.3.6.1.4.1.4203.666.1.31 "
652                         "NAME 'readOnly' "
653                         "DESC 'read/write status of a given database' "
654                         "EQUALITY booleanMatch "
655                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.7 "
656                         "SINGLE-VALUE "
657                         "USAGE directoryOperation )", SLAP_AT_HIDE,
658                         offsetof(monitor_info_t, mi_ad_readOnly) },
659                 { "restrictedOperation", "( 1.3.6.1.4.1.4203.666.1.32 "
660                         "NAME 'restrictedOperation' "
661                         "DESC 'name of restricted operation for a given database' "
662                         "SUP managedInfo )", SLAP_AT_HIDE,
663                         offsetof(monitor_info_t, mi_ad_restrictedOperation ) },
664 #ifdef INTEGRATE_CORE_SCHEMA
665                 { NULL, NULL, 0, -1 },  /* description */
666                 { NULL, NULL, 0, -1 },  /* seeAlso */
667                 { NULL, NULL, 0, -1 },  /* l */
668                 { NULL, NULL, 0, -1 },  /* labeledURI */
669 #endif /* INTEGRATE_CORE_SCHEMA */
670                 { NULL, NULL, 0, -1 }
671         }, mat_core[] = {
672                 { "description", "( 2.5.4.13 "
673                         "NAME 'description' "
674                         "DESC 'RFC2256: descriptive information' "
675                         "EQUALITY caseIgnoreMatch "
676                         "SUBSTR caseIgnoreSubstringsMatch "
677                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15{1024} )", 0,
678                         offsetof(monitor_info_t, mi_ad_description) },
679                 { "seeAlso", "( 2.5.4.34 "
680                         "NAME 'seeAlso' "
681                         "DESC 'RFC2256: DN of related object' "
682                         "SUP distinguishedName )", 0,
683                         offsetof(monitor_info_t, mi_ad_seeAlso) },
684                 { "l", "( 2.5.4.7 "
685                         "NAME ( 'l' 'localityName' ) "
686                         "DESC 'RFC2256: locality which this object resides in' "
687                         "SUP name )", 0,
688                         offsetof(monitor_info_t, mi_ad_l) },
689                 { "labeledURI", "( 1.3.6.1.4.1.250.1.57 "
690                         "NAME 'labeledURI' "
691                         "DESC 'RFC2079: Uniform Resource Identifier with optional label' "
692                         "EQUALITY caseExactMatch "
693                         "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )", 0,
694                         offsetof(monitor_info_t, mi_ad_labeledURI) },
695                 { NULL, NULL, 0, -1 }
696         };
697         
698         /*
699          * database monitor can be defined once only
700          */
701         if ( be_monitor ) {
702                 Debug( LDAP_DEBUG_ANY,
703                         "only one monitor backend is allowed\n", 0, 0, 0 );
704                 return( -1 );
705         }
706         be_monitor = be;
707
708         /* indicate system schema supported */
709         SLAP_BFLAGS(be) |= SLAP_BFLAG_MONITOR;
710
711         dn.bv_val = SLAPD_MONITOR_DN;
712         dn.bv_len = sizeof( SLAPD_MONITOR_DN ) - 1;
713
714         rc = dnNormalize( 0, NULL, NULL, &dn, &ndn, NULL );
715         if( rc != LDAP_SUCCESS ) {
716                 Debug( LDAP_DEBUG_ANY,
717                         "unable to normalize monitor DN \"%s\"\n",
718                         SLAPD_MONITOR_DN, 0, 0 );
719                 return -1;
720         }
721
722         ber_dupbv( &bv, &dn );
723         ber_bvarray_add( &be->be_suffix, &bv );
724         ber_bvarray_add( &be->be_nsuffix, &ndn );
725
726         mi = ( monitor_info_t * )ch_calloc( sizeof( monitor_info_t ), 1 );
727         if ( mi == NULL ) {
728                 Debug( LDAP_DEBUG_ANY,
729                         "unable to initialize monitor backend\n", 0, 0, 0 );
730                 return -1;
731         }
732
733         memset( mi, 0, sizeof( monitor_info_t ) );
734
735         ldap_pvt_thread_mutex_init( &mi->mi_cache_mutex );
736
737         be->be_private = mi;
738         
739 #ifdef INTEGRATE_CORE_SCHEMA
740         /* prepare for schema integration */
741         for ( k = 0; mat[ k ].name != NULL; k++ );
742 #endif /* INTEGRATE_CORE_SCHEMA */
743
744         for ( i = 0; mat_core[ i ].name != NULL; i++ ) {
745                 AttributeDescription    **ad;
746                 const char              *text;
747
748                 ad = ((AttributeDescription **)&(((char *)mi)[ mat_core[ i ].offset ]));
749                 ad[ 0 ] = NULL;
750
751                 switch (slap_str2ad( mat_core[ i ].name, ad, &text ) ) {
752                 case LDAP_SUCCESS:
753                         break;
754
755 #ifdef INTEGRATE_CORE_SCHEMA
756                 case LDAP_UNDEFINED_TYPE:
757                         mat[ k ] = mat_core[ i ];
758                         k++;
759                         break;
760 #endif /* INTEGRATE_CORE_SCHEMA */
761
762                 default:
763                         Debug( LDAP_DEBUG_ANY,
764                                 "monitor_back_db_init: %s: %s\n",
765                                 mat_core[ i ].name, text, 0 );
766                         return( -1 );
767                 }
768         }
769
770         /* schema integration */
771         for ( i = 0; mat[ i ].name; i++ ) {
772                 LDAPAttributeType       *at;
773                 int                     code;
774                 const char              *err;
775                 AttributeDescription    **ad;
776
777                 at = ldap_str2attributetype( mat[ i ].schema, &code,
778                         &err, LDAP_SCHEMA_ALLOW_ALL );
779                 if ( !at ) {
780                         Debug( LDAP_DEBUG_ANY, "monitor_back_db_init: "
781                                 "in AttributeType \"%s\" %s before %s\n",
782                                 mat[ i ].name, ldap_scherr2str(code), err );
783                         return -1;
784                 }
785
786                 if ( at->at_oid == NULL ) {
787                         Debug( LDAP_DEBUG_ANY, "monitor_back_db_init: "
788                                 "null OID for attributeType \"%s\"\n",
789                                 mat[ i ].name, 0, 0 );
790                         return -1;
791                 }
792
793                 code = at_add(at, &err);
794                 if ( code ) {
795                         Debug( LDAP_DEBUG_ANY, "monitor_back_db_init: "
796                                 "%s in attributeType \"%s\"\n",
797                                 scherr2str(code), mat[ i ].name, 0 );
798                         return -1;
799                 }
800                 ldap_memfree(at);
801
802                 ad = ((AttributeDescription **)&(((char *)mi)[ mat[ i ].offset ]));
803                 ad[ 0 ] = NULL;
804                 if ( slap_str2ad( mat[ i ].name, ad, &text ) ) {
805                         Debug( LDAP_DEBUG_ANY,
806                                 "monitor_back_db_init: %s\n", text, 0, 0 );
807                         return -1;
808                 }
809
810                 (*ad)->ad_type->sat_flags |= mat[ i ].flags;
811         }
812
813         for ( i = 0; moc[ i ].name; i++ ) {
814                 LDAPObjectClass         *oc;
815                 int                     code;
816                 const char              *err;
817                 ObjectClass             *Oc;
818
819                 oc = ldap_str2objectclass(moc[ i ].schema, &code, &err,
820                                 LDAP_SCHEMA_ALLOW_ALL );
821                 if ( !oc ) {
822                         Debug( LDAP_DEBUG_ANY,
823                                 "unable to parse monitor objectclass \"%s\": "
824                                 "%s before %s\n" , moc[ i ].name,
825                                 ldap_scherr2str(code), err );
826                         return -1;
827                 }
828
829                 if ( oc->oc_oid == NULL ) {
830                         Debug( LDAP_DEBUG_ANY,
831                                 "objectclass \"%s\" has no OID\n" ,
832                                 moc[ i ].name, 0, 0 );
833                         return -1;
834                 }
835
836                 code = oc_add(oc, 0, &err);
837                 if ( code ) {
838                         Debug( LDAP_DEBUG_ANY,
839                                 "objectclass \"%s\": %s \"%s\"\n" ,
840                                 moc[ i ].name, scherr2str(code), err );
841                         return -1;
842                 }
843
844                 ldap_memfree(oc);
845
846                 Oc = oc_find( moc[ i ].name );
847                 if ( Oc == NULL ) {
848                         Debug( LDAP_DEBUG_ANY, "monitor_back_db_init: "
849                                         "unable to find objectClass %s "
850                                         "(just added)\n", moc[ i ].name, 0, 0 );
851                         return -1;
852                 }
853
854                 Oc->soc_flags |= moc[ i ].flags;
855
856                 ((ObjectClass **)&(((char *)mi)[ moc[ i ].offset ]))[ 0 ] = Oc;
857         }
858
859         return 0;
860 }
861
862 int
863 monitor_back_db_open(
864         BackendDB       *be
865 )
866 {
867         monitor_info_t          *mi = (monitor_info_t *)be->be_private;
868         struct monitor_subsys_t **ms;
869         Entry                   *e, **ep;
870         monitor_entry_t         *mp;
871         int                     i;
872         char                    buf[ BACKMONITOR_BUFSIZE ],
873                                 *end_of_line;
874         struct berval           bv;
875         struct tm               *tms;
876 #ifdef HAVE_GMTIME_R
877         struct tm               tm_buf;
878 #endif
879         static char             tmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
880
881         /*
882          * Start
883          */
884 #ifndef HAVE_GMTIME_R
885         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
886 #endif
887 #ifdef HACK_LOCAL_TIME
888 # ifdef HAVE_LOCALTIME_R
889         tms = localtime_r( &starttime, &tm_buf );
890 # else
891         tms = localtime( &starttime );
892 # endif /* HAVE_LOCALTIME_R */
893         lutil_localtime( tmbuf, sizeof(tmbuf), tms, -timezone );
894 #else /* !HACK_LOCAL_TIME */
895 # ifdef HAVE_GMTIME_R
896         tms = gmtime_r( &starttime, &tm_buf );
897 # else
898         tms = gmtime( &starttime );
899 # endif /* HAVE_GMTIME_R */
900         lutil_gentime( tmbuf, sizeof(tmbuf), tms );
901 #endif /* !HACK_LOCAL_TIME */
902 #ifndef HAVE_GMTIME_R
903         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
904 #endif
905
906         mi->mi_startTime.bv_val = tmbuf;
907         mi->mi_startTime.bv_len = strlen( tmbuf );
908
909         if ( BER_BVISEMPTY( &be->be_rootdn ) ) {
910                 BER_BVSTR( &mi->mi_creatorsName, SLAPD_ANONYMOUS );
911         } else {
912                 mi->mi_creatorsName = be->be_rootdn;
913         }
914
915         /*
916          * creates the "cn=Monitor" entry 
917          */
918         snprintf( buf, sizeof( buf ), 
919                 "dn: %s\n"
920                 "objectClass: %s\n"
921                 "structuralObjectClass: %s\n"
922                 "cn: Monitor\n"
923                 "%s: This subtree contains monitoring/managing objects.\n"
924                 "%s: This object contains information about this server.\n"
925 #if 0
926                 "%s: createTimestamp reflects the time this server instance was created.\n"
927                 "%s: modifyTimestamp reflects the time this server instance was last accessed.\n"
928 #endif
929                 "creatorsName: %s\n"
930                 "modifiersName: %s\n"
931                 "createTimestamp: %s\n"
932                 "modifyTimestamp: %s\n",
933                 SLAPD_MONITOR_DN,
934                 mi->mi_oc_monitorServer->soc_cname.bv_val,
935                 mi->mi_oc_monitorServer->soc_cname.bv_val,
936                 mi->mi_ad_description->ad_cname.bv_val,
937                 mi->mi_ad_description->ad_cname.bv_val,
938 #if 0
939                 mi->mi_ad_description->ad_cname.bv_val,
940                 mi->mi_ad_description->ad_cname.bv_val,
941 #endif
942                 mi->mi_creatorsName.bv_val,
943                 mi->mi_creatorsName.bv_val,
944                 mi->mi_startTime.bv_val,
945                 mi->mi_startTime.bv_val );
946
947         e = str2entry( buf );
948         if ( e == NULL) {
949                 Debug( LDAP_DEBUG_ANY,
950                         "unable to create \"%s\" entry\n",
951                         SLAPD_MONITOR_DN, 0, 0 );
952                 return( -1 );
953         }
954
955         bv.bv_val = (char *) Versionstr;
956         end_of_line = strchr( Versionstr, '\n' );
957         if ( end_of_line ) {
958                 bv.bv_len = end_of_line - Versionstr;
959         } else {
960                 bv.bv_len = strlen( Versionstr );
961         }
962
963         if ( attr_merge_normalize_one( e, mi->mi_ad_monitoredInfo,
964                                 &bv, NULL ) ) {
965                 Debug( LDAP_DEBUG_ANY,
966                         "unable to add monitoredInfo to \"%s\" entry\n",
967                         SLAPD_MONITOR_DN, 0, 0 );
968                 return( -1 );
969         }
970
971         if ( mi->mi_l.bv_len ) {
972                 if ( attr_merge_normalize_one( e, mi->mi_ad_l, &mi->mi_l, NULL ) ) {
973                         Debug( LDAP_DEBUG_ANY,
974                                 "unable to add locality to \"%s\" entry\n",
975                                 SLAPD_MONITOR_DN, 0, 0 );
976                         return( -1 );
977                 }
978         }
979
980         mp = monitor_entrypriv_create();
981         if ( mp == NULL ) {
982                 return -1;
983         }
984         e->e_private = ( void * )mp;
985         ep = &mp->mp_children;
986
987         if ( monitor_cache_add( mi, e ) ) {
988                 Debug( LDAP_DEBUG_ANY,
989                         "unable to add entry \"%s\" to cache\n",
990                         SLAPD_MONITOR_DN, 0, 0 );
991                 return -1;
992         }
993
994         /*      
995          * Create all the subsystem specific entries
996          */
997         for ( i = 0; monitor_subsys[ i ] != NULL; i++ ) {
998                 int             len = strlen( monitor_subsys[ i ]->mss_name );
999                 struct berval   dn;
1000                 int             rc;
1001
1002                 dn.bv_len = len + sizeof( "cn=" ) - 1;
1003                 dn.bv_val = ch_calloc( sizeof( char ), dn.bv_len + 1 );
1004                 strcpy( dn.bv_val, "cn=" );
1005                 strcat( dn.bv_val, monitor_subsys[ i ]->mss_name );
1006                 rc = dnPretty( NULL, &dn, &monitor_subsys[ i ]->mss_rdn, NULL );
1007                 free( dn.bv_val );
1008                 if ( rc != LDAP_SUCCESS ) {
1009                         Debug( LDAP_DEBUG_ANY,
1010                                 "monitor RDN \"%s\" is invalid\n", 
1011                                 dn.bv_val, 0, 0 );
1012                         return( -1 );
1013                 }
1014
1015                 dn.bv_len += sizeof( SLAPD_MONITOR_DN ); /* 1 for the , */
1016                 dn.bv_val = ch_malloc( dn.bv_len + 1 );
1017                 strcpy( dn.bv_val , monitor_subsys[ i ]->mss_rdn.bv_val );
1018                 strcat( dn.bv_val, "," SLAPD_MONITOR_DN );
1019                 rc = dnPrettyNormal( NULL, &dn, &monitor_subsys[ i ]->mss_dn,
1020                         &monitor_subsys[ i ]->mss_ndn, NULL );
1021                 free( dn.bv_val );
1022                 if ( rc != LDAP_SUCCESS ) {
1023                         Debug( LDAP_DEBUG_ANY,
1024                                 "monitor DN \"%s\" is invalid\n", 
1025                                 dn.bv_val, 0, 0 );
1026                         return( -1 );
1027                 }
1028
1029                 snprintf( buf, sizeof( buf ),
1030                                 "dn: %s\n"
1031                                 "objectClass: %s\n"
1032                                 "structuralObjectClass: %s\n"
1033                                 "cn: %s\n"
1034                                 "creatorsName: %s\n"
1035                                 "modifiersName: %s\n"
1036                                 "createTimestamp: %s\n"
1037                                 "modifyTimestamp: %s\n",
1038                                 monitor_subsys[ i ]->mss_dn.bv_val,
1039                                 mi->mi_oc_monitorContainer->soc_cname.bv_val,
1040                                 mi->mi_oc_monitorContainer->soc_cname.bv_val,
1041                                 monitor_subsys[ i ]->mss_name,
1042                                 mi->mi_creatorsName.bv_val,
1043                                 mi->mi_creatorsName.bv_val,
1044                                 mi->mi_startTime.bv_val,
1045                                 mi->mi_startTime.bv_val );
1046                 
1047                 e = str2entry( buf );
1048                 
1049                 if ( e == NULL) {
1050                         Debug( LDAP_DEBUG_ANY,
1051                                 "unable to create \"%s\" entry\n", 
1052                                 monitor_subsys[ i ]->mss_dn.bv_val, 0, 0 );
1053                         return( -1 );
1054                 }
1055
1056                 mp = monitor_entrypriv_create();
1057                 if ( mp == NULL ) {
1058                         return -1;
1059                 }
1060                 e->e_private = ( void * )mp;
1061                 mp->mp_info = monitor_subsys[ i ];
1062                 mp->mp_flags = monitor_subsys[ i ]->mss_flags;
1063
1064                 if ( monitor_cache_add( mi, e ) ) {
1065                         Debug( LDAP_DEBUG_ANY,
1066                                 "unable to add entry \"%s\" to cache\n",
1067                                 monitor_subsys[ i ]->mss_dn.bv_val, 0, 0 );
1068                         return -1;
1069                 }
1070
1071                 *ep = e;
1072                 ep = &mp->mp_next;
1073         }
1074
1075         assert( be );
1076
1077         be->be_private = mi;
1078         
1079         /*
1080          * opens the monitor backend subsystems
1081          */
1082         for ( ms = monitor_subsys; ms[ 0 ] != NULL; ms++ ) {
1083                 if ( ms[ 0 ]->mss_open && ( *ms[ 0 ]->mss_open )( be, ms[ 0 ] ) )
1084                 {
1085                         return( -1 );
1086                 }
1087                 ms[ 0 ]->mss_flags |= MONITOR_F_OPENED;
1088         }
1089
1090         monitor_subsys_opened = 1;
1091
1092         if ( mi->mi_entry_limbo ) {
1093                 entry_limbo_t   *el = (entry_limbo_t *)mi->mi_entry_limbo;
1094
1095                 for ( ; el; ) {
1096                         entry_limbo_t   *tmp;
1097
1098                         if ( el->el_e ) {
1099                                 monitor_back_register_entry( el->el_e, el->el_cb, el->el_private );
1100
1101                         } else if ( !BER_BVISNULL( &el->el_ndn ) ) {
1102                                 monitor_back_register_entry_callback( &el->el_ndn, el->el_cb, el->el_private );
1103                                 ber_memfree( el->el_ndn.bv_val );
1104                         }
1105
1106                         tmp = el;
1107                         el = el->el_next;
1108                         ch_free( tmp );
1109                 }
1110
1111                 mi->mi_entry_limbo = NULL;
1112         }
1113
1114         return( 0 );
1115 }
1116
1117 int
1118 monitor_back_config(
1119         BackendInfo     *bi,
1120         const char      *fname,
1121         int             lineno,
1122         int             argc,
1123         char            **argv
1124 )
1125 {
1126         /*
1127          * eventually, will hold backend specific configuration parameters
1128          */
1129         return SLAP_CONF_UNKNOWN;
1130 }
1131
1132 int
1133 monitor_back_db_config(
1134         Backend     *be,
1135         const char  *fname,
1136         int         lineno,
1137         int         argc,
1138         char        **argv
1139 )
1140 {
1141         monitor_info_t  *mi = ( monitor_info_t * )be->be_private;
1142
1143         /*
1144          * eventually, will hold database specific configuration parameters
1145          */
1146         if ( strcasecmp( argv[ 0 ], "l" ) == 0 ) {
1147                 if ( argc != 2 ) {
1148                         return 1;
1149                 }
1150                 
1151                 ber_str2bv( argv[ 1 ], 0, 1, &mi->mi_l );
1152
1153         } else {
1154                 return SLAP_CONF_UNKNOWN;
1155         }
1156
1157         return( 0 );
1158 }
1159
1160 int
1161 monitor_back_db_destroy(
1162         BackendDB       *be
1163 )
1164 {
1165         /*
1166          * FIXME: destroys all the data
1167          */
1168         return 0;
1169 }
1170