xref: /freebsd/lib/libc/gen/getpeereid.c (revision 559a218c9b257775fb249b67945fe4a05b7a6b9f)
1d915a14eSPedro F. Giffuni /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3d915a14eSPedro 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 
2959797edfSJilles Tjoelker #include "namespace.h"
30e6063dd1SDima Dorfman #include <sys/param.h>
31e6063dd1SDima Dorfman #include <sys/socket.h>
32e6063dd1SDima Dorfman #include <sys/ucred.h>
33e6063dd1SDima Dorfman #include <sys/un.h>
34e6063dd1SDima Dorfman 
3576183f34SDima Dorfman #include <errno.h>
36e6063dd1SDima Dorfman #include <unistd.h>
3759797edfSJilles Tjoelker #include "un-namespace.h"
38e6063dd1SDima Dorfman 
39e6063dd1SDima Dorfman int
getpeereid(int s,uid_t * euid,gid_t * egid)40e6063dd1SDima Dorfman getpeereid(int s, uid_t *euid, gid_t *egid)
41e6063dd1SDima Dorfman {
42e6063dd1SDima Dorfman 	struct xucred xuc;
43e6063dd1SDima Dorfman 	socklen_t xuclen;
44e6063dd1SDima Dorfman 	int error;
45e6063dd1SDima Dorfman 
46e6063dd1SDima Dorfman 	xuclen = sizeof(xuc);
476e0c8e1aSKonstantin Belousov 	error = _getsockopt(s, SOL_LOCAL, LOCAL_PEERCRED, &xuc, &xuclen);
48e6063dd1SDima Dorfman 	if (error != 0)
49e6063dd1SDima Dorfman 		return (error);
508d48e738SPedro F. Giffuni 	if (xuc.cr_version != XUCRED_VERSION) {
518d48e738SPedro F. Giffuni 		errno = EINVAL;
528d48e738SPedro F. Giffuni 		return (-1);
538d48e738SPedro F. Giffuni 	}
54e6063dd1SDima Dorfman 	*euid = xuc.cr_uid;
55e6063dd1SDima Dorfman 	*egid = xuc.cr_gid;
56e6063dd1SDima Dorfman 	return (0);
57e6063dd1SDima Dorfman }
58