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