1/*	$NetBSD: listener.c,v 1.3 2021/08/14 16:15:00 christos Exp $	*/
2
3/* listener.c - deals with listener subsystem */
4/* $OpenLDAP$ */
5/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
6 *
7 * Copyright 2001-2021 The OpenLDAP Foundation.
8 * Portions Copyright 2001-2003 Pierangelo Masarati.
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted only as authorized by the OpenLDAP
13 * Public License.
14 *
15 * A copy of this license is available in file LICENSE in the
16 * top-level directory of the distribution or, alternatively, at
17 * <http://www.OpenLDAP.org/license.html>.
18 */
19/* ACKNOWLEDGEMENTS:
20 * This work was initially developed by Pierangelo Masarati for inclusion
21 * in OpenLDAP Software.
22 */
23
24#include <sys/cdefs.h>
25__RCSID("$NetBSD: listener.c,v 1.3 2021/08/14 16:15:00 christos Exp $");
26
27#include "portable.h"
28
29#include <stdio.h>
30#include <ac/string.h>
31
32#include "slap.h"
33#include "back-monitor.h"
34
35int
36monitor_subsys_listener_init(
37	BackendDB		*be,
38	monitor_subsys_t	*ms
39)
40{
41	monitor_info_t	*mi;
42	Entry		*e_listener, **ep;
43	int		i;
44	monitor_entry_t	*mp;
45	Listener	**l;
46
47	assert( be != NULL );
48
49	if ( ( l = slapd_get_listeners() ) == NULL ) {
50		if ( slapMode & SLAP_TOOL_MODE ) {
51			return 0;
52		}
53
54		Debug( LDAP_DEBUG_ANY,
55			"monitor_subsys_listener_init: "
56			"unable to get listeners\n" );
57		return( -1 );
58	}
59
60	mi = ( monitor_info_t * )be->be_private;
61
62	if ( monitor_cache_get( mi, &ms->mss_ndn, &e_listener ) ) {
63		Debug( LDAP_DEBUG_ANY,
64			"monitor_subsys_listener_init: "
65			"unable to get entry \"%s\"\n",
66			ms->mss_ndn.bv_val );
67		return( -1 );
68	}
69
70	mp = ( monitor_entry_t * )e_listener->e_private;
71	mp->mp_children = NULL;
72	ep = &mp->mp_children;
73
74	for ( i = 0; l[ i ]; i++ ) {
75		char 		buf[ BACKMONITOR_BUFSIZE ];
76		Entry		*e;
77		struct berval bv;
78
79		bv.bv_len = snprintf( buf, sizeof( buf ),
80				"cn=Listener %d", i );
81		bv.bv_val = buf;
82		e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn, &bv,
83			mi->mi_oc_monitoredObject, NULL, NULL );
84
85		if ( e == NULL ) {
86			Debug( LDAP_DEBUG_ANY,
87				"monitor_subsys_listener_init: "
88				"unable to create entry \"cn=Listener %d,%s\"\n",
89				i, ms->mss_ndn.bv_val );
90			return( -1 );
91		}
92
93		attr_merge_normalize_one( e, mi->mi_ad_monitorConnectionLocalAddress,
94				&l[ i ]->sl_name, NULL );
95
96		attr_merge_normalize_one( e, slap_schema.si_ad_labeledURI,
97				&l[ i ]->sl_url, NULL );
98
99#ifdef HAVE_TLS
100		if ( l[ i ]->sl_is_tls ) {
101			struct berval bv;
102
103			BER_BVSTR( &bv, "TLS" );
104			attr_merge_normalize_one( e, mi->mi_ad_monitoredInfo,
105					&bv, NULL );
106		}
107#endif /* HAVE_TLS */
108#ifdef LDAP_CONNECTIONLESS
109		if ( l[ i ]->sl_is_udp ) {
110			struct berval bv;
111
112			BER_BVSTR( &bv, "UDP" );
113			attr_merge_normalize_one( e, mi->mi_ad_monitoredInfo,
114					&bv, NULL );
115		}
116#endif /* HAVE_TLS */
117
118		mp = monitor_entrypriv_create();
119		if ( mp == NULL ) {
120			return -1;
121		}
122		e->e_private = ( void * )mp;
123		mp->mp_info = ms;
124		mp->mp_flags = ms->mss_flags
125			| MONITOR_F_SUB;
126
127		if ( monitor_cache_add( mi, e ) ) {
128			Debug( LDAP_DEBUG_ANY,
129				"monitor_subsys_listener_init: "
130				"unable to add entry \"cn=Listener %d,%s\"\n",
131				i, ms->mss_ndn.bv_val );
132			return( -1 );
133		}
134
135		*ep = e;
136		ep = &mp->mp_next;
137	}
138
139	monitor_cache_release( mi, e_listener );
140
141	return( 0 );
142}
143
144