]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
add function which converts one map_t to fr_cond_t
authorAlan T. DeKok <aland@freeradius.org>
Wed, 16 Dec 2020 15:51:47 +0000 (10:51 -0500)
committerAlan T. DeKok <aland@freeradius.org>
Wed, 16 Dec 2020 15:51:47 +0000 (10:51 -0500)
in preparation for using it in rlm_files

src/lib/server/cond.h
src/lib/server/cond_tokenize.c

index 4d526496885f5b8aa383276e998ddabf4bf18ccc..582e89ddfcbed39d73c3f61d2c6207cd39705af6 100644 (file)
@@ -109,6 +109,8 @@ ssize_t     cond_print(fr_sbuff_t *out, fr_cond_t const *c);
 
 bool fr_cond_walk(fr_cond_t *head, bool (*callback)(fr_cond_t *cond, void *uctx), void *uctx);
 
+int fr_cond_from_map(TALLOC_CTX *ctx, fr_cond_t **head, map_t *map);
+
 #ifdef __cplusplus
 }
 #endif
index a78a753e34d8fa0d3a6e652650cc3dd9e150ced6..0794a81e25d0f43ff3c8bf4b614ca876fc2e979a 100644 (file)
@@ -1684,3 +1684,35 @@ bool fr_cond_walk(fr_cond_t *c, bool (*callback)(fr_cond_t *cond, void *uctx), v
 
        return true;
 }
+
+/** Convert a single map to a condition.
+ *
+ * @param ctx  the talloc context where the condition is allocated
+ * @param[out] head the newly allocated condition.  Should only be one!
+ * @param[in]  map the map to convert. MAY be freed.
+ * @return
+ *     - <0 on error "map" is untouched.
+ *     - 0 on success - "map" MAY be freed
+ */
+int fr_cond_from_map(TALLOC_CTX *ctx, fr_cond_t **head, map_t *map)
+{
+       fr_cond_t *cond = talloc_zero(ctx, fr_cond_t);
+
+       if (!cond) return -1;
+
+       cond->type = COND_TYPE_MAP;
+       cond->data.map = map;
+
+       if (cond_normalise(ctx, T_BARE_WORD, &cond) < 0) return -1;
+
+       /*
+        *      If the condition is still a MAP, then make the map
+        *      owned by the condition.
+        */
+       if (cond->type == COND_TYPE_MAP) {
+               (void) talloc_steal(cond, map);
+       }
+
+       *head = cond;
+       return 0;
+}