1/*
2 * Copyright (c) 1994 University of Maryland
3 * All Rights Reserved.
4 *
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of U.M. not be used in advertising or
10 * publicity pertaining to distribution of the software without specific,
11 * written prior permission.  U.M. makes no representations about the
12 * suitability of this software for any purpose.  It is provided "as is"
13 * without express or implied warranty.
14 *
15 * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
17 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
19 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 *
22 * Author: James da Silva, Systems Design and Analysis Group
23 *			   Computer Science Department
24 *			   University of Maryland at College Park
25 */
26/*-
27 * SPDX-License-Identifier: BSD-2-Clause
28 *
29 * Copyright 2020 Alex Richardson <arichardson@FreeBSD.org>
30 *
31 * This software was developed by SRI International and the University of
32 * Cambridge Computer Laboratory (Department of Computer Science and
33 * Technology) under DARPA contract HR0011-18-C-0016 ("ECATS"), as part of the
34 * DARPA SSITH research programme.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 *    notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 *    notice, this list of conditions and the following disclaimer in the
43 *    documentation and/or other materials provided with the distribution.
44 *
45 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * SUCH DAMAGE.
56 *
57 */
58/*
59 * crunched_main.c - main program for crunched binaries, it branches to a
60 * 	particular subprogram based on the value of argv[0].  Also included
61 *	is a little program invoked when the crunched binary is called via
62 *	its EXECNAME.  This one prints out the list of compiled-in binaries,
63 *	or calls one of them based on argv[1].   This allows the testing of
64 *	the crunched binary without creating all the links.
65 */
66
67#include <sys/param.h>
68#include <sys/auxv.h>
69#include <sys/sysctl.h>
70
71#include <err.h>
72#include <stdio.h>
73#include <stdlib.h>
74#include <string.h>
75
76typedef int crunched_stub_t(int, char **, char **);
77
78struct stub {
79	const char *name;
80	crunched_stub_t *f;
81};
82
83extern const char *__progname;
84extern struct stub entry_points[];
85extern int num_entry_points;
86
87static void crunched_usage(int);
88
89crunched_stub_t crunched_main;
90crunched_stub_t crunched_list;
91
92static struct stub *
93find_entry_point(const char *basename)
94{
95	for (int i = 0; i < num_entry_points; i++) {
96		struct stub *ep = &entry_points[i];
97		if (!strcmp(basename, ep->name))
98			return (ep);
99	}
100	return (NULL);
101}
102
103static const char *
104get_basename(const char *exe_path)
105{
106	const char *slash = strrchr(exe_path, '/');
107	return (slash ? slash + 1 : exe_path);
108}
109
110int
111main(int argc, char **argv, char **envp)
112{
113	struct stub *ep = NULL;
114	const char *basename = NULL;
115	char buf[MAXPATHLEN];
116
117	/*
118	 * Look at __progname first (this will be set if the crunched binary is
119	 * invoked directly).
120	 */
121	if (__progname) {
122		basename = get_basename(__progname);
123		ep = find_entry_point(basename);
124	}
125
126	/*
127	 * Otherwise try to find entry point based on argv[0] (this works for
128	 * both symlinks as well as hardlinks). However, it does not work when
129	 * su invokes a crunched shell because it sets argv[0] to _su when
130	 * invoking the shell. In that case we look at AT_EXECPATH as a
131	 * fallback.
132	 */
133	if (ep == NULL) {
134		basename = get_basename(argv[0]);
135		ep = find_entry_point(basename);
136	}
137
138	/*
139	 * If we didn't find the entry point based on __progname or argv[0],
140	 * try AT_EXECPATH to get the actual binary that was executed.
141	 */
142	if (ep == NULL) {
143		int error = elf_aux_info(AT_EXECPATH, &buf, sizeof(buf));
144
145		if (error == 0) {
146			const char *exe_name = get_basename(buf);
147			/*
148			 * Keep using argv[0] if AT_EXECPATH is the crunched
149			 * binary so that symlinks to the crunched binary report
150			 * "not compiled in" instead of invoking
151			 * crunched_main().
152			 */
153			if (strcmp(exe_name, EXECNAME) != 0) {
154				basename = exe_name;
155				ep = find_entry_point(basename);
156			}
157		} else {
158			warnc(error, "elf_aux_info(AT_EXECPATH) failed");
159		}
160	}
161
162	if (basename == NULL || *basename == '\0')
163		crunched_usage(1);
164
165	if (ep != NULL) {
166		return ep->f(argc, argv, envp);
167	} else {
168		fprintf(stderr, "%s: %s not compiled in\n\n",
169		    EXECNAME, basename);
170		crunched_usage(1);
171	}
172}
173
174int
175crunched_main(int argc, char **argv, char **envp)
176{
177	if (argc <= 1)
178		crunched_usage(0);
179
180	__progname = get_basename(argv[1]);
181	return main(--argc, ++argv, envp);
182}
183
184int
185crunched_list(int argc __unused, char **argv __unused, char **envp __unused)
186{
187	for (int i = 0; i < num_entry_points - 2; i++)
188		printf("%s\n", entry_points[i].name);
189	return (0);
190}
191
192static void
193crunched_usage(int code)
194{
195	int columns, len;
196	FILE *out = stdout;
197	if (code > 0)
198		out = stderr;
199
200	fprintf(out,
201	    "usage: %s program [args ...]\n"
202	    "       %s --list\n"
203	    "       program [args ...]\n"
204	    "\n"
205	    "%s combines several programs in one executable. Create a link to this\n"
206	    "executable with the program name to run that program, or give the program\n"
207	    "name as the first argument.\n"
208	    "\n"
209	    "Currently defined programs:\n",
210	    EXECNAME, EXECNAME, EXECNAME);
211	columns = 0;
212	for (int i = 0; i < num_entry_points - 2; i++) {
213		struct stub *ep = &entry_points[i];
214		len = strlen(ep->name) + 1;
215		if (columns + len < 80)
216			columns += len;
217		else {
218			fprintf(out, "\n");
219			columns = len;
220		}
221		fprintf(out, " %s", ep->name);
222	}
223	fprintf(out, "\n");
224	exit(code);
225}
226
227/* end of crunched_main.c */
228