tags:

views:

80

answers:

3

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.

A: 

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.

poolie
+1  A: 

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.

Medran
A: 

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

bmargulies