1 /* pam_securetty module */
3 #define SECURETTY_FILE "/etc/securetty"
4 #define TTY_PREFIX "/dev/"
7 * by Elliot Lee <sopwith@redhat.com>, Red Hat Software.
9 * This code shamelessly ripped from the pam_rootok module.
10 * Slight modifications AGM. 1996/12/3
11 * $FreeBSD: src/contrib/libpam/modules/pam_securetty/pam_securetty.c,v 1.3.2.2 2001/06/11 15:28:27 markm Exp $
12 * $DragonFly: src/contrib/libpam/modules/pam_securetty/Attic/pam_securetty.c,v 1.2 2003/06/17 04:24:03 dillon Exp $
19 #include <sys/types.h>
30 * here, we make a definition for the externally accessible function
31 * in this file (this definition is required for static a module
32 * but strongly encouraged generally) it is used to instruct the
33 * modules include file to define the function prototypes.
38 #include <security/pam_modules.h>
42 static void _pam_log(int err, const char *format, ...)
46 va_start(args, format);
47 openlog("PAM-securetty", LOG_CONS|LOG_PID, LOG_AUTH);
48 vsyslog(err, format, args);
53 /* argument parsing */
55 #define PAM_DEBUG_ARG 0x0001
57 static int _pam_parse(int argc, const char **argv)
61 /* step through arguments */
62 for (ctrl=0; argc-- > 0; ++argv) {
66 if (!strcmp(*argv,"debug"))
67 ctrl |= PAM_DEBUG_ARG;
69 _pam_log(LOG_ERR,"pam_parse: unknown option; %s",*argv);
76 /* --- authentication management functions (only) --- */
79 int pam_sm_authenticate(pam_handle_t *pamh,int flags,int argc
82 int retval = PAM_AUTH_ERR;
85 char ttyfileline[256];
86 struct stat ttyfileinfo;
87 struct passwd *user_pwd;
91 /* parse the arguments */
92 ctrl = _pam_parse(argc, argv);
94 retval = pam_get_user(pamh, &username, NULL);
95 if (retval != PAM_SUCCESS || username == NULL) {
96 if (ctrl & PAM_DEBUG_ARG) {
97 _pam_log(LOG_WARNING, "cannot determine username");
99 return (retval == PAM_CONV_AGAIN
100 ? PAM_INCOMPLETE:PAM_SERVICE_ERR);
103 retval = pam_get_item(pamh, PAM_TTY, (const void **)&uttyname);
104 if (retval != PAM_SUCCESS || uttyname == NULL) {
105 if (ctrl & PAM_DEBUG_ARG) {
106 _pam_log(LOG_WARNING, "cannot determine user's tty");
108 return PAM_SERVICE_ERR;
111 /* The PAM_TTY item may be prefixed with "/dev/" - skip that */
112 if (strncmp(TTY_PREFIX, uttyname, sizeof(TTY_PREFIX)-1) == 0)
113 uttyname += sizeof(TTY_PREFIX)-1;
115 user_pwd = getpwnam(username);
116 if (user_pwd == NULL) {
118 } else if (user_pwd->pw_uid != 0) { /* If the user is not root,
119 securetty's does not apply
124 if (stat(SECURETTY_FILE, &ttyfileinfo)) {
125 _pam_log(LOG_NOTICE, "Couldn't open " SECURETTY_FILE);
126 return PAM_SUCCESS; /* for compatibility with old securetty handling,
127 this needs to succeed. But we still log the
131 if ((ttyfileinfo.st_mode & S_IWOTH)
132 || !S_ISREG(ttyfileinfo.st_mode)) {
133 /* If the file is world writable or is not a
134 normal file, return error */
135 _pam_log(LOG_ERR, SECURETTY_FILE
136 " is either world writable or not a normal file");
140 ttyfile = fopen(SECURETTY_FILE,"r");
141 if(ttyfile == NULL) { /* Check that we opened it successfully */
143 "Error opening " SECURETTY_FILE);
144 return PAM_SERVICE_ERR;
146 /* There should be no more errors from here on */
148 /* This loop assumes that PAM_SUCCESS == 0
149 and PAM_AUTH_ERR != 0 */
150 while((fgets(ttyfileline,sizeof(ttyfileline)-1, ttyfile) != NULL)
152 if(ttyfileline[strlen(ttyfileline) - 1] == '\n')
153 ttyfileline[strlen(ttyfileline) - 1] = '\0';
154 retval = strcmp(ttyfileline,uttyname);
158 if (ctrl & PAM_DEBUG_ARG)
159 _pam_log(LOG_WARNING, "access denied: tty '%s' is not secure !",
161 retval = PAM_AUTH_ERR;
163 if ((retval == PAM_SUCCESS) && (ctrl & PAM_DEBUG_ARG))
164 _pam_log(LOG_DEBUG, "access allowed for '%s' on '%s'",
170 int pam_sm_setcred(pam_handle_t *pamh,int flags,int argc
179 /* static module data */
181 struct pam_module _pam_securetty_modstruct = {
193 /* end of module definition */