]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Allow dictionary root dir to be manipulated at runtime
authorArran Cudbard-Bell <a.cudbardb@freeradius.org>
Tue, 5 Nov 2019 01:36:25 +0000 (19:36 -0600)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Tue, 5 Nov 2019 01:39:07 +0000 (19:39 -0600)
This is to allow us to load test dictionaries

src/lib/util/dict.h
src/lib/util/dict_tokenize.c
src/lib/util/dict_util.c

index 41bd8d2469eb70748bc7cefb562bba31c5ec0fbb..c240c86e97544a1780cdb16932e4d88f65ef4c8c 100644 (file)
@@ -414,13 +414,21 @@ void                      fr_dl_dict_autofree(dl_t const *module, void *symbol, void *user_ctx);
 int                    fr_dl_dict_attr_autoload(dl_t const *module, void *symbol, void *user_ctx);
 /** @} */
 
+/** @name Allocating and freeing
+ *
+ * @{
+ */
 void                   fr_dict_free(fr_dict_t **dict);
 
+/** @} */
+
 /** @name Initialisation
  *
  * @{
  */
  int                   fr_dict_global_init(TALLOC_CTX *ctx, char const *dict_dir);
+
+ int                   fr_dict_dir_set(char const *dict_dir);
 /** @} */
 
 /** @name Dictionary testing and validation
index 3aabb7ffc8e85ba403641b729e2e97d346dd25b0..017587f94ae52fd20e3c83c96c17a732517d7865 100644 (file)
@@ -2406,7 +2406,7 @@ int fr_dict_read(fr_dict_t *dict, char const *dir, char const *filename)
        INTERNAL_IF_NULL(dict, -1);
 
        if (!dict->attributes_by_name) {
-               fr_strerror_printf("%s: Must call fr_dict_internal_afrom_file() before fr_dict_read()", __FUNCTION__);
+               fr_strerror_printf("%s: Must initialise dictionary before calling fr_dict_read()", __FUNCTION__);
                return -1;
        }
 
index ee92e16963bf230209cb46b0b1f898d53cc37dbd..a7fe0854fa608bd18fc142a40653e87a4cecdf4f 100644 (file)
@@ -2472,7 +2472,7 @@ static int dict_onload_func(dl_t const *dl, void *symbol, UNUSED void *user_ctx)
  *
  * @note Must be called before any other dictionary functions.
  *
- * @param[in] ctx      to allocate protocol hashes in.
+ * @param[in] ctx      to allocate global resources in.
  * @param[in] dict_dir the default location for the dictionaries.
  * @return
  *     - 0 on success.
@@ -2480,8 +2480,11 @@ static int dict_onload_func(dl_t const *dl, void *symbol, UNUSED void *user_ctx)
  */
 int fr_dict_global_init(TALLOC_CTX *ctx, char const *dict_dir)
 {
+       TALLOC_FREE(dict_ctx);
+       dict_ctx = ctx;
+
        if (!protocol_by_name) {
-               protocol_by_name = fr_hash_table_create(ctx, dict_protocol_name_hash, dict_protocol_name_cmp, NULL);
+               protocol_by_name = fr_hash_table_create(dict_ctx, dict_protocol_name_hash, dict_protocol_name_cmp, NULL);
                if (!protocol_by_name) {
                        fr_strerror_printf("Failed initializing protocol_by_name hash");
                        return -1;
@@ -2489,7 +2492,7 @@ int fr_dict_global_init(TALLOC_CTX *ctx, char const *dict_dir)
        }
 
        if (!protocol_by_num) {
-               protocol_by_num = fr_hash_table_create(ctx, dict_protocol_num_hash, dict_protocol_num_cmp, NULL);
+               protocol_by_num = fr_hash_table_create(dict_ctx, dict_protocol_num_hash, dict_protocol_num_cmp, NULL);
                if (!protocol_by_num) {
                        fr_strerror_printf("Failed initializing protocol_by_num hash");
                        return -1;
@@ -2497,7 +2500,7 @@ int fr_dict_global_init(TALLOC_CTX *ctx, char const *dict_dir)
        }
 
        talloc_free(dict_dir_default);          /* Free previous value */
-       dict_dir_default = talloc_strdup(ctx, dict_dir);
+       dict_dir_default = talloc_strdup(dict_ctx, dict_dir);
 
        dict_loader = dl_loader_init(ctx, NULL, NULL, false, false);
        if (!dict_loader) return -1;
@@ -2511,6 +2514,22 @@ int fr_dict_global_init(TALLOC_CTX *ctx, char const *dict_dir)
        return 0;
 }
 
+/** Allow the default dict dir to be changed after initialisation
+ *
+ * @param[in] dict_dir New default dict dir to use.
+ * @return
+ *     - 0 on success.
+ *     - -1 on failure.
+ */
+int fr_dict_dir_set(char const *dict_dir)
+{
+       talloc_free(dict_dir_default);          /* Free previous value */
+       dict_dir_default = talloc_strdup(dict_ctx, dict_dir);
+       if (!dict_dir_default) return -1;
+
+       return 0;
+}
+
 /*
  *     [a-zA-Z0-9_-:.]+
  */