The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
rlm_sqlippool.c
Go to the documentation of this file.
1/*
2 * This program is 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: b094dd77f0577a1274cf5bfd8a0640b71227479a $
19 * @file rlm_sqlippool.c
20 * @brief Allocates an IPv4 address from pools stored in SQL.
21 *
22 * @copyright 2002 Globe.Net Communications Limited
23 * @copyright 2006 The FreeRADIUS server project
24 * @copyright 2006 Suntel Communications
25 */
26RCSID("$Id: b094dd77f0577a1274cf5bfd8a0640b71227479a $")
27
28#define LOG_PREFIX inst->name
29
30#include <rlm_sql.h>
31#include <freeradius-devel/util/debug.h>
32#include <freeradius-devel/radius/radius.h>
33#include <freeradius-devel/unlang/function.h>
34
35#include <ctype.h>
36
37/*
38 * Define a structure for our module configuration.
39 */
40typedef struct {
41 char const *name;
42 char const *sql_name;
43
44 rlm_sql_t const *sql;
46
47/** Call environment used by module alloc method
48 */
49typedef struct {
50 fr_value_box_t pool_name; //!< Name of pool address will be allocated from.
51 tmpl_t *pool_name_tmpl; //!< Tmpl used to expand pool_name
52 fr_value_box_t requested_address; //!< IP address being requested by client.
53 tmpl_t *allocated_address_attr; //!< Attribute to populate with allocated IP.
54 fr_value_box_t allocated_address; //!< Existing value for allocated IP.
55 fr_value_box_t begin; //!< SQL query to begin transaction.
56 tmpl_t *existing; //!< tmpl to expand as query for finding the existing IP.
57 tmpl_t *requested; //!< tmpl to expand as query for finding the requested IP.
58 tmpl_t *find; //!< tmpl to expand as query for finding an unused IP.
59 tmpl_t *update; //!< tmpl to expand as query for updating the found IP.
60 tmpl_t *pool_check; //!< tmpl to expand as query for checking for existence of the pool.
61 fr_value_box_t commit; //!< SQL query to commit transaction.
63
64/** Call environment used by all other module methods
65 */
66typedef struct {
67 fr_value_box_t free; //!< SQL query to clear other offered IPs. Only used in "update" method.
68 fr_value_box_t update; //!< SQL query to update an IP record.
70
71/** Current step in IP allocation state machine
72 */
73typedef enum {
74 IPPOOL_ALLOC_BEGIN_RUN, //!< Run the "begin" query
75 IPPOOL_ALLOC_EXISTING, //!< Expanding the "existing" query
76 IPPOOL_ALLOC_EXISTING_RUN, //!< Run the "existing" query
77 IPPOOL_ALLOC_REQUESTED, //!< Expanding the "requested" query
78 IPPOOL_ALLOC_REQUESTED_RUN, //!< Run the "requested" query
79 IPPOOL_ALLOC_FIND, //!< Expanding the "find" query
80 IPPOOL_ALLOC_FIND_RUN, //!< Run the "find" query
81 IPPOOL_ALLOC_NO_ADDRESS, //!< No address was found
82 IPPOOL_ALLOC_POOL_CHECK, //!< Expanding the "pool_check" query
83 IPPOOL_ALLOC_POOL_CHECK_RUN, //!< Run the "pool_check" query
84 IPPOOL_ALLOC_MAKE_PAIR, //!< Make the pair.
85 IPPOOL_ALLOC_UPDATE, //!< Expanding the "update" query
86 IPPOOL_ALLOC_UPDATE_RUN, //!< Run the "update" query
87 IPPOOL_ALLOC_COMMIT_RUN, //!< RUn the "commit" query
89
90/** Resume context for IP allocation
91 */
92typedef struct {
93 request_t *request; //!< Current request.
94 ippool_alloc_status_t status; //!< Status of the allocation.
95 ippool_alloc_call_env_t *env; //!< Call environment for the allocation.
96 trunk_t *trunk; //!< Trunk connection for queries.
97 rlm_sql_t const *sql; //!< SQL module instance.
98 fr_value_box_list_t values; //!< Where to put the expanded queries ready for execution.
99 fr_value_box_t *query; //!< Current query being run.
100 fr_sql_query_t *query_ctx; //!< Query context for allocation queries.
101 rlm_rcode_t rcode; //!< Result code to return after running "commit".
103
104/** Resume context for IP update / release
105 */
106typedef struct {
107 request_t *request; //!< Current request.
108 ippool_common_call_env_t *env; //!< Call environment for the update.
109 rlm_sql_t const *sql; //!< SQL module instance.
110 fr_sql_query_t *query_ctx; //!< Query context for allocation queries.
112
114 { FR_CONF_OFFSET("sql_module_instance", rlm_sqlippool_t, sql_name), .dflt = "sql" },
115
117};
118
119static int _sql_escape_uxtx_free(void *uctx)
120{
121 return talloc_free(uctx);
122}
123
124static void *sql_escape_uctx_alloc(UNUSED request_t *request, void const *uctx)
125{
126 static _Thread_local rlm_sql_escape_uctx_t *t_ctx;
127
128 if (unlikely(t_ctx == NULL)) {
130
131 MEM(ctx = talloc_zero(NULL, rlm_sql_escape_uctx_t));
133 }
134 t_ctx->sql = uctx;
135
136 return t_ctx;
137}
138
139/*
140 * Process the results of an SQL query expected to return a single row
141 */
142static int sqlippool_result_process(char *out, int outlen, fr_sql_query_t *query_ctx)
143{
144 rlm_rcode_t p_result;
145 int rlen, retval = 0;
146 rlm_sql_row_t row;
147 request_t *request = query_ctx->request;
148
149 *out = '\0';
150
151 query_ctx->inst->fetch_row(&p_result, NULL, query_ctx->request, query_ctx);
152 if (query_ctx->rcode < 0) {
153 REDEBUG("Failed fetching query_result");
154 goto finish;
155 }
156
157 row = query_ctx->row;
158 if (!row) {
159 RDEBUG2("SQL query did not return any results");
160 goto finish;
161 }
162
163 if (!row[0]) {
164 REDEBUG("The first column of the result was NULL");
165 goto finish;
166 }
167
168 rlen = strlen(row[0]);
169 if (rlen >= outlen) {
170 REDEBUG("The first column of the result was too long (%d)", rlen);
171 goto finish;
172 }
173
174 strcpy(out, row[0]);
175 retval = rlen;
176
177finish:
178 query_ctx->inst->driver->sql_finish_select_query(query_ctx, &query_ctx->inst->config);
179 return retval;
180}
181
182/*
183 * Do any per-module initialization that is separate to each
184 * configured instance of the module. e.g. set up connections
185 * to external databases, read configuration files, set up
186 * dictionary entries, etc.
187 *
188 * If configuration information is given in the config section
189 * that must be referenced in later calls, store a handle to it
190 * in *instance otherwise put a null pointer there.
191 */
192static int mod_instantiate(module_inst_ctx_t const *mctx)
193{
195 rlm_sqlippool_t *inst = talloc_get_type_abort(mctx->mi->data, rlm_sqlippool_t);
196 CONF_SECTION *conf = mctx->mi->conf;
197
198 inst->name = talloc_asprintf(inst, "%s - %s", mctx->mi->name, inst->sql_name);
199
200 sql = module_rlm_static_by_name(NULL, inst->sql_name);
201 if (!sql) {
202 cf_log_err(conf, "failed to find sql instance named %s",
203 inst->sql_name);
204 return -1;
205 }
206
207 inst->sql = (rlm_sql_t *) sql->data;
208
209 if (strcmp(talloc_get_name(inst->sql), "rlm_sql_t") != 0) {
210 cf_log_err(conf, "Module \"%s\" is not an instance of the rlm_sql module",
211 inst->sql_name);
212 return -1;
213 }
214
215 return 0;
216}
217
218/** Release SQL pool connections when alloc context is freed.
219 */
221{
222 if (!to_free->sql->sql_escape_arg) (void) request_data_get(to_free->request, (void *)sql_escape_uctx_alloc, 0);
223 return 0;
224}
225
226#define REPEAT_MOD_ALLOC_RESUME if (unlang_function_repeat_set(request, mod_alloc_resume) < 0) RETURN_MODULE_FAIL
227#define SUBMIT_QUERY(_query_str, _new_status, _type, _function) do { \
228 alloc_ctx->status = _new_status; \
229 REPEAT_MOD_ALLOC_RESUME; \
230 query_ctx->query_str = _query_str; \
231 query_ctx->type = _type; \
232 query_ctx->status = SQL_QUERY_PREPARED; \
233 alloc_ctx->query = query; \
234 return unlang_function_push(request, sql->_function, NULL, NULL, 0, UNLANG_SUB_FRAME, query_ctx); \
235} while (0)
236
237/** Resume function called after each IP allocation query is expanded
238 *
239 * Executes the query and, if appropriate, pushes the next tmpl for expansion
240 *
241 * Following the final (successful) query, the destination attribute is populated.
242 *
243 * @param p_result Result of IP allocation.
244 * @param priority Unused.
245 * @param request Current request.
246 * @param uctx Current allocation context.
247 * @return One of the UNLANG_ACTION_* values.
248 */
249static unlang_action_t mod_alloc_resume(rlm_rcode_t *p_result, UNUSED int *priority, request_t *request, void *uctx)
250{
251 ippool_alloc_ctx_t *alloc_ctx = talloc_get_type_abort(uctx, ippool_alloc_ctx_t);
252 ippool_alloc_call_env_t *env = alloc_ctx->env;
253 int allocation_len = 0;
254 char allocation[FR_MAX_STRING_LEN];
255 rlm_sql_t const *sql = alloc_ctx->sql;
256 fr_value_box_t *query = fr_value_box_list_pop_head(&alloc_ctx->values);
257 fr_sql_query_t *query_ctx = alloc_ctx->query_ctx;
258
259 /*
260 * If a previous async call returned one of the "failure" results just return.
261 */
262 switch (*p_result) {
265
266 default:
267 break;
268 }
269
270 switch (alloc_ctx->status) {
272 if ((env->begin.type == FR_TYPE_STRING) &&
273 env->begin.vb_length) sql->driver->sql_finish_query(query_ctx, &query_ctx->inst->config);
274
275 /*
276 * The first call of this function will always land here, whether or not a "begin" query is actually run.
277 *
278 * Having (possibly) run the "begin" query, establish which tmpl needs expanding
279 *
280 * If there is a query for finding the existing IP expand that first
281 */
282 if (env->existing) {
283 alloc_ctx->status = IPPOOL_ALLOC_EXISTING;
285 if (unlang_tmpl_push(alloc_ctx, &alloc_ctx->values, request, env->existing, NULL) < 0) {
286 error:
287 talloc_free(alloc_ctx);
289 }
291 }
292 goto expand_requested;
293
295 if (query && query->vb_length) SUBMIT_QUERY(query->vb_strvalue, IPPOOL_ALLOC_EXISTING_RUN, SQL_QUERY_SELECT, select);
296 goto expand_requested;
297
299 TALLOC_FREE(alloc_ctx->query);
300 if (query_ctx->rcode != RLM_SQL_OK) goto error;
301
302 allocation_len = sqlippool_result_process(allocation, sizeof(allocation), query_ctx);
303 if (allocation_len > 0) goto make_pair;
304
305 /*
306 * If there's a requested address and associated query, expand that
307 */
308 expand_requested:
309 if (env->requested && (env->requested_address.type != FR_TYPE_NULL)) {
310 alloc_ctx->status = IPPOOL_ALLOC_REQUESTED;
312 if (unlang_tmpl_push(alloc_ctx, &alloc_ctx->values, request, env->requested, NULL) < 0) goto error;
314 }
315 goto expand_find;
316
318 if (query && query->vb_length) SUBMIT_QUERY(query->vb_strvalue, IPPOOL_ALLOC_REQUESTED_RUN, SQL_QUERY_SELECT, select);
319
320 goto expand_find;
321
323 TALLOC_FREE(alloc_ctx->query);
324 if (query_ctx->rcode != RLM_SQL_OK) goto error;
325
326 allocation_len = sqlippool_result_process(allocation, sizeof(allocation), query_ctx);
327 if (allocation_len > 0) goto make_pair;
328
329 expand_find:
330 /*
331 * Neither "existing" nor "requested" found an address, expand "find" query
332 */
333 alloc_ctx->status = IPPOOL_ALLOC_FIND;
335 if (unlang_tmpl_push(alloc_ctx, &alloc_ctx->values, request, env->find, NULL) < 0) goto error;
337
339 SUBMIT_QUERY(query->vb_strvalue, IPPOOL_ALLOC_FIND_RUN, SQL_QUERY_SELECT, select);
340
342 TALLOC_FREE(alloc_ctx->query);
343 if (query_ctx->rcode != RLM_SQL_OK) goto error;
344
345 allocation_len = sqlippool_result_process(allocation, sizeof(allocation), query_ctx);
346
347 if (allocation_len > 0) goto make_pair;
348
349 /*
350 * Nothing found
351 */
352 if ((env->commit.type == FR_TYPE_STRING) &&
353 env->commit.vb_length) SUBMIT_QUERY(env->commit.vb_strvalue, IPPOOL_ALLOC_NO_ADDRESS, SQL_QUERY_OTHER, query);
355
357 if ((env->commit.type == FR_TYPE_STRING) &&
358 env->commit.vb_length) sql->driver->sql_finish_query(query_ctx, &query_ctx->inst->config);
359
360 /*
361 * Should we perform pool-check?
362 */
363 if (env->pool_check) {
364 alloc_ctx->status = IPPOOL_ALLOC_POOL_CHECK;
366 if (unlang_tmpl_push(alloc_ctx, &alloc_ctx->values, request, env->pool_check, NULL) < 0) goto error;
368 }
369 no_address:
370 RWDEBUG("IP address could not be allocated");
372
374 {
375 tmpl_t ip_rhs;
376 map_t ip_map;
377
378 make_pair:
379 /*
380 * See if we can create the VP from the returned data. If not,
381 * error out. If so, add it to the list.
382 */
383 ip_map = (map_t) {
385 .op = T_OP_SET,
386 .rhs = &ip_rhs
387 };
388
389 tmpl_init_shallow(&ip_rhs, TMPL_TYPE_DATA, T_BARE_WORD, "", 0, NULL);
390 fr_value_box_bstrndup_shallow(&ip_map.rhs->data.literal, NULL, allocation, allocation_len, false);
391 if (map_to_request(request, &ip_map, map_to_vp, NULL) < 0) {
392 alloc_ctx->rcode = RLM_MODULE_FAIL;
393
394 REDEBUG("Invalid IP address [%s] returned from database query.", allocation);
395 goto finish;
396 }
397
398 RDEBUG2("Allocated IP %s", allocation);
399 alloc_ctx->rcode = RLM_MODULE_UPDATED;
400
401 /*
402 * If we have an update query expand it
403 */
404 if (env->update) {
405 alloc_ctx->status = IPPOOL_ALLOC_UPDATE;
407 if (unlang_tmpl_push(alloc_ctx, &alloc_ctx->values, request, env->update, NULL) < 0) goto error;
409 }
410
411 goto finish;
412 }
413
415 /*
416 * Ok, so the allocate-find query found nothing ...
417 * Let's check if the pool exists at all
418 */
419 if (query && query->vb_length) SUBMIT_QUERY(query->vb_strvalue, IPPOOL_ALLOC_POOL_CHECK_RUN, SQL_QUERY_SELECT, select);
420 goto no_address;
421
423 TALLOC_FREE(alloc_ctx->query);
424 allocation_len = sqlippool_result_process(allocation, sizeof(allocation), query_ctx);
425
426 if (allocation_len) {
427 /*
428 * Pool exists after all... So,
429 * the failure to allocate the IP
430 * address was most likely due to
431 * the depletion of the pool. In
432 * that case, we should return
433 * NOTFOUND
434 */
435 RWDEBUG("Pool \"%pV\" appears to be full", &env->pool_name);
437 }
438
439 /*
440 * Pool doesn't exist in the table. It
441 * may be handled by some other instance of
442 * sqlippool, so we should just ignore this
443 * allocation failure and return NOOP
444 */
445 RWDEBUG("IP address could not be allocated as no pool exists with the name \"%pV\"",
446 &env->pool_name);
448
450 if (query && query->vb_length) SUBMIT_QUERY(query->vb_strvalue, IPPOOL_ALLOC_UPDATE_RUN, SQL_QUERY_OTHER, query);
451
452 goto finish;
453
455 TALLOC_FREE(alloc_ctx->query);
456 if (env->update) sql->driver->sql_finish_query(query_ctx, &query_ctx->inst->config);
457
458 finish:
459 if ((env->commit.type == FR_TYPE_STRING) &&
460 env->commit.vb_length) SUBMIT_QUERY(env->commit.vb_strvalue, IPPOOL_ALLOC_COMMIT_RUN, SQL_QUERY_OTHER, query);
461
463
465 {
466 rlm_rcode_t rcode = alloc_ctx->rcode;
467 talloc_free(alloc_ctx);
468 RETURN_MODULE_RCODE(rcode);
469 }
470 }
471
472 /*
473 * All return paths are handled within the switch statement.
474 */
475 fr_assert(0);
477}
478
479/** Initiate the allocation of an IP address from the pool.
480 *
481 * Based on configured queries and attributes which exist, determines the first
482 * query tmpl to expand.
483 *
484 * @param p_result Result of the allocation (if it fails).
485 * @param mctx Module context.
486 * @param request Current request.
487 * @return One of the UNLANG_ACTION_* values.
488 */
489static unlang_action_t CC_HINT(nonnull) mod_alloc(rlm_rcode_t *p_result, module_ctx_t const *mctx, request_t *request)
490{
491 rlm_sqlippool_t *inst = talloc_get_type_abort(mctx->mi->data, rlm_sqlippool_t);
492 ippool_alloc_call_env_t *env = talloc_get_type_abort(mctx->env_data, ippool_alloc_call_env_t);
493 rlm_sql_t const *sql = inst->sql;
494 ippool_alloc_ctx_t *alloc_ctx = NULL;
495 rlm_sql_thread_t *thread = talloc_get_type_abort(module_thread(sql->mi)->data, rlm_sql_thread_t);
496
497 /*
498 * If the allocated IP attribute already exists, do nothing
499 */
500 if (env->allocated_address.type) {
501 RDEBUG2("%s already exists (%pV)", env->allocated_address_attr->name, &env->allocated_address);
503 }
504
505 if (env->pool_name.type == FR_TYPE_NULL) {
506 RDEBUG2("No %s defined", env->pool_name_tmpl->name);
508 }
509
510 MEM(alloc_ctx = talloc(unlang_interpret_frame_talloc_ctx(request), ippool_alloc_ctx_t));
511 *alloc_ctx = (ippool_alloc_ctx_t) {
512 .env = env,
513 .trunk = thread->trunk,
514 .sql = inst->sql,
515 .request = request,
516 };
517 talloc_set_destructor(alloc_ctx, sqlippool_alloc_ctx_free);
518
519 /*
520 * Allocate a query_ctx which will be used for all queries in the allocation.
521 * Since they typically form an SQL transaction, they all need to be on the same
522 * connection, and use the same trunk request if using trunks.
523 */
524 MEM(alloc_ctx->query_ctx = sql->query_alloc(alloc_ctx, sql, request, thread->trunk, "", SQL_QUERY_OTHER));
525
526 fr_value_box_list_init(&alloc_ctx->values);
527 if (unlang_function_push(request, NULL, mod_alloc_resume, NULL, 0, UNLANG_SUB_FRAME, alloc_ctx) < 0 ) {
528 talloc_free(alloc_ctx);
530 }
531
532 if ((env->begin.type == FR_TYPE_STRING) && env->begin.vb_length) {
533 alloc_ctx->query_ctx->query_str = env->begin.vb_strvalue;
534 return unlang_function_push(request, sql->query, NULL, NULL, 0, UNLANG_SUB_FRAME, alloc_ctx->query_ctx);
535 }
536
538}
539
540/** Resume function called after mod_common "update" query has completed
541 */
542static unlang_action_t mod_common_update_resume(rlm_rcode_t *p_result, UNUSED int *priority, UNUSED request_t *request, void *uctx)
543{
544 ippool_common_ctx_t *common_ctx = talloc_get_type_abort(uctx, ippool_common_ctx_t);
545 fr_sql_query_t *query_ctx = common_ctx->query_ctx;
546 rlm_sql_t const *sql = common_ctx->sql;
547 int affected = 0;
548
549 switch (*p_result) {
552
553 default:
554 break;
555 }
556
557 affected = sql->driver->sql_affected_rows(query_ctx, &sql->config);
558
559 talloc_free(common_ctx);
560
561 if (affected > 0) RETURN_MODULE_UPDATED;
563}
564
565/** Resume function called after mod_common "free" query has completed
566 */
567static unlang_action_t mod_common_free_resume(rlm_rcode_t *p_result, UNUSED int *priority, request_t *request, void *uctx)
568{
569 ippool_common_ctx_t *common_ctx = talloc_get_type_abort(uctx, ippool_common_ctx_t);
570 fr_sql_query_t *query_ctx = common_ctx->query_ctx;
571 rlm_sql_t const *sql = common_ctx->sql;
572
573 switch (*p_result) {
576
577 default:
578 break;
579 }
580 if (common_ctx->env->update.type != FR_TYPE_STRING) RETURN_MODULE_NOOP;
581
582 sql->driver->sql_finish_query(query_ctx, &sql->config);
583
584 if (unlang_function_push(request, NULL, mod_common_update_resume, NULL, 0, UNLANG_SUB_FRAME, common_ctx) < 0) {
585 talloc_free(common_ctx);
587 }
588
589 common_ctx->query_ctx->query_str = common_ctx->env->update.vb_strvalue;
590 query_ctx->status = SQL_QUERY_PREPARED;
591 return unlang_function_push(request, sql->query, NULL, NULL, 0, UNLANG_SUB_FRAME, query_ctx);
592}
593
594/** Common function used by module methods which perform an optional "free" then "update"
595 * - update
596 * - release
597 * - bulk_release
598 * - mark
599 */
600static unlang_action_t CC_HINT(nonnull) mod_common(rlm_rcode_t *p_result, module_ctx_t const *mctx, request_t *request)
601{
602 rlm_sqlippool_t *inst = talloc_get_type_abort(mctx->mi->data, rlm_sqlippool_t);
603 ippool_common_call_env_t *env = talloc_get_type_abort(mctx->env_data, ippool_common_call_env_t);
604 rlm_sql_t const *sql = inst->sql;
605 rlm_sql_thread_t *thread = talloc_get_type_abort(module_thread(sql->mi)->data, rlm_sql_thread_t);
606 ippool_common_ctx_t *common_ctx = NULL;
607
608 if ((env->free.type != FR_TYPE_STRING) && (env->update.type != FR_TYPE_STRING)) RETURN_MODULE_NOOP;
609
610 MEM(common_ctx = talloc(unlang_interpret_frame_talloc_ctx(request), ippool_common_ctx_t));
611 *common_ctx = (ippool_common_ctx_t) {
612 .request = request,
613 .env = env,
614 .sql = sql,
615 };
616
617 MEM(common_ctx->query_ctx = sql->query_alloc(common_ctx, sql, request, thread->trunk, "", SQL_QUERY_OTHER));
618
619 /*
620 * An optional query which can be used to tidy up before updates
621 * primarily intended for multi-server setups sharing a common database
622 * allowing for tidy up of multiple offered addresses in a DHCP context.
623 */
624 if (env->free.type == FR_TYPE_STRING) {
625 common_ctx->query_ctx->query_str = env->free.vb_strvalue;
626 if (unlang_function_push(request, NULL, mod_common_free_resume, NULL, 0, UNLANG_SUB_FRAME, common_ctx) < 0) {
627 talloc_free(common_ctx);
629 }
630 return unlang_function_push(request, sql->query, NULL, NULL, 0, UNLANG_SUB_FRAME, common_ctx->query_ctx);
631 }
632
633 common_ctx->query_ctx->query_str = env->update.vb_strvalue;
634 if (unlang_function_push(request, NULL, mod_common_update_resume, NULL, 0, UNLANG_SUB_FRAME, common_ctx) < 0) {
635 talloc_free(common_ctx);
637 }
638 return unlang_function_push(request, sql->query, NULL, NULL, 0, UNLANG_SUB_FRAME, common_ctx->query_ctx);
639}
640
641/** Call SQL module box_escape_func to escape tainted values
642 */
643static int sqlippool_box_escape(fr_value_box_t *vb, void *uctx) {
644 rlm_sql_escape_uctx_t *ctx = talloc_get_type_abort(uctx, rlm_sql_escape_uctx_t);
645
646 return ctx->sql->box_escape_func(vb, uctx);
647}
648
649/** Custom parser for sqlippool call env
650 *
651 * Needed as the escape function needs to reference
652 * the correct instance of the SQL module since escaping functions
653 * are dependent on the driver used by a given module instance.
654 */
655static int call_env_parse(TALLOC_CTX *ctx, void *out, tmpl_rules_t const *t_rules, CONF_ITEM *ci,
656 call_env_ctx_t const *cec, UNUSED call_env_parser_t const *rule)
657{
660 rlm_sql_t const *sql;
661 tmpl_t *parsed_tmpl;
662 CONF_PAIR const *to_parse = cf_item_to_pair(ci);
663 tmpl_rules_t our_rules = *t_rules;
664
665 /*
666 * Lookup the sql module instance.
667 */
668 sql_inst = module_rlm_static_by_name(NULL, inst->sql_name);
669 if (!sql_inst) return -1;
670 sql = talloc_get_type_abort(sql_inst->data, rlm_sql_t);
671
672 /*
673 * Set the sql module instance data as the uctx for escaping
674 * and use the same "safe_for" as the sql module.
675 */
676 our_rules.escape.uctx.func.uctx = sql;
679
680 if (tmpl_afrom_substr(ctx, &parsed_tmpl,
681 &FR_SBUFF_IN(cf_pair_value(to_parse), talloc_array_length(cf_pair_value(to_parse)) - 1),
683 &our_rules) < 0) return -1;
684 *(void **)out = parsed_tmpl;
685 return 0;
686}
687
688#define QUERY_ESCAPE .pair.escape = { \
689 .func = sqlippool_box_escape, \
690 .mode = TMPL_ESCAPE_PRE_CONCAT, \
691 .uctx = { .func = { .alloc = sql_escape_uctx_alloc }, .type = TMPL_ESCAPE_UCTX_ALLOC_FUNC }, \
692}, .pair.func = call_env_parse
693
696 .env = (call_env_parser_t[]) {
698 ippool_alloc_call_env_t, pool_name, pool_name_tmpl),
699 .pair.dflt = "&control.IP-Pool.Name", .pair.dflt_quote = T_BARE_WORD },
701 ippool_alloc_call_env_t, requested_address) },
702 { FR_CALL_ENV_PARSE_OFFSET("allocated_address_attr", FR_TYPE_VOID,
704 ippool_alloc_call_env_t, allocated_address, allocated_address_attr) },
707 .pair.dflt = "START TRANSACTION", .pair.dflt_quote = T_SINGLE_QUOTED_STRING },
720 .pair.dflt = "COMMIT", .pair.dflt_quote = T_SINGLE_QUOTED_STRING },
722 }
723};
724
735
744
753
762
763/*
764 * The module name should be the only globally exported symbol.
765 * That is, everything else should be 'static'.
766 *
767 * If the module needs to temporarily modify it's instantiation
768 * data, the type should be changed to MODULE_TYPE_THREAD_UNSAFE.
769 * The server will then take care of ensuring that the module
770 * is single-threaded.
771 */
774 .common = {
775 .magic = MODULE_MAGIC_INIT,
776 .name = "sqlippool",
777 .inst_size = sizeof(rlm_sqlippool_t),
780 },
781 .method_group = {
782 .bindings = (module_method_binding_t[]){
783 /*
784 * RADIUS specific
785 */
786 { .section = SECTION_NAME("recv", "Access-Request"), .method = mod_alloc, .method_env = &sqlippool_alloc_method_env },
787 { .section = SECTION_NAME("accounting", "Start"), .method = mod_common, .method_env = &sqlippool_update_method_env },
788 { .section = SECTION_NAME("accounting", "Alive"), .method = mod_common, .method_env = &sqlippool_update_method_env },
789 { .section = SECTION_NAME("accounting", "Stop"), .method = mod_common, .method_env = &sqlippool_release_method_env },
790 { .section = SECTION_NAME("accounting", "Accounting-On"), .method = mod_common, .method_env = &sqlippool_bulk_release_method_env },
791 { .section = SECTION_NAME("accounting", "Accounting-Off"), .method = mod_common, .method_env = &sqlippool_bulk_release_method_env },
792
793 /*
794 * DHCPv4
795 */
796 { .section = SECTION_NAME("recv", "Discover"), .method = mod_alloc, .method_env = &sqlippool_alloc_method_env },
797 { .section = SECTION_NAME("recv", "Request"), .method = mod_common, .method_env = &sqlippool_update_method_env },
798 { .section = SECTION_NAME("recv", "Confirm"), .method = mod_common, .method_env = &sqlippool_update_method_env },
799 { .section = SECTION_NAME("recv", "Rebind"), .method = mod_common, .method_env = &sqlippool_update_method_env },
800 { .section = SECTION_NAME("recv", "Renew"), .method = mod_common, .method_env = &sqlippool_update_method_env },
801 { .section = SECTION_NAME("recv", "Release"), .method = mod_common, .method_env = &sqlippool_release_method_env },
802 { .section = SECTION_NAME("recv", "Decline"), .method = mod_common, .method_env = &sqlippool_mark_method_env },
803
804 /*
805 * Generic
806 */
807 { .section = SECTION_NAME("recv", CF_IDENT_ANY), .method = mod_common, .method_env = &sqlippool_update_method_env },
808 { .section = SECTION_NAME("send", CF_IDENT_ANY),.method = mod_alloc, .method_env = &sqlippool_alloc_method_env },
809
810 /*
811 * Named methods matching module operations
812 */
813 { .section = SECTION_NAME("allocate", NULL), .method = mod_alloc, .method_env = &sqlippool_alloc_method_env },
814 { .section = SECTION_NAME("update", NULL), .method = mod_common, .method_env = &sqlippool_update_method_env },
815 { .section = SECTION_NAME("renew", NULL), .method = mod_common, .method_env = &sqlippool_update_method_env },
816 { .section = SECTION_NAME("release", NULL), .method = mod_common, .method_env = &sqlippool_release_method_env },
817 { .section = SECTION_NAME("bulk-release", NULL), .method = mod_common, .method_env = &sqlippool_bulk_release_method_env },
818 { .section = SECTION_NAME("mark", NULL),.method = mod_common,.method_env = &sqlippool_mark_method_env },
819
821 }
822 }
823};
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_CALCULATE_RESULT
Calculate a new section rlm_rcode_t value.
Definition action.h:37
strcpy(log_entry->msg, buffer)
#define fr_atexit_thread_local(_name, _free, _uctx)
Definition atexit.h:221
#define RCSID(id)
Definition build.h:483
#define FALL_THROUGH
clang 10 doesn't recognised the FALL-THROUGH comment anymore
Definition build.h:322
#define unlikely(_x)
Definition build.h:381
#define UNUSED
Definition build.h:315
#define CALL_ENV_TERMINATOR
Definition call_env.h:231
#define FR_CALL_ENV_PARSE_OFFSET(_name, _cast_type, _flags, _struct, _field, _parse_field)
Specify a call_env_parser_t which writes out runtime results and the result of the parsing phase to t...
Definition call_env.h:360
#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:235
call_env_parser_t const * env
Parsing rules for call method env.
Definition call_env.h:242
@ CALL_ENV_FLAG_CONCAT
If the tmpl produced multiple boxes they should be concatenated.
Definition call_env.h:76
@ CALL_ENV_FLAG_ATTRIBUTE
Tmpl must contain an attribute reference.
Definition call_env.h:86
@ CALL_ENV_FLAG_PARSE_ONLY
The result of parsing will not be evaluated at runtime.
Definition call_env.h:85
@ CALL_ENV_FLAG_REQUIRED
Associated conf pair or section is required.
Definition call_env.h:75
@ 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:224
#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:335
#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:384
Per method call config.
Definition call_env.h:175
#define CONF_PARSER_TERMINATOR
Definition cf_parse.h:642
#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:268
Defines a CONF_PAIR to C data type mapping.
Definition cf_parse.h:579
Common header for all CONF_* types.
Definition cf_priv.h:49
Configuration AVP similar to a fr_pair_t.
Definition cf_priv.h:70
A section grouping multiple CONF_PAIR.
Definition cf_priv.h:101
fr_token_t cf_pair_value_quote(CONF_PAIR const *pair)
Return the value (rhs) quoting of a pair.
Definition cf_util.c:1638
CONF_PAIR * cf_item_to_pair(CONF_ITEM const *ci)
Cast a CONF_ITEM to a CONF_PAIR.
Definition cf_util.c:664
char const * cf_pair_value(CONF_PAIR const *pair)
Return the value of a CONF_PAIR.
Definition cf_util.c:1594
#define cf_log_err(_cf, _fmt,...)
Definition cf_util.h:289
#define CF_IDENT_ANY
Definition cf_util.h:78
#define MEM(x)
Definition debug.h:36
#define MODULE_MAGIC_INIT
Stop people using different module/library/server versions together.
Definition dl_module.h:63
#define unlang_function_push(_request, _func, _repeat, _signal, _sigmask, _top_frame, _uctx)
Push a generic function onto the unlang stack.
Definition function.h:111
free(array)
TALLOC_CTX * unlang_interpret_frame_talloc_ctx(request_t *request)
Get a talloc_ctx which is valid only for this frame.
Definition interpret.c:1403
#define UNLANG_SUB_FRAME
Definition interpret.h:36
#define RWDEBUG(fmt,...)
Definition log.h:361
int map_to_vp(TALLOC_CTX *ctx, fr_pair_list_t *out, request_t *request, map_t const *map, UNUSED void *uctx)
Convert a map to a fr_pair_t.
Definition map.c:1487
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:1781
talloc_free(reap)
struct map_s map_t
Definition map.h:33
@ FR_TYPE_STRING
String of printable characters.
@ FR_TYPE_NULL
Invalid (uninitialised) attribute type.
@ FR_TYPE_VOID
User data.
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
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 instantiation calls.
Definition module_ctx.h:50
module_instance_t * module_rlm_static_by_name(module_instance_t const *parent, char const *asked_name)
Definition module_rlm.c:828
module_t common
Common fields presented by all modules.
Definition module_rlm.h:39
static const conf_parser_t config[]
Definition base.c:183
#define fr_assert(_expr)
Definition rad_assert.h:38
#define REDEBUG(fmt,...)
Definition radclient.h:52
#define RDEBUG2(fmt,...)
Definition radclient.h:54
static rs_t * conf
Definition radsniff.c:53
#define RETURN_MODULE_NOOP
Definition rcode.h:62
#define RETURN_MODULE_RCODE(_rcode)
Definition rcode.h:64
#define RLM_MODULE_USER_SECTION_REJECT
Rcodes that translate to a user configurable section failing overall.
Definition rcode.h:72
#define RETURN_MODULE_FAIL
Definition rcode.h:56
#define RETURN_MODULE_UPDATED
Definition rcode.h:63
rlm_rcode_t
Return codes indicating the result of the module call.
Definition rcode.h:40
@ RLM_MODULE_FAIL
Module failed, don't reply.
Definition rcode.h:42
@ RLM_MODULE_UPDATED
OK (pairs modified).
Definition rcode.h:49
#define RETURN_MODULE_NOTFOUND
Definition rcode.h:61
void * request_data_get(request_t *request, void const *unique_ptr, int unique_int)
Get opaque data from a request.
static int instantiate(module_inst_ctx_t const *mctx)
Definition rlm_rest.c:1310
Prototypes and functions for the SQL module.
trunk_t * trunk
Trunk connection for this thread.
Definition rlm_sql.h:108
fr_sql_query_status_t status
Status of the query.
Definition rlm_sql.h:138
rlm_sql_t const * inst
Module instance for this query.
Definition rlm_sql.h:131
char const * query_str
Query string to run.
Definition rlm_sql.h:136
request_t * request
Request this query relates to.
Definition rlm_sql.h:132
@ RLM_SQL_OK
Success.
Definition rlm_sql.h:47
@ SQL_QUERY_SELECT
Definition rlm_sql.h:114
@ SQL_QUERY_OTHER
Definition rlm_sql.h:115
char ** rlm_sql_row_t
Definition rlm_sql.h:59
rlm_sql_row_t row
Row data from the last query.
Definition rlm_sql.h:140
sql_rcode_t rcode
Result code.
Definition rlm_sql.h:139
rlm_sql_t const * sql
Definition rlm_sql.h:190
@ SQL_QUERY_PREPARED
Ready to submit.
Definition rlm_sql.h:122
fr_value_box_t requested_address
IP address being requested by client.
#define REPEAT_MOD_ALLOC_RESUME
tmpl_t * pool_check
tmpl to expand as query for checking for existence of the pool.
static unlang_action_t mod_common_free_resume(rlm_rcode_t *p_result, UNUSED int *priority, request_t *request, void *uctx)
Resume function called after mod_common "free" query has completed.
#define QUERY_ESCAPE
static const call_env_method_t sqlippool_bulk_release_method_env
static int _sql_escape_uxtx_free(void *uctx)
fr_value_box_t allocated_address
Existing value for allocated IP.
rlm_sql_t const * sql
fr_sql_query_t * query_ctx
Query context for allocation queries.
fr_value_box_t free
SQL query to clear other offered IPs. Only used in "update" method.
fr_value_box_list_t values
Where to put the expanded queries ready for execution.
static int 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 sqlippool call env.
static const call_env_method_t sqlippool_release_method_env
rlm_rcode_t rcode
Result code to return after running "commit".
trunk_t * trunk
Trunk connection for queries.
static const call_env_method_t sqlippool_update_method_env
rlm_sql_t const * sql
SQL module instance.
static int sqlippool_result_process(char *out, int outlen, fr_sql_query_t *query_ctx)
static unlang_action_t mod_alloc(rlm_rcode_t *p_result, module_ctx_t const *mctx, request_t *request)
Initiate the allocation of an IP address from the pool.
char const * sql_name
request_t * request
Current request.
ippool_alloc_call_env_t * env
Call environment for the allocation.
ippool_common_call_env_t * env
Call environment for the update.
static const call_env_method_t sqlippool_alloc_method_env
fr_value_box_t * query
Current query being run.
request_t * request
Current request.
static unlang_action_t mod_common(rlm_rcode_t *p_result, module_ctx_t const *mctx, request_t *request)
Common function used by module methods which perform an optional "free" then "update".
static const call_env_method_t sqlippool_mark_method_env
char const * name
static unlang_action_t mod_alloc_resume(rlm_rcode_t *p_result, UNUSED int *priority, request_t *request, void *uctx)
Resume function called after each IP allocation query is expanded.
static int sqlippool_box_escape(fr_value_box_t *vb, void *uctx)
Call SQL module box_escape_func to escape tainted values.
tmpl_t * pool_name_tmpl
Tmpl used to expand pool_name.
static unlang_action_t mod_common_update_resume(rlm_rcode_t *p_result, UNUSED int *priority, UNUSED request_t *request, void *uctx)
Resume function called after mod_common "update" query has completed.
fr_sql_query_t * query_ctx
Query context for allocation queries.
fr_value_box_t commit
SQL query to commit transaction.
tmpl_t * existing
tmpl to expand as query for finding the existing IP.
static void * sql_escape_uctx_alloc(UNUSED request_t *request, void const *uctx)
#define SUBMIT_QUERY(_query_str, _new_status, _type, _function)
tmpl_t * requested
tmpl to expand as query for finding the requested IP.
fr_value_box_t pool_name
Name of pool address will be allocated from.
static int sqlippool_alloc_ctx_free(ippool_alloc_ctx_t *to_free)
Release SQL pool connections when alloc context is freed.
fr_value_box_t update
SQL query to update an IP record.
fr_value_box_t begin
SQL query to begin transaction.
tmpl_t * find
tmpl to expand as query for finding an unused IP.
tmpl_t * allocated_address_attr
Attribute to populate with allocated IP.
ippool_alloc_status_t status
Status of the allocation.
module_rlm_t rlm_sqlippool
ippool_alloc_status_t
Current step in IP allocation state machine.
@ IPPOOL_ALLOC_POOL_CHECK_RUN
Run the "pool_check" query.
@ IPPOOL_ALLOC_FIND_RUN
Run the "find" query.
@ IPPOOL_ALLOC_MAKE_PAIR
Make the pair.
@ IPPOOL_ALLOC_POOL_CHECK
Expanding the "pool_check" query.
@ IPPOOL_ALLOC_REQUESTED
Expanding the "requested" query.
@ IPPOOL_ALLOC_COMMIT_RUN
RUn the "commit" query.
@ IPPOOL_ALLOC_REQUESTED_RUN
Run the "requested" query.
@ IPPOOL_ALLOC_EXISTING_RUN
Run the "existing" query.
@ IPPOOL_ALLOC_UPDATE_RUN
Run the "update" query.
@ IPPOOL_ALLOC_FIND
Expanding the "find" query.
@ IPPOOL_ALLOC_EXISTING
Expanding the "existing" query.
@ IPPOOL_ALLOC_UPDATE
Expanding the "update" query.
@ IPPOOL_ALLOC_NO_ADDRESS
No address was found.
@ IPPOOL_ALLOC_BEGIN_RUN
Run the "begin" query.
static int mod_instantiate(module_inst_ctx_t const *mctx)
static conf_parser_t module_config[]
tmpl_t * update
tmpl to expand as query for updating the found IP.
rlm_sql_t const * sql
SQL module instance.
Call environment used by module alloc method.
Resume context for IP allocation.
Call environment used by all other module methods.
Resume context for IP update / release.
#define FR_SBUFF_IN(_start, _len_or_end)
#define SECTION_NAME(_name1, _name2)
Define a section name consisting of a verb and a noun.
Definition section.h:40
char const * name
Instance name e.g. user_database.
Definition module.h:335
CONF_SECTION * conf
Module's instance configuration.
Definition module.h:329
size_t inst_size
Size of the module's instance data.
Definition module.h:203
void * data
Module's instance data.
Definition module.h:271
void * data
Thread specific instance data.
Definition module.h:352
static module_thread_instance_t * module_thread(module_instance_t const *mi)
Retrieve module/thread specific instance for a module.
Definition module.h:481
#define MODULE_BINDING_TERMINATOR
Terminate a module binding list.
Definition module.h:151
Module instance data.
Definition module.h:265
Named methods exported by a module.
Definition module.h:173
tmpl_escape_t escape
How escaping should be handled during evaluation.
Definition tmpl.h:358
fr_value_box_safe_for_t literals_safe_for
safe_for value assigned to literal values in xlats, execs, and data.
Definition tmpl.h:356
@ TMPL_TYPE_DATA
Value in native boxed format.
Definition tmpl.h:142
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.
tmpl_t * tmpl_init_shallow(tmpl_t *vpt, tmpl_type_t type, fr_token_t quote, char const *name, ssize_t len, tmpl_rules_t const *t_rules))
Initialise a tmpl without copying the input name string.
Optional arguments passed to vp_tmpl functions.
Definition tmpl.h:341
eap_aka_sim_process_conf_t * inst
Value pair map.
Definition map.h:77
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
int(* sql_affected_rows)(fr_sql_query_t *query_ctx, rlm_sql_config_t const *config)
Definition rlm_sql.h:202
sql_rcode_t(* sql_finish_query)(fr_sql_query_t *query_ctx, rlm_sql_config_t const *config)
Definition rlm_sql.h:210
sql_rcode_t(* sql_finish_select_query)(fr_sql_query_t *query_ctx, rlm_sql_config_t const *config)
Definition rlm_sql.h:211
unlang_function_t fetch_row
Definition rlm_sql.h:235
unlang_function_t query
Definition rlm_sql.h:233
fr_sql_query_t *(* 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)
Definition rlm_sql.h:236
fr_value_box_escape_t box_escape_func
Definition rlm_sql.h:231
rlm_sql_config_t config
Definition rlm_sql.h:221
rlm_sql_driver_t const * driver
Driver's exported interface.
Definition rlm_sql.h:228
module_instance_t const * mi
Module instance data for thread lookups.
Definition rlm_sql.h:240
void * sql_escape_arg
Instance specific argument to be passed to escape function.
Definition rlm_sql.h:232
#define talloc_get_type_abort_const
Definition talloc.h:282
int unlang_tmpl_push(TALLOC_CTX *ctx, fr_value_box_list_t *out, request_t *request, tmpl_t const *tmpl, unlang_tmpl_args_t *args)
Push a tmpl onto the stack for evaluation.
Definition tmpl.c:262
fr_value_box_escape_t func
How to escape when returned from evaluation.
Definition tmpl_escape.h:81
struct tmpl_escape_t::@74 uctx
fr_value_box_safe_for_t safe_for
Value to set on boxes which have been escaped by the fr_value_box_escape_t function.
Definition tmpl_escape.h:83
@ T_SINGLE_QUOTED_STRING
Definition token.h:122
@ T_BARE_WORD
Definition token.h:120
@ T_OP_SET
Definition token.h:84
Main trunk management handle.
Definition trunk.c:198
fr_sbuff_parse_rules_t const * value_parse_rules_quoted[T_TOKEN_LAST]
Parse rules for quoted strings.
Definition value.c:606
void fr_value_box_bstrndup_shallow(fr_value_box_t *dst, fr_dict_attr_t const *enumv, char const *src, size_t len, bool tainted)
Assign a string to to a fr_value_box_t.
Definition value.c:4232
#define FR_MAX_STRING_LEN
Definition value.h:30
uintptr_t fr_value_box_safe_for_t
Escaping that's been applied to a value box.
Definition value.h:155
int nonnull(2, 5))
static size_t char ** out
Definition value.h:997