summaryrefslogtreecommitdiffstats
path: root/ldap/servers/slapd/test-plugins/testbind.c
blob: c39220294aac870ace4262f90384467318191257 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/** BEGIN COPYRIGHT BLOCK
 * This Program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; version 2 of the License.
 * 
 * This Program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA.
 * 
 * In addition, as a special exception, Red Hat, Inc. gives You the additional
 * right to link the code of this Program with code not covered under the GNU
 * General Public License ("Non-GPL Code") and to distribute linked combinations
 * including the two, subject to the limitations in this paragraph. Non-GPL Code
 * permitted under this exception must only link to the code of this Program
 * through those well defined interfaces identified in the file named EXCEPTION
 * found in the source code files (the "Approved Interfaces"). The files of
 * Non-GPL Code may instantiate templates or use macros or inline functions from
 * the Approved Interfaces without causing the resulting work to be covered by
 * the GNU General Public License. Only Red Hat, Inc. may make changes or
 * additions to the list of Approved Interfaces. You must obey the GNU General
 * Public License in all respects for all of the Program code and other code used
 * in conjunction with the Program except the Non-GPL Code covered by this
 * exception. If you modify this file, you may extend this exception to your
 * version of the file, but you are not obligated to do so. If you do not wish to
 * provide this exception without modification, you must delete this exception
 * statement from your version and license this file solely under the GPL without
 * exception. 
 * 
 * 
 * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
 * Copyright (C) 2005 Red Hat, Inc.
 * All rights reserved.
 * END COPYRIGHT BLOCK **/

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif


/************************************************************

 testbind.c

 This source file provides an example of a pre-operation plug-in
 function that handles authentication.

 Note that the Directory Server front-end handles bind
 operations requested by the root DN. The server does not
 invoke your plug-in function if the client is authenicating 
 as the root DN.

 To test this plug-in function, stop the server, edit the dse.ldif file
 (in the config directory)
 and add the following lines before restarting the server :

 dn: cn=Test Bind,cn=plugins,cn=config
 objectClass: top
 objectClass: nsSlapdPlugin
 objectClass: extensibleObject
 cn: Test Bind
 nsslapd-pluginPath: <server_root>/plugins/slapd/slapi/examples/libtest-plugin.so
 nsslapd-pluginInitfunc: testbind_init
 nsslapd-pluginType: preoperation
 nsslapd-pluginEnabled: on
 nsslapd-plugin-depends-on-type: database
 nsslapd-pluginId: test-bind

 ************************************************************/
#include <stdio.h>
#include <string.h>
#include "slapi-plugin.h"

Slapi_PluginDesc bindpdesc = { "test-bind", "Fedora Project", "1.0.4",
	"sample bind pre-operation plugin" };

static Slapi_ComponentId *plugin_id = NULL;




