xref: /titanic_44/usr/src/cmd/sgs/libconv/common/lddstub.c (revision 0a44ef6d9afbfe052a7e975f55ea0d2954b62a82)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 
29 #include	<unistd.h>
30 #include	<strings.h>
31 #include	<limits.h>
32 #include	<dlfcn.h>
33 #include	"_conv.h"
34 #include	"lddstub_msg.h"
35 
36 static int
37 originlddstub(char *buffer, const char *orgfile)
38 {
39 	int	len;
40 
41 	if (dlinfo(RTLD_SELF, RTLD_DI_ORIGIN, (void *)buffer) == -1)
42 		return (-1);
43 	if (strlcat(buffer, orgfile, PATH_MAX) >= PATH_MAX)
44 		return (-1);
45 	if ((len = resolvepath(buffer, buffer, (PATH_MAX - 1))) == -1)
46 		return (-1);
47 	buffer[len] = '\0';
48 	if (access(buffer, X_OK) == -1)
49 		return (-1);
50 
51 	return (1);
52 }
53 
54 static char	orgstub[PATH_MAX], orgstub64[PATH_MAX];
55 static int	orgflag, orgflag64;
56 
57 /*
58  * Determine what lddstub to run.
59  */
60 const char *
61 conv_lddstub(int class)
62 {
63 	const char *stub;
64 
65 	/*
66 	 * Establish defaults.
67 	 */
68 	if (class == ELFCLASS32)
69 		stub = MSG_ORIG(MSG_PTH_LDDSTUB);
70 	else
71 		stub = MSG_ORIG(MSG_PTH_LDDSTUB_64);
72 
73 	/*
74 	 * Provided we're not secure, determine lddstub's location from our
75 	 * own origin.
76 	 */
77 	if (geteuid()) {
78 		if ((class == ELFCLASS32) && (orgflag != -1)) {
79 			if (orgflag == 0) {
80 				if ((orgflag = originlddstub(orgstub,
81 				    MSG_ORIG(MSG_ORG_LDDSTUB))) == -1)
82 					return (stub);
83 			}
84 			stub = (const char *)orgstub;
85 		}
86 		if ((class == ELFCLASS64) && (orgflag64 != -1)) {
87 			if (orgflag64 == 0) {
88 				if ((orgflag64 = originlddstub(orgstub64,
89 				    MSG_ORIG(MSG_ORG_LDDSTUB_64))) == -1)
90 					return (stub);
91 			}
92 			stub = (const char *)orgstub64;
93 		}
94 	}
95 	return (stub);
96 }
97