xref: /dflybsd-src/lib/libpam/modules/pam_rhosts/pam_rhosts.c (revision c98db40744766ab0803912f29557df02814bcd9d)
1242be47eSzrj /*-
2*c98db407SSascha Wildner  * SPDX-License-Identifier: BSD-3-Clause
3*c98db407SSascha Wildner  *
4242be47eSzrj  * Copyright (c) 2002 Danny Braniss
5242be47eSzrj  * All rights reserved.
6242be47eSzrj  * Copyright (c) 2001,2002 Networks Associates Technology, Inc.
7242be47eSzrj  * All rights reserved.
8242be47eSzrj  *
9242be47eSzrj  * Portions of this software were developed for the FreeBSD Project by
10242be47eSzrj  * ThinkSec AS and NAI Labs, the Security Research Division of Network
11242be47eSzrj  * Associates, Inc.  under DARPA/SPAWAR contract N66001-01-C-8035
12242be47eSzrj  * ("CBOSS"), as part of the DARPA CHATS research program.
13242be47eSzrj  *
14242be47eSzrj  * Redistribution and use in source and binary forms, with or without
15242be47eSzrj  * modification, are permitted provided that the following conditions
16242be47eSzrj  * are met:
17242be47eSzrj  * 1. Redistributions of source code must retain the above copyright
18242be47eSzrj  *    notice, this list of conditions and the following disclaimer.
19242be47eSzrj  * 2. Redistributions in binary form must reproduce the above copyright
20242be47eSzrj  *    notice, this list of conditions and the following disclaimer in the
21242be47eSzrj  *    documentation and/or other materials provided with the distribution.
22242be47eSzrj  * 3. The name of the author may not be used to endorse or promote
23242be47eSzrj  *    products derived from this software without specific prior written
24242be47eSzrj  *    permission.
25242be47eSzrj  *
26242be47eSzrj  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
27242be47eSzrj  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28242be47eSzrj  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29242be47eSzrj  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
30242be47eSzrj  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31242be47eSzrj  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32242be47eSzrj  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33242be47eSzrj  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34242be47eSzrj  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35242be47eSzrj  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36242be47eSzrj  * SUCH DAMAGE.
37242be47eSzrj  *
38*c98db407SSascha Wildner  * $FreeBSD: head/lib/libpam/modules/pam_rhosts/pam_rhosts.c 326219 2017-11-26 02:00:33Z pfg $
39242be47eSzrj  */
40242be47eSzrj 
41242be47eSzrj #include <pwd.h>
42242be47eSzrj #include <stddef.h>
43242be47eSzrj #include <string.h>
44242be47eSzrj #include <unistd.h>
45242be47eSzrj 
46242be47eSzrj #define PAM_SM_AUTH
47242be47eSzrj #include <security/pam_appl.h>
48242be47eSzrj #include <security/pam_modules.h>
49242be47eSzrj #include <security/pam_mod_misc.h>
50242be47eSzrj 
51242be47eSzrj #define OPT_ALLOW_ROOT "allow_root"
52242be47eSzrj 
53242be47eSzrj PAM_EXTERN int
pam_sm_authenticate(pam_handle_t * pamh,int flags __unused,int argc __unused,const char * argv[]__unused)54242be47eSzrj pam_sm_authenticate(pam_handle_t *pamh, int flags __unused,
55242be47eSzrj     int argc __unused, const char *argv[] __unused)
56242be47eSzrj {
57242be47eSzrj 	struct passwd *pw;
58242be47eSzrj 	const char *user;
59242be47eSzrj 	const void *ruser, *rhost;
60242be47eSzrj 	int err, superuser;
61242be47eSzrj 
62242be47eSzrj 	err = pam_get_user(pamh, &user, NULL);
63242be47eSzrj 	if (err != PAM_SUCCESS)
64242be47eSzrj 		return (err);
65242be47eSzrj 
66242be47eSzrj 	if ((pw = getpwnam(user)) == NULL)
67242be47eSzrj 		return (PAM_USER_UNKNOWN);
68242be47eSzrj 	if (pw->pw_uid == 0 &&
69242be47eSzrj 	    openpam_get_option(pamh, OPT_ALLOW_ROOT) == NULL)
70242be47eSzrj 		return (PAM_AUTH_ERR);
71242be47eSzrj 
72242be47eSzrj 	err = pam_get_item(pamh, PAM_RUSER, &ruser);
73242be47eSzrj 	if (err != PAM_SUCCESS)
74242be47eSzrj 		return (PAM_AUTH_ERR);
75242be47eSzrj 
76242be47eSzrj 	err = pam_get_item(pamh, PAM_RHOST, &rhost);
77242be47eSzrj 	if (err != PAM_SUCCESS)
78242be47eSzrj 		return (PAM_AUTH_ERR);
79242be47eSzrj 
80242be47eSzrj 	superuser = (strcmp(user, "root") == 0);
81242be47eSzrj 	err = ruserok(rhost, superuser, ruser, user);
82242be47eSzrj 	if (err != 0)
83242be47eSzrj 		return (PAM_AUTH_ERR);
84242be47eSzrj 
85242be47eSzrj 	return (PAM_SUCCESS);
86242be47eSzrj }
87242be47eSzrj 
88242be47eSzrj PAM_EXTERN int
pam_sm_setcred(pam_handle_t * pamh __unused,int flags __unused,int argc __unused,const char * argv[]__unused)89242be47eSzrj pam_sm_setcred(pam_handle_t *pamh __unused, int flags __unused,
90242be47eSzrj     int argc __unused, const char *argv[] __unused)
91242be47eSzrj {
92242be47eSzrj 
93242be47eSzrj 	return (PAM_SUCCESS);
94242be47eSzrj }
95242be47eSzrj 
96242be47eSzrj PAM_MODULE_ENTRY("pam_rhosts");
97