PAM auth with program supplied password

PAM Authentication with program supplied username / password pair.

In cases like "login" and "sudo", the use of PAM involves asking user for password on the terminal. This is a feature provided by the pam-misc library. PAM will require calling program to supply an "conversasion" callback, and pam-misc library provided "misc_conv" to enable this askpass feature. As vsftpd, program could provide customized pam conversasion. Sample code (largly based on vsftpd code snippet so Thanks to VSFTPD)

  1. int
  2. pam_conv_func(int nmsg, const struct pam_message** p_msg,
  3.               struct pam_response** p_reply, void* p_addata)
  4. {
  5.   int i;
  6.   struct pam_response* p_resps = 0;
  7.   (void) p_addata;
  8.   if (nmsg < 0)
  9.   {
  10.     fprintf(stderr, "dodgy nmsg in pam_conv_func");
  11.   }
  12.   p_resps = malloc(sizeof(struct pam_response) * nmsg);
  13.   for (i=0; i<nmsg; i++)
  14.   {
  15.     switch (p_msg[i]->msg_style)
  16.     {
  17.       case PAM_PROMPT_ECHO_OFF:
  18.         p_resps[i].resp_retcode = PAM_SUCCESS;
  19.         p_resps[i].resp = (char*) strdup("PASSWORD_HERE"); // put the program supplied password here.
  20.         break;
  21.       case PAM_TEXT_INFO:
  22.       case PAM_ERROR_MSG:
  23.         p_resps[i].resp_retcode = PAM_SUCCESS;
  24.         p_resps[i].resp = 0;
  25.         break;
  26.       case PAM_PROMPT_ECHO_ON:
  27.       default:
  28.         free(p_resps);
  29.         return PAM_CONV_ERR;
  30.         break;
  31.     }
  32.   }
  33.   *p_reply = p_resps;
  34.   return PAM_SUCCESS;
  35. }
  36. static struct pam_conv conv = {
  37.      pam_conv_func,
  38.      //misc_conv,
  39.      NULL
  40. };
  41. pam_handle_t *pamh=NULL;
  42. pam_start(service, username, &conv, &pamh);
  43. pam_set_item(pamh, PAM_USER, "ryan"); // required in case "username" was NULL when calling pam_start
  44. retcode = pam_authenticate(pamh, 0); // will call "pam_conv" provided in pam_start. returns PAM_SUCCESS if auth passed.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值