如何在 FreeRADIUS 中的 C 模块回调之间传递任意数据

How to pass arbitrary data between the C module callbacks in FreeRADIUS

什么是 proper/recommended 在 FreeRADIUS 的 C 模块中的回调之间传递数据的方法?

例如,我想为请求创建一个唯一的 request_id 并将其用于该请求期间的所有日志条目。如果我在 mod_authorize 中创建这个值,我如何将它传递给同一请求线程上的 mod_authenticate,我如何检索它?

static rlm_rcode_t CC_HINT(nonnull) mod_authorize(void *instance, REQUEST *request)
{
    // Generate uuid
    uuid_t uuid;
    uuid_generate_random(uuid);

    // Convert to a string representation
    char *request_id = talloc_array(mem_ctx, char, UUID_STR_LEN);
    uuid_unparse(uuid, request_id);

    // Do stuff and log authorize messages
    radlog(L_INFO, "request_id inside mod_authorize: %s", request_id);

    // How do I pass request_id to mod_authenticate callback
    // ?????????????

    return RLM_MODULE_OK;
}

static rlm_rcode_t CC_HINT(nonnull) mod_authenticate(void *instance, REQUEST *request)
{
    char *request_id = NULL;

    // How do I retrieve the request_id value
    // ???????????????????

    // Do stuff and log authenticate messages
    radlog(L_INFO, "request_id inside mod_authenticate: %s", request_id);

    return RLM_MODULE_OK;
}

将值附加到请求对象似乎是合乎逻辑的事情,但我没有看到这样做的方法,除了将值对添加到请求->回复(而且我不想return 这个值到 NAS)。

谢谢。

显然,有一系列“用于本地存储的临时属性”(在 dictionary.freeradius.internal 文件中定义)可用于请求对象的集合之一(request->config、request- >回复->vps 和请求->数据包->vps)。您可以通过在 FreeRADIUS 存储库中搜索 dictionary.freeradius.internal 文件找到此范围的起点

ATTRIBUTE   Tmp-String-0

在这种情况下,我发现 request->packet->vps 是合适的,并使用 Tmp-String-3 在 MOD_AUTHORIZE 回调中将我的 request_id 添加到其中:

pair_make_request("Tmp-String-3", request_ctx->request_id, T_OP_EQ);

其中 pair_make_request 是定义为

的宏
fr_pair_make(request->packet, &request->packet->vps, _a, _b, _c)

然后我检索了它,而在 MOD_AUTHENTICATE 回调中:

VALUE_PAIR *vp = fr_pair_find_by_num(request->packet->vps, PW_TMP_STRING_3, 0, TAG_ANY);

这些属性的数值因版本而异,必须使用宏定义代替

这些属性的宏,例如上面示例中的 PW_TMP_STRING_3,位于构建期间自动生成的文件“attributes.h”中。这是来自 Arran Cudbard-Bell, that I found here 的引述:

If you really want to know where each one is used, download, configure, build the source. Then see src/include/attributes.h for the macro versions, and grep -r through the code. That'll at least tell you the modules, and if you're familiar with C you should be able to figure out how/when they're added or checked for. – Arran Cudbard-Bell Apr 12 '15 at 20:51

在我的例子中,生成的文件位于 /usr/include/freeradius/attributes.h

我必须说我花了不合理的努力来追踪这些信息。这些属性宏没有任何其他踪迹,none。不在代码中,不在 FreeRADIUS 文档中,不在 Google 搜索结果中。