From: Arran Cudbard-Bell Date: Fri, 25 May 2018 09:49:58 +0000 (+0600) Subject: Missed cf_pair_in_table function X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1d3409ba7c8cc657a9bd2a162d91d34faccb8fbb;p=thirdparty%2Ffreeradius-server.git Missed cf_pair_in_table function --- diff --git a/src/include/token.h b/src/include/token.h index 7576d7f734b..b1cf2f304ca 100644 --- a/src/include/token.h +++ b/src/include/token.h @@ -70,6 +70,11 @@ typedef struct FR_NAME_NUMBER { int32_t number; } FR_NAME_NUMBER; +/** Macro to use as dflt + * + */ +#define FR_NAME_NUMBER_NOT_FOUND INT32_MIN + extern const FR_NAME_NUMBER fr_tokens_table[]; extern const FR_NAME_NUMBER fr_token_quotes_table[]; extern const char *fr_tokens[]; diff --git a/src/main/cf_util.c b/src/main/cf_util.c index 06b3583b354..8660c75d87e 100644 --- a/src/main/cf_util.c +++ b/src/main/cf_util.c @@ -1629,6 +1629,48 @@ static inline void truncate_filename(char const **e, char const **p, int *len, c *e = "..."; } +/** Check to see if the CONF_PAIR value is present in the specified table + * + * If it's not present, return an error and produce a helpful log message + * + * @param[out] out The result of parsing the pair value. + * @param[in] table to look for string values in. + * @param[in] cp to parse. + * @return + * - 0 on success. + * - -1 on failure. + */ +int cf_pair_in_table(int32_t *out, FR_NAME_NUMBER const *table, CONF_PAIR *cp) +{ + FR_NAME_NUMBER const *t_p; + char *list = NULL; + int32_t res; + + res = fr_str2int(table, cf_pair_value(cp), FR_NAME_NUMBER_NOT_FOUND); + if (res != FR_NAME_NUMBER_NOT_FOUND) { + *out = res; + return 0; + } + + for (t_p = table; t_p->name != NULL; t_p++) { + MEM(list = talloc_asprintf_append_buffer(list, "'%s', ", t_p->name)); + } + + if (!list) { + cf_log_err(cp, "Internal error parsing %s: Table was empty", cf_pair_attr(cp)); + return -1; + } + + /* + * Trim the final ", " + */ + MEM(list = talloc_realloc_bstr(list, talloc_array_length(list) - 3)); + + cf_log_err(cp, "Invalid value \"%s\". Expected one of %s", cf_pair_value(cp), list); + + return -1; +} + /** Log an error message relating to a #CONF_ITEM * * @param[in] ci #CONF_ITEM to print file/lineno for.