Skip to content

Commit

Permalink
use client_from_query in rlm_sql
Browse files Browse the repository at this point in the history
  • Loading branch information
arr2036 committed Jun 24, 2013
1 parent 0144763 commit c3463f5
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 89 deletions.
2 changes: 1 addition & 1 deletion src/include/radiusd.h
Expand Up @@ -548,7 +548,7 @@ void client_free(RADCLIENT *client);
int client_add(RADCLIENT_LIST *clients, RADCLIENT *client);
#ifdef WITH_DYNAMIC_CLIENTS
void client_delete(RADCLIENT_LIST *clients, RADCLIENT *client);
RADCLIENT *client_from_query(TALLOC_CTX *ctx, char const *identifier, char const *shortname, char const *secret,
RADCLIENT *client_from_query(TALLOC_CTX *ctx, char const *identifier, char const *secret, char const *shortname,
char const *type, char const *server, bool require_ma);
RADCLIENT *client_from_request(RADCLIENT_LIST *clients, REQUEST *request);
#endif
Expand Down
18 changes: 7 additions & 11 deletions src/main/client.c
Expand Up @@ -568,8 +568,7 @@ static RADCLIENT *client_parse(CONF_SECTION *cs, int in_server)

name2 = cf_section_name2(cs);
if (!name2) {
cf_log_err_cs(cs,
"Missing client name");
cf_log_err_cs(cs, "Missing client name");
return NULL;
}

Expand All @@ -584,8 +583,7 @@ static RADCLIENT *client_parse(CONF_SECTION *cs, int in_server)
c->prefix = -1;

if (cf_section_parse(cs, c, client_config) < 0) {
cf_log_err_cs(cs,
"Error parsing client section.");
cf_log_err_cs(cs, "Error parsing client section.");
error:
client_free(c);
#ifdef WITH_TCP
Expand Down Expand Up @@ -622,9 +620,7 @@ static RADCLIENT *client_parse(CONF_SECTION *cs, int in_server)
if (prefix_ptr) {
c->prefix = atoi(prefix_ptr + 1);
if ((c->prefix < 0) || (c->prefix > 128)) {
cf_log_err_cs(cs,
"Invalid Prefix value '%s' for IP.",
prefix_ptr + 1);
cf_log_err_cs(cs, "Invalid Prefix value '%s' for IP.", prefix_ptr + 1);
goto error;
}
/* Replace '/' with '\0' */
Expand Down Expand Up @@ -657,8 +653,8 @@ static RADCLIENT *client_parse(CONF_SECTION *cs, int in_server)
c->ipaddr.ipaddr.ip4addr = cl_ip4addr;

if ((c->prefix < -1) || (c->prefix > 32)) {
cf_log_err_cs(cs,
"Netmask must be between 0 and 32");
cf_log_err_cs(cs, "Netmask must be between 0 and 32");

goto error;
}

Expand Down Expand Up @@ -1048,14 +1044,14 @@ bool client_validate(RADCLIENT_LIST *clients, RADCLIENT *master, RADCLIENT *c)
*
* @param ctx Talloc context.
* @param identifier Client IP Address / IPv4 subnet / FQDN.
* @param shortname Client friendly name.
* @param secret Client secret.
* @param shortname Client friendly name.
* @param type NAS-Type.
* @param server Virtual-Server to associate clients with.
* @param require_ma If true all packets from client must include a message-authenticator.
* @return The new client, or NULL on error.
*/
RADCLIENT *client_from_query(TALLOC_CTX *ctx, char const *identifier, char const *shortname, char const *secret,
RADCLIENT *client_from_query(TALLOC_CTX *ctx, char const *identifier, char const *secret, char const *shortname,
char const *type, char const *server, bool require_ma)
{
RADCLIENT *c;
Expand Down
2 changes: 1 addition & 1 deletion src/modules/rlm_ldap/clients.c
Expand Up @@ -200,8 +200,8 @@ int rlm_ldap_load_clients(ldap_instance_t const *inst)
/* FIXME: We should really pass a proper ctx */
c = client_from_query(NULL,
identifier[0],
shortname ? shortname[0] : NULL,
secret[0],
shortname ? shortname[0] : NULL,
type ? type[0] : NULL,
server ? server[0] : NULL,
require_ma ? strncmp(require_ma[0], "true", 4) == 0 : false);
Expand Down
107 changes: 31 additions & 76 deletions src/modules/rlm_sql/rlm_sql.c
Expand Up @@ -247,10 +247,8 @@ static int generate_sql_clients(rlm_sql_t *inst)
{
rlm_sql_handle_t *handle;
rlm_sql_row_t row;
RADCLIENT *c;
char *prefix_ptr = NULL;
unsigned int i = 0;
int numf = 0;
RADCLIENT *c;

DEBUG("rlm_sql (%s): Processing generate_sql_clients",
inst->config->xlat_name);
Expand All @@ -259,17 +257,18 @@ static int generate_sql_clients(rlm_sql_t *inst)
inst->config->xlat_name, inst->config->nas_query);

handle = sql_get_socket(inst);
if (!handle)
if (!handle) {
return -1;
if (rlm_sql_select_query(&handle,inst, inst->config->nas_query)){
}

if (rlm_sql_select_query(&handle, inst, inst->config->nas_query)){
return -1;
}

while(rlm_sql_fetch_row(&handle, inst) == 0) {
while((rlm_sql_fetch_row(&handle, inst) == 0) && (row = handle->row)) {
char *server = NULL;
i++;
row = handle->row;
if (!row)
break;

/*
* The return data for each row MUST be in the following order:
*
Expand Down Expand Up @@ -297,81 +296,37 @@ static int generate_sql_clients(rlm_sql_t *inst)
continue;
}

DEBUG("rlm_sql (%s): Read entry nasname=%s,shortname=%s,secret=%s",inst->config->xlat_name,
row[1],row[2],row[4]);

c = talloc_zero(inst, RADCLIENT);

#ifdef WITH_DYNAMIC_CLIENTS
c->dynamic = 1;
#endif

/*
* Look for prefixes
*/
c->prefix = -1;
prefix_ptr = strchr(row[1], '/');
if (prefix_ptr) {
c->prefix = atoi(prefix_ptr + 1);
if ((c->prefix < 0) || (c->prefix > 128)) {
ERROR("rlm_sql (%s): Invalid Prefix value '%s' for IP.",
inst->config->xlat_name, prefix_ptr + 1);
talloc_free(c);
continue;
}
/* Replace '/' with '\0' */
*prefix_ptr = '\0';
if (((inst->module->sql_num_fields)(handle, inst->config) > 5) && (row[5] != NULL) && *row[5]) {
server = row[5];
}

/*
* Always get the numeric representation of IP
*/
if (ip_hton(row[1], AF_UNSPEC, &c->ipaddr) < 0) {
ERROR("rlm_sql (%s): Failed to look up hostname %s: %s",
inst->config->xlat_name,
row[1], fr_strerror());
talloc_free(c);
DEBUG("rlm_sql (%s): Adding client %s (%s) to %s clients list",
inst->config->xlat_name,
row[1], row[2], server ? server : "global");

/* FIXME: We should really pass a proper ctx */
c = client_from_query(NULL,
row[1], /* identifier */
row[4], /* secret */
row[2], /* shortname */
row[3], /* type */
server, /* server */
false); /* require message authenticator */
if (!c) {
continue;
} else {
char buffer[256];
ip_ntoh(&c->ipaddr, buffer, sizeof(buffer));
c->longname = talloc_strdup(c, buffer);
}

if (c->prefix < 0) switch (c->ipaddr.af) {
case AF_INET:
c->prefix = 32;
break;
case AF_INET6:
c->prefix = 128;
break;
default:
break;
}

/*
* Other values (secret, shortname, nastype, virtual_server)
*/
c->secret = talloc_strdup(c, row[4]);
c->shortname = talloc_strdup(c, row[2]);
if(row[3] != NULL)
c->nastype = strdup(row[3]);

numf = (inst->module->sql_num_fields)(handle, inst->config);
if ((numf > 5) && (row[5] != NULL) && *row[5]) c->server = strdup(row[5]);

DEBUG("rlm_sql (%s): Adding client %s (%s, server=%s) to clients list",
inst->config->xlat_name,
c->longname,c->shortname, c->server ? c->server : "<none>");

if (!client_add(NULL, c)) {
sql_release_socket(inst, handle);
DEBUG("rlm_sql (%s): Failed to add client %s (%s) to clients list. Maybe there's a duplicate?",
inst->config->xlat_name,
c->longname,c->shortname);
WARN("Failed to add client, possible duplicate?");

client_free(c);
return -1;
continue;
}

DEBUG("rlm_sql (%s): Client \"%s\" (%s) added", c->longname, c->shortname,
inst->config->xlat_name);
}

(inst->module->sql_finish_select_query)(handle, inst->config);
sql_release_socket(inst, handle);

Expand Down

0 comments on commit c3463f5

Please sign in to comment.