17cde604eSDag-Erling Smørgrav /*-
2*5e53a4f9SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
3*5e53a4f9SPedro F. Giffuni *
47cde604eSDag-Erling Smørgrav * Copyright (c) 2003 Networks Associates Technology, Inc.
5e84da6fbSDag-Erling Smørgrav * Copyright (c) 2004-2011 Dag-Erling Smørgrav
67cde604eSDag-Erling Smørgrav * All rights reserved.
77cde604eSDag-Erling Smørgrav *
87cde604eSDag-Erling Smørgrav * Portions of this software were developed for the FreeBSD Project by
97cde604eSDag-Erling Smørgrav * ThinkSec AS and NAI Labs, the Security Research Division of Network
107cde604eSDag-Erling Smørgrav * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
117cde604eSDag-Erling Smørgrav * ("CBOSS"), as part of the DARPA CHATS research program.
127cde604eSDag-Erling Smørgrav *
137cde604eSDag-Erling Smørgrav * Redistribution and use in source and binary forms, with or without
147cde604eSDag-Erling Smørgrav * modification, are permitted provided that the following conditions
157cde604eSDag-Erling Smørgrav * are met:
167cde604eSDag-Erling Smørgrav * 1. Redistributions of source code must retain the above copyright
177cde604eSDag-Erling Smørgrav * notice, this list of conditions and the following disclaimer.
187cde604eSDag-Erling Smørgrav * 2. Redistributions in binary form must reproduce the above copyright
197cde604eSDag-Erling Smørgrav * notice, this list of conditions and the following disclaimer in the
207cde604eSDag-Erling Smørgrav * documentation and/or other materials provided with the distribution.
217cde604eSDag-Erling Smørgrav * 3. The name of the author may not be used to endorse or promote
227cde604eSDag-Erling Smørgrav * products derived from this software without specific prior written
237cde604eSDag-Erling Smørgrav * permission.
247cde604eSDag-Erling Smørgrav *
257cde604eSDag-Erling Smørgrav * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
267cde604eSDag-Erling Smørgrav * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
277cde604eSDag-Erling Smørgrav * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
287cde604eSDag-Erling Smørgrav * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
297cde604eSDag-Erling Smørgrav * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
307cde604eSDag-Erling Smørgrav * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
317cde604eSDag-Erling Smørgrav * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
327cde604eSDag-Erling Smørgrav * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
337cde604eSDag-Erling Smørgrav * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
347cde604eSDag-Erling Smørgrav * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
357cde604eSDag-Erling Smørgrav * SUCH DAMAGE.
367cde604eSDag-Erling Smørgrav */
377cde604eSDag-Erling Smørgrav
387cde604eSDag-Erling Smørgrav #include <sys/types.h>
397cde604eSDag-Erling Smørgrav
407cde604eSDag-Erling Smørgrav #include <grp.h>
417cde604eSDag-Erling Smørgrav #include <pwd.h>
427cde604eSDag-Erling Smørgrav #include <stdarg.h>
437cde604eSDag-Erling Smørgrav #include <stdio.h>
447cde604eSDag-Erling Smørgrav #include <string.h>
457cde604eSDag-Erling Smørgrav #include <syslog.h>
467cde604eSDag-Erling Smørgrav #include <unistd.h>
477cde604eSDag-Erling Smørgrav
487cde604eSDag-Erling Smørgrav #define PAM_SM_AUTH
4916722cb2SDag-Erling Smørgrav #define PAM_SM_ACCOUNT
507cde604eSDag-Erling Smørgrav
517cde604eSDag-Erling Smørgrav #include <security/pam_appl.h>
527cde604eSDag-Erling Smørgrav #include <security/pam_modules.h>
537cde604eSDag-Erling Smørgrav #include <security/openpam.h>
547cde604eSDag-Erling Smørgrav
5516722cb2SDag-Erling Smørgrav static int
pam_group(pam_handle_t * pamh)5616722cb2SDag-Erling Smørgrav pam_group(pam_handle_t *pamh)
577cde604eSDag-Erling Smørgrav {
58e84da6fbSDag-Erling Smørgrav int local, remote;
5991e93869SDag-Erling Smørgrav const char *group, *user;
6091e93869SDag-Erling Smørgrav const void *ruser;
617cde604eSDag-Erling Smørgrav char *const *list;
627cde604eSDag-Erling Smørgrav struct passwd *pwd;
637cde604eSDag-Erling Smørgrav struct group *grp;
647cde604eSDag-Erling Smørgrav
657cde604eSDag-Erling Smørgrav /* get target account */
6648c12730SDag-Erling Smørgrav if (pam_get_user(pamh, &user, NULL) != PAM_SUCCESS ||
6748c12730SDag-Erling Smørgrav user == NULL || (pwd = getpwnam(user)) == NULL)
687cde604eSDag-Erling Smørgrav return (PAM_AUTH_ERR);
697cde604eSDag-Erling Smørgrav if (pwd->pw_uid != 0 && openpam_get_option(pamh, "root_only"))
707cde604eSDag-Erling Smørgrav return (PAM_IGNORE);
717cde604eSDag-Erling Smørgrav
72e84da6fbSDag-Erling Smørgrav /* check local / remote */
73e84da6fbSDag-Erling Smørgrav local = openpam_get_option(pamh, "luser") ? 1 : 0;
74e84da6fbSDag-Erling Smørgrav remote = openpam_get_option(pamh, "ruser") ? 1 : 0;
75e84da6fbSDag-Erling Smørgrav if (local && remote) {
76864cac07SDag-Erling Smørgrav openpam_log(PAM_LOG_ERROR, "(pam_group) "
77e84da6fbSDag-Erling Smørgrav "the luser and ruser options are mutually exclusive");
78e84da6fbSDag-Erling Smørgrav return (PAM_SERVICE_ERR);
79e84da6fbSDag-Erling Smørgrav } else if (local) {
80e84da6fbSDag-Erling Smørgrav /* we already have the correct struct passwd */
81e84da6fbSDag-Erling Smørgrav } else {
82e84da6fbSDag-Erling Smørgrav if (!remote)
83864cac07SDag-Erling Smørgrav openpam_log(PAM_LOG_NOTICE, "(pam_group) "
84e84da6fbSDag-Erling Smørgrav "neither luser nor ruser specified, assuming ruser");
85e84da6fbSDag-Erling Smørgrav /* default / historical behavior */
86e84da6fbSDag-Erling Smørgrav if (pam_get_item(pamh, PAM_RUSER, &ruser) != PAM_SUCCESS ||
87e84da6fbSDag-Erling Smørgrav ruser == NULL || (pwd = getpwnam(ruser)) == NULL)
887cde604eSDag-Erling Smørgrav return (PAM_AUTH_ERR);
89e84da6fbSDag-Erling Smørgrav }
907cde604eSDag-Erling Smørgrav
917cde604eSDag-Erling Smørgrav /* get regulating group */
927cde604eSDag-Erling Smørgrav if ((group = openpam_get_option(pamh, "group")) == NULL)
937cde604eSDag-Erling Smørgrav group = "wheel";
947cde604eSDag-Erling Smørgrav if ((grp = getgrnam(group)) == NULL || grp->gr_mem == NULL)
957cde604eSDag-Erling Smørgrav goto failed;
967cde604eSDag-Erling Smørgrav
97ec5622adSDag-Erling Smørgrav /* check if user's own primary group */
987cde604eSDag-Erling Smørgrav if (pwd->pw_gid == grp->gr_gid)
997cde604eSDag-Erling Smørgrav goto found;
100ec5622adSDag-Erling Smørgrav
101ec5622adSDag-Erling Smørgrav /* iterate over members */
102ec5622adSDag-Erling Smørgrav for (list = grp->gr_mem; list != NULL && *list != NULL; ++list)
1037cde604eSDag-Erling Smørgrav if (strcmp(*list, pwd->pw_name) == 0)
1047cde604eSDag-Erling Smørgrav goto found;
1057cde604eSDag-Erling Smørgrav
1067cde604eSDag-Erling Smørgrav not_found:
1077cde604eSDag-Erling Smørgrav if (openpam_get_option(pamh, "deny"))
1087cde604eSDag-Erling Smørgrav return (PAM_SUCCESS);
1097cde604eSDag-Erling Smørgrav return (PAM_AUTH_ERR);
1107cde604eSDag-Erling Smørgrav found:
1117cde604eSDag-Erling Smørgrav if (openpam_get_option(pamh, "deny"))
1127cde604eSDag-Erling Smørgrav return (PAM_AUTH_ERR);
1137cde604eSDag-Erling Smørgrav return (PAM_SUCCESS);
1147cde604eSDag-Erling Smørgrav failed:
1157cde604eSDag-Erling Smørgrav if (openpam_get_option(pamh, "fail_safe"))
1167cde604eSDag-Erling Smørgrav goto found;
1177cde604eSDag-Erling Smørgrav else
1187cde604eSDag-Erling Smørgrav goto not_found;
1197cde604eSDag-Erling Smørgrav }
1207cde604eSDag-Erling Smørgrav
1217cde604eSDag-Erling Smørgrav PAM_EXTERN int
pam_sm_authenticate(pam_handle_t * pamh,int flags __unused,int argc __unused,const char * argv[]__unused)12216722cb2SDag-Erling Smørgrav pam_sm_authenticate(pam_handle_t *pamh, int flags __unused,
12316722cb2SDag-Erling Smørgrav int argc __unused, const char *argv[] __unused)
12416722cb2SDag-Erling Smørgrav {
12516722cb2SDag-Erling Smørgrav
12616722cb2SDag-Erling Smørgrav return (pam_group(pamh));
12716722cb2SDag-Erling Smørgrav }
12816722cb2SDag-Erling Smørgrav
12916722cb2SDag-Erling Smørgrav PAM_EXTERN int
pam_sm_setcred(pam_handle_t * pamh __unused,int flags __unused,int argc __unused,const char * argv[]__unused)1307cde604eSDag-Erling Smørgrav pam_sm_setcred(pam_handle_t * pamh __unused, int flags __unused,
1317cde604eSDag-Erling Smørgrav int argc __unused, const char *argv[] __unused)
1327cde604eSDag-Erling Smørgrav {
1337cde604eSDag-Erling Smørgrav
1347cde604eSDag-Erling Smørgrav return (PAM_SUCCESS);
1357cde604eSDag-Erling Smørgrav }
1367cde604eSDag-Erling Smørgrav
13716722cb2SDag-Erling Smørgrav PAM_EXTERN int
pam_sm_acct_mgmt(pam_handle_t * pamh,int flags __unused,int argc __unused,const char * argv[]__unused)13816722cb2SDag-Erling Smørgrav pam_sm_acct_mgmt(pam_handle_t *pamh, int flags __unused,
13916722cb2SDag-Erling Smørgrav int argc __unused, const char *argv[] __unused)
14016722cb2SDag-Erling Smørgrav {
14116722cb2SDag-Erling Smørgrav
14216722cb2SDag-Erling Smørgrav return (pam_group(pamh));
14316722cb2SDag-Erling Smørgrav }
14416722cb2SDag-Erling Smørgrav
1457cde604eSDag-Erling Smørgrav PAM_MODULE_ENTRY("pam_group");
146