blob: 814b8bcd22d62742fe625cb1164de30248960911 [file] [log] [blame]
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +00001/* vi: set sw=4 ts=4: */
2/*
Denis Vlasenko724d1962007-10-10 14:41:07 +00003 * Utility routines.
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +00004 *
Denis Vlasenko724d1962007-10-10 14:41:07 +00005 * Copyright (C) tons of folks. Tracking down who wrote what
6 * isn't something I'm going to worry about... If you wrote something
7 * here, please feel free to acknowledge your work.
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +00008 *
Denis Vlasenko724d1962007-10-10 14:41:07 +00009 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
10 * Permission has been granted to redistribute this code under the GPL.
11 *
12 * Licensed under GPLv2 or later, see file License in this tarball for details.
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +000013 */
14
Denis Vlasenko79cedcb2008-04-08 21:13:28 +000015/* We are trying to not use printf, this benefits the case when selected
16 * applets are really simple. Example:
17 *
18 * $ ./busybox
19 * ...
20 * Currently defined functions:
21 * basename, false, true
22 *
23 * $ size busybox
24 * text data bss dec hex filename
25 * 4473 52 72 4597 11f5 busybox
26 *
27 * FEATURE_INSTALLER or FEATURE_SUID will still link printf routines in. :(
28 */
29
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +000030#include <assert.h>
31#include "busybox.h"
32
33
34/* Declare <applet>_main() */
35#define PROTOTYPES
36#include "applets.h"
37#undef PROTOTYPES
38
39#if ENABLE_SHOW_USAGE && !ENABLE_FEATURE_COMPRESS_USAGE
40/* Define usage_messages[] */
41static const char usage_messages[] ALIGN1 = ""
42#define MAKE_USAGE
43#include "usage.h"
44#include "applets.h"
45;
46#undef MAKE_USAGE
47#else
48#define usage_messages 0
49#endif /* SHOW_USAGE */
50
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +000051
Denis Vlasenko32b2a9f2008-02-22 22:43:22 +000052/* Include generated applet names, pointers to <applet>_main, etc */
Denis Vlasenko1aa7e472007-11-28 06:49:03 +000053#include "applet_tables.h"
Denis Vlasenko468aea22008-04-01 14:47:57 +000054/* ...and if applet_tables generator says we have only one applet... */
55#ifdef SINGLE_APPLET_MAIN
56#undef ENABLE_FEATURE_INDIVIDUAL
57#define ENABLE_FEATURE_INDIVIDUAL 1
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000058#undef IF_FEATURE_INDIVIDUAL
59#define IF_FEATURE_INDIVIDUAL(...) __VA_ARGS__
Denis Vlasenko468aea22008-04-01 14:47:57 +000060#endif
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +000061
62
63#if ENABLE_FEATURE_COMPRESS_USAGE
64
65#include "usage_compressed.h"
66#include "unarchive.h"
67
68static const char *unpack_usage_messages(void)
69{
70 char *outbuf = NULL;
71 bunzip_data *bd;
72 int i;
73
74 i = start_bunzip(&bd,
75 /* src_fd: */ -1,
Denys Vlasenko263cff92009-08-20 23:34:01 +020076 /* inbuf: */ (void *)packed_usage,
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +000077 /* len: */ sizeof(packed_usage));
78 /* read_bunzip can longjmp to start_bunzip, and ultimately
79 * end up here with i != 0 on read data errors! Not trivial */
80 if (!i) {
81 /* Cannot use xmalloc: will leak bd in NOFORK case! */
82 outbuf = malloc_or_warn(SIZEOF_usage_messages);
83 if (outbuf)
84 read_bunzip(bd, outbuf, SIZEOF_usage_messages);
85 }
86 dealloc_bunzip(bd);
87 return outbuf;
88}
89#define dealloc_usage_messages(s) free(s)
90
91#else
92
93#define unpack_usage_messages() usage_messages
94#define dealloc_usage_messages(s) ((void)(s))
95
96#endif /* FEATURE_COMPRESS_USAGE */
97
98
Denis Vlasenko79cedcb2008-04-08 21:13:28 +000099static void full_write2_str(const char *str)
100{
Denis Vlasenko73c571a2009-03-09 00:12:37 +0000101 xwrite_str(STDERR_FILENO, str);
Denis Vlasenko79cedcb2008-04-08 21:13:28 +0000102}
103
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000104void FAST_FUNC bb_show_usage(void)
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000105{
106 if (ENABLE_SHOW_USAGE) {
Denis Vlasenko468aea22008-04-01 14:47:57 +0000107#ifdef SINGLE_APPLET_STR
108 /* Imagine that this applet is "true". Dont suck in printf! */
109 const char *p;
110 const char *usage_string = p = unpack_usage_messages();
111
112 if (*p == '\b') {
Denis Vlasenko42404082008-11-24 13:42:24 +0000113 full_write2_str("No help available.\n\n");
Denis Vlasenko468aea22008-04-01 14:47:57 +0000114 } else {
Denis Vlasenko42404082008-11-24 13:42:24 +0000115 full_write2_str("Usage: "SINGLE_APPLET_STR" ");
Denis Vlasenko79cedcb2008-04-08 21:13:28 +0000116 full_write2_str(p);
117 full_write2_str("\n\n");
Denis Vlasenko468aea22008-04-01 14:47:57 +0000118 }
Denys Vlasenko630dde12009-08-30 19:57:49 +0200119 if (ENABLE_FEATURE_CLEAN_UP)
120 dealloc_usage_messages((char*)usage_string);
Denis Vlasenko468aea22008-04-01 14:47:57 +0000121#else
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000122 const char *p;
123 const char *usage_string = p = unpack_usage_messages();
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000124 int ap = find_applet_by_name(applet_name);
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000125
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000126 if (ap < 0) /* never happens, paranoia */
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000127 xfunc_die();
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000128 while (ap) {
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000129 while (*p++) continue;
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000130 ap--;
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000131 }
Denis Vlasenko79cedcb2008-04-08 21:13:28 +0000132 full_write2_str(bb_banner);
Denys Vlasenko630dde12009-08-30 19:57:49 +0200133 full_write2_str(" multi-call binary.\n");
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000134 if (*p == '\b')
Denis Vlasenko79cedcb2008-04-08 21:13:28 +0000135 full_write2_str("\nNo help available.\n\n");
136 else {
137 full_write2_str("\nUsage: ");
138 full_write2_str(applet_name);
139 full_write2_str(" ");
140 full_write2_str(p);
141 full_write2_str("\n\n");
142 }
Denys Vlasenko630dde12009-08-30 19:57:49 +0200143 if (ENABLE_FEATURE_CLEAN_UP)
144 dealloc_usage_messages((char*)usage_string);
Denis Vlasenko468aea22008-04-01 14:47:57 +0000145#endif
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000146 }
147 xfunc_die();
148}
149
Denis Vlasenko79cedcb2008-04-08 21:13:28 +0000150#if NUM_APPLETS > 8
Denis Vlasenko745cd172007-11-29 03:31:20 +0000151/* NB: any char pointer will work as well, not necessarily applet_names */
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000152static int applet_name_compare(const void *name, const void *v)
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000153{
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000154 int i = (const char *)v - applet_names;
155 return strcmp(name, APPLET_NAME(i));
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000156}
Denis Vlasenko79cedcb2008-04-08 21:13:28 +0000157#endif
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000158int FAST_FUNC find_applet_by_name(const char *name)
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000159{
Denis Vlasenko79cedcb2008-04-08 21:13:28 +0000160#if NUM_APPLETS > 8
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000161 /* Do a binary search to find the applet entry given the name. */
Denis Vlasenko745cd172007-11-29 03:31:20 +0000162 const char *p;
163 p = bsearch(name, applet_names, ARRAY_SIZE(applet_main), 1, applet_name_compare);
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000164 if (!p)
165 return -1;
166 return p - applet_names;
Denis Vlasenko79cedcb2008-04-08 21:13:28 +0000167#else
168 /* A version which does not pull in bsearch */
169 int i = 0;
170 const char *p = applet_names;
171 while (i < NUM_APPLETS) {
172 if (strcmp(name, p) == 0)
173 return i;
174 p += strlen(p) + 1;
175 i++;
176 }
177 return -1;
178#endif
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000179}
180
181
Denis Vlasenko68404f12008-03-17 09:00:54 +0000182void lbb_prepare(const char *applet
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000183 IF_FEATURE_INDIVIDUAL(, char **argv))
Denis Vlasenko468aea22008-04-01 14:47:57 +0000184 MAIN_EXTERNALLY_VISIBLE;
185void lbb_prepare(const char *applet
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000186 IF_FEATURE_INDIVIDUAL(, char **argv))
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000187{
188#ifdef __GLIBC__
189 (*(int **)&bb_errno) = __errno_location();
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000190 barrier();
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000191#endif
Denis Vlasenko15cb4a42007-10-11 10:06:26 +0000192 applet_name = applet;
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000193
194 /* Set locale for everybody except 'init' */
195 if (ENABLE_LOCALE_SUPPORT && getpid() != 1)
196 setlocale(LC_ALL, "");
197
Denis Vlasenko82d38da2007-10-10 14:38:47 +0000198#if ENABLE_FEATURE_INDIVIDUAL
199 /* Redundant for busybox (run_applet_and_exit covers that case)
200 * but needed for "individual applet" mode */
Denis Vlasenkobd28f6b2008-07-19 08:15:13 +0000201 if (argv[1] && !argv[2] && strcmp(argv[1], "--help") == 0) {
202 /* Special case. POSIX says "test --help"
203 * should be no different from e.g. "test --foo". */
204 if (!ENABLE_TEST || strcmp(applet_name, "test") != 0)
205 bb_show_usage();
206 }
Denis Vlasenko82d38da2007-10-10 14:38:47 +0000207#endif
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000208}
Denis Vlasenko724d1962007-10-10 14:41:07 +0000209
210/* The code below can well be in applets/applets.c, as it is used only
211 * for busybox binary, not "individual" binaries.
212 * However, keeping it here and linking it into libbusybox.so
213 * (together with remaining tiny applets/applets.o)
214 * makes it possible to avoid --whole-archive at link time.
215 * This makes (shared busybox) + libbusybox smaller.
216 * (--gc-sections would be even better....)
217 */
218
219const char *applet_name;
220#if !BB_MMU
221bool re_execed;
222#endif
223
Denis Vlasenko468aea22008-04-01 14:47:57 +0000224
Denis Vlasenko10f6fb12008-04-29 00:10:27 +0000225/* If not built as a single-applet executable... */
226#if !defined(SINGLE_APPLET_MAIN)
Denis Vlasenko468aea22008-04-01 14:47:57 +0000227
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000228IF_FEATURE_SUID(static uid_t ruid;) /* real uid */
Denis Vlasenko724d1962007-10-10 14:41:07 +0000229
230#if ENABLE_FEATURE_SUID_CONFIG
231
232/* applets[] is const, so we have to define this "override" structure */
233static struct BB_suid_config {
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000234 int m_applet;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000235 uid_t m_uid;
236 gid_t m_gid;
237 mode_t m_mode;
238 struct BB_suid_config *m_next;
239} *suid_config;
240
241static bool suid_cfg_readable;
242
243/* check if u is member of group g */
244static int ingroup(uid_t u, gid_t g)
245{
246 struct group *grp = getgrgid(g);
247
248 if (grp) {
249 char **mem;
250
251 for (mem = grp->gr_mem; *mem; mem++) {
252 struct passwd *pwd = getpwnam(*mem);
253
254 if (pwd && (pwd->pw_uid == u))
255 return 1;
256 }
257 }
258 return 0;
259}
260
261/* This should probably be a libbb routine. In that case,
262 * I'd probably rename it to something like bb_trimmed_slice.
263 */
264static char *get_trimmed_slice(char *s, char *e)
265{
266 /* First, consider the value at e to be nul and back up until we
267 * reach a non-space char. Set the char after that (possibly at
268 * the original e) to nul. */
269 while (e-- > s) {
270 if (!isspace(*e)) {
271 break;
272 }
273 }
274 e[1] = '\0';
275
276 /* Next, advance past all leading space and return a ptr to the
277 * first non-space char; possibly the terminating nul. */
278 return skip_whitespace(s);
279}
280
281/* Don't depend on the tools to combine strings. */
282static const char config_file[] ALIGN1 = "/etc/busybox.conf";
283
284/* We don't supply a value for the nul, so an index adjustment is
285 * necessary below. Also, we use unsigned short here to save some
286 * space even though these are really mode_t values. */
287static const unsigned short mode_mask[] ALIGN2 = {
288 /* SST sst xxx --- */
289 S_ISUID, S_ISUID|S_IXUSR, S_IXUSR, 0, /* user */
290 S_ISGID, S_ISGID|S_IXGRP, S_IXGRP, 0, /* group */
291 0, S_IXOTH, S_IXOTH, 0 /* other */
292};
293
294#define parse_error(x) do { errmsg = x; goto pe_label; } while (0)
295
296static void parse_config_file(void)
297{
298 struct BB_suid_config *sct_head;
299 struct BB_suid_config *sct;
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000300 int applet_no;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000301 FILE *f;
302 const char *errmsg;
303 char *s;
304 char *e;
305 int i;
306 unsigned lc;
307 smallint section;
308 char buffer[256];
309 struct stat st;
310
311 assert(!suid_config); /* Should be set to NULL by bss init. */
312
313 ruid = getuid();
314 if (ruid == 0) /* run by root - don't need to even read config file */
315 return;
316
317 if ((stat(config_file, &st) != 0) /* No config file? */
318 || !S_ISREG(st.st_mode) /* Not a regular file? */
319 || (st.st_uid != 0) /* Not owned by root? */
320 || (st.st_mode & (S_IWGRP | S_IWOTH)) /* Writable by non-root? */
Denis Vlasenko5415c852008-07-21 23:05:26 +0000321 || !(f = fopen_for_read(config_file)) /* Cannot open? */
Denis Vlasenko724d1962007-10-10 14:41:07 +0000322 ) {
323 return;
324 }
325
326 suid_cfg_readable = 1;
327 sct_head = NULL;
328 section = lc = 0;
329
330 while (1) {
331 s = buffer;
332
333 if (!fgets(s, sizeof(buffer), f)) { /* Are we done? */
Denis Vlasenkod02db892008-03-17 09:05:21 +0000334// why?
Denis Vlasenko724d1962007-10-10 14:41:07 +0000335 if (ferror(f)) { /* Make sure it wasn't a read error. */
336 parse_error("reading");
337 }
338 fclose(f);
339 suid_config = sct_head; /* Success, so set the pointer. */
340 return;
341 }
342
343 lc++; /* Got a (partial) line. */
344
345 /* If a line is too long for our buffer, we consider it an error.
346 * The following test does mistreat one corner case though.
347 * If the final line of the file does not end with a newline and
348 * yet exactly fills the buffer, it will be treated as too long
349 * even though there isn't really a problem. But it isn't really
350 * worth adding code to deal with such an unlikely situation, and
351 * we do err on the side of caution. Besides, the line would be
352 * too long if it did end with a newline. */
353 if (!strchr(s, '\n') && !feof(f)) {
354 parse_error("line too long");
355 }
356
357 /* Trim leading and trailing whitespace, ignoring comments, and
358 * check if the resulting string is empty. */
359 s = get_trimmed_slice(s, strchrnul(s, '#'));
360 if (!*s) {
361 continue;
362 }
363
364 /* Check for a section header. */
365
366 if (*s == '[') {
367 /* Unlike the old code, we ignore leading and trailing
368 * whitespace for the section name. We also require that
369 * there are no stray characters after the closing bracket. */
370 e = strchr(s, ']');
371 if (!e /* Missing right bracket? */
372 || e[1] /* Trailing characters? */
373 || !*(s = get_trimmed_slice(s+1, e)) /* Missing name? */
374 ) {
375 parse_error("section header");
376 }
377 /* Right now we only have one section so just check it.
378 * If more sections are added in the future, please don't
379 * resort to cascading ifs with multiple strcasecmp calls.
380 * That kind of bloated code is all too common. A loop
381 * and a string table would be a better choice unless the
382 * number of sections is very small. */
383 if (strcasecmp(s, "SUID") == 0) {
384 section = 1;
385 continue;
386 }
387 section = -1; /* Unknown section so set to skip. */
388 continue;
389 }
390
391 /* Process sections. */
392
393 if (section == 1) { /* SUID */
394 /* Since we trimmed leading and trailing space above, we're
395 * now looking for strings of the form
396 * <key>[::space::]*=[::space::]*<value>
397 * where both key and value could contain inner whitespace. */
398
399 /* First get the key (an applet name in our case). */
400 e = strchr(s, '=');
401 if (e) {
402 s = get_trimmed_slice(s, e);
403 }
404 if (!e || !*s) { /* Missing '=' or empty key. */
405 parse_error("keyword");
406 }
407
408 /* Ok, we have an applet name. Process the rhs if this
409 * applet is currently built in and ignore it otherwise.
410 * Note: this can hide config file bugs which only pop
411 * up when the busybox configuration is changed. */
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000412 applet_no = find_applet_by_name(s);
413 if (applet_no >= 0) {
Denis Vlasenko724d1962007-10-10 14:41:07 +0000414 /* Note: We currently don't check for duplicates!
415 * The last config line for each applet will be the
416 * one used since we insert at the head of the list.
417 * I suppose this could be considered a feature. */
418 sct = xmalloc(sizeof(struct BB_suid_config));
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000419 sct->m_applet = applet_no;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000420 sct->m_mode = 0;
421 sct->m_next = sct_head;
422 sct_head = sct;
423
424 /* Get the specified mode. */
425
426 e = skip_whitespace(e+1);
427
428 for (i = 0; i < 3; i++) {
429 /* There are 4 chars + 1 nul for each of user/group/other. */
430 static const char mode_chars[] ALIGN1 = "Ssx-\0" "Ssx-\0" "Ttx-";
431
432 const char *q;
433 q = strchrnul(mode_chars + 5*i, *e++);
434 if (!*q) {
435 parse_error("mode");
436 }
437 /* Adjust by -i to account for nul. */
438 sct->m_mode |= mode_mask[(q - mode_chars) - i];
439 }
440
441 /* Now get the the user/group info. */
442
443 s = skip_whitespace(e);
444
445 /* Note: we require whitespace between the mode and the
446 * user/group info. */
447 if ((s == e) || !(e = strchr(s, '.'))) {
448 parse_error("<uid>.<gid>");
449 }
450 *e++ = '\0';
451
452 /* We can't use get_ug_id here since it would exit()
453 * if a uid or gid was not found. Oh well... */
454 sct->m_uid = bb_strtoul(s, NULL, 10);
455 if (errno) {
456 struct passwd *pwd = getpwnam(s);
457 if (!pwd) {
458 parse_error("user");
459 }
460 sct->m_uid = pwd->pw_uid;
461 }
462
463 sct->m_gid = bb_strtoul(e, NULL, 10);
464 if (errno) {
465 struct group *grp;
466 grp = getgrnam(e);
467 if (!grp) {
468 parse_error("group");
469 }
470 sct->m_gid = grp->gr_gid;
471 }
472 }
473 continue;
474 }
475
476 /* Unknown sections are ignored. */
477
478 /* Encountering configuration lines prior to seeing a
479 * section header is treated as an error. This is how
480 * the old code worked, but it may not be desirable.
481 * We may want to simply ignore such lines in case they
482 * are used in some future version of busybox. */
483 if (!section) {
484 parse_error("keyword outside section");
485 }
486
487 } /* while (1) */
488
489 pe_label:
490 fprintf(stderr, "Parse error in %s, line %d: %s\n",
491 config_file, lc, errmsg);
492
493 fclose(f);
494 /* Release any allocated memory before returning. */
495 while (sct_head) {
496 sct = sct_head->m_next;
497 free(sct_head);
498 sct_head = sct;
499 }
500}
501#else
502static inline void parse_config_file(void)
503{
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000504 IF_FEATURE_SUID(ruid = getuid();)
Denis Vlasenko724d1962007-10-10 14:41:07 +0000505}
506#endif /* FEATURE_SUID_CONFIG */
507
508
509#if ENABLE_FEATURE_SUID
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000510static void check_suid(int applet_no)
Denis Vlasenko724d1962007-10-10 14:41:07 +0000511{
512 gid_t rgid; /* real gid */
513
514 if (ruid == 0) /* set by parse_config_file() */
515 return; /* run by root - no need to check more */
516 rgid = getgid();
517
518#if ENABLE_FEATURE_SUID_CONFIG
519 if (suid_cfg_readable) {
520 uid_t uid;
521 struct BB_suid_config *sct;
522 mode_t m;
523
524 for (sct = suid_config; sct; sct = sct->m_next) {
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000525 if (sct->m_applet == applet_no)
Denis Vlasenko724d1962007-10-10 14:41:07 +0000526 goto found;
527 }
Denis Vlasenko15ca51e2007-10-29 19:25:45 +0000528 goto check_need_suid;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000529 found:
530 m = sct->m_mode;
531 if (sct->m_uid == ruid)
532 /* same uid */
533 m >>= 6;
534 else if ((sct->m_gid == rgid) || ingroup(ruid, sct->m_gid))
535 /* same group / in group */
536 m >>= 3;
537
538 if (!(m & S_IXOTH)) /* is x bit not set ? */
539 bb_error_msg_and_die("you have no permission to run this applet!");
540
541 /* _both_ sgid and group_exec have to be set for setegid */
542 if ((sct->m_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
543 rgid = sct->m_gid;
544 /* else (no setegid) we will set egid = rgid */
545
546 /* We set effective AND saved ids. If saved-id is not set
547 * like we do below, seteiud(0) can still later succeed! */
548 if (setresgid(-1, rgid, rgid))
549 bb_perror_msg_and_die("setresgid");
550
551 /* do we have to set effective uid? */
552 uid = ruid;
553 if (sct->m_mode & S_ISUID)
554 uid = sct->m_uid;
555 /* else (no seteuid) we will set euid = ruid */
556
557 if (setresuid(-1, uid, uid))
558 bb_perror_msg_and_die("setresuid");
559 return;
560 }
561#if !ENABLE_FEATURE_SUID_CONFIG_QUIET
562 {
563 static bool onetime = 0;
564
565 if (!onetime) {
566 onetime = 1;
567 fprintf(stderr, "Using fallback suid method\n");
568 }
569 }
570#endif
Denis Vlasenko15ca51e2007-10-29 19:25:45 +0000571 check_need_suid:
Denis Vlasenko724d1962007-10-10 14:41:07 +0000572#endif
Denys Vlasenko6d48d3e2009-07-30 12:57:19 +0200573 if (APPLET_SUID(applet_no) == _BB_SUID_REQUIRE) {
Denis Vlasenko724d1962007-10-10 14:41:07 +0000574 /* Real uid is not 0. If euid isn't 0 too, suid bit
575 * is most probably not set on our executable */
576 if (geteuid())
Denis Vlasenko15ca51e2007-10-29 19:25:45 +0000577 bb_error_msg_and_die("must be suid to work properly");
Denys Vlasenko6d48d3e2009-07-30 12:57:19 +0200578 } else if (APPLET_SUID(applet_no) == _BB_SUID_DROP) {
Denis Vlasenko724d1962007-10-10 14:41:07 +0000579 xsetgid(rgid); /* drop all privileges */
580 xsetuid(ruid);
581 }
582}
583#else
584#define check_suid(x) ((void)0)
585#endif /* FEATURE_SUID */
586
587
588#if ENABLE_FEATURE_INSTALLER
589/* create (sym)links for each applet */
Denys Vlasenko3b405432009-07-15 00:35:34 +0200590static void install_links(const char *busybox, int use_symbolic_links,
591 char *custom_install_dir)
Denis Vlasenko724d1962007-10-10 14:41:07 +0000592{
593 /* directory table
594 * this should be consistent w/ the enum,
595 * busybox.h::bb_install_loc_t, or else... */
596 static const char usr_bin [] ALIGN1 = "/usr/bin";
597 static const char usr_sbin[] ALIGN1 = "/usr/sbin";
598 static const char *const install_dir[] = {
599 &usr_bin [8], /* "", equivalent to "/" for concat_path_file() */
600 &usr_bin [4], /* "/bin" */
601 &usr_sbin[4], /* "/sbin" */
602 usr_bin,
603 usr_sbin
604 };
605
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000606 int (*lf)(const char *, const char *);
Denis Vlasenko724d1962007-10-10 14:41:07 +0000607 char *fpc;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000608 unsigned i;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000609 int rc;
610
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000611 lf = link;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000612 if (use_symbolic_links)
613 lf = symlink;
614
Denis Vlasenko745cd172007-11-29 03:31:20 +0000615 for (i = 0; i < ARRAY_SIZE(applet_main); i++) {
Denis Vlasenko724d1962007-10-10 14:41:07 +0000616 fpc = concat_path_file(
Denys Vlasenko3b405432009-07-15 00:35:34 +0200617 custom_install_dir ? custom_install_dir : install_dir[APPLET_INSTALL_LOC(i)],
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000618 APPLET_NAME(i));
Denis Vlasenko745cd172007-11-29 03:31:20 +0000619 // debug: bb_error_msg("%slinking %s to busybox",
620 // use_symbolic_links ? "sym" : "", fpc);
Denis Vlasenko724d1962007-10-10 14:41:07 +0000621 rc = lf(busybox, fpc);
622 if (rc != 0 && errno != EEXIST) {
623 bb_simple_perror_msg(fpc);
624 }
625 free(fpc);
626 }
627}
628#else
Denys Vlasenko3b405432009-07-15 00:35:34 +0200629#define install_links(x,y,z) ((void)0)
Denis Vlasenko724d1962007-10-10 14:41:07 +0000630#endif /* FEATURE_INSTALLER */
631
632/* If we were called as "busybox..." */
633static int busybox_main(char **argv)
634{
635 if (!argv[1]) {
636 /* Called without arguments */
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000637 const char *a;
Denys Vlasenko0149f022009-05-19 18:01:42 +0200638 int col;
639 unsigned output_width;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000640 help:
641 output_width = 80;
642 if (ENABLE_FEATURE_AUTOWIDTH) {
643 /* Obtain the terminal width */
644 get_terminal_width_height(0, &output_width, NULL);
645 }
Denis Vlasenko724d1962007-10-10 14:41:07 +0000646
Denis Vlasenko21278df2008-06-25 12:15:46 +0000647 dup2(1, 2);
Denys Vlasenko630dde12009-08-30 19:57:49 +0200648 full_write2_str(bb_banner); /* reuse const string */
649 full_write2_str(" multi-call binary.\n"); /* reuse */
650 full_write2_str(
Denys Vlasenkob3475922009-08-27 21:13:30 +0200651 "Copyright (C) 1998-2009 Erik Andersen, Rob Landley, Denys Vlasenko\n"
Denis Vlasenkofcfb5c02007-12-24 12:16:24 +0000652 "and others. Licensed under GPLv2.\n"
653 "See source distribution for full notice.\n"
Denis Vlasenko724d1962007-10-10 14:41:07 +0000654 "\n"
655 "Usage: busybox [function] [arguments]...\n"
Denis Vlasenkofcfb5c02007-12-24 12:16:24 +0000656 " or: function [arguments]...\n"
Denis Vlasenko724d1962007-10-10 14:41:07 +0000657 "\n"
658 "\tBusyBox is a multi-call binary that combines many common Unix\n"
659 "\tutilities into a single executable. Most people will create a\n"
660 "\tlink to busybox for each function they wish to use and BusyBox\n"
Denys Vlasenko630dde12009-08-30 19:57:49 +0200661 "\twill act like whatever it was invoked as.\n"
Denis Vlasenkofcfb5c02007-12-24 12:16:24 +0000662 "\n"
663 "Currently defined functions:\n");
Denis Vlasenko724d1962007-10-10 14:41:07 +0000664 col = 0;
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000665 a = applet_names;
Denys Vlasenko0149f022009-05-19 18:01:42 +0200666 /* prevent last comma to be in the very last pos */
667 output_width--;
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000668 while (*a) {
Denys Vlasenko0149f022009-05-19 18:01:42 +0200669 int len2 = strlen(a) + 2;
670 if (col >= (int)output_width - len2) {
Denis Vlasenko79cedcb2008-04-08 21:13:28 +0000671 full_write2_str(",\n");
Denis Vlasenko724d1962007-10-10 14:41:07 +0000672 col = 0;
673 }
Denys Vlasenko0149f022009-05-19 18:01:42 +0200674 if (col == 0) {
675 col = 6;
676 full_write2_str("\t");
677 } else {
678 full_write2_str(", ");
679 }
Denis Vlasenko79cedcb2008-04-08 21:13:28 +0000680 full_write2_str(a);
Denys Vlasenko0149f022009-05-19 18:01:42 +0200681 col += len2;
682 a += len2 - 1;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000683 }
Denis Vlasenko79cedcb2008-04-08 21:13:28 +0000684 full_write2_str("\n\n");
Denis Vlasenko724d1962007-10-10 14:41:07 +0000685 return 0;
686 }
687
688 if (ENABLE_FEATURE_INSTALLER && strcmp(argv[1], "--install") == 0) {
Denys Vlasenko3b405432009-07-15 00:35:34 +0200689 int use_symbolic_links;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000690 const char *busybox;
691 busybox = xmalloc_readlink(bb_busybox_exec_path);
692 if (!busybox)
693 busybox = bb_busybox_exec_path;
Denys Vlasenko0ad438b2009-07-15 02:14:15 +0200694 /* busybox --install [-s] [DIR]: */
695 /* -s: make symlinks */
696 /* DIR: directory to install links to */
Denys Vlasenko3b405432009-07-15 00:35:34 +0200697 use_symbolic_links = (argv[2] && strcmp(argv[2], "-s") == 0 && argv++);
698 install_links(busybox, use_symbolic_links, argv[2]);
Denis Vlasenko724d1962007-10-10 14:41:07 +0000699 return 0;
700 }
701
702 if (strcmp(argv[1], "--help") == 0) {
703 /* "busybox --help [<applet>]" */
704 if (!argv[2])
705 goto help;
706 /* convert to "<applet> --help" */
707 argv[0] = argv[2];
708 argv[2] = NULL;
709 } else {
710 /* "busybox <applet> arg1 arg2 ..." */
711 argv++;
712 }
713 /* We support "busybox /a/path/to/applet args..." too. Allows for
714 * "#!/bin/busybox"-style wrappers */
715 applet_name = bb_get_last_path_component_nostrip(argv[0]);
716 run_applet_and_exit(applet_name, argv);
Denis Vlasenko79cedcb2008-04-08 21:13:28 +0000717
718 /*bb_error_msg_and_die("applet not found"); - sucks in printf */
719 full_write2_str(applet_name);
720 full_write2_str(": applet not found\n");
721 xfunc_die();
Denis Vlasenko724d1962007-10-10 14:41:07 +0000722}
723
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000724void FAST_FUNC run_applet_no_and_exit(int applet_no, char **argv)
Denis Vlasenko724d1962007-10-10 14:41:07 +0000725{
726 int argc = 1;
727
728 while (argv[argc])
729 argc++;
730
731 /* Reinit some shared global data */
Denis Vlasenko724d1962007-10-10 14:41:07 +0000732 xfunc_error_retval = EXIT_FAILURE;
733
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000734 applet_name = APPLET_NAME(applet_no);
Denis Vlasenkobd28f6b2008-07-19 08:15:13 +0000735 if (argc == 2 && strcmp(argv[1], "--help") == 0) {
736 /* Special case. POSIX says "test --help"
737 * should be no different from e.g. "test --foo". */
738//TODO: just compare applet_no with APPLET_NO_test
739 if (!ENABLE_TEST || strcmp(applet_name, "test") != 0)
740 bb_show_usage();
741 }
Denis Vlasenko724d1962007-10-10 14:41:07 +0000742 if (ENABLE_FEATURE_SUID)
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000743 check_suid(applet_no);
Denis Vlasenko745cd172007-11-29 03:31:20 +0000744 exit(applet_main[applet_no](argc, argv));
Denis Vlasenko724d1962007-10-10 14:41:07 +0000745}
746
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000747void FAST_FUNC run_applet_and_exit(const char *name, char **argv)
Denis Vlasenko724d1962007-10-10 14:41:07 +0000748{
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000749 int applet = find_applet_by_name(name);
750 if (applet >= 0)
751 run_applet_no_and_exit(applet, argv);
Denis Vlasenko724d1962007-10-10 14:41:07 +0000752 if (!strncmp(name, "busybox", 7))
753 exit(busybox_main(argv));
754}
755
Denis Vlasenko10f6fb12008-04-29 00:10:27 +0000756#endif /* !defined(SINGLE_APPLET_MAIN) */
Denis Vlasenko468aea22008-04-01 14:47:57 +0000757
758
Denis Vlasenko724d1962007-10-10 14:41:07 +0000759
760#if ENABLE_BUILD_LIBBUSYBOX
Denis Vlasenko85c24712008-03-17 09:04:04 +0000761int lbb_main(char **argv)
Denis Vlasenko724d1962007-10-10 14:41:07 +0000762#else
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000763int main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko724d1962007-10-10 14:41:07 +0000764#endif
765{
Denis Vlasenko10f6fb12008-04-29 00:10:27 +0000766#if defined(SINGLE_APPLET_MAIN)
Denis Vlasenko468aea22008-04-01 14:47:57 +0000767 /* Only one applet is selected by the user! */
Denis Vlasenko79cedcb2008-04-08 21:13:28 +0000768 /* applet_names in this case is just "applet\0\0" */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000769 lbb_prepare(applet_names IF_FEATURE_INDIVIDUAL(, argv));
Denis Vlasenko468aea22008-04-01 14:47:57 +0000770 return SINGLE_APPLET_MAIN(argc, argv);
771#else
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000772 lbb_prepare("busybox" IF_FEATURE_INDIVIDUAL(, argv));
Denis Vlasenko724d1962007-10-10 14:41:07 +0000773
774#if !BB_MMU
775 /* NOMMU re-exec trick sets high-order bit in first byte of name */
776 if (argv[0][0] & 0x80) {
777 re_execed = 1;
778 argv[0][0] &= 0x7f;
779 }
780#endif
781 applet_name = argv[0];
782 if (applet_name[0] == '-')
783 applet_name++;
784 applet_name = bb_basename(applet_name);
785
786 parse_config_file(); /* ...maybe, if FEATURE_SUID_CONFIG */
787
788 run_applet_and_exit(applet_name, argv);
Denis Vlasenko79cedcb2008-04-08 21:13:28 +0000789
790 /*bb_error_msg_and_die("applet not found"); - sucks in printf */
791 full_write2_str(applet_name);
792 full_write2_str(": applet not found\n");
793 xfunc_die();
Denis Vlasenko468aea22008-04-01 14:47:57 +0000794#endif
Denis Vlasenko724d1962007-10-10 14:41:07 +0000795}