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

时间:2011-05-06 15:49:48

标签: 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);

最初尝试对话功能导致(密码被硬编码用于测试):

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);
}

任何帮助将不胜感激。谢谢!

4 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:2)

为PAM传递标准信息(例如密码)的方法是使用pam句柄中设置的变量和pam_set_item(参见pam_set_item的手册页)。

您可以将应用程序稍后需要使用的任何内容设置到pam_stack中。如果你想把密码放到pam_stack中,你应该能够在调用pam_start()之后立即执行该操作,方法是将PAM_AUTHTOK变量设置为堆栈,类似于下面的伪代码:

pam_handle_t* handle = NULL;
pam_start("common-auth", username, NULL, &handle);
pam_set_item( handle, PAM_AUTHTOK, password);

这将使堆栈上的密码可用于任何关心使用它的模块,但是您通常必须通过在服务的pam_configuration中设置标准use_first_pass或try_first_pass选项来告诉模块使用它(在此case /etc/pam.d/common-auth)。

标准的pam_unix模块确实支持try_first_pass,因此将它添加到系统的pam配置中(在pam_unix行的末尾)也没什么坏处。

执行此操作后,对来自common-auth服务调用的 pam_authenticate()的任何调用应该只需选择密码并继续使用。

关于use_first_pass和try_first_pass之间区别的一个小注意事项:它们都告诉模块(在本例中为pam_unix)尝试pam_stack上的密码,但是当它们没有密码/ AUTHTOK可用时它们的行为不同。在缺少的情况下,use_first_pass失败,try_first_pass允许模块提示输入密码。

答案 2 :(得分:1)

Fantius的解决方案对我有用,甚至是根本。

我最初选择了John的解决方案,因为它更干净,并且没有会话功能使用PAM变量(实际上,这里没有必要),但它没有,也不会有效。正如Adam Badura在两个帖子中提到的那样,PAM有一些内部检查来防止直接设置PAM_AUTHTOK。

John的解决方案将导致类似于提到here的行为,其中任何密码值都将被允许登录(即使您声明但未定义pam_conv变量)。

我还建议用户注意malloc的位置,因为它在您的应用程序中可能会有所不同(请记住,上面的代码更像是测试/模板,而不是其他任何东西)。

答案 3 :(得分:0)

df$Lat <- c(26.2,26.1,26,26.2,26.2,25.8,25.8)
df$Long <- c(83.9,84,83,84.2,84.3,82,82.1)

struct pam_conv的第二个字段(appdata_ptr)传递给对话函数, 因此我们可以将其用作密码指针。

struct pam_conv {
    int (*conv)(int num_msg, const struct pam_message **msg,
                struct pam_response **resp, void *appdata_ptr);
    void *appdata_ptr;
};
相关问题