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