xref: /titanic_44/usr/src/cmd/ssh/libopenbsd-compat/common/bsd-getpeereid.c (revision f48205be61a214698b763ff550ab9e657525104c)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright (c) 2002 Damien Miller.  All rights reserved.
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
57c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
67c478bd9Sstevel@tonic-gate  * are met:
77c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
87c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
97c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
107c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
117c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
147c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
157c478bd9Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
167c478bd9Sstevel@tonic-gate  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
177c478bd9Sstevel@tonic-gate  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
187c478bd9Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
197c478bd9Sstevel@tonic-gate  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
207c478bd9Sstevel@tonic-gate  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
217c478bd9Sstevel@tonic-gate  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
227c478bd9Sstevel@tonic-gate  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
2526ba1984Sjp161948 /*
26*f48205beScasper  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
2726ba1984Sjp161948  * Use is subject to license terms.
2826ba1984Sjp161948  */
2926ba1984Sjp161948 
307c478bd9Sstevel@tonic-gate #include "includes.h"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate RCSID("$Id: bsd-getpeereid.c,v 1.1 2002/09/12 00:33:02 djm Exp $");
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #if !defined(HAVE_GETPEEREID)
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #if defined(SO_PEERCRED)
397c478bd9Sstevel@tonic-gate int
getpeereid(int s,uid_t * euid,gid_t * gid)407c478bd9Sstevel@tonic-gate getpeereid(int s, uid_t *euid, gid_t *gid)
417c478bd9Sstevel@tonic-gate {
427c478bd9Sstevel@tonic-gate 	struct ucred cred;
437c478bd9Sstevel@tonic-gate 	size_t len = sizeof(cred);
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate 	if (getsockopt(s, SOL_SOCKET, SO_PEERCRED, &cred, &len) < 0)
467c478bd9Sstevel@tonic-gate 		return (-1);
477c478bd9Sstevel@tonic-gate 	*euid = cred.uid;
487c478bd9Sstevel@tonic-gate 	*gid = cred.gid;
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate 	return (0);
517c478bd9Sstevel@tonic-gate }
5226ba1984Sjp161948 #elif defined(HAVE_GETPEERUCRED)
5326ba1984Sjp161948 int
getpeereid(int s,uid_t * euid,gid_t * gid)5426ba1984Sjp161948 getpeereid(int s, uid_t *euid, gid_t *gid)
5526ba1984Sjp161948 {
5626ba1984Sjp161948 	ucred_t *ucred = NULL;
5726ba1984Sjp161948 
5826ba1984Sjp161948 	if (getpeerucred(s, &ucred) == -1)
5926ba1984Sjp161948 		return (-1);
60*f48205beScasper 	if ((*euid = ucred_geteuid(ucred)) == (uid_t)-1)
6126ba1984Sjp161948 		return (-1);
62*f48205beScasper 	if ((*gid = ucred_getrgid(ucred)) == (gid_t)-1)
6326ba1984Sjp161948 		return (-1);
6426ba1984Sjp161948 
6526ba1984Sjp161948 	ucred_free(ucred);
6626ba1984Sjp161948 
6726ba1984Sjp161948 	return (0);
6826ba1984Sjp161948 }
697c478bd9Sstevel@tonic-gate #else
707c478bd9Sstevel@tonic-gate int
getpeereid(int s,uid_t * euid,gid_t * gid)717c478bd9Sstevel@tonic-gate getpeereid(int s, uid_t *euid, gid_t *gid)
727c478bd9Sstevel@tonic-gate {
737c478bd9Sstevel@tonic-gate 	*euid = geteuid();
747c478bd9Sstevel@tonic-gate 	*gid = getgid();
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 	return (0);
777c478bd9Sstevel@tonic-gate }
787c478bd9Sstevel@tonic-gate #endif /* defined(SO_PEERCRED) */
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate #endif /* !defined(HAVE_GETPEEREID) */
81