10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
51618Srie * Common Development and Distribution License (the "License").
61618Srie * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
211618Srie
220Sstevel@tonic-gate /*
23*9340SRod.Evans@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate #include <unistd.h>
280Sstevel@tonic-gate #include <strings.h>
290Sstevel@tonic-gate #include <limits.h>
300Sstevel@tonic-gate #include <dlfcn.h>
310Sstevel@tonic-gate #include "_conv.h"
320Sstevel@tonic-gate #include "lddstub_msg.h"
330Sstevel@tonic-gate
340Sstevel@tonic-gate static int
originlddstub(char * buffer,const char * orgfile)350Sstevel@tonic-gate originlddstub(char *buffer, const char *orgfile)
360Sstevel@tonic-gate {
370Sstevel@tonic-gate int len;
380Sstevel@tonic-gate
390Sstevel@tonic-gate if (dlinfo(RTLD_SELF, RTLD_DI_ORIGIN, (void *)buffer) == -1)
400Sstevel@tonic-gate return (-1);
410Sstevel@tonic-gate if (strlcat(buffer, orgfile, PATH_MAX) >= PATH_MAX)
420Sstevel@tonic-gate return (-1);
430Sstevel@tonic-gate if ((len = resolvepath(buffer, buffer, (PATH_MAX - 1))) == -1)
440Sstevel@tonic-gate return (-1);
450Sstevel@tonic-gate buffer[len] = '\0';
461618Srie if (access(buffer, X_OK) == -1)
470Sstevel@tonic-gate return (-1);
480Sstevel@tonic-gate
491618Srie return (1);
500Sstevel@tonic-gate }
510Sstevel@tonic-gate
521618Srie static char orgstub[PATH_MAX], orgstub64[PATH_MAX];
531618Srie static int orgflag, orgflag64;
541618Srie
550Sstevel@tonic-gate /*
560Sstevel@tonic-gate * Determine what lddstub to run.
570Sstevel@tonic-gate */
580Sstevel@tonic-gate const char *
conv_lddstub(int class)590Sstevel@tonic-gate conv_lddstub(int class)
600Sstevel@tonic-gate {
610Sstevel@tonic-gate const char *stub;
620Sstevel@tonic-gate
630Sstevel@tonic-gate /*
640Sstevel@tonic-gate * Establish defaults.
650Sstevel@tonic-gate */
660Sstevel@tonic-gate if (class == ELFCLASS32)
670Sstevel@tonic-gate stub = MSG_ORIG(MSG_PTH_LDDSTUB);
680Sstevel@tonic-gate else
690Sstevel@tonic-gate stub = MSG_ORIG(MSG_PTH_LDDSTUB_64);
700Sstevel@tonic-gate
710Sstevel@tonic-gate /*
720Sstevel@tonic-gate * Provided we're not secure, determine lddstub's location from our
730Sstevel@tonic-gate * own origin.
740Sstevel@tonic-gate */
750Sstevel@tonic-gate if (geteuid()) {
760Sstevel@tonic-gate if ((class == ELFCLASS32) && (orgflag != -1)) {
770Sstevel@tonic-gate if (orgflag == 0) {
78*9340SRod.Evans@Sun.COM /* BEGIN CSTYLED */
790Sstevel@tonic-gate if ((orgflag = originlddstub(orgstub,
80*9340SRod.Evans@Sun.COM #ifdef _LP64
81*9340SRod.Evans@Sun.COM MSG_ORIG(MSG_ORG_64LDD_32STUB))) == -1)
82*9340SRod.Evans@Sun.COM #else
83*9340SRod.Evans@Sun.COM MSG_ORIG(MSG_ORG_32LDD_32STUB))) == -1)
84*9340SRod.Evans@Sun.COM #endif
850Sstevel@tonic-gate return (stub);
86*9340SRod.Evans@Sun.COM /* END CSTYLED */
870Sstevel@tonic-gate }
880Sstevel@tonic-gate stub = (const char *)orgstub;
890Sstevel@tonic-gate }
900Sstevel@tonic-gate if ((class == ELFCLASS64) && (orgflag64 != -1)) {
910Sstevel@tonic-gate if (orgflag64 == 0) {
92*9340SRod.Evans@Sun.COM /* BEGIN CSTYLED */
930Sstevel@tonic-gate if ((orgflag64 = originlddstub(orgstub64,
94*9340SRod.Evans@Sun.COM #ifdef _LP64
95*9340SRod.Evans@Sun.COM MSG_ORIG(MSG_ORG_64LDD_64STUB))) == -1)
96*9340SRod.Evans@Sun.COM #else
97*9340SRod.Evans@Sun.COM MSG_ORIG(MSG_ORG_32LDD_64STUB))) == -1)
98*9340SRod.Evans@Sun.COM #endif
990Sstevel@tonic-gate return (stub);
100*9340SRod.Evans@Sun.COM /* END CSTYLED */
1010Sstevel@tonic-gate }
1020Sstevel@tonic-gate stub = (const char *)orgstub64;
1030Sstevel@tonic-gate }
1040Sstevel@tonic-gate }
1050Sstevel@tonic-gate return (stub);
1060Sstevel@tonic-gate }
107