]> granicus.if.org Git - linux-pam/blob - modules/pam_shells/pam_shells.c
doc: fix module type written in MODULE TYPES PROVIDED
[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 #include "config.h"
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 #include <security/pam_ext.h>
36
37 static int perform_check(pam_handle_t *pamh)
38 {
39     int retval = PAM_AUTH_ERR;
40     const char *userName;
41     char *userShell;
42     char shellFileLine[256];
43     struct stat sb;
44     struct passwd * pw;
45     FILE * shellFile;
46
47     retval = pam_get_user(pamh, &userName, NULL);
48     if (retval != PAM_SUCCESS) {
49         return PAM_SERVICE_ERR;
50     }
51
52     if (!userName || (userName[0] == '\0')) {
53
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;
58
59         /* It could still be NULL the second time. */
60         if (!userName || (userName[0] == '\0'))
61             return PAM_SERVICE_ERR;
62     }
63
64     pw = pam_modutil_getpwnam(pamh, userName);
65     if (!pw) {
66         return PAM_AUTH_ERR;            /* user doesn't exist */
67     }
68     userShell = pw->pw_shell;
69
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 */
73     }
74
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",
78                    SHELL_FILE);
79         return PAM_AUTH_ERR;
80     }
81
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;
86     }
87
88     retval = 1;
89
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);
94     }
95
96     fclose(shellFile);
97
98     if (retval) {
99         return PAM_AUTH_ERR;
100     } else {
101         return PAM_SUCCESS;
102     }
103 }
104
105 /* --- authentication management functions (only) --- */
106
107 int pam_sm_authenticate(pam_handle_t *pamh, int flags UNUSED,
108                         int argc UNUSED, const char **argv UNUSED)
109 {
110     return perform_check(pamh);
111 }
112
113 int pam_sm_setcred(pam_handle_t *pamh UNUSED, int flags UNUSED,
114                    int argc UNUSED, const char **argv UNUSED)
115 {
116      return PAM_SUCCESS;
117 }
118
119 /* --- account management functions (only) --- */
120
121 int pam_sm_acct_mgmt(pam_handle_t *pamh, int flags UNUSED,
122                      int argc UNUSED, const char **argv UNUSED)
123 {
124     return perform_check(pamh);
125 }
126
127 /* end of module definition */