getpeereid.c revision 81861
1198134Sjhb/*
2198134Sjhb * Copyright (c) 2001 Dima Dorfman.
3283927Sjhb * All rights reserved.
4198134Sjhb *
5198134Sjhb * Redistribution and use in source and binary forms, with or without
6198134Sjhb * modification, are permitted provided that the following conditions
7198134Sjhb * are met:
8198134Sjhb * 1. Redistributions of source code must retain the above copyright
9198134Sjhb *    notice, this list of conditions and the following disclaimer.
10198134Sjhb * 2. Redistributions in binary form must reproduce the above copyright
11198134Sjhb *    notice, this list of conditions and the following disclaimer in the
12198134Sjhb *    documentation and/or other materials provided with the distribution.
13198134Sjhb *
14198134Sjhb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15198134Sjhb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16198134Sjhb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17198134Sjhb * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18198134Sjhb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19198134Sjhb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20198134Sjhb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21198134Sjhb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22198134Sjhb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23198134Sjhb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24198134Sjhb * SUCH DAMAGE.
25198134Sjhb */
26198134Sjhb
27198134Sjhb#if defined(LIBC_RCS) && !defined(lint)
28198134Sjhbstatic const char rcsid[] =
29198134Sjhb  "$FreeBSD: head/lib/libc/gen/getpeereid.c 81861 2001-08-17 22:09:15Z dd $";
30292418Sjhb#endif /* LIBC_RCS and not lint */
31198134Sjhb
32198134Sjhb#include <sys/param.h>
33198134Sjhb#include <sys/socket.h>
34198134Sjhb#include <sys/ucred.h>
35198134Sjhb#include <sys/un.h>
36198134Sjhb
37198134Sjhb#include <unistd.h>
38198134Sjhb
39198134Sjhbint
40198134Sjhbgetpeereid(int s, uid_t *euid, gid_t *egid)
41292418Sjhb{
42198134Sjhb	struct xucred xuc;
43198134Sjhb	socklen_t xuclen;
44198134Sjhb	int error;
45198134Sjhb
46198134Sjhb	xuclen = sizeof(xuc);
47198134Sjhb	error = getsockopt(s, LOCAL_PEERCRED, 1, &xuc, &xuclen);
48233648Seadler	if (error != 0)
49198134Sjhb		return (error);
50198134Sjhb	*euid = xuc.cr_uid;
51198134Sjhb	*egid = xuc.cr_gid;
52198134Sjhb	return (0);
53198134Sjhb}
54198134Sjhb