xref: /netbsd-src/lib/libpam/modules/pam_securetty/pam_securetty.c (revision 4ed3eb7f906b7cd88146ea2b13d7f0e360d1c861)
1*4ed3eb7fSchristos /*	$NetBSD: pam_securetty.c,v 1.7 2006/11/03 18:55:40 christos Exp $	*/
2e7d22a2eSchristos 
36f11bdf1Schristos /*-
46f11bdf1Schristos  * Copyright (c) 2001 Mark R V Murray
56f11bdf1Schristos  * All rights reserved.
66f11bdf1Schristos  * Copyright (c) 2001 Networks Associates Technology, Inc.
76f11bdf1Schristos  * All rights reserved.
86f11bdf1Schristos  *
96f11bdf1Schristos  * Portions of this software were developed for the FreeBSD Project by
106f11bdf1Schristos  * ThinkSec AS and NAI Labs, the Security Research Division of Network
116f11bdf1Schristos  * Associates, Inc.  under DARPA/SPAWAR contract N66001-01-C-8035
126f11bdf1Schristos  * ("CBOSS"), as part of the DARPA CHATS research program.
136f11bdf1Schristos  *
146f11bdf1Schristos  * Redistribution and use in source and binary forms, with or without
156f11bdf1Schristos  * modification, are permitted provided that the following conditions
166f11bdf1Schristos  * are met:
176f11bdf1Schristos  * 1. Redistributions of source code must retain the above copyright
186f11bdf1Schristos  *    notice, this list of conditions and the following disclaimer.
196f11bdf1Schristos  * 2. Redistributions in binary form must reproduce the above copyright
206f11bdf1Schristos  *    notice, this list of conditions and the following disclaimer in the
216f11bdf1Schristos  *    documentation and/or other materials provided with the distribution.
226f11bdf1Schristos  * 3. The name of the author may not be used to endorse or promote
236f11bdf1Schristos  *    products derived from this software without specific prior written
246f11bdf1Schristos  *    permission.
256f11bdf1Schristos  *
266f11bdf1Schristos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
276f11bdf1Schristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
286f11bdf1Schristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
296f11bdf1Schristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
306f11bdf1Schristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
316f11bdf1Schristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
326f11bdf1Schristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
336f11bdf1Schristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
346f11bdf1Schristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
356f11bdf1Schristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
366f11bdf1Schristos  * SUCH DAMAGE.
376f11bdf1Schristos  */
386f11bdf1Schristos 
396f11bdf1Schristos #include <sys/cdefs.h>
40e7d22a2eSchristos #ifdef __FreeBSD__
416f11bdf1Schristos __FBSDID("$FreeBSD: src/lib/libpam/modules/pam_securetty/pam_securetty.c,v 1.13 2004/02/10 10:13:21 des Exp $");
42e7d22a2eSchristos #else
43*4ed3eb7fSchristos __RCSID("$NetBSD: pam_securetty.c,v 1.7 2006/11/03 18:55:40 christos Exp $");
44e7d22a2eSchristos #endif
456f11bdf1Schristos 
466f11bdf1Schristos #include <sys/types.h>
476f11bdf1Schristos #include <sys/stat.h>
486f11bdf1Schristos #include <pwd.h>
496f11bdf1Schristos #include <ttyent.h>
506f11bdf1Schristos #include <string.h>
5198043981Sjnemeth #include <syslog.h>
526f11bdf1Schristos 
536f11bdf1Schristos #define PAM_SM_ACCOUNT
546f11bdf1Schristos 
556f11bdf1Schristos #include <security/pam_appl.h>
566f11bdf1Schristos #include <security/pam_modules.h>
576f11bdf1Schristos #include <security/pam_mod_misc.h>
586f11bdf1Schristos 
596f11bdf1Schristos #define TTY_PREFIX	"/dev/"
606f11bdf1Schristos 
616f11bdf1Schristos PAM_EXTERN int
pam_sm_acct_mgmt(pam_handle_t * pamh __unused,int flags __unused,int argc __unused,const char * argv[]__unused)626f11bdf1Schristos pam_sm_acct_mgmt(pam_handle_t *pamh __unused, int flags __unused,
636f11bdf1Schristos     int argc __unused, const char *argv[] __unused)
646f11bdf1Schristos {
6559cbc9e2Sthorpej 	struct passwd *pwd, pwres;
666f11bdf1Schristos 	struct ttyent *ty;
676f11bdf1Schristos 	const char *user;
686f11bdf1Schristos 	const void *tty;
6998043981Sjnemeth 	const void *hostname;
706f11bdf1Schristos 	int pam_err;
7159cbc9e2Sthorpej 	char pwbuf[1024];
72*4ed3eb7fSchristos 	struct syslog_data data = SYSLOG_DATA_INIT;
736f11bdf1Schristos 
746f11bdf1Schristos 	pam_err = pam_get_user(pamh, &user, NULL);
756f11bdf1Schristos 	if (pam_err != PAM_SUCCESS)
766f11bdf1Schristos 		return (pam_err);
7759cbc9e2Sthorpej 	if (user == NULL ||
782a62e4e1Schristos 	    getpwnam_r(user, &pwres, pwbuf, sizeof(pwbuf), &pwd) != 0 ||
792a62e4e1Schristos 	    pwd == NULL)
806f11bdf1Schristos 		return (PAM_SERVICE_ERR);
816f11bdf1Schristos 
826f11bdf1Schristos 	PAM_LOG("Got user: %s", user);
836f11bdf1Schristos 
846f11bdf1Schristos 	/* If the user is not root, secure ttys do not apply */
856f11bdf1Schristos 	if (pwd->pw_uid != 0)
866f11bdf1Schristos 		return (PAM_SUCCESS);
876f11bdf1Schristos 
886f11bdf1Schristos 	pam_err = pam_get_item(pamh, PAM_TTY, &tty);
896f11bdf1Schristos 	if (pam_err != PAM_SUCCESS)
906f11bdf1Schristos 		return (pam_err);
916f11bdf1Schristos 
926f11bdf1Schristos 	PAM_LOG("Got TTY: %s", (const char *)tty);
936f11bdf1Schristos 
946f11bdf1Schristos 	/* Ignore any "/dev/" on the PAM_TTY item */
956f11bdf1Schristos 	if (tty != NULL && strncmp(TTY_PREFIX, tty, sizeof(TTY_PREFIX)) == 0) {
966f11bdf1Schristos 		PAM_LOG("WARNING: PAM_TTY starts with " TTY_PREFIX);
976f11bdf1Schristos 		tty = (const char *)tty + sizeof(TTY_PREFIX) - 1;
986f11bdf1Schristos 	}
996f11bdf1Schristos 
1006f11bdf1Schristos 	if (tty != NULL && (ty = getttynam(tty)) != NULL &&
1016f11bdf1Schristos 	    (ty->ty_status & TTY_SECURE) != 0)
1026f11bdf1Schristos 		return (PAM_SUCCESS);
1036f11bdf1Schristos 
10498043981Sjnemeth 	pam_err = pam_get_item(pamh, PAM_RHOST, &hostname);
10598043981Sjnemeth 	if (pam_err != PAM_SUCCESS)
10698043981Sjnemeth 		hostname = NULL;
10798043981Sjnemeth 
108e76ecd93Schristos 	openlog_r("pam_securetty", LOG_PID, LOG_AUTHPRIV, &data);
10998043981Sjnemeth 	if (hostname)
110e76ecd93Schristos 		syslog_r(LOG_NOTICE, &data,
11198043981Sjnemeth 		    "LOGIN %s REFUSED FROM %s ON TTY %s",
11298043981Sjnemeth 		     pwd->pw_name, (const char *)hostname,
11398043981Sjnemeth 		     (const char *)tty);
11498043981Sjnemeth 	else
115e76ecd93Schristos 		syslog_r(LOG_NOTICE, &data,
11698043981Sjnemeth 		    "LOGIN %s REFUSED ON TTY %s",
11798043981Sjnemeth 		     pwd->pw_name, (const char *)tty);
118e76ecd93Schristos 	closelog_r(&data);
11998043981Sjnemeth 
12098043981Sjnemeth 
1216f11bdf1Schristos 	PAM_VERBOSE_ERROR("Not on secure TTY");
1226f11bdf1Schristos 	return (PAM_AUTH_ERR);
1236f11bdf1Schristos }
1246f11bdf1Schristos 
1256f11bdf1Schristos PAM_MODULE_ENTRY("pam_securetty");
126