]> granicus.if.org Git - linux-pam/blob - modules/pam_shells/pam_shells.c
Relevant BUGIDs:
[linux-pam] / modules / pam_shells / pam_shells.c
1 /* pam_shells module */
2
3 #define SHELL_FILE "/etc/shells"
4
5 /*
6  * by Erik Troan <ewt@redhat.com>, Red Hat Software.
7  * August 5, 1996.
8  * This code shamelessly ripped from the pam_securetty module.
9  */
10
11 #define _BSD_SOURCE
12
13 #include <pwd.h>
14 #include <stdarg.h>
15 #include <string.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <sys/stat.h>
20 #include <syslog.h>
21 #include <unistd.h>
22
23 /*
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.
28  */
29
30 #define PAM_SM_AUTH
31 #define PAM_SM_ACCOUNT
32
33 #include <security/pam_modules.h>
34 #include <security/_pam_modutil.h>
35
36 /* some syslogging */
37
38 static void _pam_log(int err, const char *format, ...)
39 {
40     va_list args;
41
42     va_start(args, format);
43     openlog("PAM-shells", LOG_CONS|LOG_PID, LOG_AUTH);
44     vsyslog(err, format, args);
45     va_end(args);
46     closelog();
47 }
48
49 static int perform_check(pam_handle_t *pamh, int flags)
50 {
51     int retval = PAM_AUTH_ERR;
52     const char *userName;
53     char *userShell;
54     char shellFileLine[256];
55     struct stat sb;
56     struct passwd * pw;
57     FILE * shellFile;
58
59     retval = pam_get_user(pamh, &userName, NULL);
60     if (retval != PAM_SUCCESS) {
61         return PAM_SERVICE_ERR;
62     }
63
64     if (!userName || (userName[0] == '\0')) {
65
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;
70
71         /* It could still be NULL the second time. */
72         if (!userName || (userName[0] == '\0'))
73             return PAM_SERVICE_ERR;
74     }
75
76     pw = _pammodutil_getpwnam(pamh, userName);
77     if (!pw) {
78         return PAM_AUTH_ERR;            /* user doesn't exist */
79     }
80     userShell = pw->pw_shell;
81
82     if (stat(SHELL_FILE,&sb)) {
83         _pam_log(LOG_ERR, "%s cannot be stat'd (it probably does not exist)",
84                  SHELL_FILE);
85         return PAM_AUTH_ERR;            /* must have /etc/shells */
86     }
87
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",
90                  SHELL_FILE);
91         return PAM_AUTH_ERR;
92     }
93
94     shellFile = fopen(SHELL_FILE,"r");
95     if (shellFile == NULL) {       /* Check that we opened it successfully */
96         _pam_log(LOG_ERR,
97                  "Error opening %s", SHELL_FILE);
98         return PAM_SERVICE_ERR;
99     }
100
101     retval = 1;
102
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);
107     }
108
109     fclose(shellFile);
110
111     if (retval) {
112         return PAM_AUTH_ERR;
113     } else {
114         return PAM_SUCCESS;
115     }
116 }
117
118 /* --- authentication management functions (only) --- */
119
120 PAM_EXTERN
121 int pam_sm_authenticate(pam_handle_t *pamh, int flags, int argc,
122                         const char **argv)
123 {
124     return perform_check(pamh, flags);
125 }
126
127 PAM_EXTERN
128 int pam_sm_setcred(pam_handle_t *pamh, int flags, int argc,const char **argv)
129 {
130      return PAM_SUCCESS;
131 }
132
133 /* --- account management functions (only) --- */
134
135 PAM_EXTERN
136 int pam_sm_acct_mgmt(pam_handle_t *pamh, int flags, int argc,
137                      const char **argv)
138 {
139     return perform_check(pamh, flags);
140 }
141
142 #ifdef PAM_STATIC
143
144 /* static module data */
145
146 struct pam_module _pam_shells_modstruct = {
147      "pam_shells",
148      pam_sm_authenticate,
149      pam_sm_setcred,
150      pam_sm_acct_mgmt,
151      NULL,
152      NULL,
153      NULL,
154 };
155
156 #endif /* PAM_STATIC */
157
158 /* end of module definition */