2 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
6 * Copyright (c) 2001 Kevin Steves. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 RCSID("$OpenBSD: groupaccess.c,v 1.5 2002/03/04 17:27:39 stevesk Exp $");
32 #include "groupaccess.h"
38 static int ngroups
, ngroups_lim
;
39 static char **groups_byname
;
42 * Initialize group access list for user with primary (base) and
43 * supplementary groups. Return the number of groups in the list.
46 ga_init(const char *user
, gid_t base
)
52 if (ngroups_lim
== 0) {
53 /* Add one for the base gid */
54 ngroups_lim
= sysconf(_SC_NGROUPS_MAX
) + 1;
55 groups_byname
= malloc(sizeof (char *) * ngroups_lim
);
56 } else if (ngroups
> 0)
59 groups_bygid
= alloca(ngroups_lim
* sizeof (gid_t
));
61 ngroups
= ngroups_lim
;
62 if (getgrouplist(user
, base
, groups_bygid
, &ngroups
) == -1)
63 log("getgrouplist: groups list too small");
64 for (i
= 0, j
= 0; i
< ngroups
; i
++)
65 if ((gr
= getgrgid(groups_bygid
[i
])) != NULL
)
66 groups_byname
[j
++] = xstrdup(gr
->gr_name
);
71 * Return 1 if one of user's groups is contained in groups.
72 * Return 0 otherwise. Use match_pattern() for string comparison.
75 ga_match(char * const *groups
, int n
)
79 for (i
= 0; i
< ngroups
; i
++)
80 for (j
= 0; j
< n
; j
++)
81 if (match_pattern(groups_byname
[i
], groups
[j
]))
87 * Return 1 if one of user's groups matches group_pattern list.
88 * Return 0 on negated or no match.
91 ga_match_pattern_list(const char *group_pattern
)
94 size_t len
= strlen(group_pattern
);
96 for (i
= 0; i
< ngroups
; i
++) {
97 switch (match_pattern_list(groups_byname
[i
],
98 group_pattern
, len
, 0)) {
100 return (0); /* Negated match wins */
111 * Free memory allocated for group access list.
119 for (i
= 0; i
< ngroups
; i
++)
120 xfree(groups_byname
[i
]);