The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
rlm_sql_mysql.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: 3429c51635751a12bd94d9ebd86d94156d0472e0 $
19 * @file rlm_sql_mysql.c
20 * @brief MySQL driver.
21 *
22 * @copyright 2014-2015 Arran Cudbard-Bell (a.cudbardb@freeradius.org)
23 * @copyright 2000-2007,2015 The FreeRADIUS server project
24 * @copyright 2000 Mike Machado (mike@innercite.com)
25 * @copyright 2000 Alan DeKok (aland@freeradius.org)
26 */
27RCSID("$Id: 3429c51635751a12bd94d9ebd86d94156d0472e0 $")
28
29#define LOG_PREFIX log_prefix
30
31#include <freeradius-devel/server/base.h>
32#include <freeradius-devel/util/debug.h>
33
34#include <sys/stat.h>
35
36#include "config.h"
37
38#ifdef HAVE_MYSQL_MYSQL_H
39# include <mysql/errmsg.h>
40DIAG_OFF(strict-prototypes) /* Seen with homebrew mysql client 5.7.13 */
41# include <mysql/mysql.h>
42DIAG_ON(strict-prototypes)
43# include <mysql/mysqld_error.h>
44#elif defined(HAVE_MYSQL_H)
45# include <errmsg.h>
46DIAG_OFF(strict-prototypes) /* Seen with homebrew mysql client 5.7.13 */
47# include <mysql.h>
48DIAG_ON(strict-prototypes)
49# include <mysqld_error.h>
50#endif
51
52#include "rlm_sql.h"
53#include "rlm_sql_trunk.h"
54
60
62 { L("auto"), SERVER_WARNINGS_AUTO },
63 { L("no"), SERVER_WARNINGS_NO },
64 { L("yes"), SERVER_WARNINGS_YES }
65};
67
68typedef struct {
69 MYSQL db; //!< Structure representing connection details.
70 MYSQL *sock; //!< Connection details as returned by connection init functions.
71 MYSQL_RES *result; //!< Result from most recent query.
72 connection_t *conn; //!< Generic connection structure for this connection.
73 int fd; //!< fd for this connection's I/O events.
74 fr_sql_query_t *query_ctx; //!< Current query running on this connection.
75 int status; //!< returned by the most recent non-blocking function call.
77
78typedef struct {
79 char const *tls_ca_file; //!< Path to the CA used to validate the server's certificate.
80 char const *tls_ca_path; //!< Directory containing CAs that may be used to validate the
81 //!< servers certificate.
82 char const *tls_certificate_file; //!< Public certificate we present to the server.
83 char const *tls_private_key_file; //!< Private key for the certificate we present to the server.
84
85 char const *tls_crl_file; //!< Public certificate we present to the server.
86 char const *tls_crl_path; //!< Private key for the certificate we present to the server.
87
88 char const *tls_cipher; //!< Colon separated list of TLS ciphers for TLS <= 1.2.
89
90 bool tls_required; //!< Require that the connection is encrypted.
91 bool tls_check_cert; //!< Verify there's a trust relationship between the server's
92 ///< cert and one of the CAs we have configured.
93 bool tls_check_cert_cn; //!< Verify that the CN in the server cert matches the host
94 ///< we passed to mysql_real_connect().
95
96 char const *warnings_str; //!< Whether we always query the server for additional warnings.
97 rlm_sql_mysql_warnings warnings; //!< mysql_warning_count() doesn't
98 //!< appear to work with NDB cluster
99
100 char const *character_set; //!< Character set to use on connections.
102
106 { FR_CONF_OFFSET_FLAGS("certificate_file", CONF_FLAG_FILE_READABLE, rlm_sql_mysql_t, tls_certificate_file) },
107 { FR_CONF_OFFSET_FLAGS("private_key_file", CONF_FLAG_FILE_READABLE, rlm_sql_mysql_t, tls_private_key_file) },
108 { FR_CONF_OFFSET_FLAGS("crl_file", CONF_FLAG_FILE_READABLE, rlm_sql_mysql_t, tls_crl_file) },
109 { FR_CONF_OFFSET_FLAGS("crl_path", CONF_FLAG_FILE_READABLE, rlm_sql_mysql_t, tls_crl_path) },
110 /*
111 * MySQL Specific TLS attributes
112 */
113 { FR_CONF_OFFSET("cipher", rlm_sql_mysql_t, tls_cipher) },
114
115 /*
116 * The closest thing we have to these options in other modules is
117 * in rlm_rest. rlm_ldap has its own bizarre option set.
118 *
119 * There, the options can be toggled independently, here they can't
120 * but for consistency we break them out anyway, and warn if the user
121 * has provided an invalid list of flags.
122 */
123 { FR_CONF_OFFSET("tls_required", rlm_sql_mysql_t, tls_required) },
124 { FR_CONF_OFFSET("check_cert", rlm_sql_mysql_t, tls_check_cert) },
125 { FR_CONF_OFFSET("check_cert_cn", rlm_sql_mysql_t, tls_check_cert_cn) },
127};
128
129static const conf_parser_t driver_config[] = {
130 { FR_CONF_POINTER("tls", 0, CONF_FLAG_SUBSECTION, NULL), .subcs = (void const *) tls_config },
131
132 { FR_CONF_OFFSET("warnings", rlm_sql_mysql_t, warnings_str), .dflt = "auto" },
133 { FR_CONF_OFFSET("character_set", rlm_sql_mysql_t, character_set) },
135};
136
137/* Prototypes */
139
140static int mod_instantiate(module_inst_ctx_t const *mctx)
141{
142 rlm_sql_mysql_t *inst = talloc_get_type_abort(mctx->mi->data, rlm_sql_mysql_t);
143 int warnings;
144 char const *log_prefix = mctx->mi->name;
145
146 warnings = fr_table_value_by_str(server_warnings_table, inst->warnings_str, -1);
147 if (warnings < 0) {
148 ERROR("Invalid warnings value \"%s\", must be yes, no, or auto", inst->warnings_str);
149 return -1;
150 }
151 inst->warnings = (rlm_sql_mysql_warnings)warnings;
152
153 if (inst->tls_check_cert && !inst->tls_required) {
154 WARN("Implicitly setting tls_required = yes, as tls_check_cert = yes");
155 inst->tls_required = true;
156 }
157 if (inst->tls_check_cert_cn) {
158 if (!inst->tls_required) {
159 WARN("Implicitly setting tls_required = yes, as check_cert_cn = yes");
160 inst->tls_required = true;
161 }
162
163 if (!inst->tls_check_cert) {
164 WARN("Implicitly setting check_cert = yes, as check_cert_cn = yes");
165 inst->tls_check_cert = true;
166 }
167 }
168 return 0;
169}
170
171static void mod_unload(void)
172{
173 mysql_library_end();
174}
175
176static int mod_load(void)
177{
178 char const *log_prefix = "rlm_sql_mysql";
179 if (mysql_library_init(0, NULL, NULL)) {
180 ERROR("libmysql initialisation failed");
181
182 return -1;
183 }
184
185 INFO("libmysql version: %s", mysql_get_client_info());
186
187 return 0;
188}
189
190/** Callback for I/O events in response to mysql_real_connect_start()
191 */
192static void _sql_connect_io_notify(fr_event_list_t *el, int fd, UNUSED int flags, void *uctx)
193{
194 rlm_sql_mysql_conn_t *c = talloc_get_type_abort(uctx, rlm_sql_mysql_conn_t);
195 char const *log_prefix = c->conn->name;
196
197 if (c->status == 0) goto connected;
198 c->status = mysql_real_connect_cont(&c->sock, &c->db, c->status);
199
200 /*
201 * If status is not zero, we're still waiting for something.
202 * The event will be fired again when that happens.
203 */
204 if (c->status != 0) {
205 (void) fr_event_fd_insert(c, NULL, c->conn->el, c->fd,
206 c->status & MYSQL_WAIT_READ ? _sql_connect_io_notify : NULL,
207 c->status & MYSQL_WAIT_WRITE ? _sql_connect_io_notify : NULL, NULL, c);
208 return;
209 }
210
211connected:
212 /*
213 * Pause any notifications until we're actually ready
214 * to operate on the connection.
215 */
217
218 if (!c->sock) {
219 ERROR("MySQL error: %s", mysql_error(&c->db));
221 return;
222 }
223
224 DEBUG2("Connected to database on %s, server version %s, protocol version %i",
225 mysql_get_host_info(c->sock),
226 mysql_get_server_info(c->sock), mysql_get_proto_info(c->sock));
227
229}
230
232 UNUSED connection_state_t state, void *uctx)
233{
235 rlm_sql_mysql_conn_t *sql_conn = talloc_get_type_abort(conn->h, rlm_sql_mysql_conn_t);
236 char const *log_prefix = conn->name;
237 int ret;
238 MYSQL_RES *result;
239
240 DEBUG2("Executing \"%s\"", sql->config.connect_query);
241
242 ret = mysql_real_query(sql_conn->sock, sql->config.connect_query, strlen(sql->config.connect_query));
243 if (ret != 0) {
244 char const *info;
245 ERROR("Failed running \"open_query\"");
246 info = mysql_info(sql_conn->sock);
247 if (info) ERROR("%s", info);
249 return;
250 }
251
252 /*
253 * These queries should not return any results - but let's be safe
254 */
255 result = mysql_store_result(sql_conn->sock);
256 if (result) mysql_free_result(result);
257 while ((mysql_next_result(sql_conn->sock) == 0) &&
258 (result = mysql_store_result(sql_conn->sock))) {
259 mysql_free_result(result);
260 }
261}
262
263CC_NO_UBSAN(function) /* UBSAN: false positive - public vs private connection_t trips --fsanitize=function*/
264static connection_state_t _sql_connection_init(void **h, connection_t *conn, void *uctx)
265{
267 rlm_sql_mysql_t const *inst = talloc_get_type_abort(sql->driver_submodule->data, rlm_sql_mysql_t);
268 char const *log_prefix = conn->name;
270 rlm_sql_config_t const *config = &sql->config;
271
272 unsigned long sql_flags;
273 enum mysql_option ssl_mysql_opt;
274 unsigned int ssl_mode = 0;
275 bool ssl_mode_isset = false;
276
277 MEM(c = talloc_zero(conn, rlm_sql_mysql_conn_t));
278 c->conn = conn;
279 c->fd = -1;
280
281 DEBUG("Starting connect to MySQL server");
282
283 mysql_init(&c->db);
284
285 /*
286 * If any of the TLS options are set, configure TLS
287 *
288 * According to MySQL docs this function always returns 0, so we won't
289 * know if ssl setup succeeded until mysql_real_connect is called below.
290 */
291 if (inst->tls_ca_file || inst->tls_ca_path ||
292 inst->tls_certificate_file || inst->tls_private_key_file) {
293 mysql_ssl_set(&(c->db), inst->tls_private_key_file, inst->tls_certificate_file,
294 inst->tls_ca_file, inst->tls_ca_path, inst->tls_cipher);
295 }
296
297#ifdef MARIADB_BASE_VERSION
298 if (inst->tls_required || inst->tls_check_cert || inst->tls_check_cert_cn) {
299 ssl_mode_isset = true;
300 /**
301 * For MariaDB, It should be true as can be seen in
302 * https://github.com/MariaDB/server/blob/mariadb-5.5.68/sql-common/client.c#L4338
303 */
304 ssl_mode = true;
305 ssl_mysql_opt = MYSQL_OPT_SSL_VERIFY_SERVER_CERT;
306 }
307#else
308 ssl_mysql_opt = MYSQL_OPT_SSL_MODE;
309 if (inst->tls_required) {
310 ssl_mode = SSL_MODE_REQUIRED;
311 ssl_mode_isset = true;
312 }
313 if (inst->tls_check_cert) {
314 ssl_mode = SSL_MODE_VERIFY_CA;
315 ssl_mode_isset = true;
316 }
317 if (inst->tls_check_cert_cn) {
318 ssl_mode = SSL_MODE_VERIFY_IDENTITY;
319 ssl_mode_isset = true;
320 }
321#endif
322 if (ssl_mode_isset) mysql_options(&(c->db), ssl_mysql_opt, &ssl_mode);
323
324 if (inst->tls_crl_file) mysql_options(&(c->db), MYSQL_OPT_SSL_CRL, inst->tls_crl_file);
325 if (inst->tls_crl_path) mysql_options(&(c->db), MYSQL_OPT_SSL_CRLPATH, inst->tls_crl_path);
326
327 mysql_options(&(c->db), MYSQL_READ_DEFAULT_GROUP, "freeradius");
328
329 if (inst->character_set) mysql_options(&(c->db), MYSQL_SET_CHARSET_NAME, inst->character_set);
330
331#if MYSQL_VERSION_ID < 80034
332 /*
333 * We need to know about connection errors, and are capable
334 * of reconnecting automatically.
335 *
336 * This deprecated as of 8.0.34.
337 */
338 {
339 bool reconnect = 0;
340 mysql_options(&(c->db), MYSQL_OPT_RECONNECT, &reconnect);
341 }
342#endif
343
344 sql_flags = CLIENT_MULTI_RESULTS | CLIENT_FOUND_ROWS;
345
346#ifdef CLIENT_MULTI_STATEMENTS
347 sql_flags |= CLIENT_MULTI_STATEMENTS;
348#endif
349
350 mysql_options(&c->db, MYSQL_OPT_NONBLOCK, 0);
351
352 c->status = mysql_real_connect_start(&c->sock, &c->db,
353 config->sql_server,
354 config->sql_login,
355 config->sql_password,
356 config->sql_db,
357 config->sql_port, NULL, sql_flags);
358
359 c->fd = mysql_get_socket(&c->db);
360 if (c->fd < 0) {
361 ERROR("Could't connect to MySQL server %s@%s:%s", config->sql_login,
362 config->sql_server, config->sql_db);
363 ERROR("MySQL error: %s", mysql_error(&c->db));
364 error:
365 talloc_free(c);
367 }
368
369 if (c->status == 0) {
370 DEBUG2("Connected to database '%s' on %s, server version %s, protocol version %i",
371 config->sql_db, mysql_get_host_info(c->sock),
372 mysql_get_server_info(c->sock), mysql_get_proto_info(c->sock));
373 goto finish;
374 }
375
376 if (fr_event_fd_insert(c, NULL, c->conn->el, c->fd,
377 c->status & MYSQL_WAIT_READ ? _sql_connect_io_notify : NULL,
378 c->status & MYSQL_WAIT_WRITE ? _sql_connect_io_notify : NULL, NULL, c) != 0) goto error;
379
380 DEBUG2("Connecting to database '%s' on %s:%d, fd %d",
381 config->sql_db, config->sql_server, config->sql_port, c->fd);
382
383finish:
384 *h = c;
385
387 _sql_connect_query_run, true, sql);
388
390}
391
392static void _sql_connection_close(fr_event_list_t *el, void *h, UNUSED void *uctx)
393{
394 rlm_sql_mysql_conn_t *c = talloc_get_type_abort(h, rlm_sql_mysql_conn_t);
395
396 if (c->fd >= 0) {
398 c->fd = -1;
399 }
400 mysql_close(&c->db);
401 c->query_ctx = NULL;
402 talloc_free(h);
403}
404
405/** Analyse the last error that occurred on the socket, and determine an action
406 *
407 * @param server Socket from which to extract the server error. May be NULL.
408 * @param client_errno Error from the client.
409 * @return an action for #rlm_sql_t to take.
410 */
411static sql_rcode_t sql_check_error(MYSQL *server, int client_errno)
412{
413 int sql_errno = 0;
414
415 /*
416 * The client and server error numbers are in the
417 * same numberspace.
418 */
419 if (server) sql_errno = mysql_errno(server);
420 if ((sql_errno == 0) && (client_errno != 0)) sql_errno = client_errno;
421
422 if (sql_errno > 0) switch (sql_errno) {
423 case CR_SERVER_GONE_ERROR:
424 case CR_SERVER_LOST:
425 return RLM_SQL_RECONNECT;
426
427 case CR_OUT_OF_MEMORY:
428 case CR_COMMANDS_OUT_OF_SYNC:
429 case CR_UNKNOWN_ERROR:
430 default:
431 return RLM_SQL_ERROR;
432
433 /*
434 * Constraints errors that signify a duplicate, or that we might
435 * want to try an alternative query.
436 *
437 * Error constants not found in the 3.23/4.0/4.1 manual page
438 * are checked for.
439 * Other error constants should always be available.
440 */
441 case ER_DUP_UNIQUE: /* Can't write, because of unique constraint, to table '%s'. */
442 case ER_DUP_KEY: /* Can't write; duplicate key in table '%s' */
443
444 case ER_DUP_ENTRY: /* Duplicate entry '%s' for key %d. */
445 case ER_NO_REFERENCED_ROW: /* Cannot add or update a child row: a foreign key constraint fails */
446 case ER_ROW_IS_REFERENCED: /* Cannot delete or update a parent row: a foreign key constraint fails */
447#ifdef ER_FOREIGN_DUPLICATE_KEY
448 case ER_FOREIGN_DUPLICATE_KEY: /* Upholding foreign key constraints for table '%s', entry '%s', key %d would lead to a duplicate entry. */
449#endif
450#ifdef ER_DUP_ENTRY_WITH_KEY_NAME
451 case ER_DUP_ENTRY_WITH_KEY_NAME: /* Duplicate entry '%s' for key '%s' */
452#endif
453#ifdef ER_NO_REFERENCED_ROW_2
454 case ER_NO_REFERENCED_ROW_2:
455#endif
456#ifdef ER_ROW_IS_REFERENCED_2
457 case ER_ROW_IS_REFERENCED_2:
458#endif
459 return RLM_SQL_ALT_QUERY;
460
461 /*
462 * Constraints errors that signify an invalid query
463 * that can never succeed.
464 */
465 case ER_BAD_NULL_ERROR: /* Column '%s' cannot be null */
466 case ER_NON_UNIQ_ERROR: /* Column '%s' in %s is ambiguous */
468
469 /*
470 * Constraints errors that signify no data returned.
471 *
472 * This is considered OK as the caller may look for the next result set.
473 */
474 case ER_SP_FETCH_NO_DATA:
475 return RLM_SQL_OK;
476
477 }
478
479 return RLM_SQL_OK;
480}
481
483{
484 sql_rcode_t rcode;
485 int ret;
486
487retry_store_result:
488 conn->result = mysql_store_result(conn->sock);
489 if (!conn->result) {
490 rcode = sql_check_error(conn->sock, 0);
491 if (rcode != RLM_SQL_OK) return rcode;
492 ret = mysql_next_result(conn->sock);
493 if (ret == 0) {
494 /* there are more results */
495 goto retry_store_result;
496 } else if (ret > 0) return sql_check_error(NULL, ret);
497 /* ret == -1 signals no more results */
498 }
499 return RLM_SQL_OK;
500}
501
503{
504 rlm_sql_mysql_conn_t *conn = talloc_get_type_abort(query_ctx->tconn->conn->h, rlm_sql_mysql_conn_t);
505
506 if (conn->result) return mysql_num_rows(conn->result);
507
508 return 0;
509}
510
511static sql_rcode_t sql_fields(char const **out[], fr_sql_query_t *query_ctx, UNUSED rlm_sql_config_t const *config)
512{
513 rlm_sql_mysql_conn_t *conn = talloc_get_type_abort(query_ctx->tconn->conn->h, rlm_sql_mysql_conn_t);
514
515 unsigned int fields, i;
516 MYSQL_FIELD *field_info;
517 char const **names;
518
519 /*
520 * Use our internal function to abstract out the API call.
521 * Different versions of SQL use different functions,
522 * and some don't like NULL pointers.
523 */
524 fields = mysql_field_count(conn->sock);
525 if (fields == 0) return RLM_SQL_ERROR;
526
527 /*
528 * https://bugs.mysql.com/bug.php?id=32318
529 * Hints that we don't have to free field_info.
530 */
531 field_info = mysql_fetch_fields(conn->result);
532 if (!field_info) return RLM_SQL_ERROR;
533
534 MEM(names = talloc_array(query_ctx, char const *, fields));
535
536 for (i = 0; i < fields; i++) names[i] = field_info[i].name;
537 *out = names;
538
539 return RLM_SQL_OK;
540}
541
542static unlang_action_t sql_fetch_row(unlang_result_t *p_result, UNUSED request_t *request, void *uctx)
543{
544 fr_sql_query_t *query_ctx = talloc_get_type_abort(uctx, fr_sql_query_t);
545 rlm_sql_mysql_conn_t *conn = talloc_get_type_abort(query_ctx->tconn->conn->h, rlm_sql_mysql_conn_t);
546 MYSQL_ROW row;
547 int ret;
548 unsigned int num_fields, i;
549 unsigned long *field_lens;
550
551 /*
552 * Check pointer before de-referencing it.
553 * Lack of conn->result is either an error, or no result returned.
554 */
555 if (!conn->result) {
556 query_ctx->rcode = sql_check_error(conn->sock, 0);
557 if (query_ctx->rcode == RLM_SQL_OK) {
558 query_ctx->rcode = RLM_SQL_NO_MORE_ROWS;
560 }
562 }
563
564 TALLOC_FREE(query_ctx->row); /* Clear previous row set */
565
566retry_fetch_row:
567 row = mysql_fetch_row(conn->result);
568 if (!row) {
569 query_ctx->rcode = sql_check_error(conn->sock, 0);
570 if (query_ctx->rcode != RLM_SQL_OK) RETURN_UNLANG_FAIL;
571
572 mysql_free_result(conn->result);
573 conn->result = NULL;
574
575 ret = mysql_next_result(conn->sock);
576 if (ret == 0) {
577 /* there are more results */
578 if ((sql_store_result(conn, &query_ctx->inst->config) == 0) && (conn->result != NULL)) {
579 goto retry_fetch_row;
580 }
581 } else if (ret > 0) {
582 query_ctx->rcode = sql_check_error(NULL, ret);
583 if (query_ctx->rcode == RLM_SQL_OK) RETURN_UNLANG_OK;
585 }
586 /* If ret is -1 then there are no more rows */
587
588 query_ctx->rcode = RLM_SQL_NO_MORE_ROWS;
590 }
591
592 num_fields = mysql_field_count(conn->sock);
593 if (!num_fields) {
594 query_ctx->rcode = RLM_SQL_NO_MORE_ROWS;
596 }
597
598 field_lens = mysql_fetch_lengths(conn->result);
599
600 MEM(query_ctx->row = talloc_zero_array(query_ctx, char *, num_fields + 1));
601 for (i = 0; i < num_fields; i++) {
602 if (!row[i]) continue;
603 MEM(query_ctx->row[i] = talloc_bstrndup(query_ctx->row, row[i], field_lens[i]));
604 }
605
606 query_ctx->rcode = RLM_SQL_OK;
608}
609
611{
612 rlm_sql_mysql_conn_t *conn = talloc_get_type_abort(query_ctx->tconn->conn->h, rlm_sql_mysql_conn_t);
613
614 if (conn->result) {
615 mysql_free_result(conn->result);
616 conn->result = NULL;
617 }
618 TALLOC_FREE(query_ctx->row);
619
620 return RLM_SQL_OK;
621}
622
623/** Retrieves any warnings associated with the last query
624 *
625 * MySQL stores a limited number of warnings associated with the last query
626 * executed. These can be very useful in diagnosing issues, or in some cases
627 * working around bugs in MySQL which causes it to return the wrong error.
628 *
629 * @note Caller should free any memory allocated in ctx (talloc_free_children()).
630 *
631 * @param ctx to allocate temporary error buffers in.
632 * @param out Array of sql_log_entrys to fill.
633 * @param outlen Length of out array.
634 * @param conn MySQL connection the query was run on.
635 * @return
636 * - Number of errors written to the #sql_log_entry_t array.
637 * - -1 on failure.
638 */
639static size_t sql_warnings(TALLOC_CTX *ctx, sql_log_entry_t out[], size_t outlen,
641{
642 MYSQL_RES *result;
643 MYSQL_ROW row;
644 unsigned int num_fields;
645 size_t i = 0;
646 char const *log_prefix = conn->conn->name;
647
648 if (outlen == 0) return 0;
649
650 /*
651 * Retrieve any warnings associated with the previous query
652 * that were left lingering on the server.
653 */
654 if (mysql_query(conn->sock, "SHOW WARNINGS") != 0) return -1;
655 result = mysql_store_result(conn->sock);
656 if (!result) return -1;
657
658 /*
659 * Fields should be [0] = Level, [1] = Code, [2] = Message
660 */
661 num_fields = mysql_field_count(conn->sock);
662 if (num_fields < 3) {
663 WARN("Failed retrieving warnings, expected 3 fields got %u", num_fields);
664 mysql_free_result(result);
665
666 return -1;
667 }
668
669 while ((row = mysql_fetch_row(result))) {
670 char *msg = NULL;
672
673 /*
674 * Translate the MySQL log level into our internal
675 * log levels, so they get colourised correctly.
676 */
677 if (strcasecmp(row[0], "warning") == 0) type = L_WARN;
678 else if (strcasecmp(row[0], "note") == 0) type = L_DBG;
679 else type = L_ERR;
680
681 msg = talloc_typed_asprintf(ctx, "%s: %s", row[1], row[2]);
682 out[i].type = type;
683 out[i].msg = msg;
684 if (++i == outlen) break;
685 }
686
687 mysql_free_result(result);
688
689 return i;
690}
691
692/** Retrieves any errors associated with the query context
693 *
694 * @note Caller should free any memory allocated in ctx (talloc_free_children()).
695 *
696 * @param ctx to allocate temporary error buffers in.
697 * @param out Array of sql_log_entrys to fill.
698 * @param outlen Length of out array.
699 * @param query_ctx Query context to retrieve error for.
700 * @return number of errors written to the #sql_log_entry_t array.
701 */
702static size_t sql_error(TALLOC_CTX *ctx, sql_log_entry_t out[], size_t outlen,
703 fr_sql_query_t *query_ctx)
704{
707 char const *error;
708 size_t i = 0;
709 char const *log_prefix;
710
711 if (!query_ctx->tconn) return 0;
712 conn = talloc_get_type_abort(query_ctx->tconn->conn->h, rlm_sql_mysql_conn_t);
713 log_prefix = conn->conn->name;
714
715 fr_assert(outlen > 0);
716
717 error = mysql_error(conn->sock);
718
719 /*
720 * Grab the error now in case it gets cleared on the next operation.
721 */
722 if (error && (error[0] != '\0')) {
723 error = talloc_typed_asprintf(ctx, "ERROR %u (%s): %s", mysql_errno(conn->sock), error,
724 mysql_sqlstate(conn->sock));
725 } else {
726 error = NULL;
727 }
728
729 /*
730 * Don't attempt to get errors from the server, if the last error
731 * was that the server was unavailable.
732 */
733 if ((outlen > 1) && (sql_check_error(conn->sock, 0) != RLM_SQL_RECONNECT)) {
734 size_t ret;
735 unsigned int msgs;
736
737 switch (inst->warnings) {
739 /*
740 * Check to see if any warnings can be retrieved from the server.
741 */
742 msgs = mysql_warning_count(conn->sock);
743 if (msgs == 0) {
744 DEBUG3("No additional diagnostic info on server");
745 break;
746 }
747
750 ret = sql_warnings(ctx, out, outlen - 1, conn);
751 if (ret > 0) i += ret;
752 break;
753
755 break;
756
757 default:
758 fr_assert(0);
759 }
760 }
761
762 if (error) {
763 out[i].type = L_ERR;
764 out[i].msg = error;
765 i++;
766 }
767
768 return i;
769}
770
771/** Finish query
772 *
773 * As a single SQL statement may return multiple results
774 * sets, (for example stored procedures) it is necessary to check
775 * whether more results exist and process them in turn if so.
776 *
777 */
779{
781 int ret;
782 MYSQL_RES *result;
783
784 /*
785 * If the query is not in a state which would return results, then do nothing.
786 */
787 if (query_ctx->treq && !(query_ctx->treq->state &
789
790 /*
791 * If the connection doesn't exist there's nothing to do
792 */
793 if (!query_ctx->tconn || !query_ctx->tconn->conn || !query_ctx->tconn->conn->h) return RLM_SQL_ERROR;
794
795 conn = talloc_get_type_abort(query_ctx->tconn->conn->h, rlm_sql_mysql_conn_t);
796
797 /*
798 * If the connection is not active, then all that we can do is free any stored results
799 */
800 if (query_ctx->tconn->conn->state != CONNECTION_STATE_CONNECTED) {
801 sql_free_result(query_ctx, config);
802 return RLM_SQL_OK;
803 }
804
805 /*
806 * If there's no result associated with the
807 * connection handle, assume the first result in the
808 * result set hasn't been retrieved.
809 *
810 * MySQL docs says there's no performance penalty for
811 * calling mysql_store_result for queries which don't
812 * return results.
813 */
814 if (conn->result == NULL) {
815 result = mysql_store_result(conn->sock);
816 if (result) mysql_free_result(result);
817 /*
818 * ...otherwise call sql_free_result to free an
819 * already stored result.
820 */
821 } else {
822 sql_free_result(query_ctx, config); /* sql_free_result sets conn->result to NULL */
823 }
824
825 /*
826 * Drain any other results associated with the handle
827 *
828 * mysql_next_result advances the result cursor so that
829 * the next call to mysql_store_result will retrieve
830 * the next result from the server.
831 *
832 * Unfortunately this really does appear to be the
833 * only way to return the handle to a consistent state.
834 */
835 while (((ret = mysql_next_result(conn->sock)) == 0) &&
836 (result = mysql_store_result(conn->sock))) {
837 mysql_free_result(result);
838 }
839 if (ret > 0) return sql_check_error(NULL, ret);
840
841 return RLM_SQL_OK;
842}
843
845{
846 rlm_sql_mysql_conn_t *conn = talloc_get_type_abort(query_ctx->tconn->conn->h, rlm_sql_mysql_conn_t);
847
848 return mysql_affected_rows(conn->sock);
849}
850
851static ssize_t sql_escape_func(request_t *request, char *out, size_t outlen, char const *in, void *arg)
852{
853 size_t inlen;
854 connection_t *c = talloc_get_type_abort(arg, connection_t);
856 char const *log_prefix = c->name;
857
858 if ((c->state == CONNECTION_STATE_HALTED) || (c->state == CONNECTION_STATE_CLOSED)) {
859 ROPTIONAL(RERROR, ERROR, "Connection not available for escaping");
860 return -1;
861 }
862
863 conn = talloc_get_type_abort(c->h, rlm_sql_mysql_conn_t);
864
865 /* Check for potential buffer overflow */
866 inlen = strlen(in);
867 if ((inlen * 2 + 1) > outlen) return 0;
868 /* Prevent integer overflow */
869 if ((inlen * 2 + 1) <= inlen) return 0;
870
871 return mysql_real_escape_string(&conn->db, out, in, inlen);
872}
873
875
876#undef LOG_PREFIX
877#define LOG_PREFIX "rlm_sql_mysql"
878
880
881#undef LOG_PREFIX
882#define LOG_PREFIX log_prefix
883
884CC_NO_UBSAN(function) /* UBSAN: false positive - public vs private connection_t trips --fsanitize=function*/
886 connection_t *conn, UNUSED void *uctx)
887{
888 rlm_sql_mysql_conn_t *sql_conn = talloc_get_type_abort(conn->h, rlm_sql_mysql_conn_t);
889 char const *log_prefix = conn->name;
890 request_t *request;
891 trunk_request_t *treq;
892 fr_sql_query_t *query_ctx;
893 char const *info;
894 int err;
895
896 if (trunk_connection_pop_request(&treq, tconn) != 0) return;
897 if (!treq) return;
898
899 query_ctx = talloc_get_type_abort(treq->preq, fr_sql_query_t);
900 request = query_ctx->request;
901
902 /*
903 * Each of the MariaDB async "start" calls returns a non-zero value
904 * if they are waiting on I/O.
905 * A return value of zero means that the operation completed.
906 */
907
908 switch (query_ctx->status) {
910 ROPTIONAL(RDEBUG2, DEBUG2, "Executing query: %s", query_ctx->query_str);
911 sql_conn->status = mysql_real_query_start(&err, sql_conn->sock, query_ctx->query_str, strlen(query_ctx->query_str));
912 query_ctx->tconn = tconn;
913
914 if (sql_conn->status) {
915 ROPTIONAL(RDEBUG3, DEBUG3, "Waiting for IO");
916 query_ctx->status = SQL_QUERY_SUBMITTED;
917 sql_conn->query_ctx = query_ctx;
919 return;
920 }
921
922 if (err) {
923 /*
924 * Need to check what kind of error this is - it may
925 * be a unique key conflict, we run the next query.
926 */
927 info = mysql_info(sql_conn->sock);
928 query_ctx->rcode = sql_check_error(sql_conn->sock, 0);
929 if (info) ERROR("%s", info);
930 switch (query_ctx->rcode) {
931 case RLM_SQL_OK:
933 break;
934
935 default:
936 query_ctx->status = SQL_QUERY_FAILED;
938 if (request) unlang_interpret_mark_runnable(request);
939 return;
940 }
941 } else {
942 query_ctx->rcode = RLM_SQL_OK;
943 }
944 query_ctx->status = SQL_QUERY_RETURNED;
945
946 break;
947
949 ROPTIONAL(RDEBUG2, DEBUG2, "Fetching results");
950 fr_assert(query_ctx->tconn == tconn);
951 sql_conn->status = mysql_store_result_start(&sql_conn->result, sql_conn->sock);
952
953 if (sql_conn->status) {
954 ROPTIONAL(RDEBUG3, DEBUG3, "Waiting for IO");
956 sql_conn->query_ctx = query_ctx;
958 return;
959 }
961 query_ctx->rcode = RLM_SQL_OK;
962
963 break;
964
965 default:
966 /*
967 * The request outstanding on this connection returned
968 * immediately, so we are not actually waiting for I/O.
969 */
970 return;
971 }
972
973 /*
974 * The current request is not waiting for I/O so the request can run
975 */
976 ROPTIONAL(RDEBUG3, DEBUG3, "Got immediate response");
978 if (request) unlang_interpret_mark_runnable(request);
979}
980
981CC_NO_UBSAN(function) /* UBSAN: false positive - public vs private connection_t trips --fsanitize=function*/
983 connection_t *conn, UNUSED void *uctx)
984{
985 rlm_sql_mysql_conn_t *sql_conn = talloc_get_type_abort(conn->h, rlm_sql_mysql_conn_t);
986 char const *log_prefix = conn->name;
987 fr_sql_query_t *query_ctx;
988 char const *info;
989 int err = 0;
990 request_t *request;
991
992 /*
993 * Lookup the outstanding SQL query for this connection.
994 * There will only ever be one per tconn.
995 */
996 query_ctx = sql_conn->query_ctx;
997
998 /*
999 * No outstanding query on this connection.
1000 * Should not happen, but added for safety.
1001 */
1002 if (unlikely(!query_ctx)) return;
1003
1004 switch (query_ctx->status) {
1006 sql_conn->status = mysql_real_query_cont(&err, sql_conn->sock, sql_conn->status);
1007 break;
1008
1010 sql_conn->status = mysql_store_result_cont(&sql_conn->result, sql_conn->sock, sql_conn->status);
1011 break;
1012
1013 default:
1014 /*
1015 * The request outstanding on this connection returned
1016 * immediately, so we are not actually waiting for I/O.
1017 */
1018 return;
1019 }
1020
1021 /*
1022 * Are we still waiting for any further I/O?
1023 */
1024 if (sql_conn->status != 0) return;
1025
1026 sql_conn->query_ctx = NULL;
1027
1028 switch (query_ctx->status) {
1030 query_ctx->status = SQL_QUERY_RETURNED;
1031 break;
1032
1034 query_ctx->status = SQL_QUERY_RESULTS_FETCHED;
1035 break;
1036
1037 default:
1038 fr_assert(0);
1039 }
1040
1041 request = query_ctx->request;
1042 if (request) unlang_interpret_mark_runnable(request);
1043
1044 if (err) {
1045 info = mysql_info(sql_conn->sock);
1046 query_ctx->rcode = sql_check_error(sql_conn->sock, 0);
1047 if (info) ROPTIONAL(RERROR, ERROR, "%s", info);
1048 return;
1049 }
1050
1051 query_ctx->rcode = RLM_SQL_OK;
1052}
1053
1054CC_NO_UBSAN(function) /* UBSAN: false positive - public vs private connection_t trips --fsanitize=function*/
1055static void sql_request_cancel(connection_t *conn, void *preq, trunk_cancel_reason_t reason,
1056 UNUSED void *uctx)
1057{
1058 fr_sql_query_t *query_ctx = talloc_get_type_abort(preq, fr_sql_query_t);
1059 rlm_sql_mysql_conn_t *sql_conn = talloc_get_type_abort(conn->h, rlm_sql_mysql_conn_t);
1060
1061 if (!query_ctx->treq) return;
1062 if (reason != TRUNK_CANCEL_REASON_SIGNAL) return;
1063 if (sql_conn->query_ctx == query_ctx) sql_conn->query_ctx = NULL;
1064}
1065
1066CC_NO_UBSAN(function) /* UBSAN: false positive - public vs private connection_t trips --fsanitize=function*/
1068 connection_t *conn, UNUSED void *uctx)
1069{
1070 trunk_request_t *treq;
1071
1072 /*
1073 * The MariaDB non-blocking API doesn't have any cancellation functions -
1074 * rather you are expected to close the connection.
1075 */
1076 if ((trunk_connection_pop_cancellation(&treq, tconn)) == 0) {
1079 }
1080}
1081
1084
1086{
1087 fr_sql_query_t *query_ctx = talloc_get_type_abort(uctx, fr_sql_query_t);
1088
1089 if (query_ctx->rcode != RLM_SQL_OK) RETURN_UNLANG_FAIL;
1090
1091 if (query_ctx->status == SQL_QUERY_RETURNED) {
1092 trunk_request_requeue(query_ctx->treq);
1093
1095 query_ctx->rcode = RLM_SQL_ERROR;
1097 }
1098
1099 return UNLANG_ACTION_YIELD;
1100 }
1101
1103}
1104
1105/** Allocate the argument used for the SQL escape function
1106 *
1107 * In this case, a dedicated connection to allow the escape
1108 * function to have access to server side parameters, though
1109 * no packets ever flow after the connection is made.
1110 */
1111static void *sql_escape_arg_alloc(TALLOC_CTX *ctx, fr_event_list_t *el, void *uctx)
1112{
1113 rlm_sql_t const *inst = talloc_get_type_abort(uctx, rlm_sql_t);
1114 connection_t *conn;
1115 char const *log_prefix = inst->name;
1116
1117 conn = connection_alloc(ctx, el,
1119 .init = _sql_connection_init,
1120 .close = _sql_connection_close,
1121 },
1122 inst->config.trunk_conf.conn_conf,
1123 inst->name, inst);
1124
1125 if (!conn) {
1126 PERROR("Failed allocating state handler for SQL escape connection");
1127 return NULL;
1128 }
1129
1131 return conn;
1132}
1133
1134static void sql_escape_arg_free(void *uctx)
1135{
1136 connection_t *conn = talloc_get_type_abort(uctx, connection_t);
1138}
1139
1140/* Exported to rlm_sql */
1143 .common = {
1144 .name = "sql_mysql",
1145 .magic = MODULE_MAGIC_INIT,
1146 .inst_size = sizeof(rlm_sql_mysql_t),
1147 .onload = mod_load,
1148 .unload = mod_unload,
1150 .instantiate = mod_instantiate
1151 },
1153 .sql_query_resume = sql_query_resume,
1154 .sql_select_query_resume = sql_select_query_resume,
1155 .sql_num_rows = sql_num_rows,
1156 .sql_affected_rows = sql_affected_rows,
1157 .sql_fields = sql_fields,
1158 .sql_fetch_row = sql_fetch_row,
1159 .sql_free_result = sql_free_result,
1160 .sql_error = sql_error,
1161 .sql_finish_query = sql_finish_query,
1162 .sql_finish_select_query = sql_finish_query,
1163 .sql_escape_func = sql_escape_func,
1164 .sql_escape_arg_alloc = sql_escape_arg_alloc,
1165 .sql_escape_arg_free = sql_escape_arg_free,
1166 .trunk_io_funcs = {
1167 .connection_alloc = sql_trunk_connection_alloc,
1168 .connection_notify = sql_trunk_connection_notify,
1169 .request_mux = sql_trunk_request_mux,
1170 .request_demux = sql_trunk_request_demux,
1171 .request_cancel_mux = sql_request_cancel_mux,
1172 .request_cancel = sql_request_cancel,
1173 .request_fail = sql_request_fail,
1174 }
1175};
unlang_action_t
Returned by unlang_op_t calls, determine the next action of the interpreter.
Definition action.h:35
@ UNLANG_ACTION_YIELD
Temporarily pause execution until an event occurs.
Definition action.h:41
log_entry msg
Definition acutest.h:794
#define RCSID(id)
Definition build.h:485
#define L(_str)
Helper for initialising arrays of string literals.
Definition build.h:209
#define FALL_THROUGH
clang 10 doesn't recognised the FALL-THROUGH comment anymore
Definition build.h:324
#define DIAG_ON(_x)
Definition build.h:460
#define CC_NO_UBSAN(_sanitize)
Definition build.h:428
#define unlikely(_x)
Definition build.h:383
#define UNUSED
Definition build.h:317
#define NUM_ELEMENTS(_t)
Definition build.h:339
#define DIAG_OFF(_x)
Definition build.h:459
#define CONF_PARSER_TERMINATOR
Definition cf_parse.h:660
#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:283
conf_parser_flags_t flags
Flags which control parsing behaviour.
Definition cf_parse.h:603
#define FR_CONF_POINTER(_name, _type, _flags, _res_p)
conf_parser_t which parses a single CONF_PAIR producing a single global result
Definition cf_parse.h:337
#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:271
@ CONF_FLAG_FILE_READABLE
File matching value must exist, and must be readable.
Definition cf_parse.h:438
@ CONF_FLAG_SUBSECTION
Instead of putting the information into a configuration structure, the configuration file routines MA...
Definition cf_parse.h:426
Defines a CONF_PAIR to C data type mapping.
Definition cf_parse.h:597
connection_state_t
Definition connection.h:47
@ CONNECTION_STATE_FAILED
Connection has failed.
Definition connection.h:56
@ CONNECTION_STATE_HALTED
The connection is in a halted stat.
Definition connection.h:48
@ CONNECTION_STATE_CLOSED
Connection has been closed.
Definition connection.h:57
@ CONNECTION_STATE_CONNECTED
File descriptor is open (ready for writing).
Definition connection.h:54
@ CONNECTION_STATE_CONNECTING
Waiting for connection to establish.
Definition connection.h:52
@ CONNECTION_FAILED
Connection is being reconnected because it failed.
Definition connection.h:85
Holds a complete set of functions for a connection.
Definition connection.h:195
#define MEM(x)
Definition debug.h:36
#define ERROR(fmt,...)
Definition dhcpclient.c:41
#define DEBUG(fmt,...)
Definition dhcpclient.c:39
static fr_slen_t err
Definition dict.h:861
static fr_slen_t in
Definition dict.h:861
#define MODULE_MAGIC_INIT
Stop people using different module/library/server versions together.
Definition dl_module.h:63
#define fr_event_fd_insert(...)
Definition event.h:248
@ FR_EVENT_FILTER_IO
Combined filter for read/write functions/.
Definition event.h:84
#define unlang_function_repeat_set(_request, _repeat)
Set a new repeat function for an existing function frame.
Definition function.h:108
void unlang_interpret_mark_runnable(request_t *request)
Mark a request as resumable.
Definition interpret.c:1612
#define PERROR(_fmt,...)
Definition log.h:228
#define DEBUG3(_fmt,...)
Definition log.h:266
#define ROPTIONAL(_l_request, _l_global, _fmt,...)
Use different logging functions depending on whether request is NULL or not.
Definition log.h:528
#define RDEBUG3(fmt,...)
Definition log.h:343
#define RERROR(fmt,...)
Definition log.h:298
talloc_free(reap)
int fr_event_fd_delete(fr_event_list_t *el, int fd, fr_event_filter_t filter)
Remove a file descriptor from the event loop.
Definition event.c:1203
Stores all information relating to an event list.
Definition event.c:377
fr_log_type_t
Definition log.h:54
@ L_WARN
Warning.
Definition log.h:57
@ L_ERR
Error message.
Definition log.h:56
@ L_DBG
Only displayed when debugging is enabled.
Definition log.h:59
long int ssize_t
int strcasecmp(char *s1, char *s2)
Definition missing.c:66
module_instance_t * mi
Instance of the module being instantiated.
Definition module_ctx.h:51
Temporary structure to hold arguments for instantiation calls.
Definition module_ctx.h:50
static const conf_parser_t config[]
Definition base.c:186
#define fr_assert(_expr)
Definition rad_assert.h:38
#define RDEBUG2(fmt,...)
Definition radclient.h:54
#define DEBUG2(fmt,...)
Definition radclient.h:43
#define WARN(fmt,...)
Definition radclient.h:47
#define INFO(fmt,...)
Definition radict.c:54
#define RETURN_UNLANG_FAIL
Definition rcode.h:59
#define RETURN_UNLANG_OK
Definition rcode.h:60
static char const * name
Prototypes and functions for the SQL module.
fr_sql_query_status_t status
Status of the query.
Definition rlm_sql.h:144
trunk_connection_t * tconn
Trunk connection this query is being run on.
Definition rlm_sql.h:140
rlm_sql_t const * inst
Module instance for this query.
Definition rlm_sql.h:137
char const * query_str
Query string to run.
Definition rlm_sql.h:142
request_t * request
Request this query relates to.
Definition rlm_sql.h:138
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
#define RLM_SQL_RCODE_FLAGS_ALT_QUERY
Can distinguish between other errors and those resulting from a unique key violation.
Definition rlm_sql.h:171
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
trunk_request_t * treq
Trunk request for this query.
Definition rlm_sql.h:141
char const * connect_query
Query executed after establishing new connection.
Definition rlm_sql.h:98
@ SQL_QUERY_RETURNED
Query has executed.
Definition rlm_sql.h:130
@ SQL_QUERY_FETCHING_RESULTS
Fetching results from server.
Definition rlm_sql.h:131
@ SQL_QUERY_FAILED
Failed to submit.
Definition rlm_sql.h:127
@ SQL_QUERY_SUBMITTED
Submitted for execution.
Definition rlm_sql.h:129
@ SQL_QUERY_PREPARED
Ready to submit.
Definition rlm_sql.h:128
@ SQL_QUERY_RESULTS_FETCHED
Results fetched from the server.
Definition rlm_sql.h:132
Definition rlm_sql.h:61
static void sql_trunk_connection_notify(UNUSED trunk_connection_t *tconn, connection_t *conn, fr_event_list_t *el, trunk_connection_event_t notify_on, UNUSED void *uctx)
static void sql_request_fail(request_t *request, void *preq, UNUSED void *rctx, UNUSED trunk_request_state_t state, UNUSED void *uctx)
static sql_rcode_t sql_check_error(MYSQL *server, int client_errno)
Analyse the last error that occurred on the socket, and determine an action.
static int mod_load(void)
static sql_rcode_t sql_fields(char const **out[], fr_sql_query_t *query_ctx, UNUSED rlm_sql_config_t const *config)
bool tls_check_cert
Verify there's a trust relationship between the server's cert and one of the CAs we have configured.
char const * tls_certificate_file
Public certificate we present to the server.
fr_sql_query_t * query_ctx
Current query running on this connection.
static sql_rcode_t sql_free_result(fr_sql_query_t *, rlm_sql_config_t const *)
SQL_QUERY_FAIL static SQL_QUERY_RESUME unlang_action_t sql_select_query_resume(unlang_result_t *p_result, UNUSED request_t *request, void *uctx)
static void _sql_connection_close(fr_event_list_t *el, void *h, UNUSED void *uctx)
static fr_table_num_sorted_t const server_warnings_table[]
char const * tls_private_key_file
Private key for the certificate we present to the server.
char const * tls_crl_path
Private key for the certificate we present to the server.
char const * tls_crl_file
Public certificate we present to the server.
static unlang_action_t sql_fetch_row(unlang_result_t *p_result, UNUSED request_t *request, void *uctx)
int status
returned by the most recent non-blocking function call.
char const * tls_cipher
Colon separated list of TLS ciphers for TLS <= 1.2.
static void _sql_connect_io_notify(fr_event_list_t *el, int fd, UNUSED int flags, void *uctx)
Callback for I/O events in response to mysql_real_connect_start()
static ssize_t sql_escape_func(request_t *request, char *out, size_t outlen, char const *in, void *arg)
char const * tls_ca_path
Directory containing CAs that may be used to validate the servers certificate.
static void * sql_escape_arg_alloc(TALLOC_CTX *ctx, fr_event_list_t *el, void *uctx)
Allocate the argument used for the SQL escape function.
static void sql_trunk_request_demux(UNUSED fr_event_list_t *el, UNUSED trunk_connection_t *tconn, connection_t *conn, UNUSED void *uctx)
static connection_state_t _sql_connection_init(void **h, connection_t *conn, void *uctx)
static void sql_trunk_request_mux(UNUSED fr_event_list_t *el, trunk_connection_t *tconn, connection_t *conn, UNUSED void *uctx)
rlm_sql_mysql_warnings
@ SERVER_WARNINGS_AUTO
@ SERVER_WARNINGS_NO
@ SERVER_WARNINGS_YES
static void sql_escape_arg_free(void *uctx)
int fd
fd for this connection's I/O events.
static conf_parser_t tls_config[]
static void sql_request_cancel(connection_t *conn, void *preq, trunk_cancel_reason_t reason, UNUSED void *uctx)
rlm_sql_driver_t rlm_sql_mysql
MYSQL * sock
Connection details as returned by connection init functions.
static int sql_num_rows(fr_sql_query_t *query_ctx, UNUSED rlm_sql_config_t const *config)
MYSQL db
Structure representing connection details.
static size_t sql_warnings(TALLOC_CTX *ctx, sql_log_entry_t out[], size_t outlen, rlm_sql_mysql_conn_t *conn)
Retrieves any warnings associated with the last query.
static void mod_unload(void)
static void sql_request_cancel_mux(UNUSED fr_event_list_t *el, trunk_connection_t *tconn, connection_t *conn, UNUSED void *uctx)
static size_t sql_error(TALLOC_CTX *ctx, sql_log_entry_t out[], size_t outlen, fr_sql_query_t *query_ctx)
Retrieves any errors associated with the query context.
char const * character_set
Character set to use on connections.
char const * warnings_str
Whether we always query the server for additional warnings.
static const conf_parser_t driver_config[]
static sql_rcode_t sql_store_result(rlm_sql_mysql_conn_t *conn, UNUSED rlm_sql_config_t const *config)
static size_t server_warnings_table_len
static sql_rcode_t sql_finish_query(fr_sql_query_t *query_ctx, rlm_sql_config_t const *config)
Finish query.
static void _sql_connect_query_run(connection_t *conn, UNUSED connection_state_t prev, UNUSED connection_state_t state, void *uctx)
connection_t * conn
Generic connection structure for this connection.
bool tls_check_cert_cn
Verify that the CN in the server cert matches the host we passed to mysql_real_connect().
static int sql_affected_rows(fr_sql_query_t *query_ctx, UNUSED rlm_sql_config_t const *config)
char const * tls_ca_file
Path to the CA used to validate the server's certificate.
bool tls_required
Require that the connection is encrypted.
MYSQL_RES * result
Result from most recent query.
static int mod_instantiate(module_inst_ctx_t const *mctx)
rlm_sql_mysql_warnings warnings
mysql_warning_count() doesn't appear to work with NDB cluster
Macros to reduce boilerplate in trunk SQL drivers.
#define SQL_QUERY_RESUME
#define SQL_TRUNK_CONNECTION_ALLOC
Allocate an SQL trunk connection.
#define SQL_QUERY_FAIL
void connection_signal_halt(connection_t *conn)
Shuts down a connection ungracefully.
void connection_signal_reconnect(connection_t *conn, connection_reason_t reason)
Asynchronously signal the connection should be reconnected.
void connection_signal_init(connection_t *conn)
Asynchronously signal a halted connection to start.
connection_t * connection_alloc(TALLOC_CTX *ctx, fr_event_list_t *el, connection_funcs_t const *funcs, connection_conf_t const *conf, char const *log_prefix, void const *uctx)
Allocate a new connection.
connection_watch_entry_t * connection_add_watch_post(connection_t *conn, connection_state_t state, connection_watch_t watch, bool oneshot, void const *uctx)
Add a callback to be executed after a state function has been called.
Definition connection.c:534
void connection_signal_connected(connection_t *conn)
Asynchronously signal that the connection is open.
char const * name
Instance name e.g. user_database.
Definition module.h:355
size_t inst_size
Size of the module's instance data.
Definition module.h:212
void * data
Module's instance data.
Definition module.h:291
eap_aka_sim_process_conf_t * inst
fr_aka_sim_id_type_t type
module_t common
Common fields for all loadable modules.
Definition rlm_sql.h:201
module_instance_t * driver_submodule
Driver's submodule.
Definition rlm_sql.h:234
rlm_sql_config_t config
Definition rlm_sql.h:228
#define fr_table_value_by_str(_table, _name, _def)
Convert a string to a value using a sorted or ordered table.
Definition table.h:653
An element in a lexicographically sorted array of name to num mappings.
Definition table.h:49
char * talloc_typed_asprintf(TALLOC_CTX *ctx, char const *fmt,...)
Call talloc vasprintf, setting the type on the new chunk correctly.
Definition talloc.c:514
char * talloc_bstrndup(TALLOC_CTX *ctx, char const *in, size_t inlen)
Binary safe strndup function.
Definition talloc.c:586
#define talloc_get_type_abort_const
Definition talloc.h:287
static const char * names[8]
Definition time.c:584
int trunk_connection_pop_cancellation(trunk_request_t **treq_out, trunk_connection_t *tconn)
Pop a cancellation request off a connection's cancellation queue.
Definition trunk.c:3851
void trunk_request_signal_fail(trunk_request_t *treq)
Signal that a trunk request failed.
Definition trunk.c:2148
trunk_enqueue_t trunk_request_requeue(trunk_request_t *treq)
Re-enqueue a request on the same connection.
Definition trunk.c:2692
void trunk_request_signal_cancel_complete(trunk_request_t *treq)
Signal that a remote server acked our cancellation.
Definition trunk.c:2300
int trunk_connection_pop_request(trunk_request_t **treq_out, trunk_connection_t *tconn)
Pop a request off a connection's pending queue.
Definition trunk.c:3899
void trunk_request_signal_sent(trunk_request_t *treq)
Signal that the request was written to a connection successfully.
Definition trunk.c:2066
void trunk_request_signal_reapable(trunk_request_t *treq)
Signal that the request was written to a connection successfully, but no response is expected.
Definition trunk.c:2088
Associates request queues with a connection.
Definition trunk.c:133
Wraps a normal request.
Definition trunk.c:99
#define TRUNK_NOTIFY_FUNC(_name, _type)
Helper macro for building generic trunk notify callback.
Definition trunk.h:958
trunk_cancel_reason_t
Reasons for a request being cancelled.
Definition trunk.h:55
@ TRUNK_CANCEL_REASON_SIGNAL
Request cancelled due to a signal.
Definition trunk.h:57
@ TRUNK_REQUEST_STATE_REAPABLE
Request has been written, needs to persist, but we are not currently waiting for any response.
Definition trunk.h:173
@ TRUNK_REQUEST_STATE_COMPLETE
The request is complete.
Definition trunk.h:182
@ TRUNK_REQUEST_STATE_SENT
Was written to a socket. Waiting for a response.
Definition trunk.h:172
static fr_event_list_t * el
static size_t char fr_sbuff_t size_t inlen
Definition value.h:1023
static size_t char ** out
Definition value.h:1023