From: Alan T. DeKok Date: Fri, 10 Aug 2012 10:45:37 +0000 (+0200) Subject: Clean up SQL "tracefile" code X-Git-Tag: release_3_0_0_beta0~98 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a24cdcb8dbecfff36010db807ae1a41bf4488623;p=thirdparty%2Ffreeradius-server.git Clean up SQL "tracefile" code There is now just a "logfile" configuration. There is no need to have "trace=yes" and "tracefile". Since the logfile also locks the file, we can probably get rid of the sql_log module. Also document the "logfile" directive in the dialup.conf files --- diff --git a/raddb/sql/mssql/dialup.conf b/raddb/sql/mssql/dialup.conf index 6bedf69a99f..1c3e295bc27 100644 --- a/raddb/sql/mssql/dialup.conf +++ b/raddb/sql/mssql/dialup.conf @@ -72,6 +72,13 @@ accounting { reference = "%{tolower:type.%{Acct-Status-Type}.query}" + # All SQL statements are logged to this file. + # This file is preferred to the "logfile" set in + # the mods-enabled/sql file. The filename is dynamically + # expanded at run time, so you can use %H, etc., just + # as with the detail file. +# logfile = ${logdir}/accounting.sql + type { accounting-on { query = "\ diff --git a/raddb/sql/mysql/dialup.conf b/raddb/sql/mysql/dialup.conf index e87e7d728a9..a5b445ede9c 100644 --- a/raddb/sql/mysql/dialup.conf +++ b/raddb/sql/mysql/dialup.conf @@ -187,6 +187,13 @@ accounting { reference = "%{tolower:type.%{Acct-Status-Type}.query}" + # All SQL statements are logged to this file. + # This file is preferred to the "logfile" set in + # the mods-enabled/sql file. The filename is dynamically + # expanded at run time, so you can use %H, etc., just + # as with the detail file. +# logfile = ${logdir}/accounting.sql + column_list = "\ acctsessionid, acctuniqueid, username, \ realm, nasipaddress, nasportid, \ diff --git a/raddb/sql/oracle/dialup.conf b/raddb/sql/oracle/dialup.conf index 76fc6eccead..36d7a1dccf2 100644 --- a/raddb/sql/oracle/dialup.conf +++ b/raddb/sql/oracle/dialup.conf @@ -134,6 +134,13 @@ accounting { reference = "%{tolower:type.%{Acct-Status-Type}.query}" + # All SQL statements are logged to this file. + # This file is preferred to the "logfile" set in + # the mods-enabled/sql file. The filename is dynamically + # expanded at run time, so you can use %H, etc., just + # as with the detail file. +# logfile = ${logdir}/accounting.sql + type { accounting-on { query = "\ @@ -329,4 +336,4 @@ '%{%{User-Password}:-%{Chap-Password}}', \ '%{reply:Packet-Type}', \ TO_TIMESTAMP('%S','YYYY-MM-DDHH24:MI:SS'))" - } \ No newline at end of file + } diff --git a/raddb/sql/postgresql/dialup.conf b/raddb/sql/postgresql/dialup.conf index b235a967738..a214e9f12fe 100644 --- a/raddb/sql/postgresql/dialup.conf +++ b/raddb/sql/postgresql/dialup.conf @@ -165,6 +165,13 @@ accounting { reference = "%{tolower:type.%{Acct-Status-Type}.query}" + # All SQL statements are logged to this file. + # This file is preferred to the "logfile" set in + # the mods-enabled/sql file. The filename is dynamically + # expanded at run time, so you can use %H, etc., just + # as with the detail file. +# logfile = ${logdir}/accounting.sql + type { accounting-on { query = "\ diff --git a/src/modules/rlm_sql/rlm_sql.h b/src/modules/rlm_sql/rlm_sql.h index 1a6bbad15d0..9fbaa78bf15 100644 --- a/src/modules/rlm_sql/rlm_sql.h +++ b/src/modules/rlm_sql/rlm_sql.h @@ -30,8 +30,7 @@ typedef struct rlm_sql_config_section { char *reference; - int sqltrace; - char *tracefile; + char *logfile; } rlm_sql_config_section_t; typedef struct sql_config { @@ -52,10 +51,9 @@ typedef struct sql_config { char *simul_count_query; char *simul_verify_query; char *groupmemb_query; - int sqltrace; - int do_clients; - int read_groups; - char *tracefile; + int do_clients; + int read_groups; + char *logfile; char *xlat_name; int deletestalesessions; char *allowed_chars; diff --git a/src/modules/rlm_sql/sql.c b/src/modules/rlm_sql/sql.c index d6d46f3c2e7..e3a5d234c48 100644 --- a/src/modules/rlm_sql/sql.c +++ b/src/modules/rlm_sql/sql.c @@ -405,35 +405,39 @@ int sql_getvpdata(SQL_INST * inst, SQLSOCK **sqlsocket, VALUE_PAIR **pair, char return rows; } +/* + * Log the query to a file. + */ void query_log(SQL_INST *inst, REQUEST *request, - rlm_sql_config_section_t *section, char *querystr) + rlm_sql_config_section_t *section, char *query) { - FILE *sqlfile = NULL; - - if (!inst->config->sqltrace && (!section || !section->sqltrace)) - return; - + int fd; + const char *filename; char buffer[8192]; - if (!radius_xlat(buffer, sizeof(buffer), - section && section->tracefile ? section->tracefile : - inst->config->tracefile, - request, NULL)) { - radlog(L_ERR, "rlm_sql (%s): xlat failed.", - inst->config->xlat_name); - return; + if (section) { + filename = section->logfile; + } else { + filename = inst->config->logfile; } - if ((sqlfile = fopen(buffer, "a")) == (FILE *) NULL) { - radlog(L_ERR, "rlm_sql (%s): Couldn't open file %s", - inst->config->xlat_name, - buffer); - } else { - int fd = fileno(sqlfile); + if (!filename) return; + + if (!radius_xlat(buffer, sizeof(buffer), filename, request, NULL)) { + radlog(L_ERR, "rlm_sql (%s): xlat failed.", + inst->config->xlat_name); + return; + } - rad_lockfd(fd, MAX_QUERY_LEN); - fputs(querystr, sqlfile); - fputs(";\n", sqlfile); - fclose(sqlfile); /* and release the lock */ + fd = open(filename, O_WRONLY | O_APPEND | O_CREAT, 0666); + if (fd < 0) { + radlog(L_ERR, "rlm_sql (%s): Couldn't open file %s: %s", + inst->config->xlat_name, buffer, strerror(errno)); + return; } + + rad_lockfd(fd, MAX_QUERY_LEN); + write(fd, query, strlen(query)); + write(fd, ";\n", 2); + close(fd); /* and release the lock */ }