blob: 48a8bb6f341e62a35e336265211b827a224e869c [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 }
119 dealloc_usage_messages((char*)usage_string);
120#else
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000121 const char *p;
122 const char *usage_string = p = unpack_usage_messages();
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000123 int ap = find_applet_by_name(applet_name);
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000124
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000125 if (ap < 0) /* never happens, paranoia */
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000126 xfunc_die();
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000127 while (ap) {
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000128 while (*p++) continue;
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000129 ap--;
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000130 }
Denis Vlasenko79cedcb2008-04-08 21:13:28 +0000131 full_write2_str(bb_banner);
132 full_write2_str(" multi-call binary\n");
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000133 if (*p == '\b')
Denis Vlasenko79cedcb2008-04-08 21:13:28 +0000134 full_write2_str("\nNo help available.\n\n");
135 else {
136 full_write2_str("\nUsage: ");
137 full_write2_str(applet_name);
138 full_write2_str(" ");
139 full_write2_str(p);
140 full_write2_str("\n\n");
141 }
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000142 dealloc_usage_messages((char*)usage_string);
Denis Vlasenko468aea22008-04-01 14:47:57 +0000143#endif
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000144 }
145 xfunc_die();
146}
147
Denis Vlasenko79cedcb2008-04-08 21:13:28 +0000148#if NUM_APPLETS > 8
Denis Vlasenko745cd172007-11-29 03:31:20 +0000149/* NB: any char pointer will work as well, not necessarily applet_names */
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000150static int applet_name_compare(const void *name, const void *v)
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000151{
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000152 int i = (const char *)v - applet_names;
153 return strcmp(name, APPLET_NAME(i));
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000154}
Denis Vlasenko79cedcb2008-04-08 21:13:28 +0000155#endif
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000156int FAST_FUNC find_applet_by_name(const char *name)
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000157{
Denis Vlasenko79cedcb2008-04-08 21:13:28 +0000158#if NUM_APPLETS > 8
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000159 /* Do a binary search to find the applet entry given the name. */
Denis Vlasenko745cd172007-11-29 03:31:20 +0000160 const char *p;
161 p = bsearch(name, applet_names, ARRAY_SIZE(applet_main), 1, applet_name_compare);
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000162 if (!p)
163 return -1;
164 return p - applet_names;
Denis Vlasenko79cedcb2008-04-08 21:13:28 +0000165#else
166 /* A version which does not pull in bsearch */
167 int i = 0;
168 const char *p = applet_names;
169 while (i < NUM_APPLETS) {
170 if (strcmp(name, p) == 0)
171 return i;
172 p += strlen(p) + 1;
173 i++;
174 }
175 return -1;
176#endif
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000177}
178
179
Denis Vlasenko68404f12008-03-17 09:00:54 +0000180void lbb_prepare(const char *applet
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000181 IF_FEATURE_INDIVIDUAL(, char **argv))
Denis Vlasenko468aea22008-04-01 14:47:57 +0000182 MAIN_EXTERNALLY_VISIBLE;
183void lbb_prepare(const char *applet
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000184 IF_FEATURE_INDIVIDUAL(, char **argv))
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000185{
186#ifdef __GLIBC__
187 (*(int **)&bb_errno) = __errno_location();
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000188 barrier();
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000189#endif
Denis Vlasenko15cb4a42007-10-11 10:06:26 +0000190 applet_name = applet;
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000191
192 /* Set locale for everybody except 'init' */
193 if (ENABLE_LOCALE_SUPPORT && getpid() != 1)
194 setlocale(LC_ALL, "");
195
Denis Vlasenko82d38da2007-10-10 14:38:47 +0000196#if ENABLE_FEATURE_INDIVIDUAL
197 /* Redundant for busybox (run_applet_and_exit covers that case)
198 * but needed for "individual applet" mode */
Denis Vlasenkobd28f6b2008-07-19 08:15:13 +0000199 if (argv[1] && !argv[2] && strcmp(argv[1], "--help") == 0) {
200 /* Special case. POSIX says "test --help"
201 * should be no different from e.g. "test --foo". */
202 if (!ENABLE_TEST || strcmp(applet_name, "test") != 0)
203 bb_show_usage();
204 }
Denis Vlasenko82d38da2007-10-10 14:38:47 +0000205#endif
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000206}
Denis Vlasenko724d1962007-10-10 14:41:07 +0000207
208/* The code below can well be in applets/applets.c, as it is used only
209 * for busybox binary, not "individual" binaries.
210 * However, keeping it here and linking it into libbusybox.so
211 * (together with remaining tiny applets/applets.o)
212 * makes it possible to avoid --whole-archive at link time.
213 * This makes (shared busybox) + libbusybox smaller.
214 * (--gc-sections would be even better....)
215 */
216
217const char *applet_name;
218#if !BB_MMU
219bool re_execed;
220#endif
221
Denis Vlasenko468aea22008-04-01 14:47:57 +0000222
Denis Vlasenko10f6fb12008-04-29 00:10:27 +0000223/* If not built as a single-applet executable... */
224#if !defined(SINGLE_APPLET_MAIN)
Denis Vlasenko468aea22008-04-01 14:47:57 +0000225
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000226IF_FEATURE_SUID(static uid_t ruid;) /* real uid */
Denis Vlasenko724d1962007-10-10 14:41:07 +0000227
228#if ENABLE_FEATURE_SUID_CONFIG
229
230/* applets[] is const, so we have to define this "override" structure */
231static struct BB_suid_config {
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000232 int m_applet;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000233 uid_t m_uid;
234 gid_t m_gid;
235 mode_t m_mode;
236 struct BB_suid_config *m_next;
237} *suid_config;
238
239static bool suid_cfg_readable;
240
241/* check if u is member of group g */
242static int ingroup(uid_t u, gid_t g)
243{
244 struct group *grp = getgrgid(g);
245
246 if (grp) {
247 char **mem;
248
249 for (mem = grp->gr_mem; *mem; mem++) {
250 struct passwd *pwd = getpwnam(*mem);
251
252 if (pwd && (pwd->pw_uid == u))
253 return 1;
254 }
255 }
256 return 0;
257}
258
259/* This should probably be a libbb routine. In that case,
260 * I'd probably rename it to something like bb_trimmed_slice.
261 */
262static char *get_trimmed_slice(char *s, char *e)
263{
264 /* First, consider the value at e to be nul and back up until we
265 * reach a non-space char. Set the char after that (possibly at
266 * the original e) to nul. */
267 while (e-- > s) {
268 if (!isspace(*e)) {
269 break;
270 }
271 }
272 e[1] = '\0';
273
274 /* Next, advance past all leading space and return a ptr to the
275 * first non-space char; possibly the terminating nul. */
276 return skip_whitespace(s);
277}
278
279/* Don't depend on the tools to combine strings. */
280static const char config_file[] ALIGN1 = "/etc/busybox.conf";
281
282/* We don't supply a value for the nul, so an index adjustment is
283 * necessary below. Also, we use unsigned short here to save some
284 * space even though these are really mode_t values. */
285static const unsigned short mode_mask[] ALIGN2 = {
286 /* SST sst xxx --- */
287 S_ISUID, S_ISUID|S_IXUSR, S_IXUSR, 0, /* user */
288 S_ISGID, S_ISGID|S_IXGRP, S_IXGRP, 0, /* group */
289 0, S_IXOTH, S_IXOTH, 0 /* other */
290};
291
292#define parse_error(x) do { errmsg = x; goto pe_label; } while (0)
293
294static void parse_config_file(void)
295{
296 struct BB_suid_config *sct_head;
297 struct BB_suid_config *sct;
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000298 int applet_no;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000299 FILE *f;
300 const char *errmsg;
301 char *s;
302 char *e;
303 int i;
304 unsigned lc;
305 smallint section;
306 char buffer[256];
307 struct stat st;
308
309 assert(!suid_config); /* Should be set to NULL by bss init. */
310
311 ruid = getuid();
312 if (ruid == 0) /* run by root - don't need to even read config file */
313 return;
314
315 if ((stat(config_file, &st) != 0) /* No config file? */
316 || !S_ISREG(st.st_mode) /* Not a regular file? */
317 || (st.st_uid != 0) /* Not owned by root? */
318 || (st.st_mode & (S_IWGRP | S_IWOTH)) /* Writable by non-root? */
Denis Vlasenko5415c852008-07-21 23:05:26 +0000319 || !(f = fopen_for_read(config_file)) /* Cannot open? */
Denis Vlasenko724d1962007-10-10 14:41:07 +0000320 ) {
321 return;
322 }
323
324 suid_cfg_readable = 1;
325 sct_head = NULL;
326 section = lc = 0;
327
328 while (1) {
329 s = buffer;
330
331 if (!fgets(s, sizeof(buffer), f)) { /* Are we done? */
Denis Vlasenkod02db892008-03-17 09:05:21 +0000332// why?
Denis Vlasenko724d1962007-10-10 14:41:07 +0000333 if (ferror(f)) { /* Make sure it wasn't a read error. */
334 parse_error("reading");
335 }
336 fclose(f);
337 suid_config = sct_head; /* Success, so set the pointer. */
338 return;
339 }
340
341 lc++; /* Got a (partial) line. */
342
343 /* If a line is too long for our buffer, we consider it an error.
344 * The following test does mistreat one corner case though.
345 * If the final line of the file does not end with a newline and
346 * yet exactly fills the buffer, it will be treated as too long
347 * even though there isn't really a problem. But it isn't really
348 * worth adding code to deal with such an unlikely situation, and
349 * we do err on the side of caution. Besides, the line would be
350 * too long if it did end with a newline. */
351 if (!strchr(s, '\n') && !feof(f)) {
352 parse_error("line too long");
353 }
354
355 /* Trim leading and trailing whitespace, ignoring comments, and
356 * check if the resulting string is empty. */
357 s = get_trimmed_slice(s, strchrnul(s, '#'));
358 if (!*s) {
359 continue;
360 }
361
362 /* Check for a section header. */
363
364 if (*s == '[') {
365 /* Unlike the old code, we ignore leading and trailing
366 * whitespace for the section name. We also require that
367 * there are no stray characters after the closing bracket. */
368 e = strchr(s, ']');
369 if (!e /* Missing right bracket? */
370 || e[1] /* Trailing characters? */
371 || !*(s = get_trimmed_slice(s+1, e)) /* Missing name? */
372 ) {
373 parse_error("section header");
374 }
375 /* Right now we only have one section so just check it.
376 * If more sections are added in the future, please don't
377 * resort to cascading ifs with multiple strcasecmp calls.
378 * That kind of bloated code is all too common. A loop
379 * and a string table would be a better choice unless the
380 * number of sections is very small. */
381 if (strcasecmp(s, "SUID") == 0) {
382 section = 1;
383 continue;
384 }
385 section = -1; /* Unknown section so set to skip. */
386 continue;
387 }
388
389 /* Process sections. */
390
391 if (section == 1) { /* SUID */
392 /* Since we trimmed leading and trailing space above, we're
393 * now looking for strings of the form
394 * <key>[::space::]*=[::space::]*<value>
395 * where both key and value could contain inner whitespace. */
396
397 /* First get the key (an applet name in our case). */
398 e = strchr(s, '=');
399 if (e) {
400 s = get_trimmed_slice(s, e);
401 }
402 if (!e || !*s) { /* Missing '=' or empty key. */
403 parse_error("keyword");
404 }
405
406 /* Ok, we have an applet name. Process the rhs if this
407 * applet is currently built in and ignore it otherwise.
408 * Note: this can hide config file bugs which only pop
409 * up when the busybox configuration is changed. */
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000410 applet_no = find_applet_by_name(s);
411 if (applet_no >= 0) {
Denis Vlasenko724d1962007-10-10 14:41:07 +0000412 /* Note: We currently don't check for duplicates!
413 * The last config line for each applet will be the
414 * one used since we insert at the head of the list.
415 * I suppose this could be considered a feature. */
416 sct = xmalloc(sizeof(struct BB_suid_config));
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000417 sct->m_applet = applet_no;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000418 sct->m_mode = 0;
419 sct->m_next = sct_head;
420 sct_head = sct;
421
422 /* Get the specified mode. */
423
424 e = skip_whitespace(e+1);
425
426 for (i = 0; i < 3; i++) {
427 /* There are 4 chars + 1 nul for each of user/group/other. */
428 static const char mode_chars[] ALIGN1 = "Ssx-\0" "Ssx-\0" "Ttx-";
429
430 const char *q;
431 q = strchrnul(mode_chars + 5*i, *e++);
432 if (!*q) {
433 parse_error("mode");
434 }
435 /* Adjust by -i to account for nul. */
436 sct->m_mode |= mode_mask[(q - mode_chars) - i];
437 }
438
439 /* Now get the the user/group info. */
440
441 s = skip_whitespace(e);
442
443 /* Note: we require whitespace between the mode and the
444 * user/group info. */
445 if ((s == e) || !(e = strchr(s, '.'))) {
446 parse_error("<uid>.<gid>");
447 }
448 *e++ = '\0';
449
450 /* We can't use get_ug_id here since it would exit()
451 * if a uid or gid was not found. Oh well... */
452 sct->m_uid = bb_strtoul(s, NULL, 10);
453 if (errno) {
454 struct passwd *pwd = getpwnam(s);
455 if (!pwd) {
456 parse_error("user");
457 }
458 sct->m_uid = pwd->pw_uid;
459 }
460
461 sct->m_gid = bb_strtoul(e, NULL, 10);
462 if (errno) {
463 struct group *grp;
464 grp = getgrnam(e);
465 if (!grp) {
466 parse_error("group");
467 }
468 sct->m_gid = grp->gr_gid;
469 }
470 }
471 continue;
472 }
473
474 /* Unknown sections are ignored. */
475
476 /* Encountering configuration lines prior to seeing a
477 * section header is treated as an error. This is how
478 * the old code worked, but it may not be desirable.
479 * We may want to simply ignore such lines in case they
480 * are used in some future version of busybox. */
481 if (!section) {
482 parse_error("keyword outside section");
483 }
484
485 } /* while (1) */
486
487 pe_label:
488 fprintf(stderr, "Parse error in %s, line %d: %s\n",
489 config_file, lc, errmsg);
490
491 fclose(f);
492 /* Release any allocated memory before returning. */
493 while (sct_head) {
494 sct = sct_head->m_next;
495 free(sct_head);
496 sct_head = sct;
497 }
498}
499#else
500static inline void parse_config_file(void)
501{
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000502 IF_FEATURE_SUID(ruid = getuid();)
Denis Vlasenko724d1962007-10-10 14:41:07 +0000503}
504#endif /* FEATURE_SUID_CONFIG */
505
506
507#if ENABLE_FEATURE_SUID
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000508static void check_suid(int applet_no)
Denis Vlasenko724d1962007-10-10 14:41:07 +0000509{
510 gid_t rgid; /* real gid */
511
512 if (ruid == 0) /* set by parse_config_file() */
513 return; /* run by root - no need to check more */
514 rgid = getgid();
515
516#if ENABLE_FEATURE_SUID_CONFIG
517 if (suid_cfg_readable) {
518 uid_t uid;
519 struct BB_suid_config *sct;
520 mode_t m;
521
522 for (sct = suid_config; sct; sct = sct->m_next) {
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000523 if (sct->m_applet == applet_no)
Denis Vlasenko724d1962007-10-10 14:41:07 +0000524 goto found;
525 }
Denis Vlasenko15ca51e2007-10-29 19:25:45 +0000526 goto check_need_suid;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000527 found:
528 m = sct->m_mode;
529 if (sct->m_uid == ruid)
530 /* same uid */
531 m >>= 6;
532 else if ((sct->m_gid == rgid) || ingroup(ruid, sct->m_gid))
533 /* same group / in group */
534 m >>= 3;
535
536 if (!(m & S_IXOTH)) /* is x bit not set ? */
537 bb_error_msg_and_die("you have no permission to run this applet!");
538
539 /* _both_ sgid and group_exec have to be set for setegid */
540 if ((sct->m_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
541 rgid = sct->m_gid;
542 /* else (no setegid) we will set egid = rgid */
543
544 /* We set effective AND saved ids. If saved-id is not set
545 * like we do below, seteiud(0) can still later succeed! */
546 if (setresgid(-1, rgid, rgid))
547 bb_perror_msg_and_die("setresgid");
548
549 /* do we have to set effective uid? */
550 uid = ruid;
551 if (sct->m_mode & S_ISUID)
552 uid = sct->m_uid;
553 /* else (no seteuid) we will set euid = ruid */
554
555 if (setresuid(-1, uid, uid))
556 bb_perror_msg_and_die("setresuid");
557 return;
558 }
559#if !ENABLE_FEATURE_SUID_CONFIG_QUIET
560 {
561 static bool onetime = 0;
562
563 if (!onetime) {
564 onetime = 1;
565 fprintf(stderr, "Using fallback suid method\n");
566 }
567 }
568#endif
Denis Vlasenko15ca51e2007-10-29 19:25:45 +0000569 check_need_suid:
Denis Vlasenko724d1962007-10-10 14:41:07 +0000570#endif
Denys Vlasenko6d48d3e2009-07-30 12:57:19 +0200571 if (APPLET_SUID(applet_no) == _BB_SUID_REQUIRE) {
Denis Vlasenko724d1962007-10-10 14:41:07 +0000572 /* Real uid is not 0. If euid isn't 0 too, suid bit
573 * is most probably not set on our executable */
574 if (geteuid())
Denis Vlasenko15ca51e2007-10-29 19:25:45 +0000575 bb_error_msg_and_die("must be suid to work properly");
Denys Vlasenko6d48d3e2009-07-30 12:57:19 +0200576 } else if (APPLET_SUID(applet_no) == _BB_SUID_DROP) {
Denis Vlasenko724d1962007-10-10 14:41:07 +0000577 xsetgid(rgid); /* drop all privileges */
578 xsetuid(ruid);
579 }
580}
581#else
582#define check_suid(x) ((void)0)
583#endif /* FEATURE_SUID */
584
585
586#if ENABLE_FEATURE_INSTALLER
587/* create (sym)links for each applet */
Denys Vlasenko3b405432009-07-15 00:35:34 +0200588static void install_links(const char *busybox, int use_symbolic_links,
589 char *custom_install_dir)
Denis Vlasenko724d1962007-10-10 14:41:07 +0000590{
591 /* directory table
592 * this should be consistent w/ the enum,
593 * busybox.h::bb_install_loc_t, or else... */
594 static const char usr_bin [] ALIGN1 = "/usr/bin";
595 static const char usr_sbin[] ALIGN1 = "/usr/sbin";
596 static const char *const install_dir[] = {
597 &usr_bin [8], /* "", equivalent to "/" for concat_path_file() */
598 &usr_bin [4], /* "/bin" */
599 &usr_sbin[4], /* "/sbin" */
600 usr_bin,
601 usr_sbin
602 };
603
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000604 int (*lf)(const char *, const char *);
Denis Vlasenko724d1962007-10-10 14:41:07 +0000605 char *fpc;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000606 unsigned i;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000607 int rc;
608
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000609 lf = link;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000610 if (use_symbolic_links)
611 lf = symlink;
612
Denis Vlasenko745cd172007-11-29 03:31:20 +0000613 for (i = 0; i < ARRAY_SIZE(applet_main); i++) {
Denis Vlasenko724d1962007-10-10 14:41:07 +0000614 fpc = concat_path_file(
Denys Vlasenko3b405432009-07-15 00:35:34 +0200615 custom_install_dir ? custom_install_dir : install_dir[APPLET_INSTALL_LOC(i)],
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000616 APPLET_NAME(i));
Denis Vlasenko745cd172007-11-29 03:31:20 +0000617 // debug: bb_error_msg("%slinking %s to busybox",
618 // use_symbolic_links ? "sym" : "", fpc);
Denis Vlasenko724d1962007-10-10 14:41:07 +0000619 rc = lf(busybox, fpc);
620 if (rc != 0 && errno != EEXIST) {
621 bb_simple_perror_msg(fpc);
622 }
623 free(fpc);
624 }
625}
626#else
Denys Vlasenko3b405432009-07-15 00:35:34 +0200627#define install_links(x,y,z) ((void)0)
Denis Vlasenko724d1962007-10-10 14:41:07 +0000628#endif /* FEATURE_INSTALLER */
629
630/* If we were called as "busybox..." */
631static int busybox_main(char **argv)
632{
633 if (!argv[1]) {
634 /* Called without arguments */
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000635 const char *a;
Denys Vlasenko0149f022009-05-19 18:01:42 +0200636 int col;
637 unsigned output_width;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000638 help:
639 output_width = 80;
640 if (ENABLE_FEATURE_AUTOWIDTH) {
641 /* Obtain the terminal width */
642 get_terminal_width_height(0, &output_width, NULL);
643 }
Denis Vlasenko724d1962007-10-10 14:41:07 +0000644
Denis Vlasenko21278df2008-06-25 12:15:46 +0000645 dup2(1, 2);
Denis Vlasenko79cedcb2008-04-08 21:13:28 +0000646 full_write2_str(bb_banner); /* reuse const string... */
647 full_write2_str(" multi-call binary\n"
Denys Vlasenkob3475922009-08-27 21:13:30 +0200648 "Copyright (C) 1998-2009 Erik Andersen, Rob Landley, Denys Vlasenko\n"
Denis Vlasenkofcfb5c02007-12-24 12:16:24 +0000649 "and others. Licensed under GPLv2.\n"
650 "See source distribution for full notice.\n"
Denis Vlasenko724d1962007-10-10 14:41:07 +0000651 "\n"
652 "Usage: busybox [function] [arguments]...\n"
Denis Vlasenkofcfb5c02007-12-24 12:16:24 +0000653 " or: function [arguments]...\n"
Denis Vlasenko724d1962007-10-10 14:41:07 +0000654 "\n"
655 "\tBusyBox is a multi-call binary that combines many common Unix\n"
656 "\tutilities into a single executable. Most people will create a\n"
657 "\tlink to busybox for each function they wish to use and BusyBox\n"
658 "\twill act like whatever it was invoked as!\n"
Denis Vlasenkofcfb5c02007-12-24 12:16:24 +0000659 "\n"
660 "Currently defined functions:\n");
Denis Vlasenko724d1962007-10-10 14:41:07 +0000661 col = 0;
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000662 a = applet_names;
Denys Vlasenko0149f022009-05-19 18:01:42 +0200663 /* prevent last comma to be in the very last pos */
664 output_width--;
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000665 while (*a) {
Denys Vlasenko0149f022009-05-19 18:01:42 +0200666 int len2 = strlen(a) + 2;
667 if (col >= (int)output_width - len2) {
Denis Vlasenko79cedcb2008-04-08 21:13:28 +0000668 full_write2_str(",\n");
Denis Vlasenko724d1962007-10-10 14:41:07 +0000669 col = 0;
670 }
Denys Vlasenko0149f022009-05-19 18:01:42 +0200671 if (col == 0) {
672 col = 6;
673 full_write2_str("\t");
674 } else {
675 full_write2_str(", ");
676 }
Denis Vlasenko79cedcb2008-04-08 21:13:28 +0000677 full_write2_str(a);
Denys Vlasenko0149f022009-05-19 18:01:42 +0200678 col += len2;
679 a += len2 - 1;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000680 }
Denis Vlasenko79cedcb2008-04-08 21:13:28 +0000681 full_write2_str("\n\n");
Denis Vlasenko724d1962007-10-10 14:41:07 +0000682 return 0;
683 }
684
685 if (ENABLE_FEATURE_INSTALLER && strcmp(argv[1], "--install") == 0) {
Denys Vlasenko3b405432009-07-15 00:35:34 +0200686 int use_symbolic_links;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000687 const char *busybox;
688 busybox = xmalloc_readlink(bb_busybox_exec_path);
689 if (!busybox)
690 busybox = bb_busybox_exec_path;
Denys Vlasenko0ad438b2009-07-15 02:14:15 +0200691 /* busybox --install [-s] [DIR]: */
692 /* -s: make symlinks */
693 /* DIR: directory to install links to */
Denys Vlasenko3b405432009-07-15 00:35:34 +0200694 use_symbolic_links = (argv[2] && strcmp(argv[2], "-s") == 0 && argv++);
695 install_links(busybox, use_symbolic_links, argv[2]);
Denis Vlasenko724d1962007-10-10 14:41:07 +0000696 return 0;
697 }
698
699 if (strcmp(argv[1], "--help") == 0) {
700 /* "busybox --help [<applet>]" */
701 if (!argv[2])
702 goto help;
703 /* convert to "<applet> --help" */
704 argv[0] = argv[2];
705 argv[2] = NULL;
706 } else {
707 /* "busybox <applet> arg1 arg2 ..." */
708 argv++;
709 }
710 /* We support "busybox /a/path/to/applet args..." too. Allows for
711 * "#!/bin/busybox"-style wrappers */
712 applet_name = bb_get_last_path_component_nostrip(argv[0]);
713 run_applet_and_exit(applet_name, argv);
Denis Vlasenko79cedcb2008-04-08 21:13:28 +0000714
715 /*bb_error_msg_and_die("applet not found"); - sucks in printf */
716 full_write2_str(applet_name);
717 full_write2_str(": applet not found\n");
718 xfunc_die();
Denis Vlasenko724d1962007-10-10 14:41:07 +0000719}
720
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000721void FAST_FUNC run_applet_no_and_exit(int applet_no, char **argv)
Denis Vlasenko724d1962007-10-10 14:41:07 +0000722{
723 int argc = 1;
724
725 while (argv[argc])
726 argc++;
727
728 /* Reinit some shared global data */
Denis Vlasenko724d1962007-10-10 14:41:07 +0000729 xfunc_error_retval = EXIT_FAILURE;
730
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000731 applet_name = APPLET_NAME(applet_no);
Denis Vlasenkobd28f6b2008-07-19 08:15:13 +0000732 if (argc == 2 && strcmp(argv[1], "--help") == 0) {
733 /* Special case. POSIX says "test --help"
734 * should be no different from e.g. "test --foo". */
735//TODO: just compare applet_no with APPLET_NO_test
736 if (!ENABLE_TEST || strcmp(applet_name, "test") != 0)
737 bb_show_usage();
738 }
Denis Vlasenko724d1962007-10-10 14:41:07 +0000739 if (ENABLE_FEATURE_SUID)
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000740 check_suid(applet_no);
Denis Vlasenko745cd172007-11-29 03:31:20 +0000741 exit(applet_main[applet_no](argc, argv));
Denis Vlasenko724d1962007-10-10 14:41:07 +0000742}
743
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000744void FAST_FUNC run_applet_and_exit(const char *name, char **argv)
Denis Vlasenko724d1962007-10-10 14:41:07 +0000745{
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000746 int applet = find_applet_by_name(name);
747 if (applet >= 0)
748 run_applet_no_and_exit(applet, argv);
Denis Vlasenko724d1962007-10-10 14:41:07 +0000749 if (!strncmp(name, "busybox", 7))
750 exit(busybox_main(argv));
751}
752
Denis Vlasenko10f6fb12008-04-29 00:10:27 +0000753#endif /* !defined(SINGLE_APPLET_MAIN) */
Denis Vlasenko468aea22008-04-01 14:47:57 +0000754
755
Denis Vlasenko724d1962007-10-10 14:41:07 +0000756
757#if ENABLE_BUILD_LIBBUSYBOX
Denis Vlasenko85c24712008-03-17 09:04:04 +0000758int lbb_main(char **argv)
Denis Vlasenko724d1962007-10-10 14:41:07 +0000759#else
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000760int main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko724d1962007-10-10 14:41:07 +0000761#endif
762{
Denis Vlasenko10f6fb12008-04-29 00:10:27 +0000763#if defined(SINGLE_APPLET_MAIN)
Denis Vlasenko468aea22008-04-01 14:47:57 +0000764 /* Only one applet is selected by the user! */
Denis Vlasenko79cedcb2008-04-08 21:13:28 +0000765 /* applet_names in this case is just "applet\0\0" */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000766 lbb_prepare(applet_names IF_FEATURE_INDIVIDUAL(, argv));
Denis Vlasenko468aea22008-04-01 14:47:57 +0000767 return SINGLE_APPLET_MAIN(argc, argv);
768#else
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000769 lbb_prepare("busybox" IF_FEATURE_INDIVIDUAL(, argv));
Denis Vlasenko724d1962007-10-10 14:41:07 +0000770
771#if !BB_MMU
772 /* NOMMU re-exec trick sets high-order bit in first byte of name */
773 if (argv[0][0] & 0x80) {
774 re_execed = 1;
775 argv[0][0] &= 0x7f;
776 }
777#endif
778 applet_name = argv[0];
779 if (applet_name[0] == '-')
780 applet_name++;
781 applet_name = bb_basename(applet_name);
782
783 parse_config_file(); /* ...maybe, if FEATURE_SUID_CONFIG */
784
785 run_applet_and_exit(applet_name, argv);
Denis Vlasenko79cedcb2008-04-08 21:13:28 +0000786
787 /*bb_error_msg_and_die("applet not found"); - sucks in printf */
788 full_write2_str(applet_name);
789 full_write2_str(": applet not found\n");
790 xfunc_die();
Denis Vlasenko468aea22008-04-01 14:47:57 +0000791#endif
Denis Vlasenko724d1962007-10-10 14:41:07 +0000792}