C: 在 Linux 上显示一个要求输入密码的系统通用对话框

C: Display a system generic dialog box requesting password on Linux

我正在使用 Debian Linux OS,正在用 C 语言开发文件安全应用程序。 在执行某些代码之前,我试图向用户请求密码以进行身份​​验证。我希望提供一个系统通用密码 window(即,在更新安装或安装文件系统期间,更新管理器要求我们输入 root 密码时出现的密码)。 我尝试使用 systemd-aask-passworddialog --passwordbox shell 命令从我的 C 代码使用 popen() 到 运行 来请求密码。但是,这两个命令都在 shell 中起作用,而不是在通过桌面启动器触发时起作用。

有没有办法通过出现在 shell 之外的 dia;og window 请求密码?该方法可以使用 shelll-script、python、perl 或通用 C 代码,以便我可以与我现有的 C 程序集成。

你可以试试 zenity:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

FILE *popen(const char *command, const char *mode);
int pclose(FILE *stream);

int main(void)
{
    FILE *cmd;
    char result[32];

    cmd = popen("zenity --password", "r");
    if (cmd == NULL) {
        perror("popen");
        exit(EXIT_FAILURE);
    }
    if (fgets(result, sizeof(result), cmd)) {
        result[strcspn(result, "\r\n")] = 0; 
        printf("password: %s\n", result);
    }
    pclose(cmd);
    return 0;
}