17cde604eSDag-Erling Smørgrav /*- 27cde604eSDag-Erling Smørgrav * Copyright (c) 2003 Networks Associates Technology, Inc. 37cde604eSDag-Erling Smørgrav * All rights reserved. 47cde604eSDag-Erling Smørgrav * 57cde604eSDag-Erling Smørgrav * Portions of this software were developed for the FreeBSD Project by 67cde604eSDag-Erling Smørgrav * ThinkSec AS and NAI Labs, the Security Research Division of Network 77cde604eSDag-Erling Smørgrav * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 87cde604eSDag-Erling Smørgrav * ("CBOSS"), as part of the DARPA CHATS research program. 97cde604eSDag-Erling Smørgrav * 107cde604eSDag-Erling Smørgrav * Redistribution and use in source and binary forms, with or without 117cde604eSDag-Erling Smørgrav * modification, are permitted provided that the following conditions 127cde604eSDag-Erling Smørgrav * are met: 137cde604eSDag-Erling Smørgrav * 1. Redistributions of source code must retain the above copyright 147cde604eSDag-Erling Smørgrav * notice, this list of conditions and the following disclaimer. 157cde604eSDag-Erling Smørgrav * 2. Redistributions in binary form must reproduce the above copyright 167cde604eSDag-Erling Smørgrav * notice, this list of conditions and the following disclaimer in the 177cde604eSDag-Erling Smørgrav * documentation and/or other materials provided with the distribution. 187cde604eSDag-Erling Smørgrav * 3. The name of the author may not be used to endorse or promote 197cde604eSDag-Erling Smørgrav * products derived from this software without specific prior written 207cde604eSDag-Erling Smørgrav * permission. 217cde604eSDag-Erling Smørgrav * 227cde604eSDag-Erling Smørgrav * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 237cde604eSDag-Erling Smørgrav * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 247cde604eSDag-Erling Smørgrav * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 257cde604eSDag-Erling Smørgrav * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 267cde604eSDag-Erling Smørgrav * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 277cde604eSDag-Erling Smørgrav * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 287cde604eSDag-Erling Smørgrav * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 297cde604eSDag-Erling Smørgrav * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 307cde604eSDag-Erling Smørgrav * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 317cde604eSDag-Erling Smørgrav * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 327cde604eSDag-Erling Smørgrav * SUCH DAMAGE. 337cde604eSDag-Erling Smørgrav */ 347cde604eSDag-Erling Smørgrav 357cde604eSDag-Erling Smørgrav #include <sys/cdefs.h> 367cde604eSDag-Erling Smørgrav __FBSDID("$FreeBSD$"); 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 497cde604eSDag-Erling Smørgrav 507cde604eSDag-Erling Smørgrav #include <security/pam_appl.h> 517cde604eSDag-Erling Smørgrav #include <security/pam_modules.h> 527cde604eSDag-Erling Smørgrav #include <security/openpam.h> 537cde604eSDag-Erling Smørgrav 547cde604eSDag-Erling Smørgrav 557cde604eSDag-Erling Smørgrav PAM_EXTERN int 567cde604eSDag-Erling Smørgrav pam_sm_authenticate(pam_handle_t *pamh, int flags __unused, 577cde604eSDag-Erling Smørgrav int argc __unused, const char *argv[] __unused) 587cde604eSDag-Erling Smørgrav { 597cde604eSDag-Erling Smørgrav const char *group, *user, *ruser; 607cde604eSDag-Erling Smørgrav char *const *list; 617cde604eSDag-Erling Smørgrav struct passwd *pwd; 627cde604eSDag-Erling Smørgrav struct group *grp; 637cde604eSDag-Erling Smørgrav 647cde604eSDag-Erling Smørgrav /* get target account */ 657cde604eSDag-Erling Smørgrav if (pam_get_item(pamh, PAM_USER, (const void **)&user) != PAM_SUCCESS 667cde604eSDag-Erling Smørgrav || user == NULL || (pwd = getpwnam(user)) == NULL) 677cde604eSDag-Erling Smørgrav return (PAM_AUTH_ERR); 687cde604eSDag-Erling Smørgrav if (pwd->pw_uid != 0 && openpam_get_option(pamh, "root_only")) 697cde604eSDag-Erling Smørgrav return (PAM_IGNORE); 707cde604eSDag-Erling Smørgrav 717cde604eSDag-Erling Smørgrav /* get applicant */ 727cde604eSDag-Erling Smørgrav if (pam_get_item(pamh, PAM_RUSER, (const void **)&ruser) != PAM_SUCCESS 737cde604eSDag-Erling Smørgrav || ruser == NULL || (pwd = getpwnam(ruser)) == NULL) 747cde604eSDag-Erling Smørgrav return (PAM_AUTH_ERR); 757cde604eSDag-Erling Smørgrav 767cde604eSDag-Erling Smørgrav /* get regulating group */ 777cde604eSDag-Erling Smørgrav if ((group = openpam_get_option(pamh, "group")) == NULL) 787cde604eSDag-Erling Smørgrav group = "wheel"; 797cde604eSDag-Erling Smørgrav if ((grp = getgrnam(group)) == NULL || grp->gr_mem == NULL) 807cde604eSDag-Erling Smørgrav goto failed; 817cde604eSDag-Erling Smørgrav 827cde604eSDag-Erling Smørgrav /* check if the group is empty */ 837cde604eSDag-Erling Smørgrav if (*grp->gr_mem == NULL) 847cde604eSDag-Erling Smørgrav goto failed; 857cde604eSDag-Erling Smørgrav 867cde604eSDag-Erling Smørgrav /* check membership */ 877cde604eSDag-Erling Smørgrav if (pwd->pw_gid == grp->gr_gid) 887cde604eSDag-Erling Smørgrav goto found; 897cde604eSDag-Erling Smørgrav for (list = grp->gr_mem; *list != NULL; ++list) 907cde604eSDag-Erling Smørgrav if (strcmp(*list, pwd->pw_name) == 0) 917cde604eSDag-Erling Smørgrav goto found; 927cde604eSDag-Erling Smørgrav 937cde604eSDag-Erling Smørgrav not_found: 947cde604eSDag-Erling Smørgrav fprintf(stderr, "couldn't find %s in %s\n", ruser, group); 957cde604eSDag-Erling Smørgrav if (openpam_get_option(pamh, "deny")) 967cde604eSDag-Erling Smørgrav return (PAM_SUCCESS); 977cde604eSDag-Erling Smørgrav return (PAM_AUTH_ERR); 987cde604eSDag-Erling Smørgrav found: 997cde604eSDag-Erling Smørgrav fprintf(stderr, "found %s in %s\n", ruser, group); 1007cde604eSDag-Erling Smørgrav if (openpam_get_option(pamh, "deny")) 1017cde604eSDag-Erling Smørgrav return (PAM_AUTH_ERR); 1027cde604eSDag-Erling Smørgrav return (PAM_SUCCESS); 1037cde604eSDag-Erling Smørgrav failed: 1047cde604eSDag-Erling Smørgrav if (openpam_get_option(pamh, "fail_safe")) 1057cde604eSDag-Erling Smørgrav goto found; 1067cde604eSDag-Erling Smørgrav else 1077cde604eSDag-Erling Smørgrav goto not_found; 1087cde604eSDag-Erling Smørgrav } 1097cde604eSDag-Erling Smørgrav 1107cde604eSDag-Erling Smørgrav PAM_EXTERN int 1117cde604eSDag-Erling Smørgrav pam_sm_setcred(pam_handle_t * pamh __unused, int flags __unused, 1127cde604eSDag-Erling Smørgrav int argc __unused, const char *argv[] __unused) 1137cde604eSDag-Erling Smørgrav { 1147cde604eSDag-Erling Smørgrav 1157cde604eSDag-Erling Smørgrav return (PAM_SUCCESS); 1167cde604eSDag-Erling Smørgrav } 1177cde604eSDag-Erling Smørgrav 1187cde604eSDag-Erling Smørgrav PAM_MODULE_ENTRY("pam_group"); 119