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