]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/monitor.c
ITS#7182 Expose the URI used for this connection.
[openldap] / servers / slapd / back-ldap / monitor.c
1 /* monitor.c - monitor ldap backend */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2003-2012 The OpenLDAP Foundation.
6  * Portions Copyright 1999-2003 Howard Chu.
7  * Portions Copyright 2000-2003 Pierangelo Masarati.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted only as authorized by the OpenLDAP
12  * Public License.
13  *
14  * A copy of this license is available in the file LICENSE in the
15  * top-level directory of the distribution or, alternatively, at
16  * <http://www.OpenLDAP.org/license.html>.
17  */
18 /* ACKNOWLEDGEMENTS:
19  * This work was initially developed by the Howard Chu for inclusion
20  * in OpenLDAP Software and subsequently enhanced by Pierangelo
21  * Masarati.
22  */
23
24 #include "portable.h"
25
26 #include <stdio.h>
27 #include <ac/string.h>
28 #include <ac/unistd.h>
29 #include <ac/stdlib.h>
30 #include <ac/errno.h>
31 #include <sys/stat.h>
32 #include "lutil.h"
33 #include "back-ldap.h"
34
35 #include "config.h"
36
37 static ObjectClass              *oc_olmLDAPDatabase;
38 static ObjectClass              *oc_olmLDAPConnection;
39
40 static AttributeDescription     *ad_olmDbURIList;
41 static AttributeDescription     *ad_olmDbOperations;
42 static AttributeDescription     *ad_olmDbBoundDN;
43 static AttributeDescription     *ad_olmDbConnFlags;
44 static AttributeDescription     *ad_olmDbConnURI;
45
46 /*
47  * Stolen from back-monitor/operations.c
48  * We don't need the normalized rdn's though.
49  */
50 struct ldap_back_monitor_ops_t {
51         struct berval           rdn;
52 } ldap_back_monitor_op[] = {
53         { BER_BVC( "cn=Bind" ) },
54         { BER_BVC( "cn=Unbind" ) },
55         { BER_BVC( "cn=Search" ) },
56         { BER_BVC( "cn=Compare" ) },
57         { BER_BVC( "cn=Modify" ) },
58         { BER_BVC( "cn=Modrdn" ) },
59         { BER_BVC( "cn=Add" ) },
60         { BER_BVC( "cn=Delete" ) },
61         { BER_BVC( "cn=Abandon" ) },
62         { BER_BVC( "cn=Extended" ) },
63
64         { BER_BVNULL }
65 };
66
67 /* Corresponds to connection flags in back-ldap.h */
68 static struct {
69         unsigned        flag;
70         struct berval   name;
71 }               s_flag[] = {
72         { LDAP_BACK_FCONN_ISBOUND,      BER_BVC( "bound" ) },
73         { LDAP_BACK_FCONN_ISANON,       BER_BVC( "anonymous" ) },
74         { LDAP_BACK_FCONN_ISPRIV,       BER_BVC( "privileged" ) },
75         { LDAP_BACK_FCONN_ISTLS,        BER_BVC( "TLS" ) },
76         { LDAP_BACK_FCONN_BINDING,      BER_BVC( "binding" ) },
77         { LDAP_BACK_FCONN_TAINTED,      BER_BVC( "tainted" ) },
78         { LDAP_BACK_FCONN_ABANDON,      BER_BVC( "abandon" ) },
79         { LDAP_BACK_FCONN_ISIDASR,      BER_BVC( "idassert" ) },
80         { LDAP_BACK_FCONN_CACHED,       BER_BVC( "cached" ) },
81
82         { 0 }
83 };
84
85
86 /*
87  * NOTE: there's some confusion in monitor OID arc;
88  * by now, let's consider:
89  * 
90  * Subsystems monitor attributes        1.3.6.1.4.1.4203.666.1.55.0
91  * Databases monitor attributes         1.3.6.1.4.1.4203.666.1.55.0.1
92  * LDAP database monitor attributes     1.3.6.1.4.1.4203.666.1.55.0.1.2
93  *
94  * Subsystems monitor objectclasses     1.3.6.1.4.1.4203.666.3.16.0
95  * Databases monitor objectclasses      1.3.6.1.4.1.4203.666.3.16.0.1
96  * LDAP database monitor objectclasses  1.3.6.1.4.1.4203.666.3.16.0.1.2
97  */
98
99 static struct {
100         char                    *name;
101         char                    *oid;
102 }               s_oid[] = {
103         { "olmLDAPAttributes",                  "olmDatabaseAttributes:2" },
104         { "olmLDAPObjectClasses",               "olmDatabaseObjectClasses:2" },
105
106         { NULL }
107 };
108
109 static struct {
110         char                    *desc;
111         AttributeDescription    **ad;
112 }               s_at[] = {
113         { "( olmLDAPAttributes:1 "
114                 "NAME ( 'olmDbURIList' ) "
115                 "DESC 'List of URIs a proxy is serving; can be modified run-time' "
116                 "SUP managedInfo )",
117                 &ad_olmDbURIList },
118         { "( olmLDAPAttributes:2 "
119                 "NAME ( 'olmDbOperation' ) "
120                 "DESC 'monitor operations performed' "
121                 "SUP monitorCounter "
122                 "NO-USER-MODIFICATION "
123                 "USAGE dSAOperation )",
124                 &ad_olmDbOperations },
125         { "( olmLDAPAttributes:3 "
126                 "NAME ( 'olmDbBoundDN' ) "
127                 "DESC 'monitor connection authorization DN' "
128                 "SUP monitorConnectionAuthzDN "
129                 "NO-USER-MODIFICATION "
130                 "USAGE dSAOperation )",
131                 &ad_olmDbBoundDN },
132         { "( olmLDAPAttributes:4 "
133                 "NAME ( 'olmDbConnFlags' ) "
134                 "DESC 'monitor connection flags' "
135                 "SUP monitoredInfo "
136                 "NO-USER-MODIFICATION "
137                 "USAGE dSAOperation )",
138                 &ad_olmDbConnFlags },
139         { "( olmLDAPAttributes:5 "
140                 "NAME ( 'olmDbConnURI' ) "
141                 "DESC 'monitor connection URI' "
142                 "SUP monitorConnectionPeerAddress "
143                 "NO-USER-MODIFICATION "
144                 "USAGE dSAOperation )",
145                 &ad_olmDbConnURI },
146
147         { NULL }
148 };
149
150 static struct {
151         char            *desc;
152         ObjectClass     **oc;
153 }               s_oc[] = {
154         /* augments an existing object, so it must be AUXILIARY
155          * FIXME: derive from some ABSTRACT "monitoredEntity"? */
156         { "( olmLDAPObjectClasses:1 "
157                 "NAME ( 'olmLDAPDatabase' ) "
158                 "SUP top AUXILIARY "
159                 "MAY ( "
160                         "olmDbURIList "
161                         ") )",
162                 &oc_olmLDAPDatabase },
163         { "( olmLDAPObjectClasses:2 "
164                 "NAME ( 'olmLDAPConnection' ) "
165                 "SUP monitorConnection STRUCTURAL "
166                 "MAY ( "
167                         "olmDbBoundDN "
168                         "$ olmDbConnFlags "
169                         "$ olmDbConnURI "
170                         ") )",
171                 &oc_olmLDAPConnection },
172
173         { NULL }
174 };
175
176 static int
177 ldap_back_monitor_update(
178         Operation       *op,
179         SlapReply       *rs,
180         Entry           *e,
181         void            *priv )
182 {
183         ldapinfo_t              *li = (ldapinfo_t *)priv;
184
185         Attribute               *a;
186
187         /* update olmDbURIList */
188         a = attr_find( e->e_attrs, ad_olmDbURIList );
189         if ( a != NULL ) {
190                 struct berval   bv;
191
192                 assert( a->a_vals != NULL );
193                 assert( !BER_BVISNULL( &a->a_vals[ 0 ] ) );
194                 assert( BER_BVISNULL( &a->a_vals[ 1 ] ) );
195
196                 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
197                 if ( li->li_uri ) {
198                         ber_str2bv( li->li_uri, 0, 0, &bv );
199                         if ( !bvmatch( &a->a_vals[ 0 ], &bv ) ) {
200                                 ber_bvreplace( &a->a_vals[ 0 ], &bv );
201                         }
202                 }
203                 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
204         }
205
206         return SLAP_CB_CONTINUE;
207 }
208
209 static int
210 ldap_back_monitor_modify(
211         Operation       *op,
212         SlapReply       *rs,
213         Entry           *e,
214         void            *priv )
215 {
216         ldapinfo_t              *li = (ldapinfo_t *) priv;
217         
218         Attribute               *save_attrs = NULL;
219         Modifications           *ml,
220                                 *ml_olmDbURIList = NULL;
221         struct berval           ul = BER_BVNULL;
222         int                     got = 0;
223
224         for ( ml = op->orm_modlist; ml; ml = ml->sml_next ) {
225                 if ( ml->sml_desc == ad_olmDbURIList ) {
226                         if ( ml_olmDbURIList != NULL ) {
227                                 rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
228                                 rs->sr_text = "conflicting modifications";
229                                 goto done;
230                         }
231
232                         if ( ml->sml_op != LDAP_MOD_REPLACE ) {
233                                 rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
234                                 rs->sr_text = "modification not allowed";
235                                 goto done;
236                         }
237
238                         ml_olmDbURIList = ml;
239                         got++;
240                         continue;
241                 }
242         }
243
244         if ( got == 0 ) {
245                 return SLAP_CB_CONTINUE;
246         }
247
248         save_attrs = attrs_dup( e->e_attrs );
249
250         if ( ml_olmDbURIList != NULL ) {
251                 Attribute       *a = NULL;
252                 LDAPURLDesc     *ludlist = NULL;
253                 int             rc;
254
255                 ml = ml_olmDbURIList;
256                 assert( ml->sml_nvalues != NULL );
257
258                 if ( BER_BVISNULL( &ml->sml_nvalues[ 0 ] ) ) {
259                         rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
260                         rs->sr_text = "no value provided";
261                         goto done;
262                 }
263
264                 if ( !BER_BVISNULL( &ml->sml_nvalues[ 1 ] ) ) {
265                         rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
266                         rs->sr_text = "multiple values provided";
267                         goto done;
268                 }
269
270                 rc = ldap_url_parselist_ext( &ludlist,
271                         ml->sml_nvalues[ 0 ].bv_val, NULL,
272                         LDAP_PVT_URL_PARSE_NOEMPTY_HOST
273                                 | LDAP_PVT_URL_PARSE_DEF_PORT );
274                 if ( rc != LDAP_URL_SUCCESS ) {
275                         rs->sr_err = LDAP_INVALID_SYNTAX;
276                         rs->sr_text = "unable to parse URI list";
277                         goto done;
278                 }
279
280                 ul.bv_val = ldap_url_list2urls( ludlist );
281                 ldap_free_urllist( ludlist );
282                 if ( ul.bv_val == NULL ) {
283                         rs->sr_err = LDAP_OTHER;
284                         goto done;
285                 }
286                 ul.bv_len = strlen( ul.bv_val );
287                 
288                 a = attr_find( e->e_attrs, ad_olmDbURIList );
289                 if ( a != NULL ) {
290                         if ( a->a_nvals == a->a_vals ) {
291                                 a->a_nvals = ch_calloc( sizeof( struct berval ), 2 );
292                         }
293
294                         ber_bvreplace( &a->a_vals[ 0 ], &ul );
295                         ber_bvreplace( &a->a_nvals[ 0 ], &ul );
296
297                 } else {
298                         attr_merge_normalize_one( e, ad_olmDbURIList, &ul, NULL );
299                 }
300         }
301
302         /* apply changes */
303         if ( !BER_BVISNULL( &ul ) ) {
304                 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
305                 if ( li->li_uri ) {
306                         ch_free( li->li_uri );
307                 }
308                 li->li_uri = ul.bv_val;
309                 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
310
311                 BER_BVZERO( &ul );
312         }
313
314 done:;
315         if ( !BER_BVISNULL( &ul ) ) {
316                 ldap_memfree( ul.bv_val );
317         }
318
319         if ( rs->sr_err == LDAP_SUCCESS ) {
320                 attrs_free( save_attrs );
321                 return SLAP_CB_CONTINUE;
322         }
323
324         attrs_free( e->e_attrs );
325         e->e_attrs = save_attrs;
326
327         return rs->sr_err;
328 }
329
330 static int
331 ldap_back_monitor_free(
332         Entry           *e,
333         void            **priv )
334 {
335         ldapinfo_t              *li = (ldapinfo_t *)(*priv);
336
337         *priv = NULL;
338
339         if ( !slapd_shutdown ) {
340                 memset( &li->li_monitor_info, 0, sizeof( li->li_monitor_info ) );
341         }
342
343         return SLAP_CB_CONTINUE;
344 }
345
346 static int
347 ldap_back_monitor_subsystem_destroy(
348         BackendDB               *be,
349         monitor_subsys_t        *ms)
350 {
351         free(ms->mss_dn.bv_val);
352         BER_BVZERO(&ms->mss_dn);
353
354         free(ms->mss_ndn.bv_val);
355         BER_BVZERO(&ms->mss_ndn);
356
357         return LDAP_SUCCESS;
358 }
359
360 /*
361  * Connection monitoring subsystem:
362  * Tries to mimick what the cn=connections,cn=monitor subsystem does
363  * by creating volatile entries for each connection and populating them
364  * according to the information attached to the connection.
365  * At this moment the only exposed information is the DN used to bind it.
366  * Also note that the connection IDs are not and probably never will be
367  * stable.
368  */
369
370 struct ldap_back_monitor_conn_arg {
371         monitor_subsys_t *ms;
372         Entry **ep;
373 };
374
375 static int
376 ldap_back_monitor_conn_entry(
377         ldapconn_t *lc,
378         struct ldap_back_monitor_conn_arg *arg )
379 {
380         Entry *e;
381         monitor_entry_t         *mp;
382         char buf[SLAP_TEXT_BUFLEN];
383         char *ptr;
384         struct berval bv, dn, ndn;
385         int i;
386
387         e = entry_alloc();
388
389         bv.bv_val = buf;
390         bv.bv_len = snprintf( bv.bv_val, SLAP_TEXT_BUFLEN,
391                 "cn=Connection %lu", lc->lc_connid );
392
393         build_new_dn( &dn, &arg->ms->mss_dn, &bv, NULL );
394         build_new_dn( &ndn, &arg->ms->mss_ndn, &bv, NULL );
395
396         e->e_name = dn;
397         e->e_nname = ndn;
398
399         bv.bv_val += 3;
400         bv.bv_len -= 3;
401         attr_merge_normalize_one( e, slap_schema.si_ad_cn, &bv, NULL );
402
403         BER_BVSTR( &bv, "monitorContainer" );
404         attr_merge_normalize_one( e, slap_schema.si_ad_objectClass, &bv, NULL );
405
406         attr_merge_normalize_one( e, ad_olmDbBoundDN, &lc->lc_bound_ndn, NULL );
407
408         for ( i = 0; s_flag[i].flag; i++ )
409         {
410                 if ( lc->lc_flags & s_flag[i].flag )
411                 {
412                         attr_merge_normalize_one( e, ad_olmDbConnFlags, &s_flag[i].name, NULL );
413                 }
414         }
415
416         ldap_get_option( lc->lc_ld, LDAP_OPT_URI, &bv.bv_val );
417         ptr = strchr( bv.bv_val, ' ' );
418         bv.bv_len = ptr ? ptr - bv.bv_val : strlen(bv.bv_val);
419         attr_merge_normalize_one( e, ad_olmDbConnURI, &bv, NULL );
420         ch_free( bv.bv_val );
421
422         mp = monitor_entrypriv_create();
423         e->e_private = mp;
424         mp->mp_info = arg->ms;
425         mp->mp_flags = MONITOR_F_SUB | MONITOR_F_VOLATILE;
426
427         *arg->ep = e;
428         arg->ep = &mp->mp_next;
429
430         return 0;
431 }
432
433 static int
434 ldap_back_monitor_conn_create(
435         Operation       *op,
436         SlapReply       *rs,
437         struct berval   *ndn,
438         Entry           *e_parent,
439         Entry           **ep )
440 {
441         monitor_entry_t         *mp_parent;
442         monitor_subsys_t        *ms;
443         ldapinfo_t              *li;
444         ldapconn_t              *lc;
445
446         struct ldap_back_monitor_conn_arg *arg;
447         int conn_type;
448
449         assert( e_parent->e_private != NULL );
450
451         mp_parent = e_parent->e_private;
452         ms = (monitor_subsys_t *)mp_parent->mp_info;
453         li = (ldapinfo_t *)ms->mss_private;
454
455         arg = ch_calloc( 1, sizeof(struct ldap_back_monitor_conn_arg) );
456         arg->ep = ep;
457         arg->ms = ms;
458
459         for ( conn_type = LDAP_BACK_PCONN_FIRST;
460                 conn_type < LDAP_BACK_PCONN_LAST;
461                 conn_type++ )
462         {
463                 LDAP_TAILQ_FOREACH( lc,
464                         &li->li_conn_priv[ conn_type ].lic_priv,
465                         lc_q )
466                 {
467                         ldap_back_monitor_conn_entry( lc, arg );
468                 }
469         }
470
471         avl_apply( li->li_conninfo.lai_tree, ldap_back_monitor_conn_entry,
472                 arg, -1, AVL_INORDER );
473
474         ch_free( arg );
475
476         return 0;
477 }
478
479 static int
480 ldap_back_monitor_conn_init(
481         BackendDB               *be,
482         monitor_subsys_t        *ms )
483 {
484         ldapinfo_t      *li = (ldapinfo_t *) ms->mss_private;
485         monitor_info_t  *mi;
486         monitor_extra_t *mbe;
487
488         Entry           *e;
489         int             rc;
490
491         assert( be != NULL );
492         mi = (monitor_info_t *) be->be_private;
493         mbe = (monitor_extra_t *) be->bd_info->bi_extra;
494
495         ms->mss_dn = ms->mss_ndn = li->li_monitor_info.lmi_ndn;
496         ms->mss_rdn = li->li_monitor_info.lmi_conn_rdn;
497         ms->mss_create = ldap_back_monitor_conn_create;
498         ms->mss_destroy = ldap_back_monitor_subsystem_destroy;
499
500         e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn,
501                 &ms->mss_rdn,
502                 mi->mi_oc_monitorContainer, mi, NULL, NULL );
503         if ( e == NULL ) {
504                 Debug( LDAP_DEBUG_ANY,
505                         "ldap_back_monitor_conn_init: "
506                         "unable to create entry \"%s,%s\"\n",
507                         li->li_monitor_info.lmi_conn_rdn.bv_val,
508                         ms->mss_ndn.bv_val, 0 );
509                 return( -1 );
510         }
511
512         ber_dupbv( &ms->mss_dn, &e->e_name );
513         ber_dupbv( &ms->mss_ndn, &e->e_nname );
514
515         rc = mbe->register_entry( e, NULL, ms, MONITOR_F_VOLATILE_CH );
516
517         /* add labeledURI and special, modifiable URI value */
518         if ( rc == LDAP_SUCCESS && li->li_uri != NULL ) {
519                 struct berval           bv;
520                 Attribute               *a;
521                 LDAPURLDesc             *ludlist = NULL;
522                 monitor_callback_t      *cb = NULL;
523
524                 a = attr_alloc( ad_olmDbURIList );
525
526                 ber_str2bv( li->li_uri, 0, 0, &bv );
527                 attr_valadd( a, &bv, NULL, 1 );
528                 attr_normalize( a->a_desc, a->a_vals, &a->a_nvals, NULL );
529
530                 rc = ldap_url_parselist_ext( &ludlist,
531                         li->li_uri, NULL,
532                         LDAP_PVT_URL_PARSE_NOEMPTY_HOST
533                                 | LDAP_PVT_URL_PARSE_DEF_PORT );
534                 if ( rc != LDAP_URL_SUCCESS ) {
535                         Debug( LDAP_DEBUG_ANY,
536                                 "ldap_back_monitor_db_open: "
537                                 "unable to parse URI list (ignored)\n",
538                                 0, 0, 0 );
539                 } else {
540                         Attribute *a2 = attr_alloc( slap_schema.si_ad_labeledURI );
541
542                         a->a_next = a2;
543
544                         for ( ; ludlist != NULL; ) {
545                                 LDAPURLDesc     *next = ludlist->lud_next;
546
547                                 bv.bv_val = ldap_url_desc2str( ludlist );
548                                 assert( bv.bv_val != NULL );
549                                 ldap_free_urldesc( ludlist );
550                                 bv.bv_len = strlen( bv.bv_val );
551                                 attr_valadd( a2, &bv, NULL, 1 );
552                                 ch_free( bv.bv_val );
553
554                                 ludlist = next;
555                         }
556
557                         attr_normalize( a2->a_desc, a2->a_vals, &a2->a_nvals, NULL );
558                 }
559
560                 cb = ch_calloc( sizeof( monitor_callback_t ), 1 );
561                 cb->mc_update = ldap_back_monitor_update;
562                 cb->mc_modify = ldap_back_monitor_modify;
563                 cb->mc_free = ldap_back_monitor_free;
564                 cb->mc_private = (void *)li;
565
566                 rc = mbe->register_entry_attrs( &ms->mss_ndn, a, cb, NULL, 0, NULL );
567
568                 attr_free( a->a_next );
569                 attr_free( a );
570
571                 if ( rc != LDAP_SUCCESS )
572                 {
573                         ch_free( cb );
574                 }
575         }
576
577         entry_free( e );
578
579         return rc;
580 }
581
582 /*
583  * Operation monitoring subsystem:
584  * Looks a lot like the cn=operations,cn=monitor subsystem except that at this
585  * moment, only completed operations are counted. Each entry has a separate
586  * callback with all the needed information linked there in the structure
587  * below so that the callback need not locate it over and over again.
588  */
589
590 struct ldap_back_monitor_op_counter {
591         ldap_pvt_mp_t           *data;
592         ldap_pvt_thread_mutex_t *mutex;
593 };
594
595 static void
596 ldap_back_monitor_ops_dispose(
597         void    **priv)
598 {
599         struct ldap_back_monitor_op_counter *counter = *priv;
600
601         ch_free( counter );
602         counter = NULL;
603 }
604
605 static int
606 ldap_back_monitor_ops_free(
607         Entry *e,
608         void **priv)
609 {
610         ldap_back_monitor_ops_dispose( priv );
611         return LDAP_SUCCESS;
612 }
613
614 static int
615 ldap_back_monitor_ops_update(
616         Operation       *op,
617         SlapReply       *rs,
618         Entry           *e,
619         void            *priv )
620 {
621         struct ldap_back_monitor_op_counter *counter = priv;
622         Attribute *a;
623
624         /*TODO
625          * what about initiated/completed?
626          */
627         a = attr_find( e->e_attrs, ad_olmDbOperations );
628         assert( a != NULL );
629
630         ldap_pvt_thread_mutex_lock( counter->mutex );
631         UI2BV( &a->a_vals[ 0 ], *counter->data );
632         ldap_pvt_thread_mutex_unlock( counter->mutex );
633
634         return SLAP_CB_CONTINUE;
635 }
636
637 static int
638 ldap_back_monitor_ops_init(
639         BackendDB               *be,
640         monitor_subsys_t        *ms )
641 {
642         ldapinfo_t      *li = (ldapinfo_t *) ms->mss_private;
643
644         monitor_info_t  *mi;
645         monitor_extra_t *mbe;
646         Entry           *e, *parent;
647         int             rc;
648         slap_op_t       op;
649         struct berval   value = BER_BVC( "0" );
650
651         assert( be != NULL );
652
653         mi = (monitor_info_t *) be->be_private;
654         mbe = (monitor_extra_t *) be->bd_info->bi_extra;
655
656         ms->mss_dn = ms->mss_ndn = li->li_monitor_info.lmi_ndn;
657         ms->mss_rdn = li->li_monitor_info.lmi_ops_rdn;
658         ms->mss_destroy = ldap_back_monitor_subsystem_destroy;
659
660         parent = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn,
661                 &ms->mss_rdn,
662                 mi->mi_oc_monitorContainer, mi, NULL, NULL );
663         if ( parent == NULL ) {
664                 Debug( LDAP_DEBUG_ANY,
665                         "ldap_back_monitor_ops_init: "
666                         "unable to create entry \"%s,%s\"\n",
667                         li->li_monitor_info.lmi_ops_rdn.bv_val,
668                         ms->mss_ndn.bv_val, 0 );
669                 return( -1 );
670         }
671
672         ber_dupbv( &ms->mss_dn, &parent->e_name );
673         ber_dupbv( &ms->mss_ndn, &parent->e_nname );
674
675         rc = mbe->register_entry( parent, NULL, ms, MONITOR_F_PERSISTENT_CH );
676         if ( rc != LDAP_SUCCESS )
677         {
678                 Debug( LDAP_DEBUG_ANY,
679                         "ldap_back_monitor_ops_init: "
680                         "unable to register entry \"%s\" for monitoring\n",
681                         parent->e_name.bv_val, 0, 0 );
682                 goto done;
683         }
684
685         for ( op = 0; op < SLAP_OP_LAST; op++ )
686         {
687                 monitor_callback_t *cb;
688                 struct ldap_back_monitor_op_counter *counter;
689
690                 e = monitor_entry_stub( &parent->e_name, &parent->e_nname,
691                         &ldap_back_monitor_op[op].rdn,
692                         mi->mi_oc_monitorCounterObject, mi, NULL, NULL );
693                 if ( e == NULL ) {
694                         Debug( LDAP_DEBUG_ANY,
695                                 "ldap_back_monitor_ops_init: "
696                                 "unable to create entry \"%s,%s\"\n",
697                                 ldap_back_monitor_op[op].rdn.bv_val,
698                                 parent->e_nname.bv_val, 0 );
699                         return( -1 );
700                 }
701
702                 attr_merge_normalize_one( e, ad_olmDbOperations, &value, NULL );
703
704                 counter = ch_malloc( sizeof( struct ldap_back_monitor_op_counter ) );
705                 counter->data = &li->li_ops_completed[ op ];
706                 counter->mutex = &li->li_counter_mutex;
707
708                 /*
709                  * We cannot share a single callback between entries.
710                  *
711                  * monitor_cache_destroy() tries to free all callbacks and it's called
712                  * before mss_destroy() so we have no chance of handling it ourselves
713                  */
714                 cb = ch_calloc( sizeof( monitor_callback_t ), 1 );
715                 cb->mc_update = ldap_back_monitor_ops_update;
716                 cb->mc_free = ldap_back_monitor_ops_free;
717                 cb->mc_dispose = ldap_back_monitor_ops_dispose;
718                 cb->mc_private = (void *)counter;
719
720                 rc = mbe->register_entry( e, cb, ms, 0 );
721
722                 /* TODO: register_entry has stored a duplicate so we might actually reuse it
723                  * instead of recreating it every time... */
724                 entry_free( e );
725
726                 if ( rc != LDAP_SUCCESS )
727                 {
728                         Debug( LDAP_DEBUG_ANY,
729                                 "ldap_back_monitor_ops_init: "
730                                 "unable to register entry \"%s\" for monitoring\n",
731                                 e->e_name.bv_val, 0, 0 );
732                         ch_free( cb );
733                         break;
734                 }
735         }
736
737 done:
738         entry_free( parent );
739
740         return rc;
741 }
742
743 /*
744  * call from within ldap_back_initialize()
745  */
746 static int
747 ldap_back_monitor_initialize( void )
748 {
749         int             i, code;
750         ConfigArgs c;
751         char    *argv[ 3 ];
752
753         static int      ldap_back_monitor_initialized = 0;
754
755         /* set to 0 when successfully initialized; otherwise, remember failure */
756         static int      ldap_back_monitor_initialized_failure = 1;
757
758         /* register schema here; if compiled as dynamic object,
759          * must be loaded __after__ back_monitor.la */
760
761         if ( ldap_back_monitor_initialized++ ) {
762                 return ldap_back_monitor_initialized_failure;
763         }
764
765         if ( backend_info( "monitor" ) == NULL ) {
766                 return -1;
767         }
768
769         argv[ 0 ] = "back-ldap monitor";
770         c.argv = argv;
771         c.argc = 3;
772         c.fname = argv[0];
773         for ( i = 0; s_oid[ i ].name; i++ ) {
774         
775                 argv[ 1 ] = s_oid[ i ].name;
776                 argv[ 2 ] = s_oid[ i ].oid;
777
778                 if ( parse_oidm( &c, 0, NULL ) != 0 ) {
779                         Debug( LDAP_DEBUG_ANY,
780                                 "ldap_back_monitor_initialize: unable to add "
781                                 "objectIdentifier \"%s=%s\"\n",
782                                 s_oid[ i ].name, s_oid[ i ].oid, 0 );
783                         return 2;
784                 }
785         }
786
787         for ( i = 0; s_at[ i ].desc != NULL; i++ ) {
788                 code = register_at( s_at[ i ].desc, s_at[ i ].ad, 1 );
789                 if ( code != LDAP_SUCCESS ) {
790                         Debug( LDAP_DEBUG_ANY,
791                                 "ldap_back_monitor_initialize: register_at failed for attributeType (%s)\n",
792                                 s_at[ i ].desc, 0, 0 );
793                         return 3;
794
795                 } else {
796                         (*s_at[ i ].ad)->ad_type->sat_flags |= SLAP_AT_HIDE;
797                 }
798         }
799
800         for ( i = 0; s_oc[ i ].desc != NULL; i++ ) {
801                 code = register_oc( s_oc[ i ].desc, s_oc[ i ].oc, 1 );
802                 if ( code != LDAP_SUCCESS ) {
803                         Debug( LDAP_DEBUG_ANY,
804                                 "ldap_back_monitor_initialize: register_oc failed for objectClass (%s)\n",
805                                 s_oc[ i ].desc, 0, 0 );
806                         return 4;
807
808                 } else {
809                         (*s_oc[ i ].oc)->soc_flags |= SLAP_OC_HIDE;
810                 }
811         }
812
813         return ( ldap_back_monitor_initialized_failure = LDAP_SUCCESS );
814 }
815
816 /*
817  * call from within ldap_back_db_init()
818  */
819 int
820 ldap_back_monitor_db_init( BackendDB *be )
821 {
822         int     rc;
823
824         rc = ldap_back_monitor_initialize();
825         if ( rc != LDAP_SUCCESS ) {
826                 return rc;
827         }
828
829 #if 0   /* uncomment to turn monitoring on by default */
830         SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_MONITORING;
831 #endif
832
833         return 0;
834 }
835
836 /*
837  * call from within ldap_back_db_open()
838  */
839 int
840 ldap_back_monitor_db_open( BackendDB *be )
841 {
842         ldapinfo_t              *li = (ldapinfo_t *) be->be_private;
843         monitor_subsys_t        *mss = li->li_monitor_info.lmi_mss;
844         int                     rc = 0;
845         BackendInfo             *mi;
846         monitor_extra_t         *mbe;
847
848         if ( !SLAP_DBMONITORING( be ) ) {
849                 return 0;
850         }
851
852         /* check if monitor is configured and usable */
853         mi = backend_info( "monitor" );
854         if ( !mi || !mi->bi_extra ) {
855                 SLAP_DBFLAGS( be ) ^= SLAP_DBFLAG_MONITORING;
856                 return 0;
857         }
858         mbe = mi->bi_extra;
859
860         /* don't bother if monitor is not configured */
861         if ( !mbe->is_configured() ) {
862                 static int warning = 0;
863
864                 if ( warning++ == 0 ) {
865                         Debug( LDAP_DEBUG_ANY, "ldap_back_monitor_db_open: "
866                                 "monitoring disabled; "
867                                 "configure monitor database to enable\n",
868                                 0, 0, 0 );
869                 }
870
871                 return 0;
872         }
873
874         /* caller (e.g. an overlay based on back-ldap) may want to use
875          * a different DN and RDNs... */
876         if ( BER_BVISNULL( &li->li_monitor_info.lmi_ndn ) ) {
877                 rc = mbe->register_database( be, &li->li_monitor_info.lmi_ndn );
878                 if ( rc != 0 ) {
879                         Debug( LDAP_DEBUG_ANY, "ldap_back_monitor_db_open: "
880                                 "failed to register the databse with back-monitor\n",
881                                 0, 0, 0 );
882                 }
883         }
884         if ( BER_BVISNULL( &li->li_monitor_info.lmi_conn_rdn ) ) {
885                 ber_str2bv( "cn=Connections", 0, 1,
886                         &li->li_monitor_info.lmi_conn_rdn );
887         }
888         if ( BER_BVISNULL( &li->li_monitor_info.lmi_ops_rdn ) ) {
889                 ber_str2bv( "cn=Operations", 0, 1,
890                         &li->li_monitor_info.lmi_ops_rdn );
891         }
892
893         /* set up the subsystems used to create the operation and
894          * volatile connection entries */
895
896         mss->mss_name = "back-ldap connections";
897         mss->mss_flags = MONITOR_F_VOLATILE_CH;
898         mss->mss_open = ldap_back_monitor_conn_init;
899         mss->mss_private = li;
900
901         if ( mbe->register_subsys( mss ) )
902         {
903                 Debug( LDAP_DEBUG_ANY,
904                         "ldap_back_monitor_db_open: "
905                         "failed to register connection subsystem", 0, 0, 0 );
906                 return -1;
907         }
908
909         mss++;
910
911         mss->mss_name = "back-ldap operations";
912         mss->mss_flags = MONITOR_F_PERSISTENT_CH;
913         mss->mss_open = ldap_back_monitor_ops_init;
914         mss->mss_private = li;
915
916         if ( mbe->register_subsys( mss ) )
917         {
918                 Debug( LDAP_DEBUG_ANY,
919                         "ldap_back_monitor_db_open: "
920                         "failed to register operation subsystem", 0, 0, 0 );
921                 return -1;
922         }
923
924         return rc;
925 }
926
927 /*
928  * call from within ldap_back_db_close()
929  */
930 int
931 ldap_back_monitor_db_close( BackendDB *be )
932 {
933         ldapinfo_t              *li = (ldapinfo_t *) be->be_private;
934
935         if ( li && !BER_BVISNULL( &li->li_monitor_info.lmi_ndn ) ) {
936                 BackendInfo             *mi;
937                 monitor_extra_t         *mbe;
938
939                 /* check if monitor is configured and usable */
940                 mi = backend_info( "monitor" );
941                 if ( mi && mi->bi_extra ) {
942                         mbe = mi->bi_extra;
943
944                         /*TODO
945                          * Unregister all entries our subsystems have created.
946                          * Will only really be necessary when
947                          * SLAPD_CONFIG_DELETE is enabled.
948                          *
949                          * Might need a way to unregister subsystems instead.
950                          */
951                 }
952         }
953
954         return 0;
955 }
956
957 /*
958  * call from within ldap_back_db_destroy()
959  */
960 int
961 ldap_back_monitor_db_destroy( BackendDB *be )
962 {
963         ldapinfo_t              *li = (ldapinfo_t *) be->be_private;
964
965         if ( li ) {
966                 memset( &li->li_monitor_info, 0, sizeof( li->li_monitor_info ) );
967         }
968
969         return 0;
970 }
971