旧版应用程序的PAM身份验证

Jim*_*ler 8 c c++ linux authentication pam

我有一个遗留应用程序,通过网络异步接收用户名/密码请求.由于我已将用户名和密码存储为变量,因此在Linux(Debian 6)上使用PAM进行身份验证的最佳方法是什么?

我已经尝试编写自己的会话功能,但我不确定获取密码的最佳方法.我已经考虑将它存储在appdata中并从pam_conv结构中引用它,但几乎没有关于如何做到这一点的文档.

是否有更简单的方法来验证用户,而没有过多的会话功能?我也无法成功使用pam_set_data,我不确定这是否合适.

这是我正在做的事情:

user = guiMessage->username;
pass = guiMessage->password;

pam_handle_t* pamh = NULL;
int           pam_ret;
struct pam_conv conv = {
  my_conv,
  NULL
};

pam_start("nxs_login", user, &conv, &pamh);
pam_ret = pam_authenticate(pamh, 0);

if (pam_ret == PAM_SUCCESS)
  permissions = 0xff;

pam_end(pamh, pam_ret);
Run Code Online (Sandbox Code Playgroud)

对话函数的初始尝试导致(密码被硬编码用于测试):

int 
my_conv(int num_msg, const struct pam_message **msg, struct pam_response **resp, void *data)
{
  struct pam_response *aresp;

  if (num_msg <= 0 || num_msg > PAM_MAX_NUM_MSG)
    return (PAM_CONV_ERR);
  if ((aresp = (pam_response*)calloc(num_msg, sizeof *aresp)) == NULL)
    return (PAM_BUF_ERR);
  aresp[0].resp_retcode = 0;
  aresp[0].resp = strdup("mypassword");

  *resp = aresp;
  return (PAM_SUCCESS);
}
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激.谢谢!

Fan*_*ius 14

这就是我最终做的事情.请参阅标有三个星号的注释.

#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <security/pam_appl.h>
#include <unistd.h>

// To build this:
// g++ test.cpp -lpam -o test

// if pam header files missing try:
// sudo apt install libpam0g-dev

struct pam_response *reply;

//function used to get user input
int function_conversation(int num_msg, const struct pam_message **msg, struct pam_response **resp, void *appdata_ptr)
{
  *resp = reply;
  return PAM_SUCCESS;
}

int main(int argc, char** argv)
{
  if(argc != 2) {
      fprintf(stderr, "Usage: check_user <username>\n");
      exit(1);
  }
  const char *username;
  username = argv[1];

  const struct pam_conv local_conversation = { function_conversation, NULL };
  pam_handle_t *local_auth_handle = NULL; // this gets set by pam_start

  int retval;

  // local_auth_handle gets set based on the service
  retval = pam_start("common-auth", username, &local_conversation, &local_auth_handle);

  if (retval != PAM_SUCCESS)
  {
    std::cout << "pam_start returned " << retval << std::endl;
    exit(retval);
  }

  reply = (struct pam_response *)malloc(sizeof(struct pam_response));

  // *** Get the password by any method, or maybe it was passed into this function.
  reply[0].resp = getpass("Password: ");
  reply[0].resp_retcode = 0;

  retval = pam_authenticate(local_auth_handle, 0);

  if (retval != PAM_SUCCESS)
  {
    if (retval == PAM_AUTH_ERR)
    {
      std::cout << "Authentication failure." << std::endl;
    }
    else
    {
      std::cout << "pam_authenticate returned " << retval << std::endl;
    }
    exit(retval);
  }

  std::cout << "Authenticated." << std::endl;

  retval = pam_end(local_auth_handle, retval);

  if (retval != PAM_SUCCESS)
  {
    std::cout << "pam_end returned " << retval << std::endl;
    exit(retval);
  }

  return retval;
}
Run Code Online (Sandbox Code Playgroud)

  • 如果任何 PAM 模块尝试在一个对话函数调用中处理多条消息,此解决方案将导致越界堆读取!这种情况很少见,但可能会发生。在这种情况下,“*resp”应该是“num_msg”“pam_response”结构的数组,而不是指向一个结构的指针。 (2认同)