gpt4 book ai didi

c - 为什么我的程序无法链接 pam_* 调用

转载 作者:太空宇宙 更新时间:2023-11-03 23:22:13 26 4
gpt4 key购买 nike

我正在尝试编译和构建以下非常简单直接的 C 程序:

#include <security/pam_appl.h>
#include <security/pam_misc.h>
#include <stdio.h>
#include <string.h>

// Define custom PAM conversation function
int custom_conversation(int num_msg, const struct pam_message** msg, struct pam_response** resp, void* appdata_ptr)
{
// Provide password for the PAM conversation response that was passed into appdata_ptr
struct pam_response* reply = (struct pam_response* )malloc(sizeof(struct pam_response));
reply[0].resp = (char*)appdata_ptr;
reply[0].resp_retcode = 0;

*resp = reply;

return PAM_SUCCESS;
}

int main (int argc, char* argv[])
{
if (argc > 2)
{
// Set up a custom PAM conversation passing in authentication password
char* password = (char*)malloc(strlen(argv[2]) + 1);
strcpy(password, argv[2]);
struct pam_conv pamc = { custom_conversation, password };
pam_handle_t* pamh;
int retval;

// Start PAM - just associate with something simple like the "whoami" command
if ((retval = pam_start("whoami", argv[1], &pamc, &pamh)) == PAM_SUCCESS)
{
// Authenticate the user
if ((retval = pam_authenticate(pamh, 0)) == PAM_SUCCESS)
fprintf(stdout, "OK\n");
else
fprintf(stderr, "FAIL: pam_authentication failed.\n");

// All done
pam_end(pamh, 0);
return retval;
}
else
{
fprintf(stderr, "FAIL: pam_start failed.\n");
return retval;
}
}

fprintf(stderr, "FAIL: expected two arguments for user name and password.\n");
return 1;
}

我在谷歌的某个地方找到了它。问题是它没有找到主要的 pam_* 符号:

X@Y:~$ gcc -o checkpwd -lpam -lpam_misc checkpwd.c
/tmp/ccjIBywU.o: In Funktion `main':
checkpwd.c:(.text+0xcf): Nicht definierter Verweis auf `pam_start'
checkpwd.c:(.text+0xe9): Nicht definierter Verweis auf `pam_authenticate'
checkpwd.c:(.text+0x141): Nicht definierter Verweis auf `pam_end'
collect2: error: ld returned 1 exit status

对不起德国人,它的意思是“缺少符号”之类的东西。这就是我的问题:为什么找不到符号?我需要包含哪些库?当我在 libpam.a 上执行 nm 时,我可以找到所有符号:

X@Y:~$ nm /usr/lib/x86_64-linux-gnu/libpam.a | grep pam_start
U _pam_start_timer
0000000000000010 T _pam_start_timer
0000000000001930 T _pam_start_handlers
U _pam_start_timer
pam_start.o:
0000000000000000 T pam_start
U _pam_start_handlers
kai@KTEC32:~$ nm /usr/lib/x86_64-linux-gnu/libpam.a | grep pam_authenticate
0000000000000000 T pam_authenticate

谁能看出这里出了什么问题?

感谢任何提示。

最佳答案

您必须将源文件和目标文件放在库文件之前:

gcc -o checkpwd checkpwd.c -lpam -lpam_misc 

参见:Why does the order of '-l' option in gcc matter?

关于c - 为什么我的程序无法链接 pam_* 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36217021/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com