pam_securetty.c revision 81475
185587Sobrien/*-
285587Sobrien * Copyright (c) 2001 Mark R V Murray
385587Sobrien * All rights reserved.
485587Sobrien *
585587Sobrien * Redistribution and use in source and binary forms, with or without
685587Sobrien * modification, are permitted provided that the following conditions
785587Sobrien * are met:
885587Sobrien * 1. Redistributions of source code must retain the above copyright
985587Sobrien *    notice, this list of conditions and the following disclaimer.
1085587Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1185587Sobrien *    notice, this list of conditions and the following disclaimer in the
1285587Sobrien *    documentation and/or other materials provided with the distribution.
1385587Sobrien *
1485587Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1585587Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1685587Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1785587Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1885587Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1985587Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2085587Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2185587Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2285587Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2385587Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2485587Sobrien * SUCH DAMAGE.
2585587Sobrien *
2685587Sobrien * $FreeBSD: head/lib/libpam/modules/pam_securetty/pam_securetty.c 81475 2001-08-10 19:18:52Z markm $
2785587Sobrien */
2885587Sobrien
2985587Sobrien#include <sys/types.h>
3085587Sobrien#include <sys/stat.h>
3185587Sobrien#include <pwd.h>
3285587Sobrien#include <ttyent.h>
3385587Sobrien#include <string.h>
3485587Sobrien
3585587Sobrien#define PAM_SM_AUTH
3685587Sobrien#include <security/pam_modules.h>
3785587Sobrien#include <pam_mod_misc.h>
3885587Sobrien
3985587Sobrien#define TTY_PREFIX	"/dev/"
4085587Sobrien
4185587SobrienPAM_EXTERN int
4285587Sobrienpam_sm_authenticate(pam_handle_t * pamh, int flags, int argc, const char **argv)
4385587Sobrien{
4485587Sobrien	struct options options;
4585587Sobrien	struct ttyent *ttyfileinfo;
4685587Sobrien	struct passwd *pwd;
4785587Sobrien	int retval;
4885587Sobrien	const char *user, *ttyname;
4985587Sobrien
5085587Sobrien	pam_std_option(&options, NULL, argc, argv);
5185587Sobrien
5285587Sobrien	PAM_LOG("Options processed");
5385587Sobrien
5485587Sobrien	retval = pam_get_user(pamh, &user, NULL);
5585587Sobrien	if (retval != PAM_SUCCESS)
5685587Sobrien		PAM_RETURN(retval);
5785587Sobrien
5885587Sobrien	PAM_LOG("Got user: %s", user);
5985587Sobrien
6085587Sobrien	retval = pam_get_item(pamh, PAM_TTY, (const void **)&ttyname);
6185587Sobrien	if (retval != PAM_SUCCESS)
6285587Sobrien		PAM_RETURN(retval);
6385587Sobrien
6485587Sobrien	PAM_LOG("Got TTY: %s", ttyname);
6585587Sobrien
6685587Sobrien	/* Ignore any "/dev/" on the PAM_TTY item */
6785587Sobrien	if (strncmp(TTY_PREFIX, ttyname, sizeof(TTY_PREFIX) - 1) == 0)
6885587Sobrien		ttyname += sizeof(TTY_PREFIX) - 1;
6985587Sobrien
7085587Sobrien	/* If the user is not root, secure ttys do not apply */
7185587Sobrien	pwd = getpwnam(user);
7285587Sobrien	if (pwd == NULL)
7385587Sobrien		PAM_RETURN(PAM_IGNORE);
7485587Sobrien	else if (pwd->pw_uid != 0)
7585587Sobrien		PAM_RETURN(PAM_SUCCESS);
7685587Sobrien
7785587Sobrien	PAM_LOG("User is not root");
7885587Sobrien
7985587Sobrien	ttyfileinfo = getttynam(ttyname);
8085587Sobrien	if (ttyfileinfo == NULL)
8185587Sobrien		PAM_RETURN(PAM_SERVICE_ERR);
8285587Sobrien
8385587Sobrien	PAM_LOG("Got ttyfileinfo");
8485587Sobrien
8585587Sobrien	if (ttyfileinfo->ty_status & TTY_SECURE)
8685587Sobrien		PAM_RETURN(PAM_SUCCESS);
8785587Sobrien	else {
8885587Sobrien		PAM_VERBOSE_ERROR("Not on secure TTY");
8985587Sobrien		PAM_RETURN(PAM_PERM_DENIED);
9085587Sobrien	}
9185587Sobrien}
9285587Sobrien
9385587SobrienPAM_EXTERN
9485587Sobrienint
9585587Sobrienpam_sm_setcred(pam_handle_t * pamh, int flags, int argc, const char **argv)
9690902Sdes{
9785587Sobrien	struct options options;
9885587Sobrien
9985587Sobrien	pam_std_option(&options, NULL, argc, argv);
10085587Sobrien
10185587Sobrien	PAM_LOG("Options processed");
10285587Sobrien
10385587Sobrien	PAM_RETURN(PAM_SUCCESS);
10485587Sobrien}
10585587Sobrien
10685587SobrienPAM_MODULE_ENTRY("pam_securetty");
10785587Sobrien