The FreeRADIUS server $Id: a4e7f8c94e8038b7627bcf52356b7ed15955c1d7 $
Loading...
Searching...
No Matches
connection.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: 15182f9d1a2b492b8c1273110fd569eef1a6c543 $
19 * @file lib/ldap/connection.c
20 * @brief Asynchronous connection management functions for LDAP.
21 *
22 * @copyright 2017 Arran Cudbard-Bell (a.cudbardb@freeradius.org)
23 */
24RCSID("$Id: 15182f9d1a2b492b8c1273110fd569eef1a6c543 $")
25
27
28#include <freeradius-devel/ldap/base.h>
29#include <freeradius-devel/util/debug.h>
30
31/*
32 * Lookup of libldap result message types to meaningful strings
33 */
34static char const *ldap_msg_types[UINT8_MAX + 1] = {
35 [LDAP_RES_BIND] = "bind response",
36 [LDAP_RES_SEARCH_ENTRY] = "search entry",
37 [LDAP_RES_SEARCH_REFERENCE] = "search reference",
38 [LDAP_RES_SEARCH_RESULT] = "search result",
39 [LDAP_RES_MODIFY] = "modify response",
40 [LDAP_RES_ADD] = "add response",
41 [LDAP_RES_DELETE] = "delete response",
42 [LDAP_RES_MODDN] = "modify dn response",
43 [LDAP_RES_COMPARE] = "compare response",
44 [LDAP_RES_EXTENDED] = "extended response",
45 [LDAP_RES_INTERMEDIATE] = "intermediate response"
46};
47
48
49/** Allocate and configure a new connection
50 *
51 * Configures both our ldap handle, and libldap's handle.
52 *
53 * This can be used by async code and async code as no attempt is made to connect
54 * to the LDAP server. An attempt will only be made if ldap_start_tls* or ldap_bind*
55 * functions are called.
56 *
57 * If called on an #fr_ldap_connection_t which has already been initialised, will
58 * clear any memory allocated to the connection, unbind the ldap handle, and reinitialise
59 * everything.
60 *
61 * @param[in] c to configure.
62 * @param[in] config to apply.
63 * @return
64 * - 0 on success.
65 * - -1 on error.
66 */
68{
69 LDAP *handle = NULL;
70 int ldap_errno, ldap_version, keepalive, probes, is_server;
71 struct timeval no_wait = {
72 .tv_sec = 0,
73 .tv_usec = 0
74 };
75
76 fr_assert(config->server);
77
78 ldap_errno = ldap_initialize(&handle, config->server);
79 if (ldap_errno != LDAP_SUCCESS) {
80 ERROR("ldap_initialize failed: %s", ldap_err2string(ldap_errno));
81 error:
82 return -1;
83 }
84
85 DEBUG3("New connection %p libldap handle %p", c, handle);
86
87 c->config = config;
88 c->handle = handle;
89
90 /*
91 * We now have a connection structure, but no actual connection.
92 *
93 * Set a bunch of LDAP options, using common code.
94 */
95#define do_ldap_option(_option, _name, _value) \
96 if (ldap_set_option(c->handle, _option, _value) != LDAP_OPT_SUCCESS) do { \
97 ldap_get_option(c->handle, LDAP_OPT_ERROR_NUMBER, &ldap_errno); \
98 ERROR("Failed setting connection option %s: %s", _name, \
99 (ldap_errno != LDAP_SUCCESS) ? ldap_err2string(ldap_errno) : "Unknown error"); \
100 goto error;\
101 } while (0)
102
103DIAG_OFF(unused-macros)
104#define maybe_ldap_option(_option, _name, _value) \
105 if (_value) do_ldap_option(_option, _name, _value)
106DIAG_ON(unused-macros)
107
108 /*
109 * Leave "dereference" unset to use the OpenLDAP default.
110 */
111 if (config->dereference_str) do_ldap_option(LDAP_OPT_DEREF, "dereference", &(config->dereference));
112
113 /*
114 * We handle our own referral chasing as there is no way to
115 * get the fd for a referred query.
116 */
117 do_ldap_option(LDAP_OPT_REFERRALS, "chase_referrals", LDAP_OPT_OFF);
118
119 /*
120 * libldap must never wait on the network, we do all the waiting
121 * in the event loop, and enforce deadlines with our own timers.
122 * The one exception is the StartTLS handshake, see start_tls.c.
123 */
124 do_ldap_option(LDAP_OPT_NETWORK_TIMEOUT, "network_timeout", &no_wait);
125
126 do_ldap_option(LDAP_OPT_TIMELIMIT, "srv_timelimit", &fr_time_delta_to_timeval(config->srv_timelimit));
127
128 ldap_version = LDAP_VERSION3;
129 do_ldap_option(LDAP_OPT_PROTOCOL_VERSION, "ldap_version", &ldap_version);
130
131 keepalive = (int) fr_time_delta_to_sec(config->keepalive_idle);
132 do_ldap_option(LDAP_OPT_X_KEEPALIVE_IDLE, "keepalive_idle", &keepalive);
133
134 probes = config->keepalive_probes;
135 do_ldap_option(LDAP_OPT_X_KEEPALIVE_PROBES, "keepalive_probes", &probes);
136
137 keepalive = (int) fr_time_delta_to_sec(config->keepalive_interval);
138 do_ldap_option(LDAP_OPT_X_KEEPALIVE_INTERVAL, "keepalive_interval", &keepalive);
139
140 if (config->sasl_secprops) do_ldap_option(LDAP_OPT_X_SASL_SECPROPS, "sasl_secprops", config->sasl_secprops);
141
142 /*
143 * Everything after this point is TLS related - so don't set if TLS not in use.
144 */
145 if (!config->tls_mode && !config->start_tls) return 0;
146
147 /*
148 * Set all of the TLS options
149 */
150 if (config->tls_mode) do_ldap_option(LDAP_OPT_X_TLS, "tls_mode", &(config->tls_mode));
151
152 maybe_ldap_option(LDAP_OPT_X_TLS_CACERTFILE, "ca_file", config->tls_ca_file);
153 maybe_ldap_option(LDAP_OPT_X_TLS_CACERTDIR, "ca_path", config->tls_ca_path);
154
155 /*
156 * Set certificate options
157 */
158 maybe_ldap_option(LDAP_OPT_X_TLS_CERTFILE, "certificate_file", config->tls_certificate_file);
159 maybe_ldap_option(LDAP_OPT_X_TLS_KEYFILE, "private_key_file", config->tls_private_key_file);
160
161 if (config->tls_require_cert_str) {
162 do_ldap_option(LDAP_OPT_X_TLS_REQUIRE_CERT, "require_cert", &config->tls_require_cert);
163 }
164
165 if (config->tls_min_version_str) {
166 do_ldap_option(LDAP_OPT_X_TLS_PROTOCOL_MIN, "tls_min_version", &config->tls_min_version);
167 }
168
169 /*
170 * Counter intuitively the TLS context appears to need to be initialised
171 * after all the TLS options are set on the handle.
172 */
173
174 /* Always use the new TLS configuration context */
175 is_server = 0;
176 do_ldap_option(LDAP_OPT_X_TLS_NEWCTX, "new TLS context", &is_server);
177
178 if (config->start_tls) {
179 if (config->port == LDAPS_PORT) {
180 WARN("Told to Start TLS on LDAPS port this will probably fail, please correct the "
181 "configuration");
182 }
183 }
184
185 return 0;
186}
187
188/** Free the handle, closing the connection to ldap
189 *
190 * @param[in] el UNUSED.
191 * @param[in] h to close.
192 * @param[in] uctx Connection config and handle.
193 */
195{
196 fr_ldap_connection_t *c = talloc_get_type_abort(h, fr_ldap_connection_t);
197
198 /*
199 * Explicitly remove the file descriptor event
200 *
201 * Even if the fr_ldap_connection_t has outstanding
202 * queries, we still don't want its fd in the event loop.
203 */
204 if (c->fd >= 0) {
206 c->fd = -1;
207 }
208
209 talloc_free(h);
210}
211
212/** Close and delete a connection
213 *
214 * Unbinds the LDAP connection, informing the server and freeing any memory, then releases the memory used by the
215 * connection handle.
216 *
217 * @param[in] c to destroy.
218 * @return always indicates success.
219 */
221{
222 /*
223 * If there are any pending queries, don't free
224 */
225 if (((c->queries) && (fr_rb_num_elements(c->queries) > 0)) || (fr_dlist_num_elements(&c->refs) > 0)) return -1;
226
227 talloc_free_children(c); /* Force inverted free order */
228
229 if (c->handle) {
230 LDAPControl *our_serverctrls[LDAP_MAX_CONTROLS];
231 LDAPControl *our_clientctrls[LDAP_MAX_CONTROLS];
232
233 fr_ldap_control_merge(our_serverctrls, our_clientctrls,
234 NUM_ELEMENTS(our_serverctrls),
235 NUM_ELEMENTS(our_clientctrls),
236 c, NULL, NULL);
237
238 DEBUG3("Closing connection %p libldap handle %p", c, c->handle);
239 ldap_unbind_ext(c->handle, our_serverctrls, our_clientctrls); /* Same code as ldap_unbind_ext_s */
240 }
241
243
244 return 0;
245}
246
247/** Allocate our ldap connection handle layer
248 *
249 * This is using handles outside of the connection state machine.
250 *
251 * @param[in] ctx to allocate connection handle in.
252 * @return
253 * - A new unbound/unconfigured connection handle on success.
254 * Call f#r_ldap_connection_configure next.
255 * - NULL on OOM.
256 */
258{
260
261 /*
262 * Allocate memory for the handle.
263 */
264 c = talloc_zero(ctx, fr_ldap_connection_t);
265 if (!c) return NULL;
266
267 talloc_set_destructor(c, _ldap_connection_free);
268
269 /*
270 * Ensure the fd is invalid to start with, preventing
271 * attempts to remove fd events if the server is shut down
272 * before the LDAP connection is established
273 */
274 c->fd = -1;
275
276 return c;
277}
278
279/** Watcher for LDAP connections being closed
280 *
281 * If there are any outstanding queries on the connection then
282 * re-parent the connection to the NULL ctx so that it remains
283 * until all the queries have been dealt with.
284 */
286 UNUSED connection_state_t state, void *uctx)
287{
288 fr_ldap_connection_t *ldap_conn = talloc_get_type_abort(uctx, fr_ldap_connection_t);
289
290 if ((fr_rb_num_elements(ldap_conn->queries) == 0) && (fr_dlist_num_elements(&ldap_conn->refs) == 0)) return;
291
292 talloc_reparent(conn, NULL, ldap_conn);
293 ldap_conn->conn = NULL;
294}
295
296/** (Re-)Initialises the libldap side of the connection handle
297 *
298 * The first ldap state transition is either:
299 *
300 * init -> start tls
301 * or
302 * init -> bind
303 *
304 * Either way libldap will try an open the connection so when fr_ldap_state_next
305 * returns we should have the file descriptor to pass back.
306 *
307 * The complete order of operations is:
308 *
309 * - Initialise the libldap handle with fr_ldap_connection_configure (calls ldap_init)
310 * - Initiate the connection with fr_ldap_state_next, which either binds or calls start_tls.
311 * - Either operation calls ldap_send_server_request.
312 * - Which calls ldap_new_connection.
313 * - Which calls ldap_int_open_connection.
314 * - Which calls ldap_connect_to_(host|path) and adds socket buffers, and possibly
315 * calls ldap_int_tls_start (for ldaps://).
316 * - When ldap_new_connection returns, because LDAP_OPT_CONNECT_ASYNC
317 * is set to LDAP_OPT_ON, lc->lconn_status is set to LDAP_CONNST_CONNECTING.
318 * - ldap_send_server_request checks for lconn_stats == LDAP_CONNST_CONNECTING,
319 * and calls ldap_int_poll, which checks the fd for error conditions
320 * and immediately returns due to the network timeout value.
321 * - If the socket is not yet connected:
322 * - As network timeout on the LDAP handle is 0, ld->ld_errno is set to
323 * LDAP_X_CONNECTING. ldap_send_server_request returns -1.
324 * - bind or start_tls errors with LDAP_X_CONNECTING without sending the request.
325 * - We install a write I/O handler, and wait to be called again, then we retry the
326 * operation.
327 * - else
328 * - the bind or start_tls operation succeeds, our ldap state machine advances,
329 * the connection callback is called and our socket state machine transitions to
330 * connected.
331 * - Continue running the state machine
332 *
333 * @param[out] h Underlying file descriptor from libldap handle.
334 * @param[in] conn Being initialised.
335 * @param[in] config LDAP connection configuration.
336 * @param[in] directory Where the properties of the directory being connected to are recorded.
337 * May be NULL if directory discovery is not being performed.
338 * @return
339 * - CONNECTION_STATE_CONNECTING on success.
340 * - CONNECTION_STATE_FAILED on failure.
341 */
344{
346 fr_ldap_state_t state;
347
348 c = fr_ldap_connection_alloc(conn);
349 c->conn = conn;
350 c->directory = directory;
351 /*
352 * Initialise tree for outstanding queries handled by this connection
353 */
356
357 /*
358 * Configure/allocate the libldap handle
359 */
361 error:
362 talloc_free(c);
364 }
365
366 /*
367 * Don't block. libldap returns LDAP_X_CONNECTING while connect()
368 * is in progress, and the connection state machine's own timer
369 * bounds the attempt.
370 */
371 if (ldap_set_option(c->handle, LDAP_OPT_CONNECT_ASYNC, LDAP_OPT_ON) != LDAP_OPT_SUCCESS) goto error;
372
373 state = fr_ldap_state_next(c);
374 if (state == FR_LDAP_STATE_ERROR) goto error;
375
377
378 *h = c; /* Set the handle */
379
381}
382
383/** Initialise a standalone LDAP connection
384 *
385 * @param[out] h Underlying file descriptor from libldap handle.
386 * @param[in] conn Being initialised.
387 * @param[in] uctx Connection configuration (a #fr_ldap_config_t).
388 * @return
389 * - CONNECTION_STATE_CONNECTING on success.
390 * - CONNECTION_STATE_FAILED on failure.
391 */
392CC_NO_UBSAN(function) /* UBSAN: false positive - public vs private connection_t trips --fsanitize=function*/
394{
395 fr_ldap_config_t const *config = uctx;
396
397 return ldap_connection_init(h, conn, config, NULL);
398}
399
400/** Initialise an LDAP trunk connection
401 *
402 * @param[out] h Underlying file descriptor from libldap handle.
403 * @param[in] conn Being initialised.
404 * @param[in] uctx Trunk the connection belongs to (a #fr_ldap_thread_trunk_t).
405 * @return
406 * - CONNECTION_STATE_CONNECTING on success.
407 * - CONNECTION_STATE_FAILED on failure.
408 */
409CC_NO_UBSAN(function) /* UBSAN: false positive - public vs private connection_t trips --fsanitize=function*/
411{
412 fr_ldap_thread_trunk_t *ttrunk = talloc_get_type_abort(uctx, fr_ldap_thread_trunk_t);
413
414 return ldap_connection_init(h, conn, &ttrunk->config, ttrunk->directory);
415}
416
417/** Callback for closing idle LDAP trunk
418 *
419 */
421{
422 fr_ldap_thread_trunk_t *ttrunk = talloc_get_type_abort(uctx, fr_ldap_thread_trunk_t);
423
424 if (ttrunk->trunk->req_alloc == 0) {
425 DEBUG2("Removing idle LDAP trunk to \"%s\"", ttrunk->uri);
426 talloc_free(ttrunk->trunk);
427 talloc_free(ttrunk);
428 } else {
429 /*
430 * There are still pending queries - insert a new event
431 */
432 (void) fr_timer_in(ttrunk, tl, &ttrunk->ev, ttrunk->t->config->idle_timeout,
433 false, _ldap_trunk_idle_timeout, ttrunk);
434 }
435}
436
437/** Callback when an LDAP trunk request is cancelled
438 *
439 * Ensure the request is removed from the list of outstanding requests
440 */
442 UNUSED void *uctx) {
443 fr_ldap_query_t *query = talloc_get_type_abort(preq, fr_ldap_query_t);
444
445 if (query->ldap_conn) {
446 fr_rb_remove(NULL, query->ldap_conn->queries, query);
447 query->ldap_conn = NULL;
448 }
449}
450
451/** Callback to cancel LDAP queries
452 *
453 * Inform the remote LDAP server that we no longer want responses to specific queries.
454 *
455 * @param[in] el For timer management.
456 * @param[in] tconn The trunk connection handle
457 * @param[in] conn The specific connection queries will be cancelled on
458 * @param[in] uctx Context provided to trunk_alloc
459 */
460CC_NO_UBSAN(function) /* UBSAN: false positive - public vs private connection_t trips --fsanitize=function*/
462 connection_t *conn, UNUSED void *uctx)
463{
464 trunk_request_t *treq;
465 fr_ldap_connection_t *ldap_conn = talloc_get_type_abort(conn->h, fr_ldap_connection_t);
466 fr_ldap_query_t *query;
467
468 while ((trunk_connection_pop_cancellation(&treq, tconn)) == 0) {
469 query = talloc_get_type_abort(treq->preq, fr_ldap_query_t);
470 ldap_abandon_ext(ldap_conn->handle, query->msgid, NULL, NULL);
471
473 }
474}
475
476/** Callback to tidy up when a trunk request fails
477 *
478 */
479static void ldap_request_fail(request_t *request, void *preq, UNUSED void *rctx,
480 UNUSED trunk_request_state_t state, UNUSED void *uctx)
481{
482 fr_ldap_query_t *query = talloc_get_type_abort(preq, fr_ldap_query_t);
483
484 /*
485 * Failed trunk requests get freed - so remove association in query.
486 */
487 query->treq = NULL;
488 query->ret = LDAP_RESULT_ERROR;
489
490 /*
491 * Ensure request is runnable.
492 */
494}
495
496TRUNK_NOTIFY_FUNC(ldap_trunk_connection_notify, fr_ldap_connection_t)
497
498/** Allocate an LDAP trunk connection
499 *
500 * @param[in] tconn Trunk handle.
501 * @param[in] el Event list which will be used for I/O and timer events.
502 * @param[in] conn_conf Configuration of the connection.
503 * @param[in] log_prefix What to prefix log messages with.
504 * @param[in] uctx User context passed to trunk_alloc.
505 */
506CC_NO_UBSAN(function) /* UBSAN: false positive - public vs private connection_t trips --fsanitize=function*/
508 connection_conf_t const *conn_conf,
509 char const *log_prefix, void *uctx)
510{
511 fr_ldap_thread_trunk_t *thread_trunk = talloc_get_type_abort(uctx, fr_ldap_thread_trunk_t);
512 connection_t *conn;
513
514 conn = connection_alloc(tconn, el,
518 },
519 conn_conf, log_prefix, thread_trunk);
520 if (!conn) {
521 PERROR("Failed allocating state handler for new LDAP trunk connection");
522 return NULL;
523 }
524
525 return conn;
526}
527
528#define POPULATE_LDAP_CONTROLS(_dest, _src) do { \
529 int i; \
530 for (i = 0; (i < LDAP_MAX_CONTROLS) && (_src[i].control); i++) { \
531 _dest[i] = _src[i].control; \
532 } \
533 _dest[i] = NULL; \
534} while (0)
535
536/** Take LDAP pending queries from the queue and send them.
537 *
538 * @param[in] el Event list for timers.
539 * @param[in] tconn Trunk handle.
540 * @param[in] conn on which to send the queries
541 * @param[in] uctx User context passed to trunk_alloc
542 */
543CC_NO_UBSAN(function) /* UBSAN: false positive - public vs private connection_t trips --fsanitize=function*/
545 connection_t *conn, UNUSED void *uctx)
546{
547 fr_ldap_connection_t *ldap_conn = talloc_get_type_abort(conn->h, fr_ldap_connection_t);
548 trunk_request_t *treq;
549
550 LDAPURLDesc *referral_url = NULL;
551
552 fr_ldap_query_t *query = NULL;
553 fr_ldap_rcode_t status;
554
555 while (trunk_connection_pop_request(&treq, tconn) == 0) {
556 LDAPControl *our_serverctrls[LDAP_MAX_CONTROLS + 1];
557 LDAPControl *our_clientctrls[LDAP_MAX_CONTROLS + 1];
558
559 if (!treq) break;
560
561 query = talloc_get_type_abort(treq->preq, fr_ldap_query_t);
562
563 switch (query->type) {
565 /*
566 * This query is a LDAP search
567 */
568 if (query->referral) referral_url = query->referral->referral_url;
569
570 POPULATE_LDAP_CONTROLS(our_serverctrls, query->serverctrls);
571 POPULATE_LDAP_CONTROLS(our_clientctrls, query->clientctrls);
572
573 /*
574 * If we are chasing a referral, referral_url will be populated and may
575 * have a base dn or scope to override the original query
576 */
577 status = fr_ldap_search_async(&query->msgid, query->treq->request, ldap_conn,
578 (referral_url && referral_url->lud_dn) ?
579 referral_url->lud_dn : query->dn,
580 (referral_url && referral_url->lud_scope) ?
581 referral_url->lud_scope : query->search.scope,
582 query->search.filter, query->search.attrs,
583 our_serverctrls, our_clientctrls);
584 break;
585
587 /*
588 * Send a request to modify an object
589 */
590 POPULATE_LDAP_CONTROLS(our_serverctrls, query->serverctrls);
591 POPULATE_LDAP_CONTROLS(our_clientctrls, query->clientctrls);
592
593 status = fr_ldap_modify_async(&query->msgid, query->treq->request,
594 ldap_conn, query->dn, query->mods,
595 our_serverctrls, our_clientctrls);
596 break;
597
599 /*
600 * Send a request to delete an object
601 */
602 POPULATE_LDAP_CONTROLS(our_serverctrls, query->serverctrls);
603 POPULATE_LDAP_CONTROLS(our_clientctrls, query->clientctrls);
604
605 status = fr_ldap_delete_async(&query->msgid, query->treq->request,
606 ldap_conn, query->dn,
607 our_serverctrls, our_clientctrls);
608 break;
609
611 /*
612 * This query is an LDAP extended operation.
613 */
614 status = fr_ldap_extended_async(&query->msgid, query->treq->request, ldap_conn,
615 query->extended.reqoid, query->extended.reqdata);
616 break;
617
618 default:
619 status = LDAP_PROC_ERROR;
620 ERROR("Invalid LDAP query for trunk connection");
621 error:
624 continue;
625
626 }
627
628 if (status != LDAP_PROC_SUCCESS) goto error;
629
630 /*
631 * If the query has previously been associated with a different
632 * connection, remove that reference. Typically when following references.
633 */
634 if (query->ldap_conn) fr_dlist_remove(&query->ldap_conn->refs, query);
635
636 /*
637 * Record which connection was used for this query
638 * - results processing often needs access to an LDAP handle
639 */
640 query->ldap_conn = ldap_conn;
641
642 /*
643 * Add the query to the tree of pending queries for this trunk
644 */
645 fr_rb_insert(query->ldap_conn->queries, query);
646
648 }
649
650}
651
652/** Read LDAP responses
653 *
654 * Responses from the LDAP server will cause the fd to become readable and trigger this
655 * callback. Most LDAP search responses have multiple messages in their response - we
656 * only gather those which are complete before either following a referral or passing
657 * the head of the resulting chain of messages back.
658 *
659 * @param[in] el To insert timers into.
660 * @param[in] tconn Trunk connection associated with these results.
661 * @param[in] conn Connection handle for these results.
662 * @param[in] uctx Thread specific trunk structure - contains tree of pending queries.
663 */
664CC_NO_UBSAN(function) /* UBSAN: false positive - public vs private connection_t trips --fsanitize=function*/
666{
667 fr_ldap_connection_t *ldap_conn = talloc_get_type_abort(conn->h, fr_ldap_connection_t);
668 fr_ldap_thread_trunk_t *ttrunk = talloc_get_type_abort(uctx, fr_ldap_thread_trunk_t);
669
670 int ret = 0, msgtype;
671 struct timeval poll = { 0, 10 };
672 LDAPMessage *result = NULL;
673 fr_ldap_rcode_t rcode;
674 fr_ldap_query_t find = { .msgid = -1 }, *query = NULL;
676 bool really_no_result = false;
677 trunk_request_t *treq;
678
679 /*
680 * Reset the idle timeout event
681 */
682 (void) fr_timer_in(ttrunk, el->tl, &ttrunk->ev,
683 ttrunk->t->config->idle_timeout, false, _ldap_trunk_idle_timeout, ttrunk);
684
685 do {
686 /*
687 * Look for any results for which we have the complete result message
688 * ldap_result will return a pointer to a chain of messages.
689 *
690 * The first time ldap_result is called when there's pending network
691 * data, it may read the data, but not return any results.
692 *
693 * In order to fix the spurious debugging messages and overhead,
694 * if this is the first iteration through the loop and ldap_result
695 * returns no result (0), we call it again.
696 */
697 ret = ldap_result(ldap_conn->handle, LDAP_RES_ANY, LDAP_MSG_ALL, &poll, &result);
698 switch (ret) {
699 case 0:
700 if (really_no_result) return;
701 really_no_result = true;
702 continue;
703
704 case -1:
705 rcode = fr_ldap_error_check(NULL, ldap_conn, NULL, NULL);
706 if (rcode == LDAP_PROC_BAD_CONN) {
707 ERROR("Bad LDAP connection");
709 }
710 return;
711
712 default:
713 /*
714 * We only retry ldap_result the first time through the loop.
715 */
716 really_no_result = true;
717 break;
718 }
719
720 find.msgid = ldap_msgid(result);
721 fr_rb_find((void **)&query, ldap_conn->queries, &find);
722
723 if (!query) {
724 WARN("Ignoring msgid %i - doesn't match any outstanding queries (it may have been cancelled)",
725 find.msgid);
726 ldap_msgfree(result);
727 continue;
728 }
729
730 /*
731 * Remove the query from the tree of outstanding queries
732 */
733 fr_rb_remove(NULL, ldap_conn->queries, query);
734
735 /*
736 * Add the query to the list of queries referencing this connection.
737 * Prevents the connection from being freed until the query has finished using it.
738 */
739 fr_dlist_insert_tail(&ldap_conn->refs, query);
740
741 /*
742 * This really shouldn't happen - as we only retrieve complete sets of results -
743 * but as the query data structure will last until its results are fully handled
744 * better to have this safety check here.
745 */
746 if (query->ret != LDAP_RESULT_PENDING) {
747 WARN("Received results for msgid %i which has already been handled - ignoring", find.msgid);
748 ldap_msgfree(result);
749 continue;
750 }
751
752 msgtype = ldap_msgtype(result);
753
754 /*
755 * Request to reference in debug output
756 */
757 request = query->treq->request;
758
759 ROPTIONAL(RDEBUG2, DEBUG2, "Got %s response for message %d",
760 ((unsigned int)msgtype < NUM_ELEMENTS(ldap_msg_types) && ldap_msg_types[msgtype]) ?
761 ldap_msg_types[msgtype] : "unknown", query->msgid);
762 rcode = fr_ldap_error_check(NULL, ldap_conn, result, query->dn);
763
764 switch (rcode) {
766 switch (query->type) {
768 query->ret = (ldap_count_entries(ldap_conn->handle, result) == 0) ?
770 break;
771
772 default:
773 query->ret = LDAP_RESULT_SUCCESS;
774 break;
775 }
776 break;
777
779 if (!ttrunk->t->config->chase_referrals) {
781 "LDAP referral received but 'chase_referrals' is set to 'no'");
782 query->ret = LDAP_RESULT_EXCESS_REFERRALS;
783 break;
784 }
785
786 if (query->referral_depth >= ttrunk->t->config->referral_depth) {
787 ROPTIONAL(REDEBUG, ERROR, "Maximum LDAP referral depth (%d) exceeded",
788 ttrunk->t->config->referral_depth);
789 query->ret = LDAP_RESULT_EXCESS_REFERRALS;
790 break;
791 }
792
793 /*
794 * If we've come here as the result of an existing referral
795 * clear the previous list of URLs before getting the next list.
796 */
797 if (query->referral_urls) ldap_memvfree((void **)query->referral_urls);
798
799 ldap_get_option(ldap_conn->handle, LDAP_OPT_REFERRAL_URLS, &query->referral_urls);
800 if (!(query->referral_urls) || (!(query->referral_urls[0]))) {
801 ROPTIONAL(REDEBUG, ERROR, "LDAP referral missing referral URL");
802 query->ret = LDAP_RESULT_MISSING_REFERRAL;
803 break;
804 }
805
806 query->referral_depth ++;
807
808 if (fr_ldap_referral_follow(ttrunk->t, request, query) == 0) {
809 next_follow:
810 ldap_msgfree(result);
811 continue;
812 }
813
814 ROPTIONAL(REDEBUG, ERROR, "Unable to follow any LDAP referral URLs");
815 query->ret = LDAP_RESULT_REFERRAL_FAIL;
816 break;
817
818 case LDAP_PROC_BAD_DN:
819 ROPTIONAL(RDEBUG2, DEBUG2, "DN %s does not exist", query->dn);
820 query->ret = LDAP_RESULT_BAD_DN;
821 break;
822
823 default:
824 ROPTIONAL(RPERROR, PERROR, "LDAP server returned an error");
825
826 if (query->referral_depth > 0) {
827 /*
828 * We're processing a referral - see if there are any more to try
829 */
830 fr_dlist_talloc_free_item(&query->referrals, query->referral);
831 query->referral = NULL;
832
833 if ((fr_dlist_num_elements(&query->referrals) > 0) &&
834 (fr_ldap_referral_next(ttrunk->t, request, query) == 0)) goto next_follow;
835 }
836
837 query->ret = LDAP_RESULT_REFERRAL_FAIL;
838 break;
839 }
840
841 query->result = result;
842
843 /*
844 * If we have a specific parser to handle the result, call it
845 */
846 if (query->parser && (rcode == LDAP_PROC_SUCCESS)) query->parser(ldap_conn->handle, query,
847 result, query->treq->rctx);
848
849 /*
850 * Set the request as runnable
851 */
853
854 /*
855 * If referral following failed, there is no active trunk request.
856 */
857 if (!query->treq) continue;
858
859 /*
860 * If the query is parented off the treq then it will be freed when
861 * the request is completed. If it is parented by something else then it will not.
862 */
863 treq = query->treq;
864 query->treq = NULL;
866 } while (1);
867}
868
870{
871 if (ttrunk->t && fr_rb_node_inline_in_tree(&ttrunk->node)) fr_rb_remove(NULL, ttrunk->t->trunks, ttrunk);
872
873 return 0;
874}
875
876/** Find a thread specific LDAP connection for a specific URI / bind DN
877 *
878 * If no existing connection exists for that combination then create a new one
879 *
880 * @param[in] thread to which the connection belongs
881 * @param[in] uri of the host to find / create a connection to
882 * @param[in] bind_dn to make the connection as
883 * @param[in] bind_password for making connection
884 * @param[in] request currently being processed (only for debug messages)
885 * @param[in] config LDAP config of the module requesting the connection.
886 * @return
887 * - an existing or new connection matching the URI and bind DN
888 * - NULL on failure
889 */
891 char const *bind_dn, char const *bind_password,
893{
894 fr_ldap_thread_trunk_t *found, find = {.uri = uri, .bind_dn = bind_dn};
896 trunk_conf_t trunk_conf;
897
898 ROPTIONAL(RDEBUG2, DEBUG2, "Looking for LDAP connection to \"%s\" bound as \"%s\"", uri,
899 bind_dn ? bind_dn : "(anonymous)");
900 fr_rb_find((void **)&found, thread->trunks, &find);
901
902 if (found) return found;
903
904 /*
905 * No existing connection matching the requirement - create a new one
906 */
907 ROPTIONAL(RDEBUG2, DEBUG2, "No existing connection new - creating new one");
908 MEM(new = talloc_zero(thread, fr_ldap_thread_trunk_t));
909 talloc_set_destructor(new, _thread_ldap_trunk_free);
910
911 /*
912 * Build config for this connection - start with module settings and
913 * override server and bind details
914 */
915 memcpy(&new->config, config, sizeof(fr_ldap_config_t));
916 new->config.server = talloc_strdup(new, uri);
917 new->config.admin_identity = talloc_strdup(new, bind_dn);
918 new->config.admin_password = talloc_strdup(new, bind_password);
919
920 new->uri = new->config.server;
921 new->bind_dn = new->config.admin_identity;
922
923 /*
924 * Allocated before the trunk so the pointer is valid when
925 * connection init callbacks run, populated by the first
926 * connection's discovery state before it starts serving
927 * requests.
928 */
929 new->directory = fr_ldap_directory_alloc(new);
930
931 /*
932 * Referral chasing creates trunks and waits for one to
933 * transition to active before sending the query, so a trunk
934 * must always open a connection, even with no requests
935 * enqueued.
936 */
937 trunk_conf = *thread->trunk_conf;
938 if (trunk_conf.start == 0) trunk_conf.start = 1;
939
940 new->trunk = trunk_alloc(new, thread->el,
942 .connection_alloc = ldap_trunk_connection_alloc,
943 .connection_notify = ldap_trunk_connection_notify,
944 .request_mux = ldap_trunk_request_mux,
945 .request_demux = ldap_trunk_request_demux,
946 .request_cancel = ldap_request_cancel,
947 .request_cancel_mux = ldap_request_cancel_mux,
948 .request_fail = ldap_request_fail,
949 },
950 &trunk_conf,
951 "rlm_ldap", new, false, thread->trigger_args);
952
953 if (!new->trunk) {
954 error:
955 ROPTIONAL(REDEBUG, ERROR, "Unable to create LDAP connection");
956 talloc_free(new);
957 return NULL;
958 }
959
960 new->t = thread;
961
962 /*
963 * Insert event to close trunk if it becomes idle
964 */
965 if (!fr_cond_assert_msg(fr_timer_in(new, thread->el->tl, &new->ev, thread->config->idle_timeout,
966 false, _ldap_trunk_idle_timeout, new) == 0, "cannot insert trunk idle event")) goto error;
967
968 fr_rb_insert(thread->trunks, new);
969
970 return new;
971}
972
973/** Lookup the state of a thread specific LDAP connection trunk for a specific URI / bind DN
974 *
975 * @param[in] thread to which the connection belongs
976 * @param[in] uri of the host to find / create a connection to
977 * @param[in] bind_dn to make the connection as
978 * @return
979 * - State of a trunk matching the URI and bind DN
980 * - TRUNK_STATE_MAX if no matching trunk
981 */
982trunk_state_t fr_thread_ldap_trunk_state(fr_ldap_thread_t *thread, char const *uri, char const *bind_dn)
983{
984 fr_ldap_thread_trunk_t *found, find = {.uri = uri, .bind_dn = bind_dn};
985
986 fr_rb_find((void **)&found, thread->trunks, &find);
987
988 return (found) ? found->trunk->state : TRUNK_STATE_MAX;
989}
990
991/** Take pending LDAP bind auths from the queue and send them.
992 *
993 * @param[in] el Event list for timers.
994 * @param[in] tconn Trunk handle.
995 * @param[in] conn on which to send the queries
996 * @param[in] uctx User context passed to trunk_alloc
997 */
998CC_NO_UBSAN(function) /* UBSAN: false positive - public vs private connection_t trips --fsanitize=function*/
1000 connection_t *conn, void *uctx)
1001{
1002 fr_ldap_connection_t *ldap_conn = talloc_get_type_abort(conn->h, fr_ldap_connection_t);
1003 fr_ldap_thread_trunk_t *ttrunk = talloc_get_type_abort(uctx, fr_ldap_thread_trunk_t);
1004 fr_ldap_thread_t *thread = ttrunk->t;
1005 trunk_request_t *treq;
1006
1007 fr_ldap_bind_auth_ctx_t *bind = NULL;
1008 int ret = 0;
1009 struct berval cred;
1011
1012 if (trunk_connection_pop_request(&treq, tconn) != 0) return;
1013
1014 /* Pacify clang scan */
1015 if (!treq) return;
1016
1017 bind = talloc_get_type_abort(treq->preq, fr_ldap_bind_auth_ctx_t);
1018 request = bind->request;
1019
1020 switch (bind->type) {
1021 case LDAP_BIND_SIMPLE:
1022 {
1023 fr_ldap_bind_ctx_t *bind_ctx = bind->bind_ctx;
1024
1025 RDEBUG2("Starting bind auth operation as %s", bind_ctx->bind_dn);
1026
1027 if (bind_ctx->password) {
1028 memcpy(&cred.bv_val, &bind_ctx->password, sizeof(cred.bv_val));
1029 cred.bv_len = talloc_strlen(bind_ctx->password);
1030 } else {
1031 cred.bv_val = NULL;
1032 cred.bv_len = 0;
1033 }
1034
1035 ret = ldap_sasl_bind(ldap_conn->handle, bind_ctx->bind_dn, LDAP_SASL_SIMPLE,
1036 &cred, NULL, NULL, &bind->msgid);
1037
1038 switch (ret) {
1039 case LDAP_SUCCESS:
1040 fr_rb_insert(thread->binds, bind);
1041 RDEBUG3("Bind auth sent as LDAP msgid %d", bind->msgid);
1042 break;
1043
1044 default:
1045 bind->ret = LDAP_PROC_ERROR;
1046 unlang_interpret_mark_runnable(treq->request);
1047 RERROR("Failed to send bind auth");
1048 break;
1049 }
1050 }
1051 break;
1052
1053#ifdef WITH_SASL
1054 case LDAP_BIND_SASL:
1055 {
1056 fr_ldap_sasl_ctx_t *sasl_ctx = bind->sasl_ctx;
1057
1058 RDEBUG2("%s SASL bind auth operation as %s", sasl_ctx->rmech ? "Continuing" : "Starting",
1059 sasl_ctx->identity);
1060
1061 ret = fr_ldap_sasl_bind_auth_send(sasl_ctx, &bind->msgid, ldap_conn);
1062
1063 switch (ret) {
1064 case LDAP_SASL_BIND_IN_PROGRESS:
1065 /*
1066 * Add the bind to the list of pending binds.
1067 */
1068 fr_rb_insert(thread->binds, bind);
1069 RDEBUG3("SASL bind auth sent as LDAP msgid %d", bind->msgid);
1070 break;
1071
1072 case LDAP_SUCCESS:
1073 bind->ret = LDAP_PROC_SUCCESS;
1074 unlang_interpret_mark_runnable(treq->request);
1075 break;
1076
1077 default:
1078 bind->ret = LDAP_PROC_ERROR;
1079 unlang_interpret_mark_runnable(treq->request);
1080 RERROR("Failed to send SASL bind auth");
1081 break;
1082 }
1083 }
1084 break;
1085#endif
1086 }
1087 /*
1088 * The request is marked as sent, to remove from the pending list.
1089 * This is regardless of whether the sending was successful or not as
1090 * the different states are handled by the resume function which then
1091 * marks the request as complete triggering the tidy up.
1092 */
1094}
1095
1096/** Read LDAP bind auth responses
1097 *
1098 * @param[in] el To insert timers into.
1099 * @param[in] tconn Trunk connection associated with these results.
1100 * @param[in] conn Connection handle for these results.
1101 * @param[in] uctx Thread specific trunk structure - contains tree of pending queries.
1102 */
1103CC_NO_UBSAN(function) /* UBSAN: false positive - public vs private connection_t trips --fsanitize=function*/
1105 connection_t *conn, void *uctx)
1106{
1107 fr_ldap_connection_t *ldap_conn = talloc_get_type_abort(conn->h, fr_ldap_connection_t);
1108 fr_ldap_thread_trunk_t *ttrunk = talloc_get_type_abort(uctx, fr_ldap_thread_trunk_t);
1109 fr_ldap_thread_t *thread = ttrunk->t;
1110 fr_ldap_bind_auth_ctx_t find = { .msgid = -1 }, *bind = NULL;
1111
1112 int ret = 0;
1113 LDAPMessage *result = NULL;
1115 bool really_no_result = false;
1116
1117 do {
1118 /*
1119 * The first time ldap_result is called when there's pending network
1120 * data, it may read the data, but actually return a timeout.
1121 *
1122 * In order to fix the spurious debugging messages and overhead,
1123 * if this is the first iteration through the loop and fr_ldap_result
1124 * returns a timeout, we call it again.
1125 */
1126 ret = fr_ldap_result(&result, NULL, ldap_conn, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, fr_time_delta_wrap(10));
1127 if (ret == LDAP_PROC_TIMEOUT) {
1128 if (really_no_result) return;
1129 really_no_result = true;
1130 continue;
1131 }
1132
1133 if (!result) return;
1134
1135 really_no_result = true;
1136 find.msgid = ldap_msgid(result);
1137 fr_rb_find((void **)&bind, thread->binds, &find);
1138
1139 if (!bind) {
1140 WARN("Ignoring bind result msgid %i - doesn't match any outstanding binds", find.msgid);
1141 ldap_msgfree(result);
1142 result = NULL;
1143 continue;
1144 }
1145 } while (!bind);
1146
1147 /*
1148 * There will only ever be one bind in flight at a time on a given
1149 * connection - so having got a result, no need to loop.
1150 */
1151
1152 fr_rb_remove(NULL, thread->binds, bind);
1153 request = bind->request;
1154 bind->ret = ret;
1155
1156 switch (ret) {
1157 /*
1158 * Accept or reject will be SUCCESS, NOT_PERMITTED or REJECT
1159 */
1161 case LDAP_PROC_REJECT:
1162 case LDAP_PROC_BAD_DN:
1164 break;
1165
1166 case LDAP_PROC_SUCCESS:
1167 if (bind->type == LDAP_BIND_SIMPLE) break;
1168
1169 /*
1170 * With SASL binds, we will be here after ldap_sasl_interactive_bind
1171 * returned LDAP_SASL_BIND_IN_PROGRESS. That always requires a further
1172 * call of ldap_sasl_interactive_bind to get the final result.
1173 */
1174 bind->ret = LDAP_PROC_CONTINUE;
1176
1177 case LDAP_PROC_CONTINUE:
1178 {
1179 fr_ldap_sasl_ctx_t *sasl_ctx = bind->sasl_ctx;
1180 struct berval *srv_cred;
1181
1182 /*
1183 * Free any previous result and track the new one.
1184 */
1185 if (sasl_ctx->result) ldap_msgfree(sasl_ctx->result);
1186 sasl_ctx->result = result;
1187 result = NULL;
1188
1189 ret = ldap_parse_sasl_bind_result(ldap_conn->handle, sasl_ctx->result, &srv_cred, 0);
1190 if (ret != LDAP_SUCCESS) {
1191 RERROR("SASL decode failed (bind failed): %s", ldap_err2string(ret));
1192 break;
1193 }
1194
1195 if (srv_cred) {
1196 RDEBUG3("SASL response : %pV",
1197 fr_box_strvalue_len(srv_cred->bv_val, srv_cred->bv_len));
1198 ber_bvfree(srv_cred);
1199 }
1200
1201 if (sasl_ctx->rmech) RDEBUG3("Continuing SASL mech %s...", sasl_ctx->rmech);
1202 }
1203 break;
1204
1205 default:
1206 break;
1207 }
1208
1209 ldap_msgfree(result);
1211}
1212
1213/** Callback to cancel LDAP bind auth
1214 *
1215 * Inform the remote LDAP server that we no longer want responses to specific bind.
1216 *
1217 * @param[in] el For timer management.
1218 * @param[in] tconn The trunk connection handle
1219 * @param[in] conn The specific connection binds will be cancelled on
1220 * @param[in] uctx Context provided to trunk_alloc
1221 */
1222CC_NO_UBSAN(function) /* UBSAN: false positive - public vs private connection_t trips --fsanitize=function*/
1224 connection_t *conn, UNUSED void *uctx)
1225{
1226 trunk_request_t *treq;
1227 fr_ldap_connection_t *ldap_conn = talloc_get_type_abort(conn->h, fr_ldap_connection_t);
1229
1230 while ((trunk_connection_pop_cancellation(&treq, tconn)) == 0) {
1231 bind = talloc_get_type_abort(treq->preq, fr_ldap_bind_auth_ctx_t);
1232#ifdef WITH_SASL
1233 if (bind->type == LDAP_BIND_SASL) {
1234 /*
1235 * With SASL binds, abandoning the bind part way through
1236 * seems to leave the connection in an unpredictable state
1237 * so safer to restart.
1238 */
1240 } else {
1241#endif
1242 ldap_abandon_ext(ldap_conn->handle, bind->msgid, NULL, NULL);
1243#ifdef WITH_SASL
1244 }
1245#endif
1247 }
1248}
1249
1250/** Callback to tidy up when a bind auth trunk request fails
1251 *
1252 */
1253static void ldap_trunk_bind_auth_fail(request_t *request, void *preq, UNUSED void *rctx,
1254 UNUSED trunk_request_state_t state, UNUSED void *uctx)
1255{
1256 fr_ldap_bind_auth_ctx_t *bind = talloc_get_type_abort(preq, fr_ldap_bind_auth_ctx_t);
1257
1258 /*
1259 * Remove the connection from the bind tree before freeing it.
1260 */
1261 if (fr_rb_node_inline_in_tree(&bind->node)) fr_rb_remove(NULL, bind->thread->binds, bind);
1262
1263 /*
1264 * Failed trunk requests get freed - so remove association in bind structure,
1265 * and change talloc parentage so resume function still has something to work with.
1266 */
1267 bind->treq = NULL;
1268 bind->ret = LDAP_PROC_ERROR;
1269 talloc_steal(NULL, bind);
1270
1271 /*
1272 * Ensure request is runnable.
1273 */
1275}
1276
1277/** Find the thread specific trunk to use for LDAP bind auths
1278 *
1279 * If there is no current trunk then a new one is created.
1280 *
1281 * @param[in] thread to which the connection belongs
1282 * @return
1283 * - an existing or new trunk.
1284 * - NULL on failure
1285 */
1287{
1288 fr_ldap_thread_trunk_t *ttrunk;
1289
1290 if (thread->bind_trunk) return (thread->bind_trunk);
1291
1292 MEM(ttrunk = talloc_zero(thread, fr_ldap_thread_trunk_t));
1293 memcpy(&ttrunk->config, thread->config, sizeof(fr_ldap_config_t));
1294
1295 ttrunk->uri = ttrunk->config.server;
1296 ttrunk->bind_dn = ttrunk->config.admin_identity;
1297
1298 ttrunk->trunk = trunk_alloc(ttrunk, thread->el,
1300 .connection_alloc = ldap_trunk_connection_alloc,
1301 .connection_notify = ldap_trunk_connection_notify,
1302 .request_mux = ldap_trunk_bind_auth_mux,
1303 .request_demux = ldap_trunk_bind_auth_demux,
1304 .request_cancel_mux = ldap_bind_auth_cancel_mux,
1305 .request_fail = ldap_trunk_bind_auth_fail,
1306 },
1307 thread->bind_trunk_conf,
1308 "rlm_ldap bind auth", ttrunk, false, thread->bind_trigger_args);
1309
1310 if (!ttrunk->trunk) {
1311 ERROR("Unable to create LDAP connection");
1312 talloc_free(ttrunk);
1313 return NULL;
1314 }
1315
1316 ttrunk->t = thread;
1317 thread->bind_trunk = ttrunk;
1318
1319 return ttrunk;
1320}
static uint8_t request[4]
#define USES_APPLE_DEPRECATED_API
Definition build.h:590
#define RCSID(id)
Definition build.h:603
#define FALL_THROUGH
clang 10 doesn't recognised the FALL-THROUGH comment anymore
Definition build.h:422
#define DIAG_ON(_x)
Definition build.h:576
#define CC_NO_UBSAN(_sanitize)
Definition build.h:534
#define UNUSED
Definition build.h:415
#define NUM_ELEMENTS(_t)
Definition build.h:437
#define DIAG_OFF(_x)
Definition build.h:575
#define fr_cond_assert_msg(_x, _fmt,...)
Calls panic_action ifndef NDEBUG, else logs error and evaluates to value of _x.
Definition debug.h:194
#define MEM(x)
Definition debug.h:38
#define ERROR(fmt,...)
Definition dhcpclient.c:40
#define fr_dlist_init(_head, _type, _field)
Initialise the head structure of a doubly linked list.
Definition dlist.h:242
static void * fr_dlist_remove(fr_dlist_head_t *list_head, void *ptr)
Remove an item from the list.
Definition dlist.h:620
static unsigned int fr_dlist_num_elements(fr_dlist_head_t const *head)
Return the number of elements in the dlist.
Definition dlist.h:921
static void * fr_dlist_talloc_free_item(fr_dlist_head_t *list_head, void *ptr)
Free the item specified.
Definition dlist.h:860
static int fr_dlist_insert_tail(fr_dlist_head_t *list_head, void *ptr)
Insert an item into the tail of a list.
Definition dlist.h:360
@ FR_EVENT_FILTER_IO
Combined filter for read/write functions/.
Definition event.h:83
talloc_free(hp)
void unlang_interpret_mark_runnable(request_t *request)
Mark a request as resumable.
Definition interpret.c:2022
fr_ldap_control_t serverctrls[LDAP_MAX_CONTROLS]
Server controls specific to this query.
Definition base.h:437
LDAPURLDesc * referral_url
URL for the referral.
Definition base.h:467
fr_ldap_thread_t * t
Thread this connection is associated with.
Definition base.h:393
fr_ldap_rcode_t ret
Return code of bind operation.
Definition base.h:611
@ LDAP_REQUEST_MODIFY
A modification to an LDAP entity.
Definition base.h:180
@ LDAP_REQUEST_SEARCH
A lookup in an LDAP directory.
Definition base.h:179
@ LDAP_REQUEST_DELETE
A deletion of an LDAP entity.
Definition base.h:181
@ LDAP_REQUEST_EXTENDED
An extended LDAP operation.
Definition base.h:182
fr_ldap_config_t * config
Module instance config.
Definition base.h:368
void fr_ldap_control_clear(fr_ldap_connection_t *conn)
Clear and free any controls associated with a connection.
Definition control.c:134
bool chase_referrals
If the LDAP server returns a referral to another server or point in the tree, follow it,...
Definition base.h:249
int msgid
libldap msgid for this bind.
Definition base.h:604
char * server
Initial server to bind to.
Definition base.h:233
LDAP * handle
libldap handle.
Definition base.h:327
int msgid
The unique identifier for this query.
Definition base.h:440
char const * dn
Base DN for searches, DN for modifications.
Definition base.h:416
char const * bind_dn
DN connection is bound as.
Definition base.h:389
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
fr_rb_node_t node
Entry in the tree of connections.
Definition base.h:387
static fr_cmp_ret_t fr_ldap_query_cmp(void const *one, void const *two)
Compare two ldap query structures on msgid.
Definition base.h:698
char const * admin_identity
Identity we bind as when we need to query the LDAP directory.
Definition base.h:238
fr_ldap_result_code_t ret
Result code.
Definition base.h:455
fr_ldap_directory_t * fr_ldap_directory_alloc(TALLOC_CTX *ctx)
Allocate a directory structure with defaults.
Definition directory.c:272
fr_rb_tree_t * trunks
Tree of LDAP trunks used by this thread.
Definition base.h:367
int fr_ldap_referral_follow(fr_ldap_thread_t *thread, request_t *request, fr_ldap_query_t *query)
Follow an LDAP referral.
Definition referral.c:113
trunk_conf_t * trunk_conf
Module trunk config.
Definition base.h:369
fr_rb_tree_t * queries
Outstanding queries on this connection.
Definition base.h:345
fr_ldap_directory_t * directory
The type of directory we're connected to.
Definition base.h:336
char const * identity
of the user.
Definition base.h:494
trunk_request_t * treq
Trunk request this query is associated with.
Definition base.h:443
fr_dlist_head_t refs
Replied to queries still referencing this connection.
Definition base.h:346
int fd
File descriptor for this connection.
Definition base.h:343
fr_timer_t * ev
Event to close the thread when it has been idle.
Definition base.h:394
char const * uri
Server URI for this connection.
Definition base.h:388
LDAPMessage * result
Previous result.
Definition base.h:502
fr_ldap_config_t const * config
rlm_ldap connection configuration.
Definition base.h:338
request_t * request
this bind relates to.
Definition base.h:605
fr_ldap_control_t clientctrls[LDAP_MAX_CONTROLS]
Client controls specific to this query.
Definition base.h:438
trunk_request_t * treq
Trunk request this bind is associated with.
Definition base.h:603
@ LDAP_BIND_SIMPLE
Definition base.h:507
fr_ldap_config_t config
Config used for this connection.
Definition base.h:390
fr_ldap_state_t fr_ldap_state_next(fr_ldap_connection_t *c)
Move between LDAP connection states.
Definition state.c:50
fr_pair_list_t * bind_trigger_args
Passed to trigger request for bind trunks.
Definition base.h:375
fr_ldap_connection_t * ldap_conn
LDAP connection this query is running on.
Definition base.h:444
@ LDAP_RESULT_EXCESS_REFERRALS
The referral chain took too many hops.
Definition base.h:197
@ LDAP_RESULT_REFERRAL_FAIL
Initial results indicated a referral was needed but the referral could not be followed.
Definition base.h:195
@ LDAP_RESULT_ERROR
A general error occurred.
Definition base.h:191
@ LDAP_RESULT_SUCCESS
Successfully got LDAP results.
Definition base.h:190
@ LDAP_RESULT_PENDING
Result not yet returned.
Definition base.h:189
@ LDAP_RESULT_NO_RESULT
No results returned.
Definition base.h:194
@ LDAP_RESULT_BAD_DN
The requested DN does not exist.
Definition base.h:193
@ LDAP_RESULT_MISSING_REFERRAL
A referral was indicated but no URL was provided.
Definition base.h:198
#define LDAP_MAX_CONTROLS
Maximum number of client/server controls.
Definition base.h:94
trunk_conf_t * bind_trunk_conf
Trunk config for bind auth trunk.
Definition base.h:370
uint16_t referral_depth
How many referrals to chase.
Definition base.h:256
fr_ldap_rcode_t
Codes returned by fr_ldap internal functions.
Definition base.h:567
@ LDAP_PROC_CONTINUE
Operation is in progress.
Definition base.h:571
@ LDAP_PROC_SUCCESS
Operation was successful.
Definition base.h:572
@ LDAP_PROC_REFERRAL
LDAP server returned referral URLs.
Definition base.h:570
@ LDAP_PROC_TIMEOUT
Operation timed out.
Definition base.h:589
@ LDAP_PROC_ERROR
Unrecoverable library/server error.
Definition base.h:574
@ LDAP_PROC_BAD_CONN
Transitory error, caller should retry the operation with a new connection.
Definition base.h:576
@ LDAP_PROC_NOT_PERMITTED
Operation was not permitted, either current user was locked out in the case of binds,...
Definition base.h:579
@ LDAP_PROC_REJECT
Bind failed, user was rejected.
Definition base.h:583
@ LDAP_PROC_BAD_DN
Specified an invalid object in a bind or search DN.
Definition base.h:585
@ LDAP_PROC_NO_RESULT
Got no results.
Definition base.h:587
fr_event_list_t * el
Thread event list for callbacks / timeouts.
Definition base.h:371
fr_ldap_directory_t * directory
The type of directory we're connected to.
Definition base.h:391
char const * rmech
Mech we're continuing with.
Definition base.h:503
fr_ldap_state_t
LDAP connection handle states.
Definition base.h:166
@ FR_LDAP_STATE_ERROR
Connection is in an error state.
Definition base.h:172
fr_ldap_thread_trunk_t * bind_trunk
LDAP trunk used for bind auths.
Definition base.h:372
char const * bind_dn
of the user, may be NULL to bind anonymously.
Definition base.h:480
trunk_t * trunk
Connection trunk.
Definition base.h:392
fr_pair_list_t * trigger_args
Passed to trigger request for normal trunks.
Definition base.h:374
fr_ldap_thread_t * thread
This bind is being run by.
Definition base.h:602
connection_t * conn
Connection state handle.
Definition base.h:339
fr_ldap_referral_t * referral
Referral actually being followed.
Definition base.h:449
fr_rb_tree_t * binds
Tree of outstanding bind auths.
Definition base.h:373
fr_rb_node_t node
Entry in the tree of outstanding bind requests.
Definition base.h:601
fr_ldap_bind_type_t type
type of bind.
Definition base.h:606
int fr_ldap_referral_next(fr_ldap_thread_t *thread, request_t *request, fr_ldap_query_t *query)
Follow an alternative LDAP referral.
Definition referral.c:310
char const * password
of the user, may be NULL if no password is specified.
Definition base.h:481
fr_time_delta_t idle_timeout
How long to wait before closing unused connections.
Definition base.h:307
fr_ldap_request_type_t type
What type of query this is.
Definition base.h:435
Holds arguments for async bind auth requests.
Definition base.h:600
Holds arguments for the async bind operation.
Definition base.h:478
Connection configuration.
Definition base.h:230
Tracks the state of a libldap connection handle.
Definition base.h:326
LDAP query structure.
Definition base.h:409
Holds arguments for the async SASL bind operation.
Definition base.h:491
Thread specific structure to manage LDAP trunk connections.
Definition base.h:366
Thread LDAP trunk structure.
Definition base.h:386
static void ldap_request_cancel_mux(UNUSED fr_event_list_t *el, trunk_connection_t *tconn, connection_t *conn, UNUSED void *uctx)
Callback to cancel LDAP queries.
Definition connection.c:461
static void ldap_trunk_request_mux(UNUSED fr_event_list_t *el, trunk_connection_t *tconn, connection_t *conn, UNUSED void *uctx)
Take LDAP pending queries from the queue and send them.
Definition connection.c:544
#define do_ldap_option(_option, _name, _value)
static int _ldap_connection_free(fr_ldap_connection_t *c)
Close and delete a connection.
Definition connection.c:220
fr_ldap_thread_trunk_t * fr_thread_ldap_trunk_get(fr_ldap_thread_t *thread, char const *uri, char const *bind_dn, char const *bind_password, request_t *request, fr_ldap_config_t const *config)
Find a thread specific LDAP connection for a specific URI / bind DN.
Definition connection.c:890
static void ldap_trunk_bind_auth_mux(UNUSED fr_event_list_t *el, trunk_connection_t *tconn, connection_t *conn, void *uctx)
Take pending LDAP bind auths from the queue and send them.
Definition connection.c:999
static void ldap_request_fail(request_t *request, void *preq, UNUSED void *rctx, UNUSED trunk_request_state_t state, UNUSED void *uctx)
Callback to tidy up when a trunk request fails.
Definition connection.c:479
static USES_APPLE_DEPRECATED_API char const * ldap_msg_types[UINT8_MAX+1]
Definition connection.c:34
static void ldap_trunk_bind_auth_fail(request_t *request, void *preq, UNUSED void *rctx, UNUSED trunk_request_state_t state, UNUSED void *uctx)
Callback to tidy up when a bind auth trunk request fails.
static connection_t * ldap_trunk_connection_alloc(trunk_connection_t *tconn, fr_event_list_t *el, connection_conf_t const *conn_conf, char const *log_prefix, void *uctx)
Allocate an LDAP trunk connection.
Definition connection.c:507
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.
trunk_state_t fr_thread_ldap_trunk_state(fr_ldap_thread_t *thread, char const *uri, char const *bind_dn)
Lookup the state of a thread specific LDAP connection trunk for a specific URI / bind DN.
Definition connection.c:982
static void _ldap_connection_close_watch(connection_t *conn, UNUSED connection_state_t prev, UNUSED connection_state_t state, void *uctx)
Watcher for LDAP connections being closed.
Definition connection.c:285
#define maybe_ldap_option(_option, _name, _value)
static int _thread_ldap_trunk_free(fr_ldap_thread_trunk_t *ttrunk)
Definition connection.c:869
connection_state_t fr_ldap_connection_init(void **h, connection_t *conn, void *uctx)
Initialise a standalone LDAP connection.
Definition connection.c:393
static void _ldap_trunk_idle_timeout(fr_timer_list_t *tl, UNUSED fr_time_t now, void *uctx)
Callback for closing idle LDAP trunk.
Definition connection.c:420
fr_ldap_connection_t * fr_ldap_connection_alloc(TALLOC_CTX *ctx)
Allocate our ldap connection handle layer.
Definition connection.c:257
static void ldap_trunk_bind_auth_demux(UNUSED fr_event_list_t *el, UNUSED trunk_connection_t *tconn, connection_t *conn, void *uctx)
Read LDAP bind auth responses.
static void ldap_trunk_request_demux(fr_event_list_t *el, trunk_connection_t *tconn, connection_t *conn, void *uctx)
Read LDAP responses.
Definition connection.c:665
static connection_state_t ldap_connection_init(void **h, connection_t *conn, fr_ldap_config_t const *config, fr_ldap_directory_t *directory)
(Re-)Initialises the libldap side of the connection handle
Definition connection.c:342
#define POPULATE_LDAP_CONTROLS(_dest, _src)
Definition connection.c:528
int fr_ldap_connection_configure(fr_ldap_connection_t *c, fr_ldap_config_t const *config)
Allocate and configure a new connection.
Definition connection.c:67
connection_state_t fr_ldap_trunk_connection_init(void **h, connection_t *conn, void *uctx)
Initialise an LDAP trunk connection.
Definition connection.c:410
void fr_ldap_connection_close(fr_event_list_t *el, void *h, UNUSED void *uctx)
Free the handle, closing the connection to ldap.
Definition connection.c:194
static void ldap_bind_auth_cancel_mux(UNUSED fr_event_list_t *el, trunk_connection_t *tconn, connection_t *conn, UNUSED void *uctx)
Callback to cancel LDAP bind auth.
static void ldap_request_cancel(UNUSED connection_t *conn, void *preq, UNUSED trunk_cancel_reason_t reason, UNUSED void *uctx)
Callback when an LDAP trunk request is cancelled.
Definition connection.c:441
fr_ldap_rcode_t fr_ldap_error_check(LDAPControl ***ctrls, fr_ldap_connection_t const *conn, LDAPMessage *msg, char const *dn)
Perform basic parsing of multiple types of messages, checking for error conditions.
Definition base.c:174
fr_ldap_rcode_t fr_ldap_search_async(int *msgid, request_t *request, fr_ldap_connection_t *pconn, char const *dn, int scope, char const *filter, char const *const *attrs, LDAPControl **serverctrls, LDAPControl **clientctrls)
Search for something in the LDAP directory.
Definition base.c:510
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:409
fr_ldap_rcode_t fr_ldap_modify_async(int *msgid, request_t *request, fr_ldap_connection_t *pconn, char const *dn, LDAPMod *mods[], LDAPControl **serverctrls, LDAPControl **clientctrls)
Modify something in the LDAP directory.
Definition base.c:798
fr_ldap_rcode_t fr_ldap_extended_async(int *msgid, request_t *request, fr_ldap_connection_t *pconn, char const *reqoid, struct berval *reqdata)
Initiate an LDAP extended operation.
Definition base.c:923
fr_ldap_rcode_t fr_ldap_delete_async(int *msgid, request_t *request, fr_ldap_connection_t *pconn, char const *dn, LDAPControl **serverctrls, LDAPControl **clientctrls)
Modify something in the LDAP directory.
Definition base.c:836
#define PERROR(_fmt,...)
Definition log.h:233
#define DEBUG3(_fmt,...)
Definition log.h:271
#define ROPTIONAL(_l_request, _l_global, _fmt,...)
Use different logging functions depending on whether request is NULL or not.
Definition log.h:545
#define RDEBUG3(fmt,...)
Definition log.h:360
#define RERROR(fmt,...)
Definition log.h:315
#define RPERROR(fmt,...)
Definition log.h:319
int fr_event_fd_delete(fr_event_list_t *el, int fd, fr_event_filter_t filter)
Remove a file descriptor from the event loop.
Definition event.c:1204
Stores all information relating to an event list.
Definition event.c:375
#define UINT8_MAX
static const conf_parser_t config[]
Definition base.c:162
#define fr_assert(_expr)
Definition rad_assert.h:37
#define REDEBUG(fmt,...)
#define RDEBUG2(fmt,...)
#define DEBUG2(fmt,...)
#define WARN(fmt,...)
uint32_t fr_rb_num_elements(fr_rb_tree_t *tree)
Return how many nodes there are in a tree.
Definition rb.c:807
int fr_rb_remove(void **removed, fr_rb_tree_t *tree, void const *data)
Remove an entry from the tree, without freeing the data.
Definition rb.c:718
int fr_rb_find(void **found, fr_rb_tree_t const *tree, void const *data)
Find an element in the tree, returning the data, not the node.
Definition rb.c:586
int fr_rb_insert(fr_rb_tree_t *tree, void const *data)
Insert data into a tree.
Definition rb.c:637
#define fr_rb_inline_talloc_alloc(_ctx, _type, _field, _data_cmp, _data_free)
Allocs a red black that verifies elements are of a specific talloc type.
Definition rb.h:244
static bool fr_rb_node_inline_in_tree(fr_rb_node_t const *node)
Check to see if an item is in a tree by examining its inline fr_rb_node_t.
Definition rb.h:312
int fr_ldap_sasl_bind_auth_send(fr_ldap_sasl_ctx_t *sasl_ctx, int *msgid, fr_ldap_connection_t *ldap_conn)
Send a SASL LDAP auth bind.
Definition sasl.c:378
void connection_signal_reconnect(connection_t *conn, connection_reason_t reason)
Asynchronously signal the connection should be reconnected.
connection_t * connection_alloc(TALLOC_CTX *ctx, fr_event_list_t *el, connection_funcs_t const *funcs, connection_conf_t const *conf, char const *log_prefix, void const *uctx)
Allocate a new connection.
connection_watch_entry_t * connection_add_watch_pre(connection_t *conn, connection_state_t state, connection_watch_t watch, bool oneshot, void const *uctx)
Add a callback to be executed before a state function has been called.
Definition connection.c:495
@ CONNECTION_FAILED
Connection is being reconnected because it failed.
Definition connection.h:89
connection_state_t
Definition connection.h:47
@ CONNECTION_STATE_FAILED
Connection has failed.
Definition connection.h:56
@ CONNECTION_STATE_CLOSED
Connection has been closed.
Definition connection.h:57
@ CONNECTION_STATE_CONNECTING
Waiting for connection to establish.
Definition connection.h:52
Holds a complete set of functions for a connection.
Definition connection.h:199
#define talloc_strdup(_ctx, _str)
Definition talloc.h:149
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
#define fr_time_delta_to_timeval(_delta)
Convert a delta to a timeval.
Definition time.h:674
static int64_t fr_time_delta_to_sec(fr_time_delta_t delta)
Definition time.h:656
"server local" time.
Definition time.h:69
An event timer list.
Definition timer.c:65
#define fr_timer_in(...)
Definition timer.h:87
int trunk_connection_pop_cancellation(trunk_request_t **treq_out, trunk_connection_t *tconn)
Pop a cancellation request off a connection's cancellation queue.
Definition trunk.c:3931
void trunk_request_signal_fail(trunk_request_t *treq)
Signal that a trunk request failed.
Definition trunk.c:2196
void trunk_request_signal_cancel_complete(trunk_request_t *treq)
Signal that a remote server acked our cancellation.
Definition trunk.c:2348
int trunk_connection_pop_request(trunk_request_t **treq_out, trunk_connection_t *tconn)
Pop a request off a connection's pending queue.
Definition trunk.c:3979
trunk_t * trunk_alloc(TALLOC_CTX *ctx, fr_event_list_t *el, trunk_io_funcs_t const *funcs, trunk_conf_t const *conf, char const *log_prefix, void const *uctx, bool delay_start, fr_pair_list_t *trigger_args)
Allocate a new collection of connections.
Definition trunk.c:5124
void trunk_request_signal_sent(trunk_request_t *treq)
Signal that the request was written to a connection successfully.
Definition trunk.c:2114
void trunk_request_signal_complete(trunk_request_t *treq)
Signal that a trunk request is complete.
Definition trunk.c:2158
void trunk_connection_signal_reconnect(trunk_connection_t *tconn, connection_reason_t reason)
Signal a trunk connection is no longer viable.
Definition trunk.c:4095
Associates request queues with a connection.
Definition trunk.c:137
Wraps a normal request.
Definition trunk.c:99
trunk_state_t
Definition trunk.h:62
@ TRUNK_STATE_MAX
Definition trunk.h:75
#define TRUNK_NOTIFY_FUNC(_name, _type)
Helper macro for building generic trunk notify callback.
Definition trunk.h:970
uint16_t start
How many connections to start.
Definition trunk.h:237
trunk_request_state_t
Used for sanity checks and to simplify freeing.
Definition trunk.h:171
trunk_cancel_reason_t
Reasons for a request being cancelled.
Definition trunk.h:55
Common configuration parameters for a trunk.
Definition trunk.h:234
I/O functions to pass to trunk_alloc.
Definition trunk.h:746
static fr_event_list_t * el
#define fr_box_strvalue_len(_val, _len)
Definition value.h:340