1331722Seadler/*
281861Sdd * Copyright (c) 2001 Dima Dorfman.
381861Sdd * All rights reserved.
481861Sdd *
581861Sdd * Redistribution and use in source and binary forms, with or without
681861Sdd * modification, are permitted provided that the following conditions
781861Sdd * are met:
881861Sdd * 1. Redistributions of source code must retain the above copyright
981861Sdd *    notice, this list of conditions and the following disclaimer.
1081861Sdd * 2. Redistributions in binary form must reproduce the above copyright
1181861Sdd *    notice, this list of conditions and the following disclaimer in the
1281861Sdd *    documentation and/or other materials provided with the distribution.
1381861Sdd *
1481861Sdd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1581861Sdd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1681861Sdd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1781861Sdd * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1881861Sdd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1981861Sdd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2081861Sdd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2181861Sdd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2281861Sdd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2381861Sdd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2481861Sdd * SUCH DAMAGE.
2581861Sdd */
2681861Sdd
2790050Sobrien#include <sys/cdefs.h>
2890039Sobrien__FBSDID("$FreeBSD: stable/11/lib/libc/gen/getpeereid.c 330094 2018-02-28 02:37:59Z pfg $");
2981861Sdd
30292521Sjilles#include "namespace.h"
3181861Sdd#include <sys/param.h>
3281861Sdd#include <sys/socket.h>
3381861Sdd#include <sys/ucred.h>
3481861Sdd#include <sys/un.h>
3581861Sdd
3691354Sdd#include <errno.h>
3781861Sdd#include <unistd.h>
38292521Sjilles#include "un-namespace.h"
3981861Sdd
4081861Sddint
4181861Sddgetpeereid(int s, uid_t *euid, gid_t *egid)
4281861Sdd{
4381861Sdd	struct xucred xuc;
4481861Sdd	socklen_t xuclen;
4581861Sdd	int error;
4681861Sdd
4781861Sdd	xuclen = sizeof(xuc);
48292521Sjilles	error = _getsockopt(s, 0, LOCAL_PEERCRED, &xuc, &xuclen);
4981861Sdd	if (error != 0)
5081861Sdd		return (error);
51330094Spfg	if (xuc.cr_version != XUCRED_VERSION) {
52330094Spfg		errno = EINVAL;
53330094Spfg		return (-1);
54330094Spfg	}
5581861Sdd	*euid = xuc.cr_uid;
5681861Sdd	*egid = xuc.cr_gid;
5781861Sdd	return (0);
5881861Sdd}
59