getpeereid.c revision 107929
11541Srgrimes/*
21541Srgrimes * Copyright (c) 2001 Dima Dorfman.
31541Srgrimes * All rights reserved.
41541Srgrimes *
51541Srgrimes * Redistribution and use in source and binary forms, with or without
61541Srgrimes * modification, are permitted provided that the following conditions
71541Srgrimes * are met:
81541Srgrimes * 1. Redistributions of source code must retain the above copyright
91541Srgrimes *    notice, this list of conditions and the following disclaimer.
101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111541Srgrimes *    notice, this list of conditions and the following disclaimer in the
121541Srgrimes *    documentation and/or other materials provided with the distribution.
131541Srgrimes *
141541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
151541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
161541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
171541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
181541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
191541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
201541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
211541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
221541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
231541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
241541Srgrimes * SUCH DAMAGE.
251541Srgrimes */
261541Srgrimes
271541Srgrimes#include <sys/cdefs.h>
281541Srgrimes__FBSDID("$FreeBSD: head/lib/libc/gen/getpeereid.c 107929 2002-12-16 13:42:13Z maxim $");
291541Srgrimes
301541Srgrimes#include <sys/param.h>
311541Srgrimes#include <sys/socket.h>
321541Srgrimes#include <sys/ucred.h>
331541Srgrimes#include <sys/un.h>
341541Srgrimes
351541Srgrimes#include <errno.h>
361541Srgrimes#include <unistd.h>
371541Srgrimes
3850477Speterint
391541Srgrimesgetpeereid(int s, uid_t *euid, gid_t *egid)
401541Srgrimes{
41104667Sphk	struct xucred xuc;
42104667Sphk	socklen_t xuclen;
43104667Sphk	int error;
441541Srgrimes
451541Srgrimes	xuclen = sizeof(xuc);
461541Srgrimes	error = getsockopt(s, 0, LOCAL_PEERCRED, &xuc, &xuclen);
471541Srgrimes	if (error != 0)
481541Srgrimes		return (error);
491541Srgrimes	if (xuc.cr_version != XUCRED_VERSION)
501541Srgrimes		return (EINVAL);
511541Srgrimes	*euid = xuc.cr_uid;
521541Srgrimes	*egid = xuc.cr_gid;
531541Srgrimes	return (0);
541541Srgrimes}
551541Srgrimes