user.c revision 273929
1139804Simp/* Copyright (c) 2007 The NetBSD Foundation, Inc.
230489Sphk * All rights reserved.
330489Sphk *
430489Sphk * Redistribution and use in source and binary forms, with or without
530489Sphk * modification, are permitted provided that the following conditions
630489Sphk * are met:
730489Sphk * 1. Redistributions of source code must retain the above copyright
830489Sphk *    notice, this list of conditions and the following disclaimer.
930489Sphk * 2. Redistributions in binary form must reproduce the above copyright
1030489Sphk *    notice, this list of conditions and the following disclaimer in the
1130489Sphk *    documentation and/or other materials provided with the distribution.
1230489Sphk *
1330489Sphk * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
1430489Sphk * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
1530489Sphk * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
1630489Sphk * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1730489Sphk * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
1830489Sphk * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1930489Sphk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
2030489Sphk * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2130489Sphk * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
2230489Sphk * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
2330489Sphk * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
2430489Sphk * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  */
2530489Sphk
2630489Sphk#include "atf-c/detail/user.h"
2730489Sphk
2830489Sphk#include <sys/param.h>
2930489Sphk#include <sys/types.h>
3030489Sphk#include <limits.h>
3130489Sphk#include <unistd.h>
3230489Sphk
3330489Sphk#include "atf-c/detail/sanity.h"
3430489Sphk
35116182Sobrien/* ---------------------------------------------------------------------
36116182Sobrien * Free functions.
37116182Sobrien * --------------------------------------------------------------------- */
3830489Sphk
3930489Sphkuid_t
4060041Sphkatf_user_euid(void)
4144272Sbde{
4265770Sbp    return geteuid();
43147198Sssouhlal}
4430489Sphk
45114216Skanbool
4631561Sbdeatf_user_is_member_of_group(gid_t gid)
47178243Skib{
4830743Sphk    static gid_t groups[NGROUPS_MAX];
4951068Salfred    static int ngroups = -1;
50189539Smarcus    bool found;
51248084Sattilio    int i;
52189539Smarcus
5330492Sphk    if (ngroups == -1) {
5430489Sphk        ngroups = getgroups(NGROUPS_MAX, groups);
55189539Smarcus        INV(ngroups >= 0);
5630743Sphk    }
5730489Sphk
58193508Srwatson    found = false;
59193508Srwatson    for (i = 0; !found && i < ngroups; i++)
6065770Sbp        if (groups[i] == gid)
6165770Sbp            found = true;
6265770Sbp    return found;
6365770Sbp}
6465770Sbp
6565770Sbpbool
6665770Sbpatf_user_is_root(void)
6765770Sbp{
6865770Sbp    return geteuid() == 0;
6992723Salfred}
70206094Skib
7192723Salfredbool
72189539Smarcusatf_user_is_unprivileged(void)
73189539Smarcus{
74189539Smarcus    return geteuid() != 0;
75189539Smarcus}
76189539Smarcus