Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 1 | /* 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 McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 9 | * Based in part on code from sash, Copyright (c) 1999 by David I. Bell |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 10 | * Permission has been granted to redistribute this code under the GPL. |
| 11 | * |
Rob Landley | 73f5470 | 2006-05-01 00:53:40 +0000 | [diff] [blame] | 12 | * Licensed under GPLv2 or later, see file License in this tarball for details. |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 13 | */ |
| 14 | |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 15 | #include <assert.h> |
Denis Vlasenko | c7ba8b9 | 2007-02-03 17:27:14 +0000 | [diff] [blame] | 16 | #include "busybox.h" |
| 17 | |
Denis Vlasenko | 07159f0 | 2006-11-09 00:00:12 +0000 | [diff] [blame] | 18 | /* Apparently uclibc defines __GLIBC__ (compat trick?). Oh well. */ |
| 19 | #if ENABLE_STATIC && defined(__GLIBC__) && !defined(__UCLIBC__) |
Denis Vlasenko | df51892 | 2006-10-20 13:42:57 +0000 | [diff] [blame] | 20 | #warning Static linking against glibc produces buggy executables |
Denis Vlasenko | 067e3f0 | 2006-11-10 23:25:53 +0000 | [diff] [blame] | 21 | #warning (glibc does not cope well with ld --gc-sections). |
Denis Vlasenko | afea46b | 2006-10-29 19:37:13 +0000 | [diff] [blame] | 22 | #warning See sources.redhat.com/bugzilla/show_bug.cgi?id=3400 |
Denis Vlasenko | 83e5d6f | 2006-12-18 21:49:06 +0000 | [diff] [blame] | 23 | #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 Vlasenko | 4500c58 | 2007-05-18 07:37:06 +0000 | [diff] [blame] | 26 | #error Aborting compilation. |
Denis Vlasenko | df51892 | 2006-10-20 13:42:57 +0000 | [diff] [blame] | 27 | #endif |
| 28 | |
Denis Vlasenko | 32b633a | 2007-04-09 03:05:48 +0000 | [diff] [blame] | 29 | |
| 30 | /* Declare <applet>_main() */ |
| 31 | #define PROTOTYPES |
| 32 | #include "applets.h" |
| 33 | #undef PROTOTYPES |
| 34 | |
Rob Landley | 7e21d5f | 2006-04-27 23:34:46 +0000 | [diff] [blame] | 35 | #if ENABLE_SHOW_USAGE && !ENABLE_FEATURE_COMPRESS_USAGE |
Denis Vlasenko | 32b633a | 2007-04-09 03:05:48 +0000 | [diff] [blame] | 36 | /* Define usage_messages[] */ |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 37 | static const char usage_messages[] ALIGN1 = "" |
Eric Andersen | 674b08a | 2004-04-06 14:28:35 +0000 | [diff] [blame] | 38 | #define MAKE_USAGE |
| 39 | #include "usage.h" |
Eric Andersen | 674b08a | 2004-04-06 14:28:35 +0000 | [diff] [blame] | 40 | #include "applets.h" |
Eric Andersen | 674b08a | 2004-04-06 14:28:35 +0000 | [diff] [blame] | 41 | ; |
Eric Andersen | 674b08a | 2004-04-06 14:28:35 +0000 | [diff] [blame] | 42 | #undef MAKE_USAGE |
Rob Landley | 73f5470 | 2006-05-01 00:53:40 +0000 | [diff] [blame] | 43 | #else |
| 44 | #define usage_messages 0 |
Denis Vlasenko | 32b633a | 2007-04-09 03:05:48 +0000 | [diff] [blame] | 45 | #endif /* SHOW_USAGE */ |
Rob Landley | 7e21d5f | 2006-04-27 23:34:46 +0000 | [diff] [blame] | 46 | |
Denis Vlasenko | 335b63d | 2007-04-10 21:38:30 +0000 | [diff] [blame] | 47 | /* Define struct bb_applet applets[] */ |
Eric Andersen | 2ccfef2 | 2001-03-19 19:30:24 +0000 | [diff] [blame] | 48 | #include "applets.h" |
Denis Vlasenko | 1b6fa4c | 2007-03-24 12:08:36 +0000 | [diff] [blame] | 49 | /* The -1 arises because of the {0,NULL,0,-1} entry. */ |
Denis Vlasenko | 32b633a | 2007-04-09 03:05:48 +0000 | [diff] [blame] | 50 | |
Denis Vlasenko | 856be77 | 2007-08-17 08:29:48 +0000 | [diff] [blame] | 51 | #if ENABLE_FEATURE_SH_STANDALONE |
| 52 | const unsigned short NUM_APPLETS = ARRAY_SIZE(applets); |
| 53 | #endif |
Denis Vlasenko | 335b63d | 2007-04-10 21:38:30 +0000 | [diff] [blame] | 54 | const struct bb_applet *current_applet; |
Denis Vlasenko | 32b633a | 2007-04-09 03:05:48 +0000 | [diff] [blame] | 55 | const char *applet_name ATTRIBUTE_EXTERNALLY_VISIBLE; |
Denis Vlasenko | 473dae0 | 2007-04-11 07:04:23 +0000 | [diff] [blame] | 56 | #if !BB_MMU |
Bernhard Reutner-Fischer | 163516d | 2007-04-10 14:16:19 +0000 | [diff] [blame] | 57 | bool re_execed; |
Denis Vlasenko | 32b633a | 2007-04-09 03:05:48 +0000 | [diff] [blame] | 58 | #endif |
| 59 | |
Denis Vlasenko | 17e3c34 | 2007-04-16 20:55:27 +0000 | [diff] [blame] | 60 | USE_FEATURE_SUID(static uid_t ruid;) /* real uid */ |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 61 | |
Denis Vlasenko | 1b6fa4c | 2007-03-24 12:08:36 +0000 | [diff] [blame] | 62 | #if ENABLE_FEATURE_SUID_CONFIG |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 63 | |
Denis Vlasenko | 32b633a | 2007-04-09 03:05:48 +0000 | [diff] [blame] | 64 | /* applets[] is const, so we have to define this "override" structure */ |
Denis Vlasenko | c44ab01 | 2007-04-09 03:11:58 +0000 | [diff] [blame] | 65 | static struct BB_suid_config { |
Denis Vlasenko | 335b63d | 2007-04-10 21:38:30 +0000 | [diff] [blame] | 66 | const struct bb_applet *m_applet; |
Denis Vlasenko | 01a74f9 | 2006-09-23 16:34:39 +0000 | [diff] [blame] | 67 | uid_t m_uid; |
| 68 | gid_t m_gid; |
| 69 | mode_t m_mode; |
Denis Vlasenko | 01a74f9 | 2006-09-23 16:34:39 +0000 | [diff] [blame] | 70 | struct BB_suid_config *m_next; |
Bernhard Reutner-Fischer | 6973abc | 2005-10-28 09:45:07 +0000 | [diff] [blame] | 71 | } *suid_config; |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 72 | |
Bernhard Reutner-Fischer | 163516d | 2007-04-10 14:16:19 +0000 | [diff] [blame] | 73 | static bool suid_cfg_readable; |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 74 | |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 75 | /* check if u is member of group g */ |
Denis Vlasenko | 01a74f9 | 2006-09-23 16:34:39 +0000 | [diff] [blame] | 76 | static int ingroup(uid_t u, gid_t g) |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 77 | { |
Denis Vlasenko | 01a74f9 | 2006-09-23 16:34:39 +0000 | [diff] [blame] | 78 | struct group *grp = getgrgid(g); |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 79 | |
Denis Vlasenko | 01a74f9 | 2006-09-23 16:34:39 +0000 | [diff] [blame] | 80 | if (grp) { |
| 81 | char **mem; |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 82 | |
Denis Vlasenko | 01a74f9 | 2006-09-23 16:34:39 +0000 | [diff] [blame] | 83 | for (mem = grp->gr_mem; *mem; mem++) { |
| 84 | struct passwd *pwd = getpwnam(*mem); |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 85 | |
Denis Vlasenko | 01a74f9 | 2006-09-23 16:34:39 +0000 | [diff] [blame] | 86 | if (pwd && (pwd->pw_uid == u)) |
| 87 | return 1; |
| 88 | } |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 89 | } |
Denis Vlasenko | 01a74f9 | 2006-09-23 16:34:39 +0000 | [diff] [blame] | 90 | return 0; |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 93 | /* This should probably be a libbb routine. In that case, |
| 94 | * I'd probably rename it to something like bb_trimmed_slice. |
| 95 | */ |
| 96 | static char *get_trimmed_slice(char *s, char *e) |
| 97 | { |
| 98 | /* First, consider the value at e to be nul and back up until we |
| 99 | * reach a non-space char. Set the char after that (possibly at |
| 100 | * the original e) to nul. */ |
| 101 | while (e-- > s) { |
| 102 | if (!isspace(*e)) { |
| 103 | break; |
| 104 | } |
| 105 | } |
Denis Vlasenko | 32b633a | 2007-04-09 03:05:48 +0000 | [diff] [blame] | 106 | e[1] = '\0'; |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 107 | |
| 108 | /* Next, advance past all leading space and return a ptr to the |
| 109 | * first non-space char; possibly the terminating nul. */ |
Rob Landley | ea224be | 2006-06-18 20:20:07 +0000 | [diff] [blame] | 110 | return skip_whitespace(s); |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 113 | /* Don't depend on the tools to combine strings. */ |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 114 | static const char config_file[] ALIGN1 = "/etc/busybox.conf"; |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 115 | |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 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. */ |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 119 | static const unsigned short mode_mask[] ALIGN2 = { |
Denis Vlasenko | 32b633a | 2007-04-09 03:05:48 +0000 | [diff] [blame] | 120 | /* SST sst xxx --- */ |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 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 */ |
| 124 | }; |
| 125 | |
Denis Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 126 | #define parse_error(x) do { errmsg = x; goto pe_label; } while (0) |
Denis Vlasenko | 32b633a | 2007-04-09 03:05:48 +0000 | [diff] [blame] | 127 | |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 128 | static void parse_config_file(void) |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 129 | { |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 130 | struct BB_suid_config *sct_head; |
| 131 | struct BB_suid_config *sct; |
Denis Vlasenko | 335b63d | 2007-04-10 21:38:30 +0000 | [diff] [blame] | 132 | const struct bb_applet *applet; |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 133 | FILE *f; |
Denis Vlasenko | a41fdf3 | 2007-01-29 22:51:00 +0000 | [diff] [blame] | 134 | const char *errmsg; |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 135 | char *s; |
| 136 | char *e; |
Bernhard Reutner-Fischer | 163516d | 2007-04-10 14:16:19 +0000 | [diff] [blame] | 137 | int i; |
| 138 | unsigned lc; |
| 139 | smallint section; |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 140 | char buffer[256]; |
| 141 | struct stat st; |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 142 | |
Denis Vlasenko | 1b6fa4c | 2007-03-24 12:08:36 +0000 | [diff] [blame] | 143 | assert(!suid_config); /* Should be set to NULL by bss init. */ |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 144 | |
Denis Vlasenko | 5f9468e | 2007-04-14 13:22:09 +0000 | [diff] [blame] | 145 | ruid = getuid(); |
| 146 | if (ruid == 0) /* run by root - don't need to even read config file */ |
| 147 | return; |
| 148 | |
Denis Vlasenko | 1b6fa4c | 2007-03-24 12:08:36 +0000 | [diff] [blame] | 149 | if ((stat(config_file, &st) != 0) /* No config file? */ |
| 150 | || !S_ISREG(st.st_mode) /* Not a regular file? */ |
| 151 | || (st.st_uid != 0) /* Not owned by root? */ |
| 152 | || (st.st_mode & (S_IWGRP | S_IWOTH)) /* Writable by non-root? */ |
| 153 | || !(f = fopen(config_file, "r")) /* Cannot open? */ |
| 154 | ) { |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 155 | return; |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 156 | } |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 157 | |
Manuel Novoa III | 7b565a0 | 2004-02-17 10:16:21 +0000 | [diff] [blame] | 158 | suid_cfg_readable = 1; |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 159 | sct_head = NULL; |
| 160 | section = lc = 0; |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 161 | |
Denis Vlasenko | 32b633a | 2007-04-09 03:05:48 +0000 | [diff] [blame] | 162 | while (1) { |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 163 | s = buffer; |
| 164 | |
| 165 | if (!fgets(s, sizeof(buffer), f)) { /* Are we done? */ |
| 166 | if (ferror(f)) { /* Make sure it wasn't a read error. */ |
| 167 | parse_error("reading"); |
| 168 | } |
| 169 | fclose(f); |
| 170 | suid_config = sct_head; /* Success, so set the pointer. */ |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | lc++; /* Got a (partial) line. */ |
| 175 | |
| 176 | /* If a line is too long for our buffer, we consider it an error. |
| 177 | * The following test does mistreat one corner case though. |
| 178 | * If the final line of the file does not end with a newline and |
| 179 | * yet exactly fills the buffer, it will be treated as too long |
| 180 | * even though there isn't really a problem. But it isn't really |
| 181 | * worth adding code to deal with such an unlikely situation, and |
| 182 | * we do err on the side of caution. Besides, the line would be |
| 183 | * too long if it did end with a newline. */ |
| 184 | if (!strchr(s, '\n') && !feof(f)) { |
| 185 | parse_error("line too long"); |
| 186 | } |
| 187 | |
| 188 | /* Trim leading and trailing whitespace, ignoring comments, and |
| 189 | * check if the resulting string is empty. */ |
Denis Vlasenko | 1b6fa4c | 2007-03-24 12:08:36 +0000 | [diff] [blame] | 190 | s = get_trimmed_slice(s, strchrnul(s, '#')); |
| 191 | if (!*s) { |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 192 | continue; |
| 193 | } |
| 194 | |
| 195 | /* Check for a section header. */ |
| 196 | |
| 197 | if (*s == '[') { |
| 198 | /* Unlike the old code, we ignore leading and trailing |
| 199 | * whitespace for the section name. We also require that |
| 200 | * there are no stray characters after the closing bracket. */ |
Denis Vlasenko | 1b6fa4c | 2007-03-24 12:08:36 +0000 | [diff] [blame] | 201 | e = strchr(s, ']'); |
| 202 | if (!e /* Missing right bracket? */ |
| 203 | || e[1] /* Trailing characters? */ |
| 204 | || !*(s = get_trimmed_slice(s+1, e)) /* Missing name? */ |
| 205 | ) { |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 206 | parse_error("section header"); |
| 207 | } |
| 208 | /* Right now we only have one section so just check it. |
| 209 | * If more sections are added in the future, please don't |
| 210 | * resort to cascading ifs with multiple strcasecmp calls. |
| 211 | * That kind of bloated code is all too common. A loop |
| 212 | * and a string table would be a better choice unless the |
| 213 | * number of sections is very small. */ |
| 214 | if (strcasecmp(s, "SUID") == 0) { |
| 215 | section = 1; |
| 216 | continue; |
| 217 | } |
| 218 | section = -1; /* Unknown section so set to skip. */ |
| 219 | continue; |
| 220 | } |
| 221 | |
| 222 | /* Process sections. */ |
| 223 | |
| 224 | if (section == 1) { /* SUID */ |
| 225 | /* Since we trimmed leading and trailing space above, we're |
| 226 | * now looking for strings of the form |
| 227 | * <key>[::space::]*=[::space::]*<value> |
| 228 | * where both key and value could contain inner whitespace. */ |
| 229 | |
| 230 | /* First get the key (an applet name in our case). */ |
Denis Vlasenko | 1b6fa4c | 2007-03-24 12:08:36 +0000 | [diff] [blame] | 231 | e = strchr(s, '='); |
| 232 | if (e) { |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 233 | s = get_trimmed_slice(s, e); |
| 234 | } |
| 235 | if (!e || !*s) { /* Missing '=' or empty key. */ |
| 236 | parse_error("keyword"); |
| 237 | } |
| 238 | |
| 239 | /* Ok, we have an applet name. Process the rhs if this |
| 240 | * applet is currently built in and ignore it otherwise. |
Denis Vlasenko | 32b633a | 2007-04-09 03:05:48 +0000 | [diff] [blame] | 241 | * Note: this can hide config file bugs which only pop |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 242 | * up when the busybox configuration is changed. */ |
Denis Vlasenko | 1b6fa4c | 2007-03-24 12:08:36 +0000 | [diff] [blame] | 243 | applet = find_applet_by_name(s); |
| 244 | if (applet) { |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 245 | /* Note: We currently don't check for duplicates! |
| 246 | * The last config line for each applet will be the |
| 247 | * one used since we insert at the head of the list. |
| 248 | * I suppose this could be considered a feature. */ |
| 249 | sct = xmalloc(sizeof(struct BB_suid_config)); |
| 250 | sct->m_applet = applet; |
| 251 | sct->m_mode = 0; |
| 252 | sct->m_next = sct_head; |
| 253 | sct_head = sct; |
| 254 | |
| 255 | /* Get the specified mode. */ |
| 256 | |
Rob Landley | ea224be | 2006-06-18 20:20:07 +0000 | [diff] [blame] | 257 | e = skip_whitespace(e+1); |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 258 | |
Denis Vlasenko | 1b6fa4c | 2007-03-24 12:08:36 +0000 | [diff] [blame] | 259 | for (i = 0; i < 3; i++) { |
Denis Vlasenko | 3bc1825 | 2007-05-02 23:01:32 +0000 | [diff] [blame] | 260 | /* There are 4 chars + 1 nul for each of user/group/other. */ |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 261 | static const char mode_chars[] ALIGN1 = "Ssx-\0" "Ssx-\0" "Ttx-"; |
Denis Vlasenko | 3bc1825 | 2007-05-02 23:01:32 +0000 | [diff] [blame] | 262 | |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 263 | const char *q; |
Denis Vlasenko | 1b6fa4c | 2007-03-24 12:08:36 +0000 | [diff] [blame] | 264 | q = strchrnul(mode_chars + 5*i, *e++); |
| 265 | if (!*q) { |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 266 | parse_error("mode"); |
| 267 | } |
| 268 | /* Adjust by -i to account for nul. */ |
| 269 | sct->m_mode |= mode_mask[(q - mode_chars) - i]; |
| 270 | } |
| 271 | |
| 272 | /* Now get the the user/group info. */ |
Bernhard Reutner-Fischer | 7ca61b6 | 2006-01-15 14:04:57 +0000 | [diff] [blame] | 273 | |
Rob Landley | ea224be | 2006-06-18 20:20:07 +0000 | [diff] [blame] | 274 | s = skip_whitespace(e); |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 275 | |
Denis Vlasenko | 32b633a | 2007-04-09 03:05:48 +0000 | [diff] [blame] | 276 | /* Note: we require whitespace between the mode and the |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 277 | * user/group info. */ |
| 278 | if ((s == e) || !(e = strchr(s, '.'))) { |
| 279 | parse_error("<uid>.<gid>"); |
| 280 | } |
Denis Vlasenko | 1b6fa4c | 2007-03-24 12:08:36 +0000 | [diff] [blame] | 281 | *e++ = '\0'; |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 282 | |
| 283 | /* We can't use get_ug_id here since it would exit() |
| 284 | * if a uid or gid was not found. Oh well... */ |
Denis Vlasenko | 1b6fa4c | 2007-03-24 12:08:36 +0000 | [diff] [blame] | 285 | sct->m_uid = bb_strtoul(s, NULL, 10); |
| 286 | if (errno) { |
| 287 | struct passwd *pwd = getpwnam(s); |
| 288 | if (!pwd) { |
| 289 | parse_error("user"); |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 290 | } |
Denis Vlasenko | 1b6fa4c | 2007-03-24 12:08:36 +0000 | [diff] [blame] | 291 | sct->m_uid = pwd->pw_uid; |
| 292 | } |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 293 | |
Denis Vlasenko | 1b6fa4c | 2007-03-24 12:08:36 +0000 | [diff] [blame] | 294 | sct->m_gid = bb_strtoul(e, NULL, 10); |
| 295 | if (errno) { |
| 296 | struct group *grp; |
| 297 | grp = getgrnam(e); |
| 298 | if (!grp) { |
| 299 | parse_error("group"); |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 300 | } |
Denis Vlasenko | 1b6fa4c | 2007-03-24 12:08:36 +0000 | [diff] [blame] | 301 | sct->m_gid = grp->gr_gid; |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | continue; |
| 305 | } |
| 306 | |
| 307 | /* Unknown sections are ignored. */ |
| 308 | |
| 309 | /* Encountering configuration lines prior to seeing a |
| 310 | * section header is treated as an error. This is how |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 311 | * the old code worked, but it may not be desirable. |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 312 | * We may want to simply ignore such lines in case they |
| 313 | * are used in some future version of busybox. */ |
| 314 | if (!section) { |
| 315 | parse_error("keyword outside section"); |
| 316 | } |
| 317 | |
Denis Vlasenko | 32b633a | 2007-04-09 03:05:48 +0000 | [diff] [blame] | 318 | } /* while (1) */ |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 319 | |
| 320 | pe_label: |
| 321 | fprintf(stderr, "Parse error in %s, line %d: %s\n", |
Denis Vlasenko | a41fdf3 | 2007-01-29 22:51:00 +0000 | [diff] [blame] | 322 | config_file, lc, errmsg); |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 323 | |
| 324 | fclose(f); |
| 325 | /* Release any allocated memory before returning. */ |
| 326 | while (sct_head) { |
| 327 | sct = sct_head->m_next; |
| 328 | free(sct_head); |
| 329 | sct_head = sct; |
| 330 | } |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 331 | } |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 332 | #else |
Denis Vlasenko | 5f9468e | 2007-04-14 13:22:09 +0000 | [diff] [blame] | 333 | static inline void parse_config_file(void) |
| 334 | { |
Denis Vlasenko | 17e3c34 | 2007-04-16 20:55:27 +0000 | [diff] [blame] | 335 | USE_FEATURE_SUID(ruid = getuid();) |
Denis Vlasenko | 5f9468e | 2007-04-14 13:22:09 +0000 | [diff] [blame] | 336 | } |
Denis Vlasenko | 32b633a | 2007-04-09 03:05:48 +0000 | [diff] [blame] | 337 | #endif /* FEATURE_SUID_CONFIG */ |
| 338 | |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 339 | |
Denis Vlasenko | 1b6fa4c | 2007-03-24 12:08:36 +0000 | [diff] [blame] | 340 | #if ENABLE_FEATURE_SUID |
Denis Vlasenko | 335b63d | 2007-04-10 21:38:30 +0000 | [diff] [blame] | 341 | static void check_suid(const struct bb_applet *applet) |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 342 | { |
Denis Vlasenko | 3bc1825 | 2007-05-02 23:01:32 +0000 | [diff] [blame] | 343 | gid_t rgid; /* real gid */ |
Denis Vlasenko | 5f9468e | 2007-04-14 13:22:09 +0000 | [diff] [blame] | 344 | |
| 345 | if (ruid == 0) /* set by parse_config_file() */ |
| 346 | return; /* run by root - no need to check more */ |
| 347 | rgid = getgid(); |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 348 | |
Denis Vlasenko | 1b6fa4c | 2007-03-24 12:08:36 +0000 | [diff] [blame] | 349 | #if ENABLE_FEATURE_SUID_CONFIG |
Denis Vlasenko | 01a74f9 | 2006-09-23 16:34:39 +0000 | [diff] [blame] | 350 | if (suid_cfg_readable) { |
Denis Vlasenko | 3349fc4 | 2007-05-04 14:54:36 +0000 | [diff] [blame] | 351 | uid_t uid; |
Denis Vlasenko | 01a74f9 | 2006-09-23 16:34:39 +0000 | [diff] [blame] | 352 | struct BB_suid_config *sct; |
Denis Vlasenko | 1b6fa4c | 2007-03-24 12:08:36 +0000 | [diff] [blame] | 353 | mode_t m; |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 354 | |
Denis Vlasenko | 01a74f9 | 2006-09-23 16:34:39 +0000 | [diff] [blame] | 355 | for (sct = suid_config; sct; sct = sct->m_next) { |
| 356 | if (sct->m_applet == applet) |
Denis Vlasenko | 1b6fa4c | 2007-03-24 12:08:36 +0000 | [diff] [blame] | 357 | goto found; |
Denis Vlasenko | 01a74f9 | 2006-09-23 16:34:39 +0000 | [diff] [blame] | 358 | } |
Denis Vlasenko | 1b6fa4c | 2007-03-24 12:08:36 +0000 | [diff] [blame] | 359 | /* default: drop all privileges */ |
| 360 | xsetgid(rgid); |
| 361 | xsetuid(ruid); |
| 362 | return; |
| 363 | found: |
| 364 | m = sct->m_mode; |
| 365 | if (sct->m_uid == ruid) |
| 366 | /* same uid */ |
| 367 | m >>= 6; |
| 368 | else if ((sct->m_gid == rgid) || ingroup(ruid, sct->m_gid)) |
| 369 | /* same group / in group */ |
| 370 | m >>= 3; |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 371 | |
Denis Vlasenko | 1b6fa4c | 2007-03-24 12:08:36 +0000 | [diff] [blame] | 372 | if (!(m & S_IXOTH)) /* is x bit not set ? */ |
| 373 | bb_error_msg_and_die("you have no permission to run this applet!"); |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 374 | |
Denis Vlasenko | 3bc1825 | 2007-05-02 23:01:32 +0000 | [diff] [blame] | 375 | /* _both_ sgid and group_exec have to be set for setegid */ |
| 376 | if ((sct->m_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) |
| 377 | rgid = sct->m_gid; |
| 378 | /* else (no setegid) we will set egid = rgid */ |
| 379 | |
| 380 | /* We set effective AND saved ids. If saved-id is not set |
| 381 | * like we do below, seteiud(0) can still later succeed! */ |
| 382 | if (setresgid(-1, rgid, rgid)) |
| 383 | bb_perror_msg_and_die("setresgid"); |
| 384 | |
| 385 | /* do we have to set effective uid? */ |
| 386 | uid = ruid; |
| 387 | if (sct->m_mode & S_ISUID) |
| 388 | uid = sct->m_uid; |
| 389 | /* else (no seteuid) we will set euid = ruid */ |
| 390 | |
| 391 | if (setresuid(-1, uid, uid)) |
| 392 | bb_perror_msg_and_die("setresuid"); |
Denis Vlasenko | 01a74f9 | 2006-09-23 16:34:39 +0000 | [diff] [blame] | 393 | return; |
Denis Vlasenko | 1b6fa4c | 2007-03-24 12:08:36 +0000 | [diff] [blame] | 394 | } |
| 395 | #if !ENABLE_FEATURE_SUID_CONFIG_QUIET |
| 396 | { |
Bernhard Reutner-Fischer | 163516d | 2007-04-10 14:16:19 +0000 | [diff] [blame] | 397 | static bool onetime = 0; |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 398 | |
Denis Vlasenko | 01a74f9 | 2006-09-23 16:34:39 +0000 | [diff] [blame] | 399 | if (!onetime) { |
| 400 | onetime = 1; |
Denis Vlasenko | 13c5a68 | 2006-10-16 22:39:51 +0000 | [diff] [blame] | 401 | fprintf(stderr, "Using fallback suid method\n"); |
Denis Vlasenko | 01a74f9 | 2006-09-23 16:34:39 +0000 | [diff] [blame] | 402 | } |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 403 | } |
| 404 | #endif |
Denis Vlasenko | 1b6fa4c | 2007-03-24 12:08:36 +0000 | [diff] [blame] | 405 | #endif |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 406 | |
Denis Vlasenko | 01a74f9 | 2006-09-23 16:34:39 +0000 | [diff] [blame] | 407 | if (applet->need_suid == _BB_SUID_ALWAYS) { |
Denis Vlasenko | 3bc1825 | 2007-05-02 23:01:32 +0000 | [diff] [blame] | 408 | /* Real uid is not 0. If euid isn't 0 too, suid bit |
| 409 | * is most probably not set on our executable */ |
Denis Vlasenko | 1b6fa4c | 2007-03-24 12:08:36 +0000 | [diff] [blame] | 410 | if (geteuid()) |
| 411 | bb_error_msg_and_die("applet requires root privileges!"); |
Denis Vlasenko | 01a74f9 | 2006-09-23 16:34:39 +0000 | [diff] [blame] | 412 | } else if (applet->need_suid == _BB_SUID_NEVER) { |
Denis Vlasenko | 5f9468e | 2007-04-14 13:22:09 +0000 | [diff] [blame] | 413 | xsetgid(rgid); /* drop all privileges */ |
Denis Vlasenko | 01a74f9 | 2006-09-23 16:34:39 +0000 | [diff] [blame] | 414 | xsetuid(ruid); |
| 415 | } |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 416 | } |
| 417 | #else |
Denis Vlasenko | 32b633a | 2007-04-09 03:05:48 +0000 | [diff] [blame] | 418 | #define check_suid(x) ((void)0) |
| 419 | #endif /* FEATURE_SUID */ |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 420 | |
| 421 | |
Denis Vlasenko | 1b6fa4c | 2007-03-24 12:08:36 +0000 | [diff] [blame] | 422 | #if ENABLE_FEATURE_COMPRESS_USAGE |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 423 | |
Rob Landley | 7e21d5f | 2006-04-27 23:34:46 +0000 | [diff] [blame] | 424 | #include "usage_compressed.h" |
| 425 | #include "unarchive.h" |
| 426 | |
| 427 | static const char *unpack_usage_messages(void) |
| 428 | { |
Denis Vlasenko | c6758a0 | 2007-04-10 21:40:19 +0000 | [diff] [blame] | 429 | char *outbuf = NULL; |
| 430 | bunzip_data *bd; |
| 431 | int i; |
Rob Landley | 7e21d5f | 2006-04-27 23:34:46 +0000 | [diff] [blame] | 432 | |
Denis Vlasenko | c6758a0 | 2007-04-10 21:40:19 +0000 | [diff] [blame] | 433 | i = start_bunzip(&bd, |
| 434 | /* src_fd: */ -1, |
| 435 | /* inbuf: */ packed_usage, |
| 436 | /* len: */ sizeof(packed_usage)); |
| 437 | /* read_bunzip can longjmp to start_bunzip, and ultimately |
| 438 | * end up here with i != 0 on read data errors! Not trivial */ |
| 439 | if (!i) { |
| 440 | /* Cannot use xmalloc: will leak bd in NOFORK case! */ |
| 441 | outbuf = malloc_or_warn(SIZEOF_usage_messages); |
| 442 | if (outbuf) |
| 443 | read_bunzip(bd, outbuf, SIZEOF_usage_messages); |
Rob Landley | 7e21d5f | 2006-04-27 23:34:46 +0000 | [diff] [blame] | 444 | } |
Denis Vlasenko | c6758a0 | 2007-04-10 21:40:19 +0000 | [diff] [blame] | 445 | dealloc_bunzip(bd); |
| 446 | return outbuf; |
Rob Landley | 7e21d5f | 2006-04-27 23:34:46 +0000 | [diff] [blame] | 447 | } |
Denis Vlasenko | c6758a0 | 2007-04-10 21:40:19 +0000 | [diff] [blame] | 448 | #define dealloc_usage_messages(s) free(s) |
| 449 | |
Rob Landley | 7e21d5f | 2006-04-27 23:34:46 +0000 | [diff] [blame] | 450 | #else |
Denis Vlasenko | c6758a0 | 2007-04-10 21:40:19 +0000 | [diff] [blame] | 451 | |
Rob Landley | 1801e9c | 2006-05-03 20:19:14 +0000 | [diff] [blame] | 452 | #define unpack_usage_messages() usage_messages |
Denis Vlasenko | c6758a0 | 2007-04-10 21:40:19 +0000 | [diff] [blame] | 453 | #define dealloc_usage_messages(s) ((void)(s)) |
| 454 | |
Denis Vlasenko | 32b633a | 2007-04-09 03:05:48 +0000 | [diff] [blame] | 455 | #endif /* FEATURE_COMPRESS_USAGE */ |
| 456 | |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 457 | |
Denis Vlasenko | 01a74f9 | 2006-09-23 16:34:39 +0000 | [diff] [blame] | 458 | void bb_show_usage(void) |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 459 | { |
Rob Landley | 7e21d5f | 2006-04-27 23:34:46 +0000 | [diff] [blame] | 460 | if (ENABLE_SHOW_USAGE) { |
| 461 | const char *format_string; |
Denis Vlasenko | c6758a0 | 2007-04-10 21:40:19 +0000 | [diff] [blame] | 462 | const char *p; |
| 463 | const char *usage_string = p = unpack_usage_messages(); |
Rob Landley | 7e21d5f | 2006-04-27 23:34:46 +0000 | [diff] [blame] | 464 | int i; |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 465 | |
Denis Vlasenko | 32b633a | 2007-04-09 03:05:48 +0000 | [diff] [blame] | 466 | i = current_applet - applets; |
| 467 | while (i) { |
Denis Vlasenko | c6758a0 | 2007-04-10 21:40:19 +0000 | [diff] [blame] | 468 | while (*p++) continue; |
Denis Vlasenko | 32b633a | 2007-04-09 03:05:48 +0000 | [diff] [blame] | 469 | i--; |
| 470 | } |
Rob Landley | 7e21d5f | 2006-04-27 23:34:46 +0000 | [diff] [blame] | 471 | |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 472 | fprintf(stderr, "%s multi-call binary\n", bb_banner); |
| 473 | format_string = "\nUsage: %s %s\n\n"; |
Denis Vlasenko | c6758a0 | 2007-04-10 21:40:19 +0000 | [diff] [blame] | 474 | if (*p == '\b') |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 475 | format_string = "\nNo help available.\n\n"; |
| 476 | fprintf(stderr, format_string, applet_name, p); |
Denis Vlasenko | c6758a0 | 2007-04-10 21:40:19 +0000 | [diff] [blame] | 477 | dealloc_usage_messages((char*)usage_string); |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 478 | } |
Denis Vlasenko | 335b63d | 2007-04-10 21:38:30 +0000 | [diff] [blame] | 479 | xfunc_die(); |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 480 | } |
| 481 | |
Denis Vlasenko | 32b633a | 2007-04-09 03:05:48 +0000 | [diff] [blame] | 482 | |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 483 | static int applet_name_compare(const void *name, const void *vapplet) |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 484 | { |
Denis Vlasenko | 335b63d | 2007-04-10 21:38:30 +0000 | [diff] [blame] | 485 | const struct bb_applet *applet = vapplet; |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 486 | |
Denis Vlasenko | 01a74f9 | 2006-09-23 16:34:39 +0000 | [diff] [blame] | 487 | return strcmp(name, applet->name); |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 488 | } |
| 489 | |
Denis Vlasenko | 335b63d | 2007-04-10 21:38:30 +0000 | [diff] [blame] | 490 | const struct bb_applet *find_applet_by_name(const char *name) |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 491 | { |
Denis Vlasenko | 32b633a | 2007-04-09 03:05:48 +0000 | [diff] [blame] | 492 | /* Do a binary search to find the applet entry given the name. */ |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 493 | return bsearch(name, applets, ARRAY_SIZE(applets)-1, sizeof(applets[0]), |
Denis Vlasenko | 01a74f9 | 2006-09-23 16:34:39 +0000 | [diff] [blame] | 494 | applet_name_compare); |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 495 | } |
| 496 | |
Denis Vlasenko | 32b633a | 2007-04-09 03:05:48 +0000 | [diff] [blame] | 497 | |
| 498 | #if ENABLE_FEATURE_INSTALLER |
Denis Vlasenko | 32b633a | 2007-04-09 03:05:48 +0000 | [diff] [blame] | 499 | /* create (sym)links for each applet */ |
| 500 | static void install_links(const char *busybox, int use_symbolic_links) |
| 501 | { |
Denis Vlasenko | 335b63d | 2007-04-10 21:38:30 +0000 | [diff] [blame] | 502 | /* directory table |
| 503 | * this should be consistent w/ the enum, |
| 504 | * busybox.h::bb_install_loc_t, or else... */ |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 505 | static const char usr_bin [] ALIGN1 = "/usr/bin"; |
| 506 | static const char usr_sbin[] ALIGN1 = "/usr/sbin"; |
Denis Vlasenko | 335b63d | 2007-04-10 21:38:30 +0000 | [diff] [blame] | 507 | static const char *const install_dir[] = { |
| 508 | &usr_bin [8], /* "", equivalent to "/" for concat_path_file() */ |
| 509 | &usr_bin [4], /* "/bin" */ |
| 510 | &usr_sbin[4], /* "/sbin" */ |
| 511 | usr_bin, |
| 512 | usr_sbin |
| 513 | }; |
| 514 | |
Denis Vlasenko | 32b633a | 2007-04-09 03:05:48 +0000 | [diff] [blame] | 515 | int (*lf)(const char *, const char *) = link; |
| 516 | char *fpc; |
| 517 | int i; |
| 518 | int rc; |
| 519 | |
| 520 | if (use_symbolic_links) |
| 521 | lf = symlink; |
| 522 | |
| 523 | for (i = 0; applets[i].name != NULL; i++) { |
| 524 | fpc = concat_path_file( |
Denis Vlasenko | 335b63d | 2007-04-10 21:38:30 +0000 | [diff] [blame] | 525 | install_dir[applets[i].install_loc], |
Denis Vlasenko | 32b633a | 2007-04-09 03:05:48 +0000 | [diff] [blame] | 526 | applets[i].name); |
| 527 | rc = lf(busybox, fpc); |
| 528 | if (rc != 0 && errno != EEXIST) { |
| 529 | bb_perror_msg("%s", fpc); |
| 530 | } |
| 531 | free(fpc); |
| 532 | } |
| 533 | } |
| 534 | #else |
| 535 | #define install_links(x,y) ((void)0) |
| 536 | #endif /* FEATURE_INSTALLER */ |
| 537 | |
| 538 | |
| 539 | /* If we were called as "busybox..." */ |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 540 | static int busybox_main(char **argv) |
Denis Vlasenko | 32b633a | 2007-04-09 03:05:48 +0000 | [diff] [blame] | 541 | { |
Denis Vlasenko | 2ace1e3 | 2007-04-18 21:00:21 +0000 | [diff] [blame] | 542 | if (!argv[1]) { |
| 543 | /* Called without arguments */ |
| 544 | const struct bb_applet *a; |
| 545 | int col, output_width; |
| 546 | help: |
| 547 | output_width = 80; |
| 548 | if (ENABLE_FEATURE_AUTOWIDTH) { |
Denis Vlasenko | beffd43 | 2007-09-05 11:30:34 +0000 | [diff] [blame] | 549 | /* Obtain the terminal width */ |
Denis Vlasenko | 2ace1e3 | 2007-04-18 21:00:21 +0000 | [diff] [blame] | 550 | get_terminal_width_height(0, &output_width, NULL); |
| 551 | } |
| 552 | /* leading tab and room to wrap */ |
| 553 | output_width -= sizeof("start-stop-daemon, ") + 8; |
| 554 | |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 555 | printf("%s multi-call binary\n", bb_banner); /* reuse const string... */ |
Denis Vlasenko | 9b0a439 | 2007-09-20 19:44:10 +0000 | [diff] [blame] | 556 | printf("Copyright (C) 1998-2006 Erik Andersen, Rob Landley, and others.\n" |
| 557 | "Licensed under GPLv2. See source distribution for full notice.\n" |
Denis Vlasenko | 2ace1e3 | 2007-04-18 21:00:21 +0000 | [diff] [blame] | 558 | "\n" |
| 559 | "Usage: busybox [function] [arguments]...\n" |
| 560 | " or: [function] [arguments]...\n" |
| 561 | "\n" |
| 562 | "\tBusyBox is a multi-call binary that combines many common Unix\n" |
| 563 | "\tutilities into a single executable. Most people will create a\n" |
| 564 | "\tlink to busybox for each function they wish to use and BusyBox\n" |
| 565 | "\twill act like whatever it was invoked as!\n" |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 566 | "\nCurrently defined functions:\n"); |
Denis Vlasenko | 2ace1e3 | 2007-04-18 21:00:21 +0000 | [diff] [blame] | 567 | col = 0; |
| 568 | a = applets; |
| 569 | while (a->name) { |
| 570 | if (col > output_width) { |
| 571 | puts(","); |
| 572 | col = 0; |
| 573 | } |
| 574 | col += printf("%s%s", (col ? ", " : "\t"), a->name); |
| 575 | a++; |
| 576 | } |
| 577 | puts("\n"); |
| 578 | return 0; |
| 579 | } |
| 580 | |
| 581 | if (ENABLE_FEATURE_INSTALLER && strcmp(argv[1], "--install") == 0) { |
Denis Vlasenko | bdbbb7e | 2007-06-08 15:02:55 +0000 | [diff] [blame] | 582 | const char *busybox; |
Denis Vlasenko | beffd43 | 2007-09-05 11:30:34 +0000 | [diff] [blame] | 583 | busybox = xmalloc_readlink(bb_busybox_exec_path); |
Denis Vlasenko | 32b633a | 2007-04-09 03:05:48 +0000 | [diff] [blame] | 584 | if (!busybox) |
Denis Vlasenko | bdbbb7e | 2007-06-08 15:02:55 +0000 | [diff] [blame] | 585 | busybox = bb_busybox_exec_path; |
| 586 | /* -s makes symlinks */ |
Denis Vlasenko | beffd43 | 2007-09-05 11:30:34 +0000 | [diff] [blame] | 587 | install_links(busybox, argv[2] && strcmp(argv[2], "-s") == 0); |
Denis Vlasenko | 32b633a | 2007-04-09 03:05:48 +0000 | [diff] [blame] | 588 | return 0; |
| 589 | } |
| 590 | |
Denis Vlasenko | 2ace1e3 | 2007-04-18 21:00:21 +0000 | [diff] [blame] | 591 | if (strcmp(argv[1], "--help") == 0) { |
| 592 | /* "busybox --help [<applet>]" */ |
| 593 | if (!argv[2]) |
| 594 | goto help; |
| 595 | /* convert to "<applet> --help" */ |
| 596 | argv[0] = argv[2]; |
| 597 | argv[2] = NULL; |
Denis Vlasenko | 2dfdd44 | 2007-04-09 03:29:43 +0000 | [diff] [blame] | 598 | } else { |
Denis Vlasenko | 2ace1e3 | 2007-04-18 21:00:21 +0000 | [diff] [blame] | 599 | /* "busybox <applet> arg1 arg2 ..." */ |
| 600 | argv++; |
Denis Vlasenko | 2dfdd44 | 2007-04-09 03:29:43 +0000 | [diff] [blame] | 601 | } |
Denis Vlasenko | 737d131 | 2007-08-25 18:25:24 +0000 | [diff] [blame] | 602 | /* We support "busybox /a/path/to/applet args..." too. Allows for |
| 603 | * "#!/bin/busybox"-style wrappers */ |
Denis Vlasenko | 818322b | 2007-09-24 18:27:04 +0000 | [diff] [blame] | 604 | applet_name = bb_get_last_path_component_nostrip(argv[0]); |
Denis Vlasenko | 737d131 | 2007-08-25 18:25:24 +0000 | [diff] [blame] | 605 | run_applet_and_exit(applet_name, argv); |
Denis Vlasenko | 32b633a | 2007-04-09 03:05:48 +0000 | [diff] [blame] | 606 | bb_error_msg_and_die("applet not found"); |
| 607 | } |
| 608 | |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 609 | void run_current_applet_and_exit(char **argv) |
Denis Vlasenko | c44ab01 | 2007-04-09 03:11:58 +0000 | [diff] [blame] | 610 | { |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 611 | int argc = 1; |
| 612 | |
| 613 | while (argv[argc]) |
| 614 | argc++; |
| 615 | |
Denis Vlasenko | 831a20f | 2007-04-12 12:27:32 +0000 | [diff] [blame] | 616 | /* Reinit some shared global data */ |
| 617 | optind = 1; |
| 618 | xfunc_error_retval = EXIT_FAILURE; |
| 619 | |
Denis Vlasenko | c44ab01 | 2007-04-09 03:11:58 +0000 | [diff] [blame] | 620 | applet_name = current_applet->name; |
| 621 | if (argc == 2 && !strcmp(argv[1], "--help")) |
| 622 | bb_show_usage(); |
| 623 | if (ENABLE_FEATURE_SUID) |
| 624 | check_suid(current_applet); |
| 625 | exit(current_applet->main(argc, argv)); |
| 626 | } |
| 627 | |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 628 | void run_applet_and_exit(const char *name, char **argv) |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 629 | { |
Denis Vlasenko | 32b633a | 2007-04-09 03:05:48 +0000 | [diff] [blame] | 630 | current_applet = find_applet_by_name(name); |
Denis Vlasenko | c44ab01 | 2007-04-09 03:11:58 +0000 | [diff] [blame] | 631 | if (current_applet) |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 632 | run_current_applet_and_exit(argv); |
Denis Vlasenko | 1b6fa4c | 2007-03-24 12:08:36 +0000 | [diff] [blame] | 633 | if (!strncmp(name, "busybox", 7)) |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 634 | exit(busybox_main(argv)); |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 635 | } |
Denis Vlasenko | 32b633a | 2007-04-09 03:05:48 +0000 | [diff] [blame] | 636 | |
| 637 | |
Denis Vlasenko | fad2b86 | 2007-05-31 22:16:38 +0000 | [diff] [blame] | 638 | #ifdef __GLIBC__ |
| 639 | /* Make it reside in R/W memory: */ |
| 640 | int *const bb_errno __attribute__ ((section (".data"))); |
| 641 | #endif |
| 642 | |
Denis Vlasenko | 32b633a | 2007-04-09 03:05:48 +0000 | [diff] [blame] | 643 | int main(int argc, char **argv) |
| 644 | { |
Denis Vlasenko | fad2b86 | 2007-05-31 22:16:38 +0000 | [diff] [blame] | 645 | #ifdef __GLIBC__ |
| 646 | (*(int **)&bb_errno) = __errno_location(); |
| 647 | #endif |
| 648 | |
Denis Vlasenko | 473dae0 | 2007-04-11 07:04:23 +0000 | [diff] [blame] | 649 | #if !BB_MMU |
Denis Vlasenko | 32b633a | 2007-04-09 03:05:48 +0000 | [diff] [blame] | 650 | /* NOMMU re-exec trick sets high-order bit in first byte of name */ |
Denis Vlasenko | f1a7141 | 2007-04-10 23:32:37 +0000 | [diff] [blame] | 651 | if (argv[0][0] & 0x80) { |
Denis Vlasenko | 32b633a | 2007-04-09 03:05:48 +0000 | [diff] [blame] | 652 | re_execed = 1; |
Denis Vlasenko | f1a7141 | 2007-04-10 23:32:37 +0000 | [diff] [blame] | 653 | argv[0][0] &= 0x7f; |
Denis Vlasenko | 32b633a | 2007-04-09 03:05:48 +0000 | [diff] [blame] | 654 | } |
| 655 | #endif |
Denis Vlasenko | f1a7141 | 2007-04-10 23:32:37 +0000 | [diff] [blame] | 656 | applet_name = argv[0]; |
Denis Vlasenko | 32b633a | 2007-04-09 03:05:48 +0000 | [diff] [blame] | 657 | if (applet_name[0] == '-') |
| 658 | applet_name++; |
Denis Vlasenko | dc757aa | 2007-06-30 08:04:05 +0000 | [diff] [blame] | 659 | applet_name = bb_basename(applet_name); |
Denis Vlasenko | 32b633a | 2007-04-09 03:05:48 +0000 | [diff] [blame] | 660 | |
Denis Vlasenko | 5f9468e | 2007-04-14 13:22:09 +0000 | [diff] [blame] | 661 | parse_config_file(); /* ...maybe, if FEATURE_SUID_CONFIG */ |
Denis Vlasenko | 32b633a | 2007-04-09 03:05:48 +0000 | [diff] [blame] | 662 | |
| 663 | /* Set locale for everybody except 'init' */ |
| 664 | if (ENABLE_LOCALE_SUPPORT && getpid() != 1) |
| 665 | setlocale(LC_ALL, ""); |
| 666 | |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 667 | run_applet_and_exit(applet_name, argv); |
Denis Vlasenko | 32b633a | 2007-04-09 03:05:48 +0000 | [diff] [blame] | 668 | bb_error_msg_and_die("applet not found"); |
| 669 | } |