/* Pre-operation plug-in function */
int
test_bind( Slapi_PBlock *pb )
{
	char			*dn, *attrs[2] = { SLAPI_USERPWD_ATTR, NULL };
	int			method, rc = LDAP_SUCCESS;
	struct berval		*credentials;
	struct berval		**pwvals;
    	Slapi_DN		*sdn = NULL;
	Slapi_Entry		*e = NULL;
	Slapi_Attr		*attr = NULL;

	/* Log a message to the server error log. */
	slapi_log_error( SLAPI_LOG_PLUGIN, "test_bind", 
		"Pre-operation bind function called.\n" );

	/* Gets parameters available when processing an LDAP bind
	   operation. */
	if ( slapi_pblock_get( pb, SLAPI_BIND_TARGET, &dn ) != 0 ||
	    slapi_pblock_get( pb, SLAPI_BIND_METHOD, &method ) != 0 ||
	    slapi_pblock_get( pb, SLAPI_BIND_CREDENTIALS, &credentials ) != 0 ) {

		slapi_log_error( SLAPI_LOG_PLUGIN, "test_bind",
			"Could not get parameters for bind operation\n" );
		slapi_send_ldap_result( pb, LDAP_OPERATIONS_ERROR, 
			NULL, NULL, 0, NULL );
		return( 1 );
	}

	/* Check the authentication method */
	switch( method ) {
	case LDAP_AUTH_SIMPLE:
		/* First, get the entry specified by the DN. */
		sdn = slapi_sdn_new_dn_byref( dn );
		rc = slapi_search_internal_get_entry( sdn, attrs, &e,
			    plugin_id );
		slapi_sdn_free( &sdn );

		if ( rc != LDAP_SUCCESS ) {
			slapi_log_error( SLAPI_LOG_PLUGIN, "test_bind",
					"Could not find entry %s (error %d)\n", 
					dn, rc );
			break;
		}

		/* Next, check credentials against the userpassword attribute
		   of that entry. */
		if ( e != NULL ) {
			Slapi_Value	*credval, **pwvals;
			int		i, hint, valcount;

			
			if ( slapi_entry_attr_find( e, SLAPI_USERPWD_ATTR,
			    &attr ) != 0 || slapi_attr_get_numvalues( attr,
			    &valcount ) != 0 ) {
				slapi_log_error( SLAPI_LOG_PLUGIN, "test_bind",
					"Entry has no %s attribute values\n",
					SLAPI_USERPWD_ATTR );
				rc = LDAP_INAPPROPRIATE_AUTH;
				break;
			}

			credval = slapi_value_new_berval( credentials );
			pwvals = (Slapi_Value **)slapi_ch_calloc( valcount,
			    sizeof( Slapi_Value * ));
			i = 0;
			for ( hint = slapi_attr_first_value( attr, &pwvals[i] );
			    hint != -1; hint = slapi_attr_next_value( attr,
			    hint, &pwvals[i] )) {
				++i;
			}

			if ( slapi_pw_find_sv( pwvals, credval ) != 0 ) {
				slapi_log_error( SLAPI_LOG_PLUGIN, "test_bind",
					"Credentials are not correct\n" );
				rc = LDAP_INVALID_CREDENTIALS;
			}

			slapi_value_free( &credval );
			slapi_ch_free( (void **)&pwvals );

			if ( LDAP_SUCCESS != rc ) {
			    break;
			}
		} else {
			/* This should not happen. The previous section of code 
			   already checks for this case. */
			slapi_log_error( SLAPI_LOG_PLUGIN, "test_bind",
				"Could find entry for %s\n", dn );
			rc = LDAP_NO_SUCH_OBJECT;
			break;
		}

		/* Set the DN and authentication method for the connection. */
		if ( slapi_pblock_set( pb, SLAPI_CONN_DN, 
			slapi_ch_strdup( dn ) ) != 0 ||
		     slapi_pblock_set( pb, SLAPI_CONN_AUTHMETHOD, 
			SLAPD_AUTH_SIMPLE ) != 0 ) {

			slapi_log_error( SLAPI_LOG_PLUGIN, "test_bind",
				"Failed to set DN and method for connection\n" );
			rc = LDAP_OPERATIONS_ERROR;
			break;
		}

		/* Send a "success" result code back to the client. */
		slapi_log_error( SLAPI_LOG_PLUGIN, "test_bind", 
			"Authenticated: %s\n", dn );
		rc = LDAP_SUCCESS;
		break;

	/* If NONE is specified, the client is requesting to bind anonymously.
	   Normally, this case should be handled by the server's front-end
	   before it calls this plug-in function.  Just in case this does
	   get through to the plug-in function, you can handle this by
	   sending a successful result code back to the client and returning
	   1.
	 */
	case LDAP_AUTH_NONE:
		slapi_log_error( SLAPI_LOG_PLUGIN, "test_bind", 
			"Authenticating anonymously\n" );
		rc = LDAP_SUCCESS;
		break;

	/* This plug-in does not support any other method of authentication */
	case LDAP_AUTH_SASL:
	default:
		slapi_log_error( SLAPI_LOG_PLUGIN, "test_bind",
			"Unsupported authentication method requested: %d\n",
			method );
		rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
		break;
	}

	slapi_send_ldap_result( pb, rc, NULL, NULL, 0, NULL );
	return( 1 );
}

/* Pre-operation plug-in function */
int
test_search( Slapi_PBlock *pb )
{
	char		*reqdn;

	/* Log a message to the server error log. */
	slapi_log_error( SLAPI_LOG_PLUGIN, "test_search", 
		"Pre-operation search function called.\n" );

	/* Get requestor of search operation.  This is not critical
	   to performing the search (this plug-in just serves as 
	   confirmation that the bind plug-in works), so return 0 
	   if this fails. */
	if ( slapi_pblock_get( pb, SLAPI_REQUESTOR_DN, &reqdn ) != 0 ) {

		slapi_log_error( SLAPI_LOG_PLUGIN, "test_search",
		"Could not get requestor parameter for search operation\n" );
		return( 0 );
	}

	/* Indicate who is requesting the search */
	if ( reqdn != NULL && *reqdn != '\0' ) {
		slapi_log_error( SLAPI_LOG_PLUGIN, "test_search",
			"Search requested by %s\n", reqdn );
	} else {
		slapi_log_error( SLAPI_LOG_PLUGIN, "test_search",
			"Search requested by anonymous client\n" );
	}
	return( 0 );
}

/* Initialization function */
#ifdef _WIN32
__declspec(dllexport)
#endif
int
testbind_init( Slapi_PBlock *pb )
{

	/* Retrieve and save the plugin identity to later pass to
	   internal operations */
	if ( slapi_pblock_get( pb, SLAPI_PLUGIN_IDENTITY, &plugin_id ) != 0 ) {
		slapi_log_error( SLAPI_LOG_PLUGIN, "testbind_init",
			"Failed to retrieve SLAPI_PLUGIN_IDENTITY\n" );
		return( -1 );
	}

	/* Register the pre-operation bind function and specify
	   the server plug-in version. */
	if ( slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION, 
		SLAPI_PLUGIN_VERSION_01 ) != 0 ||
	     slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION, 
		(void *)&bindpdesc ) != 0 ||
	     slapi_pblock_set( pb, SLAPI_PLUGIN_PRE_BIND_FN, 
		(void *) test_bind ) != 0 ||
	     slapi_pblock_set( pb, SLAPI_PLUGIN_PRE_SEARCH_FN, 
		(void *) test_search ) != 0 ) {

		slapi_log_error( SLAPI_LOG_PLUGIN, "testbind_init",
			"Failed to set version and functions\n" );
		return( -1 );
	}

	return( 0 );
}