1 /* vi: set sw=4 ts=4: */
5 * 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.
9 * 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.
12 * Licensed under GPLv2 or later, see file License in this tarball for details.
15 /* We are trying to not use printf, this benefits the case when selected
16 * applets are really simple. Example:
20 * Currently defined functions:
21 * basename, false, true
24 * text data bss dec hex filename
25 * 4473 52 72 4597 11f5 busybox
27 * FEATURE_INSTALLER or FEATURE_SUID will still link printf routines in. :(
32 /* Try to pull in PAGE_SIZE */
34 # include <sys/user.h>
36 #ifdef __GNU__ /* Hurd */
37 # include <mach/vm_param.h>
41 /* Declare <applet>_main() */
46 #if ENABLE_SHOW_USAGE && !ENABLE_FEATURE_COMPRESS_USAGE
47 /* Define usage_messages[] */
48 static const char usage_messages[] ALIGN1 = ""
55 # define usage_messages 0
56 #endif /* SHOW_USAGE */
59 /* Include generated applet names, pointers to <applet>_main, etc */
60 #include "applet_tables.h"
61 /* ...and if applet_tables generator says we have only one applet... */
62 #ifdef SINGLE_APPLET_MAIN
63 #undef ENABLE_FEATURE_INDIVIDUAL
64 #define ENABLE_FEATURE_INDIVIDUAL 1
65 #undef IF_FEATURE_INDIVIDUAL
66 #define IF_FEATURE_INDIVIDUAL(...) __VA_ARGS__
70 #if ENABLE_FEATURE_COMPRESS_USAGE
72 #include "usage_compressed.h"
73 #include "unarchive.h"
75 static const char *unpack_usage_messages(void)
83 /* inbuf: */ (void *)packed_usage,
84 /* len: */ sizeof(packed_usage));
85 /* read_bunzip can longjmp to start_bunzip, and ultimately
86 * end up here with i != 0 on read data errors! Not trivial */
88 /* Cannot use xmalloc: will leak bd in NOFORK case! */
89 outbuf = malloc_or_warn(SIZEOF_usage_messages);
91 read_bunzip(bd, outbuf, SIZEOF_usage_messages);
96 #define dealloc_usage_messages(s) free(s)
100 #define unpack_usage_messages() usage_messages
101 #define dealloc_usage_messages(s) ((void)(s))
103 #endif /* FEATURE_COMPRESS_USAGE */
106 static void full_write2_str(const char *str)
109 //xwrite_str(STDERR_FILENO, str);
110 write(STDERR_FILENO, str, strlen(str));
113 void FAST_FUNC bb_show_usage(void)
115 if (ENABLE_SHOW_USAGE) {
116 #ifdef SINGLE_APPLET_STR
117 /* Imagine that this applet is "true". Dont suck in printf! */
119 const char *usage_string = p = unpack_usage_messages();
122 full_write2_str("No help available.\n\n");
124 full_write2_str("Usage: "SINGLE_APPLET_STR" ");
126 full_write2_str("\n\n");
128 if (ENABLE_FEATURE_CLEAN_UP)
129 dealloc_usage_messages((char*)usage_string);
132 const char *usage_string = p = unpack_usage_messages();
133 int ap = find_applet_by_name(applet_name);
135 if (ap < 0) /* never happens, paranoia */
138 while (*p++) continue;
141 full_write2_str(bb_banner);
142 full_write2_str(" multi-call binary.\n");
144 full_write2_str("\nNo help available.\n\n");
146 full_write2_str("\nUsage: ");
147 full_write2_str(applet_name);
148 full_write2_str(" ");
150 full_write2_str("\n\n");
152 if (ENABLE_FEATURE_CLEAN_UP)
153 dealloc_usage_messages((char*)usage_string);
160 /* NB: any char pointer will work as well, not necessarily applet_names */
161 static int applet_name_compare(const void *name, const void *v)
163 int i = (const char *)v - applet_names;
164 return strcmp(name, APPLET_NAME(i));
167 int FAST_FUNC find_applet_by_name(const char *name)
170 /* Do a binary search to find the applet entry given the name. */
172 p = bsearch(name, applet_names, ARRAY_SIZE(applet_main), 1, applet_name_compare);
175 return p - applet_names;
177 /* A version which does not pull in bsearch */
179 const char *p = applet_names;
180 while (i < NUM_APPLETS) {
181 if (strcmp(name, p) == 0)
191 void lbb_prepare(const char *applet
192 IF_FEATURE_INDIVIDUAL(, char **argv))
193 MAIN_EXTERNALLY_VISIBLE;
194 void lbb_prepare(const char *applet
195 IF_FEATURE_INDIVIDUAL(, char **argv))
198 (*(int **)&bb_errno) = __errno_location();
201 applet_name = applet;
203 /* Set locale for everybody except 'init' */
204 if (ENABLE_LOCALE_SUPPORT && getpid() != 1)
205 setlocale(LC_ALL, "");
207 #if ENABLE_FEATURE_INDIVIDUAL
208 /* Redundant for busybox (run_applet_and_exit covers that case)
209 * but needed for "individual applet" mode */
210 if (argv[1] && !argv[2] && strcmp(argv[1], "--help") == 0) {
211 /* Special case. POSIX says "test --help"
212 * should be no different from e.g. "test --foo". */
213 if (!ENABLE_TEST || strcmp(applet_name, "test") != 0)
219 /* The code below can well be in applets/applets.c, as it is used only
220 * for busybox binary, not "individual" binaries.
221 * However, keeping it here and linking it into libbusybox.so
222 * (together with remaining tiny applets/applets.o)
223 * makes it possible to avoid --whole-archive at link time.
224 * This makes (shared busybox) + libbusybox smaller.
225 * (--gc-sections would be even better....)
228 const char *applet_name;
234 /* If not built as a single-applet executable... */
235 #if !defined(SINGLE_APPLET_MAIN)
237 IF_FEATURE_SUID(static uid_t ruid;) /* real uid */
239 #if ENABLE_FEATURE_SUID_CONFIG
241 /* applets[] is const, so we have to define this "override" structure */
242 static struct BB_suid_config {
247 struct BB_suid_config *m_next;
250 static bool suid_cfg_readable;
252 /* check if u is member of group g */
253 static int ingroup(uid_t u, gid_t g)
255 struct group *grp = getgrgid(g);
260 for (mem = grp->gr_mem; *mem; mem++) {
261 struct passwd *pwd = getpwnam(*mem);
263 if (pwd && (pwd->pw_uid == u))
270 /* This should probably be a libbb routine. In that case,
271 * I'd probably rename it to something like bb_trimmed_slice.
273 static char *get_trimmed_slice(char *s, char *e)
275 /* First, consider the value at e to be nul and back up until we
276 * reach a non-space char. Set the char after that (possibly at
277 * the original e) to nul. */
285 /* Next, advance past all leading space and return a ptr to the
286 * first non-space char; possibly the terminating nul. */
287 return skip_whitespace(s);
290 /* Don't depend on the tools to combine strings. */
291 static const char config_file[] ALIGN1 = "/etc/busybox.conf";
293 /* We don't supply a value for the nul, so an index adjustment is
294 * necessary below. Also, we use unsigned short here to save some
295 * space even though these are really mode_t values. */
296 static const unsigned short mode_mask[] ALIGN2 = {
297 /* SST sst xxx --- */
298 S_ISUID, S_ISUID|S_IXUSR, S_IXUSR, 0, /* user */
299 S_ISGID, S_ISGID|S_IXGRP, S_IXGRP, 0, /* group */
300 0, S_IXOTH, S_IXOTH, 0 /* other */
303 #define parse_error(x) do { errmsg = x; goto pe_label; } while (0)
305 static void parse_config_file(void)
307 struct BB_suid_config *sct_head;
308 struct BB_suid_config *sct;
320 assert(!suid_config); /* Should be set to NULL by bss init. */
323 if (ruid == 0) /* run by root - don't need to even read config file */
326 if ((stat(config_file, &st) != 0) /* No config file? */
327 || !S_ISREG(st.st_mode) /* Not a regular file? */
328 || (st.st_uid != 0) /* Not owned by root? */
329 || (st.st_mode & (S_IWGRP | S_IWOTH)) /* Writable by non-root? */
330 || !(f = fopen_for_read(config_file)) /* Cannot open? */
335 suid_cfg_readable = 1;
342 if (!fgets(s, sizeof(buffer), f)) { /* Are we done? */
344 if (ferror(f)) { /* Make sure it wasn't a read error. */
345 parse_error("reading");
348 suid_config = sct_head; /* Success, so set the pointer. */
352 lc++; /* Got a (partial) line. */
354 /* If a line is too long for our buffer, we consider it an error.
355 * The following test does mistreat one corner case though.
356 * If the final line of the file does not end with a newline and
357 * yet exactly fills the buffer, it will be treated as too long
358 * even though there isn't really a problem. But it isn't really
359 * worth adding code to deal with such an unlikely situation, and
360 * we do err on the side of caution. Besides, the line would be
361 * too long if it did end with a newline. */
362 if (!strchr(s, '\n') && !feof(f)) {
363 parse_error("line too long");
366 /* Trim leading and trailing whitespace, ignoring comments, and
367 * check if the resulting string is empty. */
368 s = get_trimmed_slice(s, strchrnul(s, '#'));
373 /* Check for a section header. */
376 /* Unlike the old code, we ignore leading and trailing
377 * whitespace for the section name. We also require that
378 * there are no stray characters after the closing bracket. */
380 if (!e /* Missing right bracket? */
381 || e[1] /* Trailing characters? */
382 || !*(s = get_trimmed_slice(s+1, e)) /* Missing name? */
384 parse_error("section header");
386 /* Right now we only have one section so just check it.
387 * If more sections are added in the future, please don't
388 * resort to cascading ifs with multiple strcasecmp calls.
389 * That kind of bloated code is all too common. A loop
390 * and a string table would be a better choice unless the
391 * number of sections is very small. */
392 if (strcasecmp(s, "SUID") == 0) {
396 section = -1; /* Unknown section so set to skip. */
400 /* Process sections. */
402 if (section == 1) { /* SUID */
403 /* Since we trimmed leading and trailing space above, we're
404 * now looking for strings of the form
405 * <key>[::space::]*=[::space::]*<value>
406 * where both key and value could contain inner whitespace. */
408 /* First get the key (an applet name in our case). */
411 s = get_trimmed_slice(s, e);
413 if (!e || !*s) { /* Missing '=' or empty key. */
414 parse_error("keyword");
417 /* Ok, we have an applet name. Process the rhs if this
418 * applet is currently built in and ignore it otherwise.
419 * Note: this can hide config file bugs which only pop
420 * up when the busybox configuration is changed. */
421 applet_no = find_applet_by_name(s);
422 if (applet_no >= 0) {
423 /* Note: We currently don't check for duplicates!
424 * The last config line for each applet will be the
425 * one used since we insert at the head of the list.
426 * I suppose this could be considered a feature. */
427 sct = xmalloc(sizeof(struct BB_suid_config));
428 sct->m_applet = applet_no;
430 sct->m_next = sct_head;
433 /* Get the specified mode. */
435 e = skip_whitespace(e+1);
437 for (i = 0; i < 3; i++) {
438 /* There are 4 chars + 1 nul for each of user/group/other. */
439 static const char mode_chars[] ALIGN1 = "Ssx-\0" "Ssx-\0" "Ttx-";
442 q = strchrnul(mode_chars + 5*i, *e++);
446 /* Adjust by -i to account for nul. */
447 sct->m_mode |= mode_mask[(q - mode_chars) - i];
450 /* Now get the the user/group info. */
452 s = skip_whitespace(e);
454 /* Note: we require whitespace between the mode and the
455 * user/group info. */
456 if ((s == e) || !(e = strchr(s, '.'))) {
457 parse_error("<uid>.<gid>");
461 /* We can't use get_ug_id here since it would exit()
462 * if a uid or gid was not found. Oh well... */
463 sct->m_uid = bb_strtoul(s, NULL, 10);
465 struct passwd *pwd = getpwnam(s);
469 sct->m_uid = pwd->pw_uid;
472 sct->m_gid = bb_strtoul(e, NULL, 10);
477 parse_error("group");
479 sct->m_gid = grp->gr_gid;
485 /* Unknown sections are ignored. */
487 /* Encountering configuration lines prior to seeing a
488 * section header is treated as an error. This is how
489 * the old code worked, but it may not be desirable.
490 * We may want to simply ignore such lines in case they
491 * are used in some future version of busybox. */
493 parse_error("keyword outside section");
499 fprintf(stderr, "Parse error in %s, line %d: %s\n",
500 config_file, lc, errmsg);
503 /* Release any allocated memory before returning. */
505 sct = sct_head->m_next;
511 static inline void parse_config_file(void)
513 IF_FEATURE_SUID(ruid = getuid();)
515 #endif /* FEATURE_SUID_CONFIG */
518 #if ENABLE_FEATURE_SUID
519 static void check_suid(int applet_no)
521 gid_t rgid; /* real gid */
523 if (ruid == 0) /* set by parse_config_file() */
524 return; /* run by root - no need to check more */
527 #if ENABLE_FEATURE_SUID_CONFIG
528 if (suid_cfg_readable) {
530 struct BB_suid_config *sct;
533 for (sct = suid_config; sct; sct = sct->m_next) {
534 if (sct->m_applet == applet_no)
537 goto check_need_suid;
540 if (sct->m_uid == ruid)
543 else if ((sct->m_gid == rgid) || ingroup(ruid, sct->m_gid))
544 /* same group / in group */
547 if (!(m & S_IXOTH)) /* is x bit not set ? */
548 bb_error_msg_and_die("you have no permission to run this applet!");
550 /* _both_ sgid and group_exec have to be set for setegid */
551 if ((sct->m_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
553 /* else (no setegid) we will set egid = rgid */
555 /* We set effective AND saved ids. If saved-id is not set
556 * like we do below, seteiud(0) can still later succeed! */
557 if (setresgid(-1, rgid, rgid))
558 bb_perror_msg_and_die("setresgid");
560 /* do we have to set effective uid? */
562 if (sct->m_mode & S_ISUID)
564 /* else (no seteuid) we will set euid = ruid */
566 if (setresuid(-1, uid, uid))
567 bb_perror_msg_and_die("setresuid");
570 #if !ENABLE_FEATURE_SUID_CONFIG_QUIET
572 static bool onetime = 0;
576 fprintf(stderr, "Using fallback suid method\n");
582 if (APPLET_SUID(applet_no) == _BB_SUID_REQUIRE) {
583 /* Real uid is not 0. If euid isn't 0 too, suid bit
584 * is most probably not set on our executable */
586 bb_error_msg_and_die("must be suid to work properly");
587 } else if (APPLET_SUID(applet_no) == _BB_SUID_DROP) {
588 xsetgid(rgid); /* drop all privileges */
593 #define check_suid(x) ((void)0)
594 #endif /* FEATURE_SUID */
597 #if ENABLE_FEATURE_INSTALLER
598 /* create (sym)links for each applet */
599 static void install_links(const char *busybox, int use_symbolic_links,
600 char *custom_install_dir)
603 * this should be consistent w/ the enum,
604 * busybox.h::bb_install_loc_t, or else... */
605 static const char usr_bin [] ALIGN1 = "/usr/bin";
606 static const char usr_sbin[] ALIGN1 = "/usr/sbin";
607 static const char *const install_dir[] = {
608 &usr_bin [8], /* "", equivalent to "/" for concat_path_file() */
609 &usr_bin [4], /* "/bin" */
610 &usr_sbin[4], /* "/sbin" */
615 int (*lf)(const char *, const char *);
621 if (use_symbolic_links)
624 for (i = 0; i < ARRAY_SIZE(applet_main); i++) {
625 fpc = concat_path_file(
626 custom_install_dir ? custom_install_dir : install_dir[APPLET_INSTALL_LOC(i)],
628 // debug: bb_error_msg("%slinking %s to busybox",
629 // use_symbolic_links ? "sym" : "", fpc);
630 rc = lf(busybox, fpc);
631 if (rc != 0 && errno != EEXIST) {
632 bb_simple_perror_msg(fpc);
638 #define install_links(x,y,z) ((void)0)
639 #endif /* FEATURE_INSTALLER */
641 /* If we were called as "busybox..." */
642 static int busybox_main(char **argv)
645 /* Called without arguments */
648 unsigned output_width;
651 if (ENABLE_FEATURE_AUTOWIDTH) {
652 /* Obtain the terminal width */
653 get_terminal_width_height(0, &output_width, NULL);
657 full_write2_str(bb_banner); /* reuse const string */
658 full_write2_str(" multi-call binary.\n"); /* reuse */
660 "Copyright (C) 1998-2009 Erik Andersen, Rob Landley, Denys Vlasenko\n"
661 "and others. Licensed under GPLv2.\n"
662 "See source distribution for full notice.\n"
664 "Usage: busybox [function] [arguments]...\n"
665 " or: function [arguments]...\n"
667 "\tBusyBox is a multi-call binary that combines many common Unix\n"
668 "\tutilities into a single executable. Most people will create a\n"
669 "\tlink to busybox for each function they wish to use and BusyBox\n"
670 "\twill act like whatever it was invoked as.\n"
672 "Currently defined functions:\n");
675 /* prevent last comma to be in the very last pos */
678 int len2 = strlen(a) + 2;
679 if (col >= (int)output_width - len2) {
680 full_write2_str(",\n");
685 full_write2_str("\t");
687 full_write2_str(", ");
693 full_write2_str("\n\n");
697 if (ENABLE_FEATURE_INSTALLER && strcmp(argv[1], "--install") == 0) {
698 int use_symbolic_links;
700 busybox = xmalloc_readlink(bb_busybox_exec_path);
702 busybox = bb_busybox_exec_path;
703 /* busybox --install [-s] [DIR]: */
704 /* -s: make symlinks */
705 /* DIR: directory to install links to */
706 use_symbolic_links = (argv[2] && strcmp(argv[2], "-s") == 0 && argv++);
707 install_links(busybox, use_symbolic_links, argv[2]);
711 if (strcmp(argv[1], "--help") == 0) {
712 /* "busybox --help [<applet>]" */
715 /* convert to "<applet> --help" */
719 /* "busybox <applet> arg1 arg2 ..." */
722 /* We support "busybox /a/path/to/applet args..." too. Allows for
723 * "#!/bin/busybox"-style wrappers */
724 applet_name = bb_get_last_path_component_nostrip(argv[0]);
725 run_applet_and_exit(applet_name, argv);
727 /*bb_error_msg_and_die("applet not found"); - sucks in printf */
728 full_write2_str(applet_name);
729 full_write2_str(": applet not found\n");
733 void FAST_FUNC run_applet_no_and_exit(int applet_no, char **argv)
740 /* Reinit some shared global data */
741 xfunc_error_retval = EXIT_FAILURE;
743 applet_name = APPLET_NAME(applet_no);
744 if (argc == 2 && strcmp(argv[1], "--help") == 0) {
745 /* Special case. POSIX says "test --help"
746 * should be no different from e.g. "test --foo". */
747 //TODO: just compare applet_no with APPLET_NO_test
748 if (!ENABLE_TEST || strcmp(applet_name, "test") != 0)
751 if (ENABLE_FEATURE_SUID)
752 check_suid(applet_no);
753 exit(applet_main[applet_no](argc, argv));
756 void FAST_FUNC run_applet_and_exit(const char *name, char **argv)
758 int applet = find_applet_by_name(name);
760 run_applet_no_and_exit(applet, argv);
761 if (!strncmp(name, "busybox", 7))
762 exit(busybox_main(argv));
765 #endif /* !defined(SINGLE_APPLET_MAIN) */
769 #if ENABLE_BUILD_LIBBUSYBOX
770 int lbb_main(char **argv)
772 int main(int argc UNUSED_PARAM, char **argv)
775 /* Tweak malloc for reduced memory consumption */
777 # define PAGE_SIZE (4*1024) /* guess */
779 #ifdef M_TRIM_THRESHOLD
780 /* M_TRIM_THRESHOLD is the maximum amount of freed top-most memory
781 * to keep before releasing to the OS
782 * Default is way too big: 256k
784 mallopt(M_TRIM_THRESHOLD, 2 * PAGE_SIZE);
786 #ifdef M_MMAP_THRESHOLD
787 /* M_MMAP_THRESHOLD is the request size threshold for using mmap()
788 * Default is too big: 256k
790 mallopt(M_MMAP_THRESHOLD, 8 * PAGE_SIZE - 256);
793 #if defined(SINGLE_APPLET_MAIN)
794 /* Only one applet is selected by the user! */
795 /* applet_names in this case is just "applet\0\0" */
796 lbb_prepare(applet_names IF_FEATURE_INDIVIDUAL(, argv));
797 return SINGLE_APPLET_MAIN(argc, argv);
799 lbb_prepare("busybox" IF_FEATURE_INDIVIDUAL(, argv));
802 /* NOMMU re-exec trick sets high-order bit in first byte of name */
803 if (argv[0][0] & 0x80) {
808 applet_name = argv[0];
809 if (applet_name[0] == '-')
811 applet_name = bb_basename(applet_name);
813 parse_config_file(); /* ...maybe, if FEATURE_SUID_CONFIG */
815 run_applet_and_exit(applet_name, argv);
817 /*bb_error_msg_and_die("applet not found"); - sucks in printf */
818 full_write2_str(applet_name);
819 full_write2_str(": applet not found\n");