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.