10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * Copyright (c) 2002 Damien Miller.  All rights reserved.
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
50Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
60Sstevel@tonic-gate  * are met:
70Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
80Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
90Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
100Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
110Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
140Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
150Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
160Sstevel@tonic-gate  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
170Sstevel@tonic-gate  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
180Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
190Sstevel@tonic-gate  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
200Sstevel@tonic-gate  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
210Sstevel@tonic-gate  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
220Sstevel@tonic-gate  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
230Sstevel@tonic-gate  */
240Sstevel@tonic-gate 
252757Sjp161948 /*
26*4321Scasper  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
272757Sjp161948  * Use is subject to license terms.
282757Sjp161948  */
292757Sjp161948 
300Sstevel@tonic-gate #include "includes.h"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate RCSID("$Id: bsd-getpeereid.c,v 1.1 2002/09/12 00:33:02 djm Exp $");
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #if !defined(HAVE_GETPEEREID)
370Sstevel@tonic-gate 
380Sstevel@tonic-gate #if defined(SO_PEERCRED)
390Sstevel@tonic-gate int
getpeereid(int s,uid_t * euid,gid_t * gid)400Sstevel@tonic-gate getpeereid(int s, uid_t *euid, gid_t *gid)
410Sstevel@tonic-gate {
420Sstevel@tonic-gate 	struct ucred cred;
430Sstevel@tonic-gate 	size_t len = sizeof(cred);
440Sstevel@tonic-gate 
450Sstevel@tonic-gate 	if (getsockopt(s, SOL_SOCKET, SO_PEERCRED, &cred, &len) < 0)
460Sstevel@tonic-gate 		return (-1);
470Sstevel@tonic-gate 	*euid = cred.uid;
480Sstevel@tonic-gate 	*gid = cred.gid;
490Sstevel@tonic-gate 
500Sstevel@tonic-gate 	return (0);
510Sstevel@tonic-gate }
522757Sjp161948 #elif defined(HAVE_GETPEERUCRED)
532757Sjp161948 int
getpeereid(int s,uid_t * euid,gid_t * gid)542757Sjp161948 getpeereid(int s, uid_t *euid, gid_t *gid)
552757Sjp161948 {
562757Sjp161948 	ucred_t *ucred = NULL;
572757Sjp161948 
582757Sjp161948 	if (getpeerucred(s, &ucred) == -1)
592757Sjp161948 		return (-1);
60*4321Scasper 	if ((*euid = ucred_geteuid(ucred)) == (uid_t)-1)
612757Sjp161948 		return (-1);
62*4321Scasper 	if ((*gid = ucred_getrgid(ucred)) == (gid_t)-1)
632757Sjp161948 		return (-1);
642757Sjp161948 
652757Sjp161948 	ucred_free(ucred);
662757Sjp161948 
672757Sjp161948 	return (0);
682757Sjp161948 }
690Sstevel@tonic-gate #else
700Sstevel@tonic-gate int
getpeereid(int s,uid_t * euid,gid_t * gid)710Sstevel@tonic-gate getpeereid(int s, uid_t *euid, gid_t *gid)
720Sstevel@tonic-gate {
730Sstevel@tonic-gate 	*euid = geteuid();
740Sstevel@tonic-gate 	*gid = getgid();
750Sstevel@tonic-gate 
760Sstevel@tonic-gate 	return (0);
770Sstevel@tonic-gate }
780Sstevel@tonic-gate #endif /* defined(SO_PEERCRED) */
790Sstevel@tonic-gate 
800Sstevel@tonic-gate #endif /* !defined(HAVE_GETPEEREID) */
81