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>
38 static void _pam_log(int err, const char *format, ...)
42 va_start(args, format);
43 openlog("PAM-shells", LOG_CONS|LOG_PID, LOG_AUTH);
44 vsyslog(err, format, args);
49 static int perform_check(pam_handle_t *pamh, int flags)
51 int retval = PAM_AUTH_ERR;
54 char shellFileLine[256];
59 retval = pam_get_user(pamh, &userName, NULL);
60 if (retval != PAM_SUCCESS) {
61 return PAM_SERVICE_ERR;
64 if (!userName || (userName[0] == '\0')) {
66 /* Don't let them use a NULL username... */
67 retval = pam_get_user(pamh,&userName,NULL);
68 if (retval != PAM_SUCCESS)
69 return PAM_SERVICE_ERR;
71 /* It could still be NULL the second time. */
72 if (!userName || (userName[0] == '\0'))
73 return PAM_SERVICE_ERR;
76 pw = _pammodutil_getpwnam(pamh, userName);
78 return PAM_AUTH_ERR; /* user doesn't exist */
80 userShell = pw->pw_shell;
82 if (stat(SHELL_FILE,&sb)) {
83 _pam_log(LOG_ERR, "%s cannot be stat'd (it probably does not exist)",
85 return PAM_AUTH_ERR; /* must have /etc/shells */
88 if ((sb.st_mode & S_IWOTH) || !S_ISREG(sb.st_mode)) {
89 _pam_log(LOG_ERR, "%s is either world writable or not a normal file",
94 shellFile = fopen(SHELL_FILE,"r");
95 if (shellFile == NULL) { /* Check that we opened it successfully */
97 "Error opening %s", SHELL_FILE);
98 return PAM_SERVICE_ERR;
103 while(retval && (fgets(shellFileLine, 255, shellFile) != NULL)) {
104 if (shellFileLine[strlen(shellFileLine) - 1] == '\n')
105 shellFileLine[strlen(shellFileLine) - 1] = '\0';
106 retval = strcmp(shellFileLine, userShell);
118 /* --- authentication management functions (only) --- */
121 int pam_sm_authenticate(pam_handle_t *pamh, int flags, int argc,
124 return perform_check(pamh, flags);
128 int pam_sm_setcred(pam_handle_t *pamh, int flags, int argc,const char **argv)
133 /* --- account management functions (only) --- */
136 int pam_sm_acct_mgmt(pam_handle_t *pamh, int flags, int argc,
139 return perform_check(pamh, flags);
144 /* static module data */
146 struct pam_module _pam_shells_modstruct = {
156 #endif /* PAM_STATIC */
158 /* end of module definition */