Home | History | Annotate | Line # | Download | only in dist
      1   1.3  christos /*	$NetBSD: groupaccess.c,v 1.10 2025/04/09 15:49:32 christos Exp $	*/
      2  1.10  christos /* $OpenBSD: groupaccess.c,v 1.18 2024/11/04 21:59:15 jca Exp $ */
      3  1.10  christos 
      4   1.1  christos /*
      5   1.1  christos  * Copyright (c) 2001 Kevin Steves.  All rights reserved.
      6   1.1  christos  *
      7   1.1  christos  * Redistribution and use in source and binary forms, with or without
      8   1.1  christos  * modification, are permitted provided that the following conditions
      9   1.1  christos  * are met:
     10   1.1  christos  * 1. Redistributions of source code must retain the above copyright
     11   1.1  christos  *    notice, this list of conditions and the following disclaimer.
     12   1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     14   1.1  christos  *    documentation and/or other materials provided with the distribution.
     15   1.1  christos  *
     16   1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17   1.1  christos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18   1.1  christos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19   1.1  christos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20   1.1  christos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21   1.1  christos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22   1.1  christos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23   1.1  christos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24   1.1  christos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25   1.1  christos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26   1.1  christos  */
     27   1.1  christos 
     28   1.2  christos #include "includes.h"
     29   1.3  christos __RCSID("$NetBSD: groupaccess.c,v 1.10 2025/04/09 15:49:32 christos Exp $");
     30   1.1  christos #include <sys/types.h>
     31   1.1  christos 
     32   1.1  christos #include <grp.h>
     33   1.1  christos #include <unistd.h>
     34   1.1  christos #include <stdarg.h>
     35   1.3  christos #include <stdlib.h>
     36   1.1  christos #include <string.h>
     37   1.5  christos #include <limits.h>
     38   1.1  christos 
     39   1.1  christos #include "xmalloc.h"
     40   1.1  christos #include "groupaccess.h"
     41   1.1  christos #include "match.h"
     42   1.1  christos #include "log.h"
     43   1.1  christos 
     44   1.1  christos static int ngroups;
     45   1.1  christos static char *groups_byname[NGROUPS_MAX + 1];	/* +1 for base/primary group */
     46   1.1  christos 
     47   1.1  christos /*
     48   1.1  christos  * Initialize group access list for user with primary (base) and
     49   1.1  christos  * supplementary groups.  Return the number of groups in the list.
     50   1.1  christos  */
     51   1.1  christos int
     52   1.1  christos ga_init(const char *user, gid_t base)
     53   1.1  christos {
     54   1.1  christos 	gid_t groups_bygid[NGROUPS_MAX + 1];
     55  1.10  christos 	int i, j, maxgroups;
     56   1.1  christos 	struct group *gr;
     57   1.1  christos 
     58   1.1  christos 	if (ngroups > 0)
     59   1.1  christos 		ga_free();
     60   1.1  christos 
     61  1.10  christos 	maxgroups = ngroups = sizeof(groups_bygid) / sizeof(gid_t);
     62  1.10  christos 	if (getgrouplist(user, base, groups_bygid, &ngroups) == -1) {
     63   1.1  christos 		logit("getgrouplist: groups list too small");
     64  1.10  christos 		/* Truncate group list */
     65  1.10  christos 		ngroups = maxgroups;
     66  1.10  christos 	}
     67   1.1  christos 	for (i = 0, j = 0; i < ngroups; i++)
     68   1.1  christos 		if ((gr = getgrgid(groups_bygid[i])) != NULL)
     69   1.1  christos 			groups_byname[j++] = xstrdup(gr->gr_name);
     70   1.1  christos 	return (ngroups = j);
     71   1.1  christos }
     72   1.1  christos 
     73   1.1  christos /*
     74   1.1  christos  * Return 1 if one of user's groups is contained in groups.
     75   1.1  christos  * Return 0 otherwise.  Use match_pattern() for string comparison.
     76   1.1  christos  */
     77   1.1  christos int
     78   1.1  christos ga_match(char * const *groups, int n)
     79   1.1  christos {
     80   1.1  christos 	int i, j;
     81   1.1  christos 
     82   1.1  christos 	for (i = 0; i < ngroups; i++)
     83   1.1  christos 		for (j = 0; j < n; j++)
     84   1.1  christos 			if (match_pattern(groups_byname[i], groups[j]))
     85   1.1  christos 				return 1;
     86   1.1  christos 	return 0;
     87   1.1  christos }
     88   1.1  christos 
     89   1.1  christos /*
     90   1.1  christos  * Return 1 if one of user's groups matches group_pattern list.
     91   1.1  christos  * Return 0 on negated or no match.
     92   1.1  christos  */
     93   1.1  christos int
     94   1.1  christos ga_match_pattern_list(const char *group_pattern)
     95   1.1  christos {
     96   1.1  christos 	int i, found = 0;
     97   1.1  christos 
     98   1.1  christos 	for (i = 0; i < ngroups; i++) {
     99   1.9  christos 		switch (match_usergroup_pattern_list(groups_byname[i],
    100   1.9  christos 		    group_pattern)) {
    101   1.1  christos 		case -1:
    102   1.1  christos 			return 0;	/* Negated match wins */
    103   1.1  christos 		case 0:
    104   1.1  christos 			continue;
    105   1.1  christos 		case 1:
    106   1.1  christos 			found = 1;
    107   1.1  christos 		}
    108   1.1  christos 	}
    109   1.1  christos 	return found;
    110   1.1  christos }
    111   1.1  christos 
    112   1.1  christos /*
    113   1.1  christos  * Free memory allocated for group access list.
    114   1.1  christos  */
    115   1.1  christos void
    116   1.1  christos ga_free(void)
    117   1.1  christos {
    118   1.1  christos 	int i;
    119   1.1  christos 
    120   1.1  christos 	if (ngroups > 0) {
    121   1.1  christos 		for (i = 0; i < ngroups; i++)
    122   1.3  christos 			free(groups_byname[i]);
    123   1.1  christos 		ngroups = 0;
    124   1.1  christos 	}
    125   1.1  christos }
    126