Skip to content

Commit

Permalink
Add new "utf8" option to rlm_sql_log.
Browse files Browse the repository at this point in the history
This allows to pass through utf8 characters (disabled by default)
  • Loading branch information
sYc committed May 12, 2009
1 parent c03a241 commit 769d356
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
1 change: 1 addition & 0 deletions raddb/modules/sql_log
Expand Up @@ -23,6 +23,7 @@ sql_log {
acct_table = "radacct"
postauth_table = "radpostauth"
sql_user_name = "%{%{User-Name}:-DEFAULT}"
utf8 = no

Start = "INSERT INTO ${acct_table} (AcctSessionId, UserName, \
NASIPAddress, FramedIPAddress, AcctStartTime, AcctStopTime, \
Expand Down
73 changes: 72 additions & 1 deletion src/modules/rlm_sql_log/rlm_sql_log.c
Expand Up @@ -47,6 +47,7 @@ typedef struct rlm_sql_log_t {
char *path;
char *postauth_query;
char *sql_user_name;
int utf8;
char *allowed_chars;
CONF_SECTION *conf_section;
} rlm_sql_log_t;
Expand All @@ -61,6 +62,8 @@ static const CONF_PARSER module_config[] = {
offsetof(rlm_sql_log_t,postauth_query), NULL, ""},
{"sql_user_name", PW_TYPE_STRING_PTR,
offsetof(rlm_sql_log_t,sql_user_name), NULL, ""},
{"utf8", PW_TYPE_BOOLEAN,
offsetof(rlm_sql_log_t,utf8), NULL, "no"},
{"safe-characters", PW_TYPE_STRING_PTR,
offsetof(rlm_sql_log_t,allowed_chars), NULL,
"@abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_: /"},
Expand Down Expand Up @@ -193,6 +196,71 @@ static size_t sql_escape_func(char *out, size_t outlen, const char *in)
return len;
}

static size_t sql_utf8_escape_func(char *out, size_t outlen, const char *in)
{
int len = 0;
int utf8 = 0;

while (in[0]) {
/*
* Skip over UTF8 characters
*/
utf8 = utf8_char((uint8_t *)in);
if (utf8) {
if (outlen <= utf8) {
break;
}
while (utf8-- > 0) {
*out = *in;
out++;
in++;
outlen--;
len++;
}
continue;
}

/*
* Non-printable characters get replaced with their
* mime-encoded equivalents.
*/
if ((in[0] < 32) ||
strchr(allowed_chars, *in) == NULL) {
/*
* Only 3 or less bytes available.
*/
if (outlen <= 3) {
break;
}

snprintf(out, outlen, "=%02X", (unsigned char) in[0]);
in++;
out += 3;
outlen -= 3;
len += 3;
continue;
}

/*
* Only one byte left.
*/
if (outlen <= 1) {
break;
}

/*
* Allowed character.
*/
*out = *in;
out++;
in++;
outlen--;
len++;
}
*out = '\0';
return len;
}

/*
* Add the 'SQL-User-Name' attribute to the packet.
*/
Expand Down Expand Up @@ -254,7 +322,10 @@ static int sql_xlat_query(rlm_sql_log_t *inst, REQUEST *request, const char *que

/* Expand variables in the query */
xlat_query[0] = '\0';
radius_xlat(xlat_query, len, query, request, sql_escape_func);
if (inst->utf8)
radius_xlat(xlat_query, len, query, request, sql_utf8_escape_func);
else
radius_xlat(xlat_query, len, query, request, sql_escape_func);
if (xlat_query[0] == '\0') {
radlog_request(L_ERR, 0, request, "Couldn't xlat the query %s",
query);
Expand Down

0 comments on commit 769d356

Please sign in to comment.