stat() 函数似乎在测试 PAM 模块中不起作用

发布于 2024-09-08 17:53:50 字数 1465 浏览 1 评论 0原文

我正在尝试编写一个 PAM 模块。 PAM 模块在首次登录时创建一个目录。与 pam_mkhomedir 非常相似。

这是代码。

PAM_EXTERN int
pam_sm_open_session (pam_handle_t *pamh, int flags, int argc,
             const char **argv)
{
   int retval;
   const char *user;
   const struct passwd *pwd;
   struct stat St;

    // Parse the args
   _pam_parse(pamh, flags, argc, argv);
   pam_info (pamh, "\nThese are the args flags. skel: %s, mkdir: %s, umask: %o",SkelDir, MkDir, UMask );

   // Determine the user name  
   retval = pam_get_user(pamh, &user, NULL);
   if (retval != PAM_SUCCESS || user == NULL || *(const char *)user == '\0')
   {
      pam_syslog(pamh, LOG_NOTICE, "user unknown");
      return PAM_USER_UNKNOWN;
   }

   strcpy(DestDir, MkDir);
   strcat(DestDir,"/");
   strcat(DestDir, user);

   // Get the password entry   
   pwd = pam_modutil_getpwnam (pamh, user);
   if (pwd == NULL)
   {
      return PAM_CRED_INSUFFICIENT;
   }

   // For some reason stat wont work, using access instead. 

   //retval = stat(DestDir,&St); 


   retval = access(DestDir, F_OK);
   if ( retval == 0)
   {
    pam_info(pamh, "directory exists %s" ,DestDir); 
   }

    return PAM_SUCCESS;

}

如您所见,现在该模块仅在登录时打印 pam 配置文件中指定的参数。
问题出在 stat 函数上。当我使用它时,即使在 stat 函数之前调用 pam_info 函数,也不会打印任何内容。
如果我使用访问函数,模块将正常执行。 我有点困惑为什么会发生这种情况。
编辑:我已经包含了一些检查 errno 和其他返回值的代码,但该代码不会被执行。我没有将其包含在问题中,因为我不想让代码太长而难以阅读。该模块似乎失败并完全退出。它不打印任何东西。它只是什么都不做。但如果我使用 access() 模块工作正常。

I'm trying to write a PAM module. The PAM module creates a directory on first log in. Very similar to the pam_mkhomedir.

Here is the code.

PAM_EXTERN int
pam_sm_open_session (pam_handle_t *pamh, int flags, int argc,
             const char **argv)
{
   int retval;
   const char *user;
   const struct passwd *pwd;
   struct stat St;

    // Parse the args
   _pam_parse(pamh, flags, argc, argv);
   pam_info (pamh, "\nThese are the args flags. skel: %s, mkdir: %s, umask: %o",SkelDir, MkDir, UMask );

   // Determine the user name  
   retval = pam_get_user(pamh, &user, NULL);
   if (retval != PAM_SUCCESS || user == NULL || *(const char *)user == '\0')
   {
      pam_syslog(pamh, LOG_NOTICE, "user unknown");
      return PAM_USER_UNKNOWN;
   }

   strcpy(DestDir, MkDir);
   strcat(DestDir,"/");
   strcat(DestDir, user);

   // Get the password entry   
   pwd = pam_modutil_getpwnam (pamh, user);
   if (pwd == NULL)
   {
      return PAM_CRED_INSUFFICIENT;
   }

   // For some reason stat wont work, using access instead. 

   //retval = stat(DestDir,&St); 


   retval = access(DestDir, F_OK);
   if ( retval == 0)
   {
    pam_info(pamh, "directory exists %s" ,DestDir); 
   }

    return PAM_SUCCESS;

}

As you can see, right now, the module just prints the arguments specified in the pam config file at login time.
The problem is with the stat function. When I use it, nothing gets printed, even though the pam_info functions are called before the stat function.
If I use the access function, the module executes properly.
I'm kinda stumped as to why this is happening.
EDIT: I had included some code that checked the errno and other return values, but that code does not get executed. I didnt include it in the question because I didnt want to make the code too long to read. The module seems to fail and exit completely. It doesnt print anything. It just does nothing. But if I use access() the module works fine.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

亢潮 2024-09-15 17:53:50

有时,与您正在调用的库相比,统计结构可能会被错误定义,要查看是否是这种情况,请使用字符数组填充您的统计结构(确保它不是指向字符的指针,实际上定义了数组的大小),其中表示“测试堆栈损坏”。现在,当您运行时,如果该缓冲区损坏,那就是问题的根源。您可以尝试切换编译器,或者不使用 stat 函数。最近,当我使用 clang 编译器与 gcc 时,stat 函数表现不佳。

sometimes the stat structure can be misdefined compared against the library you are calling, to see if this is the case pad your stat structure with a character array (make sure its not a pointer to characters, actually define the size of the array) that says "testing for stack corruption". Now when you run if that buffer gets corrupted thats the root of the problem. You can try switching compilers, or not using the stat function. I just recently had the stat function misbehaving on me when I used the clang compiler vs gcc.

烟若柳尘 2024-09-15 17:53:50

只需发布完整代码即可。

使用 -W -Wall 编译它,看看是否有帮助。

尝试在 stracevalgrind 下运行它,看看 stat 是否失败或者 valgrind 是否发现问题。

Just post the full code.

Compile it with -W -Wall and see if that helps.

Try running it under strace or valgrind and see if the stat fails or if valgrind finds problems.

秋意浓 2024-09-15 17:53:50

我没有看到 DestDir 的声明,但我敢打赌它太小了。

I don't see a declaration for DestDir, but I bet it's too small.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文