17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * Copyright (c) 2001 Kevin Steves. All rights reserved. 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 57c478bd9Sstevel@tonic-gate * modification, are permitted provided that the following conditions 67c478bd9Sstevel@tonic-gate * are met: 77c478bd9Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright 87c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 97c478bd9Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 107c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the 117c478bd9Sstevel@tonic-gate * documentation and/or other materials provided with the distribution. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 147c478bd9Sstevel@tonic-gate * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 157c478bd9Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 167c478bd9Sstevel@tonic-gate * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 177c478bd9Sstevel@tonic-gate * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 187c478bd9Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 197c478bd9Sstevel@tonic-gate * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 207c478bd9Sstevel@tonic-gate * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 217c478bd9Sstevel@tonic-gate * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 227c478bd9Sstevel@tonic-gate * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 237c478bd9Sstevel@tonic-gate */ 247c478bd9Sstevel@tonic-gate 257c478bd9Sstevel@tonic-gate #include "includes.h" 267c478bd9Sstevel@tonic-gate RCSID("$OpenBSD: groupaccess.c,v 1.5 2002/03/04 17:27:39 stevesk Exp $"); 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate #include "groupaccess.h" 297c478bd9Sstevel@tonic-gate #include "xmalloc.h" 307c478bd9Sstevel@tonic-gate #include "match.h" 317c478bd9Sstevel@tonic-gate #include "log.h" 327c478bd9Sstevel@tonic-gate 337c478bd9Sstevel@tonic-gate static int ngroups; 34743541abSjp161948 static char *groups_byname[NGROUPS_UMAX + 1]; /* +1 for base/primary group */ 357c478bd9Sstevel@tonic-gate 367c478bd9Sstevel@tonic-gate /* 377c478bd9Sstevel@tonic-gate * Initialize group access list for user with primary (base) and 387c478bd9Sstevel@tonic-gate * supplementary groups. Return the number of groups in the list. 397c478bd9Sstevel@tonic-gate */ 407c478bd9Sstevel@tonic-gate int 417c478bd9Sstevel@tonic-gate ga_init(const char *user, gid_t base) 427c478bd9Sstevel@tonic-gate { 43743541abSjp161948 gid_t groups_bygid[NGROUPS_UMAX + 1]; 447c478bd9Sstevel@tonic-gate int i, j; 457c478bd9Sstevel@tonic-gate struct group *gr; 467c478bd9Sstevel@tonic-gate 477c478bd9Sstevel@tonic-gate if (ngroups > 0) 487c478bd9Sstevel@tonic-gate ga_free(); 497c478bd9Sstevel@tonic-gate 507c478bd9Sstevel@tonic-gate ngroups = sizeof(groups_bygid) / sizeof(gid_t); 517c478bd9Sstevel@tonic-gate if (getgrouplist(user, base, groups_bygid, &ngroups) == -1) 527c478bd9Sstevel@tonic-gate log("getgrouplist: groups list too small"); 537c478bd9Sstevel@tonic-gate for (i = 0, j = 0; i < ngroups; i++) 547c478bd9Sstevel@tonic-gate if ((gr = getgrgid(groups_bygid[i])) != NULL) 557c478bd9Sstevel@tonic-gate groups_byname[j++] = xstrdup(gr->gr_name); 567c478bd9Sstevel@tonic-gate return (ngroups = j); 577c478bd9Sstevel@tonic-gate } 587c478bd9Sstevel@tonic-gate 597c478bd9Sstevel@tonic-gate /* 607c478bd9Sstevel@tonic-gate * Return 1 if one of user's groups is contained in groups. 617c478bd9Sstevel@tonic-gate * Return 0 otherwise. Use match_pattern() for string comparison. 627c478bd9Sstevel@tonic-gate */ 637c478bd9Sstevel@tonic-gate int 647c478bd9Sstevel@tonic-gate ga_match(char * const *groups, int n) 657c478bd9Sstevel@tonic-gate { 667c478bd9Sstevel@tonic-gate int i, j; 677c478bd9Sstevel@tonic-gate 687c478bd9Sstevel@tonic-gate for (i = 0; i < ngroups; i++) 697c478bd9Sstevel@tonic-gate for (j = 0; j < n; j++) 707c478bd9Sstevel@tonic-gate if (match_pattern(groups_byname[i], groups[j])) 717c478bd9Sstevel@tonic-gate return 1; 727c478bd9Sstevel@tonic-gate return 0; 737c478bd9Sstevel@tonic-gate } 747c478bd9Sstevel@tonic-gate 757c478bd9Sstevel@tonic-gate /* 76*b07b2f5cSHuie-Ying Lee * Return 1 if one of user's groups matches group_pattern list. 77*b07b2f5cSHuie-Ying Lee * Return 0 on negated or no match. 78*b07b2f5cSHuie-Ying Lee */ 79*b07b2f5cSHuie-Ying Lee int 80*b07b2f5cSHuie-Ying Lee ga_match_pattern_list(const char *group_pattern) 81*b07b2f5cSHuie-Ying Lee { 82*b07b2f5cSHuie-Ying Lee int i, found = 0; 83*b07b2f5cSHuie-Ying Lee size_t len = strlen(group_pattern); 84*b07b2f5cSHuie-Ying Lee 85*b07b2f5cSHuie-Ying Lee for (i = 0; i < ngroups; i++) { 86*b07b2f5cSHuie-Ying Lee switch (match_pattern_list(groups_byname[i], 87*b07b2f5cSHuie-Ying Lee group_pattern, len, 0)) { 88*b07b2f5cSHuie-Ying Lee case -1: 89*b07b2f5cSHuie-Ying Lee return 0; /* Negated match wins */ 90*b07b2f5cSHuie-Ying Lee case 0: 91*b07b2f5cSHuie-Ying Lee continue; 92*b07b2f5cSHuie-Ying Lee case 1: 93*b07b2f5cSHuie-Ying Lee found = 1; 94*b07b2f5cSHuie-Ying Lee } 95*b07b2f5cSHuie-Ying Lee } 96*b07b2f5cSHuie-Ying Lee return found; 97*b07b2f5cSHuie-Ying Lee } 98*b07b2f5cSHuie-Ying Lee 99*b07b2f5cSHuie-Ying Lee /* 1007c478bd9Sstevel@tonic-gate * Free memory allocated for group access list. 1017c478bd9Sstevel@tonic-gate */ 1027c478bd9Sstevel@tonic-gate void 1037c478bd9Sstevel@tonic-gate ga_free(void) 1047c478bd9Sstevel@tonic-gate { 1057c478bd9Sstevel@tonic-gate int i; 1067c478bd9Sstevel@tonic-gate 1077c478bd9Sstevel@tonic-gate if (ngroups > 0) { 1087c478bd9Sstevel@tonic-gate for (i = 0; i < ngroups; i++) 1097c478bd9Sstevel@tonic-gate xfree(groups_byname[i]); 1107c478bd9Sstevel@tonic-gate ngroups = 0; 1117c478bd9Sstevel@tonic-gate } 1127c478bd9Sstevel@tonic-gate } 113