1/* listener.c - deals with listener subsystem */
2/* $OpenLDAP$ */
3/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 *
5 * Copyright 2001-2011 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 "slap.h"
28#include "back-monitor.h"
29
30int
31monitor_subsys_listener_init(
32	BackendDB		*be,
33	monitor_subsys_t	*ms
34)
35{
36	monitor_info_t	*mi;
37	Entry		*e_listener, **ep;
38	int		i;
39	monitor_entry_t	*mp;
40	Listener	**l;
41
42	assert( be != NULL );
43
44	if ( ( l = slapd_get_listeners() ) == NULL ) {
45		if ( slapMode & SLAP_TOOL_MODE ) {
46			return 0;
47		}
48
49		Debug( LDAP_DEBUG_ANY,
50			"monitor_subsys_listener_init: "
51			"unable to get listeners\n", 0, 0, 0 );
52		return( -1 );
53	}
54
55	mi = ( monitor_info_t * )be->be_private;
56
57	if ( monitor_cache_get( mi, &ms->mss_ndn, &e_listener ) ) {
58		Debug( LDAP_DEBUG_ANY,
59			"monitor_subsys_listener_init: "
60			"unable to get entry \"%s\"\n",
61			ms->mss_ndn.bv_val, 0, 0 );
62		return( -1 );
63	}
64
65	mp = ( monitor_entry_t * )e_listener->e_private;
66	mp->mp_children = NULL;
67	ep = &mp->mp_children;
68
69	for ( i = 0; l[ i ]; i++ ) {
70		char 		buf[ BACKMONITOR_BUFSIZE ];
71		Entry		*e;
72		struct berval bv;
73
74		bv.bv_len = snprintf( buf, sizeof( buf ),
75				"cn=Listener %d", i );
76		bv.bv_val = buf;
77		e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn, &bv,
78			mi->mi_oc_monitoredObject, mi, NULL, NULL );
79
80		if ( e == NULL ) {
81			Debug( LDAP_DEBUG_ANY,
82				"monitor_subsys_listener_init: "
83				"unable to create entry \"cn=Listener %d,%s\"\n",
84				i, ms->mss_ndn.bv_val, 0 );
85			return( -1 );
86		}
87
88		attr_merge_normalize_one( e, mi->mi_ad_monitorConnectionLocalAddress,
89				&l[ i ]->sl_name, NULL );
90
91		attr_merge_normalize_one( e, slap_schema.si_ad_labeledURI,
92				&l[ i ]->sl_url, NULL );
93
94#ifdef HAVE_TLS
95		if ( l[ i ]->sl_is_tls ) {
96			struct berval bv;
97
98			BER_BVSTR( &bv, "TLS" );
99			attr_merge_normalize_one( e, mi->mi_ad_monitoredInfo,
100					&bv, NULL );
101		}
102#endif /* HAVE_TLS */
103#ifdef LDAP_CONNECTIONLESS
104		if ( l[ i ]->sl_is_udp ) {
105			struct berval bv;
106
107			BER_BVSTR( &bv, "UDP" );
108			attr_merge_normalize_one( e, mi->mi_ad_monitoredInfo,
109					&bv, NULL );
110		}
111#endif /* HAVE_TLS */
112
113		mp = monitor_entrypriv_create();
114		if ( mp == NULL ) {
115			return -1;
116		}
117		e->e_private = ( void * )mp;
118		mp->mp_info = ms;
119		mp->mp_flags = ms->mss_flags
120			| MONITOR_F_SUB;
121
122		if ( monitor_cache_add( mi, e ) ) {
123			Debug( LDAP_DEBUG_ANY,
124				"monitor_subsys_listener_init: "
125				"unable to add entry \"cn=Listener %d,%s\"\n",
126				i, ms->mss_ndn.bv_val, 0 );
127			return( -1 );
128		}
129
130		*ep = e;
131		ep = &mp->mp_next;
132	}
133
134	monitor_cache_release( mi, e_listener );
135
136	return( 0 );
137}
138
139