124da5b34Srie /*
224da5b34Srie  * CDDL HEADER START
324da5b34Srie  *
424da5b34Srie  * The contents of this file are subject to the terms of the
524da5b34Srie  * Common Development and Distribution License (the "License").
624da5b34Srie  * You may not use this file except in compliance with the License.
724da5b34Srie  *
824da5b34Srie  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
924da5b34Srie  * or http://www.opensolaris.org/os/licensing.
1024da5b34Srie  * See the License for the specific language governing permissions
1124da5b34Srie  * and limitations under the License.
1224da5b34Srie  *
1324da5b34Srie  * When distributing Covered Code, include this CDDL HEADER in each
1424da5b34Srie  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1524da5b34Srie  * If applicable, add the following below this CDDL HEADER, with the
1624da5b34Srie  * fields enclosed by brackets "[]" replaced with your own identifying
1724da5b34Srie  * information: Portions Copyright [yyyy] [name of copyright owner]
1824da5b34Srie  *
1924da5b34Srie  * CDDL HEADER END
2024da5b34Srie  */
2124da5b34Srie 
2224da5b34Srie /*
2324da5b34Srie  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
2424da5b34Srie  * Use is subject to license terms.
2524da5b34Srie  */
2624da5b34Srie 
2724da5b34Srie #include	<sys/types.h>
2824da5b34Srie #include	<unistd.h>
2924da5b34Srie #include	<dlfcn.h>
3024da5b34Srie #include	"k5-int.h"
3124da5b34Srie 
3224da5b34Srie #define		KRB5_UID	"app_krb5_user_uid"
3324da5b34Srie 
3424da5b34Srie /*
35*bbf21555SRichard Lowe  * mech_krb5 makes various calls to getuid().  When employed by gssd(8) and
36*bbf21555SRichard Lowe  * ktkt_warnd(8), app_krb5_user_uid() is used to select a given user's
3724da5b34Srie  * credential cache, rather than the id of the process.
3824da5b34Srie  */
3924da5b34Srie uid_t
krb5_getuid()4024da5b34Srie krb5_getuid()
4124da5b34Srie {
4224da5b34Srie 	static uid_t	(*gptr)() = NULL;
4324da5b34Srie 	void		*handle;
4424da5b34Srie 
4524da5b34Srie 	if (gptr == NULL) {
4624da5b34Srie 		/*
4724da5b34Srie 		 * Specifically look for app_krb5_user_uid() in the application,
4824da5b34Srie 		 * and don't fall into an exhaustive search through all of the
4924da5b34Srie 		 * process dependencies.  This interface is suplied from
50*bbf21555SRichard Lowe 		 * gssd(8) and ktkt_warnd(8).
5124da5b34Srie 		 */
5224da5b34Srie 		if (((handle = dlopen(0, (RTLD_LAZY | RTLD_FIRST))) == NULL) ||
5324da5b34Srie 		    ((gptr = (uid_t (*)())dlsym(handle, KRB5_UID)) == NULL)) {
5424da5b34Srie 			/*
5524da5b34Srie 			 * Fall back to the default getuid(), which is probably
5624da5b34Srie 			 * libc.
5724da5b34Srie 			 */
5824da5b34Srie 			gptr = &getuid;
5924da5b34Srie 		}
6024da5b34Srie 	}
6124da5b34Srie 
6224da5b34Srie 	/*
6324da5b34Srie 	 * Return the appropriate uid.  Note, if a default getuid() couldn't
6424da5b34Srie 	 * be found, the getuid assignment would have failed to relocate, and
6524da5b34Srie 	 * hence this module would fail to load.
6624da5b34Srie 	 */
6724da5b34Srie 	return ((*gptr)());
6824da5b34Srie }
69