187396Sdes/*-
287396Sdes * Copyright (c) 2001 Mark R V Murray
387396Sdes * All rights reserved.
493984Sdes * Copyright (c) 2001,2002 Networks Associates Technology, Inc.
587396Sdes * All rights reserved.
687396Sdes *
787396Sdes * Portions of this software were developed for the FreeBSD Project by
887396Sdes * ThinkSec AS and NAI Labs, the Security Research Division of Network
987396Sdes * Associates, Inc.  under DARPA/SPAWAR contract N66001-01-C-8035
1087396Sdes * ("CBOSS"), as part of the DARPA CHATS research program.
1187396Sdes *
1287396Sdes * Redistribution and use in source and binary forms, with or without
1387396Sdes * modification, are permitted provided that the following conditions
1487396Sdes * are met:
1587396Sdes * 1. Redistributions of source code must retain the above copyright
1687396Sdes *    notice, this list of conditions and the following disclaimer.
1787396Sdes * 2. Redistributions in binary form must reproduce the above copyright
1887396Sdes *    notice, this list of conditions and the following disclaimer in the
1987396Sdes *    documentation and/or other materials provided with the distribution.
2087396Sdes * 3. The name of the author may not be used to endorse or promote
2187396Sdes *    products derived from this software without specific prior written
2287396Sdes *    permission.
2387396Sdes *
2487396Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2587396Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2687396Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2787396Sdes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2887396Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2987396Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3087396Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3187396Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3287396Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3387396Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3487396Sdes * SUCH DAMAGE.
3587396Sdes */
3687396Sdes
3787396Sdes#include <sys/cdefs.h>
3887396Sdes__FBSDID("$FreeBSD: releng/10.3/lib/libpam/modules/pam_self/pam_self.c 94564 2002-04-12 22:27:25Z des $");
3987396Sdes
4087396Sdes#define _BSD_SOURCE
4187396Sdes
4289704Sdes#include <pwd.h>
4387396Sdes#include <unistd.h>
4487396Sdes#include <syslog.h>
4587396Sdes
4687396Sdes#define PAM_SM_AUTH
4787396Sdes
4890229Sdes#include <security/pam_appl.h>
4987396Sdes#include <security/pam_modules.h>
5090229Sdes#include <security/pam_mod_misc.h>
5187396Sdes
5294564Sdes#define OPT_ALLOW_ROOT "allow_root"
5389733Sdes
5487396SdesPAM_EXTERN int
5594564Sdespam_sm_authenticate(pam_handle_t *pamh, int flags __unused,
5694564Sdes    int argc __unused, const char *argv[] __unused)
5787396Sdes{
5889704Sdes	struct passwd *pwd;
5989704Sdes	const char *luser;
6089704Sdes	int pam_err;
6189733Sdes	uid_t uid;
6287396Sdes
6393984Sdes	pam_err = pam_get_user(pamh, &luser, NULL);
6489704Sdes	if (pam_err != PAM_SUCCESS)
6594564Sdes		return (pam_err);
6689704Sdes	if (luser == NULL || (pwd = getpwnam(luser)) == NULL)
6794564Sdes		return (PAM_AUTH_ERR);
6887396Sdes
6989733Sdes	uid = getuid();
7094564Sdes	if (uid == 0 && !openpam_get_option(pamh, OPT_ALLOW_ROOT))
7194564Sdes		return (PAM_AUTH_ERR);
7294564Sdes
7389733Sdes	if (uid == (uid_t)pwd->pw_uid)
7494564Sdes		return (PAM_SUCCESS);
7594564Sdes
7687396Sdes	PAM_VERBOSE_ERROR("Refused; source and target users differ");
7787396Sdes
7894564Sdes	return (PAM_AUTH_ERR);
7987396Sdes}
8087396Sdes
8187396SdesPAM_EXTERN int
8294564Sdespam_sm_setcred(pam_handle_t *pamh __unused, int flags __unused,
8394564Sdes    int argc __unused, const char *argv[] __unused)
8487396Sdes{
8587396Sdes
8694564Sdes	return (PAM_SUCCESS);
8787396Sdes}
8887396Sdes
8987396SdesPAM_MODULE_ENTRY("pam_self");
90