pam_group.c revision 219564
1/*-
2 * Copyright (c) 2003 Networks Associates Technology, Inc.
3 * Copyright (c) 2004-2011 Dag-Erling Sm��rgrav
4 * All rights reserved.
5 *
6 * Portions of this software were developed for the FreeBSD Project by
7 * ThinkSec AS and NAI Labs, the Security Research Division of Network
8 * Associates, Inc.  under DARPA/SPAWAR contract N66001-01-C-8035
9 * ("CBOSS"), as part of the DARPA CHATS research program.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote
20 *    products derived from this software without specific prior written
21 *    permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36#include <sys/cdefs.h>
37__FBSDID("$FreeBSD: head/lib/libpam/modules/pam_group/pam_group.c 219564 2011-03-12 11:26:37Z des $");
38
39#include <sys/types.h>
40
41#include <grp.h>
42#include <pwd.h>
43#include <stdarg.h>
44#include <stdio.h>
45#include <string.h>
46#include <syslog.h>
47#include <unistd.h>
48
49#define PAM_SM_AUTH
50
51#include <security/pam_appl.h>
52#include <security/pam_modules.h>
53#include <security/openpam.h>
54
55
56PAM_EXTERN int
57pam_sm_authenticate(pam_handle_t *pamh, int flags __unused,
58    int argc __unused, const char *argv[] __unused)
59{
60	int local, remote;
61	const char *group, *user;
62	const void *ruser;
63	char *const *list;
64	struct passwd *pwd;
65	struct group *grp;
66
67	/* get target account */
68	if (pam_get_user(pamh, &user, NULL) != PAM_SUCCESS ||
69	    user == NULL || (pwd = getpwnam(user)) == NULL)
70		return (PAM_AUTH_ERR);
71	if (pwd->pw_uid != 0 && openpam_get_option(pamh, "root_only"))
72		return (PAM_IGNORE);
73
74	/* check local / remote */
75	local = openpam_get_option(pamh, "luser") ? 1 : 0;
76	remote = openpam_get_option(pamh, "ruser") ? 1 : 0;
77	if (local && remote) {
78		openpam_log(PAM_LOG_ERROR, "(pam_group) "
79		    "the luser and ruser options are mutually exclusive");
80		return (PAM_SERVICE_ERR);
81	} else if (local) {
82		/* we already have the correct struct passwd */
83	} else {
84		if (!remote)
85			openpam_log(PAM_LOG_NOTICE, "(pam_group) "
86			    "neither luser nor ruser specified, assuming ruser");
87		/* default / historical behavior */
88		if (pam_get_item(pamh, PAM_RUSER, &ruser) != PAM_SUCCESS ||
89		    ruser == NULL || (pwd = getpwnam(ruser)) == NULL)
90			return (PAM_AUTH_ERR);
91	}
92
93	/* get regulating group */
94	if ((group = openpam_get_option(pamh, "group")) == NULL)
95		group = "wheel";
96	if ((grp = getgrnam(group)) == NULL || grp->gr_mem == NULL)
97		goto failed;
98
99	/* check if the group is empty */
100	if (*grp->gr_mem == NULL)
101		goto failed;
102
103	/* check membership */
104	if (pwd->pw_gid == grp->gr_gid)
105		goto found;
106	for (list = grp->gr_mem; *list != NULL; ++list)
107		if (strcmp(*list, pwd->pw_name) == 0)
108			goto found;
109
110 not_found:
111	if (openpam_get_option(pamh, "deny"))
112		return (PAM_SUCCESS);
113	return (PAM_AUTH_ERR);
114 found:
115	if (openpam_get_option(pamh, "deny"))
116		return (PAM_AUTH_ERR);
117	return (PAM_SUCCESS);
118 failed:
119	if (openpam_get_option(pamh, "fail_safe"))
120		goto found;
121	else
122		goto not_found;
123}
124
125PAM_EXTERN int
126pam_sm_setcred(pam_handle_t * pamh __unused, int flags __unused,
127    int argc __unused, const char *argv[] __unused)
128{
129
130	return (PAM_SUCCESS);
131}
132
133PAM_MODULE_ENTRY("pam_group");
134