bsd-getpeereid.c revision 113908
11558Srgrimes/*
21558Srgrimes * Copyright (c) 2002 Damien Miller.  All rights reserved.
31558Srgrimes *
41558Srgrimes * Redistribution and use in source and binary forms, with or without
51558Srgrimes * modification, are permitted provided that the following conditions
61558Srgrimes * are met:
71558Srgrimes * 1. Redistributions of source code must retain the above copyright
81558Srgrimes *    notice, this list of conditions and the following disclaimer.
91558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
101558Srgrimes *    notice, this list of conditions and the following disclaimer in the
111558Srgrimes *    documentation and/or other materials provided with the distribution.
121558Srgrimes *
131558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
141558Srgrimes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
151558Srgrimes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
161558Srgrimes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
171558Srgrimes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
181558Srgrimes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
191558Srgrimes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
201558Srgrimes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
211558Srgrimes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
221558Srgrimes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
231558Srgrimes */
241558Srgrimes
251558Srgrimes#include "includes.h"
261558Srgrimes
271558SrgrimesRCSID("$Id: bsd-getpeereid.c,v 1.2 2003/03/24 22:07:52 djm Exp $");
281558Srgrimes
291558Srgrimes#if !defined(HAVE_GETPEEREID)
301558Srgrimes
311558Srgrimes#if defined(SO_PEERCRED)
321558Srgrimesint
331558Srgrimesgetpeereid(int s, uid_t *euid, gid_t *gid)
341558Srgrimes{
351558Srgrimes	struct ucred cred;
361558Srgrimes	socklen_t len = sizeof(cred);
371558Srgrimes
381558Srgrimes	if (getsockopt(s, SOL_SOCKET, SO_PEERCRED, &cred, &len) < 0)
391558Srgrimes		return (-1);
401558Srgrimes	*euid = cred.uid;
411558Srgrimes	*gid = cred.gid;
421558Srgrimes
431558Srgrimes	return (0);
441558Srgrimes}
451558Srgrimes#else
461558Srgrimesint
471558Srgrimesgetpeereid(int s, uid_t *euid, gid_t *gid)
481558Srgrimes{
491558Srgrimes	*euid = geteuid();
501558Srgrimes	*gid = getgid();
511558Srgrimes
521558Srgrimes	return (0);
531558Srgrimes}
541558Srgrimes#endif /* defined(SO_PEERCRED) */
551558Srgrimes
561558Srgrimes#endif /* !defined(HAVE_GETPEEREID) */
571558Srgrimes