xref: /netbsd-src/crypto/external/bsd/openssh/dist/getpeereid.c (revision 41768fc151975edf5da042dafee95bcbf07f4525)
1*41768fc1Schristos /*	$NetBSD: getpeereid.c,v 1.5 2017/04/18 18:41:46 christos Exp $	*/
2313c6c94Schristos 
3313c6c94Schristos /*
4313c6c94Schristos  * Copyright (c) 2002 Damien Miller.  All rights reserved.
5313c6c94Schristos  *
6313c6c94Schristos  * Redistribution and use in source and binary forms, with or without
7313c6c94Schristos  * modification, are permitted provided that the following conditions
8313c6c94Schristos  * are met:
9313c6c94Schristos  * 1. Redistributions of source code must retain the above copyright
10313c6c94Schristos  *    notice, this list of conditions and the following disclaimer.
11313c6c94Schristos  * 2. Redistributions in binary form must reproduce the above copyright
12313c6c94Schristos  *    notice, this list of conditions and the following disclaimer in the
13313c6c94Schristos  *    documentation and/or other materials provided with the distribution.
14313c6c94Schristos  *
15313c6c94Schristos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16313c6c94Schristos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17313c6c94Schristos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18313c6c94Schristos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19313c6c94Schristos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20313c6c94Schristos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21313c6c94Schristos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22313c6c94Schristos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23313c6c94Schristos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24313c6c94Schristos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25313c6c94Schristos  */
26313c6c94Schristos 
27313c6c94Schristos #include "includes.h"
28313c6c94Schristos #include "getpeereid.h"
29313c6c94Schristos #include <unistd.h>
30313c6c94Schristos 
31*41768fc1Schristos __RCSID("$NetBSD: getpeereid.c,v 1.5 2017/04/18 18:41:46 christos Exp $");
32313c6c94Schristos 
33313c6c94Schristos #if defined(SO_PEERCRED)
34313c6c94Schristos int
getpeereid(int s,uid_t * euid,gid_t * gid)35313c6c94Schristos getpeereid(int s, uid_t *euid, gid_t *gid)
36313c6c94Schristos {
37313c6c94Schristos 	struct ucred cred;
38313c6c94Schristos 	socklen_t len = sizeof(cred);
39313c6c94Schristos 
40313c6c94Schristos 	if (getsockopt(s, SOL_SOCKET, SO_PEERCRED, &cred, &len) < 0)
41313c6c94Schristos 		return (-1);
42313c6c94Schristos 	*euid = cred.uid;
43313c6c94Schristos 	*gid = cred.gid;
44313c6c94Schristos 
45313c6c94Schristos 	return (0);
46313c6c94Schristos }
47313c6c94Schristos #else
48313c6c94Schristos int
getpeereid(int s,uid_t * euid,gid_t * gid)49313c6c94Schristos getpeereid(int s, uid_t *euid, gid_t *gid)
50313c6c94Schristos {
51313c6c94Schristos 	*euid = geteuid();
52313c6c94Schristos 	*gid = getgid();
53313c6c94Schristos 
54313c6c94Schristos 	return (0);
55313c6c94Schristos }
56313c6c94Schristos #endif /* defined(SO_PEERCRED) */
57