1*213aa76dSnia /* $NetBSD: getpeereid.c,v 1.4 2021/08/08 20:54:48 nia Exp $ */
2fd961c44She
3fd961c44She /*-
4fd961c44She * Copyright (c) 2007 The NetBSD Foundation, Inc.
5fd961c44She * All rights reserved.
6fd961c44She *
7fd961c44She * This code is derived from software contributed to The NetBSD Foundation
8fd961c44She * by Arne H. Juul.
9fd961c44She *
10fd961c44She * Redistribution and use in source and binary forms, with or without
11fd961c44She * modification, are permitted provided that the following conditions
12fd961c44She * are met:
13fd961c44She * 1. Redistributions of source code must retain the above copyright
14fd961c44She * notice, this list of conditions and the following disclaimer.
15fd961c44She * 2. Redistributions in binary form must reproduce the above copyright
16fd961c44She * notice, this list of conditions and the following disclaimer in the
17fd961c44She * documentation and/or other materials provided with the distribution.
18fd961c44She *
19fd961c44She * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20fd961c44She * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21fd961c44She * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22fd961c44She * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23fd961c44She * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24fd961c44She * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25fd961c44She * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26fd961c44She * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27fd961c44She * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28fd961c44She * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29fd961c44She * POSSIBILITY OF SUCH DAMAGE.
30fd961c44She */
31fd961c44She
32fd961c44She #include <sys/cdefs.h>
33fd961c44She #if defined(LIBC_SCCS) && !defined(lint)
34*213aa76dSnia __RCSID("$NetBSD: getpeereid.c,v 1.4 2021/08/08 20:54:48 nia Exp $");
35fd961c44She #endif /* LIBC_SCCS and not lint */
36fd961c44She
37fd961c44She #include <sys/types.h>
38fd961c44She #include <sys/un.h>
39fd961c44She #include <sys/socket.h>
405a7f327fSchristos #include <unistd.h>
415a7f327fSchristos #include <errno.h>
42fd961c44She
43fd961c44She
44fd961c44She int
getpeereid(int s,uid_t * euid,gid_t * egid)45fd961c44She getpeereid(int s, uid_t *euid, gid_t *egid)
46fd961c44She {
47fd961c44She struct unpcbid cred;
485a7f327fSchristos struct sockaddr_storage ss;
495a7f327fSchristos socklen_t len;
505a7f327fSchristos
515a7f327fSchristos len = sizeof(ss);
525a7f327fSchristos if (getsockname(s, (void *)&ss, &len) == -1)
53fd961c44She return -1;
545a7f327fSchristos if (ss.ss_family != AF_LOCAL) {
555a7f327fSchristos errno = EOPNOTSUPP;
565a7f327fSchristos return -1;
575a7f327fSchristos }
585a7f327fSchristos
595a7f327fSchristos len = sizeof(cred);
60*213aa76dSnia if (getsockopt(s, SOL_LOCAL, LOCAL_PEEREID, &cred, &len) == -1)
615a7f327fSchristos return -1;
625a7f327fSchristos
63fd961c44She if (euid != NULL)
64fd961c44She *euid = cred.unp_euid;
65fd961c44She if (egid != NULL)
66fd961c44She *egid = cred.unp_egid;
67fd961c44She return 0;
68fd961c44She }
69