The FreeRADIUS server $Id: f3670dba8951ca10eb4948feb3dc3db9423a334f $
Loading...
Searching...
No Matches
rlm_sql.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: 1c8e02f78fbdec60338c2d3cc0bd98d39aec5123 $
19 * @file rlm_sql.c
20 * @brief Implements SQL 'users' file, and SQL accounting.
21 *
22 * @copyright 2012-2014 Arran Cudbard-Bell (a.cudbardb@freeradius.org)
23 * @copyright 2000,2006 The FreeRADIUS server project
24 * @copyright 2000 Mike Machado (mike@innercite.com)
25 * @copyright 2000 Alan DeKok (aland@freeradius.org)
26 */
27
28RCSID("$Id: 1c8e02f78fbdec60338c2d3cc0bd98d39aec5123 $")
29
30#define LOG_PREFIX mctx->mi->name
31
32#include <freeradius-devel/server/base.h>
33#include <freeradius-devel/server/module_rlm.h>
34#include <freeradius-devel/util/skip.h>
35#include <freeradius-devel/unlang/function.h>
36#include <freeradius-devel/unlang/xlat_func.h>
37#include <freeradius-devel/unlang/map.h>
38
39#include <sys/stat.h>
40
41#include "rlm_sql.h"
42
43#define SQL_SAFE_FOR (fr_value_box_safe_for_t)inst->driver
44
46
47static int submodule_parse(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, conf_parser_t const *rule);
48
53
54static const conf_parser_t module_config[] = {
55 { FR_CONF_OFFSET_TYPE_FLAGS("driver", FR_TYPE_VOID, 0, rlm_sql_t, driver_submodule), .dflt = "null",
57 { FR_CONF_OFFSET("server", rlm_sql_config_t, sql_server), .dflt = "" }, /* Must be zero length so drivers can determine if it was set */
58 { FR_CONF_OFFSET("port", rlm_sql_config_t, sql_port), .dflt = "0" },
59 { FR_CONF_OFFSET("login", rlm_sql_config_t, sql_login), .dflt = "" },
60 { FR_CONF_OFFSET_FLAGS("password", CONF_FLAG_SECRET, rlm_sql_config_t, sql_password), .dflt = "" },
61 { FR_CONF_OFFSET("radius_db", rlm_sql_config_t, sql_db), .dflt = "radius" },
62 { FR_CONF_OFFSET("read_groups", rlm_sql_config_t, read_groups), .dflt = "yes" },
63 { FR_CONF_OFFSET("group_attribute", rlm_sql_config_t, group_attribute) },
64 { FR_CONF_OFFSET("cache_groups", rlm_sql_config_t, cache_groups) },
65 { FR_CONF_OFFSET("read_profiles", rlm_sql_config_t, read_profiles), .dflt = "yes" },
66 { FR_CONF_OFFSET("open_query", rlm_sql_config_t, connect_query) },
67 { FR_CONF_OFFSET("query_number_attribute", rlm_sql_config_t, query_number_attribute) },
68
69 { FR_CONF_OFFSET("safe_characters", rlm_sql_config_t, allowed_chars), .dflt = "@abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_: /" },
70
71 /*
72 * This only works for a few drivers.
73 */
74 { FR_CONF_OFFSET("query_timeout", rlm_sql_config_t, query_timeout), .dflt = "5" },
75
76 /*
77 * The pool section is used for trunk config
78 */
79 { FR_CONF_OFFSET_SUBSECTION("pool", 0, rlm_sql_config_t, trunk_conf, trunk_config) },
80
82
84};
85
87
90 { .out = &dict_freeradius, .proto = "freeradius" },
92};
93
98
101 { .out = &attr_fall_through, .name = "Fall-Through", .type = FR_TYPE_BOOL, .dict = &dict_freeradius },
102 { .out = &attr_sql_user_name, .name = "SQL-User-Name", .type = FR_TYPE_STRING, .dict = &dict_freeradius },
103 { .out = &attr_user_profile, .name = "User-Profile", .type = FR_TYPE_STRING, .dict = &dict_freeradius },
104 { .out = &attr_expr_bool_enum, .name = "Expr-Bool-Enum", .type = FR_TYPE_BOOL, .dict = &dict_freeradius },
106};
107
108typedef struct {
109 fr_value_box_t user; //!< Expansion of the sql_user_name
110 tmpl_t *check_query; //!< Tmpl to expand to form authorize_check_query
111 tmpl_t *reply_query; //!< Tmpl to expand to form authorize_reply_query
112 tmpl_t *membership_query; //!< Tmpl to expand to form group_membership_query
113 tmpl_t *group_check_query; //!< Tmpl to expand to form authorize_group_check_query
114 tmpl_t *group_reply_query; //!< Tmpl to expand to form authorize_group_reply_query
116
117static int logfile_call_env_parse(TALLOC_CTX *ctx, call_env_parsed_head_t *out, tmpl_rules_t const *t_rules, CONF_ITEM *cc,
118 call_env_ctx_t const *cec, call_env_parser_t const *rule);
119
120static int query_call_env_parse(TALLOC_CTX *ctx, call_env_parsed_head_t *out, tmpl_rules_t const *t_rules, CONF_ITEM *cc,
121 call_env_ctx_t const *cec, call_env_parser_t const *rule);
122
126
129 .env = (call_env_parser_t[]) {
131 .pair.escape = {
132 .box_escape = {
135 .always_escape = false,
136 },
138 },
139 .pair.literals_safe_for = (fr_value_box_safe_for_t)rad_filename_box_make_safe,
140 },
142 }
143};
144
146
147/** Status of the authorization process
148 */
149typedef enum {
150 SQL_AUTZ_CHECK = 0x12, //!< Running user `check` query
151 SQL_AUTZ_CHECK_RESUME = 0x13, //!< Completed user `check` query
152 SQL_AUTZ_REPLY = 0x14, //!< Running user `reply` query
153 SQL_AUTZ_REPLY_RESUME = 0x15, //!< Completed user `reply` query
154 SQL_AUTZ_GROUP_MEMB = 0x20, //!< Running group membership query
155 SQL_AUTZ_GROUP_MEMB_RESUME = 0x21, //!< Completed group membership query
156 SQL_AUTZ_GROUP_CHECK = 0x22, //!< Running group `check` query
157 SQL_AUTZ_GROUP_CHECK_RESUME = 0x23, //!< Completed group `check` query
158 SQL_AUTZ_GROUP_REPLY = 0x24, //!< Running group `reply` query
159 SQL_AUTZ_GROUP_REPLY_RESUME = 0x25, //!< Completed group `reply` query
160 SQL_AUTZ_PROFILE_START = 0x40, //!< Starting processing user profiles
161 SQL_AUTZ_PROFILE_CHECK = 0x42, //!< Running profile `check` query
162 SQL_AUTZ_PROFILE_CHECK_RESUME = 0x43, //!< Completed profile `check` query
163 SQL_AUTZ_PROFILE_REPLY = 0x44, //!< Running profile `reply` query
164 SQL_AUTZ_PROFILE_REPLY_RESUME = 0x45, //!< Completed profile `reply` query
166
167#define SQL_AUTZ_STAGE_GROUP 0x20
168#define SQL_AUTZ_STAGE_PROFILE 0x40
169
170/** Context for group membership query evaluation
171 */
172typedef struct {
173 rlm_sql_t const *inst; //!< Module instance.
174 fr_value_box_t *query; //!< Query string used for evaluating group membership.
175 fr_sql_query_t *query_ctx; //!< Query context.
176 rlm_sql_grouplist_t *groups; //!< List of groups retrieved.
177 int num_groups; //!< How many groups have been retrieved.
179
180/** Context for SQL authorization
181 */
182typedef struct {
183 rlm_sql_t const *inst; //!< Module instance.
184 request_t *request; //!< Request being processed.
185 rlm_rcode_t rcode; //!< Module return code.
186 trunk_t *trunk; //!< Trunk connection for current authorization.
187 sql_autz_call_env_t *call_env; //!< Call environment data.
188 map_list_t check_tmp; //!< List to store check items before processing.
189 map_list_t reply_tmp; //!< List to store reply items before processing.
190 sql_autz_status_t status; //!< Current status of the authorization.
191 fr_value_box_list_t query; //!< Where expanded query tmpls will be written.
192 bool user_found; //!< Has the user been found anywhere?
193 rlm_sql_grouplist_t *group; //!< Current group being processed.
194 fr_pair_t *sql_group; //!< Pair to update with group being processed.
195 fr_pair_t *profile; //!< Current profile being processed.
196 fr_sql_map_ctx_t *map_ctx; //!< Context used for retrieving attribute value pairs as a map list.
197 sql_group_ctx_t *group_ctx; //!< Context used for retrieving user group membership.
199
200typedef struct {
201 fr_value_box_t user; //!< Expansion of sql_user_name.
202 fr_value_box_t filename; //!< File name to write SQL logs to.
203 tmpl_t **query; //!< Array of tmpls for list of queries to run.
205
215
225
226/** Context for tracking redundant SQL query sets
227 */
228typedef struct {
229 rlm_sql_t const *inst; //!< Module instance.
230 request_t *request; //!< Request being processed.
231 trunk_t *trunk; //!< Trunk connection for queries.
232 sql_redundant_call_env_t *call_env; //!< Call environment data.
233 size_t query_no; //!< Current query number.
234 fr_value_box_list_t query; //!< Where expanded query tmpl will be written.
235 fr_value_box_t *query_vb; //!< Current query string.
236 fr_sql_query_t *query_ctx; //!< Query context for current query.
238
243
252
253int submodule_parse(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, conf_parser_t const *rule)
254{
255 rlm_sql_t *inst = talloc_get_type_abort(parent, rlm_sql_t);
257 int ret;
258
259 if (unlikely((ret = module_rlm_submodule_parse(ctx, out, parent, ci, rule)) < 0)) return ret;
260
261 mi = talloc_get_type_abort(*((void **)out), module_instance_t);
262 inst->driver = (rlm_sql_driver_t const *)mi->exported; /* Public symbol exported by the submodule */
263
264 return 0;
265}
266
267static int _sql_escape_uxtx_free(void *uctx)
268{
269 return talloc_free(uctx);
270}
271
272/*
273 * Create a thread local uctx which is used in SQL value box escaping
274 * so that an already reserved connection can be used.
275 */
276static void *sql_escape_uctx_alloc(UNUSED request_t *request, void const *uctx)
277{
279
280 if (unlikely(t_ctx == NULL)) {
282
283 MEM(ctx = talloc_zero(NULL, rlm_sql_escape_uctx_t));
285 }
286 t_ctx->sql = uctx;
287
288 return t_ctx;
289}
290
291/*
292 * Fall-Through checking function from rlm_files.c
293 */
294static sql_fall_through_t fall_through(map_list_t *maps)
295{
296 bool rcode;
297 map_t *map, *next;
298
299 for (map = map_list_head(maps);
300 map != NULL;
301 map = next) {
302 next = map_list_next(maps, map);
303
305
307 (void) map_list_remove(maps, map);
308
309 if (tmpl_is_data(map->rhs)) {
311
312 rcode = tmpl_value(map->rhs)->vb_bool;
313 } else {
314 rcode = false;
315 }
316
317 talloc_free(map);
318 return rcode;
319 }
320 }
321
323}
324
325/** Escape a tainted VB used as an xlat argument
326 *
327 */
328static int CC_HINT(nonnull) sql_box_escape(fr_value_box_t *vb, void *uctx)
329{
330 void *arg = NULL;
331 rlm_sql_escape_uctx_t *ctx = uctx;
333 rlm_sql_thread_t *thread = talloc_get_type_abort(module_thread(inst->mi)->data, rlm_sql_thread_t);
334
335 /*
336 * If it's already safe, don't do anything.
337 */
338 if (fr_value_box_is_safe_for(vb, inst->driver)) return 0;
339
340 /*
341 * Don't print "::" for enum names. Instead we convert
342 * the box to a string which contains the enum name, and
343 * then see if we need to escape it.
344 */
345 if (vb->enumv && vb->enumv->flags.has_value) {
346 char const *name;
347
348 name = fr_dict_enum_name_by_value(vb->enumv, vb);
349 if (name) {
350 int rcode;
351
352 /*
353 * Store list pointers to restore later - fr_value_box_cast clears them
354 */
355 fr_value_box_entry_t entry = vb->entry;
356
357 rcode = fr_value_box_strdup(vb, vb, NULL, name, false);
358 vb->entry = entry;
359
360 if (rcode < 0) return rcode;
361
362 goto check_escape_arg;
363 }
364 }
365
366 /*
367 * No need to escape types with inherently safe data
368 */
369 switch (vb->type) {
370 case FR_TYPE_NUMERIC:
371 case FR_TYPE_IP:
372 case FR_TYPE_ETHERNET:
373 fr_value_box_mark_safe_for(vb, inst->driver);
374 return 0;
375
376 default:
377 break;
378 }
379
380check_escape_arg:
381 if (inst->sql_escape_arg) {
382 arg = inst->sql_escape_arg;
383 } else if (thread->sql_escape_arg) {
384 arg = thread->sql_escape_arg;
385 }
386 if (!arg) {
387 error:
389 return -1;
390 }
391
392 /*
393 * The escape function works with the bytes already in the box
394 * (using vb_length, not strlen()) so binary data containing NUL
395 * bytes survives the escape. Cast non-string boxes to a string
396 * first - the cast preserves the underlying bytes.
397 */
398 if ((vb->type != FR_TYPE_STRING) && (fr_value_box_cast_in_place(vb, vb, FR_TYPE_STRING, NULL) < 0)) goto error;
399
400 if (inst->sql_escape_func(vb, arg) < 0) goto error;
401
402 /*
403 * Different databases have slightly different ideas as
404 * to what is safe. So we track the database type in the
405 * safe value. This means that we don't
406 * cross-contaminate "safe" values across databases.
407 */
408 fr_value_box_mark_safe_for(vb, inst->driver);
409
410 return 0;
411}
412
413/** Escape a value to make it SQL safe.
414 *
415@verbatim
416%sql.escape(<value>)
417@endverbatim
418 *
419 * @ingroup xlat_functions
420 */
421static xlat_action_t sql_escape_xlat(UNUSED TALLOC_CTX *ctx, fr_dcursor_t *out, xlat_ctx_t const *xctx,
422 request_t *request, fr_value_box_list_t *in)
423{
424 rlm_sql_t const *inst = talloc_get_type_abort(xctx->mctx->mi->data, rlm_sql_t);
425 fr_value_box_t *vb;
426 rlm_sql_escape_uctx_t *escape_uctx = NULL;
427
428 while ((vb = fr_value_box_list_pop_head(in))) {
429 if (fr_value_box_is_safe_for(vb, inst->driver)) goto append;
430 if (!escape_uctx) escape_uctx = sql_escape_uctx_alloc(request, inst);
431 if (sql_box_escape(vb, escape_uctx) < 0) {
432 talloc_free(vb);
433 return XLAT_ACTION_FAIL;
434 }
435 append:
437 }
438 return XLAT_ACTION_DONE;
439}
440
441static xlat_action_t sql_xlat_query_resume(TALLOC_CTX *ctx, fr_dcursor_t *out, xlat_ctx_t const *xctx,
442 request_t *request, UNUSED fr_value_box_list_t *in)
443{
444 fr_sql_query_t *query_ctx = talloc_get_type_abort(xctx->rctx, fr_sql_query_t);
445 rlm_sql_t const *inst = query_ctx->inst;
446 fr_value_box_t *vb;
448 int numaffected;
449
450 fr_assert(query_ctx->type == SQL_QUERY_OTHER);
451
452 switch (query_ctx->rcode) {
454 case RLM_SQL_ERROR:
457 query_ctx->rcode, "<INVALID>"));
458 rlm_sql_print_error(inst, request, query_ctx, false);
459 ret = XLAT_ACTION_FAIL;
460 goto finish;
461
462 default:
463 break;
464 }
465
466 numaffected = (inst->driver->sql_affected_rows)(query_ctx, &inst->config);
467 if (numaffected < 1) {
468 RDEBUG2("SQL query affected no rows");
469 numaffected = 0;
470 }
471
472 MEM(vb = fr_value_box_alloc_null(ctx));
473 fr_value_box_uint32(vb, NULL, (uint32_t)numaffected, false);
475
476finish:
477 talloc_free(query_ctx);
478
479 return ret;
480}
481
482static xlat_action_t sql_xlat_select_resume(TALLOC_CTX *ctx, fr_dcursor_t *out, xlat_ctx_t const *xctx,
483 request_t *request, UNUSED fr_value_box_list_t *in)
484{
485 fr_sql_query_t *query_ctx = talloc_get_type_abort(xctx->rctx, fr_sql_query_t);
486 rlm_sql_t const *inst = query_ctx->inst;
487 fr_value_box_t *vb;
489 unlang_result_t p_result;
490 rlm_sql_row_t row;
491 bool fetched = false;
492
493 fr_assert(query_ctx->type == SQL_QUERY_SELECT);
494
495 if (query_ctx->rcode != RLM_SQL_OK) {
496 query_error:
498 query_ctx->rcode, "<INVALID>"));
499 rlm_sql_print_error(inst, request, query_ctx, false);
500 ret = XLAT_ACTION_FAIL;
501 goto finish;
502 }
503
504 do {
505 inst->fetch_row(&p_result, request, query_ctx);
506 row = query_ctx->row;
507 switch (query_ctx->rcode) {
508 case RLM_SQL_OK:
509 if (row[0]) break;
510
511 RDEBUG2("NULL value in first column of result");
512 goto finish;
513
515 if (!fetched) RDEBUG2("SQL query returned no results");
516 goto finish;
517
518 default:
519 goto query_error;
520 }
521
522 fetched = true;
523
524 MEM(vb = fr_value_box_alloc_null(ctx));
525 fr_value_box_strdup(vb, vb, NULL, row[0], false);
527
528 } while (1);
529
530finish:
531 talloc_free(query_ctx);
532
533 return ret;
534}
535
536/** Execute an arbitrary SQL query
537 *
538 * For SELECTs, the values of the first column will be returned.
539 * For INSERTS, UPDATEs and DELETEs, the number of rows affected will
540 * be returned instead.
541 *
542@verbatim
543%sql(<sql statement>)
544@endverbatim
545 *
546 * @ingroup xlat_functions
547 */
548static xlat_action_t sql_xlat(TALLOC_CTX *ctx, fr_dcursor_t *out,
549 xlat_ctx_t const *xctx,
550 request_t *request, fr_value_box_list_t *in)
551{
552 sql_xlat_call_env_t *call_env = talloc_get_type_abort(xctx->env_data, sql_xlat_call_env_t);
553 rlm_sql_t const *inst = talloc_get_type_abort(xctx->mctx->mi->data, rlm_sql_t);
554 rlm_sql_thread_t *thread = talloc_get_type_abort(xctx->mctx->thread, rlm_sql_thread_t);
555 char const *p;
556 fr_value_box_t *arg = fr_value_box_list_head(in);
557 fr_sql_query_t *query_ctx = NULL;
559
560 if (call_env->filename.type == FR_TYPE_STRING && call_env->filename.vb_length > 0) {
561 rlm_sql_query_log(inst, call_env->filename.vb_strvalue, arg->vb_strvalue);
562 }
563
564 p = arg->vb_strvalue;
565
566 /*
567 * Trim whitespace for the prefix check
568 */
570
571 /*
572 * If the query starts with any of the following prefixes,
573 * then return the number of rows affected
574 */
575 if ((strncasecmp(p, "insert", 6) == 0) ||
576 (strncasecmp(p, "update", 6) == 0) ||
577 (strncasecmp(p, "delete", 6) == 0)) {
578 MEM(query_ctx = fr_sql_query_alloc(unlang_interpret_frame_talloc_ctx(request), inst, request,
579 thread->trunk, arg->vb_strvalue, SQL_QUERY_OTHER));
580
581 unlang_xlat_yield(request, sql_xlat_query_resume, NULL, 0, query_ctx);
582
583 /* Modify current frame's rcode directly */
584 query_ret = inst->query(unlang_interpret_result(request), request, query_ctx);
586
587 return sql_xlat_query_resume(ctx, out, &(xlat_ctx_t){.rctx = query_ctx, .inst = inst}, request, in);
588 } /* else it's a SELECT statement */
589
590 MEM(query_ctx = fr_sql_query_alloc(unlang_interpret_frame_talloc_ctx(request), inst, request,
591 thread->trunk, arg->vb_strvalue, SQL_QUERY_SELECT));
592
593 unlang_xlat_yield(request, sql_xlat_select_resume, NULL, 0, query_ctx);
594
595 if (unlang_function_push_with_result(/* discard, sql_xlat_select_resume just uses query_ctx->rcode */ NULL,
596 request,
597 inst->select,
598 NULL,
599 NULL, 0,
602}
603
604/** Execute an arbitrary SQL query, expecting results to be returned
605 *
606@verbatim
607%sql.fetch(<sql statement>)
608@endverbatim
609 *
610 * @ingroup xlat_functions
611 */
613 request_t *request, fr_value_box_list_t *in)
614{
615 sql_xlat_call_env_t *call_env = talloc_get_type_abort(xctx->env_data, sql_xlat_call_env_t);
616 rlm_sql_t const *inst = talloc_get_type_abort(xctx->mctx->mi->data, rlm_sql_t);
617 rlm_sql_thread_t *thread = talloc_get_type_abort(xctx->mctx->thread, rlm_sql_thread_t);
618 fr_value_box_t *arg = fr_value_box_list_head(in);
619 fr_sql_query_t *query_ctx = NULL;
620
621 if (call_env->filename.type == FR_TYPE_STRING && call_env->filename.vb_length > 0) {
622 rlm_sql_query_log(inst, call_env->filename.vb_strvalue, arg->vb_strvalue);
623 }
624
625 MEM(query_ctx = fr_sql_query_alloc(unlang_interpret_frame_talloc_ctx(request), inst, request,
626 thread->trunk, arg->vb_strvalue, SQL_QUERY_SELECT));
627
628 unlang_xlat_yield(request, sql_xlat_select_resume, NULL, 0, query_ctx);
629 if (unlang_function_push_with_result(/* discard, sql_xlat_select_resume just uses query_ctx->rcode */NULL,
630 request,
631 inst->select,
632 NULL,
633 NULL, 0,
636}
637
638/** Execute an arbitrary SQL query, returning the number of rows affected
639 *
640@verbatim
641%sql.modify(<sql statement>)
642@endverbatim
643 *
644 * @ingroup xlat_functions
645 */
646static xlat_action_t sql_modify_xlat(TALLOC_CTX *ctx, fr_dcursor_t *out, xlat_ctx_t const *xctx,
647 request_t *request, fr_value_box_list_t *in)
648{
649 sql_xlat_call_env_t *call_env = talloc_get_type_abort(xctx->env_data, sql_xlat_call_env_t);
650 rlm_sql_t const *inst = talloc_get_type_abort(xctx->mctx->mi->data, rlm_sql_t);
651 rlm_sql_thread_t *thread = talloc_get_type_abort(xctx->mctx->thread, rlm_sql_thread_t);
652 fr_value_box_t *arg = fr_value_box_list_head(in);
653 fr_sql_query_t *query_ctx = NULL;
654
655 if (call_env->filename.type == FR_TYPE_STRING && call_env->filename.vb_length > 0) {
656 rlm_sql_query_log(inst, call_env->filename.vb_strvalue, arg->vb_strvalue);
657 }
658
659 MEM(query_ctx = fr_sql_query_alloc(unlang_interpret_frame_talloc_ctx(request), inst, request,
660 thread->trunk, arg->vb_strvalue, SQL_QUERY_OTHER));
661
662 unlang_xlat_yield(request, sql_xlat_query_resume, NULL, 0, query_ctx);
663 /* Write out the result directly to this frame's rcode */
664 if (inst->query(unlang_interpret_result(request), request, query_ctx) == UNLANG_ACTION_PUSHED_CHILD) return XLAT_ACTION_PUSH_UNLANG;
665
666 return sql_xlat_query_resume(ctx, out, &(xlat_ctx_t){.rctx = query_ctx, .inst = inst}, request, in);
667}
668
669/** Converts a string value into a #fr_pair_t
670 *
671 * @param[in,out] ctx to allocate #fr_pair_t (s).
672 * @param[out] out where to write the resulting #fr_pair_t.
673 * @param[in] request The current request.
674 * @param[in] map to process.
675 * @param[in] uctx The value to parse.
676 * @return
677 * - 0 on success.
678 * - -1 on failure.
679 */
680static int _sql_map_proc_get_value(TALLOC_CTX *ctx, fr_pair_list_t *out,
681 request_t *request, map_t const *map, void *uctx)
682{
683 fr_pair_t *vp;
684 char const *value = uctx;
685
687 if (!vp) return -1;
688
689 /*
690 * Buffer not always talloced, sometimes it's
691 * just a pointer to a field in a result struct.
692 */
693 if (fr_pair_value_from_str(vp, value, strlen(value), NULL, true) < 0) {
694 RPEDEBUG("Failed parsing value \"%pV\" for attribute %s",
697 return -1;
698 }
700
701 return 0;
702}
703
704/*
705 * Verify the result of the map.
706 */
707static int sql_map_verify(CONF_SECTION *cs, UNUSED void const *mod_inst, UNUSED void *proc_inst,
708 tmpl_t const *src, UNUSED map_list_t const *maps)
709{
710 if (!src) {
711 cf_log_err(cs, "Missing SQL query");
712
713 return -1;
714 }
715
716 return 0;
717}
718
719#define MAX_SQL_FIELD_INDEX (64)
720
721/** Process the results of an SQL map query
722 *
723 * @param p_result Result of map expansion:
724 * - #RLM_MODULE_NOOP no rows were returned or columns matched.
725 * - #RLM_MODULE_UPDATED if one or more #fr_pair_t were added to the #request_t.
726 * - #RLM_MODULE_FAIL if a fault occurred.
727 * @param mpctx Map context, containing the module instance.
728 * @param request The current request.
729 * @param query string to execute.
730 * @param maps Head of the map list.
731 * @return UNLANG_ACTION_CALCULATE_RESULT
732 */
733static unlang_action_t mod_map_resume(unlang_result_t *p_result, map_ctx_t const *mpctx, request_t *request,
734 UNUSED fr_value_box_list_t *query, map_list_t const *maps)
735{
736 fr_sql_query_t *query_ctx = talloc_get_type_abort(mpctx->rctx, fr_sql_query_t);
737 rlm_sql_t const *inst = mpctx->moi;
738 map_t const *map;
740 sql_rcode_t ret;
741 char const **fields = NULL, *map_rhs;
742 rlm_sql_row_t row;
743 int i, j, field_cnt, rows = 0;
744 int field_index[MAX_SQL_FIELD_INDEX];
745 char map_rhs_buff[128];
746 bool found_field = false; /* Did we find any matching fields in the result set ? */
747
748 if (query_ctx->rcode != RLM_SQL_OK) {
749 RERROR("SQL query failed: %s", fr_table_str_by_value(sql_rcode_description_table, query_ctx->rcode, "<INVALID>"));
750 rcode = RLM_MODULE_FAIL;
751 goto finish;
752 }
753
754 /*
755 * Not every driver provides an sql_num_rows function
756 */
757 if (inst->driver->sql_num_rows) {
758 ret = inst->driver->sql_num_rows(query_ctx, &inst->config);
759 if (ret == 0) {
760 RDEBUG2("Server returned an empty result");
761 rcode = RLM_MODULE_NOTFOUND;
762 goto finish;
763 }
764
765 if (ret < 0) {
766 RERROR("Failed retrieving row count");
767 error:
768 rcode = RLM_MODULE_FAIL;
769 goto finish;
770 }
771 }
772
773 for (i = 0; i < MAX_SQL_FIELD_INDEX; i++) field_index[i] = -1;
774
775 /*
776 * Map proc only registered if driver provides an sql_fields function
777 */
778 ret = (inst->driver->sql_fields)(&fields, query_ctx, &inst->config);
779 if (ret != RLM_SQL_OK) {
780 RERROR("Failed retrieving field names: %s", fr_table_str_by_value(sql_rcode_description_table, ret, "<INVALID>"));
781 goto error;
782 }
783 fr_assert(fields);
784 field_cnt = talloc_array_length(fields);
785
786 if (RDEBUG_ENABLED3) for (j = 0; j < field_cnt; j++) RDEBUG3("Got field: %s", fields[j]);
787
788 /*
789 * Iterate over the maps, it's O(N2)ish but probably
790 * faster than building a radix tree each time the
791 * map set is evaluated (map->rhs can be dynamic).
792 */
793 for (map = map_list_head(maps), i = 0;
794 map && (i < MAX_SQL_FIELD_INDEX);
795 map = map_list_next(maps, map), i++) {
796 /*
797 * Expand the RHS to get the name of the SQL field
798 */
799 if (tmpl_expand(&map_rhs, map_rhs_buff, sizeof(map_rhs_buff),
800 request, map->rhs) < 0) {
801 RPERROR("Failed getting field name");
802 goto error;
803 }
804
805 for (j = 0; j < field_cnt; j++) {
806 if (strcasecmp(fields[j], map_rhs) != 0) continue;
807 field_index[i] = j;
808 found_field = true;
809 }
810 }
811
812 /*
813 * Couldn't resolve any map RHS values to fields
814 * in the result set.
815 */
816 if (!found_field) {
817 RDEBUG2("No fields matching map found in query result");
818 rcode = RLM_MODULE_NOOP;
819 goto finish;
820 }
821
822 /*
823 * We've resolved all the maps to result indexes, now convert
824 * the values at those indexes into fr_pair_ts.
825 *
826 * Note: Not all SQL client libraries provide a row count,
827 * so we have to do the count here.
828 */
829 while ((inst->fetch_row(p_result, request, query_ctx) == UNLANG_ACTION_CALCULATE_RESULT) &&
830 (query_ctx->rcode == RLM_SQL_OK)) {
831 row = query_ctx->row;
832 rows++;
833 for (map = map_list_head(maps), j = 0;
834 map && (j < MAX_SQL_FIELD_INDEX);
835 map = map_list_next(maps, map), j++) {
836 if (field_index[j] < 0) continue; /* We didn't find the map RHS in the field set */
837 if (!row[field_index[j]]) {
838 RDEBUG2("Database returned NULL for %s", fields[j]);
839 continue;
840 }
841 if (map_to_request(request, map, _sql_map_proc_get_value, row[field_index[j]]) < 0) goto error;
842 }
843 }
844
845 if (query_ctx->rcode == RLM_SQL_ERROR) goto error;
846
847 if (rows == 0) {
848 RDEBUG2("SQL query returned no results");
849 rcode = RLM_MODULE_NOTFOUND;
850 }
851
852finish:
853 talloc_free(fields);
854 talloc_free(query_ctx);
855
856 RETURN_UNLANG_RCODE(rcode);
857}
858
859/** Executes a SELECT query and maps the result to server attributes
860 *
861 * @param p_result Result of map expansion:
862 * - #RLM_MODULE_NOOP no rows were returned or columns matched.
863 * - #RLM_MODULE_UPDATED if one or more #fr_pair_t were added to the #request_t.
864 * - #RLM_MODULE_FAIL if a fault occurred.
865 * @param mpctx Map context, containing the module instance.
866 * @param request The current request.
867 * @param query string to execute.
868 * @param maps Head of the map list.
869 * @return UNLANG_ACTION_CALCULATE_RESULT
870 */
871static unlang_action_t mod_map_proc(unlang_result_t *p_result, map_ctx_t const *mpctx, request_t *request,
872 fr_value_box_list_t *query, UNUSED map_list_t const *maps)
873{
875 rlm_sql_thread_t *thread = talloc_get_type_abort(module_thread(inst->mi)->data, rlm_sql_thread_t);
876 fr_value_box_t *query_head = fr_value_box_list_head(query);
877 fr_sql_query_t *query_ctx = NULL;
878 fr_value_box_t *vb = NULL;
879 rlm_sql_escape_uctx_t *escape_uctx = NULL;
880
881 fr_assert(inst->driver->sql_fields); /* Should have been caught during validation... */
882
883 if (!query_head) {
884 REDEBUG("Query cannot be (null)");
886 }
887
888 while ((vb = fr_value_box_list_next(query, vb))) {
889 if (fr_value_box_is_safe_for(vb, inst->driver)) continue;
890 if (!escape_uctx) escape_uctx = sql_escape_uctx_alloc(request, inst);
891 if (sql_box_escape(vb, escape_uctx) < 0) RETURN_UNLANG_FAIL;
892 }
893
895 query_head, query, FR_TYPE_STRING,
897 SIZE_MAX) < 0) {
898 RPEDEBUG("Failed concatenating input string");
900 }
901
902 query_ctx = fr_sql_query_alloc(unlang_interpret_frame_talloc_ctx(request), inst, request,
903 thread->trunk, query_head->vb_strvalue, SQL_QUERY_SELECT);
904
905 if (unlang_map_yield(request, mod_map_resume, NULL, 0, query_ctx) != UNLANG_ACTION_YIELD) RETURN_UNLANG_FAIL;
906 return unlang_function_push_with_result(/* discard, mod_map_resume just uses query_ctx->rcode */ NULL,
907 request,
908 inst->select,
909 NULL,
910 NULL,
912 query_ctx);
913}
914
915/** Default escape function for drivers which do not provide their own
916 *
917 * Multi-byte UTF-8 characters pass through unchanged, `\n` / `\r` / `\t` become
918 * their backslash-escaped equivalents, and anything else outside
919 * `allowed_chars` is replaced with `=XX` (mime-style hex).
920 */
921static int sql_escape_func(fr_value_box_t *vb, void *arg)
922{
924 char const *in = vb->vb_strvalue;
925 char const *end = in + vb->vb_length;
926 char *out, *p;
927 size_t outlen = vb->vb_length * 3 + 1;
928 size_t len = 0;
929
930 /*
931 * Worst case: every input byte expands to `=XX` (3 chars), plus a NUL.
932 */
933 MEM(p = out = talloc_array(vb, char, outlen));
934
935 while (in < end) {
936 size_t utf8_len;
937
938 /*
939 * Allow all multi-byte UTF8 characters.
940 */
941 utf8_len = fr_utf8_char((uint8_t const *) in, end - in);
942 if (utf8_len > 1) {
943 memcpy(p, in, utf8_len);
944 in += utf8_len;
945 p += utf8_len;
946 len += utf8_len;
947 continue;
948 }
949
950 /*
951 * Because we register our own escape function
952 * we're now responsible for escaping all special
953 * chars in an xlat expansion or attribute value.
954 */
955 switch (*in) {
956 case '\n':
957 p[0] = '\\';
958 p[1] = 'n';
959 goto next;
960
961 case '\r':
962 p[0] = '\\';
963 p[1] = 'r';
964 goto next;
965
966 case '\t':
967 p[0] = '\\';
968 p[1] = 't';
969
970 next:
971 in++;
972 p += 2;
973 len += 2;
974 continue;
975 }
976
977 /*
978 * Non-printable characters (including embedded NULs) get
979 * replaced with their mime-encoded equivalents.
980 */
981 if (((unsigned char)*in < 32) ||
982 strchr(inst->config.allowed_chars, *in) == NULL) {
983 snprintf(p, 4, "=%02X", (unsigned char) in[0]);
984 in++;
985 p += 3;
986 len += 3;
987 continue;
988 }
989
990 /*
991 * Allowed character.
992 */
993 *p++ = *in++;
994 len++;
995 }
996 *p = '\0';
997
998 /*
999 * Shrink the buffer to fit the actual escaped length, then
1000 * hand it to the box.
1001 */
1002 if (len + 1 < outlen) MEM(out = talloc_realloc(vb, out, char, len + 1));
1004
1005 return 0;
1006}
1007
1008/*
1009 * Set the SQL user name.
1010 *
1011 * We don't call the escape function here. The resulting string
1012 * will be escaped later in the queries xlat so we don't need to
1013 * escape it twice. (it will make things wrong if we have an
1014 * escape candidate character in the username)
1015 */
1016static void sql_set_user(rlm_sql_t const *inst, request_t *request, fr_value_box_t *user)
1017{
1018 fr_pair_t *vp = NULL;
1019
1020 fr_assert(request->packet != NULL);
1021
1022 MEM(pair_update_request(&vp, inst->sql_user) >= 0);
1023 if(!user || (user->type != FR_TYPE_STRING)) {
1025 return;
1026 }
1027
1028 /*
1029 * Replace any existing SQL-User-Name with new value
1030 */
1031 fr_pair_value_bstrdup_buffer(vp, user->vb_strvalue, user->tainted);
1032 RDEBUG2("SQL-User-Name set to '%pV'", &vp->data);
1033}
1034
1035/*
1036 * Do a set/unset user, so it's a bit clearer what's going on.
1037 */
1038#define sql_unset_user(_i, _r) fr_pair_delete_by_da(&_r->request_pairs, _i->sql_user)
1039
1040
1045
1047{
1048 sql_group_ctx_t *group_ctx = talloc_get_type_abort(uctx, sql_group_ctx_t);
1049 fr_sql_query_t *query_ctx = group_ctx->query_ctx;
1050 rlm_sql_t const *inst = group_ctx->inst;
1051 rlm_sql_row_t row;
1052 rlm_sql_grouplist_t *entry = group_ctx->groups;
1053
1054 if (query_ctx->rcode != RLM_SQL_OK) {
1055 error:
1056 rlm_sql_print_error(inst, request, query_ctx, false);
1057 talloc_free(query_ctx);
1059 }
1060
1061 while ((inst->fetch_row(p_result, request, query_ctx) == UNLANG_ACTION_CALCULATE_RESULT) &&
1062 (query_ctx->rcode == RLM_SQL_OK)) {
1063 row = query_ctx->row;
1064 if (!row[0]){
1065 RDEBUG2("row[0] returned NULL");
1066 goto error;
1067 }
1068
1069 if (!entry) {
1070 group_ctx->groups = talloc_zero(group_ctx, rlm_sql_grouplist_t);
1071 entry = group_ctx->groups;
1072 } else {
1073 entry->next = talloc_zero(group_ctx, rlm_sql_grouplist_t);
1074 entry = entry->next;
1075 }
1076 entry->next = NULL;
1077 entry->name = talloc_strdup(entry, row[0]);
1078
1079 group_ctx->num_groups++;
1080 }
1081
1082 talloc_free(query_ctx);
1084}
1085
1087{
1088 rlm_sql_t const *inst = group_ctx->inst;
1089
1090 /* NOTE: sql_set_user should have been run before calling this function */
1091
1092 if (!group_ctx->query || (group_ctx->query->vb_length == 0)) return UNLANG_ACTION_CALCULATE_RESULT;
1093
1094 MEM(group_ctx->query_ctx = fr_sql_query_alloc(group_ctx, inst, request, trunk,
1095 group_ctx->query->vb_strvalue, SQL_QUERY_SELECT));
1096
1097 if (unlang_function_push_with_result(/* sql_get_grouplist_resume translates the query_ctx->rocde into a module rcode */p_result,
1098 request,
1099 NULL,
1101 NULL,
1103 group_ctx) < 0) return UNLANG_ACTION_FAIL;
1104
1105 return unlang_function_push_with_result(/* discard, sql_get_grouplist_resume translates rcodes */NULL,
1106 request,
1107 inst->select,
1108 NULL,
1109 NULL, 0,
1110 UNLANG_SUB_FRAME, group_ctx->query_ctx);
1111}
1112
1113typedef struct {
1114 fr_value_box_list_t query;
1117
1118/** Compare list of groups returned from SQL query to xlat argument.
1119 *
1120 * Called after the SQL query has completed and group list has been built.
1121 */
1123 UNUSED request_t *request, fr_value_box_list_t *in)
1124{
1125 sql_group_xlat_ctx_t *xlat_ctx = talloc_get_type_abort(xctx->rctx, sql_group_xlat_ctx_t);
1126 sql_group_ctx_t *group_ctx = talloc_get_type_abort(xlat_ctx->group_ctx, sql_group_ctx_t);
1127 fr_value_box_t *arg = fr_value_box_list_head(in);
1128 char const *name = arg->vb_strvalue;
1129 fr_value_box_t *vb;
1130 rlm_sql_grouplist_t *entry;
1131
1133
1135 for (entry = group_ctx->groups; entry != NULL; entry = entry->next) {
1136 if (strcmp(entry->name, name) == 0) {
1137 vb->vb_bool = true;
1138 break;
1139 }
1140 }
1142
1144
1145 return XLAT_ACTION_DONE;
1146}
1147
1148/** Run SQL query for group membership to return list of groups
1149 *
1150 * Called after group membership query tmpl is expanded
1151 */
1153 request_t *request, UNUSED fr_value_box_list_t *in)
1154{
1155 sql_group_xlat_ctx_t *xlat_ctx = talloc_get_type_abort(xctx->rctx, sql_group_xlat_ctx_t);
1156 rlm_sql_t const *inst = talloc_get_type_abort(xctx->mctx->mi->data, rlm_sql_t);
1157 rlm_sql_thread_t *thread = talloc_get_type_abort(xctx->mctx->thread, rlm_sql_thread_t);
1158 fr_value_box_t *query;
1159
1160 query = fr_value_box_list_head(&xlat_ctx->query);
1161 if (!query) return XLAT_ACTION_FAIL;
1162
1163 MEM(xlat_ctx->group_ctx = talloc(xlat_ctx, sql_group_ctx_t));
1164
1165 *xlat_ctx->group_ctx = (sql_group_ctx_t) {
1166 .inst = inst,
1167 .query = query,
1168 };
1169
1171
1172 if (sql_get_grouplist(NULL, xlat_ctx->group_ctx, thread->trunk, request) != UNLANG_ACTION_PUSHED_CHILD) {
1173 return XLAT_ACTION_FAIL;
1174 }
1175
1177}
1178
1179
1180/** Check if the user is a member of a particular group
1181 *
1182@verbatim
1183%sql.group(<name>)
1184@endverbatim
1185 *
1186 * @ingroup xlat_functions
1187 */
1189 request_t *request, UNUSED fr_value_box_list_t *in)
1190{
1191 sql_group_xlat_call_env_t *call_env = talloc_get_type_abort(xctx->env_data, sql_group_xlat_call_env_t);
1193 rlm_sql_t const *inst = talloc_get_type_abort(xctx->mctx->mi->data, rlm_sql_t);
1194
1195 if (!call_env->membership_query) {
1196 RWARN("Cannot check group membership - group_membership_query not set");
1197 return XLAT_ACTION_FAIL;
1198 }
1199
1200 /*
1201 * Set the user attr here
1202 */
1203 sql_set_user(inst, request, &call_env->user);
1204
1206 fr_value_box_list_init(&xlat_ctx->query);
1207
1209 if (unlang_tmpl_push(xlat_ctx, NULL, &xlat_ctx->query, request, call_env->membership_query, NULL, UNLANG_SUB_FRAME) < 0) return XLAT_ACTION_FAIL;
1211}
1212
1213/** Process a "check" map
1214 *
1215 * Any entries using an assignment operator will be moved to the reply map
1216 * for later merging into the request.
1217 *
1218 * @param request Current request.
1219 * @param check_map to process.
1220 * @param reply_map where any assignment entries will be moved.
1221 * @return
1222 * - 0 if all the check entries pass.
1223 * - -1 if the checks fail.
1224 */
1225static int check_map_process(request_t *request, map_list_t *check_map, map_list_t *reply_map)
1226{
1227 map_t *map, *next;
1228
1229 for (map = map_list_head(check_map);
1230 map != NULL;
1231 map = next) {
1232 next = map_list_next(check_map, map);
1233
1234 if (fr_assignment_op[map->op]) {
1235 (void) map_list_remove(check_map, map);
1236 map_list_insert_tail(reply_map, map);
1237 continue;
1238 }
1239
1240 if (!fr_comparison_op[map->op]) {
1241 REDEBUG("Invalid operator '%s'", fr_tokens[map->op]);
1242 goto fail;
1243 }
1244
1245 if (fr_type_is_structural(tmpl_attr_tail_da(map->lhs)->type) &&
1246 (map->op != T_OP_CMP_TRUE) && (map->op != T_OP_CMP_FALSE)) {
1247 REDEBUG("Invalid comparison for structural type");
1248 goto fail;
1249 }
1250
1251 RDEBUG2(" &%s %s %s", map->lhs->name, fr_tokens[map->op], map->rhs->name);
1252 if (radius_legacy_map_cmp(request, map) != 1) {
1253 fail:
1254 map_list_talloc_free(check_map);
1255 map_list_talloc_free(reply_map);
1256 RDEBUG2("failed match: skipping this entry");
1257 return -1;
1258 }
1259 }
1260 return 0;
1261}
1262
1264{
1265 if (!to_free->inst->sql_escape_arg) (void) request_data_get(to_free->request, (void *)sql_escape_uctx_alloc, 0);
1266 map_list_talloc_free(&to_free->check_tmp);
1267 map_list_talloc_free(&to_free->reply_tmp);
1268 sql_unset_user(to_free->inst, to_free->request);
1269
1270 return 0;
1271}
1272
1273/** Resume function called after authorization group / profile expansion of check / reply query tmpl
1274 *
1275 * Groups and profiles are treated almost identically except:
1276 * - groups are read from an SQL query
1277 * - profiles are read from &control.User-Profile
1278 * - if `cache_groups` is set, groups populate &control.SQL-Group
1279 *
1280 * Profiles are handled after groups, and will not happend if the last group resulted in `Fall-Through = no`
1281 *
1282 * Before each query is run, &request.SQL-Group is populated with the value of the group being evaluated.
1283 *
1284 * @param p_result Result of current authorization.
1285 * @param mctx Current request.
1286 * @param request Current authorization context.
1287 * @return one of the RLM_MODULE_* values.
1288 */
1290{
1291 sql_autz_ctx_t *autz_ctx = talloc_get_type_abort(mctx->rctx, sql_autz_ctx_t);
1292 sql_autz_call_env_t *call_env = autz_ctx->call_env;
1293 sql_group_ctx_t *group_ctx = autz_ctx->group_ctx;
1294 fr_sql_map_ctx_t *map_ctx = autz_ctx->map_ctx;
1295 rlm_sql_t const *inst = autz_ctx->inst;
1296 fr_value_box_t *query = fr_value_box_list_pop_head(&autz_ctx->query);
1297 sql_fall_through_t do_fall_through = FALL_THROUGH_DEFAULT;
1298 fr_pair_t *vp;
1299
1300 switch (p_result->rcode) {
1303
1304 default:
1305 break;
1306 }
1307
1308 switch(autz_ctx->status) {
1310 if (!query) RETURN_UNLANG_FAIL;
1312 MEM(autz_ctx->group_ctx = talloc(autz_ctx, sql_group_ctx_t));
1313 *autz_ctx->group_ctx = (sql_group_ctx_t) {
1314 .inst = inst,
1315 .query = query,
1316 };
1317
1318 if (sql_get_grouplist(p_result, autz_ctx->group_ctx, autz_ctx->trunk, request) == UNLANG_ACTION_PUSHED_CHILD) {
1321 }
1322
1323 group_ctx = autz_ctx->group_ctx;
1324
1326
1328 talloc_free(group_ctx->query);
1329
1330 if (group_ctx->num_groups == 0) {
1331 RDEBUG2("User not found in any groups");
1332 break;
1333 }
1334 fr_assert(group_ctx->groups);
1335
1336 RDEBUG2("User found in the group table");
1337 autz_ctx->user_found = true;
1338 autz_ctx->group = group_ctx->groups;
1339 MEM(pair_update_request(&autz_ctx->sql_group, inst->group_da) >= 0);
1340
1341 next_group:
1342 fr_pair_value_strdup(autz_ctx->sql_group, autz_ctx->group->name, true);
1343 autz_ctx->status = SQL_AUTZ_GROUP_CHECK;
1345
1347 next_profile:
1348 if (autz_ctx->status & SQL_AUTZ_STAGE_PROFILE) {
1349 fr_pair_value_strdup(autz_ctx->sql_group, autz_ctx->profile->vp_strvalue, true);
1350 autz_ctx->status = SQL_AUTZ_PROFILE_CHECK;
1351 }
1352 RDEBUG3("Processing %s %pV",
1353 autz_ctx->status & SQL_AUTZ_STAGE_GROUP ? "group" : "profile", &autz_ctx->sql_group->data);
1354 if (inst->config.cache_groups && autz_ctx->status & SQL_AUTZ_STAGE_GROUP) {
1355 MEM(pair_append_control(&vp, inst->group_da) >= 0);
1356 fr_pair_value_strdup(vp, autz_ctx->group->name, true);
1357 }
1358
1359 if (call_env->group_check_query) {
1361 if (unlang_tmpl_push(autz_ctx, NULL, &autz_ctx->query, request,
1364 }
1365
1366 if (call_env->group_reply_query) goto group_reply_push;
1367
1368 break;
1369
1372 if (!query) RETURN_UNLANG_FAIL;
1373 *autz_ctx->map_ctx = (fr_sql_map_ctx_t) {
1374 .ctx = autz_ctx,
1375 .inst = inst,
1376 .out = &autz_ctx->check_tmp,
1377 .list = request_attr_request,
1378 .query = query,
1379 };
1380
1382 if (sql_get_map_list(p_result, request, map_ctx, autz_ctx->trunk) == UNLANG_ACTION_PUSHED_CHILD) {
1385 }
1386
1388
1391 talloc_free(map_ctx->query);
1392
1393 /*
1394 * If we got check rows we need to process them before we decide to
1395 * process the reply rows
1396 */
1397 if (map_ctx->rows > 0) {
1398 if (check_map_process(request, &autz_ctx->check_tmp, &autz_ctx->reply_tmp) < 0) {
1399 map_list_talloc_free(&autz_ctx->check_tmp);
1400 goto next_group_find;
1401 }
1402 RDEBUG2("%s \"%pV\": Conditional check items matched",
1403 autz_ctx->status & SQL_AUTZ_STAGE_GROUP ? "Group" : "Profile", &autz_ctx->sql_group->data);
1404 } else {
1405 RDEBUG2("%s \"%pV\": Conditional check items matched (empty)",
1406 autz_ctx->status & SQL_AUTZ_STAGE_GROUP ? "Group" : "Profile", &autz_ctx->sql_group->data);
1407 }
1408
1409 if (autz_ctx->rcode == RLM_MODULE_NOOP) autz_ctx->rcode = RLM_MODULE_OK;
1410
1411 map_list_talloc_free(&autz_ctx->check_tmp);
1412
1413 if (call_env->group_reply_query) {
1414 group_reply_push:
1416 if (unlang_tmpl_push(autz_ctx, NULL, &autz_ctx->query, request,
1420 }
1421
1422 if (map_list_num_elements(&autz_ctx->reply_tmp)) goto group_attr_cache;
1423
1424 goto next_group_find;
1425
1428 if (!query) RETURN_UNLANG_FAIL;
1429 *autz_ctx->map_ctx = (fr_sql_map_ctx_t) {
1430 .ctx = autz_ctx,
1431 .inst = inst,
1432 .out = &autz_ctx->reply_tmp,
1433 .list = request_attr_reply,
1434 .query = query,
1435 .expand_rhs = true,
1436 };
1437
1439 if (sql_get_map_list(p_result, request, map_ctx, autz_ctx->trunk) == UNLANG_ACTION_PUSHED_CHILD) {
1442 }
1443
1445
1448 talloc_free(map_ctx->query);
1449
1450 if (map_ctx->rows == 0) {
1451 do_fall_through = FALL_THROUGH_DEFAULT;
1452 goto group_attr_cache;
1453 }
1454
1455 fr_assert(!map_list_empty(&autz_ctx->reply_tmp)); /* coverity, among others */
1456 do_fall_through = fall_through(&autz_ctx->reply_tmp);
1457
1458 group_attr_cache:
1459 if (inst->config.cache_groups && autz_ctx->status & SQL_AUTZ_STAGE_GROUP) {
1460 MEM(pair_append_control(&vp, inst->group_da) >= 0);
1461 fr_pair_value_strdup(vp, autz_ctx->group->name, true);
1462 }
1463
1464 if (map_list_num_elements(&autz_ctx->reply_tmp) == 0) goto next_group_find;
1465 RDEBUG2("%s \"%pV\": Merging control and reply items",
1466 autz_ctx->status & SQL_AUTZ_STAGE_GROUP ? "Group" : "Profile", &autz_ctx->sql_group->data);
1467 autz_ctx->rcode = RLM_MODULE_UPDATED;
1468
1469 RINDENT();
1470 if (radius_legacy_map_list_apply(request, &autz_ctx->reply_tmp, NULL) < 0) {
1471 RPEDEBUG("Failed applying reply item");
1472 REXDENT();
1474 }
1475 REXDENT();
1476 map_list_talloc_free(&autz_ctx->reply_tmp);
1477
1478 next_group_find:
1479 if (do_fall_through != FALL_THROUGH_YES) break;
1480 if (autz_ctx->status & SQL_AUTZ_STAGE_PROFILE) {
1481 autz_ctx->profile = fr_pair_find_by_da(&request->control_pairs, autz_ctx->profile, attr_user_profile);
1482 if (autz_ctx->profile) goto next_profile;
1483 break;
1484 }
1485 autz_ctx->group = autz_ctx->group->next;
1486 if (autz_ctx->group) goto next_group;
1487
1488 break;
1489
1490 default:
1491 fr_assert(0);
1492 }
1493
1494 /*
1495 * If group processing has completed, check to see if profile processing should be done
1496 */
1497 if ((autz_ctx->status & SQL_AUTZ_STAGE_GROUP) &&
1498 ((do_fall_through == FALL_THROUGH_YES) ||
1499 (inst->config.read_profiles && (do_fall_through == FALL_THROUGH_DEFAULT)))) {
1500 RDEBUG3("... falling-through to profile processing");
1501
1502 autz_ctx->profile = fr_pair_find_by_da(&request->control_pairs, NULL, attr_user_profile);
1503 if (autz_ctx->profile) {
1504 MEM(pair_update_request(&autz_ctx->sql_group, inst->group_da) >= 0);
1505 autz_ctx->status = SQL_AUTZ_PROFILE_START;
1506 goto next_profile;
1507 }
1508 }
1509
1510 if (!autz_ctx->user_found) RETURN_UNLANG_NOTFOUND;
1511
1512 RETURN_UNLANG_RCODE(autz_ctx->rcode);
1513}
1514
1515/** Resume function called after authorization check / reply tmpl expansion
1516 *
1517 * @param p_result Result of current authorization.
1518 * @param mctx Module call ctx.
1519 * @param request Current request.
1520 * @return one of the RLM_MODULE_* values.
1521 */
1523{
1524 sql_autz_ctx_t *autz_ctx = talloc_get_type_abort(mctx->rctx, sql_autz_ctx_t);
1525 sql_autz_call_env_t *call_env = autz_ctx->call_env;
1526 rlm_sql_t const *inst = autz_ctx->inst;
1527 fr_value_box_t *query = fr_value_box_list_pop_head(&autz_ctx->query);
1528 sql_fall_through_t do_fall_through = FALL_THROUGH_DEFAULT;
1529 fr_sql_map_ctx_t *map_ctx = autz_ctx->map_ctx;
1530
1531 /*
1532 * If a previous async call returned one of the "failure" results just return.
1533 */
1534 switch (p_result->rcode) {
1537
1538 default:
1539 break;
1540 }
1541
1542 switch(autz_ctx->status) {
1543 case SQL_AUTZ_CHECK:
1544 if (!query) RETURN_UNLANG_FAIL;
1545 *autz_ctx->map_ctx = (fr_sql_map_ctx_t) {
1546 .ctx = autz_ctx,
1547 .inst = inst,
1548 .out = &autz_ctx->check_tmp,
1549 .list = request_attr_request,
1550 .query = query,
1551 };
1552
1554 if (sql_get_map_list(p_result, request, map_ctx, autz_ctx->trunk) == UNLANG_ACTION_PUSHED_CHILD){
1555 autz_ctx->status = SQL_AUTZ_CHECK_RESUME;
1557 }
1558
1560
1562 talloc_free(map_ctx->query);
1563
1564 if (map_ctx->rows == 0) goto skip_reply; /* Don't need to handle map entries we don't have */
1565
1566 /*
1567 * Only do this if *some* check pairs were returned
1568 */
1569 RDEBUG2("User found in radcheck table");
1570 autz_ctx->user_found = true;
1571
1572 if (check_map_process(request, &autz_ctx->check_tmp, &autz_ctx->reply_tmp) < 0) goto skip_reply;
1573 RDEBUG2("Conditional check items matched");
1574
1575 autz_ctx->rcode = RLM_MODULE_OK;
1576 map_list_talloc_free(&autz_ctx->check_tmp);
1577
1578 if (!call_env->reply_query) goto skip_reply;
1579
1581 if (unlang_tmpl_push(autz_ctx, NULL, &autz_ctx->query, request, call_env->reply_query, NULL, UNLANG_SUB_FRAME) < 0) RETURN_UNLANG_FAIL;
1582 autz_ctx->status = SQL_AUTZ_REPLY;
1584
1585 case SQL_AUTZ_REPLY:
1586 if (!query) RETURN_UNLANG_FAIL;
1587 *autz_ctx->map_ctx = (fr_sql_map_ctx_t) {
1588 .ctx = autz_ctx,
1589 .inst = inst,
1590 .out = &autz_ctx->reply_tmp,
1591 .list = request_attr_reply,
1592 .query = query,
1593 .expand_rhs = true,
1594 };
1595
1597 if (sql_get_map_list(p_result, request, map_ctx, autz_ctx->trunk) == UNLANG_ACTION_PUSHED_CHILD){
1598 autz_ctx->status = SQL_AUTZ_REPLY_RESUME;
1600 }
1601
1603
1605 talloc_free(map_ctx->query);
1606
1607 if (map_ctx->rows == 0) goto skip_reply;
1608
1609 do_fall_through = fall_through(&autz_ctx->reply_tmp);
1610
1611 RDEBUG2("User found in radreply table");
1612 autz_ctx->user_found = true;
1613
1614 skip_reply:
1615 if (map_list_num_elements(&autz_ctx->reply_tmp)) {
1616 RDEBUG2("Merging control and reply items");
1617 RINDENT();
1618 if (radius_legacy_map_list_apply(request, &autz_ctx->reply_tmp, NULL) < 0) {
1619 RPEDEBUG("Failed applying item");
1620 REXDENT();
1622 }
1623 REXDENT();
1624
1625 autz_ctx->rcode = RLM_MODULE_UPDATED;
1626 map_list_talloc_free(&autz_ctx->reply_tmp);
1627 }
1628
1629 if ((do_fall_through == FALL_THROUGH_YES) ||
1630 (inst->config.read_groups && (do_fall_through == FALL_THROUGH_DEFAULT))) {
1631 RDEBUG3("... falling-through to group processing");
1632
1633 if (!call_env->membership_query) {
1634 RWARN("Cannot check groups when group_membership_query is not set");
1635 break;
1636 }
1637
1638 if (!call_env->group_check_query && !call_env->group_reply_query) {
1639 RWARN("Cannot process groups when neither authorize_group_check_query nor authorize_group_reply_query are set");
1640 break;
1641 }
1642
1644 if (unlang_tmpl_push(autz_ctx, NULL, &autz_ctx->query, request,
1646 autz_ctx->status = SQL_AUTZ_GROUP_MEMB;
1648 }
1649
1650 if ((do_fall_through == FALL_THROUGH_YES) ||
1651 (inst->config.read_profiles && (do_fall_through == FALL_THROUGH_DEFAULT))) {
1652 RDEBUG3("... falling-through to profile processing");
1653
1654 if (!call_env->group_check_query && !call_env->group_reply_query) {
1655 RWARN("Cannot process profiles when neither authorize_group_check_query nor authorize_group_reply_query are set");
1656 break;
1657 }
1658
1659 autz_ctx->profile = fr_pair_find_by_da(&request->control_pairs, NULL, attr_user_profile);
1660 if (!autz_ctx->profile) break;
1661
1662 MEM(pair_update_request(&autz_ctx->sql_group, inst->group_da) >= 0);
1663 autz_ctx->status = SQL_AUTZ_PROFILE_START;
1664 return mod_autz_group_resume(p_result, mctx, request);
1665 }
1666 break;
1667
1668 default:
1669 fr_assert_msg(0, "Invalid status %d in mod_authorize_resume", autz_ctx->status);
1670 }
1671
1672 if (!autz_ctx->user_found) RETURN_UNLANG_NOTFOUND;
1673 RETURN_UNLANG_RCODE(autz_ctx->rcode);
1674}
1675
1676/** Start of module authorize method
1677 *
1678 * Pushes the tmpl relating to the first required query for evaluation
1679 */
1680static unlang_action_t CC_HINT(nonnull) mod_authorize(unlang_result_t *p_result, module_ctx_t const *mctx, request_t *request)
1681{
1683 rlm_sql_thread_t *thread = talloc_get_type_abort(mctx->thread, rlm_sql_thread_t);
1684 sql_autz_call_env_t *call_env = talloc_get_type_abort(mctx->env_data, sql_autz_call_env_t);
1685 sql_autz_ctx_t *autz_ctx;
1686
1687 fr_assert(request->packet != NULL);
1688 fr_assert(request->reply != NULL);
1689
1690 if (!call_env->check_query && !call_env->reply_query && !(inst->config.read_groups && call_env->membership_query)) {
1691 RWDEBUG("No authorization checks configured, returning noop");
1693 }
1694
1695 /*
1696 * Set and check the user attr here
1697 */
1698 sql_set_user(inst, request, &call_env->user);
1699
1700 MEM(autz_ctx = talloc(unlang_interpret_frame_talloc_ctx(request), sql_autz_ctx_t));
1701 *autz_ctx = (sql_autz_ctx_t) {
1702 .inst = inst,
1703 .call_env = call_env,
1704 .request = request,
1705 .trunk = thread->trunk,
1706 .rcode = RLM_MODULE_NOOP
1707 };
1708 map_list_init(&autz_ctx->check_tmp);
1709 map_list_init(&autz_ctx->reply_tmp);
1710 MEM(autz_ctx->map_ctx = talloc_zero(autz_ctx, fr_sql_map_ctx_t));
1711 talloc_set_destructor(autz_ctx, sql_autz_ctx_free);
1712
1713 if (unlang_module_yield(request,
1715 NULL, 0, autz_ctx) == UNLANG_ACTION_FAIL) {
1716 error:
1717 talloc_free(autz_ctx);
1719 }
1720
1721 fr_value_box_list_init(&autz_ctx->query);
1722
1723 /*
1724 * Query the check table to find any conditions associated with this user/realm/whatever...
1725 */
1726 if (call_env->check_query) {
1727 if (unlang_tmpl_push(autz_ctx, NULL, &autz_ctx->query, request, call_env->check_query, NULL, UNLANG_SUB_FRAME) < 0) goto error;
1728 autz_ctx->status = SQL_AUTZ_CHECK;
1730 }
1731
1732 if (call_env->reply_query) {
1733 if (unlang_tmpl_push(autz_ctx, NULL, &autz_ctx->query, request, call_env->reply_query, NULL, UNLANG_SUB_FRAME) < 0) goto error;
1734 autz_ctx->status = SQL_AUTZ_REPLY;
1736 }
1737
1738 /*
1739 * Neither check nor reply queries were set, so we must be doing group stuff
1740 */
1741 if (unlang_tmpl_push(autz_ctx, NULL, &autz_ctx->query, request, call_env->membership_query, NULL, UNLANG_SUB_FRAME) < 0) goto error;
1742 autz_ctx->status = SQL_AUTZ_GROUP_MEMB;
1744}
1745
1746/** Tidy up when freeing an SQL redundant context
1747 *
1748 * Release the connection handle and unset the SQL-User attribute.
1749 */
1751{
1752 if (!to_free->inst->sql_escape_arg) (void) request_data_get(to_free->request, (void *)sql_escape_uctx_alloc, 0);
1753 sql_unset_user(to_free->inst, to_free->request);
1754
1755 return 0;
1756}
1757
1758static unlang_action_t mod_sql_redundant_resume(unlang_result_t *p_result, module_ctx_t const *mctx, request_t *request);
1759
1760/** Resume function called after executing an SQL query in a redundant list of queries.
1761 *
1762 * @param p_result Result of current module call.
1763 * @param mctx Current module ctx.
1764 * @param request Current request.
1765 * @return one of the RLM_MODULE_* values.
1766 */
1768{
1769 sql_redundant_ctx_t *redundant_ctx = talloc_get_type_abort(mctx->rctx, sql_redundant_ctx_t);
1770 sql_redundant_call_env_t *call_env = redundant_ctx->call_env;
1771 rlm_sql_t const *inst = redundant_ctx->inst;
1772 fr_sql_query_t *query_ctx = redundant_ctx->query_ctx;
1773 int numaffected = 0;
1774
1775 RDEBUG2("SQL query returned: %s", fr_table_str_by_value(sql_rcode_description_table, query_ctx->rcode, "<INVALID>"));
1776
1777 switch (query_ctx->rcode) {
1778 /*
1779 * Query was a success! Now we just need to check if it did anything.
1780 */
1781 case RLM_SQL_OK:
1782 break;
1783
1784 /*
1785 * A general, unrecoverable server fault.
1786 */
1787 case RLM_SQL_ERROR:
1788 /*
1789 * If we get RLM_SQL_RECONNECT it means all connections in the pool
1790 * were exhausted, and we couldn't create a new connection,
1791 * so we do not need to call fr_pool_connection_release.
1792 */
1793 case RLM_SQL_RECONNECT:
1794 rlm_sql_print_error(inst, request, query_ctx, false);
1796
1797 /*
1798 * Query was invalid, this is a terminal error.
1799 */
1801 rlm_sql_print_error(inst, request, query_ctx, false);
1803
1804 /*
1805 * Driver found an error (like a unique key constraint violation)
1806 * that hinted it might be a good idea to try an alternative query.
1807 */
1808 case RLM_SQL_ALT_QUERY:
1809 goto next;
1810
1812 break;
1813 }
1814
1815 /*
1816 * We need to have updated something for the query to have been
1817 * counted as successful.
1818 */
1819 numaffected = (inst->driver->sql_affected_rows)(query_ctx, &inst->config);
1820 TALLOC_FREE(query_ctx);
1821 RDEBUG2("%i record(s) updated", numaffected);
1822
1823 if (numaffected > 0) {
1824 if (inst->query_number_da) {
1825 fr_pair_t *vp;
1826 if (unlikely(pair_update_control(&vp, inst->query_number_da) < 0)) RETURN_UNLANG_FAIL;
1827 vp->vp_uint32 = redundant_ctx->query_no + 1;
1828 RDEBUG2("control.%pP", vp);
1829 }
1830 RETURN_UNLANG_OK; /* A query succeeded, were done! */
1831 }
1832next:
1833 /*
1834 * Look to see if there are any more queries to expand
1835 */
1836 talloc_free(query_ctx);
1837 redundant_ctx->query_no++;
1838 if (redundant_ctx->query_no >= talloc_array_length(call_env->query)) RETURN_UNLANG_NOOP;
1840 if (unlang_tmpl_push(redundant_ctx, NULL, &redundant_ctx->query, request, call_env->query[redundant_ctx->query_no], NULL, UNLANG_SUB_FRAME) < 0) RETURN_UNLANG_FAIL;
1841
1842 RDEBUG2("Trying next query...");
1843
1845}
1846
1847
1848/** Resume function called after expansion of next query in a redundant list of queries
1849 *
1850 * @param p_result Result of current module call.
1851 * @param mctx Current module ctx.
1852 * @param request Current request.
1853 * @return one of the RLM_MODULE_* values.
1854 */
1856{
1857 sql_redundant_ctx_t *redundant_ctx = talloc_get_type_abort(mctx->rctx, sql_redundant_ctx_t);
1858 sql_redundant_call_env_t *call_env = redundant_ctx->call_env;
1859 rlm_sql_t const *inst = redundant_ctx->inst;
1860
1861 redundant_ctx->query_vb = fr_value_box_list_pop_head(&redundant_ctx->query);
1862 if (!redundant_ctx->query_vb) RETURN_UNLANG_FAIL;
1863
1864 if ((call_env->filename.type == FR_TYPE_STRING) && (call_env->filename.vb_length > 0)) {
1865 rlm_sql_query_log(inst, call_env->filename.vb_strvalue, redundant_ctx->query_vb->vb_strvalue);
1866 }
1867
1868 MEM(redundant_ctx->query_ctx = fr_sql_query_alloc(redundant_ctx, inst, request, redundant_ctx->trunk,
1869 redundant_ctx->query_vb->vb_strvalue, SQL_QUERY_OTHER));
1870
1871 unlang_module_yield(request, mod_sql_redundant_query_resume, NULL, 0, redundant_ctx);
1872 return unlang_function_push_with_result(/* discard, mod_sql_redundant_query_resume uses query_ctx->rcode*/ NULL,
1873 request,
1874 inst->query,
1875 NULL,
1876 NULL,
1878 redundant_ctx->query_ctx);
1879}
1880
1881/** Generic module call for failing between a bunch of queries.
1882 *
1883 * Used for `accounting` and `send` module calls
1884 *
1885 */
1886static unlang_action_t CC_HINT(nonnull) mod_sql_redundant(unlang_result_t *p_result, module_ctx_t const *mctx, request_t *request)
1887{
1889 rlm_sql_thread_t *thread = talloc_get_type_abort(mctx->thread, rlm_sql_thread_t);
1890 sql_redundant_call_env_t *call_env = talloc_get_type_abort(mctx->env_data, sql_redundant_call_env_t);
1891 sql_redundant_ctx_t *redundant_ctx;
1892
1893 /*
1894 * No query to expand - do nothing.
1895 */
1896 if (!call_env->query) {
1897 RWARN("No query configured");
1899 }
1900
1901 MEM(redundant_ctx = talloc_zero(unlang_interpret_frame_talloc_ctx(request), sql_redundant_ctx_t));
1902 *redundant_ctx = (sql_redundant_ctx_t) {
1903 .inst = inst,
1904 .request = request,
1905 .trunk = thread->trunk,
1906 .call_env = call_env,
1907 .query_no = 0
1908 };
1909 talloc_set_destructor(redundant_ctx, sql_redundant_ctx_free);
1910
1911 sql_set_user(inst, request, &call_env->user);
1912
1913 fr_value_box_list_init(&redundant_ctx->query);
1914
1915 return unlang_module_yield_to_tmpl(request, &redundant_ctx->query, request, *call_env->query,
1916 NULL, mod_sql_redundant_resume, NULL, 0, redundant_ctx);
1917}
1918
1919static int logfile_call_env_parse(TALLOC_CTX *ctx, call_env_parsed_head_t *out, tmpl_rules_t const *t_rules,
1920 CONF_ITEM *ci,
1921 call_env_ctx_t const *cec, UNUSED call_env_parser_t const *rule)
1922{
1923 CONF_SECTION const *subcs = NULL, *subsubcs = NULL;
1924 CONF_PAIR const *to_parse = NULL;
1925 tmpl_t *parsed_tmpl;
1926 call_env_parsed_t *parsed_env;
1927 tmpl_rules_t our_rules;
1928 char *section2, *p;
1929
1931
1932 /*
1933 * The call env subsection which calls this has CF_IDENT_ANY as its name
1934 * which results in finding the first child section of the module config.
1935 * We actually want the whole module config - so go to the parent.
1936 */
1937 ci = cf_parent(ci);
1938
1939 /*
1940 * Find the instance of "logfile" to parse
1941 *
1942 * If the module call is from `accounting Start` then first is
1943 * <module> { accounting { start { logfile } } }
1944 * then
1945 * <module> { accounting { logfile } }
1946 * falling back to
1947 * <module> { logfile }
1948 */
1950 if (subcs) {
1951 if (cec->asked->name2) {
1952 section2 = talloc_strdup(NULL, cec->asked->name2);
1953 p = section2;
1954 while (*p != '\0') {
1955 *(p) = tolower((uint8_t)*p);
1956 p++;
1957 }
1958 subsubcs = cf_section_find(subcs, section2, CF_IDENT_ANY);
1959 talloc_free(section2);
1960 if (subsubcs) to_parse = cf_pair_find(subsubcs, "logfile");
1961 }
1962 if (!to_parse) to_parse = cf_pair_find(subcs, "logfile");
1963 }
1964
1965 if (!to_parse) to_parse = cf_pair_find(cf_item_to_section(ci), "logfile");
1966
1967 if (!to_parse) return 0;
1968
1969 /*
1970 * Use filename safety escape functions
1971 */
1972 our_rules = *t_rules;
1976 .always_escape = false,
1977 };
1979 our_rules.literals_safe_for = our_rules.escape.box_escape.safe_for;
1980
1981 MEM(parsed_env = call_env_parsed_add(ctx, out,
1983
1984 if (tmpl_afrom_substr(parsed_env, &parsed_tmpl,
1985 &FR_SBUFF_IN(cf_pair_value(to_parse), talloc_strlen(cf_pair_value(to_parse))),
1987 &our_rules) < 0) {
1988 error:
1989 call_env_parsed_free(out, parsed_env);
1990 return -1;
1991 }
1992 if (tmpl_needs_resolving(parsed_tmpl) &&
1993 (tmpl_resolve(parsed_tmpl, &(tmpl_res_rules_t){ .dict_def = our_rules.attr.dict_def }) < 0)) goto error;
1994
1995 call_env_parsed_set_tmpl(parsed_env, parsed_tmpl);
1996
1997 return 0;
1998}
1999
2000static int query_call_env_parse(TALLOC_CTX *ctx, call_env_parsed_head_t *out, tmpl_rules_t const *t_rules,
2001 CONF_ITEM *ci,
2002 call_env_ctx_t const *cec, UNUSED call_env_parser_t const *rule)
2003{
2005 CONF_SECTION const *subcs = NULL;
2006 CONF_PAIR const *to_parse = NULL;
2007 tmpl_t *parsed_tmpl;
2008 call_env_parsed_t *parsed_env;
2009 tmpl_rules_t our_rules;
2010 char *section2, *p;
2011 ssize_t count, multi_index = 0;
2012 fr_slen_t slen;
2013
2015
2016 /*
2017 * Find the instance(s) of "query" to parse
2018 *
2019 * If the module call is from `accounting Start` then it should be
2020 * <module> { accounting { start { query } } }
2021 */
2022 section2 = talloc_strdup(NULL, section_name_str(cec->asked->name2));
2023 p = section2;
2024 while (*p != '\0') {
2025 *(p) = tolower((uint8_t)*p);
2026 p++;
2027 }
2028 subcs = cf_section_find(cf_item_to_section(ci), section2, CF_IDENT_ANY);
2029 if (!subcs) {
2030 no_query:
2031 cf_log_warn(ci, "No query found for \"%s.%s\", this query will be disabled",
2032 section_name_str(cec->asked->name1), section2);
2033 talloc_free(section2);
2034 return 0;
2035 }
2036 count = cf_pair_count(subcs, "query");
2037 if (count == 0) goto no_query;
2038
2039 talloc_free(section2);
2040
2041 /*
2042 * Use module specific escape functions
2043 */
2044 our_rules = *t_rules;
2045 our_rules.escape = (tmpl_escape_t) {
2048 .safe_for = SQL_SAFE_FOR,
2049 .always_escape = false,
2050 },
2051 .uctx = { .func = { .uctx = inst, .alloc = sql_escape_uctx_alloc }, .type = TMPL_ESCAPE_UCTX_ALLOC_FUNC },
2052 .mode = TMPL_ESCAPE_PRE_CONCAT,
2053 };
2054 our_rules.literals_safe_for = our_rules.escape.box_escape.safe_for;
2055
2056 while ((to_parse = cf_pair_find_next(subcs, to_parse, "query"))) {
2057 MEM(parsed_env = call_env_parsed_add(ctx, out,
2061 }));
2062
2063 slen = tmpl_afrom_substr(parsed_env, &parsed_tmpl,
2064 &FR_SBUFF_IN(cf_pair_value(to_parse), talloc_strlen(cf_pair_value(to_parse))),
2066 &our_rules);
2067 if (slen <= 0) {
2068 cf_canonicalize_error(to_parse, slen, "Failed parsing query", cf_pair_value(to_parse));
2069 error:
2070 call_env_parsed_free(out, parsed_env);
2071 return -1;
2072 }
2073 if (tmpl_needs_resolving(parsed_tmpl) &&
2074 (tmpl_resolve(parsed_tmpl, &(tmpl_res_rules_t){ .dict_def = our_rules.attr.dict_def }) < 0)) {
2075 cf_log_perr(to_parse, "Failed resolving query");
2076 goto error;
2077 }
2078
2079 call_env_parsed_set_multi_index(parsed_env, count, multi_index++);
2080 call_env_parsed_set_data(parsed_env, parsed_tmpl);
2081 }
2082
2083 return 0;
2084}
2085
2086static int mod_detach(module_detach_ctx_t const *mctx)
2087{
2088 rlm_sql_t *inst = talloc_get_type_abort(mctx->mi->data, rlm_sql_t);
2089
2090 /*
2091 * We need to explicitly free all children, so if the driver
2092 * parented any memory off the instance, their destructors
2093 * run before we unload the bytecode for them.
2094 *
2095 * If we don't do this, we get a SEGV deep inside the talloc code
2096 * when it tries to call a destructor that no longer exists.
2097 */
2098 talloc_free_children(inst);
2099
2100 return 0;
2101}
2102
2103static int mod_instantiate(module_inst_ctx_t const *mctx)
2104{
2105 rlm_sql_boot_t const *boot = talloc_get_type_abort(mctx->mi->boot, rlm_sql_boot_t);
2106 rlm_sql_t *inst = talloc_get_type_abort(mctx->mi->data, rlm_sql_t);
2107 CONF_SECTION *conf = mctx->mi->conf;
2108
2109 /*
2110 * We can't modify the inst field in bootstrap, and there's no
2111 * point in making rlm_sql_boot_t available everywhere.
2112 */
2113 inst->group_da = boot->group_da;
2114 inst->query_number_da = boot->query_number_da;
2115
2116 inst->name = mctx->mi->name; /* Need this for functions in sql.c */
2117 inst->mi = mctx->mi; /* For looking up thread instance data */
2118
2119 /*
2120 * We need authorize_group_check_query or authorize_group_reply_query
2121 * if group_membership_query is set.
2122 *
2123 * Or we need group_membership_query if authorize_group_check_query or
2124 * authorize_group_reply_query is set.
2125 */
2126 if (!cf_pair_find(conf, "group_membership_query")) {
2127 if (cf_pair_find(conf, "authorize_group_check_query")) {
2128 WARN("Ignoring authorize_group_check_query as group_membership_query is not configured");
2129 }
2130
2131 if (cf_pair_find(conf, "authorize_group_reply_query")) {
2132 WARN("Ignoring authorize_group_reply_query as group_membership_query is not configured");
2133 }
2134
2135 if (!inst->config.read_groups) {
2136 WARN("Ignoring read_groups as group_membership_query is not configured");
2137 inst->config.read_groups = false;
2138 }
2139 } /* allow the group check / reply queries to be NULL */
2140
2141 /*
2142 * Cache the SQL-User-Name fr_dict_attr_t, so we can be slightly
2143 * more efficient about creating SQL-User-Name attributes.
2144 */
2145 inst->sql_user = attr_sql_user_name;
2146
2147 /*
2148 * Export these methods, too. This avoids RTDL_GLOBAL.
2149 */
2150 inst->query = rlm_sql_trunk_query;
2151 inst->select = rlm_sql_trunk_query;
2152 inst->fetch_row = rlm_sql_fetch_row;
2153 inst->query_alloc = fr_sql_query_alloc;
2154
2155 /*
2156 * Either use the module specific escape function
2157 * or our default one.
2158 */
2159 if (inst->driver->sql_escape_func) {
2160 inst->sql_escape_func = inst->driver->sql_escape_func;
2161 } else {
2162 inst->sql_escape_func = sql_escape_func;
2163 inst->sql_escape_arg = inst;
2164 }
2165 inst->box_escape = (fr_value_box_escape_t) {
2166 .func = sql_box_escape,
2167 .safe_for = SQL_SAFE_FOR,
2168 .always_escape = false,
2169 };
2170
2171 inst->ef = module_rlm_exfile_init(inst, conf, 256, fr_time_delta_from_sec(30), true, false, NULL, NULL);
2172 if (!inst->ef) {
2173 cf_log_err(conf, "Failed creating log file context");
2174 return -1;
2175 }
2176
2177 /*
2178 * Most SQL trunks can only have one running request per connection.
2179 */
2180 if (!(inst->driver->flags & RLM_SQL_MULTI_QUERY_CONN)) {
2181 inst->config.trunk_conf.target_req_per_conn = 1;
2182 inst->config.trunk_conf.max_req_per_conn = 1;
2183 }
2184 if (!inst->driver->trunk_io_funcs.connection_notify) {
2185 inst->config.trunk_conf.always_writable = true;
2186 }
2187
2188 /*
2189 * Instantiate the driver module
2190 */
2191 if (unlikely(module_instantiate(inst->driver_submodule) < 0)) {
2192 cf_log_err(conf, "Failed instantiating driver module");
2193 return -1;
2194 }
2195
2196 if (!inst->config.trunk_conf.conn_triggers) return 0;
2197
2198 MEM(inst->trigger_args = fr_pair_list_alloc(inst));
2199 return module_trigger_args_build(inst->trigger_args, inst->trigger_args, cf_section_find(conf, "pool", NULL),
2201 .module = inst->mi->module->name,
2202 .name = inst->name,
2203 .server = inst->config.sql_server,
2204 .port = inst->config.sql_port
2205 });
2206}
2207
2208static int mod_bootstrap(module_inst_ctx_t const *mctx)
2209{
2210 rlm_sql_boot_t *boot = talloc_get_type_abort(mctx->mi->boot, rlm_sql_boot_t);
2211 rlm_sql_t const *inst = talloc_get_type_abort(mctx->mi->data, rlm_sql_t);
2212 CONF_SECTION *conf = mctx->mi->conf;
2213 xlat_t *xlat;
2214 xlat_arg_parser_t *sql_xlat_arg;
2216
2217 /*
2218 * Register the group comparison attribute
2219 */
2220 if (cf_pair_find(conf, "group_membership_query")) {
2221 char const *group_attribute;
2222 char buffer[256];
2223
2224 if (inst->config.group_attribute) {
2225 group_attribute = inst->config.group_attribute;
2226 } else if (cf_section_name2(conf)) {
2227 snprintf(buffer, sizeof(buffer), "%s-SQL-Group", mctx->mi->name);
2228 group_attribute = buffer;
2229 } else {
2230 group_attribute = "SQL-Group";
2231 }
2232
2233 boot->group_da = fr_dict_attr_by_name(NULL, fr_dict_root(dict_freeradius), group_attribute);
2234 if (!boot->group_da) {
2236 cf_log_perr(conf, "Failed defining group attribute");
2237 return -1;
2238 }
2239
2240 boot->group_da = fr_dict_attr_search_by_qualified_oid(NULL, dict_freeradius, group_attribute,
2241 false, false);
2242 if (!boot->group_da) {
2243 cf_log_perr(conf, "Failed resolving group attribute");
2244 return -1;
2245 }
2246 }
2247
2248 /*
2249 * Define the new %sql.group(name) xlat. The
2250 * register function automatically adds the
2251 * module instance name as a prefix.
2252 */
2253 xlat = module_rlm_xlat_register(boot, mctx, "group", sql_group_xlat, FR_TYPE_BOOL);
2254 if (!xlat) {
2255 cf_log_perr(conf, "Failed registering %s expansion", group_attribute);
2256 return -1;
2257 }
2259
2260 /*
2261 * The xlat escape function needs access to inst - so
2262 * argument parser details need to be defined here
2263 */
2264 sql_xlat_arg = talloc_zero_array(xlat, xlat_arg_parser_t, 2);
2265 sql_xlat_arg[0] = (xlat_arg_parser_t){
2267 .required = true,
2268 .concat = true
2269 };
2271
2272 xlat_func_args_set(xlat, sql_xlat_arg);
2273 }
2274
2275 /*
2276 * If we need to record which query from a redundant set succeeds, find / create the attribute to use.
2277 */
2278 if (inst->config.query_number_attribute) {
2280 inst->config.query_number_attribute);
2281 if (!boot->query_number_da) {
2283 inst->config.query_number_attribute, FR_TYPE_UINT32, NULL) < 0) {
2284 ERROR("Failed defining query number attribute \"%s\"", inst->config.query_number_attribute);
2285 return -1;
2286 }
2287
2289 inst->config.query_number_attribute,
2290 false, false);
2291 if (!boot->query_number_da) {
2292 ERROR("Failed resolving query number attribute \"%s\"", inst->config.query_number_attribute);
2293 return -1;
2294 }
2295 } else {
2296 if (boot->query_number_da->type != FR_TYPE_UINT32) {
2297 ERROR("Query number attribute \"%s\" is type \"%s\", needs to be uint32",
2298 inst->config.query_number_attribute, fr_type_to_str(boot->query_number_da->type));
2299 return -1;
2300 }
2301 }
2302 }
2303
2304 /*
2305 * Register the SQL xlat function
2306 */
2307 xlat = module_rlm_xlat_register(boot, mctx, NULL, sql_xlat, FR_TYPE_VOID); /* Returns an integer sometimes */
2308 if (!xlat) {
2309 cf_log_perr(conf, "Failed registering %s expansion", mctx->mi->name);
2310 return -1;
2311 }
2313
2314 /*
2315 * The xlat escape function needs access to inst - so
2316 * argument parser details need to be defined here.
2317 * Parented off the module instance "boot" so it can be shared
2318 * between three xlats.
2319 */
2320 MEM(sql_xlat_arg = talloc_zero_array(boot, xlat_arg_parser_t, 2));
2321 MEM(uctx = talloc_zero(sql_xlat_arg, rlm_sql_escape_uctx_t));
2322 *uctx = (rlm_sql_escape_uctx_t){ .sql = inst };
2323 sql_xlat_arg[0] = (xlat_arg_parser_t){
2325 .required = true,
2326 .concat = true,
2327 .func = sql_box_escape,
2328 .safe_for = SQL_SAFE_FOR,
2329 .uctx = uctx
2330 };
2332
2333 xlat_func_args_set(xlat, sql_xlat_arg);
2334
2335 /*
2336 * Register instances of the SQL xlat with pre-determined output types
2337 */
2338 if (unlikely(!(xlat = module_rlm_xlat_register(boot, mctx, "fetch", sql_fetch_xlat, FR_TYPE_VOID)))) return -1;
2340 xlat_func_args_set(xlat, sql_xlat_arg);
2341
2342 if (unlikely(!(xlat = module_rlm_xlat_register(boot, mctx, "modify", sql_modify_xlat, FR_TYPE_UINT32)))) return -1;
2344 xlat_func_args_set(xlat, sql_xlat_arg);
2345
2346 if (unlikely(!(xlat = module_rlm_xlat_register(boot, mctx, "escape", sql_escape_xlat, FR_TYPE_STRING)))) return -1;
2347 sql_xlat_arg = talloc_zero_array(xlat, xlat_arg_parser_t, 2);
2348 sql_xlat_arg[0] = (xlat_arg_parser_t){
2350 .variadic = true,
2351 .concat = true,
2352 };
2354 xlat_func_args_set(xlat, sql_xlat_arg);
2357
2358 if (unlikely(!(xlat = module_rlm_xlat_register(boot, mctx, "safe", xlat_transparent, FR_TYPE_STRING)))) return -1;
2359 sql_xlat_arg = talloc_zero_array(xlat, xlat_arg_parser_t, 2);
2360 sql_xlat_arg[0] = (xlat_arg_parser_t){
2362 .variadic = true,
2363 .concat = true
2364 };
2366 xlat_func_args_set(xlat, sql_xlat_arg);
2369
2370 /*
2371 * Register the SQL map processor function
2372 */
2373 if (inst->driver->sql_fields) map_proc_register(mctx->mi->boot, inst, mctx->mi->name, mod_map_proc, sql_map_verify, 0, SQL_SAFE_FOR);
2374
2375 return 0;
2376}
2377
2378/** Initialise thread specific data structure
2379 *
2380 */
2382{
2383 rlm_sql_thread_t *thread = talloc_get_type_abort(mctx->thread, rlm_sql_thread_t);
2384 rlm_sql_t *inst = talloc_get_type_abort(mctx->mi->data, rlm_sql_t);
2385
2386 if (inst->driver->sql_escape_arg_alloc) {
2387 thread->sql_escape_arg = inst->driver->sql_escape_arg_alloc(thread, mctx->el, inst);
2388 if (!thread->sql_escape_arg) return -1;
2389 }
2390
2391 thread->inst = inst;
2392
2393 thread->trunk = trunk_alloc(thread, mctx->el, &inst->driver->trunk_io_funcs,
2394 &inst->config.trunk_conf, inst->name, thread, false, inst->trigger_args);
2395 if (!thread->trunk) return -1;
2396
2397 return 0;
2398}
2399
2401{
2402 rlm_sql_thread_t *thread = talloc_get_type_abort(mctx->thread, rlm_sql_thread_t);
2403 rlm_sql_t *inst = talloc_get_type_abort(mctx->mi->data, rlm_sql_t);
2404
2405 if (inst->driver->sql_escape_arg_free) inst->driver->sql_escape_arg_free(thread->sql_escape_arg);
2406
2407 return 0;
2408}
2409
2410/** Custom parser for sql call env queries
2411 *
2412 * Needed as the escape function needs to reference the correct SQL driver
2413 */
2414static int sql_call_env_parse(TALLOC_CTX *ctx, void *out, tmpl_rules_t const *t_rules, CONF_ITEM *ci,
2415 call_env_ctx_t const *cec, UNUSED call_env_parser_t const *rule)
2416{
2418 tmpl_t *parsed_tmpl;
2419 CONF_PAIR const *to_parse = cf_item_to_pair(ci);
2420 tmpl_rules_t our_rules = *t_rules;
2421
2422 /*
2423 * Set the sql module instance data as the uctx for escaping
2424 * and use the same "safe_for" as the sql module.
2425 */
2428 .safe_for = SQL_SAFE_FOR,
2429 .always_escape = false,
2430 };
2431 our_rules.escape.uctx.func.uctx = inst;
2432 our_rules.literals_safe_for = SQL_SAFE_FOR;
2433
2434 if (tmpl_afrom_substr(ctx, &parsed_tmpl,
2435 &FR_SBUFF_IN(cf_pair_value(to_parse), talloc_strlen(cf_pair_value(to_parse))),
2437 &our_rules) < 0) return -1;
2438 *(void **)out = parsed_tmpl;
2439 return 0;
2440}
2441
2442#define QUERY_ESCAPE .pair.escape = { \
2443 .mode = TMPL_ESCAPE_PRE_CONCAT, \
2444 .uctx = { .func = { .alloc = sql_escape_uctx_alloc }, .type = TMPL_ESCAPE_UCTX_ALLOC_FUNC }, \
2445}, .pair.func = sql_call_env_parse
2446
2459
2460/* globally exported name */
2462 .common = {
2463 .magic = MODULE_MAGIC_INIT,
2464 .name = "sql",
2465 .boot_size = sizeof(rlm_sql_boot_t),
2466 .boot_type = "rlm_sql_boot_t",
2467 .inst_size = sizeof(rlm_sql_t),
2468 .config = module_config,
2469 .bootstrap = mod_bootstrap,
2470 .instantiate = mod_instantiate,
2471 .detach = mod_detach,
2472 .thread_inst_size = sizeof(rlm_sql_thread_t),
2475 },
2476 .method_group = {
2477 .bindings = (module_method_binding_t[]){
2478 /*
2479 * Hack to support old configurations
2480 */
2481 { .section = SECTION_NAME("accounting", CF_IDENT_ANY), .method = mod_sql_redundant, .method_env = &accounting_method_env },
2482 { .section = SECTION_NAME("authorize", CF_IDENT_ANY), .method = mod_authorize, .method_env = &authorize_method_env },
2483
2484 { .section = SECTION_NAME("recv", CF_IDENT_ANY), .method = mod_authorize, .method_env = &authorize_method_env },
2485 { .section = SECTION_NAME("send", CF_IDENT_ANY), .method = mod_sql_redundant, .method_env = &send_method_env },
2487 }
2488 }
2489};
unlang_action_t
Returned by unlang_op_t calls, determine the next action of the interpreter.
Definition action.h:35
@ UNLANG_ACTION_PUSHED_CHILD
unlang_t pushed a new child onto the stack, execute it instead of continuing.
Definition action.h:39
@ UNLANG_ACTION_FAIL
Encountered an unexpected error.
Definition action.h:36
@ UNLANG_ACTION_CALCULATE_RESULT
Calculate a new section rlm_rcode_t value.
Definition action.h:37
@ UNLANG_ACTION_YIELD
Temporarily pause execution until an event occurs.
Definition action.h:41
static int const char char buffer[256]
Definition acutest.h:576
#define _Thread_local
Definition atexit.h:213
#define fr_atexit_thread_local(_name, _free, _uctx)
Definition atexit.h:224
#define RCSID(id)
Definition build.h:560
#define FALL_THROUGH
clang 10 doesn't recognised the FALL-THROUGH comment anymore
Definition build.h:391
#define unlikely(_x)
Definition build.h:455
#define UNUSED
Definition build.h:384
void call_env_parsed_free(call_env_parsed_head_t *parsed, call_env_parsed_t *ptr)
Remove a call_env_parsed_t from the list of parsed call envs.
Definition call_env.c:776
call_env_parsed_t * call_env_parsed_add(TALLOC_CTX *ctx, call_env_parsed_head_t *head, call_env_parser_t const *rule)
Allocate a new call_env_parsed_t structure and add it to the list of parsed call envs.
Definition call_env.c:689
void call_env_parsed_set_multi_index(call_env_parsed_t *parsed, size_t count, size_t index)
Assign a count and index to a call_env_parsed_t.
Definition call_env.c:761
void call_env_parsed_set_data(call_env_parsed_t *parsed, void const *data)
Assign data to a call_env_parsed_t.
Definition call_env.c:746
void call_env_parsed_set_tmpl(call_env_parsed_t *parsed, tmpl_t const *tmpl)
Assign a tmpl to a call_env_parsed_t.
Definition call_env.c:718
#define CALL_ENV_TERMINATOR
Definition call_env.h:236
call_env_ctx_type_t type
Type of callenv ctx.
Definition call_env.h:227
@ CALL_ENV_CTX_TYPE_MODULE
The callenv is registered to a module method.
Definition call_env.h:222
#define FR_CALL_ENV_METHOD_OUT(_inst)
Helper macro for populating the size/type fields of a call_env_method_t from the output structure typ...
Definition call_env.h:240
call_env_parser_t const * env
Parsing rules for call method env.
Definition call_env.h:247
section_name_t const * asked
The actual name1/name2 that resolved to a module_method_binding_t.
Definition call_env.h:232
@ CALL_ENV_FLAG_CONCAT
If the tmpl produced multiple boxes they should be concatenated.
Definition call_env.h:76
@ CALL_ENV_FLAG_SUBSECTION
This is a subsection.
Definition call_env.h:87
@ CALL_ENV_FLAG_PARSE_ONLY
The result of parsing will not be evaluated at runtime.
Definition call_env.h:85
@ CALL_ENV_FLAG_MULTI
Multiple instances of the conf pairs are allowed.
Definition call_env.h:78
@ CALL_ENV_FLAG_NULLABLE
Tmpl expansions are allowed to produce no output.
Definition call_env.h:80
module_instance_t const * mi
Module instance that the callenv is registered to.
Definition call_env.h:229
#define FR_CALL_ENV_SUBSECTION_FUNC(_name, _name2, _flags, _func)
Specify a call_env_parser_t which parses a subsection using a callback function.
Definition call_env.h:412
#define FR_CALL_ENV_OFFSET(_name, _cast_type, _flags, _struct, _field)
Specify a call_env_parser_t which writes out runtime results to the specified field.
Definition call_env.h:340
#define FR_CALL_ENV_PARSE_ONLY_OFFSET(_name, _cast_type, _flags, _struct, _parse_field)
Specify a call_env_parser_t which writes out the result of the parsing phase to the field specified.
Definition call_env.h:389
Per method call config.
Definition call_env.h:180
#define CONF_PARSER_TERMINATOR
Definition cf_parse.h:673
cf_parse_t func
Override default parsing behaviour for the specified type with a custom parsing function.
Definition cf_parse.h:627
#define FR_CONF_OFFSET(_name, _struct, _field)
conf_parser_t which parses a single CONF_PAIR, writing the result to a field in a struct
Definition cf_parse.h:280
#define FR_CONF_OFFSET_FLAGS(_name, _flags, _struct, _field)
conf_parser_t which parses a single CONF_PAIR, writing the result to a field in a struct
Definition cf_parse.h:268
#define FR_CONF_OFFSET_SUBSECTION(_name, _flags, _struct, _field, _subcs)
conf_parser_t which populates a sub-struct using a CONF_SECTION
Definition cf_parse.h:309
@ CONF_FLAG_SECRET
Only print value if debug level >= 3.
Definition cf_parse.h:437
@ CONF_FLAG_HIDDEN
Used by scripts to omit items from the generated documentation.
Definition cf_parse.h:459
#define FR_CONF_OFFSET_TYPE_FLAGS(_name, _type, _flags, _struct, _field)
conf_parser_t which parses a single CONF_PAIR, writing the result to a field in a struct
Definition cf_parse.h:238
Defines a CONF_PAIR to C data type mapping.
Definition cf_parse.h:610
Common header for all CONF_* types.
Definition cf_priv.h:54
Configuration AVP similar to a fr_pair_t.
Definition cf_priv.h:77
A section grouping multiple CONF_PAIR.
Definition cf_priv.h:106
CONF_PAIR * cf_pair_find_next(CONF_SECTION const *cs, CONF_PAIR const *prev, char const *attr)
Find a pair with a name matching attr, after specified pair.
Definition cf_util.c:1612
unsigned int cf_pair_count(CONF_SECTION const *cs, char const *attr)
Count the number of times an attribute occurs in a parent section.
Definition cf_util.c:1679
char const * cf_section_name2(CONF_SECTION const *cs)
Return the second identifier of a CONF_SECTION.
Definition cf_util.c:1363
CONF_SECTION * cf_section_find(CONF_SECTION const *cs, char const *name1, char const *name2)
Find a CONF_SECTION with name1 and optionally name2.
Definition cf_util.c:1205
CONF_SECTION * cf_item_to_section(CONF_ITEM const *ci)
Cast a CONF_ITEM to a CONF_SECTION.
Definition cf_util.c:696
CONF_PAIR * cf_pair_find(CONF_SECTION const *cs, char const *attr)
Search for a CONF_PAIR with a specific name.
Definition cf_util.c:1598
fr_token_t cf_pair_value_quote(CONF_PAIR const *pair)
Return the value (rhs) quoting of a pair.
Definition cf_util.c:1760
CONF_PAIR * cf_item_to_pair(CONF_ITEM const *ci)
Cast a CONF_ITEM to a CONF_PAIR.
Definition cf_util.c:676
char const * cf_pair_value(CONF_PAIR const *pair)
Return the value of a CONF_PAIR.
Definition cf_util.c:1716
#define cf_log_err(_cf, _fmt,...)
Definition cf_util.h:343
#define cf_parent(_cf)
Definition cf_util.h:118
#define cf_canonicalize_error(_ci, _slen, _msg, _str)
Definition cf_util.h:421
#define cf_log_perr(_cf, _fmt,...)
Definition cf_util.h:350
#define cf_log_warn(_cf, _fmt,...)
Definition cf_util.h:344
#define CF_IDENT_ANY
Definition cf_util.h:80
static int fr_dcursor_append(fr_dcursor_t *cursor, void *v)
Insert a single item at the end of the list.
Definition dcursor.h:406
#define fr_assert_msg(_x, _msg,...)
Calls panic_action ifndef NDEBUG, else logs error and causes the server to exit immediately with code...
Definition debug.h:248
#define MEM(x)
Definition debug.h:38
#define ERROR(fmt,...)
Definition dhcpclient.c:40
fr_dict_attr_t const * fr_dict_attr_search_by_qualified_oid(fr_dict_attr_err_t *err, fr_dict_t const *dict_def, char const *attr, bool internal, bool foreign))
Locate a qualified fr_dict_attr_t by its name and a dictionary qualifier.
Definition dict_util.c:3420
int fr_dict_attr_add_name_only(fr_dict_t *dict, fr_dict_attr_t const *parent, char const *name, fr_type_t type, fr_dict_attr_flags_t const *flags))
Add an attribute to the dictionary.
Definition dict_util.c:2062
fr_dict_t * fr_dict_unconst(fr_dict_t const *dict)
Coerce to non-const.
Definition dict_util.c:5040
fr_dict_attr_t const * fr_dict_attr_by_name(fr_dict_attr_err_t *err, fr_dict_attr_t const *parent, char const *attr))
Locate a fr_dict_attr_t by its name.
Definition dict_util.c:3603
fr_dict_attr_t const * fr_dict_root(fr_dict_t const *dict)
Return the root attribute of a dictionary.
Definition dict_util.c:2720
fr_dict_attr_t const ** out
Where to write a pointer to the resolved fr_dict_attr_t.
Definition dict.h:316
fr_dict_t const ** out
Where to write a pointer to the loaded/resolved fr_dict_t.
Definition dict.h:329
char const * fr_dict_enum_name_by_value(fr_dict_attr_t const *da, fr_value_box_t const *value)
Lookup the name of an enum value in a fr_dict_attr_t.
Definition dict_util.c:3768
#define DICT_AUTOLOAD_TERMINATOR
Definition dict.h:335
static fr_slen_t in
Definition dict.h:904
Specifies an attribute which must be present for the module to function.
Definition dict.h:315
Specifies a dictionary which must be loaded/loadable for the module to function.
Definition dict.h:328
Test enumeration values.
Definition dict_test.h:92
#define MODULE_MAGIC_INIT
Stop people using different module/library/server versions together.
Definition dl_module.h:63
#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
static xlat_action_t sql_escape_xlat(UNUSED TALLOC_CTX *ctx, fr_dcursor_t *out, xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *in)
Escape a value to make it SQL safe.
Definition rlm_sql.c:421
static xlat_action_t sql_group_xlat(UNUSED TALLOC_CTX *ctx, UNUSED fr_dcursor_t *out, xlat_ctx_t const *xctx, request_t *request, UNUSED fr_value_box_list_t *in)
Check if the user is a member of a particular group.
Definition rlm_sql.c:1188
static xlat_action_t sql_modify_xlat(TALLOC_CTX *ctx, fr_dcursor_t *out, xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *in)
Execute an arbitrary SQL query, returning the number of rows affected.
Definition rlm_sql.c:646
static xlat_action_t sql_fetch_xlat(UNUSED TALLOC_CTX *ctx, UNUSED fr_dcursor_t *out, xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *in)
Execute an arbitrary SQL query, expecting results to be returned.
Definition rlm_sql.c:612
static xlat_action_t sql_xlat(TALLOC_CTX *ctx, fr_dcursor_t *out, xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *in)
Execute an arbitrary SQL query.
Definition rlm_sql.c:548
talloc_free(hp)
unlang_result_t * unlang_interpret_result(request_t *request)
Get the last instruction result OR the last frame that was popped.
Definition interpret.c:1953
TALLOC_CTX * unlang_interpret_frame_talloc_ctx(request_t *request)
Get a talloc_ctx which is valid only for this frame.
Definition interpret.c:2053
#define UNLANG_SUB_FRAME
Definition interpret.h:37
rlm_rcode_t rcode
The current rcode, from executing the instruction or merging the result from a frame.
Definition interpret.h:140
#define REXDENT()
Exdent (unindent) R* messages by one level.
Definition log.h:460
#define RWDEBUG(fmt,...)
Definition log.h:378
#define RDEBUG_ENABLED3
True if request debug level 1-3 messages are enabled.
Definition log.h:352
#define RDEBUG3(fmt,...)
Definition log.h:360
#define RWARN(fmt,...)
Definition log.h:314
#define RERROR(fmt,...)
Definition log.h:315
#define RPERROR(fmt,...)
Definition log.h:319
#define RPEDEBUG(fmt,...)
Definition log.h:393
#define RINDENT()
Indent R* messages by one level.
Definition log.h:447
int map_to_request(request_t *request, map_t const *map, radius_map_getvalue_t func, void *ctx)
Convert map_t to fr_pair_t (s) and add them to a request_t.
Definition map.c:1832
int rad_filename_box_make_safe(fr_value_box_t *vb, UNUSED void *uxtc)
Definition util.c:131
unlang_action_t unlang_map_yield(request_t *request, map_proc_func_t resume, unlang_map_signal_t signal, fr_signal_t sigmask, void *rctx)
Yield a request back to the interpreter from within a module.
Definition map.c:109
static TALLOC_CTX * map_ctx
Definition map_builtin.c:32
int map_proc_register(TALLOC_CTX *ctx, void const *mod_inst, char const *name, map_proc_func_t evaluate, map_proc_instantiate_t instantiate, size_t inst_size, fr_value_box_safe_for_t literals_safe_for)
Register a map processor.
Definition map_proc.c:127
void * rctx
Resume ctx that a module previously set.
Definition map_proc.h:53
void const * moi
Map module instance.
Definition map_proc.h:54
Temporary structure to hold arguments for map calls.
Definition map_proc.h:52
@ FR_TYPE_ETHERNET
48 Bit Mac-Address.
@ FR_TYPE_STRING
String of printable characters.
@ FR_TYPE_UINT32
32 Bit unsigned integer.
@ FR_TYPE_VOID
User data.
@ FR_TYPE_BOOL
A truth value.
unsigned int uint32_t
long int ssize_t
unsigned char uint8_t
ssize_t fr_slen_t
int strncasecmp(char *s1, char *s2, int n)
Definition missing.c:35
int strcasecmp(char *s1, char *s2)
Definition missing.c:65
void * env_data
Per call environment data.
Definition module_ctx.h:44
module_instance_t const * mi
Instance of the module being instantiated.
Definition module_ctx.h:42
void * thread
Thread specific instance data.
Definition module_ctx.h:43
void * rctx
Resume ctx that a module previously set.
Definition module_ctx.h:45
fr_event_list_t * el
Event list to register any IO handlers and timers against.
Definition module_ctx.h:68
module_instance_t * mi
Module instance to detach.
Definition module_ctx.h:57
void * thread
Thread instance data.
Definition module_ctx.h:67
module_instance_t const * mi
Instance of the module being instantiated.
Definition module_ctx.h:64
module_instance_t * mi
Instance of the module being instantiated.
Definition module_ctx.h:51
Temporary structure to hold arguments for module calls.
Definition module_ctx.h:41
Temporary structure to hold arguments for detach calls.
Definition module_ctx.h:56
Temporary structure to hold arguments for instantiation calls.
Definition module_ctx.h:50
Temporary structure to hold arguments for thread_instantiation calls.
Definition module_ctx.h:63
exfile_t * module_rlm_exfile_init(TALLOC_CTX *ctx, CONF_SECTION *module, uint32_t max_entries, fr_time_delta_t max_idle, bool locking, bool triggers, char const *trigger_prefix, fr_pair_list_t *trigger_args)
Initialise a module specific exfile handle.
Definition module_rlm.c:105
xlat_t * module_rlm_xlat_register(TALLOC_CTX *ctx, module_inst_ctx_t const *mctx, char const *name, xlat_func_t func, fr_type_t return_type)
Definition module_rlm.c:234
int module_rlm_submodule_parse(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, conf_parser_t const *rule)
Generic conf_parser_t func for loading drivers.
Definition module_rlm.c:954
module_t common
Common fields presented by all modules.
Definition module_rlm.h:39
int fr_pair_value_strdup(fr_pair_t *vp, char const *src, bool tainted)
Copy data into an "string" data type.
Definition pair.c:2595
int fr_pair_value_from_str(fr_pair_t *vp, char const *value, size_t inlen, fr_sbuff_unescape_rules_t const *uerules, UNUSED bool tainted)
Convert string value to native attribute value.
Definition pair.c:2549
fr_pair_t * fr_pair_find_by_da(fr_pair_list_t const *list, fr_pair_t const *prev, fr_dict_attr_t const *da)
Find the first pair with a matching da.
Definition pair.c:708
fr_pair_list_t * fr_pair_list_alloc(TALLOC_CTX *ctx)
Allocate a new pair list on the heap.
Definition pair.c:120
int fr_pair_append(fr_pair_list_t *list, fr_pair_t *to_add)
Add a VP to the end of the list.
Definition pair.c:1298
fr_pair_t * fr_pair_afrom_da(TALLOC_CTX *ctx, fr_dict_attr_t const *da)
Dynamically allocate a new attribute and assign a fr_dict_attr_t.
Definition pair.c:291
int radius_legacy_map_cmp(request_t *request, map_t const *map)
Definition pairmove.c:797
int radius_legacy_map_list_apply(request_t *request, map_list_t const *list, fr_edit_list_t *el)
Definition pairmove.c:778
size_t fr_utf8_char(uint8_t const *str, ssize_t inlen)
Checks for utf-8, taken from http://www.w3.org/International/questions/qa-forms-utf-8.
Definition print.c:39
#define fr_assert(_expr)
Definition rad_assert.h:37
#define pair_update_request(_attr, _da)
#define REDEBUG(fmt,...)
#define RDEBUG2(fmt,...)
#define WARN(fmt,...)
static void thread_detach(UNUSED void *uctx)
Explicitly cleanup module/xlat resources.
Definition radiusd.c:139
static int thread_instantiate(TALLOC_CTX *ctx, fr_event_list_t *el, UNUSED void *uctx)
Create module and xlat per-thread instances.
Definition radiusd.c:127
static rs_t * conf
Definition radsniff.c:52
#define RLM_MODULE_USER_SECTION_REJECT
Rcodes that translate to a user configurable section failing overall.
Definition rcode.h:79
#define RETURN_UNLANG_INVALID
Definition rcode.h:66
#define RETURN_UNLANG_RCODE(_rcode)
Definition rcode.h:61
#define RETURN_UNLANG_NOTFOUND
Definition rcode.h:68
#define RETURN_UNLANG_FAIL
Definition rcode.h:63
#define RETURN_UNLANG_OK
Definition rcode.h:64
rlm_rcode_t
Return codes indicating the result of the module call.
Definition rcode.h:44
@ 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_NOTFOUND
User not found.
Definition rcode.h:53
@ RLM_MODULE_UPDATED
OK (pairs modified).
Definition rcode.h:55
@ RLM_MODULE_NOOP
Module succeeded without doing anything.
Definition rcode.h:54
#define RETURN_UNLANG_NOOP
Definition rcode.h:69
fr_dict_attr_t const * request_attr_request
Definition request.c:43
fr_dict_attr_t const * request_attr_reply
Definition request.c:44
void * request_data_get(request_t *request, void const *unique_ptr, int unique_int)
Get opaque data from a request.
static char const * name
fr_value_box_t user
Definition rlm_sql.c:240
rlm_sql_t const * inst
Module instance.
Definition rlm_sql.c:229
static int sql_map_verify(CONF_SECTION *cs, UNUSED void const *mod_inst, UNUSED void *proc_inst, tmpl_t const *src, UNUSED map_list_t const *maps)
Definition rlm_sql.c:707
static int mod_detach(module_detach_ctx_t const *mctx)
Definition rlm_sql.c:2086
static unlang_action_t mod_autz_group_resume(unlang_result_t *p_result, module_ctx_t const *mctx, request_t *request)
Resume function called after authorization group / profile expansion of check / reply query tmpl.
Definition rlm_sql.c:1289
fr_sql_query_t * query_ctx
Query context.
Definition rlm_sql.c:175
sql_group_ctx_t * group_ctx
Definition rlm_sql.c:1115
rlm_sql_t const * inst
Module instance.
Definition rlm_sql.c:173
sql_redundant_call_env_t * call_env
Call environment data.
Definition rlm_sql.c:232
#define QUERY_ESCAPE
Definition rlm_sql.c:2442
tmpl_t * check_query
Tmpl to expand to form authorize_check_query.
Definition rlm_sql.c:110
static sql_fall_through_t fall_through(map_list_t *maps)
Definition rlm_sql.c:294
rlm_sql_grouplist_t * group
Current group being processed.
Definition rlm_sql.c:193
fr_value_box_t filename
File name to write SQL logs to.
Definition rlm_sql.c:202
static int _sql_map_proc_get_value(TALLOC_CTX *ctx, fr_pair_list_t *out, request_t *request, map_t const *map, void *uctx)
Converts a string value into a fr_pair_t.
Definition rlm_sql.c:680
request_t * request
Request being processed.
Definition rlm_sql.c:230
size_t query_no
Current query number.
Definition rlm_sql.c:233
static const call_env_method_t authorize_method_env
Definition rlm_sql.c:2447
tmpl_t * reply_query
Tmpl to expand to form authorize_reply_query.
Definition rlm_sql.c:111
static int _sql_escape_uxtx_free(void *uctx)
Definition rlm_sql.c:267
static fr_dict_attr_t const * attr_sql_user_name
Definition rlm_sql.c:95
fr_dict_attr_t const * query_number_da
Definition rlm_sql.c:51
static unlang_action_t sql_get_grouplist_resume(unlang_result_t *p_result, request_t *request, void *uctx)
Definition rlm_sql.c:1046
#define SQL_SAFE_FOR
Definition rlm_sql.c:43
static unlang_action_t mod_authorize_resume(unlang_result_t *p_result, module_ctx_t const *mctx, request_t *request)
Resume function called after authorization check / reply tmpl expansion.
Definition rlm_sql.c:1522
static int check_map_process(request_t *request, map_list_t *check_map, map_list_t *reply_map)
Process a "check" map.
Definition rlm_sql.c:1225
#define sql_unset_user(_i, _r)
Definition rlm_sql.c:1038
static fr_dict_attr_t const * attr_fall_through
Definition rlm_sql.c:94
static int logfile_call_env_parse(TALLOC_CTX *ctx, call_env_parsed_head_t *out, tmpl_rules_t const *t_rules, CONF_ITEM *cc, call_env_ctx_t const *cec, call_env_parser_t const *rule)
static fr_dict_t const * dict_freeradius
Definition rlm_sql.c:86
static xlat_action_t sql_group_xlat_resume(UNUSED TALLOC_CTX *ctx, UNUSED fr_dcursor_t *out, xlat_ctx_t const *xctx, request_t *request, UNUSED fr_value_box_list_t *in)
Run SQL query for group membership to return list of groups.
Definition rlm_sql.c:1152
fr_sql_map_ctx_t * map_ctx
Context used for retrieving attribute value pairs as a map list.
Definition rlm_sql.c:196
static xlat_action_t sql_xlat_select_resume(TALLOC_CTX *ctx, fr_dcursor_t *out, xlat_ctx_t const *xctx, request_t *request, UNUSED fr_value_box_list_t *in)
Definition rlm_sql.c:482
static void sql_set_user(rlm_sql_t const *inst, request_t *request, fr_value_box_t *user)
Definition rlm_sql.c:1016
rlm_sql_grouplist_t * groups
List of groups retrieved.
Definition rlm_sql.c:176
fr_value_box_t filename
Definition rlm_sql.c:124
static xlat_action_t sql_group_xlat_query_resume(TALLOC_CTX *ctx, fr_dcursor_t *out, xlat_ctx_t const *xctx, UNUSED request_t *request, fr_value_box_list_t *in)
Compare list of groups returned from SQL query to xlat argument.
Definition rlm_sql.c:1122
static fr_dict_attr_t const * attr_expr_bool_enum
Definition rlm_sql.c:97
fr_value_box_t user
Expansion of the sql_user_name.
Definition rlm_sql.c:109
fr_pair_t * sql_group
Pair to update with group being processed.
Definition rlm_sql.c:194
trunk_t * trunk
Trunk connection for queries.
Definition rlm_sql.c:231
#define SQL_AUTZ_STAGE_PROFILE
Definition rlm_sql.c:168
static xlat_action_t sql_xlat_query_resume(TALLOC_CTX *ctx, fr_dcursor_t *out, xlat_ctx_t const *xctx, request_t *request, UNUSED fr_value_box_list_t *in)
Definition rlm_sql.c:441
map_list_t check_tmp
List to store check items before processing.
Definition rlm_sql.c:188
static int submodule_parse(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, conf_parser_t const *rule)
Definition rlm_sql.c:253
static int mod_bootstrap(module_inst_ctx_t const *mctx)
Definition rlm_sql.c:2208
fr_value_box_t * query
Query string used for evaluating group membership.
Definition rlm_sql.c:174
sql_autz_call_env_t * call_env
Call environment data.
Definition rlm_sql.c:187
module_rlm_t rlm_sql
Definition rlm_sql.c:2461
static unlang_action_t mod_authorize(unlang_result_t *p_result, module_ctx_t const *mctx, request_t *request)
Start of module authorize method.
Definition rlm_sql.c:1680
static unlang_action_t sql_get_grouplist(unlang_result_t *p_result, sql_group_ctx_t *group_ctx, trunk_t *trunk, request_t *request)
Definition rlm_sql.c:1086
static const call_env_method_t xlat_method_env
Definition rlm_sql.c:127
sql_autz_status_t
Status of the authorization process.
Definition rlm_sql.c:149
@ SQL_AUTZ_PROFILE_REPLY
Running profile reply query.
Definition rlm_sql.c:163
@ SQL_AUTZ_CHECK
Running user check query.
Definition rlm_sql.c:150
@ SQL_AUTZ_GROUP_MEMB_RESUME
Completed group membership query.
Definition rlm_sql.c:155
@ SQL_AUTZ_REPLY
Running user reply query.
Definition rlm_sql.c:152
@ SQL_AUTZ_GROUP_MEMB
Running group membership query.
Definition rlm_sql.c:154
@ SQL_AUTZ_PROFILE_REPLY_RESUME
Completed profile reply query.
Definition rlm_sql.c:164
@ SQL_AUTZ_PROFILE_CHECK_RESUME
Completed profile check query.
Definition rlm_sql.c:162
@ SQL_AUTZ_CHECK_RESUME
Completed user check query.
Definition rlm_sql.c:151
@ SQL_AUTZ_PROFILE_START
Starting processing user profiles.
Definition rlm_sql.c:160
@ SQL_AUTZ_GROUP_REPLY_RESUME
Completed group reply query.
Definition rlm_sql.c:159
@ SQL_AUTZ_REPLY_RESUME
Completed user reply query.
Definition rlm_sql.c:153
@ SQL_AUTZ_GROUP_CHECK
Running group check query.
Definition rlm_sql.c:156
@ SQL_AUTZ_PROFILE_CHECK
Running profile check query.
Definition rlm_sql.c:161
@ SQL_AUTZ_GROUP_REPLY
Running group reply query.
Definition rlm_sql.c:158
@ SQL_AUTZ_GROUP_CHECK_RESUME
Completed group check query.
Definition rlm_sql.c:157
rlm_sql_t const * inst
Module instance.
Definition rlm_sql.c:183
#define SQL_AUTZ_STAGE_GROUP
Definition rlm_sql.c:167
tmpl_t * group_reply_query
Tmpl to expand to form authorize_group_reply_query.
Definition rlm_sql.c:114
tmpl_t * group_check_query
Tmpl to expand to form authorize_group_check_query.
Definition rlm_sql.c:113
int num_groups
How many groups have been retrieved.
Definition rlm_sql.c:177
static int sql_autz_ctx_free(sql_autz_ctx_t *to_free)
Definition rlm_sql.c:1263
trunk_t * trunk
Trunk connection for current authorization.
Definition rlm_sql.c:186
fr_value_box_t * query_vb
Current query string.
Definition rlm_sql.c:235
fr_value_box_list_t query
Where expanded query tmpl will be written.
Definition rlm_sql.c:234
map_list_t reply_tmp
List to store reply items before processing.
Definition rlm_sql.c:189
sql_autz_status_t status
Current status of the authorization.
Definition rlm_sql.c:190
request_t * request
Request being processed.
Definition rlm_sql.c:184
fr_pair_t * profile
Current profile being processed.
Definition rlm_sql.c:195
#define MAX_SQL_FIELD_INDEX
Definition rlm_sql.c:719
fr_dict_attr_autoload_t rlm_sql_dict_attr[]
Definition rlm_sql.c:100
fr_value_box_list_t query
Definition rlm_sql.c:1114
static int mod_thread_instantiate(module_thread_inst_ctx_t const *mctx)
Initialise thread specific data structure.
Definition rlm_sql.c:2381
static void * sql_escape_uctx_alloc(UNUSED request_t *request, void const *uctx)
Definition rlm_sql.c:276
static unlang_action_t mod_sql_redundant(unlang_result_t *p_result, module_ctx_t const *mctx, request_t *request)
Generic module call for failing between a bunch of queries.
Definition rlm_sql.c:1886
fr_sql_query_t * query_ctx
Query context for current query.
Definition rlm_sql.c:236
static unlang_action_t mod_sql_redundant_query_resume(unlang_result_t *p_result, module_ctx_t const *mctx, request_t *request)
Resume function called after executing an SQL query in a redundant list of queries.
Definition rlm_sql.c:1767
rlm_rcode_t rcode
Module return code.
Definition rlm_sql.c:185
static int sql_escape_func(fr_value_box_t *vb, void *arg)
Default escape function for drivers which do not provide their own.
Definition rlm_sql.c:921
tmpl_t ** query
Array of tmpls for list of queries to run.
Definition rlm_sql.c:203
rlm_sql_grouplist_t * next
Definition rlm_sql.c:1043
static const call_env_method_t accounting_method_env
Definition rlm_sql.c:206
static int sql_redundant_ctx_free(sql_redundant_ctx_t *to_free)
Tidy up when freeing an SQL redundant context.
Definition rlm_sql.c:1750
static const call_env_method_t group_xlat_method_env
Definition rlm_sql.c:244
sql_group_ctx_t * group_ctx
Context used for retrieving user group membership.
Definition rlm_sql.c:197
tmpl_t * membership_query
Tmpl to expand to form group_membership_query.
Definition rlm_sql.c:112
static int sql_box_escape(fr_value_box_t *vb, void *uctx)
Escape a tainted VB used as an xlat argument.
Definition rlm_sql.c:328
static const conf_parser_t module_config[]
Definition rlm_sql.c:54
fr_value_box_list_t query
Where expanded query tmpls will be written.
Definition rlm_sql.c:191
bool user_found
Has the user been found anywhere?
Definition rlm_sql.c:192
static fr_dict_attr_t const * attr_user_profile
Definition rlm_sql.c:96
static const call_env_method_t send_method_env
Definition rlm_sql.c:216
fr_dict_autoload_t rlm_sql_dict[]
Definition rlm_sql.c:89
fr_dict_attr_t const * group_da
Definition rlm_sql.c:50
static int mod_thread_detach(module_thread_inst_ctx_t const *mctx)
Definition rlm_sql.c:2400
static unlang_action_t mod_map_resume(unlang_result_t *p_result, map_ctx_t const *mpctx, request_t *request, UNUSED fr_value_box_list_t *query, map_list_t const *maps)
Process the results of an SQL map query.
Definition rlm_sql.c:733
static int mod_instantiate(module_inst_ctx_t const *mctx)
Definition rlm_sql.c:2103
fr_value_box_t user
Expansion of sql_user_name.
Definition rlm_sql.c:201
static int query_call_env_parse(TALLOC_CTX *ctx, call_env_parsed_head_t *out, tmpl_rules_t const *t_rules, CONF_ITEM *cc, call_env_ctx_t const *cec, call_env_parser_t const *rule)
static unlang_action_t mod_map_proc(unlang_result_t *p_result, map_ctx_t const *mpctx, request_t *request, fr_value_box_list_t *query, UNUSED map_list_t const *maps)
Executes a SELECT query and maps the result to server attributes.
Definition rlm_sql.c:871
static int sql_call_env_parse(TALLOC_CTX *ctx, void *out, tmpl_rules_t const *t_rules, CONF_ITEM *ci, call_env_ctx_t const *cec, UNUSED call_env_parser_t const *rule)
Custom parser for sql call env queries.
Definition rlm_sql.c:2414
static unlang_action_t mod_sql_redundant_resume(unlang_result_t *p_result, module_ctx_t const *mctx, request_t *request)
Resume function called after expansion of next query in a redundant list of queries.
Definition rlm_sql.c:1855
Context for SQL authorization.
Definition rlm_sql.c:182
Context for group membership query evaluation.
Definition rlm_sql.c:172
Context for tracking redundant SQL query sets.
Definition rlm_sql.c:228
Prototypes and functions for the SQL module.
fr_sql_query_t * fr_sql_query_alloc(TALLOC_CTX *ctx, rlm_sql_t const *inst, request_t *request, trunk_t *trunk, char const *query_str, fr_sql_query_type_t type)
Allocate an sql query structure.
Definition sql.c:196
trunk_t * trunk
Trunk connection for this thread.
Definition rlm_sql.h:114
rlm_sql_t const * inst
Module instance data.
Definition rlm_sql.h:115
#define RLM_SQL_MULTI_QUERY_CONN
Can support multiple queries on a single connection.
Definition rlm_sql.h:173
fr_sql_query_type_t type
Type of query.
Definition rlm_sql.h:143
unlang_action_t sql_get_map_list(unlang_result_t *p_result, request_t *request, fr_sql_map_ctx_t *map_ctx, trunk_t *trunk)
Submit the query to get any user / group check or reply pairs.
Definition sql.c:374
unlang_action_t rlm_sql_fetch_row(unlang_result_t *p_result, request_t *request, void *uctx)
Call the driver's sql_fetch_row function.
Definition sql.c:79
void rlm_sql_query_log(rlm_sql_t const *inst, char const *filename, char const *query)
Definition sql.c:404
rlm_sql_t const * inst
Module instance for this query.
Definition rlm_sql.h:137
sql_rcode_t
Action to take at end of an SQL query.
Definition rlm_sql.h:44
@ RLM_SQL_QUERY_INVALID
Query syntax error.
Definition rlm_sql.h:45
@ RLM_SQL_ALT_QUERY
Key constraint violation, use an alternative query.
Definition rlm_sql.h:49
@ RLM_SQL_RECONNECT
Stale connection, should reconnect.
Definition rlm_sql.h:48
@ RLM_SQL_ERROR
General connection/server error.
Definition rlm_sql.h:46
@ RLM_SQL_OK
Success.
Definition rlm_sql.h:47
@ RLM_SQL_NO_MORE_ROWS
No more rows available.
Definition rlm_sql.h:50
@ SQL_QUERY_SELECT
Definition rlm_sql.h:120
@ SQL_QUERY_OTHER
Definition rlm_sql.h:121
TALLOC_CTX * ctx
To allocate map entries in.
Definition rlm_sql.h:153
void * sql_escape_arg
Thread specific argument to be passed to escape function.
Definition rlm_sql.h:116
sql_fall_through_t
Definition rlm_sql.h:53
@ FALL_THROUGH_DEFAULT
Definition rlm_sql.h:56
@ FALL_THROUGH_YES
Definition rlm_sql.h:55
char ** rlm_sql_row_t
Definition rlm_sql.h:59
void rlm_sql_print_error(rlm_sql_t const *inst, request_t *request, fr_sql_query_t *query_ctx, bool force_debug)
Retrieve any errors from the SQL driver.
Definition sql.c:122
rlm_sql_row_t row
Row data from the last query.
Definition rlm_sql.h:146
sql_rcode_t rcode
Result code.
Definition rlm_sql.h:145
rlm_sql_t const * sql
Definition rlm_sql.h:197
fr_table_num_sorted_t const sql_rcode_description_table[]
Definition sql.c:45
unlang_action_t rlm_sql_trunk_query(unlang_result_t *p_result, request_t *request, void *uctx)
Submit an SQL query using a trunk connection.
Definition sql.c:260
Context used when fetching attribute value pairs as a map list.
Definition rlm_sql.h:152
#define FR_SBUFF_IN(_start, _len_or_end)
static char const * section_name_str(char const *name)
Return a printable string for the section name.
Definition section.h:97
#define SECTION_NAME(_name1, _name2)
Define a section name consisting of a verb and a noun.
Definition section.h:39
char const * name2
Second section name. Usually a packet type like 'access-request', 'access-accept',...
Definition section.h:45
char const * name1
First section name. Usually a verb like 'recv', 'send', etc...
Definition section.h:44
char const * name
Instance name e.g. user_database.
Definition module.h:359
CONF_SECTION * conf
Module's instance configuration.
Definition module.h:353
void * data
Module's instance data.
Definition module.h:295
void * boot
Data allocated during the boostrap phase.
Definition module.h:298
void * data
Thread specific instance data.
Definition module.h:376
static module_thread_instance_t * module_thread(module_instance_t const *mi)
Retrieve module/thread specific instance for a module.
Definition module.h:515
#define MODULE_BINDING_TERMINATOR
Terminate a module binding list.
Definition module.h:152
size_t boot_size
Size of the module's bootstrap data.
Definition module.h:209
module_t * exported
Public module structure.
Definition module.h:300
Module instance data.
Definition module.h:289
Named methods exported by a module.
Definition module.h:174
#define pair_append_control(_attr, _da)
Allocate and append a fr_pair_t to the control list.
Definition pair.h:57
#define pair_delete_request(_pair_or_da)
Delete a fr_pair_t in the request list.
Definition pair.h:172
#define pair_update_control(_attr, _da)
Return or allocate a fr_pair_t in the control list.
Definition pair.h:140
tmpl_escape_t escape
How escaping should be handled during evaluation.
Definition tmpl.h:362
int tmpl_resolve(tmpl_t *vpt, tmpl_res_rules_t const *tr_rules))
Attempt to resolve functions and attributes in xlats and attribute references.
#define tmpl_value(_tmpl)
Definition tmpl.h:960
fr_value_box_safe_for_t literals_safe_for
safe_for value assigned to literal values in xlats, execs, and data.
Definition tmpl.h:360
#define tmpl_is_attr(vpt)
Definition tmpl.h:215
ssize_t tmpl_afrom_substr(TALLOC_CTX *ctx, tmpl_t **out, fr_sbuff_t *in, fr_token_t quote, fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules))
Convert an arbitrary string into a tmpl_t.
#define tmpl_is_data(vpt)
Definition tmpl.h:213
#define tmpl_expand(_out, _buff, _buff_len, _request, _vpt)
Expand a tmpl to a C type, using existing storage to hold variably sized types.
Definition tmpl.h:1077
#define tmpl_value_type(_tmpl)
Definition tmpl.h:962
tmpl_attr_rules_t attr
Rules/data for parsing attribute references.
Definition tmpl.h:346
static fr_dict_attr_t const * tmpl_attr_tail_da(tmpl_t const *vpt)
Return the last attribute reference da.
Definition tmpl.h:824
#define tmpl_needs_resolving(vpt)
Definition tmpl.h:230
Similar to tmpl_rules_t, but used to specify parameters that may change during subsequent resolution ...
Definition tmpl.h:391
Optional arguments passed to vp_tmpl functions.
Definition tmpl.h:343
#define fr_skip_whitespace(_p)
Skip whitespace ('\t', '\n', '\v', '\f', '\r', ' ')
Definition skip.h:36
PUBLIC int snprintf(char *string, size_t length, char *format, va_alist)
Definition snprintf.c:689
return count
Definition module.c:155
int module_instantiate(module_instance_t *instance)
Manually complete module setup by calling its instantiate function.
Definition module.c:1253
unlang_action_t unlang_module_yield(request_t *request, module_method_t resume, unlang_module_signal_t signal, fr_signal_t sigmask, void *rctx)
Yield a request back to the interpreter from within a module.
Definition module.c:431
unlang_action_t unlang_module_yield_to_tmpl(TALLOC_CTX *ctx, fr_value_box_list_t *out, request_t *request, tmpl_t const *vpt, unlang_tmpl_args_t *args, module_method_t resume, unlang_module_signal_t signal, fr_signal_t sigmask, void *rctx)
Push a pre-compiled tmpl and resumption state onto the stack for evaluation.
Definition module.c:216
eap_aka_sim_process_conf_t * inst
fr_pair_value_bstrdup_buffer(vp, eap_session->identity, true)
fr_pair_t * vp
eap_type_t type
The preferred EAP-Type of this instance of the EAP-SIM/AKA/AKA' state machine.
Value pair map.
Definition map.h:77
fr_token_t op
The operator that controls insertion of the dst attribute.
Definition map.h:82
tmpl_t * lhs
Typically describes the attribute to add, modify or compare.
Definition map.h:78
tmpl_t * rhs
Typically describes a literal value or a src attribute to copy or compare.
Definition map.h:79
void * sql_escape_arg
Instance specific argument to be passed to escape function.
Definition rlm_sql.h:239
fr_dict_t const * dict_def
Default dictionary to use with unqualified attribute references.
Definition tmpl.h:280
Stores an attribute, a value and various bits of other data.
Definition pair.h:68
fr_dict_attr_t const *_CONST da
Dictionary attribute defines the attribute number, vendor and type of the pair.
Definition pair.h:69
#define fr_table_str_by_value(_table, _number, _def)
Convert an integer to a string.
Definition table.h:804
#define talloc_get_type_abort_const
Definition talloc.h:117
#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
static fr_time_delta_t fr_time_delta_from_sec(int64_t sec)
Definition time.h:590
int unlang_tmpl_push(TALLOC_CTX *ctx, unlang_result_t *p_result, fr_value_box_list_t *out, request_t *request, tmpl_t const *tmpl, unlang_tmpl_args_t *args, bool top_frame)
Push a tmpl onto the stack for evaluation.
Definition tmpl.c:276
@ TMPL_ESCAPE_UCTX_ALLOC_FUNC
A new uctx of the specified size and type is allocated and pre-populated by memcpying uctx....
Definition tmpl_escape.h:35
fr_value_box_escape_t box_escape
How to escape when returned from evaluation.
Definition tmpl_escape.h:81
@ TMPL_ESCAPE_PRE_CONCAT
Pre-concatenation escaping is useful for DSLs where elements of the expansion are static,...
Definition tmpl_escape.h:61
struct tmpl_escape_t::@80 uctx
tmpl_escape_mode_t mode
Whether to apply escape function after concatenation, i.e.
Definition tmpl_escape.h:83
Escaping rules for tmpls.
Definition tmpl_escape.h:80
const bool fr_assignment_op[T_TOKEN_LAST]
Definition token.c:236
char const * fr_tokens[T_TOKEN_LAST]
Definition token.c:146
const bool fr_comparison_op[T_TOKEN_LAST]
Definition token.c:266
@ T_OP_CMP_TRUE
Definition token.h:102
@ T_OP_CMP_FALSE
Definition token.h:103
int module_trigger_args_build(TALLOC_CTX *ctx, fr_pair_list_t *list, CONF_SECTION const *cs, module_trigger_args_t *args)
Build trigger args pair list for modules.
Definition trigger.c:504
Common values used by modules when building trigger args.
Definition trigger.h:42
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
conf_parser_t const trunk_config[]
Config parser definitions to populate a trunk_conf_t.
Definition trunk.c:345
Main trunk management handle.
Definition trunk.c:219
static int expand_rhs(request_t *request, unlang_frame_state_edit_t *state, edit_map_t *current)
Definition edit.c:1192
xlat_action_t unlang_xlat_yield(request_t *request, xlat_func_t resume, xlat_func_signal_t signal, fr_signal_t sigmask, void *rctx)
Yield a request back to the interpreter from within a module.
Definition xlat.c:543
fr_type_t type
Type to cast argument to.
Definition xlat.h:145
xlat_action_t xlat_transparent(UNUSED TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *args)
#define XLAT_ARG_PARSER_TERMINATOR
Definition xlat.h:160
xlat_action_t
Definition xlat.h:37
@ XLAT_ACTION_FAIL
An xlat function failed.
Definition xlat.h:44
@ XLAT_ACTION_YIELD
An xlat function pushed a resume frame onto the stack.
Definition xlat.h:42
@ XLAT_ACTION_PUSH_UNLANG
An xlat function pushed an unlang frame onto the unlang stack.
Definition xlat.h:39
@ XLAT_ACTION_DONE
We're done evaluating this level of nesting.
Definition xlat.h:43
Definition for a single argument consumed by an xlat function.
Definition xlat.h:135
static fr_slen_t parent
Definition pair.h:860
#define fr_type_is_structural(_x)
Definition types.h:392
#define FR_TYPE_IP
Definition types.h:308
static char const * fr_type_to_str(fr_type_t type)
Return a static string containing the type name.
Definition types.h:454
#define FR_TYPE_NUMERIC
Definition types.h:306
fr_sbuff_parse_rules_t const * value_parse_rules_quoted[T_TOKEN_LAST]
Parse rules for quoted strings.
Definition value.c:611
int fr_value_box_cast_in_place(TALLOC_CTX *ctx, fr_value_box_t *vb, fr_type_t dst_type, fr_dict_attr_t const *dst_enumv)
Convert one type of fr_value_box_t to another in place.
Definition value.c:4223
void fr_value_box_clear_value(fr_value_box_t *data)
Clear/free any existing value.
Definition value.c:4358
int fr_value_box_strdup(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, char const *src, bool tainted)
Copy a nul terminated string to a fr_value_box_t.
Definition value.c:4648
void fr_value_box_strdup_shallow_replace(fr_value_box_t *vb, char const *src, ssize_t len)
Free the existing buffer (if talloced) associated with the valuebox, and replace it with a new one.
Definition value.c:4774
int fr_value_box_list_concat_in_place(TALLOC_CTX *ctx, fr_value_box_t *out, fr_value_box_list_t *list, fr_type_t type, fr_value_box_list_action_t proc_action, bool flatten, size_t max_size)
Concatenate a list of value boxes.
Definition value.c:6678
@ FR_VALUE_BOX_LIST_FREE
Definition value.h:261
#define fr_value_box_alloc(_ctx, _type, _enumv)
Allocate a value box of a specific type.
Definition value.h:669
#define fr_value_box_mark_safe_for(_box, _safe_for)
Definition value.h:1125
#define fr_box_strvalue_buffer(_val)
Definition value.h:337
#define fr_value_box_is_safe_for(_box, _safe_for)
Definition value.h:1132
static fr_sbuff_err_t char ** out
Definition value.h:1062
static fr_sbuff_err_t char size_t * len
Definition value.h:1062
fr_value_box_safe_for_t safe_for
Definition value.h:707
uintptr_t fr_value_box_safe_for_t
Escaping that's been applied to a value box.
Definition value.h:162
int nonnull(2, 5))
#define fr_value_box_alloc_null(_ctx)
Allocate a value box for later use with a value assignment function.
Definition value.h:680
fr_value_box_escape_func_t func
Definition value.h:706
static TALLOC_CTX * xlat_ctx
void * rctx
Resume context.
Definition xlat_ctx.h:54
void * env_data
Expanded call env data.
Definition xlat_ctx.h:53
module_ctx_t const * mctx
Synthesised module calling ctx.
Definition xlat_ctx.h:52
An xlat calling ctx.
Definition xlat_ctx.h:49
void xlat_func_flags_set(xlat_t *x, xlat_func_flags_t flags)
Specify flags that alter the xlat's behaviour.
Definition xlat_func.c:401
int xlat_func_args_set(xlat_t *x, xlat_arg_parser_t const args[])
Register the arguments of an xlat.
Definition xlat_func.c:374
void xlat_func_call_env_set(xlat_t *x, call_env_method_t const *env_method)
Register call environment of an xlat.
Definition xlat_func.c:391
#define xlat_func_safe_for_set(_xlat, _escaped)
Set the escaped values for output boxes.
Definition xlat_func.h:84
@ XLAT_FUNC_FLAG_PURE
Definition xlat_func.h:38