#include "pamtest.h" #include #include PamTest::PamTest(QObject *parent) : QObject (parent) { } PamTest::~PamTest() { } void PamTest::startPam() { pam_handle_t *pamHandle = nullptr; pam_conv conv = {PAMConversation, static_cast(this)}; // pam_start第一個參數對應於/etc/pam.d下面的指定的PAM文件,第二個參數對應於登錄的賬戶名 int ret = pam_start("test_pam", "uos", &conv, &pamHandle); if (ret != PAM_SUCCESS) { //qCritical() << "ERROR: PAM start failed, error: " << pam_strerror(pamHandle, ret) << ", start infomation: " << ret; } else { qDebug() << "PAM start..."; } // 可以在此處設置密碼 //const char *password = "testpassword"; //pam_set_item(pamHandle, PAM_AUTHTOK, password); int rc = pam_authenticate(pamHandle, 0); if (rc != PAM_SUCCESS) { //qWarning() << "PAM authenticate failed, error: " << pam_strerror(pamHandle, rc) << ", PAM authenticate: " << rc; } else { qDebug() << "PAM authenticate finished."; } int tmpRt = pam_acct_mgmt(pamHandle, 1); if (tmpRt != PAM_SUCCESS) { //qCritical() << "PAM acct failed:" << pam_strerror(pamHandle, tmpRt) << "PAM end infomation: " << tmpRt; } else { qDebug() << "PAM acct finished."; } int re = pam_end(pamHandle, rc); if (re != PAM_SUCCESS) { //qCritical() << "PAM end failed:" << pam_strerror(pamHandle, re) << "PAM end infomation: " << re; } else { qDebug() << "PAM end..."; } } int PamTest::PAMConversation(int num_msg, const pam_message **msg, pam_response **resp, void *app_data) { return PAM_SUCCESS; }