1273929Sjmmv/* Copyright (c) 2007 The NetBSD Foundation, Inc.
2240116Smarcel * All rights reserved.
3240116Smarcel *
4240116Smarcel * Redistribution and use in source and binary forms, with or without
5240116Smarcel * modification, are permitted provided that the following conditions
6240116Smarcel * are met:
7240116Smarcel * 1. Redistributions of source code must retain the above copyright
8240116Smarcel *    notice, this list of conditions and the following disclaimer.
9240116Smarcel * 2. Redistributions in binary form must reproduce the above copyright
10240116Smarcel *    notice, this list of conditions and the following disclaimer in the
11240116Smarcel *    documentation and/or other materials provided with the distribution.
12240116Smarcel *
13240116Smarcel * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
14240116Smarcel * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
15240116Smarcel * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16240116Smarcel * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17240116Smarcel * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
18240116Smarcel * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19240116Smarcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20240116Smarcel * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21240116Smarcel * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22240116Smarcel * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23240116Smarcel * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24273929Sjmmv * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  */
25240116Smarcel
26273929Sjmmv#include "atf-c/detail/user.h"
27273929Sjmmv
28240116Smarcel#include <sys/param.h>
29240116Smarcel#include <sys/types.h>
30240116Smarcel#include <limits.h>
31240116Smarcel#include <unistd.h>
32240116Smarcel
33273929Sjmmv#include "atf-c/detail/sanity.h"
34240116Smarcel
35240116Smarcel/* ---------------------------------------------------------------------
36240116Smarcel * Free functions.
37240116Smarcel * --------------------------------------------------------------------- */
38240116Smarcel
39240116Smarceluid_t
40240116Smarcelatf_user_euid(void)
41240116Smarcel{
42240116Smarcel    return geteuid();
43240116Smarcel}
44240116Smarcel
45240116Smarcelbool
46240116Smarcelatf_user_is_member_of_group(gid_t gid)
47240116Smarcel{
48240116Smarcel    static gid_t groups[NGROUPS_MAX];
49240116Smarcel    static int ngroups = -1;
50240116Smarcel    bool found;
51240116Smarcel    int i;
52240116Smarcel
53240116Smarcel    if (ngroups == -1) {
54240116Smarcel        ngroups = getgroups(NGROUPS_MAX, groups);
55240116Smarcel        INV(ngroups >= 0);
56240116Smarcel    }
57240116Smarcel
58240116Smarcel    found = false;
59240116Smarcel    for (i = 0; !found && i < ngroups; i++)
60240116Smarcel        if (groups[i] == gid)
61240116Smarcel            found = true;
62240116Smarcel    return found;
63240116Smarcel}
64240116Smarcel
65240116Smarcelbool
66240116Smarcelatf_user_is_root(void)
67240116Smarcel{
68240116Smarcel    return geteuid() == 0;
69240116Smarcel}
70240116Smarcel
71240116Smarcelbool
72240116Smarcelatf_user_is_unprivileged(void)
73240116Smarcel{
74240116Smarcel    return geteuid() != 0;
75240116Smarcel}
76