编写一个认证插件的 st_mysql_auth 结构体

解读

MariaDB 的认证流程在 10.4 之后全部插件化,st_mysql_auth 是服务器与插件之间的唯一契约。面试官通过让你手写这个结构体,考察三点:

  1. 是否熟记 MariaDB 10.6 源码 plugin/auth/auth_plugin.h 里对字段顺序、类型、语义的最细粒度要求;
  2. 是否理解 认证插件生命周期(init → authenticate_user → deinit)与内存所有权边界;
  3. 能否把国内合规场景(国密 SM2、双向 TLS、审计留痕)映射到结构体回调,体现“金融级”思维。
    只要漏掉 version 或 authenticate_user 签名对不上,编译期直接报错;若把 generate_authentication_string 写成同步哈希而没考虑加盐,则会被现场追问“如何过等保 2.0”。

知识点

  1. st_mysql_auth 版本号:MariaDB 10.6 必须填 0x0103,低于此值服务器拒绝加载。
  2. 关键函数指针:
    • authenticate_user:核心回调,返回值 0 表示认证通过,非 0 必须填充 error_msg 供审计插件落盘;
    • generate_authentication_string:用于 ALTER USER 时生成密文,必须自己 malloc 返回,服务器负责 free;
    • validate_authentication_string:做 CREATE USER 语法校验,防止弱口令写入 mysql.user;
    • set_salt:若插件需要随机盐,盐长度 ≤ 20 字节,服务器会把它先写到握手包。
  3. 内存与线程安全:authenticate_user 会跑在 线程池 worker 线程,所有插件内部缓存须用 my_malloc(PSI_NOT_INSTRUMENTED) 并加原子变量。
  4. 国内合规细节:
    • 若对接国密,generate_authentication_string 里调用 SM3_SM2 哈希后,需把公钥指纹写进 authentication_string 列,方便等保测评抽样;
    • 审计字段:在 authenticate_user 里通过 my_error_printf_service 输出 “client_ip|user|succ” 格式,与公安部 ELK 规则对齐。

答案

#include <mysql/plugin_auth.h>
#include <mysql/service_my_plugin_log.h>
#include <string.h>
#include <openssl/evp.h>

static int sm3_sm2_auth_init(MYSQL_PLUGIN plugin_info)
{
    /* 国密算法全局初始化,仅一次 */
    OpenSSL_add_all_algorithms();
    return 0;
}

static int sm3_sm2_auth_deinit(MYSQL_PLUGIN plugin_info)
{
    EVP_cleanup();
    return 0;
}

static int sm3_sm2_authenticate(MYSQL_PLUGIN_VIO *vio,
                                MYSQL_SERVER_AUTH_INFO *info)
{
    unsigned char client_hash[EVP_MAX_MD_SIZE];
    unsigned char server_hash[EVP_MAX_MD_SIZE];
    unsigned int  md_len;

    /* 1. 读客户端发来的 32 字节 SM3 哈希 */
    if (vio->read_packet(vio, (unsigned char**)&client_hash, &md_len) || md_len != 32)
        return CR_ERROR;

    /* 2. 用 mysql.user.authentication_string 里的公钥对 client_hash 验签 */
    /* 此处省略 SM2 验签细节,返回 0 表示验签成功 */
    int rc = sm2_verify_signature(info->auth_string, client_hash);
    if (rc != 0) {
        strcpy(info->status_info, "SM2 signature verify failed");
        return CR_ERROR;
    }

    /* 3. 审计留痕 */
    my_plugin_log_message(&plugin_info, MY_INFORMATION_LEVEL,
                          "sm3_sm2_auth: user=%s, host=%s, ip=%s, result=OK",
                          info->user_name, info->host_or_ip, info->host_or_ip);
    return CR_OK;
}

static int sm3_sm2_gen_auth_string(char *outbuf,
                                   unsigned int *buflen,
                                   const char *password,
                                   unsigned int password_len)
{
    /* 生成 SM3(password+salt) 并拼成 hex 返回 */
    unsigned char salt[12];
    my_random_bytes(salt, sizeof(salt));

    unsigned char hash[32];
    EVP_MD_CTX *ctx = EVP_MD_CTX_new();
    EVP_DigestInit_ex(ctx, EVP_sm3(), NULL);
    EVP_DigestUpdate(ctx, password, password_len);
    EVP_DigestUpdate(ctx, salt, sizeof(salt));
    EVP_DigestFinal_ex(ctx, hash, NULL);
    EVP_MD_CTX_free(ctx);

    /* 输出格式:$SM3$<hex_salt>$<hex_hash> */
    int needed = 1 + 3 + 1 + 2 * sizeof(salt) + 1 + 2 * 32 + 1;
    if (*buflen < needed) {
        *buflen = needed;
        return 1; /* 告诉服务器缓冲区不够 */
    }
    sprintf(outbuf, "$SM3$");
    octet2hex(outbuf + 5, salt, sizeof(salt));
    strcat(outbuf, "$");
    octet2hex(outbuf + 5 + 2 * sizeof(salt) + 1, hash, 32);
    *buflen = strlen(outbuf);
    return 0;
}

static struct st_mysql_auth sm3_sm2_info =
{
    MYSQL_AUTHENTICATION_INTERFACE_VERSION, /* 0x0103 */
    "sm3_sm2_auth",                         /* 插件名 */
    sm3_sm2_authenticate,                   /* authenticate_user */
    sm3_sm2_gen_auth_string,                /* generate_authentication_string */
    NULL,                                   /* validate_authentication_string 用默认 */
    NULL,                                   /* set_salt 本插件不需要 */
    sm3_sm2_auth_init,                      /* init */
    sm3_sm2_auth_deinit                     /* deinit */
};

拓展思考

  1. 双向 TLS 场景:若客户要求“证书 + 国密”双因子,可在 authenticate_user 里先通过 vio->ssl 获取客户端证书序列号,再与 mysql.user.authentication_string 里预置的序列号比对,失败直接返回 CR_ERROR_HANDSHAKE。
  2. K8s 热升级:把插件编译成 .so 后打进 distroless 镜像,利用 MariaDB Operator 的 sidecar 注入机制 做 rolling update,确保 Galera 集群在升级认证插件时 0 事务中断;此时需在 deinit 里保证 引用计数归零 再卸载,防止线程池仍在调用已卸载的函数指针。
  3. 等保 2.0 审计对接:在 authenticate_user 里调用 mysql_audit_notify 触发 MYSQL_AUDIT_AUTHENTICATION_AUTH 事件,把 客户端 IP、用户名、成功标志 以 JSON 格式发到 Kafka,供国内 SOC 平台消费;注意 error_msg 不能带敏感明文口令,否则会被测评机构判为 高风险项。