1*11134SCasper.Dik@Sun.COM /*
2*11134SCasper.Dik@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
3*11134SCasper.Dik@Sun.COM * Use is subject to license terms.
4*11134SCasper.Dik@Sun.COM */
50Sstevel@tonic-gate /*
60Sstevel@tonic-gate * Copyright (c) 2001 Kevin Steves. All rights reserved.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
90Sstevel@tonic-gate * modification, are permitted provided that the following conditions
100Sstevel@tonic-gate * are met:
110Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
120Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
130Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
140Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
150Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
160Sstevel@tonic-gate *
170Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
180Sstevel@tonic-gate * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
190Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
200Sstevel@tonic-gate * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
210Sstevel@tonic-gate * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
220Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
230Sstevel@tonic-gate * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
240Sstevel@tonic-gate * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
250Sstevel@tonic-gate * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
260Sstevel@tonic-gate * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
270Sstevel@tonic-gate */
280Sstevel@tonic-gate
290Sstevel@tonic-gate #include "includes.h"
300Sstevel@tonic-gate RCSID("$OpenBSD: groupaccess.c,v 1.5 2002/03/04 17:27:39 stevesk Exp $");
310Sstevel@tonic-gate
320Sstevel@tonic-gate #include "groupaccess.h"
330Sstevel@tonic-gate #include "xmalloc.h"
340Sstevel@tonic-gate #include "match.h"
350Sstevel@tonic-gate #include "log.h"
36*11134SCasper.Dik@Sun.COM #include <alloca.h>
370Sstevel@tonic-gate
38*11134SCasper.Dik@Sun.COM static int ngroups, ngroups_lim;
39*11134SCasper.Dik@Sun.COM static char **groups_byname;
400Sstevel@tonic-gate
410Sstevel@tonic-gate /*
420Sstevel@tonic-gate * Initialize group access list for user with primary (base) and
430Sstevel@tonic-gate * supplementary groups. Return the number of groups in the list.
440Sstevel@tonic-gate */
450Sstevel@tonic-gate int
ga_init(const char * user,gid_t base)460Sstevel@tonic-gate ga_init(const char *user, gid_t base)
470Sstevel@tonic-gate {
48*11134SCasper.Dik@Sun.COM gid_t *groups_bygid;
490Sstevel@tonic-gate int i, j;
500Sstevel@tonic-gate struct group *gr;
510Sstevel@tonic-gate
52*11134SCasper.Dik@Sun.COM if (ngroups_lim == 0) {
53*11134SCasper.Dik@Sun.COM /* Add one for the base gid */
54*11134SCasper.Dik@Sun.COM ngroups_lim = sysconf(_SC_NGROUPS_MAX) + 1;
55*11134SCasper.Dik@Sun.COM groups_byname = malloc(sizeof (char *) * ngroups_lim);
56*11134SCasper.Dik@Sun.COM } else if (ngroups > 0)
570Sstevel@tonic-gate ga_free();
580Sstevel@tonic-gate
59*11134SCasper.Dik@Sun.COM groups_bygid = alloca(ngroups_lim * sizeof (gid_t));
60*11134SCasper.Dik@Sun.COM
61*11134SCasper.Dik@Sun.COM ngroups = ngroups_lim;
620Sstevel@tonic-gate if (getgrouplist(user, base, groups_bygid, &ngroups) == -1)
630Sstevel@tonic-gate log("getgrouplist: groups list too small");
640Sstevel@tonic-gate for (i = 0, j = 0; i < ngroups; i++)
650Sstevel@tonic-gate if ((gr = getgrgid(groups_bygid[i])) != NULL)
660Sstevel@tonic-gate groups_byname[j++] = xstrdup(gr->gr_name);
670Sstevel@tonic-gate return (ngroups = j);
680Sstevel@tonic-gate }
690Sstevel@tonic-gate
700Sstevel@tonic-gate /*
710Sstevel@tonic-gate * Return 1 if one of user's groups is contained in groups.
720Sstevel@tonic-gate * Return 0 otherwise. Use match_pattern() for string comparison.
730Sstevel@tonic-gate */
740Sstevel@tonic-gate int
ga_match(char * const * groups,int n)750Sstevel@tonic-gate ga_match(char * const *groups, int n)
760Sstevel@tonic-gate {
770Sstevel@tonic-gate int i, j;
780Sstevel@tonic-gate
790Sstevel@tonic-gate for (i = 0; i < ngroups; i++)
800Sstevel@tonic-gate for (j = 0; j < n; j++)
810Sstevel@tonic-gate if (match_pattern(groups_byname[i], groups[j]))
82*11134SCasper.Dik@Sun.COM return (1);
83*11134SCasper.Dik@Sun.COM return (0);
840Sstevel@tonic-gate }
850Sstevel@tonic-gate
860Sstevel@tonic-gate /*
8711044SHuie-Ying.Lee@Sun.COM * Return 1 if one of user's groups matches group_pattern list.
8811044SHuie-Ying.Lee@Sun.COM * Return 0 on negated or no match.
8911044SHuie-Ying.Lee@Sun.COM */
9011044SHuie-Ying.Lee@Sun.COM int
ga_match_pattern_list(const char * group_pattern)9111044SHuie-Ying.Lee@Sun.COM ga_match_pattern_list(const char *group_pattern)
9211044SHuie-Ying.Lee@Sun.COM {
9311044SHuie-Ying.Lee@Sun.COM int i, found = 0;
9411044SHuie-Ying.Lee@Sun.COM size_t len = strlen(group_pattern);
9511044SHuie-Ying.Lee@Sun.COM
9611044SHuie-Ying.Lee@Sun.COM for (i = 0; i < ngroups; i++) {
9711044SHuie-Ying.Lee@Sun.COM switch (match_pattern_list(groups_byname[i],
9811044SHuie-Ying.Lee@Sun.COM group_pattern, len, 0)) {
9911044SHuie-Ying.Lee@Sun.COM case -1:
100*11134SCasper.Dik@Sun.COM return (0); /* Negated match wins */
10111044SHuie-Ying.Lee@Sun.COM case 0:
10211044SHuie-Ying.Lee@Sun.COM continue;
10311044SHuie-Ying.Lee@Sun.COM case 1:
10411044SHuie-Ying.Lee@Sun.COM found = 1;
10511044SHuie-Ying.Lee@Sun.COM }
10611044SHuie-Ying.Lee@Sun.COM }
107*11134SCasper.Dik@Sun.COM return (found);
10811044SHuie-Ying.Lee@Sun.COM }
10911044SHuie-Ying.Lee@Sun.COM
11011044SHuie-Ying.Lee@Sun.COM /*
1110Sstevel@tonic-gate * Free memory allocated for group access list.
1120Sstevel@tonic-gate */
1130Sstevel@tonic-gate void
ga_free(void)1140Sstevel@tonic-gate ga_free(void)
1150Sstevel@tonic-gate {
1160Sstevel@tonic-gate int i;
1170Sstevel@tonic-gate
1180Sstevel@tonic-gate if (ngroups > 0) {
1190Sstevel@tonic-gate for (i = 0; i < ngroups; i++)
1200Sstevel@tonic-gate xfree(groups_byname[i]);
1210Sstevel@tonic-gate ngroups = 0;
1220Sstevel@tonic-gate }
1230Sstevel@tonic-gate }
124