1106121Sdes/* 2126274Sdes * Copyright (c) 2002,2004 Damien Miller <djm@mindrot.org> 3106121Sdes * 4126274Sdes * Permission to use, copy, modify, and distribute this software for any 5126274Sdes * purpose with or without fee is hereby granted, provided that the above 6126274Sdes * copyright notice and this permission notice appear in all copies. 7106121Sdes * 8126274Sdes * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9126274Sdes * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10126274Sdes * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11126274Sdes * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12126274Sdes * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13126274Sdes * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14126274Sdes * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15106121Sdes */ 16106121Sdes 17106121Sdes#include "includes.h" 18106121Sdes 19106121Sdes#if !defined(HAVE_GETPEEREID) 20106121Sdes 21162852Sdes#include <sys/types.h> 22162852Sdes#include <sys/socket.h> 23162852Sdes 24162852Sdes#include <unistd.h> 25162852Sdes 26106121Sdes#if defined(SO_PEERCRED) 27106121Sdesint 28106121Sdesgetpeereid(int s, uid_t *euid, gid_t *gid) 29106121Sdes{ 30106121Sdes struct ucred cred; 31113908Sdes socklen_t len = sizeof(cred); 32106121Sdes 33106121Sdes if (getsockopt(s, SOL_SOCKET, SO_PEERCRED, &cred, &len) < 0) 34106121Sdes return (-1); 35106121Sdes *euid = cred.uid; 36106121Sdes *gid = cred.gid; 37106121Sdes 38106121Sdes return (0); 39106121Sdes} 40181111Sdes#elif defined(HAVE_GETPEERUCRED) 41181111Sdes 42181111Sdes#ifdef HAVE_UCRED_H 43181111Sdes# include <ucred.h> 44181111Sdes#endif 45181111Sdes 46181111Sdesint 47181111Sdesgetpeereid(int s, uid_t *euid, gid_t *gid) 48181111Sdes{ 49181111Sdes ucred_t *ucred = NULL; 50181111Sdes 51181111Sdes if (getpeerucred(s, &ucred) == -1) 52181111Sdes return (-1); 53181111Sdes if ((*euid = ucred_geteuid(ucred)) == -1) 54181111Sdes return (-1); 55181111Sdes if ((*gid = ucred_getrgid(ucred)) == -1) 56181111Sdes return (-1); 57181111Sdes 58181111Sdes ucred_free(ucred); 59181111Sdes 60181111Sdes return (0); 61181111Sdes} 62106121Sdes#else 63106121Sdesint 64106121Sdesgetpeereid(int s, uid_t *euid, gid_t *gid) 65106121Sdes{ 66106121Sdes *euid = geteuid(); 67106121Sdes *gid = getgid(); 68106121Sdes 69106121Sdes return (0); 70106121Sdes} 71106121Sdes#endif /* defined(SO_PEERCRED) */ 72106121Sdes 73106121Sdes#endif /* !defined(HAVE_GETPEEREID) */ 74