]> andersk Git - openssh.git/blame - groupaccess.c
- djm@cvs.openbsd.org 2006/03/25 01:30:23
[openssh.git] / groupaccess.c
CommitLineData
63b68889 1/*
2 * Copyright (c) 2001 Kevin Steves. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25#include "includes.h"
26
27#include "groupaccess.h"
63b68889 28#include "xmalloc.h"
29#include "match.h"
42f11eb2 30#include "log.h"
63b68889 31
32static int ngroups;
13dff404 33static char **groups_byname;
63b68889 34
255cabd9 35/*
36 * Initialize group access list for user with primary (base) and
37 * supplementary groups. Return the number of groups in the list.
38 */
63b68889 39int
40ga_init(const char *user, gid_t base)
41{
13dff404 42 gid_t *groups_bygid;
63b68889 43 int i, j;
44 struct group *gr;
45
46 if (ngroups > 0)
47 ga_free();
48
13dff404 49 ngroups = NGROUPS_MAX;
50#if defined(HAVE_SYSCONF) && defined(_SC_NGROUPS_MAX)
51 ngroups = MAX(NGROUPS_MAX, sysconf(_SC_NGROUPS_MAX));
52#endif
53
54 groups_bygid = xmalloc(ngroups * sizeof(*groups_bygid));
55 groups_byname = xmalloc(ngroups * sizeof(*groups_byname));
56
63b68889 57 if (getgrouplist(user, base, groups_bygid, &ngroups) == -1)
bbe88b6d 58 logit("getgrouplist: groups list too small");
63b68889 59 for (i = 0, j = 0; i < ngroups; i++)
60 if ((gr = getgrgid(groups_bygid[i])) != NULL)
61 groups_byname[j++] = xstrdup(gr->gr_name);
13dff404 62 xfree(groups_bygid);
63b68889 63 return (ngroups = j);
64}
65
255cabd9 66/*
67 * Return 1 if one of user's groups is contained in groups.
68 * Return 0 otherwise. Use match_pattern() for string comparison.
69 */
63b68889 70int
71ga_match(char * const *groups, int n)
72{
73 int i, j;
74
75 for (i = 0; i < ngroups; i++)
76 for (j = 0; j < n; j++)
77 if (match_pattern(groups_byname[i], groups[j]))
78 return 1;
79 return 0;
80}
81
255cabd9 82/*
83 * Free memory allocated for group access list.
84 */
63b68889 85void
86ga_free(void)
87{
88 int i;
89
90 if (ngroups > 0) {
91 for (i = 0; i < ngroups; i++)
92 xfree(groups_byname[i]);
93 ngroups = 0;
13dff404 94 xfree(groups_byname);
63b68889 95 }
96}
This page took 0.190768 seconds and 5 git commands to generate.