栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

这就是我最终要做的。请参阅标有三个星号的注释。

#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-devstruct pam_response *reply;//function used to get user inputint 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_retpre = 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;}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/412504.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号