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.
20 #if ENABLE_STATIC && __GLIBC__
21 #warning Static linking against glibc produces buggy executables
22 #warning (glibc doesn't cope well with ld --gc-sections).
23 #warning See http://sources.redhat.com/bugzilla/show_bug.cgi?id=3400
24 #warning Note that glibc is utterly unsuitable for static linking anyway.
27 #if ENABLE_SHOW_USAGE && !ENABLE_FEATURE_COMPRESS_USAGE
28 static const char usage_messages[] =
35 #define usage_messages 0
36 #endif /* ENABLE_SHOW_USAGE */
43 static struct BB_applet *applet_using;
45 /* The -1 arises because of the {0,NULL,0,-1} entry above. */
46 const size_t NUM_APPLETS = (sizeof (applets) / sizeof (struct BB_applet) - 1);
49 #ifdef CONFIG_FEATURE_SUID_CONFIG
53 #define CONFIG_FILE "/etc/busybox.conf"
55 /* applets [] is const, so we have to define this "override" structure */
56 static struct BB_suid_config
58 struct BB_applet *m_applet;
64 struct BB_suid_config *m_next;
67 static int suid_cfg_readable;
69 /* check if u is member of group g */
70 static int ingroup(uid_t u, gid_t g)
72 struct group *grp = getgrgid(g);
77 for (mem = grp->gr_mem; *mem; mem++) {
78 struct passwd *pwd = getpwnam(*mem);
80 if (pwd && (pwd->pw_uid == u))
87 /* This should probably be a libbb routine. In that case,
88 * I'd probably rename it to something like bb_trimmed_slice.
90 static char *get_trimmed_slice(char *s, char *e)
92 /* First, consider the value at e to be nul and back up until we
93 * reach a non-space char. Set the char after that (possibly at
94 * the original e) to nul. */
102 /* Next, advance past all leading space and return a ptr to the
103 * first non-space char; possibly the terminating nul. */
104 return skip_whitespace(s);
108 #define parse_error(x) { err=x; goto pe_label; }
110 /* Don't depend on the tools to combine strings. */
111 static const char config_file[] = CONFIG_FILE;
113 /* There are 4 chars + 1 nul for each of user/group/other. */
114 static const char mode_chars[] = "Ssx-\0Ssx-\0Ttx-";
116 /* We don't supply a value for the nul, so an index adjustment is
117 * necessary below. Also, we use unsigned short here to save some
118 * space even though these are really mode_t values. */
119 static const unsigned short mode_mask[] = {
120 /* SST sst xxx --- */
121 S_ISUID, S_ISUID|S_IXUSR, S_IXUSR, 0, /* user */
122 S_ISGID, S_ISGID|S_IXGRP, S_IXGRP, 0, /* group */
123 0, S_IXOTH, S_IXOTH, 0 /* other */
126 static void parse_config_file(void)
128 struct BB_suid_config *sct_head;
129 struct BB_suid_config *sct;
130 struct BB_applet *applet;
139 assert(!suid_config); /* Should be set to NULL by bss init. */
141 if ((stat(config_file, &st) != 0) /* No config file? */
142 || !S_ISREG(st.st_mode) /* Not a regular file? */
143 || (st.st_uid != 0) /* Not owned by root? */
144 || (st.st_mode & (S_IWGRP | S_IWOTH)) /* Writable by non-root? */
145 || !(f = fopen(config_file, "r")) /* Cannot open? */
150 suid_cfg_readable = 1;
157 if (!fgets(s, sizeof(buffer), f)) { /* Are we done? */
158 if (ferror(f)) { /* Make sure it wasn't a read error. */
159 parse_error("reading");
162 suid_config = sct_head; /* Success, so set the pointer. */
166 lc++; /* Got a (partial) line. */
168 /* If a line is too long for our buffer, we consider it an error.
169 * The following test does mistreat one corner case though.
170 * If the final line of the file does not end with a newline and
171 * yet exactly fills the buffer, it will be treated as too long
172 * even though there isn't really a problem. But it isn't really
173 * worth adding code to deal with such an unlikely situation, and
174 * we do err on the side of caution. Besides, the line would be
175 * too long if it did end with a newline. */
176 if (!strchr(s, '\n') && !feof(f)) {
177 parse_error("line too long");
180 /* Trim leading and trailing whitespace, ignoring comments, and
181 * check if the resulting string is empty. */
182 if (!*(s = get_trimmed_slice(s, strchrnul(s, '#')))) {
186 /* Check for a section header. */
189 /* Unlike the old code, we ignore leading and trailing
190 * whitespace for the section name. We also require that
191 * there are no stray characters after the closing bracket. */
192 if (!(e = strchr(s, ']')) /* Missing right bracket? */
193 || e[1] /* Trailing characters? */
194 || !*(s = get_trimmed_slice(s+1, e)) /* Missing name? */
196 parse_error("section header");
198 /* Right now we only have one section so just check it.
199 * If more sections are added in the future, please don't
200 * resort to cascading ifs with multiple strcasecmp calls.
201 * That kind of bloated code is all too common. A loop
202 * and a string table would be a better choice unless the
203 * number of sections is very small. */
204 if (strcasecmp(s, "SUID") == 0) {
208 section = -1; /* Unknown section so set to skip. */
212 /* Process sections. */
214 if (section == 1) { /* SUID */
215 /* Since we trimmed leading and trailing space above, we're
216 * now looking for strings of the form
217 * <key>[::space::]*=[::space::]*<value>
218 * where both key and value could contain inner whitespace. */
220 /* First get the key (an applet name in our case). */
221 if (!!(e = strchr(s, '='))) {
222 s = get_trimmed_slice(s, e);
224 if (!e || !*s) { /* Missing '=' or empty key. */
225 parse_error("keyword");
228 /* Ok, we have an applet name. Process the rhs if this
229 * applet is currently built in and ignore it otherwise.
230 * Note: This can hide config file bugs which only pop
231 * up when the busybox configuration is changed. */
232 if ((applet = find_applet_by_name(s))) {
233 /* Note: We currently don't check for duplicates!
234 * The last config line for each applet will be the
235 * one used since we insert at the head of the list.
236 * I suppose this could be considered a feature. */
237 sct = xmalloc(sizeof(struct BB_suid_config));
238 sct->m_applet = applet;
240 sct->m_next = sct_head;
243 /* Get the specified mode. */
245 e = skip_whitespace(e+1);
247 for (i=0 ; i < 3 ; i++) {
249 if (!*(q = strchrnul(mode_chars + 5*i, *e++))) {
252 /* Adjust by -i to account for nul. */
253 sct->m_mode |= mode_mask[(q - mode_chars) - i];
256 /* Now get the the user/group info. */
258 s = skip_whitespace(e);
260 /* Note: We require whitespace between the mode and the
261 * user/group info. */
262 if ((s == e) || !(e = strchr(s, '.'))) {
263 parse_error("<uid>.<gid>");
267 /* We can't use get_ug_id here since it would exit()
268 * if a uid or gid was not found. Oh well... */
272 sct->m_uid = strtoul(s, &e2, 10);
273 if (*e2 || (s == e2)) {
274 struct passwd *pwd = getpwnam(s);
278 sct->m_uid = pwd->pw_uid;
281 sct->m_gid = strtoul(e, &e2, 10);
282 if (*e2 || (e == e2)) {
284 if (!(grp = getgrnam(e))) {
285 parse_error("group");
287 sct->m_gid = grp->gr_gid;
294 /* Unknown sections are ignored. */
296 /* Encountering configuration lines prior to seeing a
297 * section header is treated as an error. This is how
298 * the old code worked, but it may not be desirable.
299 * We may want to simply ignore such lines in case they
300 * are used in some future version of busybox. */
302 parse_error("keyword outside section");
308 fprintf(stderr, "Parse error in %s, line %d: %s\n",
309 config_file, lc, err);
312 /* Release any allocated memory before returning. */
314 sct = sct_head->m_next;
322 #define parse_config_file()
323 #endif /* CONFIG_FEATURE_SUID_CONFIG */
325 #ifdef CONFIG_FEATURE_SUID
326 static void check_suid(struct BB_applet *applet)
328 uid_t ruid = getuid(); /* real [ug]id */
329 uid_t rgid = getgid();
331 #ifdef CONFIG_FEATURE_SUID_CONFIG
332 if (suid_cfg_readable) {
333 struct BB_suid_config *sct;
335 for (sct = suid_config; sct; sct = sct->m_next) {
336 if (sct->m_applet == applet)
340 mode_t m = sct->m_mode;
342 if (sct->m_uid == ruid) /* same uid */
344 else if ((sct->m_gid == rgid) || ingroup(ruid, sct->m_gid)) /* same group / in group */
347 if (!(m & S_IXOTH)) /* is x bit not set ? */
348 bb_error_msg_and_die("you have no permission to run this applet!");
350 if ((sct->m_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) { /* *both* have to be set for sgid */
352 } else xsetgid(rgid); /* no sgid -> drop */
354 if (sct->m_mode & S_ISUID) xsetuid(sct->m_uid);
355 else xsetuid(ruid); /* no suid -> drop */
357 /* default: drop all privileges */
363 #ifndef CONFIG_FEATURE_SUID_CONFIG_QUIET
364 static int onetime = 0;
368 fprintf(stderr, "Using fallback suid method\n");
374 if (applet->need_suid == _BB_SUID_ALWAYS) {
375 if (geteuid()) bb_error_msg_and_die("applet requires root privileges!");
376 } else if (applet->need_suid == _BB_SUID_NEVER) {
377 xsetgid(rgid); /* drop all privileges */
382 #define check_suid(x)
383 #endif /* CONFIG_FEATURE_SUID */
387 #ifdef CONFIG_FEATURE_COMPRESS_USAGE
389 #include "usage_compressed.h"
390 #include "unarchive.h"
392 static const char *unpack_usage_messages(void)
394 int input[2], output[2], pid;
397 if(pipe(input) < 0 || pipe(output) < 0)
407 uncompressStream(input[0], output[1]);
419 full_write(input[1], packed_usage, sizeof(packed_usage));
425 buf = xmalloc(SIZEOF_usage_messages);
426 full_read(output[0], buf, SIZEOF_usage_messages);
431 #define unpack_usage_messages() usage_messages
432 #endif /* ENABLE_FEATURE_COMPRESS_USAGE */
434 void bb_show_usage(void)
436 if (ENABLE_SHOW_USAGE) {
437 const char *format_string;
438 const char *usage_string = unpack_usage_messages();
441 for (i = applet_using - applets; i > 0;)
442 if (!*usage_string++) --i;
444 format_string = "%s\n\nUsage: %s %s\n\n";
445 if (*usage_string == '\b')
446 format_string = "%s\n\nNo help available.\n\n";
447 fprintf (stderr, format_string, bb_msg_full_version,
448 applet_using->name, usage_string);
451 exit(xfunc_error_retval);
454 static int applet_name_compare(const void *name, const void *vapplet)
456 const struct BB_applet *applet = vapplet;
458 return strcmp(name, applet->name);
461 extern const size_t NUM_APPLETS;
463 struct BB_applet *find_applet_by_name(const char *name)
465 return bsearch(name, applets, NUM_APPLETS, sizeof(struct BB_applet),
466 applet_name_compare);
469 void run_applet_by_name(const char *name, int argc, char **argv)
471 if (ENABLE_FEATURE_SUID_CONFIG) parse_config_file();
473 if (!strncmp(name, "busybox", 7)) busybox_main(argc, argv);
474 /* Do a binary search to find the applet entry given the name. */
475 applet_using = find_applet_by_name(name);
477 applet_name = applet_using->name;
478 if(argc==2 && !strcmp(argv[1], "--help")) bb_show_usage();
479 if(ENABLE_FEATURE_SUID) check_suid(applet_using);
480 exit((*(applet_using->main))(argc, argv));