The FreeRADIUS server $Id: f3670dba8951ca10eb4948feb3dc3db9423a334f $
Loading...
Searching...
No Matches
bind.c
Go to the documentation of this file.
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or (at
5 * your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17/**
18 * $Id: 87d453df20e3f8e3419f62d646aa3979cedb4a21 $
19 * @file lib/ldap/bind.c
20 * @brief Asynchronous bind functions for LDAP.
21 *
22 * @copyright 2017 Arran Cudbard-Bell (a.cudbardb@freeradius.org)
23 */
24RCSID("$Id: 87d453df20e3f8e3419f62d646aa3979cedb4a21 $")
25
27
28#include <freeradius-devel/ldap/base.h>
29#include <freeradius-devel/util/debug.h>
30
31/** Error reading from or writing to the file descriptor
32 *
33 * @param[in] el the event occurred in.
34 * @param[in] fd the event occurred on.
35 * @param[in] flags from kevent.
36 * @param[in] fd_errno The error that occurred.
37 * @param[in] uctx Connection config and handle.
38 */
40 UNUSED int flags, UNUSED int fd_errno, void *uctx)
41{
42 fr_ldap_bind_ctx_t *bind_ctx = talloc_get_type_abort(uctx, fr_ldap_bind_ctx_t);
43 fr_ldap_connection_t *c = bind_ctx->c;
44
45 talloc_free(bind_ctx);
46 fr_ldap_state_error(c); /* Restart the connection state machine */
47}
48
49/** Parse a bind response from a server
50 *
51 * @param[in] el the event occurred in.
52 * @param[in] fd the event occurred on.
53 * @param[in] flags from kevent.
54 * @param[in] uctx bind_ctx containing credentials, and connection config/handle.
55 */
56static void _ldap_bind_io_read(UNUSED fr_event_list_t *el, UNUSED int fd, UNUSED int flags, void *uctx)
57{
58 fr_ldap_bind_ctx_t *bind_ctx = talloc_get_type_abort(uctx, fr_ldap_bind_ctx_t);
59 fr_ldap_connection_t *c = bind_ctx->c;
60
61 fr_ldap_rcode_t status;
62
63 /*
64 * We're I/O driven, if there's no data someone lied to us
65 */
66 status = fr_ldap_result(NULL, NULL, c, bind_ctx->msgid, LDAP_MSG_ALL, bind_ctx->bind_dn, fr_time_delta_wrap(0));
67 switch (status) {
69 DEBUG2("Bind as \"%s\" to \"%s\" successful",
70 *bind_ctx->bind_dn ? bind_ctx->bind_dn : "(anonymous)", c->config->server);
71 fr_ldap_state_next(c); /* onto the next operation */
72 break;
73
75 PERROR("Bind as \"%s\" to \"%s\" not permitted",
76 *bind_ctx->bind_dn ? bind_ctx->bind_dn : "(anonymous)", c->config->server);
77 talloc_free(bind_ctx); /* Also removes fd events */
78 fr_ldap_state_error(c); /* Restart the connection state machine */
79 return;
80
81 default:
82 PERROR("Bind as \"%s\" to \"%s\" failed",
83 *bind_ctx->bind_dn ? bind_ctx->bind_dn : "(anonymous)", c->config->server);
84 talloc_free(bind_ctx); /* Also removes fd events */
85 fr_ldap_state_error(c); /* Restart the connection state machine */
86 return;
87 }
88 talloc_free(bind_ctx); /* Also removes fd events */
89}
90
91/** Send a bind request to a server
92 *
93 * @param[in] el the event occurred in.
94 * @param[in] fd the event occurred on.
95 * @param[in] flags from kevent.
96 * @param[in] uctx bind_ctx containing credentials, and connection config/handle.
97 */
98static void _ldap_bind_io_write(fr_event_list_t *el, int fd, UNUSED int flags, void *uctx)
99{
100 fr_ldap_bind_ctx_t *bind_ctx = talloc_get_type_abort(uctx, fr_ldap_bind_ctx_t);
101 fr_ldap_connection_t *c = bind_ctx->c;
102
103 LDAPControl *our_serverctrls[LDAP_MAX_CONTROLS];
104 LDAPControl *our_clientctrls[LDAP_MAX_CONTROLS];
105
106 int ret;
107 struct berval cred;
108
109 fr_ldap_control_merge(our_serverctrls, our_clientctrls,
110 NUM_ELEMENTS(our_serverctrls),
111 NUM_ELEMENTS(our_clientctrls),
112 c, bind_ctx->serverctrls, bind_ctx->clientctrls);
113
114 if (bind_ctx->password) {
115 memcpy(&cred.bv_val, &bind_ctx->password, sizeof(cred.bv_val));
116 cred.bv_len = talloc_strlen(bind_ctx->password);
117 } else {
118 cred.bv_val = NULL;
119 cred.bv_len = 0;
120 }
121
122 /*
123 * Yes, confusingly named. This is the simple version
124 * of the SASL bind function that should always be
125 * available.
126 */
127 ret = ldap_sasl_bind(c->handle, bind_ctx->bind_dn, LDAP_SASL_SIMPLE, &cred,
128 our_serverctrls, our_clientctrls, &bind_ctx->msgid);
129 switch (ret) {
130 /*
131 * If the handle was not connected, this operation
132 * can return either LDAP_X_CONNECTING or LDAP_SUCCESS
133 * depending on how fast the connection came up
134 * and whether it was connectionless.
135 */
136 case LDAP_X_CONNECTING: /* Connection in progress - retry later */
137 ret = ldap_get_option(c->handle, LDAP_OPT_DESC, &fd);
138 if (!fr_cond_assert(ret == LDAP_OPT_SUCCESS)) {
139 error:
140 talloc_free(bind_ctx);
142 fr_ldap_state_error(c); /* Restart the connection state machine */
143 return;
144 }
145
146 ret = fr_event_fd_insert(bind_ctx, NULL, el, fd,
147 NULL,
148 _ldap_bind_io_write, /* We'll be called again when the conn is open */
150 bind_ctx);
151 if (!fr_cond_assert(ret == 0)) goto error;
152 break;
153
154 case LDAP_SUCCESS:
155 if (fd < 0 ) {
156 ret = ldap_get_option(c->handle, LDAP_OPT_DESC, &fd);
157 if ((ret != LDAP_OPT_SUCCESS) || (fd < 0)) goto error;
158 }
159 c->fd = fd;
160 ret = fr_event_fd_insert(bind_ctx, NULL, el, fd,
162 NULL,
164 bind_ctx);
165 if (!fr_cond_assert(ret == 0)) goto error;
166 break;
167
168 default:
169 ERROR("Bind failed: %s", ldap_err2string(ret));
170 goto error;
171 }
172
174}
175
176/** Install I/O handlers for the bind operation
177 *
178 * @param[in] c connection to StartTLS on.
179 * @param[in] bind_dn Identity to bind with.
180 * @param[in] password Password to bind with.
181 * @param[in] serverctrls Extra controls to pass to the server.
182 * @param[in] clientctrls Extra controls to pass to libldap.
183 * @return
184 * - 0 on success.
185 * - -1 on failure.
186 */
188 char const *bind_dn, char const *password,
189 LDAPControl **serverctrls, LDAPControl **clientctrls)
190{
191 int fd = -1;
192 fr_ldap_bind_ctx_t *bind_ctx;
194
195 DEBUG2("Starting bind operation");
196
197 MEM(bind_ctx = talloc_zero(c, fr_ldap_bind_ctx_t));
198 bind_ctx->c = c;
199
200 /*
201 * Bind as anonymous user
202 */
203 bind_ctx->bind_dn = bind_dn ? bind_dn : "";
204 bind_ctx->password = password;
205 bind_ctx->serverctrls = serverctrls;
206 bind_ctx->clientctrls = clientctrls;
207
208 el = c->conn->el;
209
210 /*
211 * ldap_get_option can return a LDAP_SUCCESS even if the fd is not yet available
212 * - hence the test for fd >= 0
213 */
214 if ((ldap_get_option(c->handle, LDAP_OPT_DESC, &fd) == LDAP_SUCCESS) && (fd >= 0)) {
215 int ret;
216
217 ret = fr_event_fd_insert(bind_ctx, NULL, el, fd,
218 NULL,
221 bind_ctx);
222 if (!fr_cond_assert(ret == 0)) {
223 talloc_free(bind_ctx);
224 return -1;
225 }
226 } else {
227 /*
228 * Connections initialised with ldap_init() do not have a fd until
229 * the first request (usually bind) occurs - so this code path
230 * starts the bind process to open the connection.
231 */
232 _ldap_bind_io_write(el, -1, 0, bind_ctx);
233 }
234
235 return 0;
236}
237
238/** Yield interpreter after queueing LDAP bind
239 *
240 */
245
246/** Handle the return code from parsed LDAP results to set the module rcode
247 *
248 */
250{
251 fr_ldap_bind_auth_ctx_t *bind_auth_ctx = talloc_get_type_abort(uctx, fr_ldap_bind_auth_ctx_t);
252 fr_ldap_bind_ctx_t *bind_ctx = bind_auth_ctx->bind_ctx;
254
255 switch (bind_auth_ctx->ret) {
257 RDEBUG2("Bind as user \"%s\" was successful", bind_ctx->bind_dn);
258 break;
259
261 RDEBUG2("Bind as user \"%s\" not permitted", bind_ctx->bind_dn);
262 rcode = RLM_MODULE_DISALLOW;
263 break;
264
265 case LDAP_PROC_REJECT:
266 RDEBUG2("Bind as user \"%s\" rejected", bind_ctx->bind_dn);
267 rcode = RLM_MODULE_REJECT;
268 break;
269
270 case LDAP_PROC_BAD_DN:
271 rcode = RLM_MODULE_INVALID;
272 break;
273
275 rcode = RLM_MODULE_NOTFOUND;
276 break;
277
278 default:
279 rcode = RLM_MODULE_FAIL;
280 break;
281 }
282
283 if (bind_auth_ctx->treq) {
284 /*
285 * Bind auth ctx is freed by trunk request free.
286 */
287 trunk_request_signal_complete(bind_auth_ctx->treq);
288 } else {
289 /*
290 * If there is no trunk request, the request failed, and we need to free the ctx
291 */
292 talloc_free(bind_auth_ctx);
293 }
294
295 RETURN_UNLANG_RCODE(rcode);
296}
297
298/** Signal an outstanding LDAP bind request to cancel
299 *
300 */
301static void ldap_async_auth_bind_cancel(request_t *request, UNUSED fr_signal_t action, void *uctx)
302{
303 fr_ldap_bind_auth_ctx_t *bind_auth_ctx = talloc_get_type_abort(uctx, fr_ldap_bind_auth_ctx_t);
304
305 RWARN("Cancelling bind auth");
306 if (bind_auth_ctx->msgid > 0) fr_rb_remove(bind_auth_ctx->thread->binds, bind_auth_ctx);
307 trunk_request_signal_cancel(bind_auth_ctx->treq);
308}
309
310/** Initiate an async LDAP bind for authentication
311 *
312 * @param[out] p_result Where to write the result of the bind operation.
313 * If this is NULL, the rcode result will be discarded.
314 * - LDAP_PROC_SUCCES = RLM_MODULE_OK,
315 * - LDAP_PROC_NOT_PERMITTED = RLM_MODULE_DISALLOW,
316 * - LDAP_PROC_REJECT = RLM_MODULE_REJECT,
317 * - LDAP_PROC_BAD_DN = RLM_MODULE_INVALID,
318 * - LDAP_PROC_NO_RESULT = RLM_MODULE_NOTFOUND
319 * - * = RLM_MODULE_FAIL.
320 * @param[in] request this bind relates to.
321 * @param[in] thread whose connection the bind should be performed on.
322 * @param[in] bind_dn Identity to bind with.
323 * @param[in] password Password to bind with.
324 * @return
325 * - 0 on success.
326 * - -1 on failure.
327 */
328unlang_action_t fr_ldap_bind_auth_async(unlang_result_t *p_result, request_t *request, fr_ldap_thread_t *thread, char const *bind_dn, char const *password)
329{
330 fr_ldap_bind_auth_ctx_t *bind_auth_ctx;
331 trunk_request_t *treq;
333 trunk_enqueue_t ret;
334
335 if (!ttrunk) {
336 ERROR("Failed to get trunk connection for LDAP bind");
338 }
339
340 treq = trunk_request_alloc(ttrunk->trunk, request);
341 if (!treq) {
342 ERROR ("Failed to allocate trunk request for LDAP bind");
344 }
345
346 MEM(bind_auth_ctx = talloc(treq, fr_ldap_bind_auth_ctx_t));
347 *bind_auth_ctx = (fr_ldap_bind_auth_ctx_t) {
348 .treq = treq,
349 .request = request,
350 .thread = thread,
352 };
353
354 MEM(bind_auth_ctx->bind_ctx = talloc(bind_auth_ctx, fr_ldap_bind_ctx_t));
355 *bind_auth_ctx->bind_ctx = (fr_ldap_bind_ctx_t) {
356 .bind_dn = bind_dn,
357 .password = password
358 };
359
360 ret = trunk_request_enqueue(&bind_auth_ctx->treq, ttrunk->trunk, request, bind_auth_ctx, NULL);
361
362 switch (ret) {
363 case TRUNK_ENQUEUE_OK:
365 break;
366
367 default:
368 ERROR("Failed to enqueue bind request");
369 trunk_request_free(&treq);
371 }
372
373 return unlang_function_push_with_result(p_result,
374 request,
379 bind_auth_ctx);
380}
unlang_action_t
Returned by unlang_op_t calls, determine the next action of the interpreter.
Definition action.h:35
@ UNLANG_ACTION_YIELD
Temporarily pause execution until an event occurs.
Definition action.h:41
static void ldap_async_auth_bind_cancel(request_t *request, UNUSED fr_signal_t action, void *uctx)
Signal an outstanding LDAP bind request to cancel.
Definition bind.c:301
int fr_ldap_bind_async(fr_ldap_connection_t *c, char const *bind_dn, char const *password, LDAPControl **serverctrls, LDAPControl **clientctrls)
Install I/O handlers for the bind operation.
Definition bind.c:187
static unlang_action_t ldap_async_auth_bind_start(UNUSED unlang_result_t *p_result, UNUSED request_t *request, UNUSED void *uctx)
Yield interpreter after queueing LDAP bind.
Definition bind.c:241
static unlang_action_t ldap_async_auth_bind_results(unlang_result_t *p_result, request_t *request, void *uctx)
Handle the return code from parsed LDAP results to set the module rcode.
Definition bind.c:249
static void _ldap_bind_io_read(UNUSED fr_event_list_t *el, UNUSED int fd, UNUSED int flags, void *uctx)
Parse a bind response from a server.
Definition bind.c:56
static USES_APPLE_DEPRECATED_API void _ldap_bind_io_error(UNUSED fr_event_list_t *el, UNUSED int fd, UNUSED int flags, UNUSED int fd_errno, void *uctx)
Error reading from or writing to the file descriptor.
Definition bind.c:39
unlang_action_t fr_ldap_bind_auth_async(unlang_result_t *p_result, request_t *request, fr_ldap_thread_t *thread, char const *bind_dn, char const *password)
Initiate an async LDAP bind for authentication.
Definition bind.c:328
static void _ldap_bind_io_write(fr_event_list_t *el, int fd, UNUSED int flags, void *uctx)
Send a bind request to a server.
Definition bind.c:98
#define USES_APPLE_DEPRECATED_API
Definition build.h:499
#define RCSID(id)
Definition build.h:512
#define UNUSED
Definition build.h:336
#define NUM_ELEMENTS(_t)
Definition build.h:358
#define fr_cond_assert(_x)
Calls panic_action ifndef NDEBUG, else logs error and evaluates to value of _x.
Definition debug.h:131
#define MEM(x)
Definition debug.h:36
#define ERROR(fmt,...)
Definition dhcpclient.c:40
#define fr_event_fd_insert(...)
Definition event.h:247
#define unlang_function_push_with_result(_result_p, _request, _func, _repeat, _signal, _sigmask, _top_frame, _uctx)
Push a generic function onto the unlang stack that produces a result.
Definition function.h:144
talloc_free(hp)
#define UNLANG_SUB_FRAME
Definition interpret.h:37
LDAPControl ** clientctrls
Controls to pass to the client (library).
Definition base.h:500
fr_ldap_rcode_t ret
Return code of bind operation.
Definition base.h:626
int msgid
libldap msgid for this bind.
Definition base.h:619
char * server
Initial server to bind to.
Definition base.h:224
LDAP * handle
libldap handle.
Definition base.h:333
void fr_ldap_control_merge(LDAPControl *serverctrls_out[], LDAPControl *clientctrls_out[], size_t serverctrls_len, size_t clientctrls_len, fr_ldap_connection_t *conn, LDAPControl *serverctrls_in[], LDAPControl *clientctrls_in[])
Merge connection and call specific client and server controls.
Definition control.c:48
int fd
File descriptor for this connection.
Definition base.h:349
void fr_ldap_state_error(fr_ldap_connection_t *c)
Signal that there's been an error on the connection.
Definition state.c:134
int msgid
Of the bind operation. Only used when binding as admin.
Definition base.h:502
fr_ldap_config_t const * config
rlm_ldap connection configuration.
Definition base.h:344
fr_ldap_connection_t * c
to bind. Only used when binding as admin user.
Definition base.h:496
trunk_request_t * treq
Trunk request this bind is associated with.
Definition base.h:618
fr_ldap_state_t fr_ldap_state_next(fr_ldap_connection_t *c)
Move between LDAP connection states.
Definition state.c:49
LDAPControl ** serverctrls
Controls to pass to the server.
Definition base.h:499
fr_ldap_thread_trunk_t * fr_thread_ldap_bind_trunk_get(fr_ldap_thread_t *thread)
Find the thread specific trunk to use for LDAP bind auths.
#define LDAP_MAX_CONTROLS
Maximum number of client/server controls.
Definition base.h:94
int fr_ldap_connection_timeout_reset(fr_ldap_connection_t const *conn)
Definition connection.c:431
char const * bind_dn
of the user, may be NULL to bind anonymously.
Definition base.h:497
trunk_t * trunk
Connection trunk.
Definition base.h:407
fr_ldap_thread_t * thread
This bind is being run by.
Definition base.h:617
connection_t * conn
Connection state handle.
Definition base.h:345
fr_rb_tree_t * binds
Tree of outstanding bind auths.
Definition base.h:388
char const * password
of the user, may be NULL if no password is specified.
Definition base.h:498
fr_ldap_rcode_t
Codes returned by fr_ldap internal functions.
Definition base.h:584
@ LDAP_PROC_SUCCESS
Operation was successful.
Definition base.h:587
@ LDAP_PROC_NOT_PERMITTED
Operation was not permitted, either current user was locked out in the case of binds,...
Definition base.h:594
@ LDAP_PROC_REJECT
Bind failed, user was rejected.
Definition base.h:598
@ LDAP_PROC_BAD_DN
Specified an invalid object in a bind or search DN.
Definition base.h:600
@ LDAP_PROC_NO_RESULT
Got no results.
Definition base.h:602
Holds arguments for async bind auth requests.
Definition base.h:615
Holds arguments for the async bind operation.
Definition base.h:495
Tracks the state of a libldap connection handle.
Definition base.h:332
Thread specific structure to manage LDAP trunk connections.
Definition base.h:381
Thread LDAP trunk structure.
Definition base.h:401
fr_ldap_rcode_t fr_ldap_result(LDAPMessage **result, LDAPControl ***ctrls, fr_ldap_connection_t const *conn, int msgid, int all, char const *dn, fr_time_delta_t timeout)
Parse response from LDAP server dealing with any errors.
Definition base.c:449
#define PERROR(_fmt,...)
Definition log.h:228
#define RWARN(fmt,...)
Definition log.h:309
Stores all information relating to an event list.
Definition event.c:377
#define RDEBUG2(fmt,...)
#define DEBUG2(fmt,...)
void * fr_rb_remove(fr_rb_tree_t *tree, void const *data)
Remove an entry from the tree, without freeing the data.
Definition rb.c:695
#define RETURN_UNLANG_RCODE(_rcode)
Definition rcode.h:61
#define RETURN_UNLANG_FAIL
Definition rcode.h:63
rlm_rcode_t
Return codes indicating the result of the module call.
Definition rcode.h:44
@ RLM_MODULE_INVALID
The module considers the request invalid.
Definition rcode.h:51
@ RLM_MODULE_OK
The module is OK, continue.
Definition rcode.h:49
@ RLM_MODULE_FAIL
Module failed, don't reply.
Definition rcode.h:48
@ RLM_MODULE_DISALLOW
Reject the request (user is locked out).
Definition rcode.h:52
@ RLM_MODULE_REJECT
Immediately reject the request.
Definition rcode.h:47
@ RLM_MODULE_NOTFOUND
User not found.
Definition rcode.h:53
fr_signal_t
Signals that can be generated/processed by request signal handlers.
Definition signal.h:38
@ FR_SIGNAL_CANCEL
Request has been cancelled.
Definition signal.h:40
static size_t talloc_strlen(char const *s)
Returns the length of a talloc array containing a string.
Definition talloc.h:143
#define fr_time_delta_wrap(_time)
Definition time.h:152
trunk_request_t * trunk_request_alloc(trunk_t *trunk, request_t *request)
(Pre-)Allocate a new trunk request
Definition trunk.c:2524
trunk_enqueue_t trunk_request_enqueue(trunk_request_t **treq_out, trunk_t *trunk, request_t *request, void *preq, void *rctx)
Enqueue a request that needs data written to the trunk.
Definition trunk.c:2639
void trunk_request_signal_cancel(trunk_request_t *treq)
Cancel a trunk request.
Definition trunk.c:2198
void trunk_request_free(trunk_request_t **treq_to_free)
If the trunk request is freed then update the target requests.
Definition trunk.c:2368
void trunk_request_signal_complete(trunk_request_t *treq)
Signal that a trunk request is complete.
Definition trunk.c:2140
Wraps a normal request.
Definition trunk.c:99
trunk_enqueue_t
Definition trunk.h:149
@ TRUNK_ENQUEUE_OK
Operation was successful.
Definition trunk.h:151
@ TRUNK_ENQUEUE_IN_BACKLOG
Request should be enqueued in backlog.
Definition trunk.h:150
static fr_event_list_t * el