blob: ebd1ff31329fcd155f696f7e979500d8a60f7444 [file] [log] [blame]
Eric Andersenaad1a882001-03-16 22:47:14 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
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.
8 *
Glenn L McGrath2faee7b2003-05-26 14:09:12 +00009 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
Eric Andersenaad1a882001-03-16 22:47:14 +000010 * Permission has been granted to redistribute this code under the GPL.
11 *
Rob Landley73f54702006-05-01 00:53:40 +000012 * Licensed under GPLv2 or later, see file License in this tarball for details.
Eric Andersenaad1a882001-03-16 22:47:14 +000013 */
14
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000015#include "busybox.h"
Manuel Novoa III 31b98dd2004-02-01 10:03:05 +000016#include <assert.h>
Eric Andersenaad1a882001-03-16 22:47:14 +000017
Denis Vlasenko07159f02006-11-09 00:00:12 +000018/* Apparently uclibc defines __GLIBC__ (compat trick?). Oh well. */
19#if ENABLE_STATIC && defined(__GLIBC__) && !defined(__UCLIBC__)
Denis Vlasenkodf518922006-10-20 13:42:57 +000020#warning Static linking against glibc produces buggy executables
Denis Vlasenko067e3f02006-11-10 23:25:53 +000021#warning (glibc does not cope well with ld --gc-sections).
Denis Vlasenkoafea46b2006-10-29 19:37:13 +000022#warning See sources.redhat.com/bugzilla/show_bug.cgi?id=3400
Denis Vlasenko83e5d6f2006-12-18 21:49:06 +000023#warning Note that glibc is unsuitable for static linking anyway.
24#warning If you still want to do it, remove -Wl,--gc-sections
25#warning from top-level Makefile and remove this warning.
Denis Vlasenkodf518922006-10-20 13:42:57 +000026#endif
27
Rob Landley7e21d5f2006-04-27 23:34:46 +000028#if ENABLE_SHOW_USAGE && !ENABLE_FEATURE_COMPRESS_USAGE
29static const char usage_messages[] =
Eric Andersen674b08a2004-04-06 14:28:35 +000030#define MAKE_USAGE
31#include "usage.h"
Eric Andersen674b08a2004-04-06 14:28:35 +000032#include "applets.h"
Eric Andersen674b08a2004-04-06 14:28:35 +000033;
Eric Andersen674b08a2004-04-06 14:28:35 +000034#undef MAKE_USAGE
Rob Landley73f54702006-05-01 00:53:40 +000035#else
36#define usage_messages 0
Bernhard Reutner-Fischer81901a02006-03-31 18:43:55 +000037#endif /* ENABLE_SHOW_USAGE */
Rob Landley7e21d5f2006-04-27 23:34:46 +000038
Eric Andersen2ccfef22001-03-19 19:30:24 +000039#undef APPLET
40#undef APPLET_NOUSAGE
41#undef PROTOTYPES
42#include "applets.h"
43
Glenn L McGrath7fc504c2004-02-22 11:13:28 +000044static struct BB_applet *applet_using;
Eric Andersenaad1a882001-03-16 22:47:14 +000045
Eric Andersen2ccfef22001-03-19 19:30:24 +000046/* The -1 arises because of the {0,NULL,0,-1} entry above. */
Denis Vlasenko0ee39992006-12-24 15:23:28 +000047const unsigned short NUM_APPLETS = (sizeof (applets) / sizeof (struct BB_applet) - 1);
Eric Andersen2ccfef22001-03-19 19:30:24 +000048
Robert Grieblc9aca452002-06-04 20:06:25 +000049
Robert Grieblc9aca452002-06-04 20:06:25 +000050#ifdef CONFIG_FEATURE_SUID_CONFIG
51
Robert Grieblc9aca452002-06-04 20:06:25 +000052#include <ctype.h>
Robert Grieblc9aca452002-06-04 20:06:25 +000053
Robert Grieblc9aca452002-06-04 20:06:25 +000054#define CONFIG_FILE "/etc/busybox.conf"
55
Glenn L McGrathb37367a2002-08-22 13:12:40 +000056/* applets [] is const, so we have to define this "override" structure */
Bernhard Reutner-Fischer6973abc2005-10-28 09:45:07 +000057static struct BB_suid_config
Glenn L McGrath2faee7b2003-05-26 14:09:12 +000058{
Denis Vlasenko01a74f92006-09-23 16:34:39 +000059 struct BB_applet *m_applet;
Robert Grieblc9aca452002-06-04 20:06:25 +000060
Denis Vlasenko01a74f92006-09-23 16:34:39 +000061 uid_t m_uid;
62 gid_t m_gid;
63 mode_t m_mode;
Glenn L McGrathb37367a2002-08-22 13:12:40 +000064
Denis Vlasenko01a74f92006-09-23 16:34:39 +000065 struct BB_suid_config *m_next;
Bernhard Reutner-Fischer6973abc2005-10-28 09:45:07 +000066} *suid_config;
Robert Grieblc9aca452002-06-04 20:06:25 +000067
Manuel Novoa III 7b565a02004-02-17 10:16:21 +000068static int suid_cfg_readable;
Robert Grieblc9aca452002-06-04 20:06:25 +000069
Glenn L McGrathb37367a2002-08-22 13:12:40 +000070/* check if u is member of group g */
Denis Vlasenko01a74f92006-09-23 16:34:39 +000071static int ingroup(uid_t u, gid_t g)
Robert Grieblc9aca452002-06-04 20:06:25 +000072{
Denis Vlasenko01a74f92006-09-23 16:34:39 +000073 struct group *grp = getgrgid(g);
Glenn L McGrathb37367a2002-08-22 13:12:40 +000074
Denis Vlasenko01a74f92006-09-23 16:34:39 +000075 if (grp) {
76 char **mem;
Glenn L McGrathb37367a2002-08-22 13:12:40 +000077
Denis Vlasenko01a74f92006-09-23 16:34:39 +000078 for (mem = grp->gr_mem; *mem; mem++) {
79 struct passwd *pwd = getpwnam(*mem);
Glenn L McGrathb37367a2002-08-22 13:12:40 +000080
Denis Vlasenko01a74f92006-09-23 16:34:39 +000081 if (pwd && (pwd->pw_uid == u))
82 return 1;
83 }
Robert Grieblc9aca452002-06-04 20:06:25 +000084 }
Denis Vlasenko01a74f92006-09-23 16:34:39 +000085 return 0;
Robert Grieblc9aca452002-06-04 20:06:25 +000086}
87
Manuel Novoa III 31b98dd2004-02-01 10:03:05 +000088/* This should probably be a libbb routine. In that case,
89 * I'd probably rename it to something like bb_trimmed_slice.
90 */
91static char *get_trimmed_slice(char *s, char *e)
92{
93 /* First, consider the value at e to be nul and back up until we
94 * reach a non-space char. Set the char after that (possibly at
95 * the original e) to nul. */
96 while (e-- > s) {
97 if (!isspace(*e)) {
98 break;
99 }
100 }
101 e[1] = 0;
102
103 /* Next, advance past all leading space and return a ptr to the
104 * first non-space char; possibly the terminating nul. */
Rob Landleyea224be2006-06-18 20:20:07 +0000105 return skip_whitespace(s);
Manuel Novoa III 31b98dd2004-02-01 10:03:05 +0000106}
107
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000108
109#define parse_error(x) { err=x; goto pe_label; }
110
Manuel Novoa III 31b98dd2004-02-01 10:03:05 +0000111/* Don't depend on the tools to combine strings. */
112static const char config_file[] = CONFIG_FILE;
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000113
Manuel Novoa III 31b98dd2004-02-01 10:03:05 +0000114/* There are 4 chars + 1 nul for each of user/group/other. */
115static const char mode_chars[] = "Ssx-\0Ssx-\0Ttx-";
116
117/* We don't supply a value for the nul, so an index adjustment is
118 * necessary below. Also, we use unsigned short here to save some
119 * space even though these are really mode_t values. */
120static const unsigned short mode_mask[] = {
121 /* SST sst xxx --- */
122 S_ISUID, S_ISUID|S_IXUSR, S_IXUSR, 0, /* user */
123 S_ISGID, S_ISGID|S_IXGRP, S_IXGRP, 0, /* group */
124 0, S_IXOTH, S_IXOTH, 0 /* other */
125};
126
127static void parse_config_file(void)
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000128{
Manuel Novoa III 31b98dd2004-02-01 10:03:05 +0000129 struct BB_suid_config *sct_head;
130 struct BB_suid_config *sct;
131 struct BB_applet *applet;
132 FILE *f;
133 char *err;
134 char *s;
135 char *e;
136 int i, lc, section;
137 char buffer[256];
138 struct stat st;
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000139
Manuel Novoa III 31b98dd2004-02-01 10:03:05 +0000140 assert(!suid_config); /* Should be set to NULL by bss init. */
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000141
Manuel Novoa III 31b98dd2004-02-01 10:03:05 +0000142 if ((stat(config_file, &st) != 0) /* No config file? */
143 || !S_ISREG(st.st_mode) /* Not a regular file? */
144 || (st.st_uid != 0) /* Not owned by root? */
145 || (st.st_mode & (S_IWGRP | S_IWOTH)) /* Writable by non-root? */
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000146 || !(f = fopen(config_file, "r")) /* Cannot open? */
Manuel Novoa III 31b98dd2004-02-01 10:03:05 +0000147 ) {
148 return;
Robert Grieblc9aca452002-06-04 20:06:25 +0000149 }
Glenn L McGrathb37367a2002-08-22 13:12:40 +0000150
Manuel Novoa III 7b565a02004-02-17 10:16:21 +0000151 suid_cfg_readable = 1;
Manuel Novoa III 31b98dd2004-02-01 10:03:05 +0000152 sct_head = NULL;
153 section = lc = 0;
Robert Grieblc9aca452002-06-04 20:06:25 +0000154
Manuel Novoa III 31b98dd2004-02-01 10:03:05 +0000155 do {
156 s = buffer;
157
158 if (!fgets(s, sizeof(buffer), f)) { /* Are we done? */
159 if (ferror(f)) { /* Make sure it wasn't a read error. */
160 parse_error("reading");
161 }
162 fclose(f);
163 suid_config = sct_head; /* Success, so set the pointer. */
164 return;
165 }
166
167 lc++; /* Got a (partial) line. */
168
169 /* If a line is too long for our buffer, we consider it an error.
170 * The following test does mistreat one corner case though.
171 * If the final line of the file does not end with a newline and
172 * yet exactly fills the buffer, it will be treated as too long
173 * even though there isn't really a problem. But it isn't really
174 * worth adding code to deal with such an unlikely situation, and
175 * we do err on the side of caution. Besides, the line would be
176 * too long if it did end with a newline. */
177 if (!strchr(s, '\n') && !feof(f)) {
178 parse_error("line too long");
179 }
180
181 /* Trim leading and trailing whitespace, ignoring comments, and
182 * check if the resulting string is empty. */
183 if (!*(s = get_trimmed_slice(s, strchrnul(s, '#')))) {
184 continue;
185 }
186
187 /* Check for a section header. */
188
189 if (*s == '[') {
190 /* Unlike the old code, we ignore leading and trailing
191 * whitespace for the section name. We also require that
192 * there are no stray characters after the closing bracket. */
193 if (!(e = strchr(s, ']')) /* Missing right bracket? */
194 || e[1] /* Trailing characters? */
195 || !*(s = get_trimmed_slice(s+1, e)) /* Missing name? */
196 ) {
197 parse_error("section header");
198 }
199 /* Right now we only have one section so just check it.
200 * If more sections are added in the future, please don't
201 * resort to cascading ifs with multiple strcasecmp calls.
202 * That kind of bloated code is all too common. A loop
203 * and a string table would be a better choice unless the
204 * number of sections is very small. */
205 if (strcasecmp(s, "SUID") == 0) {
206 section = 1;
207 continue;
208 }
209 section = -1; /* Unknown section so set to skip. */
210 continue;
211 }
212
213 /* Process sections. */
214
215 if (section == 1) { /* SUID */
216 /* Since we trimmed leading and trailing space above, we're
217 * now looking for strings of the form
218 * <key>[::space::]*=[::space::]*<value>
219 * where both key and value could contain inner whitespace. */
220
221 /* First get the key (an applet name in our case). */
222 if (!!(e = strchr(s, '='))) {
223 s = get_trimmed_slice(s, e);
224 }
225 if (!e || !*s) { /* Missing '=' or empty key. */
226 parse_error("keyword");
227 }
228
229 /* Ok, we have an applet name. Process the rhs if this
230 * applet is currently built in and ignore it otherwise.
231 * Note: This can hide config file bugs which only pop
232 * up when the busybox configuration is changed. */
233 if ((applet = find_applet_by_name(s))) {
234 /* Note: We currently don't check for duplicates!
235 * The last config line for each applet will be the
236 * one used since we insert at the head of the list.
237 * I suppose this could be considered a feature. */
238 sct = xmalloc(sizeof(struct BB_suid_config));
239 sct->m_applet = applet;
240 sct->m_mode = 0;
241 sct->m_next = sct_head;
242 sct_head = sct;
243
244 /* Get the specified mode. */
245
Rob Landleyea224be2006-06-18 20:20:07 +0000246 e = skip_whitespace(e+1);
Manuel Novoa III 31b98dd2004-02-01 10:03:05 +0000247
248 for (i=0 ; i < 3 ; i++) {
249 const char *q;
250 if (!*(q = strchrnul(mode_chars + 5*i, *e++))) {
251 parse_error("mode");
252 }
253 /* Adjust by -i to account for nul. */
254 sct->m_mode |= mode_mask[(q - mode_chars) - i];
255 }
256
257 /* Now get the the user/group info. */
Bernhard Reutner-Fischer7ca61b62006-01-15 14:04:57 +0000258
Rob Landleyea224be2006-06-18 20:20:07 +0000259 s = skip_whitespace(e);
Manuel Novoa III 31b98dd2004-02-01 10:03:05 +0000260
261 /* Note: We require whitespace between the mode and the
262 * user/group info. */
263 if ((s == e) || !(e = strchr(s, '.'))) {
264 parse_error("<uid>.<gid>");
265 }
266 *e++ = 0;
267
268 /* We can't use get_ug_id here since it would exit()
269 * if a uid or gid was not found. Oh well... */
270 {
271 char *e2;
272
273 sct->m_uid = strtoul(s, &e2, 10);
274 if (*e2 || (s == e2)) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000275 struct passwd *pwd = getpwnam(s);
276 if (!pwd) {
Manuel Novoa III 31b98dd2004-02-01 10:03:05 +0000277 parse_error("user");
278 }
279 sct->m_uid = pwd->pw_uid;
280 }
281
282 sct->m_gid = strtoul(e, &e2, 10);
283 if (*e2 || (e == e2)) {
284 struct group *grp;
285 if (!(grp = getgrnam(e))) {
286 parse_error("group");
287 }
288 sct->m_gid = grp->gr_gid;
289 }
290 }
291 }
292 continue;
293 }
294
295 /* Unknown sections are ignored. */
296
297 /* Encountering configuration lines prior to seeing a
298 * section header is treated as an error. This is how
Eric Andersenaff114c2004-04-14 17:51:38 +0000299 * the old code worked, but it may not be desirable.
Manuel Novoa III 31b98dd2004-02-01 10:03:05 +0000300 * We may want to simply ignore such lines in case they
301 * are used in some future version of busybox. */
302 if (!section) {
303 parse_error("keyword outside section");
304 }
305
306 } while (1);
307
308 pe_label:
309 fprintf(stderr, "Parse error in %s, line %d: %s\n",
310 config_file, lc, err);
311
312 fclose(f);
313 /* Release any allocated memory before returning. */
314 while (sct_head) {
315 sct = sct_head->m_next;
316 free(sct_head);
317 sct_head = sct;
318 }
319 return;
Robert Grieblc9aca452002-06-04 20:06:25 +0000320}
321
Rob Landley8a7a6782005-09-05 04:13:33 +0000322#else
Rob Landleycc59aae2005-12-07 23:17:28 +0000323#define parse_config_file()
Rob Landley8a7a6782005-09-05 04:13:33 +0000324#endif /* CONFIG_FEATURE_SUID_CONFIG */
325
326#ifdef CONFIG_FEATURE_SUID
Denis Vlasenko13c5a682006-10-16 22:39:51 +0000327static void check_suid(struct BB_applet *applet)
Rob Landley8a7a6782005-09-05 04:13:33 +0000328{
Denis Vlasenko13c5a682006-10-16 22:39:51 +0000329 uid_t ruid = getuid(); /* real [ug]id */
330 uid_t rgid = getgid();
Rob Landley8a7a6782005-09-05 04:13:33 +0000331
332#ifdef CONFIG_FEATURE_SUID_CONFIG
Denis Vlasenko01a74f92006-09-23 16:34:39 +0000333 if (suid_cfg_readable) {
334 struct BB_suid_config *sct;
Rob Landley8a7a6782005-09-05 04:13:33 +0000335
Denis Vlasenko01a74f92006-09-23 16:34:39 +0000336 for (sct = suid_config; sct; sct = sct->m_next) {
337 if (sct->m_applet == applet)
338 break;
339 }
340 if (sct) {
341 mode_t m = sct->m_mode;
Rob Landley8a7a6782005-09-05 04:13:33 +0000342
Denis Vlasenko01a74f92006-09-23 16:34:39 +0000343 if (sct->m_uid == ruid) /* same uid */
344 m >>= 6;
Denis Vlasenko13c5a682006-10-16 22:39:51 +0000345 else if ((sct->m_gid == rgid) || ingroup(ruid, sct->m_gid)) /* same group / in group */
Denis Vlasenko01a74f92006-09-23 16:34:39 +0000346 m >>= 3;
Rob Landley8a7a6782005-09-05 04:13:33 +0000347
Denis Vlasenko01a74f92006-09-23 16:34:39 +0000348 if (!(m & S_IXOTH)) /* is x bit not set ? */
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000349 bb_error_msg_and_die("you have no permission to run this applet!");
Rob Landley8a7a6782005-09-05 04:13:33 +0000350
Denis Vlasenko01a74f92006-09-23 16:34:39 +0000351 if ((sct->m_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) { /* *both* have to be set for sgid */
352 xsetgid(sct->m_gid);
353 } else xsetgid(rgid); /* no sgid -> drop */
Rob Landley8a7a6782005-09-05 04:13:33 +0000354
Denis Vlasenko01a74f92006-09-23 16:34:39 +0000355 if (sct->m_mode & S_ISUID) xsetuid(sct->m_uid);
356 else xsetuid(ruid); /* no suid -> drop */
357 } else {
358 /* default: drop all privileges */
359 xsetgid(rgid);
360 xsetuid(ruid);
361 }
362 return;
Rob Landley8a7a6782005-09-05 04:13:33 +0000363 } else {
Rob Landley8a7a6782005-09-05 04:13:33 +0000364#ifndef CONFIG_FEATURE_SUID_CONFIG_QUIET
Denis Vlasenko01a74f92006-09-23 16:34:39 +0000365 static int onetime = 0;
Rob Landley8a7a6782005-09-05 04:13:33 +0000366
Denis Vlasenko01a74f92006-09-23 16:34:39 +0000367 if (!onetime) {
368 onetime = 1;
Denis Vlasenko13c5a682006-10-16 22:39:51 +0000369 fprintf(stderr, "Using fallback suid method\n");
Denis Vlasenko01a74f92006-09-23 16:34:39 +0000370 }
371#endif
Rob Landley8a7a6782005-09-05 04:13:33 +0000372 }
373#endif
Robert Grieblc9aca452002-06-04 20:06:25 +0000374
Denis Vlasenko01a74f92006-09-23 16:34:39 +0000375 if (applet->need_suid == _BB_SUID_ALWAYS) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000376 if (geteuid()) bb_error_msg_and_die("applet requires root privileges!");
Denis Vlasenko01a74f92006-09-23 16:34:39 +0000377 } else if (applet->need_suid == _BB_SUID_NEVER) {
378 xsetgid(rgid); /* drop all privileges */
379 xsetuid(ruid);
380 }
Rob Landley8a7a6782005-09-05 04:13:33 +0000381}
382#else
383#define check_suid(x)
384#endif /* CONFIG_FEATURE_SUID */
385
386
387
Rob Landleye1a0f532006-07-26 15:38:46 +0000388#ifdef CONFIG_FEATURE_COMPRESS_USAGE
Rob Landley8a7a6782005-09-05 04:13:33 +0000389
Rob Landley7e21d5f2006-04-27 23:34:46 +0000390#include "usage_compressed.h"
391#include "unarchive.h"
392
393static const char *unpack_usage_messages(void)
394{
395 int input[2], output[2], pid;
396 char *buf;
397
398 if(pipe(input) < 0 || pipe(output) < 0)
399 exit(1);
400
401 pid = fork();
402 switch (pid) {
403 case -1: /* error */
404 exit(1);
405 case 0: /* child */
406 close(input[1]);
407 close(output[0]);
408 uncompressStream(input[0], output[1]);
409 exit(0);
410 }
411 /* parent */
412
413 close(input[0]);
414 close(output[1]);
415 pid = fork();
416 switch (pid) {
417 case -1: /* error */
418 exit(1);
419 case 0: /* child */
Rob Landley53437472006-07-16 08:14:35 +0000420 full_write(input[1], packed_usage, sizeof(packed_usage));
Rob Landley7e21d5f2006-04-27 23:34:46 +0000421 exit(0);
422 }
423 /* parent */
424 close(input[1]);
425
426 buf = xmalloc(SIZEOF_usage_messages);
Rob Landley53437472006-07-16 08:14:35 +0000427 full_read(output[0], buf, SIZEOF_usage_messages);
Rob Landley7e21d5f2006-04-27 23:34:46 +0000428 return buf;
429}
430
431#else
Rob Landley1801e9c2006-05-03 20:19:14 +0000432#define unpack_usage_messages() usage_messages
Rob Landley7e21d5f2006-04-27 23:34:46 +0000433#endif /* ENABLE_FEATURE_COMPRESS_USAGE */
Rob Landley8a7a6782005-09-05 04:13:33 +0000434
Denis Vlasenko01a74f92006-09-23 16:34:39 +0000435void bb_show_usage(void)
Rob Landley8a7a6782005-09-05 04:13:33 +0000436{
Rob Landley7e21d5f2006-04-27 23:34:46 +0000437 if (ENABLE_SHOW_USAGE) {
438 const char *format_string;
439 const char *usage_string = unpack_usage_messages();
440 int i;
Rob Landley8a7a6782005-09-05 04:13:33 +0000441
Rob Landley7e21d5f2006-04-27 23:34:46 +0000442 for (i = applet_using - applets; i > 0;)
443 if (!*usage_string++) --i;
444
445 format_string = "%s\n\nUsage: %s %s\n\n";
446 if (*usage_string == '\b')
447 format_string = "%s\n\nNo help available.\n\n";
Denis Vlasenko92258542006-11-01 10:25:35 +0000448 fprintf(stderr, format_string, bb_msg_full_version,
Rob Landley7e21d5f2006-04-27 23:34:46 +0000449 applet_using->name, usage_string);
Rob Landley8a7a6782005-09-05 04:13:33 +0000450 }
Rob Landley8a7a6782005-09-05 04:13:33 +0000451
Denis Vlasenko40920822006-10-03 20:28:06 +0000452 exit(xfunc_error_retval);
Rob Landley8a7a6782005-09-05 04:13:33 +0000453}
454
Rob Landley53437472006-07-16 08:14:35 +0000455static int applet_name_compare(const void *name, const void *vapplet)
Rob Landley8a7a6782005-09-05 04:13:33 +0000456{
Denis Vlasenko01a74f92006-09-23 16:34:39 +0000457 const struct BB_applet *applet = vapplet;
Rob Landley8a7a6782005-09-05 04:13:33 +0000458
Denis Vlasenko01a74f92006-09-23 16:34:39 +0000459 return strcmp(name, applet->name);
Rob Landley8a7a6782005-09-05 04:13:33 +0000460}
461
Rob Landley53437472006-07-16 08:14:35 +0000462struct BB_applet *find_applet_by_name(const char *name)
Rob Landley8a7a6782005-09-05 04:13:33 +0000463{
Denis Vlasenko01a74f92006-09-23 16:34:39 +0000464 return bsearch(name, applets, NUM_APPLETS, sizeof(struct BB_applet),
465 applet_name_compare);
Rob Landley8a7a6782005-09-05 04:13:33 +0000466}
467
Rob Landley53437472006-07-16 08:14:35 +0000468void run_applet_by_name(const char *name, int argc, char **argv)
Rob Landley8a7a6782005-09-05 04:13:33 +0000469{
Denis Vlasenko0ee39992006-12-24 15:23:28 +0000470 if (ENABLE_FEATURE_SUID_CONFIG)
471 parse_config_file();
Rob Landley8a7a6782005-09-05 04:13:33 +0000472
Denis Vlasenko0ee39992006-12-24 15:23:28 +0000473 if (!strncmp(name, "busybox", 7))
474 exit(busybox_main(argc, argv));
Rob Landley8a7a6782005-09-05 04:13:33 +0000475 /* Do a binary search to find the applet entry given the name. */
476 applet_using = find_applet_by_name(name);
Rob Landley53437472006-07-16 08:14:35 +0000477 if (applet_using) {
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000478 applet_name = applet_using->name;
Denis Vlasenko0ee39992006-12-24 15:23:28 +0000479 if (argc == 2 && !strcmp(argv[1], "--help"))
480 bb_show_usage();
481 if (ENABLE_FEATURE_SUID)
482 check_suid(applet_using);
483 exit(applet_using->main(argc, argv));
Rob Landley8a7a6782005-09-05 04:13:33 +0000484 }
485}