blob: fc3e962d5fb17f1def11f8a252e28b6585428cda [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
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020010 * Permission has been granted to redistribute this code under GPL.
Denis Vlasenko724d1962007-10-10 14:41:07 +000011 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020012 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
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 */
Denys Vlasenkoc1947f12009-10-23 01:30:26 +020029#include "busybox.h"
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +000030#include <assert.h>
Denys Vlasenko8da415e2010-12-05 01:30:14 +010031#if !(defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) \
32 || defined(__APPLE__) \
33 )
34# include <malloc.h> /* for mallopt */
35#endif
Denys Vlasenko3d0e7792010-03-29 11:02:00 +020036/* Try to pull in PAGE_SIZE */
37#ifdef __linux__
38# include <sys/user.h>
39#endif
40#ifdef __GNU__ /* Hurd */
41# include <mach/vm_param.h>
42#endif
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +000043
44
45/* Declare <applet>_main() */
46#define PROTOTYPES
47#include "applets.h"
48#undef PROTOTYPES
49
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +000050
Denis Vlasenko32b2a9f2008-02-22 22:43:22 +000051/* Include generated applet names, pointers to <applet>_main, etc */
Denis Vlasenko1aa7e472007-11-28 06:49:03 +000052#include "applet_tables.h"
Denis Vlasenko468aea22008-04-01 14:47:57 +000053/* ...and if applet_tables generator says we have only one applet... */
54#ifdef SINGLE_APPLET_MAIN
Denys Vlasenko0e5ba082010-06-05 23:11:07 +020055# undef ENABLE_FEATURE_INDIVIDUAL
56# define ENABLE_FEATURE_INDIVIDUAL 1
57# undef IF_FEATURE_INDIVIDUAL
58# define IF_FEATURE_INDIVIDUAL(...) __VA_ARGS__
Denis Vlasenko468aea22008-04-01 14:47:57 +000059#endif
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +000060
61
Denys Vlasenko0e5ba082010-06-05 23:11:07 +020062#include "usage_compressed.h"
63
64#if ENABLE_SHOW_USAGE && !ENABLE_FEATURE_COMPRESS_USAGE
65static const char usage_messages[] ALIGN1 = UNPACKED_USAGE;
66#else
67# define usage_messages 0
Denys Vlasenko1fcbff22010-06-26 02:40:08 +020068#endif
Denys Vlasenko0e5ba082010-06-05 23:11:07 +020069
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +000070#if ENABLE_FEATURE_COMPRESS_USAGE
71
Denys Vlasenko5c296de2010-07-03 14:28:35 +020072static const char packed_usage[] ALIGN1 = { PACKED_USAGE };
Denys Vlasenko833d4e72010-11-03 02:38:31 +010073# include "archive.h"
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +000074static const char *unpack_usage_messages(void)
75{
76 char *outbuf = NULL;
77 bunzip_data *bd;
78 int i;
79
80 i = start_bunzip(&bd,
81 /* src_fd: */ -1,
Denys Vlasenkocaddfc82010-10-28 23:08:53 +020082 /* inbuf: */ packed_usage,
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +000083 /* len: */ sizeof(packed_usage));
84 /* read_bunzip can longjmp to start_bunzip, and ultimately
85 * end up here with i != 0 on read data errors! Not trivial */
86 if (!i) {
87 /* Cannot use xmalloc: will leak bd in NOFORK case! */
Denys Vlasenko0e5ba082010-06-05 23:11:07 +020088 outbuf = malloc_or_warn(sizeof(UNPACKED_USAGE));
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +000089 if (outbuf)
Denys Vlasenko0e5ba082010-06-05 23:11:07 +020090 read_bunzip(bd, outbuf, sizeof(UNPACKED_USAGE));
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +000091 }
92 dealloc_bunzip(bd);
93 return outbuf;
94}
Denys Vlasenko0e5ba082010-06-05 23:11:07 +020095# define dealloc_usage_messages(s) free(s)
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +000096
97#else
98
Denys Vlasenko0e5ba082010-06-05 23:11:07 +020099# define unpack_usage_messages() usage_messages
100# define dealloc_usage_messages(s) ((void)(s))
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000101
102#endif /* FEATURE_COMPRESS_USAGE */
103
104
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000105void FAST_FUNC bb_show_usage(void)
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000106{
107 if (ENABLE_SHOW_USAGE) {
Denis Vlasenko468aea22008-04-01 14:47:57 +0000108#ifdef SINGLE_APPLET_STR
109 /* Imagine that this applet is "true". Dont suck in printf! */
Lauri Kasanen2b662c52010-11-13 23:16:05 +0100110 const char *usage_string = unpack_usage_messages();
Denis Vlasenko468aea22008-04-01 14:47:57 +0000111
Lauri Kasanen2b662c52010-11-13 23:16:05 +0100112 if (*usage_string == '\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" ");
Lauri Kasanen2b662c52010-11-13 23:16:05 +0100116 full_write2_str(usage_string);
Denis Vlasenko79cedcb2008-04-08 21:13:28 +0000117 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 */
Denys Vlasenko9297dbc2010-07-05 21:37:12 +0200201 if (argv[1]
202 && !argv[2]
203 && strcmp(argv[1], "--help") == 0
204 && strncmp(applet, "busybox", 7) != 0
205 ) {
Denis Vlasenkobd28f6b2008-07-19 08:15:13 +0000206 /* Special case. POSIX says "test --help"
207 * should be no different from e.g. "test --foo". */
208 if (!ENABLE_TEST || strcmp(applet_name, "test") != 0)
209 bb_show_usage();
210 }
Denis Vlasenko82d38da2007-10-10 14:38:47 +0000211#endif
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000212}
Denis Vlasenko724d1962007-10-10 14:41:07 +0000213
214/* The code below can well be in applets/applets.c, as it is used only
215 * for busybox binary, not "individual" binaries.
216 * However, keeping it here and linking it into libbusybox.so
217 * (together with remaining tiny applets/applets.o)
218 * makes it possible to avoid --whole-archive at link time.
219 * This makes (shared busybox) + libbusybox smaller.
220 * (--gc-sections would be even better....)
221 */
222
223const char *applet_name;
224#if !BB_MMU
225bool re_execed;
226#endif
227
Denis Vlasenko468aea22008-04-01 14:47:57 +0000228
Denis Vlasenko10f6fb12008-04-29 00:10:27 +0000229/* If not built as a single-applet executable... */
230#if !defined(SINGLE_APPLET_MAIN)
Denis Vlasenko468aea22008-04-01 14:47:57 +0000231
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000232IF_FEATURE_SUID(static uid_t ruid;) /* real uid */
Denis Vlasenko724d1962007-10-10 14:41:07 +0000233
Denys Vlasenko8f0af3b2010-11-29 02:55:35 +0100234# if ENABLE_FEATURE_SUID_CONFIG
Denis Vlasenko724d1962007-10-10 14:41:07 +0000235
236/* applets[] is const, so we have to define this "override" structure */
237static struct BB_suid_config {
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000238 int m_applet;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000239 uid_t m_uid;
240 gid_t m_gid;
241 mode_t m_mode;
242 struct BB_suid_config *m_next;
243} *suid_config;
244
245static bool suid_cfg_readable;
246
247/* check if u is member of group g */
248static int ingroup(uid_t u, gid_t g)
249{
250 struct group *grp = getgrgid(g);
251
252 if (grp) {
253 char **mem;
254
255 for (mem = grp->gr_mem; *mem; mem++) {
256 struct passwd *pwd = getpwnam(*mem);
257
258 if (pwd && (pwd->pw_uid == u))
259 return 1;
260 }
261 }
262 return 0;
263}
264
265/* This should probably be a libbb routine. In that case,
266 * I'd probably rename it to something like bb_trimmed_slice.
267 */
268static char *get_trimmed_slice(char *s, char *e)
269{
270 /* First, consider the value at e to be nul and back up until we
271 * reach a non-space char. Set the char after that (possibly at
272 * the original e) to nul. */
273 while (e-- > s) {
274 if (!isspace(*e)) {
275 break;
276 }
277 }
278 e[1] = '\0';
279
280 /* Next, advance past all leading space and return a ptr to the
281 * first non-space char; possibly the terminating nul. */
282 return skip_whitespace(s);
283}
284
285/* Don't depend on the tools to combine strings. */
286static const char config_file[] ALIGN1 = "/etc/busybox.conf";
287
288/* We don't supply a value for the nul, so an index adjustment is
289 * necessary below. Also, we use unsigned short here to save some
290 * space even though these are really mode_t values. */
291static const unsigned short mode_mask[] ALIGN2 = {
292 /* SST sst xxx --- */
293 S_ISUID, S_ISUID|S_IXUSR, S_IXUSR, 0, /* user */
294 S_ISGID, S_ISGID|S_IXGRP, S_IXGRP, 0, /* group */
295 0, S_IXOTH, S_IXOTH, 0 /* other */
296};
297
298#define parse_error(x) do { errmsg = x; goto pe_label; } while (0)
299
300static void parse_config_file(void)
301{
302 struct BB_suid_config *sct_head;
303 struct BB_suid_config *sct;
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000304 int applet_no;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000305 FILE *f;
306 const char *errmsg;
307 char *s;
308 char *e;
309 int i;
310 unsigned lc;
311 smallint section;
312 char buffer[256];
313 struct stat st;
314
315 assert(!suid_config); /* Should be set to NULL by bss init. */
316
317 ruid = getuid();
318 if (ruid == 0) /* run by root - don't need to even read config file */
319 return;
320
321 if ((stat(config_file, &st) != 0) /* No config file? */
322 || !S_ISREG(st.st_mode) /* Not a regular file? */
323 || (st.st_uid != 0) /* Not owned by root? */
324 || (st.st_mode & (S_IWGRP | S_IWOTH)) /* Writable by non-root? */
Denis Vlasenko5415c852008-07-21 23:05:26 +0000325 || !(f = fopen_for_read(config_file)) /* Cannot open? */
Denis Vlasenko724d1962007-10-10 14:41:07 +0000326 ) {
327 return;
328 }
329
330 suid_cfg_readable = 1;
331 sct_head = NULL;
332 section = lc = 0;
333
334 while (1) {
335 s = buffer;
336
337 if (!fgets(s, sizeof(buffer), f)) { /* Are we done? */
Denis Vlasenkod02db892008-03-17 09:05:21 +0000338// why?
Denis Vlasenko724d1962007-10-10 14:41:07 +0000339 if (ferror(f)) { /* Make sure it wasn't a read error. */
340 parse_error("reading");
341 }
342 fclose(f);
343 suid_config = sct_head; /* Success, so set the pointer. */
344 return;
345 }
346
347 lc++; /* Got a (partial) line. */
348
349 /* If a line is too long for our buffer, we consider it an error.
350 * The following test does mistreat one corner case though.
351 * If the final line of the file does not end with a newline and
352 * yet exactly fills the buffer, it will be treated as too long
353 * even though there isn't really a problem. But it isn't really
354 * worth adding code to deal with such an unlikely situation, and
355 * we do err on the side of caution. Besides, the line would be
356 * too long if it did end with a newline. */
357 if (!strchr(s, '\n') && !feof(f)) {
358 parse_error("line too long");
359 }
360
361 /* Trim leading and trailing whitespace, ignoring comments, and
362 * check if the resulting string is empty. */
363 s = get_trimmed_slice(s, strchrnul(s, '#'));
364 if (!*s) {
365 continue;
366 }
367
368 /* Check for a section header. */
369
370 if (*s == '[') {
371 /* Unlike the old code, we ignore leading and trailing
372 * whitespace for the section name. We also require that
373 * there are no stray characters after the closing bracket. */
374 e = strchr(s, ']');
375 if (!e /* Missing right bracket? */
376 || e[1] /* Trailing characters? */
377 || !*(s = get_trimmed_slice(s+1, e)) /* Missing name? */
378 ) {
379 parse_error("section header");
380 }
381 /* Right now we only have one section so just check it.
382 * If more sections are added in the future, please don't
383 * resort to cascading ifs with multiple strcasecmp calls.
384 * That kind of bloated code is all too common. A loop
385 * and a string table would be a better choice unless the
386 * number of sections is very small. */
387 if (strcasecmp(s, "SUID") == 0) {
388 section = 1;
389 continue;
390 }
391 section = -1; /* Unknown section so set to skip. */
392 continue;
393 }
394
395 /* Process sections. */
396
397 if (section == 1) { /* SUID */
398 /* Since we trimmed leading and trailing space above, we're
399 * now looking for strings of the form
400 * <key>[::space::]*=[::space::]*<value>
401 * where both key and value could contain inner whitespace. */
402
403 /* First get the key (an applet name in our case). */
404 e = strchr(s, '=');
405 if (e) {
406 s = get_trimmed_slice(s, e);
407 }
408 if (!e || !*s) { /* Missing '=' or empty key. */
409 parse_error("keyword");
410 }
411
412 /* Ok, we have an applet name. Process the rhs if this
413 * applet is currently built in and ignore it otherwise.
414 * Note: this can hide config file bugs which only pop
415 * up when the busybox configuration is changed. */
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000416 applet_no = find_applet_by_name(s);
417 if (applet_no >= 0) {
Denis Vlasenko724d1962007-10-10 14:41:07 +0000418 /* Note: We currently don't check for duplicates!
419 * The last config line for each applet will be the
420 * one used since we insert at the head of the list.
421 * I suppose this could be considered a feature. */
422 sct = xmalloc(sizeof(struct BB_suid_config));
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000423 sct->m_applet = applet_no;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000424 sct->m_mode = 0;
425 sct->m_next = sct_head;
426 sct_head = sct;
427
428 /* Get the specified mode. */
429
430 e = skip_whitespace(e+1);
431
432 for (i = 0; i < 3; i++) {
433 /* There are 4 chars + 1 nul for each of user/group/other. */
434 static const char mode_chars[] ALIGN1 = "Ssx-\0" "Ssx-\0" "Ttx-";
435
436 const char *q;
437 q = strchrnul(mode_chars + 5*i, *e++);
438 if (!*q) {
439 parse_error("mode");
440 }
441 /* Adjust by -i to account for nul. */
442 sct->m_mode |= mode_mask[(q - mode_chars) - i];
443 }
444
445 /* Now get the the user/group info. */
446
447 s = skip_whitespace(e);
448
449 /* Note: we require whitespace between the mode and the
450 * user/group info. */
451 if ((s == e) || !(e = strchr(s, '.'))) {
452 parse_error("<uid>.<gid>");
453 }
454 *e++ = '\0';
455
456 /* We can't use get_ug_id here since it would exit()
457 * if a uid or gid was not found. Oh well... */
458 sct->m_uid = bb_strtoul(s, NULL, 10);
459 if (errno) {
460 struct passwd *pwd = getpwnam(s);
461 if (!pwd) {
462 parse_error("user");
463 }
464 sct->m_uid = pwd->pw_uid;
465 }
466
467 sct->m_gid = bb_strtoul(e, NULL, 10);
468 if (errno) {
469 struct group *grp;
470 grp = getgrnam(e);
471 if (!grp) {
472 parse_error("group");
473 }
474 sct->m_gid = grp->gr_gid;
475 }
476 }
477 continue;
478 }
479
480 /* Unknown sections are ignored. */
481
482 /* Encountering configuration lines prior to seeing a
483 * section header is treated as an error. This is how
484 * the old code worked, but it may not be desirable.
485 * We may want to simply ignore such lines in case they
486 * are used in some future version of busybox. */
487 if (!section) {
488 parse_error("keyword outside section");
489 }
490
491 } /* while (1) */
492
493 pe_label:
494 fprintf(stderr, "Parse error in %s, line %d: %s\n",
495 config_file, lc, errmsg);
496
497 fclose(f);
498 /* Release any allocated memory before returning. */
499 while (sct_head) {
500 sct = sct_head->m_next;
501 free(sct_head);
502 sct_head = sct;
503 }
504}
Denys Vlasenko8f0af3b2010-11-29 02:55:35 +0100505# else
Denis Vlasenko724d1962007-10-10 14:41:07 +0000506static inline void parse_config_file(void)
507{
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000508 IF_FEATURE_SUID(ruid = getuid();)
Denis Vlasenko724d1962007-10-10 14:41:07 +0000509}
Denys Vlasenko8f0af3b2010-11-29 02:55:35 +0100510# endif /* FEATURE_SUID_CONFIG */
Denis Vlasenko724d1962007-10-10 14:41:07 +0000511
512
Denys Vlasenko8f0af3b2010-11-29 02:55:35 +0100513# if ENABLE_FEATURE_SUID
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000514static void check_suid(int applet_no)
Denis Vlasenko724d1962007-10-10 14:41:07 +0000515{
516 gid_t rgid; /* real gid */
517
518 if (ruid == 0) /* set by parse_config_file() */
519 return; /* run by root - no need to check more */
520 rgid = getgid();
521
Denys Vlasenko8f0af3b2010-11-29 02:55:35 +0100522# if ENABLE_FEATURE_SUID_CONFIG
Denis Vlasenko724d1962007-10-10 14:41:07 +0000523 if (suid_cfg_readable) {
524 uid_t uid;
525 struct BB_suid_config *sct;
526 mode_t m;
527
528 for (sct = suid_config; sct; sct = sct->m_next) {
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000529 if (sct->m_applet == applet_no)
Denis Vlasenko724d1962007-10-10 14:41:07 +0000530 goto found;
531 }
Denis Vlasenko15ca51e2007-10-29 19:25:45 +0000532 goto check_need_suid;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000533 found:
534 m = sct->m_mode;
535 if (sct->m_uid == ruid)
536 /* same uid */
537 m >>= 6;
538 else if ((sct->m_gid == rgid) || ingroup(ruid, sct->m_gid))
539 /* same group / in group */
540 m >>= 3;
541
542 if (!(m & S_IXOTH)) /* is x bit not set ? */
543 bb_error_msg_and_die("you have no permission to run this applet!");
544
545 /* _both_ sgid and group_exec have to be set for setegid */
546 if ((sct->m_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
547 rgid = sct->m_gid;
548 /* else (no setegid) we will set egid = rgid */
549
550 /* We set effective AND saved ids. If saved-id is not set
551 * like we do below, seteiud(0) can still later succeed! */
552 if (setresgid(-1, rgid, rgid))
553 bb_perror_msg_and_die("setresgid");
554
555 /* do we have to set effective uid? */
556 uid = ruid;
557 if (sct->m_mode & S_ISUID)
558 uid = sct->m_uid;
559 /* else (no seteuid) we will set euid = ruid */
560
561 if (setresuid(-1, uid, uid))
562 bb_perror_msg_and_die("setresuid");
563 return;
564 }
Denys Vlasenko8f0af3b2010-11-29 02:55:35 +0100565# if !ENABLE_FEATURE_SUID_CONFIG_QUIET
Denis Vlasenko724d1962007-10-10 14:41:07 +0000566 {
567 static bool onetime = 0;
568
569 if (!onetime) {
570 onetime = 1;
571 fprintf(stderr, "Using fallback suid method\n");
572 }
573 }
Denys Vlasenko8f0af3b2010-11-29 02:55:35 +0100574# endif
Denis Vlasenko15ca51e2007-10-29 19:25:45 +0000575 check_need_suid:
Denys Vlasenko8f0af3b2010-11-29 02:55:35 +0100576# endif
Denys Vlasenkob9f2d9f2011-01-18 13:58:01 +0100577 if (APPLET_SUID(applet_no) == BB_SUID_REQUIRE) {
Denis Vlasenko724d1962007-10-10 14:41:07 +0000578 /* Real uid is not 0. If euid isn't 0 too, suid bit
579 * is most probably not set on our executable */
580 if (geteuid())
Denis Vlasenko15ca51e2007-10-29 19:25:45 +0000581 bb_error_msg_and_die("must be suid to work properly");
Denys Vlasenkob9f2d9f2011-01-18 13:58:01 +0100582 } else if (APPLET_SUID(applet_no) == BB_SUID_DROP) {
Denis Vlasenko724d1962007-10-10 14:41:07 +0000583 xsetgid(rgid); /* drop all privileges */
584 xsetuid(ruid);
585 }
586}
Denys Vlasenko8f0af3b2010-11-29 02:55:35 +0100587# else
588# define check_suid(x) ((void)0)
589# endif /* FEATURE_SUID */
Denis Vlasenko724d1962007-10-10 14:41:07 +0000590
591
Denys Vlasenko8f0af3b2010-11-29 02:55:35 +0100592# if ENABLE_FEATURE_INSTALLER
Denys Vlasenko5a7c7202010-04-20 21:02:57 -0400593static const char usr_bin [] ALIGN1 = "/usr/bin/";
594static const char usr_sbin[] ALIGN1 = "/usr/sbin/";
595static const char *const install_dir[] = {
596 &usr_bin [8], /* "/" */
597 &usr_bin [4], /* "/bin/" */
Denys Vlasenkod4d289a2010-10-12 04:18:05 +0200598 &usr_sbin[4] /* "/sbin/" */
Denys Vlasenko8f0af3b2010-11-29 02:55:35 +0100599# if !ENABLE_INSTALL_NO_USR
Denys Vlasenkod4d289a2010-10-12 04:18:05 +0200600 ,usr_bin
601 ,usr_sbin
Denys Vlasenko8f0af3b2010-11-29 02:55:35 +0100602# endif
Denys Vlasenko5a7c7202010-04-20 21:02:57 -0400603};
604
Denis Vlasenko724d1962007-10-10 14:41:07 +0000605/* create (sym)links for each applet */
Denys Vlasenko3b405432009-07-15 00:35:34 +0200606static void install_links(const char *busybox, int use_symbolic_links,
607 char *custom_install_dir)
Denis Vlasenko724d1962007-10-10 14:41:07 +0000608{
609 /* directory table
610 * this should be consistent w/ the enum,
611 * busybox.h::bb_install_loc_t, or else... */
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000612 int (*lf)(const char *, const char *);
Denis Vlasenko724d1962007-10-10 14:41:07 +0000613 char *fpc;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000614 unsigned i;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000615 int rc;
616
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000617 lf = link;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000618 if (use_symbolic_links)
619 lf = symlink;
620
Denis Vlasenko745cd172007-11-29 03:31:20 +0000621 for (i = 0; i < ARRAY_SIZE(applet_main); i++) {
Denis Vlasenko724d1962007-10-10 14:41:07 +0000622 fpc = concat_path_file(
Denys Vlasenko3b405432009-07-15 00:35:34 +0200623 custom_install_dir ? custom_install_dir : install_dir[APPLET_INSTALL_LOC(i)],
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000624 APPLET_NAME(i));
Denis Vlasenko745cd172007-11-29 03:31:20 +0000625 // debug: bb_error_msg("%slinking %s to busybox",
626 // use_symbolic_links ? "sym" : "", fpc);
Denis Vlasenko724d1962007-10-10 14:41:07 +0000627 rc = lf(busybox, fpc);
628 if (rc != 0 && errno != EEXIST) {
629 bb_simple_perror_msg(fpc);
630 }
631 free(fpc);
632 }
633}
Denys Vlasenko8f0af3b2010-11-29 02:55:35 +0100634# else
635# define install_links(x,y,z) ((void)0)
636# endif
Denis Vlasenko724d1962007-10-10 14:41:07 +0000637
638/* If we were called as "busybox..." */
639static int busybox_main(char **argv)
640{
641 if (!argv[1]) {
642 /* Called without arguments */
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000643 const char *a;
Denys Vlasenko0149f022009-05-19 18:01:42 +0200644 int col;
645 unsigned output_width;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000646 help:
647 output_width = 80;
648 if (ENABLE_FEATURE_AUTOWIDTH) {
649 /* Obtain the terminal width */
650 get_terminal_width_height(0, &output_width, NULL);
651 }
Denis Vlasenko724d1962007-10-10 14:41:07 +0000652
Denis Vlasenko21278df2008-06-25 12:15:46 +0000653 dup2(1, 2);
Denys Vlasenko630dde12009-08-30 19:57:49 +0200654 full_write2_str(bb_banner); /* reuse const string */
655 full_write2_str(" multi-call binary.\n"); /* reuse */
656 full_write2_str(
Denys Vlasenko5a7c7202010-04-20 21:02:57 -0400657 "Copyright (C) 1998-2009 Erik Andersen, Rob Landley, Denys Vlasenko\n"
658 "and others. Licensed under GPLv2.\n"
659 "See source distribution for full notice.\n"
660 "\n"
661 "Usage: busybox [function] [arguments]...\n"
Denys Vlasenko819cacf2010-09-12 16:17:37 +0200662 " or: busybox --list[-full]\n"
Denys Vlasenko5a7c7202010-04-20 21:02:57 -0400663 " or: function [arguments]...\n"
664 "\n"
665 "\tBusyBox is a multi-call binary that combines many common Unix\n"
666 "\tutilities into a single executable. Most people will create a\n"
667 "\tlink to busybox for each function they wish to use and BusyBox\n"
668 "\twill act like whatever it was invoked as.\n"
669 "\n"
670 "Currently defined functions:\n"
671 );
Denis Vlasenko724d1962007-10-10 14:41:07 +0000672 col = 0;
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000673 a = applet_names;
Denys Vlasenko0149f022009-05-19 18:01:42 +0200674 /* prevent last comma to be in the very last pos */
675 output_width--;
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000676 while (*a) {
Denys Vlasenko0149f022009-05-19 18:01:42 +0200677 int len2 = strlen(a) + 2;
678 if (col >= (int)output_width - len2) {
Denis Vlasenko79cedcb2008-04-08 21:13:28 +0000679 full_write2_str(",\n");
Denis Vlasenko724d1962007-10-10 14:41:07 +0000680 col = 0;
681 }
Denys Vlasenko0149f022009-05-19 18:01:42 +0200682 if (col == 0) {
683 col = 6;
684 full_write2_str("\t");
685 } else {
686 full_write2_str(", ");
687 }
Denis Vlasenko79cedcb2008-04-08 21:13:28 +0000688 full_write2_str(a);
Denys Vlasenko0149f022009-05-19 18:01:42 +0200689 col += len2;
690 a += len2 - 1;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000691 }
Denis Vlasenko79cedcb2008-04-08 21:13:28 +0000692 full_write2_str("\n\n");
Denis Vlasenko724d1962007-10-10 14:41:07 +0000693 return 0;
694 }
695
Denys Vlasenko5a7c7202010-04-20 21:02:57 -0400696 if (strncmp(argv[1], "--list", 6) == 0) {
697 unsigned i = 0;
698 const char *a = applet_names;
699 dup2(1, 2);
700 while (*a) {
Denys Vlasenko8f0af3b2010-11-29 02:55:35 +0100701# if ENABLE_FEATURE_INSTALLER
Denys Vlasenko5a7c7202010-04-20 21:02:57 -0400702 if (argv[1][6]) /* --list-path? */
703 full_write2_str(install_dir[APPLET_INSTALL_LOC(i)] + 1);
Denys Vlasenko8f0af3b2010-11-29 02:55:35 +0100704# endif
Denys Vlasenko5a7c7202010-04-20 21:02:57 -0400705 full_write2_str(a);
706 full_write2_str("\n");
707 i++;
708 a += strlen(a) + 1;
709 }
710 return 0;
711 }
712
Denis Vlasenko724d1962007-10-10 14:41:07 +0000713 if (ENABLE_FEATURE_INSTALLER && strcmp(argv[1], "--install") == 0) {
Denys Vlasenko3b405432009-07-15 00:35:34 +0200714 int use_symbolic_links;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000715 const char *busybox;
716 busybox = xmalloc_readlink(bb_busybox_exec_path);
717 if (!busybox)
718 busybox = bb_busybox_exec_path;
Denys Vlasenko0ad438b2009-07-15 02:14:15 +0200719 /* busybox --install [-s] [DIR]: */
720 /* -s: make symlinks */
721 /* DIR: directory to install links to */
Denys Vlasenko3b405432009-07-15 00:35:34 +0200722 use_symbolic_links = (argv[2] && strcmp(argv[2], "-s") == 0 && argv++);
723 install_links(busybox, use_symbolic_links, argv[2]);
Denis Vlasenko724d1962007-10-10 14:41:07 +0000724 return 0;
725 }
726
727 if (strcmp(argv[1], "--help") == 0) {
728 /* "busybox --help [<applet>]" */
729 if (!argv[2])
730 goto help;
731 /* convert to "<applet> --help" */
732 argv[0] = argv[2];
733 argv[2] = NULL;
734 } else {
735 /* "busybox <applet> arg1 arg2 ..." */
736 argv++;
737 }
738 /* We support "busybox /a/path/to/applet args..." too. Allows for
739 * "#!/bin/busybox"-style wrappers */
740 applet_name = bb_get_last_path_component_nostrip(argv[0]);
741 run_applet_and_exit(applet_name, argv);
Denis Vlasenko79cedcb2008-04-08 21:13:28 +0000742
743 /*bb_error_msg_and_die("applet not found"); - sucks in printf */
744 full_write2_str(applet_name);
745 full_write2_str(": applet not found\n");
746 xfunc_die();
Denis Vlasenko724d1962007-10-10 14:41:07 +0000747}
748
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000749void FAST_FUNC run_applet_no_and_exit(int applet_no, char **argv)
Denis Vlasenko724d1962007-10-10 14:41:07 +0000750{
751 int argc = 1;
752
753 while (argv[argc])
754 argc++;
755
756 /* Reinit some shared global data */
Denis Vlasenko724d1962007-10-10 14:41:07 +0000757 xfunc_error_retval = EXIT_FAILURE;
758
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000759 applet_name = APPLET_NAME(applet_no);
Denis Vlasenkobd28f6b2008-07-19 08:15:13 +0000760 if (argc == 2 && strcmp(argv[1], "--help") == 0) {
761 /* Special case. POSIX says "test --help"
762 * should be no different from e.g. "test --foo". */
763//TODO: just compare applet_no with APPLET_NO_test
764 if (!ENABLE_TEST || strcmp(applet_name, "test") != 0)
765 bb_show_usage();
766 }
Denis Vlasenko724d1962007-10-10 14:41:07 +0000767 if (ENABLE_FEATURE_SUID)
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000768 check_suid(applet_no);
Denis Vlasenko745cd172007-11-29 03:31:20 +0000769 exit(applet_main[applet_no](argc, argv));
Denis Vlasenko724d1962007-10-10 14:41:07 +0000770}
771
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000772void FAST_FUNC run_applet_and_exit(const char *name, char **argv)
Denis Vlasenko724d1962007-10-10 14:41:07 +0000773{
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000774 int applet = find_applet_by_name(name);
775 if (applet >= 0)
776 run_applet_no_and_exit(applet, argv);
Denys Vlasenko8f0af3b2010-11-29 02:55:35 +0100777 if (strncmp(name, "busybox", 7) == 0)
Denis Vlasenko724d1962007-10-10 14:41:07 +0000778 exit(busybox_main(argv));
779}
780
Denis Vlasenko10f6fb12008-04-29 00:10:27 +0000781#endif /* !defined(SINGLE_APPLET_MAIN) */
Denis Vlasenko468aea22008-04-01 14:47:57 +0000782
783
Denis Vlasenko724d1962007-10-10 14:41:07 +0000784
785#if ENABLE_BUILD_LIBBUSYBOX
Denis Vlasenko85c24712008-03-17 09:04:04 +0000786int lbb_main(char **argv)
Denis Vlasenko724d1962007-10-10 14:41:07 +0000787#else
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000788int main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko724d1962007-10-10 14:41:07 +0000789#endif
790{
Denys Vlasenko1f7c1672009-09-06 02:12:28 +0200791 /* Tweak malloc for reduced memory consumption */
792#ifndef PAGE_SIZE
793# define PAGE_SIZE (4*1024) /* guess */
794#endif
795#ifdef M_TRIM_THRESHOLD
796 /* M_TRIM_THRESHOLD is the maximum amount of freed top-most memory
797 * to keep before releasing to the OS
798 * Default is way too big: 256k
799 */
800 mallopt(M_TRIM_THRESHOLD, 2 * PAGE_SIZE);
801#endif
802#ifdef M_MMAP_THRESHOLD
803 /* M_MMAP_THRESHOLD is the request size threshold for using mmap()
804 * Default is too big: 256k
805 */
806 mallopt(M_MMAP_THRESHOLD, 8 * PAGE_SIZE - 256);
807#endif
808
Denis Vlasenko724d1962007-10-10 14:41:07 +0000809#if !BB_MMU
810 /* NOMMU re-exec trick sets high-order bit in first byte of name */
811 if (argv[0][0] & 0x80) {
812 re_execed = 1;
813 argv[0][0] &= 0x7f;
814 }
815#endif
Denys Vlasenko8f0af3b2010-11-29 02:55:35 +0100816
817#if defined(SINGLE_APPLET_MAIN)
818 /* Only one applet is selected in .config */
Denys Vlasenko0ab45da2010-11-29 03:21:55 +0100819 if (argv[1] && strncmp(argv[0], "busybox", 7) == 0) {
Denys Vlasenko8f0af3b2010-11-29 02:55:35 +0100820 /* "busybox <applet> <params>" should still work as expected */
821 argv++;
822 }
823 /* applet_names in this case is just "applet\0\0" */
824 lbb_prepare(applet_names IF_FEATURE_INDIVIDUAL(, argv));
825 return SINGLE_APPLET_MAIN(argc, argv);
826#else
827 lbb_prepare("busybox" IF_FEATURE_INDIVIDUAL(, argv));
828
Denis Vlasenko724d1962007-10-10 14:41:07 +0000829 applet_name = argv[0];
830 if (applet_name[0] == '-')
831 applet_name++;
832 applet_name = bb_basename(applet_name);
833
834 parse_config_file(); /* ...maybe, if FEATURE_SUID_CONFIG */
835
836 run_applet_and_exit(applet_name, argv);
Denis Vlasenko79cedcb2008-04-08 21:13:28 +0000837
838 /*bb_error_msg_and_die("applet not found"); - sucks in printf */
839 full_write2_str(applet_name);
840 full_write2_str(": applet not found\n");
841 xfunc_die();
Denis Vlasenko468aea22008-04-01 14:47:57 +0000842#endif
Denis Vlasenko724d1962007-10-10 14:41:07 +0000843}