1 /* pam_shells module */
3 #define SHELL_FILE "/etc/shells"
6 * by Erik Troan <ewt@redhat.com>, Red Hat Software.
8 * This code shamelessly ripped from the pam_securetty module.
24 * here, we make a definition for the externally accessible function
25 * in this file (this definition is required for static a module
26 * but strongly encouraged generally) it is used to instruct the
27 * modules include file to define the function prototypes.
31 #define PAM_SM_ACCOUNT
33 #include <security/pam_modules.h>
34 #include <security/pam_modutil.h>
35 #include <security/pam_ext.h>
37 static int perform_check(pam_handle_t *pamh)
39 int retval = PAM_AUTH_ERR;
42 char shellFileLine[256];
47 retval = pam_get_user(pamh, &userName, NULL);
48 if (retval != PAM_SUCCESS) {
49 return PAM_SERVICE_ERR;
52 if (!userName || (userName[0] == '\0')) {
54 /* Don't let them use a NULL username... */
55 retval = pam_get_user(pamh,&userName,NULL);
56 if (retval != PAM_SUCCESS)
57 return PAM_SERVICE_ERR;
59 /* It could still be NULL the second time. */
60 if (!userName || (userName[0] == '\0'))
61 return PAM_SERVICE_ERR;
64 pw = pam_modutil_getpwnam(pamh, userName);
66 return PAM_AUTH_ERR; /* user doesn't exist */
68 userShell = pw->pw_shell;
70 if (stat(SHELL_FILE,&sb)) {
71 pam_syslog(pamh, LOG_ERR, "Cannot stat %s: %m", SHELL_FILE);
72 return PAM_AUTH_ERR; /* must have /etc/shells */
75 if ((sb.st_mode & S_IWOTH) || !S_ISREG(sb.st_mode)) {
76 pam_syslog(pamh, LOG_ERR,
77 "%s is either world writable or not a normal file",
82 shellFile = fopen(SHELL_FILE,"r");
83 if (shellFile == NULL) { /* Check that we opened it successfully */
84 pam_syslog(pamh, LOG_ERR, "Error opening %s: %m", SHELL_FILE);
85 return PAM_SERVICE_ERR;
90 while(retval && (fgets(shellFileLine, 255, shellFile) != NULL)) {
91 if (shellFileLine[strlen(shellFileLine) - 1] == '\n')
92 shellFileLine[strlen(shellFileLine) - 1] = '\0';
93 retval = strcmp(shellFileLine, userShell);
105 /* --- authentication management functions (only) --- */
107 int pam_sm_authenticate(pam_handle_t *pamh, int flags UNUSED,
108 int argc UNUSED, const char **argv UNUSED)
110 return perform_check(pamh);
113 int pam_sm_setcred(pam_handle_t *pamh UNUSED, int flags UNUSED,
114 int argc UNUSED, const char **argv UNUSED)
119 /* --- account management functions (only) --- */
121 int pam_sm_acct_mgmt(pam_handle_t *pamh, int flags UNUSED,
122 int argc UNUSED, const char **argv UNUSED)
124 return perform_check(pamh);
127 /* end of module definition */