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.
18 /* Apparently uclibc defines __GLIBC__ (compat trick?). Oh well. */
19 #if ENABLE_STATIC && defined(__GLIBC__) && !defined(__UCLIBC__)
20 #warning Static linking against glibc produces buggy executables
21 #warning (glibc does not cope well with ld --gc-sections).
22 #warning See sources.redhat.com/bugzilla/show_bug.cgi?id=3400
23 #warning Note that glibc is unsuitable for static linking anyway.
24 #warning If you still want to do it, remove -Wl,--gc-sections
25 #warning from top-level Makefile and remove this warning.
26 #error Aborting compilation.
30 /* Declare <applet>_main() */
35 #if ENABLE_SHOW_USAGE && !ENABLE_FEATURE_COMPRESS_USAGE
36 /* Define usage_messages[] */
37 static const char usage_messages[] = ""
44 #define usage_messages 0
45 #endif /* SHOW_USAGE */
47 /* Define struct bb_applet applets[] */
49 /* The -1 arises because of the {0,NULL,0,-1} entry. */
50 const unsigned short NUM_APPLETS = sizeof(applets) / sizeof(applets[0]) - 1;
52 const struct bb_applet *current_applet;
53 const char *applet_name ATTRIBUTE_EXTERNALLY_VISIBLE;
58 USE_FEATURE_SUID(static uid_t ruid;) /* real uid */
60 #if ENABLE_FEATURE_SUID_CONFIG
62 /* applets[] is const, so we have to define this "override" structure */
63 static struct BB_suid_config {
64 const struct bb_applet *m_applet;
68 struct BB_suid_config *m_next;
71 static bool suid_cfg_readable;
73 /* check if u is member of group g */
74 static int ingroup(uid_t u, gid_t g)
76 struct group *grp = getgrgid(g);
81 for (mem = grp->gr_mem; *mem; mem++) {
82 struct passwd *pwd = getpwnam(*mem);
84 if (pwd && (pwd->pw_uid == u))
91 /* This should probably be a libbb routine. In that case,
92 * I'd probably rename it to something like bb_trimmed_slice.
94 static char *get_trimmed_slice(char *s, char *e)
96 /* First, consider the value at e to be nul and back up until we
97 * reach a non-space char. Set the char after that (possibly at
98 * the original e) to nul. */
106 /* Next, advance past all leading space and return a ptr to the
107 * first non-space char; possibly the terminating nul. */
108 return skip_whitespace(s);
111 /* Don't depend on the tools to combine strings. */
112 static const char config_file[] = "/etc/busybox.conf";
114 /* We don't supply a value for the nul, so an index adjustment is
115 * necessary below. Also, we use unsigned short here to save some
116 * space even though these are really mode_t values. */
117 static const unsigned short mode_mask[] = {
118 /* SST sst xxx --- */
119 S_ISUID, S_ISUID|S_IXUSR, S_IXUSR, 0, /* user */
120 S_ISGID, S_ISGID|S_IXGRP, S_IXGRP, 0, /* group */
121 0, S_IXOTH, S_IXOTH, 0 /* other */
124 #define parse_error(x) do { errmsg = x; goto pe_label; } while (0)
126 static void parse_config_file(void)
128 struct BB_suid_config *sct_head;
129 struct BB_suid_config *sct;
130 const struct bb_applet *applet;
141 assert(!suid_config); /* Should be set to NULL by bss init. */
144 if (ruid == 0) /* run by root - don't need to even read config file */
147 if ((stat(config_file, &st) != 0) /* No config file? */
148 || !S_ISREG(st.st_mode) /* Not a regular file? */
149 || (st.st_uid != 0) /* Not owned by root? */
150 || (st.st_mode & (S_IWGRP | S_IWOTH)) /* Writable by non-root? */
151 || !(f = fopen(config_file, "r")) /* Cannot open? */
156 suid_cfg_readable = 1;
163 if (!fgets(s, sizeof(buffer), f)) { /* Are we done? */
164 if (ferror(f)) { /* Make sure it wasn't a read error. */
165 parse_error("reading");
168 suid_config = sct_head; /* Success, so set the pointer. */
172 lc++; /* Got a (partial) line. */
174 /* If a line is too long for our buffer, we consider it an error.
175 * The following test does mistreat one corner case though.
176 * If the final line of the file does not end with a newline and
177 * yet exactly fills the buffer, it will be treated as too long
178 * even though there isn't really a problem. But it isn't really
179 * worth adding code to deal with such an unlikely situation, and
180 * we do err on the side of caution. Besides, the line would be
181 * too long if it did end with a newline. */
182 if (!strchr(s, '\n') && !feof(f)) {
183 parse_error("line too long");
186 /* Trim leading and trailing whitespace, ignoring comments, and
187 * check if the resulting string is empty. */
188 s = get_trimmed_slice(s, strchrnul(s, '#'));
193 /* Check for a section header. */
196 /* Unlike the old code, we ignore leading and trailing
197 * whitespace for the section name. We also require that
198 * there are no stray characters after the closing bracket. */
200 if (!e /* Missing right bracket? */
201 || e[1] /* Trailing characters? */
202 || !*(s = get_trimmed_slice(s+1, e)) /* Missing name? */
204 parse_error("section header");
206 /* Right now we only have one section so just check it.
207 * If more sections are added in the future, please don't
208 * resort to cascading ifs with multiple strcasecmp calls.
209 * That kind of bloated code is all too common. A loop
210 * and a string table would be a better choice unless the
211 * number of sections is very small. */
212 if (strcasecmp(s, "SUID") == 0) {
216 section = -1; /* Unknown section so set to skip. */
220 /* Process sections. */
222 if (section == 1) { /* SUID */
223 /* Since we trimmed leading and trailing space above, we're
224 * now looking for strings of the form
225 * <key>[::space::]*=[::space::]*<value>
226 * where both key and value could contain inner whitespace. */
228 /* First get the key (an applet name in our case). */
231 s = get_trimmed_slice(s, e);
233 if (!e || !*s) { /* Missing '=' or empty key. */
234 parse_error("keyword");
237 /* Ok, we have an applet name. Process the rhs if this
238 * applet is currently built in and ignore it otherwise.
239 * Note: this can hide config file bugs which only pop
240 * up when the busybox configuration is changed. */
241 applet = find_applet_by_name(s);
243 /* Note: We currently don't check for duplicates!
244 * The last config line for each applet will be the
245 * one used since we insert at the head of the list.
246 * I suppose this could be considered a feature. */
247 sct = xmalloc(sizeof(struct BB_suid_config));
248 sct->m_applet = applet;
250 sct->m_next = sct_head;
253 /* Get the specified mode. */
255 e = skip_whitespace(e+1);
257 for (i = 0; i < 3; i++) {
258 /* There are 4 chars + 1 nul for each of user/group/other. */
259 static const char mode_chars[] = "Ssx-\0" "Ssx-\0" "Ttx-";
262 q = strchrnul(mode_chars + 5*i, *e++);
266 /* Adjust by -i to account for nul. */
267 sct->m_mode |= mode_mask[(q - mode_chars) - i];
270 /* Now get the the user/group info. */
272 s = skip_whitespace(e);
274 /* Note: we require whitespace between the mode and the
275 * user/group info. */
276 if ((s == e) || !(e = strchr(s, '.'))) {
277 parse_error("<uid>.<gid>");
281 /* We can't use get_ug_id here since it would exit()
282 * if a uid or gid was not found. Oh well... */
283 sct->m_uid = bb_strtoul(s, NULL, 10);
285 struct passwd *pwd = getpwnam(s);
289 sct->m_uid = pwd->pw_uid;
292 sct->m_gid = bb_strtoul(e, NULL, 10);
297 parse_error("group");
299 sct->m_gid = grp->gr_gid;
305 /* Unknown sections are ignored. */
307 /* Encountering configuration lines prior to seeing a
308 * section header is treated as an error. This is how
309 * the old code worked, but it may not be desirable.
310 * We may want to simply ignore such lines in case they
311 * are used in some future version of busybox. */
313 parse_error("keyword outside section");
319 fprintf(stderr, "Parse error in %s, line %d: %s\n",
320 config_file, lc, errmsg);
323 /* Release any allocated memory before returning. */
325 sct = sct_head->m_next;
331 static inline void parse_config_file(void)
333 USE_FEATURE_SUID(ruid = getuid();)
335 #endif /* FEATURE_SUID_CONFIG */
338 #if ENABLE_FEATURE_SUID
339 static void check_suid(const struct bb_applet *applet)
341 gid_t rgid; /* real gid */
343 if (ruid == 0) /* set by parse_config_file() */
344 return; /* run by root - no need to check more */
347 #if ENABLE_FEATURE_SUID_CONFIG
348 if (suid_cfg_readable) {
350 struct BB_suid_config *sct;
353 for (sct = suid_config; sct; sct = sct->m_next) {
354 if (sct->m_applet == applet)
357 /* default: drop all privileges */
363 if (sct->m_uid == ruid)
366 else if ((sct->m_gid == rgid) || ingroup(ruid, sct->m_gid))
367 /* same group / in group */
370 if (!(m & S_IXOTH)) /* is x bit not set ? */
371 bb_error_msg_and_die("you have no permission to run this applet!");
373 /* _both_ sgid and group_exec have to be set for setegid */
374 if ((sct->m_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
376 /* else (no setegid) we will set egid = rgid */
378 /* We set effective AND saved ids. If saved-id is not set
379 * like we do below, seteiud(0) can still later succeed! */
380 if (setresgid(-1, rgid, rgid))
381 bb_perror_msg_and_die("setresgid");
383 /* do we have to set effective uid? */
385 if (sct->m_mode & S_ISUID)
387 /* else (no seteuid) we will set euid = ruid */
389 if (setresuid(-1, uid, uid))
390 bb_perror_msg_and_die("setresuid");
393 #if !ENABLE_FEATURE_SUID_CONFIG_QUIET
395 static bool onetime = 0;
399 fprintf(stderr, "Using fallback suid method\n");
405 if (applet->need_suid == _BB_SUID_ALWAYS) {
406 /* Real uid is not 0. If euid isn't 0 too, suid bit
407 * is most probably not set on our executable */
409 bb_error_msg_and_die("applet requires root privileges!");
410 } else if (applet->need_suid == _BB_SUID_NEVER) {
411 xsetgid(rgid); /* drop all privileges */
416 #define check_suid(x) ((void)0)
417 #endif /* FEATURE_SUID */
420 #if ENABLE_FEATURE_COMPRESS_USAGE
422 #include "usage_compressed.h"
423 #include "unarchive.h"
425 static const char *unpack_usage_messages(void)
431 i = start_bunzip(&bd,
433 /* inbuf: */ packed_usage,
434 /* len: */ sizeof(packed_usage));
435 /* read_bunzip can longjmp to start_bunzip, and ultimately
436 * end up here with i != 0 on read data errors! Not trivial */
438 /* Cannot use xmalloc: will leak bd in NOFORK case! */
439 outbuf = malloc_or_warn(SIZEOF_usage_messages);
441 read_bunzip(bd, outbuf, SIZEOF_usage_messages);
446 #define dealloc_usage_messages(s) free(s)
450 #define unpack_usage_messages() usage_messages
451 #define dealloc_usage_messages(s) ((void)(s))
453 #endif /* FEATURE_COMPRESS_USAGE */
456 void bb_show_usage(void)
458 if (ENABLE_SHOW_USAGE) {
459 const char *format_string;
461 const char *usage_string = p = unpack_usage_messages();
464 i = current_applet - applets;
466 while (*p++) continue;
470 format_string = "%s\n\nUsage: %s %s\n\n";
472 format_string = "%s\n\nNo help available.\n\n";
473 fprintf(stderr, format_string, bb_msg_full_version,
475 dealloc_usage_messages((char*)usage_string);
481 static int applet_name_compare(const void *name, const void *vapplet)
483 const struct bb_applet *applet = vapplet;
485 return strcmp(name, applet->name);
488 const struct bb_applet *find_applet_by_name(const char *name)
490 /* Do a binary search to find the applet entry given the name. */
491 return bsearch(name, applets, NUM_APPLETS, sizeof(applets[0]),
492 applet_name_compare);
496 #if ENABLE_FEATURE_INSTALLER
497 /* create (sym)links for each applet */
498 static void install_links(const char *busybox, int use_symbolic_links)
501 * this should be consistent w/ the enum,
502 * busybox.h::bb_install_loc_t, or else... */
503 static const char usr_bin [] = "/usr/bin";
504 static const char usr_sbin[] = "/usr/sbin";
505 static const char *const install_dir[] = {
506 &usr_bin [8], /* "", equivalent to "/" for concat_path_file() */
507 &usr_bin [4], /* "/bin" */
508 &usr_sbin[4], /* "/sbin" */
513 int (*lf)(const char *, const char *) = link;
518 if (use_symbolic_links)
521 for (i = 0; applets[i].name != NULL; i++) {
522 fpc = concat_path_file(
523 install_dir[applets[i].install_loc],
525 rc = lf(busybox, fpc);
526 if (rc != 0 && errno != EEXIST) {
527 bb_perror_msg("%s", fpc);
533 #define install_links(x,y) ((void)0)
534 #endif /* FEATURE_INSTALLER */
537 /* If we were called as "busybox..." */
538 static int busybox_main(char **argv)
541 /* Called without arguments */
542 const struct bb_applet *a;
543 int col, output_width;
546 if (ENABLE_FEATURE_AUTOWIDTH) {
547 /* Obtain the terminal width. */
548 get_terminal_width_height(0, &output_width, NULL);
550 /* leading tab and room to wrap */
551 output_width -= sizeof("start-stop-daemon, ") + 8;
554 "Copyright (C) 1998-2006 Erik Andersen, Rob Landley, and others.\n"
555 "Licensed under GPLv2. See source distribution for full notice.\n"
557 "Usage: busybox [function] [arguments]...\n"
558 " or: [function] [arguments]...\n"
560 "\tBusyBox is a multi-call binary that combines many common Unix\n"
561 "\tutilities into a single executable. Most people will create a\n"
562 "\tlink to busybox for each function they wish to use and BusyBox\n"
563 "\twill act like whatever it was invoked as!\n"
564 "\nCurrently defined functions:\n", bb_msg_full_version);
568 if (col > output_width) {
572 col += printf("%s%s", (col ? ", " : "\t"), a->name);
579 if (ENABLE_FEATURE_INSTALLER && strcmp(argv[1], "--install") == 0) {
580 int use_symbolic_links = 0;
583 /* to use symlinks, or not to use symlinks... */
585 if (strcmp(argv[2], "-s") == 0)
586 use_symbolic_links = 1;
589 busybox = xmalloc_readlink_or_warn("/proc/self/exe");
592 install_links(busybox, use_symbolic_links);
593 if (ENABLE_FEATURE_CLEAN_UP)
598 if (strcmp(argv[1], "--help") == 0) {
599 /* "busybox --help [<applet>]" */
602 /* convert to "<applet> --help" */
606 /* "busybox <applet> arg1 arg2 ..." */
609 /* we want "<argv[0]>: applet not found", not "busybox: ..." */
610 applet_name = argv[0];
611 run_applet_and_exit(argv[0], argv);
612 bb_error_msg_and_die("applet not found");
615 void run_current_applet_and_exit(char **argv)
622 /* Reinit some shared global data */
624 xfunc_error_retval = EXIT_FAILURE;
626 applet_name = current_applet->name;
627 if (argc == 2 && !strcmp(argv[1], "--help"))
629 if (ENABLE_FEATURE_SUID)
630 check_suid(current_applet);
631 exit(current_applet->main(argc, argv));
634 void run_applet_and_exit(const char *name, char **argv)
636 current_applet = find_applet_by_name(name);
638 run_current_applet_and_exit(argv);
639 if (!strncmp(name, "busybox", 7))
640 exit(busybox_main(argv));
644 int main(int argc, char **argv)
649 /* NOMMU re-exec trick sets high-order bit in first byte of name */
650 if (argv[0][0] & 0x80) {
655 applet_name = argv[0];
656 if (applet_name[0] == '-')
658 s = strrchr(applet_name, '/');
662 parse_config_file(); /* ...maybe, if FEATURE_SUID_CONFIG */
664 /* Set locale for everybody except 'init' */
665 if (ENABLE_LOCALE_SUPPORT && getpid() != 1)
666 setlocale(LC_ALL, "");
668 run_applet_and_exit(applet_name, argv);
669 bb_error_msg_and_die("applet not found");