1 /* vi: set sw=4 ts=4: */
5 * Copyright (C) tons of folks. Tracking down who wrote what
6 * isn't something I'm going to worry about... If you wrote something
7 * here, please feel free to acknowledge your work.
9 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
10 * Permission has been granted to redistribute this code under the GPL.
12 * Licensed under GPLv2 or later, see file License in this tarball for details.
22 #if ENABLE_SHOW_USAGE && !ENABLE_FEATURE_COMPRESS_USAGE
23 static const char usage_messages[] =
30 #define usage_messages 0
31 #endif /* ENABLE_SHOW_USAGE */
38 static struct BB_applet *applet_using;
40 /* The -1 arises because of the {0,NULL,0,-1} entry above. */
41 const size_t NUM_APPLETS = (sizeof (applets) / sizeof (struct BB_applet) - 1);
44 #ifdef CONFIG_FEATURE_SUID_CONFIG
51 #define CONFIG_FILE "/etc/busybox.conf"
53 /* applets [] is const, so we have to define this "override" structure */
54 static struct BB_suid_config
56 struct BB_applet *m_applet;
62 struct BB_suid_config *m_next;
65 static int suid_cfg_readable;
67 /* check if u is member of group g */
68 static int ingroup (uid_t u, gid_t g)
70 struct group *grp = getgrgid (g);
75 for (mem = grp->gr_mem; *mem; mem++) {
76 struct passwd *pwd = getpwnam (*mem);
78 if (pwd && (pwd->pw_uid == u))
85 /* This should probably be a libbb routine. In that case,
86 * I'd probably rename it to something like bb_trimmed_slice.
88 static char *get_trimmed_slice(char *s, char *e)
90 /* First, consider the value at e to be nul and back up until we
91 * reach a non-space char. Set the char after that (possibly at
92 * the original e) to nul. */
100 /* Next, advance past all leading space and return a ptr to the
101 * first non-space char; possibly the terminating nul. */
102 return (char *) bb_skip_whitespace(s);
106 #define parse_error(x) { err=x; goto pe_label; }
108 /* Don't depend on the tools to combine strings. */
109 static const char config_file[] = CONFIG_FILE;
111 /* There are 4 chars + 1 nul for each of user/group/other. */
112 static const char mode_chars[] = "Ssx-\0Ssx-\0Ttx-";
114 /* We don't supply a value for the nul, so an index adjustment is
115 * necessary below. Also, we use unsigned short here to save some
116 * space even though these are really mode_t values. */
117 static const unsigned short mode_mask[] = {
118 /* SST sst xxx --- */
119 S_ISUID, S_ISUID|S_IXUSR, S_IXUSR, 0, /* user */
120 S_ISGID, S_ISGID|S_IXGRP, S_IXGRP, 0, /* group */
121 0, S_IXOTH, S_IXOTH, 0 /* other */
124 static void parse_config_file(void)
126 struct BB_suid_config *sct_head;
127 struct BB_suid_config *sct;
128 struct BB_applet *applet;
137 assert(!suid_config); /* Should be set to NULL by bss init. */
139 if ((stat(config_file, &st) != 0) /* No config file? */
140 || !S_ISREG(st.st_mode) /* Not a regular file? */
141 || (st.st_uid != 0) /* Not owned by root? */
142 || (st.st_mode & (S_IWGRP | S_IWOTH)) /* Writable by non-root? */
143 || !(f = fopen(config_file, "r")) /* Can not open? */
148 suid_cfg_readable = 1;
155 if (!fgets(s, sizeof(buffer), f)) { /* Are we done? */
156 if (ferror(f)) { /* Make sure it wasn't a read error. */
157 parse_error("reading");
160 suid_config = sct_head; /* Success, so set the pointer. */
164 lc++; /* Got a (partial) line. */
166 /* If a line is too long for our buffer, we consider it an error.
167 * The following test does mistreat one corner case though.
168 * If the final line of the file does not end with a newline and
169 * yet exactly fills the buffer, it will be treated as too long
170 * even though there isn't really a problem. But it isn't really
171 * worth adding code to deal with such an unlikely situation, and
172 * we do err on the side of caution. Besides, the line would be
173 * too long if it did end with a newline. */
174 if (!strchr(s, '\n') && !feof(f)) {
175 parse_error("line too long");
178 /* Trim leading and trailing whitespace, ignoring comments, and
179 * check if the resulting string is empty. */
180 if (!*(s = get_trimmed_slice(s, strchrnul(s, '#')))) {
184 /* Check for a section header. */
187 /* Unlike the old code, we ignore leading and trailing
188 * whitespace for the section name. We also require that
189 * there are no stray characters after the closing bracket. */
190 if (!(e = strchr(s, ']')) /* Missing right bracket? */
191 || e[1] /* Trailing characters? */
192 || !*(s = get_trimmed_slice(s+1, e)) /* Missing name? */
194 parse_error("section header");
196 /* Right now we only have one section so just check it.
197 * If more sections are added in the future, please don't
198 * resort to cascading ifs with multiple strcasecmp calls.
199 * That kind of bloated code is all too common. A loop
200 * and a string table would be a better choice unless the
201 * number of sections is very small. */
202 if (strcasecmp(s, "SUID") == 0) {
206 section = -1; /* Unknown section so set to skip. */
210 /* Process sections. */
212 if (section == 1) { /* SUID */
213 /* Since we trimmed leading and trailing space above, we're
214 * now looking for strings of the form
215 * <key>[::space::]*=[::space::]*<value>
216 * where both key and value could contain inner whitespace. */
218 /* First get the key (an applet name in our case). */
219 if (!!(e = strchr(s, '='))) {
220 s = get_trimmed_slice(s, e);
222 if (!e || !*s) { /* Missing '=' or empty key. */
223 parse_error("keyword");
226 /* Ok, we have an applet name. Process the rhs if this
227 * applet is currently built in and ignore it otherwise.
228 * Note: This can hide config file bugs which only pop
229 * up when the busybox configuration is changed. */
230 if ((applet = find_applet_by_name(s))) {
231 /* Note: We currently don't check for duplicates!
232 * The last config line for each applet will be the
233 * one used since we insert at the head of the list.
234 * I suppose this could be considered a feature. */
235 sct = xmalloc(sizeof(struct BB_suid_config));
236 sct->m_applet = applet;
238 sct->m_next = sct_head;
241 /* Get the specified mode. */
243 e = (char *) bb_skip_whitespace(e+1);
245 for (i=0 ; i < 3 ; i++) {
247 if (!*(q = strchrnul(mode_chars + 5*i, *e++))) {
250 /* Adjust by -i to account for nul. */
251 sct->m_mode |= mode_mask[(q - mode_chars) - i];
254 /* Now get the the user/group info. */
256 s = (char *) bb_skip_whitespace(e);
258 /* Note: We require whitespace between the mode and the
259 * user/group info. */
260 if ((s == e) || !(e = strchr(s, '.'))) {
261 parse_error("<uid>.<gid>");
265 /* We can't use get_ug_id here since it would exit()
266 * if a uid or gid was not found. Oh well... */
270 sct->m_uid = strtoul(s, &e2, 10);
271 if (*e2 || (s == e2)) {
273 if (!(pwd = getpwnam(s))) {
276 sct->m_uid = pwd->pw_uid;
279 sct->m_gid = strtoul(e, &e2, 10);
280 if (*e2 || (e == e2)) {
282 if (!(grp = getgrnam(e))) {
283 parse_error("group");
285 sct->m_gid = grp->gr_gid;
292 /* Unknown sections are ignored. */
294 /* Encountering configuration lines prior to seeing a
295 * section header is treated as an error. This is how
296 * the old code worked, but it may not be desirable.
297 * We may want to simply ignore such lines in case they
298 * are used in some future version of busybox. */
300 parse_error("keyword outside section");
306 fprintf(stderr, "Parse error in %s, line %d: %s\n",
307 config_file, lc, err);
310 /* Release any allocated memory before returning. */
312 sct = sct_head->m_next;
320 #define parse_config_file()
321 #endif /* CONFIG_FEATURE_SUID_CONFIG */
323 #ifdef CONFIG_FEATURE_SUID
324 static void check_suid (struct BB_applet *applet)
326 uid_t ruid = getuid (); /* real [ug]id */
327 uid_t rgid = getgid ();
329 #ifdef CONFIG_FEATURE_SUID_CONFIG
330 if (suid_cfg_readable) {
331 struct BB_suid_config *sct;
333 for (sct = suid_config; sct; sct = sct->m_next) {
334 if (sct->m_applet == applet)
338 mode_t m = sct->m_mode;
340 if (sct->m_uid == ruid) /* same uid */
342 else if ((sct->m_gid == rgid) || ingroup (ruid, sct->m_gid)) /* same group / in group */
345 if (!(m & S_IXOTH)) /* is x bit not set ? */
346 bb_error_msg_and_die ("You have no permission to run this applet!");
348 if ((sct->m_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) { /* *both* have to be set for sgid */
349 if (setegid (sct->m_gid))
351 ("BusyBox binary has insufficient rights to set proper GID for applet!");
353 setgid (rgid); /* no sgid -> drop */
355 if (sct->m_mode & S_ISUID) {
356 if (seteuid (sct->m_uid))
358 ("BusyBox binary has insufficient rights to set proper UID for applet!");
360 setuid (ruid); /* no suid -> drop */
362 /* default: drop all privileges */
368 #ifndef CONFIG_FEATURE_SUID_CONFIG_QUIET
369 static int onetime = 0;
373 fprintf (stderr, "Using fallback suid method\n");
379 if (applet->need_suid == _BB_SUID_ALWAYS) {
381 bb_error_msg_and_die ("This applet requires root privileges!");
382 } else if (applet->need_suid == _BB_SUID_NEVER) {
383 setgid (rgid); /* drop all privileges */
388 #define check_suid(x)
389 #endif /* CONFIG_FEATURE_SUID */
393 #if ENABLE_FEATURE_COMPRESS_USAGE
395 #include "usage_compressed.h"
396 #include "unarchive.h"
398 static const char *unpack_usage_messages(void)
400 int input[2], output[2], pid;
403 if(pipe(input) < 0 || pipe(output) < 0)
413 uncompressStream(input[0], output[1]);
425 bb_full_write(input[1], packed_usage, sizeof(packed_usage));
431 buf = xmalloc(SIZEOF_usage_messages);
432 bb_full_read(output[0], buf, SIZEOF_usage_messages);
437 #define unpack_usage_messages() usage_messages
438 #endif /* ENABLE_FEATURE_COMPRESS_USAGE */
440 void bb_show_usage (void)
442 if (ENABLE_SHOW_USAGE) {
443 const char *format_string;
444 const char *usage_string = unpack_usage_messages();
447 for (i = applet_using - applets; i > 0;)
448 if (!*usage_string++) --i;
450 format_string = "%s\n\nUsage: %s %s\n\n";
451 if (*usage_string == '\b')
452 format_string = "%s\n\nNo help available.\n\n";
453 fprintf (stderr, format_string, bb_msg_full_version,
454 applet_using->name, usage_string);
457 exit (bb_default_error_retval);
460 static int applet_name_compare (const void *x, const void *y)
462 const char *name = x;
463 const struct BB_applet *applet = y;
465 return strcmp (name, applet->name);
468 extern const size_t NUM_APPLETS;
470 struct BB_applet *find_applet_by_name (const char *name)
472 return bsearch (name, applets, NUM_APPLETS, sizeof (struct BB_applet),
473 applet_name_compare);
476 void run_applet_by_name (const char *name, int argc, char **argv)
478 if(ENABLE_FEATURE_SUID_CONFIG) parse_config_file ();
480 if(!strncmp(name, "busybox", 7)) busybox_main(argc, argv);
481 /* Do a binary search to find the applet entry given the name. */
482 applet_using = find_applet_by_name(name);
484 bb_applet_name = applet_using->name;
485 if(argc==2 && !strcmp(argv[1], "--help")) bb_show_usage ();
486 if(ENABLE_FEATURE_SUID) check_suid (applet_using);
487 exit ((*(applet_using->main)) (argc, argv));