getpeereid.c revision 81861
181861Sdd/*
281861Sdd * Copyright (c) 2001 Dima Dorfman.
381861Sdd * All rights reserved.
481861Sdd *
581861Sdd * Redistribution and use in source and binary forms, with or without
681861Sdd * modification, are permitted provided that the following conditions
781861Sdd * are met:
881861Sdd * 1. Redistributions of source code must retain the above copyright
981861Sdd *    notice, this list of conditions and the following disclaimer.
1081861Sdd * 2. Redistributions in binary form must reproduce the above copyright
1181861Sdd *    notice, this list of conditions and the following disclaimer in the
1281861Sdd *    documentation and/or other materials provided with the distribution.
1381861Sdd *
1481861Sdd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1581861Sdd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1681861Sdd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1781861Sdd * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1881861Sdd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1981861Sdd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2081861Sdd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2181861Sdd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2281861Sdd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2381861Sdd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2481861Sdd * SUCH DAMAGE.
2581861Sdd */
2681861Sdd
2781861Sdd#if defined(LIBC_RCS) && !defined(lint)
2881861Sddstatic const char rcsid[] =
2981861Sdd  "$FreeBSD: head/lib/libc/gen/getpeereid.c 81861 2001-08-17 22:09:15Z dd $";
3081861Sdd#endif /* LIBC_RCS and not lint */
3181861Sdd
3281861Sdd#include <sys/param.h>
3381861Sdd#include <sys/socket.h>
3481861Sdd#include <sys/ucred.h>
3581861Sdd#include <sys/un.h>
3681861Sdd
3781861Sdd#include <unistd.h>
3881861Sdd
3981861Sddint
4081861Sddgetpeereid(int s, uid_t *euid, gid_t *egid)
4181861Sdd{
4281861Sdd	struct xucred xuc;
4381861Sdd	socklen_t xuclen;
4481861Sdd	int error;
4581861Sdd
4681861Sdd	xuclen = sizeof(xuc);
4781861Sdd	error = getsockopt(s, LOCAL_PEERCRED, 1, &xuc, &xuclen);
4881861Sdd	if (error != 0)
4981861Sdd		return (error);
5081861Sdd	*euid = xuc.cr_uid;
5181861Sdd	*egid = xuc.cr_gid;
5281861Sdd	return (0);
5381861Sdd}
54