1/* Toybox infrastructure.
2 *
3 * Copyright 2006 Rob Landley <rob@landley.net>
4 */
5
6#include "toys.h"
7
8#ifndef TOYBOX_VERSION
9#define TOYBOX_VERSION "0.7.0"
10#endif
11
12// Populate toy_list[].
13
14#undef NEWTOY
15#undef OLDTOY
16#define NEWTOY(name, opts, flags) {#name, name##_main, opts, flags},
17#define OLDTOY(name, oldname, flags) \
18  {#name, oldname##_main, OPTSTR_##oldname, flags},
19
20struct toy_list toy_list[] = {
21#include "generated/newtoys.h"
22};
23
24// global context for this command.
25
26struct toy_context toys;
27union global_union this;
28char toybuf[4096], libbuf[4096];
29
30struct toy_list *toy_find(char *name)
31{
32  int top, bottom, middle;
33
34  if (!CFG_TOYBOX) return 0;
35
36  // If the name starts with "toybox" accept that as a match.  Otherwise
37  // skip the first entry, which is out of order.
38
39  if (!strncmp(name,"toybox",6)) return toy_list;
40  bottom = 1;
41
42  // Binary search to find this command.
43
44  top = ARRAY_LEN(toy_list)-1;
45  for (;;) {
46    int result;
47
48    middle = (top+bottom)/2;
49    if (middle<bottom || middle>top) return NULL;
50    result = strcmp(name,toy_list[middle].name);
51    if (!result) return toy_list+middle;
52    if (result<0) top=--middle;
53    else bottom = ++middle;
54  }
55}
56
57// Figure out whether or not anything is using the option parsing logic,
58// because the compiler can't figure out whether or not to optimize it away
59// on its' own.  NEED_OPTIONS becomes a constant allowing if() to optimize
60// stuff out via dead code elimination.
61
62#undef NEWTOY
63#undef OLDTOY
64#define NEWTOY(name, opts, flags) opts ||
65#define OLDTOY(name, oldname, flags) OPTSTR_##oldname ||
66static const int NEED_OPTIONS =
67#include "generated/newtoys.h"
680;  // Ends the opts || opts || opts...
69
70// Setup toybox global state for this command.
71static void toy_singleinit(struct toy_list *which, char *argv[])
72{
73  toys.which = which;
74  toys.argv = argv;
75
76  if (CFG_TOYBOX_I18N) setlocale(LC_ALL, "C"+!!(which->flags & TOYFLAG_LOCALE));
77
78  if (CFG_TOYBOX_HELP_DASHDASH && argv[1] && !strcmp(argv[1], "--help")) {
79    if (CFG_TOYBOX && toys.which == toy_list && toys.argv[2])
80      if (!(toys.which = toy_find(toys.argv[2]))) return;
81    show_help(stdout);
82    xexit();
83  }
84
85  if (NEED_OPTIONS && which->options) get_optflags();
86  else {
87    toys.optargs = argv+1;
88    for (toys.optc = 0; toys.optargs[toys.optc]; toys.optc++);
89  }
90  toys.old_umask = umask(0);
91  if (!(which->flags & TOYFLAG_UMASK)) umask(toys.old_umask);
92  toys.signalfd--;
93  toys.toycount = ARRAY_LEN(toy_list);
94}
95
96// Full init needed by multiplexer or reentrant calls, calls singleinit at end
97void toy_init(struct toy_list *which, char *argv[])
98{
99  void *oldwhich = toys.which;
100
101  // Drop permissions for non-suid commands.
102
103  if (CFG_TOYBOX_SUID) {
104    if (!toys.which) toys.which = toy_list;
105
106    uid_t uid = getuid(), euid = geteuid();
107
108    if (!(which->flags & TOYFLAG_STAYROOT)) {
109      if (uid != euid) {
110        if (setuid(uid)) perror_exit("setuid %d->%d", euid, uid); // drop root
111        euid = uid;
112        toys.wasroot++;
113      }
114    } else if (CFG_TOYBOX_DEBUG && uid && which != toy_list)
115      error_msg("Not installed suid root");
116
117    if ((which->flags & TOYFLAG_NEEDROOT) && euid) help_exit("Not root");
118  }
119
120  // Free old toys contents (to be reentrant), but leave rebound if any
121  // don't blank old optargs if our new argc lives in the old optargs.
122  if (argv<toys.optargs || argv>toys.optargs+toys.optc) free(toys.optargs);
123  memset(&toys, 0, offsetof(struct toy_context, rebound));
124  if (oldwhich) memset(&this, 0, sizeof(this));
125
126  // Continue to portion of init needed by standalone commands
127  toy_singleinit(which, argv);
128}
129
130// Like exec() but runs an internal toybox command instead of another file.
131// Only returns if it can't run command internally, otherwise exit() when done.
132void toy_exec(char *argv[])
133{
134  struct toy_list *which;
135
136  // Return if we can't find it (which includes no multiplexer case),
137  if (!(which = toy_find(*argv))) return;
138
139  // Return if stack depth getting noticeable (proxy for leaked heap, etc).
140  if (toys.stacktop && labs((char *)toys.stacktop-(char *)&which)>6000)
141    return;
142
143  // Return if we need to re-exec to acquire root via suid bit.
144  if (toys.which && (which->flags&TOYFLAG_ROOTONLY) && toys.wasroot) return;
145
146  // Run command
147  toy_init(which, argv);
148  if (toys.which) toys.which->toy_main();
149  xexit();
150}
151
152// Multiplexer command, first argument is command to run, rest are args to that.
153// If first argument starts with - output list of command install paths.
154void toybox_main(void)
155{
156  static char *toy_paths[]={"usr/","bin/","sbin/",0};
157  int i, len = 0;
158
159  // fast path: try to exec immediately.
160  // (Leave toys.which null to disable suid return logic.)
161  if (toys.argv[1]) toy_exec(toys.argv+1);
162
163  // For early error reporting
164  toys.which = toy_list;
165
166  if (toys.argv[1]) {
167    if (!strcmp("--version", toys.argv[1])) {
168      xputs(TOYBOX_VERSION);
169      xexit();
170    }
171    if (toys.argv[1][0] != '-') {
172      toys.exitval = 127;
173      error_exit("Unknown command %s", toys.argv[1]);
174    }
175  }
176
177  // Output list of command.
178  for (i=1; i<ARRAY_LEN(toy_list); i++) {
179    int fl = toy_list[i].flags;
180    if (fl & TOYMASK_LOCATION) {
181      if (toys.argv[1]) {
182        int j;
183        for (j=0; toy_paths[j]; j++)
184          if (fl & (1<<j)) len += printf("%s", toy_paths[j]);
185      }
186      len += printf("%s",toy_list[i].name);
187      if (++len > 65) len = 0;
188      xputc(len ? ' ' : '\n');
189    }
190  }
191  xputc('\n');
192}
193
194int main(int argc, char *argv[])
195{
196  if (!*argv) return 127;
197
198  // Snapshot stack location so we can detect recursion depth later.
199  // This is its own block so probe doesn't permanently consume stack.
200  else {
201    int stack;
202
203    toys.stacktop = &stack;
204  }
205  *argv = basename_r(*argv);
206
207  // If nommu can't fork, special reentry path.
208  // Use !stacktop to signal "vfork happened", both before and after xexec()
209  if (!CFG_TOYBOX_FORK) {
210    if (0x80 & **argv) {
211      **argv &= 0x7f;
212      toys.stacktop = 0;
213    }
214  }
215
216  if (CFG_TOYBOX) {
217    // Call the multiplexer, adjusting this argv[] to be its' argv[1].
218    // (It will adjust it back before calling toy_exec().)
219    toys.argv = argv-1;
220    toybox_main();
221  } else {
222    // a single toybox command built standalone with no multiplexer
223    toy_singleinit(toy_list, argv);
224    toy_list->toy_main();
225  }
226
227  xexit();
228}
229