44#include <radcli/radcli.h>
45#include <radcli/radcli2.h>
72 rh = radcli2_priv_new();
77 rh = radcli2_priv_config_init(rh);
82 radcli2_priv_load_builtin_dict(rh) != 0) {
83 radcli2_priv_destroy(rh);
131 return radcli2_priv_read_dictionary(ctx, path);
150 return radcli2_priv_read_dictionary_from_buffer(ctx, buf, size);
166 return radcli2_priv_apply_config(ctx);
176 radcli2_priv_destroy(ctx);
190static const OPTION *radcli_opt_lookup(
const radcli_ctx *ctx,
radcli_opt_id opt)
192 if (ctx == NULL || ctx->config_options == NULL || (
unsigned)opt >= OPT_COUNT)
194 return &ctx->config_options[opt];
214static int radcli_opt_already_set(
const OPTION *o)
218 if (o->type & RADCLI_OPT_TYPE_SRV)
222 return ((SERVER *)o->val)->max > 0;
244 const OPTION *o = radcli_opt_lookup(ctx, opt);
246 if (o == NULL || !(o->type & (RADCLI_OPT_TYPE_STR | RADCLI_OPT_TYPE_SRV)))
249 if (radcli_opt_already_set(o)) {
250 rc_log(LOG_ERR,
"radcli_ctx_set_opt_str: %s is already set", o->name);
254 if ((o->type & RADCLI_OPT_TYPE_SRV) && val != NULL && strpbrk(val,
", \t") != NULL) {
259 rc_log(LOG_ERR,
"radcli_ctx_set_opt_str: %s must name a single "
260 "server, not a list", o->name);
264 return radcli2_priv_add_config(ctx, o->name, val,
"radcli_ctx_set_opt_str", 0);
276 const OPTION *o = radcli_opt_lookup(ctx, opt);
279 if (o == NULL || !(o->type & RADCLI_OPT_TYPE_INT))
282 if (radcli_opt_already_set(o)) {
283 rc_log(LOG_ERR,
"radcli_ctx_set_opt_int: %s is already set", o->name);
287 snprintf(buf,
sizeof(buf),
"%ld", val);
288 return radcli2_priv_add_config(ctx, o->name, buf,
"radcli_ctx_set_opt_int", 0);
305 const OPTION *o = radcli_opt_lookup(ctx, opt);
307 if (o == NULL || !(o->type & RADCLI_OPT_TYPE_STR))
309 return (
const char *)o->val;
322 const OPTION *o = radcli_opt_lookup(ctx, opt);
324 if (o == NULL || !(o->type & RADCLI_OPT_TYPE_INT) || out == NULL || o->val == NULL)
327 *out = *((
int *)o->val);
341static int radcli_set_one_secret(radcli_ctx *ctx,
const char *optname,
const char *secret)
343 SERVER *serv = radcli2_priv_conf_srv(ctx, optname);
346 if (serv == NULL || serv->max == 0)
349 dup = strdup(secret);
353 free(serv->secret[0]);
354 serv->secret[0] = dup;
382 if (ctx == NULL || secret == NULL || target_mask == 0 ||
387 ret |= radcli_set_one_secret(ctx,
"authserver", secret);
389 ret |= radcli_set_one_secret(ctx,
"acctserver", secret);
391 return ret == 0 ? 0 : -1;
418 const void *identity,
size_t identity_len,
419 const uint8_t *key,
size_t keylen)
424 if (ctx == NULL || identity == NULL || key == NULL || keylen == 0)
427 id_copy = malloc(identity_len + 1);
430 memcpy(id_copy, identity, identity_len);
431 id_copy[identity_len] =
'\0';
433 key_copy = malloc(keylen);
434 if (key_copy == NULL) {
438 memcpy(key_copy, key, keylen);
440 free(ctx->tls_psk_identity);
441 free(ctx->tls_psk_key);
442 ctx->tls_psk_identity = id_copy;
443 ctx->tls_psk_key = key_copy;
444 ctx->tls_psk_key_len = keylen;
radcli_ctx * radcli_ctx_new(unsigned flags)
Create an empty context, ready for radcli_ctx_set_opt_str()/ _set_opt_int() and radcli_ctx_apply() – ...
int radcli_ctx_set_opt_str(radcli_ctx *ctx, radcli_opt_id opt, const char *val)
Set a string-typed configuration option.
const char * radcli_ctx_get_opt_str(const radcli_ctx *ctx, radcli_opt_id opt)
Read back a string-typed configuration option.
int radcli_ctx_set_opt_int(radcli_ctx *ctx, radcli_opt_id opt, long val)
Set an integer-typed configuration option.
int radcli_ctx_apply(radcli_ctx *ctx)
Validate the options set so far and initialise the transport.
int radcli_ctx_read_dictionary(radcli_ctx *ctx, const char *path)
Load an additional attribute dictionary.
radcli_ctx * radcli_ctx_read_config(const char *filename, unsigned flags)
Create a context by parsing a config file – the main, recommended way to configure radcli.
void radcli_ctx_free(radcli_ctx *ctx)
Release a context.
int radcli_ctx_read_dictionary_from_buffer(radcli_ctx *ctx, const char *buf, size_t size)
Load an additional attribute dictionary from an in-memory buffer.
int radcli_ctx_set_tls_psk(radcli_ctx *ctx, const void *identity, size_t identity_len, const uint8_t *key, size_t keylen)
Set the RFC 6614/7360 TLS-transport Pre-Shared Key credentials for the configured authserver.
int radcli_ctx_set_secret(radcli_ctx *ctx, unsigned target_mask, const char *secret)
Set the RADIUS shared secret for the configured authserver and/or acctserver.
int radcli_ctx_get_opt_int(const radcli_ctx *ctx, radcli_opt_id opt, long *out)
Read back an integer-typed configuration option.
@ RADCLI_SECRET_AUTH
Applies to the configured authserver (Access-Request).
@ RADCLI_SECRET_ACCT
Applies to the configured acctserver (Accounting-Request).
@ RADCLI_CTX_NO_BUILTIN_DICT
Skip loading the built-in RFC 2865/2866/2869 dictionary.