1/*	$NetBSD: pbind.c,v 1.3 2021/08/14 16:14:59 christos Exp $	*/
2
3/* pbind.c - passthru Bind overlay */
4/* $OpenLDAP$ */
5/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
6 *
7 * Copyright 2003-2021 The OpenLDAP Foundation.
8 * Portions Copyright 2003-2010 Howard Chu.
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 the 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 the Howard Chu for inclusion
21 * in OpenLDAP Software.
22 */
23
24#include <sys/cdefs.h>
25__RCSID("$NetBSD: pbind.c,v 1.3 2021/08/14 16:14:59 christos Exp $");
26
27#include "portable.h"
28
29#include <stdio.h>
30
31#include <ac/string.h>
32#include <ac/socket.h>
33
34#include "lutil.h"
35#include "slap.h"
36#include "back-ldap.h"
37#include "slap-config.h"
38
39static BackendInfo	*lback;
40
41static slap_overinst ldappbind;
42
43static int
44ldap_pbind_bind(
45	Operation	*op,
46	SlapReply	*rs )
47{
48	slap_overinst	*on = (slap_overinst *) op->o_bd->bd_info;
49	void *private = op->o_bd->be_private;
50	void *bi = op->o_bd->bd_info;
51	int rc;
52
53	op->o_bd->bd_info = lback;
54	op->o_bd->be_private = on->on_bi.bi_private;
55	rc = lback->bi_op_bind( op, rs );
56	op->o_bd->be_private = private;
57	op->o_bd->bd_info = bi;
58
59	return rc;
60}
61
62static int
63ldap_pbind_db_init(
64	BackendDB *be,
65	ConfigReply *cr )
66{
67	slap_overinst	*on = (slap_overinst *)be->bd_info;
68	ConfigOCs	*be_cf_ocs = be->be_cf_ocs;
69	void		*private = be->be_private;
70	int rc;
71
72	if ( lback == NULL ) {
73		lback = backend_info( "ldap" );
74
75		if ( lback == NULL ) {
76			return 1;
77		}
78	}
79
80	rc = lback->bi_db_init( be, cr );
81	on->on_bi.bi_private = be->be_private;
82	be->be_cf_ocs = be_cf_ocs;
83	be->be_private = private;
84
85	return rc;
86}
87
88static int
89ldap_pbind_db_open(
90	BackendDB	*be,
91	ConfigReply	*cr )
92{
93	slap_overinst	*on = (slap_overinst *) be->bd_info;
94	void	*private = be->be_private;
95	int		rc;
96	int		monitoring;
97
98    be->be_private = on->on_bi.bi_private;
99	monitoring = ( SLAP_DBFLAGS( be ) & SLAP_DBFLAG_MONITORING );
100	SLAP_DBFLAGS( be ) &= ~SLAP_DBFLAG_MONITORING;
101	rc = lback->bi_db_open( be, cr );
102	SLAP_DBFLAGS( be ) |= monitoring;
103	be->be_private = private;
104
105	return rc;
106}
107
108static int
109ldap_pbind_db_close(
110	BackendDB	*be,
111	ConfigReply	*cr )
112{
113	slap_overinst	*on = (slap_overinst *) be->bd_info;
114	void	*private = be->be_private;
115	int		rc;
116
117    be->be_private = on->on_bi.bi_private;
118	rc = lback->bi_db_close( be, cr );
119	be->be_private = private;
120
121	return rc;
122}
123
124static int
125ldap_pbind_db_destroy(
126	BackendDB	*be,
127	ConfigReply	*cr )
128{
129	slap_overinst	*on = (slap_overinst *) be->bd_info;
130	void	*private = be->be_private;
131	int		rc;
132
133    be->be_private = on->on_bi.bi_private;
134	rc = lback->bi_db_close( be, cr );
135	on->on_bi.bi_private = be->be_private;
136	be->be_private = private;
137
138	return rc;
139}
140
141static int
142ldap_pbind_connection_destroy(
143	BackendDB *be,
144	Connection *conn
145)
146{
147	slap_overinst	*on = (slap_overinst *) be->bd_info;
148	void			*private = be->be_private;
149	int				rc;
150
151	be->be_private = on->on_bi.bi_private;
152	rc = lback->bi_connection_destroy( be, conn );
153	be->be_private = private;
154
155	return rc;
156}
157
158int
159pbind_initialize( void )
160{
161	int rc;
162
163	ldappbind.on_bi.bi_type = "pbind";
164	ldappbind.on_bi.bi_db_init = ldap_pbind_db_init;
165	ldappbind.on_bi.bi_db_open = ldap_pbind_db_open;
166	ldappbind.on_bi.bi_db_close = ldap_pbind_db_close;
167	ldappbind.on_bi.bi_db_destroy = ldap_pbind_db_destroy;
168
169	ldappbind.on_bi.bi_op_bind = ldap_pbind_bind;
170	ldappbind.on_bi.bi_connection_destroy = ldap_pbind_connection_destroy;
171
172	rc = ldap_pbind_init_cf( &ldappbind.on_bi );
173	if ( rc ) {
174		return rc;
175	}
176
177	return overlay_register( &ldappbind );
178}
179