xref: /freebsd/lib/libc/gen/getpeereid.c (revision d915a14ef094c8dfc1a5aee70e135abfec01d0f1)
1*d915a14eSPedro F. Giffuni /*-
2*d915a14eSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*d915a14eSPedro F. Giffuni  *
4e6063dd1SDima Dorfman  * Copyright (c) 2001 Dima Dorfman.
5e6063dd1SDima Dorfman  * All rights reserved.
6e6063dd1SDima Dorfman  *
7e6063dd1SDima Dorfman  * Redistribution and use in source and binary forms, with or without
8e6063dd1SDima Dorfman  * modification, are permitted provided that the following conditions
9e6063dd1SDima Dorfman  * are met:
10e6063dd1SDima Dorfman  * 1. Redistributions of source code must retain the above copyright
11e6063dd1SDima Dorfman  *    notice, this list of conditions and the following disclaimer.
12e6063dd1SDima Dorfman  * 2. Redistributions in binary form must reproduce the above copyright
13e6063dd1SDima Dorfman  *    notice, this list of conditions and the following disclaimer in the
14e6063dd1SDima Dorfman  *    documentation and/or other materials provided with the distribution.
15e6063dd1SDima Dorfman  *
16e6063dd1SDima Dorfman  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17e6063dd1SDima Dorfman  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18e6063dd1SDima Dorfman  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19e6063dd1SDima Dorfman  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20e6063dd1SDima Dorfman  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21e6063dd1SDima Dorfman  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22e6063dd1SDima Dorfman  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23e6063dd1SDima Dorfman  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24e6063dd1SDima Dorfman  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25e6063dd1SDima Dorfman  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26e6063dd1SDima Dorfman  * SUCH DAMAGE.
27e6063dd1SDima Dorfman  */
28e6063dd1SDima Dorfman 
2960e104f3SDavid E. O'Brien #include <sys/cdefs.h>
30ea8d448aSDavid E. O'Brien __FBSDID("$FreeBSD$");
31e6063dd1SDima Dorfman 
3259797edfSJilles Tjoelker #include "namespace.h"
33e6063dd1SDima Dorfman #include <sys/param.h>
34e6063dd1SDima Dorfman #include <sys/socket.h>
35e6063dd1SDima Dorfman #include <sys/ucred.h>
36e6063dd1SDima Dorfman #include <sys/un.h>
37e6063dd1SDima Dorfman 
3876183f34SDima Dorfman #include <errno.h>
39e6063dd1SDima Dorfman #include <unistd.h>
4059797edfSJilles Tjoelker #include "un-namespace.h"
41e6063dd1SDima Dorfman 
42e6063dd1SDima Dorfman int
43e6063dd1SDima Dorfman getpeereid(int s, uid_t *euid, gid_t *egid)
44e6063dd1SDima Dorfman {
45e6063dd1SDima Dorfman 	struct xucred xuc;
46e6063dd1SDima Dorfman 	socklen_t xuclen;
47e6063dd1SDima Dorfman 	int error;
48e6063dd1SDima Dorfman 
49e6063dd1SDima Dorfman 	xuclen = sizeof(xuc);
5059797edfSJilles Tjoelker 	error = _getsockopt(s, 0, LOCAL_PEERCRED, &xuc, &xuclen);
51e6063dd1SDima Dorfman 	if (error != 0)
52e6063dd1SDima Dorfman 		return (error);
5376183f34SDima Dorfman 	if (xuc.cr_version != XUCRED_VERSION)
5476183f34SDima Dorfman 		return (EINVAL);
55e6063dd1SDima Dorfman 	*euid = xuc.cr_uid;
56e6063dd1SDima Dorfman 	*egid = xuc.cr_gid;
57e6063dd1SDima Dorfman 	return (0);
58e6063dd1SDima Dorfman }
59