Debian Codegraph @

servers/slapd/back-ldap/monitor.c

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