blob: 03f712821e871588cc84c3f1033a9da001987f54 [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;
Denys Vlasenko4566e172011-05-16 00:01:08 +0200239 struct bb_uidgid_t m_ugid;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000240 mode_t m_mode;
241 struct BB_suid_config *m_next;
242} *suid_config;
243
244static bool suid_cfg_readable;
245
246/* check if u is member of group g */
247static int ingroup(uid_t u, gid_t g)
248{
249 struct group *grp = getgrgid(g);
250
251 if (grp) {
252 char **mem;
253
254 for (mem = grp->gr_mem; *mem; mem++) {
255 struct passwd *pwd = getpwnam(*mem);
256
257 if (pwd && (pwd->pw_uid == u))
258 return 1;
259 }
260 }
261 return 0;
262}
263
264/* This should probably be a libbb routine. In that case,
265 * I'd probably rename it to something like bb_trimmed_slice.
266 */
267static char *get_trimmed_slice(char *s, char *e)
268{
269 /* First, consider the value at e to be nul and back up until we
270 * reach a non-space char. Set the char after that (possibly at
271 * the original e) to nul. */
272 while (e-- > s) {
273 if (!isspace(*e)) {
274 break;
275 }
276 }
277 e[1] = '\0';
278
279 /* Next, advance past all leading space and return a ptr to the
280 * first non-space char; possibly the terminating nul. */
281 return skip_whitespace(s);
282}
283
284/* Don't depend on the tools to combine strings. */
285static const char config_file[] ALIGN1 = "/etc/busybox.conf";
286
287/* We don't supply a value for the nul, so an index adjustment is
288 * necessary below. Also, we use unsigned short here to save some
289 * space even though these are really mode_t values. */
290static const unsigned short mode_mask[] ALIGN2 = {
291 /* SST sst xxx --- */
292 S_ISUID, S_ISUID|S_IXUSR, S_IXUSR, 0, /* user */
293 S_ISGID, S_ISGID|S_IXGRP, S_IXGRP, 0, /* group */
294 0, S_IXOTH, S_IXOTH, 0 /* other */
295};
296
Denis Vlasenko724d1962007-10-10 14:41:07 +0000297static void parse_config_file(void)
298{
299 struct BB_suid_config *sct_head;
300 struct BB_suid_config *sct;
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000301 int applet_no;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000302 FILE *f;
303 const char *errmsg;
304 char *s;
305 char *e;
306 int i;
307 unsigned lc;
308 smallint section;
309 char buffer[256];
310 struct stat st;
311
Denis Vlasenko724d1962007-10-10 14:41:07 +0000312 ruid = getuid();
313 if (ruid == 0) /* run by root - don't need to even read config file */
314 return;
315
316 if ((stat(config_file, &st) != 0) /* No config file? */
317 || !S_ISREG(st.st_mode) /* Not a regular file? */
318 || (st.st_uid != 0) /* Not owned by root? */
319 || (st.st_mode & (S_IWGRP | S_IWOTH)) /* Writable by non-root? */
Denys Vlasenko4566e172011-05-16 00:01:08 +0200320 || !(f = fopen_for_read(config_file)) /* Cannot open? */
Denis Vlasenko724d1962007-10-10 14:41:07 +0000321 ) {
322 return;
323 }
324
325 suid_cfg_readable = 1;
326 sct_head = NULL;
327 section = lc = 0;
328
329 while (1) {
330 s = buffer;
331
332 if (!fgets(s, sizeof(buffer), f)) { /* Are we done? */
Denys Vlasenko4566e172011-05-16 00:01:08 +0200333 // Looks like bloat
334 //if (ferror(f)) { /* Make sure it wasn't a read error. */
335 // errmsg = "reading";
336 // goto pe_label;
337 //}
Denis Vlasenko724d1962007-10-10 14:41:07 +0000338 fclose(f);
339 suid_config = sct_head; /* Success, so set the pointer. */
340 return;
341 }
342
343 lc++; /* Got a (partial) line. */
344
345 /* If a line is too long for our buffer, we consider it an error.
346 * The following test does mistreat one corner case though.
347 * If the final line of the file does not end with a newline and
348 * yet exactly fills the buffer, it will be treated as too long
349 * even though there isn't really a problem. But it isn't really
350 * worth adding code to deal with such an unlikely situation, and
351 * we do err on the side of caution. Besides, the line would be
352 * too long if it did end with a newline. */
353 if (!strchr(s, '\n') && !feof(f)) {
Denys Vlasenko4566e172011-05-16 00:01:08 +0200354 errmsg = "line too long";
355 goto pe_label;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000356 }
357
358 /* Trim leading and trailing whitespace, ignoring comments, and
359 * check if the resulting string is empty. */
360 s = get_trimmed_slice(s, strchrnul(s, '#'));
361 if (!*s) {
362 continue;
363 }
364
365 /* Check for a section header. */
366
367 if (*s == '[') {
368 /* Unlike the old code, we ignore leading and trailing
369 * whitespace for the section name. We also require that
370 * there are no stray characters after the closing bracket. */
371 e = strchr(s, ']');
372 if (!e /* Missing right bracket? */
373 || e[1] /* Trailing characters? */
374 || !*(s = get_trimmed_slice(s+1, e)) /* Missing name? */
375 ) {
Denys Vlasenko4566e172011-05-16 00:01:08 +0200376 errmsg = "section header";
377 goto pe_label;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000378 }
379 /* Right now we only have one section so just check it.
380 * If more sections are added in the future, please don't
381 * resort to cascading ifs with multiple strcasecmp calls.
382 * That kind of bloated code is all too common. A loop
383 * and a string table would be a better choice unless the
384 * number of sections is very small. */
385 if (strcasecmp(s, "SUID") == 0) {
386 section = 1;
387 continue;
388 }
389 section = -1; /* Unknown section so set to skip. */
390 continue;
391 }
392
393 /* Process sections. */
394
395 if (section == 1) { /* SUID */
396 /* Since we trimmed leading and trailing space above, we're
397 * now looking for strings of the form
398 * <key>[::space::]*=[::space::]*<value>
399 * where both key and value could contain inner whitespace. */
400
401 /* First get the key (an applet name in our case). */
402 e = strchr(s, '=');
403 if (e) {
404 s = get_trimmed_slice(s, e);
405 }
406 if (!e || !*s) { /* Missing '=' or empty key. */
Denys Vlasenko4566e172011-05-16 00:01:08 +0200407 errmsg = "keyword";
408 goto pe_label;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000409 }
410
411 /* Ok, we have an applet name. Process the rhs if this
412 * applet is currently built in and ignore it otherwise.
413 * Note: this can hide config file bugs which only pop
414 * up when the busybox configuration is changed. */
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000415 applet_no = find_applet_by_name(s);
416 if (applet_no >= 0) {
Denis Vlasenko724d1962007-10-10 14:41:07 +0000417 /* Note: We currently don't check for duplicates!
418 * The last config line for each applet will be the
419 * one used since we insert at the head of the list.
420 * I suppose this could be considered a feature. */
Denys Vlasenko4566e172011-05-16 00:01:08 +0200421 sct = xzalloc(sizeof(*sct));
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000422 sct->m_applet = applet_no;
Denys Vlasenko4566e172011-05-16 00:01:08 +0200423 /*sct->m_mode = 0;*/
Denis Vlasenko724d1962007-10-10 14:41:07 +0000424 sct->m_next = sct_head;
425 sct_head = sct;
426
427 /* Get the specified mode. */
428
429 e = skip_whitespace(e+1);
430
431 for (i = 0; i < 3; i++) {
432 /* There are 4 chars + 1 nul for each of user/group/other. */
433 static const char mode_chars[] ALIGN1 = "Ssx-\0" "Ssx-\0" "Ttx-";
434
435 const char *q;
436 q = strchrnul(mode_chars + 5*i, *e++);
437 if (!*q) {
Denys Vlasenko4566e172011-05-16 00:01:08 +0200438 errmsg = "mode";
439 goto pe_label;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000440 }
441 /* Adjust by -i to account for nul. */
442 sct->m_mode |= mode_mask[(q - mode_chars) - i];
443 }
444
Marek Polacekb0b88842011-04-16 17:33:43 +0200445 /* Now get the user/group info. */
Denis Vlasenko724d1962007-10-10 14:41:07 +0000446
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, '.'))) {
Denys Vlasenko4566e172011-05-16 00:01:08 +0200452 errmsg = "uid.gid";
453 goto pe_label;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000454 }
455
Denys Vlasenko4566e172011-05-16 00:01:08 +0200456 *e++ = ':'; /* get_uidgid doesn't understand user.group */
457 if (get_uidgid(&sct->m_ugid, s, /*allow_numeric:*/ 1) == 0) {
458 errmsg = "unknown user/group";
459 goto pe_label;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000460 }
461 }
462 continue;
463 }
464
465 /* Unknown sections are ignored. */
466
467 /* Encountering configuration lines prior to seeing a
468 * section header is treated as an error. This is how
469 * the old code worked, but it may not be desirable.
470 * We may want to simply ignore such lines in case they
471 * are used in some future version of busybox. */
472 if (!section) {
Denys Vlasenko4566e172011-05-16 00:01:08 +0200473 errmsg = "keyword outside section";
474 goto pe_label;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000475 }
476
477 } /* while (1) */
478
479 pe_label:
Denys Vlasenko4566e172011-05-16 00:01:08 +0200480 bb_error_msg("parse error in %s, line %u: %s", config_file, lc, errmsg);
Denis Vlasenko724d1962007-10-10 14:41:07 +0000481
482 fclose(f);
483 /* Release any allocated memory before returning. */
484 while (sct_head) {
485 sct = sct_head->m_next;
486 free(sct_head);
487 sct_head = sct;
488 }
489}
Denys Vlasenko8f0af3b2010-11-29 02:55:35 +0100490# else
Denis Vlasenko724d1962007-10-10 14:41:07 +0000491static inline void parse_config_file(void)
492{
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000493 IF_FEATURE_SUID(ruid = getuid();)
Denis Vlasenko724d1962007-10-10 14:41:07 +0000494}
Denys Vlasenko8f0af3b2010-11-29 02:55:35 +0100495# endif /* FEATURE_SUID_CONFIG */
Denis Vlasenko724d1962007-10-10 14:41:07 +0000496
497
Denys Vlasenko8f0af3b2010-11-29 02:55:35 +0100498# if ENABLE_FEATURE_SUID
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000499static void check_suid(int applet_no)
Denis Vlasenko724d1962007-10-10 14:41:07 +0000500{
501 gid_t rgid; /* real gid */
502
503 if (ruid == 0) /* set by parse_config_file() */
504 return; /* run by root - no need to check more */
505 rgid = getgid();
506
Denys Vlasenko8f0af3b2010-11-29 02:55:35 +0100507# if ENABLE_FEATURE_SUID_CONFIG
Denis Vlasenko724d1962007-10-10 14:41:07 +0000508 if (suid_cfg_readable) {
509 uid_t uid;
510 struct BB_suid_config *sct;
511 mode_t m;
512
513 for (sct = suid_config; sct; sct = sct->m_next) {
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000514 if (sct->m_applet == applet_no)
Denis Vlasenko724d1962007-10-10 14:41:07 +0000515 goto found;
516 }
Denis Vlasenko15ca51e2007-10-29 19:25:45 +0000517 goto check_need_suid;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000518 found:
519 m = sct->m_mode;
Denys Vlasenko4566e172011-05-16 00:01:08 +0200520 if (sct->m_ugid.uid == ruid)
Denis Vlasenko724d1962007-10-10 14:41:07 +0000521 /* same uid */
522 m >>= 6;
Denys Vlasenko4566e172011-05-16 00:01:08 +0200523 else if ((sct->m_ugid.gid == rgid) || ingroup(ruid, sct->m_ugid.gid))
Denis Vlasenko724d1962007-10-10 14:41:07 +0000524 /* same group / in group */
525 m >>= 3;
526
527 if (!(m & S_IXOTH)) /* is x bit not set ? */
528 bb_error_msg_and_die("you have no permission to run this applet!");
529
530 /* _both_ sgid and group_exec have to be set for setegid */
531 if ((sct->m_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
Denys Vlasenko4566e172011-05-16 00:01:08 +0200532 rgid = sct->m_ugid.gid;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000533 /* else (no setegid) we will set egid = rgid */
534
535 /* We set effective AND saved ids. If saved-id is not set
536 * like we do below, seteiud(0) can still later succeed! */
537 if (setresgid(-1, rgid, rgid))
538 bb_perror_msg_and_die("setresgid");
539
540 /* do we have to set effective uid? */
541 uid = ruid;
542 if (sct->m_mode & S_ISUID)
Denys Vlasenko4566e172011-05-16 00:01:08 +0200543 uid = sct->m_ugid.uid;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000544 /* else (no seteuid) we will set euid = ruid */
545
546 if (setresuid(-1, uid, uid))
547 bb_perror_msg_and_die("setresuid");
548 return;
549 }
Denys Vlasenko8f0af3b2010-11-29 02:55:35 +0100550# if !ENABLE_FEATURE_SUID_CONFIG_QUIET
Denis Vlasenko724d1962007-10-10 14:41:07 +0000551 {
552 static bool onetime = 0;
553
554 if (!onetime) {
555 onetime = 1;
556 fprintf(stderr, "Using fallback suid method\n");
557 }
558 }
Denys Vlasenko8f0af3b2010-11-29 02:55:35 +0100559# endif
Denis Vlasenko15ca51e2007-10-29 19:25:45 +0000560 check_need_suid:
Denys Vlasenko8f0af3b2010-11-29 02:55:35 +0100561# endif
Denys Vlasenkob9f2d9f2011-01-18 13:58:01 +0100562 if (APPLET_SUID(applet_no) == BB_SUID_REQUIRE) {
Denis Vlasenko724d1962007-10-10 14:41:07 +0000563 /* Real uid is not 0. If euid isn't 0 too, suid bit
564 * is most probably not set on our executable */
565 if (geteuid())
Denis Vlasenko15ca51e2007-10-29 19:25:45 +0000566 bb_error_msg_and_die("must be suid to work properly");
Denys Vlasenkob9f2d9f2011-01-18 13:58:01 +0100567 } else if (APPLET_SUID(applet_no) == BB_SUID_DROP) {
Denis Vlasenko724d1962007-10-10 14:41:07 +0000568 xsetgid(rgid); /* drop all privileges */
569 xsetuid(ruid);
570 }
571}
Denys Vlasenko8f0af3b2010-11-29 02:55:35 +0100572# else
573# define check_suid(x) ((void)0)
574# endif /* FEATURE_SUID */
Denis Vlasenko724d1962007-10-10 14:41:07 +0000575
576
Denys Vlasenko8f0af3b2010-11-29 02:55:35 +0100577# if ENABLE_FEATURE_INSTALLER
Denys Vlasenko5a7c7202010-04-20 21:02:57 -0400578static const char usr_bin [] ALIGN1 = "/usr/bin/";
579static const char usr_sbin[] ALIGN1 = "/usr/sbin/";
580static const char *const install_dir[] = {
581 &usr_bin [8], /* "/" */
582 &usr_bin [4], /* "/bin/" */
Denys Vlasenkod4d289a2010-10-12 04:18:05 +0200583 &usr_sbin[4] /* "/sbin/" */
Denys Vlasenko8f0af3b2010-11-29 02:55:35 +0100584# if !ENABLE_INSTALL_NO_USR
Denys Vlasenkod4d289a2010-10-12 04:18:05 +0200585 ,usr_bin
586 ,usr_sbin
Denys Vlasenko8f0af3b2010-11-29 02:55:35 +0100587# endif
Denys Vlasenko5a7c7202010-04-20 21:02:57 -0400588};
589
Denis Vlasenko724d1962007-10-10 14:41:07 +0000590/* create (sym)links for each applet */
Denys Vlasenko3b405432009-07-15 00:35:34 +0200591static void install_links(const char *busybox, int use_symbolic_links,
592 char *custom_install_dir)
Denis Vlasenko724d1962007-10-10 14:41:07 +0000593{
594 /* directory table
595 * this should be consistent w/ the enum,
596 * busybox.h::bb_install_loc_t, or else... */
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000597 int (*lf)(const char *, const char *);
Denis Vlasenko724d1962007-10-10 14:41:07 +0000598 char *fpc;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000599 unsigned i;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000600 int rc;
601
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000602 lf = link;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000603 if (use_symbolic_links)
604 lf = symlink;
605
Denis Vlasenko745cd172007-11-29 03:31:20 +0000606 for (i = 0; i < ARRAY_SIZE(applet_main); i++) {
Denis Vlasenko724d1962007-10-10 14:41:07 +0000607 fpc = concat_path_file(
Denys Vlasenko3b405432009-07-15 00:35:34 +0200608 custom_install_dir ? custom_install_dir : install_dir[APPLET_INSTALL_LOC(i)],
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000609 APPLET_NAME(i));
Denis Vlasenko745cd172007-11-29 03:31:20 +0000610 // debug: bb_error_msg("%slinking %s to busybox",
611 // use_symbolic_links ? "sym" : "", fpc);
Denis Vlasenko724d1962007-10-10 14:41:07 +0000612 rc = lf(busybox, fpc);
613 if (rc != 0 && errno != EEXIST) {
614 bb_simple_perror_msg(fpc);
615 }
616 free(fpc);
617 }
618}
Denys Vlasenko8f0af3b2010-11-29 02:55:35 +0100619# else
620# define install_links(x,y,z) ((void)0)
621# endif
Denis Vlasenko724d1962007-10-10 14:41:07 +0000622
623/* If we were called as "busybox..." */
624static int busybox_main(char **argv)
625{
626 if (!argv[1]) {
627 /* Called without arguments */
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000628 const char *a;
Denys Vlasenko0149f022009-05-19 18:01:42 +0200629 int col;
630 unsigned output_width;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000631 help:
632 output_width = 80;
633 if (ENABLE_FEATURE_AUTOWIDTH) {
634 /* Obtain the terminal width */
635 get_terminal_width_height(0, &output_width, NULL);
636 }
Denis Vlasenko724d1962007-10-10 14:41:07 +0000637
Denis Vlasenko21278df2008-06-25 12:15:46 +0000638 dup2(1, 2);
Denys Vlasenko630dde12009-08-30 19:57:49 +0200639 full_write2_str(bb_banner); /* reuse const string */
640 full_write2_str(" multi-call binary.\n"); /* reuse */
641 full_write2_str(
Denys Vlasenko5a7c7202010-04-20 21:02:57 -0400642 "Copyright (C) 1998-2009 Erik Andersen, Rob Landley, Denys Vlasenko\n"
643 "and others. Licensed under GPLv2.\n"
644 "See source distribution for full notice.\n"
645 "\n"
646 "Usage: busybox [function] [arguments]...\n"
Denys Vlasenko819cacf2010-09-12 16:17:37 +0200647 " or: busybox --list[-full]\n"
Denys Vlasenko5a7c7202010-04-20 21:02:57 -0400648 " or: function [arguments]...\n"
649 "\n"
650 "\tBusyBox is a multi-call binary that combines many common Unix\n"
651 "\tutilities into a single executable. Most people will create a\n"
652 "\tlink to busybox for each function they wish to use and BusyBox\n"
653 "\twill act like whatever it was invoked as.\n"
654 "\n"
655 "Currently defined functions:\n"
656 );
Denis Vlasenko724d1962007-10-10 14:41:07 +0000657 col = 0;
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000658 a = applet_names;
Denys Vlasenko0149f022009-05-19 18:01:42 +0200659 /* prevent last comma to be in the very last pos */
660 output_width--;
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000661 while (*a) {
Denys Vlasenko0149f022009-05-19 18:01:42 +0200662 int len2 = strlen(a) + 2;
663 if (col >= (int)output_width - len2) {
Denis Vlasenko79cedcb2008-04-08 21:13:28 +0000664 full_write2_str(",\n");
Denis Vlasenko724d1962007-10-10 14:41:07 +0000665 col = 0;
666 }
Denys Vlasenko0149f022009-05-19 18:01:42 +0200667 if (col == 0) {
668 col = 6;
669 full_write2_str("\t");
670 } else {
671 full_write2_str(", ");
672 }
Denis Vlasenko79cedcb2008-04-08 21:13:28 +0000673 full_write2_str(a);
Denys Vlasenko0149f022009-05-19 18:01:42 +0200674 col += len2;
675 a += len2 - 1;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000676 }
Denis Vlasenko79cedcb2008-04-08 21:13:28 +0000677 full_write2_str("\n\n");
Denis Vlasenko724d1962007-10-10 14:41:07 +0000678 return 0;
679 }
680
Denys Vlasenko5a7c7202010-04-20 21:02:57 -0400681 if (strncmp(argv[1], "--list", 6) == 0) {
682 unsigned i = 0;
683 const char *a = applet_names;
684 dup2(1, 2);
685 while (*a) {
Denys Vlasenko8f0af3b2010-11-29 02:55:35 +0100686# if ENABLE_FEATURE_INSTALLER
Denys Vlasenko5a7c7202010-04-20 21:02:57 -0400687 if (argv[1][6]) /* --list-path? */
688 full_write2_str(install_dir[APPLET_INSTALL_LOC(i)] + 1);
Denys Vlasenko8f0af3b2010-11-29 02:55:35 +0100689# endif
Denys Vlasenko5a7c7202010-04-20 21:02:57 -0400690 full_write2_str(a);
691 full_write2_str("\n");
692 i++;
693 a += strlen(a) + 1;
694 }
695 return 0;
696 }
697
Denis Vlasenko724d1962007-10-10 14:41:07 +0000698 if (ENABLE_FEATURE_INSTALLER && strcmp(argv[1], "--install") == 0) {
Denys Vlasenko3b405432009-07-15 00:35:34 +0200699 int use_symbolic_links;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000700 const char *busybox;
Denys Vlasenko4a2a86d2011-03-06 06:02:31 +0100701
Denis Vlasenko724d1962007-10-10 14:41:07 +0000702 busybox = xmalloc_readlink(bb_busybox_exec_path);
Denys Vlasenko4a2a86d2011-03-06 06:02:31 +0100703 if (!busybox) {
704 /* bb_busybox_exec_path is usually "/proc/self/exe".
705 * In chroot, readlink("/proc/self/exe") usually fails.
706 * In such case, better use argv[0] as symlink target
707 * if it is a full path name.
708 */
Denys Vlasenko5c942712011-03-12 06:08:28 +0100709 if (argv[0][0] != '/')
710 bb_error_msg_and_die("'%s' is not an absolute path", argv[0]);
711 busybox = argv[0];
Denys Vlasenko4a2a86d2011-03-06 06:02:31 +0100712 }
713 /* busybox --install [-s] [DIR]:
714 * -s: make symlinks
715 * DIR: directory to install links to
716 */
Denys Vlasenko3b405432009-07-15 00:35:34 +0200717 use_symbolic_links = (argv[2] && strcmp(argv[2], "-s") == 0 && argv++);
718 install_links(busybox, use_symbolic_links, argv[2]);
Denis Vlasenko724d1962007-10-10 14:41:07 +0000719 return 0;
720 }
721
722 if (strcmp(argv[1], "--help") == 0) {
723 /* "busybox --help [<applet>]" */
724 if (!argv[2])
725 goto help;
726 /* convert to "<applet> --help" */
727 argv[0] = argv[2];
728 argv[2] = NULL;
729 } else {
730 /* "busybox <applet> arg1 arg2 ..." */
731 argv++;
732 }
733 /* We support "busybox /a/path/to/applet args..." too. Allows for
734 * "#!/bin/busybox"-style wrappers */
735 applet_name = bb_get_last_path_component_nostrip(argv[0]);
736 run_applet_and_exit(applet_name, argv);
Denis Vlasenko79cedcb2008-04-08 21:13:28 +0000737
738 /*bb_error_msg_and_die("applet not found"); - sucks in printf */
739 full_write2_str(applet_name);
740 full_write2_str(": applet not found\n");
741 xfunc_die();
Denis Vlasenko724d1962007-10-10 14:41:07 +0000742}
743
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000744void FAST_FUNC run_applet_no_and_exit(int applet_no, char **argv)
Denis Vlasenko724d1962007-10-10 14:41:07 +0000745{
746 int argc = 1;
747
748 while (argv[argc])
749 argc++;
750
751 /* Reinit some shared global data */
Denis Vlasenko724d1962007-10-10 14:41:07 +0000752 xfunc_error_retval = EXIT_FAILURE;
753
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000754 applet_name = APPLET_NAME(applet_no);
Denis Vlasenkobd28f6b2008-07-19 08:15:13 +0000755 if (argc == 2 && strcmp(argv[1], "--help") == 0) {
756 /* Special case. POSIX says "test --help"
757 * should be no different from e.g. "test --foo". */
758//TODO: just compare applet_no with APPLET_NO_test
759 if (!ENABLE_TEST || strcmp(applet_name, "test") != 0)
760 bb_show_usage();
761 }
Denis Vlasenko724d1962007-10-10 14:41:07 +0000762 if (ENABLE_FEATURE_SUID)
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000763 check_suid(applet_no);
Denis Vlasenko745cd172007-11-29 03:31:20 +0000764 exit(applet_main[applet_no](argc, argv));
Denis Vlasenko724d1962007-10-10 14:41:07 +0000765}
766
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000767void FAST_FUNC run_applet_and_exit(const char *name, char **argv)
Denis Vlasenko724d1962007-10-10 14:41:07 +0000768{
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000769 int applet = find_applet_by_name(name);
770 if (applet >= 0)
771 run_applet_no_and_exit(applet, argv);
Denys Vlasenko8f0af3b2010-11-29 02:55:35 +0100772 if (strncmp(name, "busybox", 7) == 0)
Denis Vlasenko724d1962007-10-10 14:41:07 +0000773 exit(busybox_main(argv));
774}
775
Denis Vlasenko10f6fb12008-04-29 00:10:27 +0000776#endif /* !defined(SINGLE_APPLET_MAIN) */
Denis Vlasenko468aea22008-04-01 14:47:57 +0000777
778
Denis Vlasenko724d1962007-10-10 14:41:07 +0000779
780#if ENABLE_BUILD_LIBBUSYBOX
Denis Vlasenko85c24712008-03-17 09:04:04 +0000781int lbb_main(char **argv)
Denis Vlasenko724d1962007-10-10 14:41:07 +0000782#else
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000783int main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko724d1962007-10-10 14:41:07 +0000784#endif
785{
Denys Vlasenko1f7c1672009-09-06 02:12:28 +0200786 /* Tweak malloc for reduced memory consumption */
787#ifndef PAGE_SIZE
788# define PAGE_SIZE (4*1024) /* guess */
789#endif
790#ifdef M_TRIM_THRESHOLD
791 /* M_TRIM_THRESHOLD is the maximum amount of freed top-most memory
792 * to keep before releasing to the OS
793 * Default is way too big: 256k
794 */
795 mallopt(M_TRIM_THRESHOLD, 2 * PAGE_SIZE);
796#endif
797#ifdef M_MMAP_THRESHOLD
798 /* M_MMAP_THRESHOLD is the request size threshold for using mmap()
799 * Default is too big: 256k
800 */
801 mallopt(M_MMAP_THRESHOLD, 8 * PAGE_SIZE - 256);
802#endif
803
Denis Vlasenko724d1962007-10-10 14:41:07 +0000804#if !BB_MMU
805 /* NOMMU re-exec trick sets high-order bit in first byte of name */
806 if (argv[0][0] & 0x80) {
807 re_execed = 1;
808 argv[0][0] &= 0x7f;
809 }
810#endif
Denys Vlasenko8f0af3b2010-11-29 02:55:35 +0100811
812#if defined(SINGLE_APPLET_MAIN)
813 /* Only one applet is selected in .config */
Denys Vlasenko0ab45da2010-11-29 03:21:55 +0100814 if (argv[1] && strncmp(argv[0], "busybox", 7) == 0) {
Denys Vlasenko8f0af3b2010-11-29 02:55:35 +0100815 /* "busybox <applet> <params>" should still work as expected */
816 argv++;
817 }
818 /* applet_names in this case is just "applet\0\0" */
819 lbb_prepare(applet_names IF_FEATURE_INDIVIDUAL(, argv));
820 return SINGLE_APPLET_MAIN(argc, argv);
821#else
822 lbb_prepare("busybox" IF_FEATURE_INDIVIDUAL(, argv));
823
Denis Vlasenko724d1962007-10-10 14:41:07 +0000824 applet_name = argv[0];
825 if (applet_name[0] == '-')
826 applet_name++;
827 applet_name = bb_basename(applet_name);
828
829 parse_config_file(); /* ...maybe, if FEATURE_SUID_CONFIG */
830
831 run_applet_and_exit(applet_name, argv);
Denis Vlasenko79cedcb2008-04-08 21:13:28 +0000832
833 /*bb_error_msg_and_die("applet not found"); - sucks in printf */
834 full_write2_str(applet_name);
835 full_write2_str(": applet not found\n");
836 xfunc_die();
Denis Vlasenko468aea22008-04-01 14:47:57 +0000837#endif
Denis Vlasenko724d1962007-10-10 14:41:07 +0000838}