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 | |
Bernhard Reutner-Fischer | e15d757 | 2006-06-02 20:56:16 +0000 | [diff] [blame] | 15 | #include "busybox.h" |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 16 | #include <unistd.h> |
Eric Andersen | 08ff8a4 | 2001-03-23 17:02:05 +0000 | [diff] [blame] | 17 | #include <string.h> |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 18 | #include <assert.h> |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 19 | |
Rob Landley | 7e21d5f | 2006-04-27 23:34:46 +0000 | [diff] [blame] | 20 | #if ENABLE_SHOW_USAGE && !ENABLE_FEATURE_COMPRESS_USAGE |
| 21 | static const char usage_messages[] = |
Eric Andersen | 674b08a | 2004-04-06 14:28:35 +0000 | [diff] [blame] | 22 | #define MAKE_USAGE |
| 23 | #include "usage.h" |
Eric Andersen | 674b08a | 2004-04-06 14:28:35 +0000 | [diff] [blame] | 24 | #include "applets.h" |
Eric Andersen | 674b08a | 2004-04-06 14:28:35 +0000 | [diff] [blame] | 25 | ; |
Eric Andersen | 674b08a | 2004-04-06 14:28:35 +0000 | [diff] [blame] | 26 | #undef MAKE_USAGE |
Rob Landley | 73f5470 | 2006-05-01 00:53:40 +0000 | [diff] [blame] | 27 | #else |
| 28 | #define usage_messages 0 |
Bernhard Reutner-Fischer | 81901a0 | 2006-03-31 18:43:55 +0000 | [diff] [blame] | 29 | #endif /* ENABLE_SHOW_USAGE */ |
Rob Landley | 7e21d5f | 2006-04-27 23:34:46 +0000 | [diff] [blame] | 30 | |
Eric Andersen | 2ccfef2 | 2001-03-19 19:30:24 +0000 | [diff] [blame] | 31 | #undef APPLET |
| 32 | #undef APPLET_NOUSAGE |
| 33 | #undef PROTOTYPES |
| 34 | #include "applets.h" |
| 35 | |
Glenn L McGrath | 7fc504c | 2004-02-22 11:13:28 +0000 | [diff] [blame] | 36 | static struct BB_applet *applet_using; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 37 | |
Eric Andersen | 2ccfef2 | 2001-03-19 19:30:24 +0000 | [diff] [blame] | 38 | /* The -1 arises because of the {0,NULL,0,-1} entry above. */ |
| 39 | const size_t NUM_APPLETS = (sizeof (applets) / sizeof (struct BB_applet) - 1); |
| 40 | |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 41 | |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 42 | #ifdef CONFIG_FEATURE_SUID_CONFIG |
| 43 | |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 44 | #include <ctype.h> |
Eric Andersen | 887ca79 | 2002-07-03 23:19:26 +0000 | [diff] [blame] | 45 | #include "pwd_.h" |
| 46 | #include "grp_.h" |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 47 | |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 48 | #define CONFIG_FILE "/etc/busybox.conf" |
| 49 | |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 50 | /* applets [] is const, so we have to define this "override" structure */ |
Bernhard Reutner-Fischer | 6973abc | 2005-10-28 09:45:07 +0000 | [diff] [blame] | 51 | static struct BB_suid_config |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 52 | { |
| 53 | struct BB_applet *m_applet; |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 54 | |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 55 | uid_t m_uid; |
| 56 | gid_t m_gid; |
| 57 | mode_t m_mode; |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 58 | |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 59 | struct BB_suid_config *m_next; |
Bernhard Reutner-Fischer | 6973abc | 2005-10-28 09:45:07 +0000 | [diff] [blame] | 60 | } *suid_config; |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 61 | |
Manuel Novoa III | 7b565a0 | 2004-02-17 10:16:21 +0000 | [diff] [blame] | 62 | static int suid_cfg_readable; |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 63 | |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 64 | /* check if u is member of group g */ |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 65 | static int ingroup (uid_t u, gid_t g) |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 66 | { |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 67 | struct group *grp = getgrgid (g); |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 68 | |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 69 | if (grp) { |
| 70 | char **mem; |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 71 | |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 72 | for (mem = grp->gr_mem; *mem; mem++) { |
| 73 | struct passwd *pwd = getpwnam (*mem); |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 74 | |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 75 | if (pwd && (pwd->pw_uid == u)) |
| 76 | return 1; |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 77 | } |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 78 | } |
| 79 | return 0; |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 82 | /* This should probably be a libbb routine. In that case, |
| 83 | * I'd probably rename it to something like bb_trimmed_slice. |
| 84 | */ |
| 85 | static char *get_trimmed_slice(char *s, char *e) |
| 86 | { |
| 87 | /* First, consider the value at e to be nul and back up until we |
| 88 | * reach a non-space char. Set the char after that (possibly at |
| 89 | * the original e) to nul. */ |
| 90 | while (e-- > s) { |
| 91 | if (!isspace(*e)) { |
| 92 | break; |
| 93 | } |
| 94 | } |
| 95 | e[1] = 0; |
| 96 | |
| 97 | /* Next, advance past all leading space and return a ptr to the |
| 98 | * first non-space char; possibly the terminating nul. */ |
Rob Landley | ea224be | 2006-06-18 20:20:07 +0000 | [diff] [blame] | 99 | return skip_whitespace(s); |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 102 | |
| 103 | #define parse_error(x) { err=x; goto pe_label; } |
| 104 | |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 105 | /* Don't depend on the tools to combine strings. */ |
| 106 | static const char config_file[] = CONFIG_FILE; |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 107 | |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 108 | /* There are 4 chars + 1 nul for each of user/group/other. */ |
| 109 | static const char mode_chars[] = "Ssx-\0Ssx-\0Ttx-"; |
| 110 | |
| 111 | /* We don't supply a value for the nul, so an index adjustment is |
| 112 | * necessary below. Also, we use unsigned short here to save some |
| 113 | * space even though these are really mode_t values. */ |
| 114 | static const unsigned short mode_mask[] = { |
| 115 | /* SST sst xxx --- */ |
| 116 | S_ISUID, S_ISUID|S_IXUSR, S_IXUSR, 0, /* user */ |
| 117 | S_ISGID, S_ISGID|S_IXGRP, S_IXGRP, 0, /* group */ |
| 118 | 0, S_IXOTH, S_IXOTH, 0 /* other */ |
| 119 | }; |
| 120 | |
| 121 | static void parse_config_file(void) |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 122 | { |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 123 | struct BB_suid_config *sct_head; |
| 124 | struct BB_suid_config *sct; |
| 125 | struct BB_applet *applet; |
| 126 | FILE *f; |
| 127 | char *err; |
| 128 | char *s; |
| 129 | char *e; |
| 130 | int i, lc, section; |
| 131 | char buffer[256]; |
| 132 | struct stat st; |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 133 | |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 134 | assert(!suid_config); /* Should be set to NULL by bss init. */ |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 135 | |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 136 | if ((stat(config_file, &st) != 0) /* No config file? */ |
| 137 | || !S_ISREG(st.st_mode) /* Not a regular file? */ |
| 138 | || (st.st_uid != 0) /* Not owned by root? */ |
| 139 | || (st.st_mode & (S_IWGRP | S_IWOTH)) /* Writable by non-root? */ |
| 140 | || !(f = fopen(config_file, "r")) /* Can not open? */ |
| 141 | ) { |
| 142 | return; |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 143 | } |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 144 | |
Manuel Novoa III | 7b565a0 | 2004-02-17 10:16:21 +0000 | [diff] [blame] | 145 | suid_cfg_readable = 1; |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 146 | sct_head = NULL; |
| 147 | section = lc = 0; |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 148 | |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 149 | do { |
| 150 | s = buffer; |
| 151 | |
| 152 | if (!fgets(s, sizeof(buffer), f)) { /* Are we done? */ |
| 153 | if (ferror(f)) { /* Make sure it wasn't a read error. */ |
| 154 | parse_error("reading"); |
| 155 | } |
| 156 | fclose(f); |
| 157 | suid_config = sct_head; /* Success, so set the pointer. */ |
| 158 | return; |
| 159 | } |
| 160 | |
| 161 | lc++; /* Got a (partial) line. */ |
| 162 | |
| 163 | /* If a line is too long for our buffer, we consider it an error. |
| 164 | * The following test does mistreat one corner case though. |
| 165 | * If the final line of the file does not end with a newline and |
| 166 | * yet exactly fills the buffer, it will be treated as too long |
| 167 | * even though there isn't really a problem. But it isn't really |
| 168 | * worth adding code to deal with such an unlikely situation, and |
| 169 | * we do err on the side of caution. Besides, the line would be |
| 170 | * too long if it did end with a newline. */ |
| 171 | if (!strchr(s, '\n') && !feof(f)) { |
| 172 | parse_error("line too long"); |
| 173 | } |
| 174 | |
| 175 | /* Trim leading and trailing whitespace, ignoring comments, and |
| 176 | * check if the resulting string is empty. */ |
| 177 | if (!*(s = get_trimmed_slice(s, strchrnul(s, '#')))) { |
| 178 | continue; |
| 179 | } |
| 180 | |
| 181 | /* Check for a section header. */ |
| 182 | |
| 183 | if (*s == '[') { |
| 184 | /* Unlike the old code, we ignore leading and trailing |
| 185 | * whitespace for the section name. We also require that |
| 186 | * there are no stray characters after the closing bracket. */ |
| 187 | if (!(e = strchr(s, ']')) /* Missing right bracket? */ |
| 188 | || e[1] /* Trailing characters? */ |
| 189 | || !*(s = get_trimmed_slice(s+1, e)) /* Missing name? */ |
| 190 | ) { |
| 191 | parse_error("section header"); |
| 192 | } |
| 193 | /* Right now we only have one section so just check it. |
| 194 | * If more sections are added in the future, please don't |
| 195 | * resort to cascading ifs with multiple strcasecmp calls. |
| 196 | * That kind of bloated code is all too common. A loop |
| 197 | * and a string table would be a better choice unless the |
| 198 | * number of sections is very small. */ |
| 199 | if (strcasecmp(s, "SUID") == 0) { |
| 200 | section = 1; |
| 201 | continue; |
| 202 | } |
| 203 | section = -1; /* Unknown section so set to skip. */ |
| 204 | continue; |
| 205 | } |
| 206 | |
| 207 | /* Process sections. */ |
| 208 | |
| 209 | if (section == 1) { /* SUID */ |
| 210 | /* Since we trimmed leading and trailing space above, we're |
| 211 | * now looking for strings of the form |
| 212 | * <key>[::space::]*=[::space::]*<value> |
| 213 | * where both key and value could contain inner whitespace. */ |
| 214 | |
| 215 | /* First get the key (an applet name in our case). */ |
| 216 | if (!!(e = strchr(s, '='))) { |
| 217 | s = get_trimmed_slice(s, e); |
| 218 | } |
| 219 | if (!e || !*s) { /* Missing '=' or empty key. */ |
| 220 | parse_error("keyword"); |
| 221 | } |
| 222 | |
| 223 | /* Ok, we have an applet name. Process the rhs if this |
| 224 | * applet is currently built in and ignore it otherwise. |
| 225 | * Note: This can hide config file bugs which only pop |
| 226 | * up when the busybox configuration is changed. */ |
| 227 | if ((applet = find_applet_by_name(s))) { |
| 228 | /* Note: We currently don't check for duplicates! |
| 229 | * The last config line for each applet will be the |
| 230 | * one used since we insert at the head of the list. |
| 231 | * I suppose this could be considered a feature. */ |
| 232 | sct = xmalloc(sizeof(struct BB_suid_config)); |
| 233 | sct->m_applet = applet; |
| 234 | sct->m_mode = 0; |
| 235 | sct->m_next = sct_head; |
| 236 | sct_head = sct; |
| 237 | |
| 238 | /* Get the specified mode. */ |
| 239 | |
Rob Landley | ea224be | 2006-06-18 20:20:07 +0000 | [diff] [blame] | 240 | e = skip_whitespace(e+1); |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 241 | |
| 242 | for (i=0 ; i < 3 ; i++) { |
| 243 | const char *q; |
| 244 | if (!*(q = strchrnul(mode_chars + 5*i, *e++))) { |
| 245 | parse_error("mode"); |
| 246 | } |
| 247 | /* Adjust by -i to account for nul. */ |
| 248 | sct->m_mode |= mode_mask[(q - mode_chars) - i]; |
| 249 | } |
| 250 | |
| 251 | /* Now get the the user/group info. */ |
Bernhard Reutner-Fischer | 7ca61b6 | 2006-01-15 14:04:57 +0000 | [diff] [blame] | 252 | |
Rob Landley | ea224be | 2006-06-18 20:20:07 +0000 | [diff] [blame] | 253 | s = skip_whitespace(e); |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 254 | |
| 255 | /* Note: We require whitespace between the mode and the |
| 256 | * user/group info. */ |
| 257 | if ((s == e) || !(e = strchr(s, '.'))) { |
| 258 | parse_error("<uid>.<gid>"); |
| 259 | } |
| 260 | *e++ = 0; |
| 261 | |
| 262 | /* We can't use get_ug_id here since it would exit() |
| 263 | * if a uid or gid was not found. Oh well... */ |
| 264 | { |
| 265 | char *e2; |
| 266 | |
| 267 | sct->m_uid = strtoul(s, &e2, 10); |
| 268 | if (*e2 || (s == e2)) { |
| 269 | struct passwd *pwd; |
| 270 | if (!(pwd = getpwnam(s))) { |
| 271 | parse_error("user"); |
| 272 | } |
| 273 | sct->m_uid = pwd->pw_uid; |
| 274 | } |
| 275 | |
| 276 | sct->m_gid = strtoul(e, &e2, 10); |
| 277 | if (*e2 || (e == e2)) { |
| 278 | struct group *grp; |
| 279 | if (!(grp = getgrnam(e))) { |
| 280 | parse_error("group"); |
| 281 | } |
| 282 | sct->m_gid = grp->gr_gid; |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | continue; |
| 287 | } |
| 288 | |
| 289 | /* Unknown sections are ignored. */ |
| 290 | |
| 291 | /* Encountering configuration lines prior to seeing a |
| 292 | * section header is treated as an error. This is how |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 293 | * the old code worked, but it may not be desirable. |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 294 | * We may want to simply ignore such lines in case they |
| 295 | * are used in some future version of busybox. */ |
| 296 | if (!section) { |
| 297 | parse_error("keyword outside section"); |
| 298 | } |
| 299 | |
| 300 | } while (1); |
| 301 | |
| 302 | pe_label: |
| 303 | fprintf(stderr, "Parse error in %s, line %d: %s\n", |
| 304 | config_file, lc, err); |
| 305 | |
| 306 | fclose(f); |
| 307 | /* Release any allocated memory before returning. */ |
| 308 | while (sct_head) { |
| 309 | sct = sct_head->m_next; |
| 310 | free(sct_head); |
| 311 | sct_head = sct; |
| 312 | } |
| 313 | return; |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 316 | #else |
Rob Landley | cc59aae | 2005-12-07 23:17:28 +0000 | [diff] [blame] | 317 | #define parse_config_file() |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 318 | #endif /* CONFIG_FEATURE_SUID_CONFIG */ |
| 319 | |
| 320 | #ifdef CONFIG_FEATURE_SUID |
| 321 | static void check_suid (struct BB_applet *applet) |
| 322 | { |
| 323 | uid_t ruid = getuid (); /* real [ug]id */ |
| 324 | uid_t rgid = getgid (); |
| 325 | |
| 326 | #ifdef CONFIG_FEATURE_SUID_CONFIG |
| 327 | if (suid_cfg_readable) { |
| 328 | struct BB_suid_config *sct; |
| 329 | |
| 330 | for (sct = suid_config; sct; sct = sct->m_next) { |
| 331 | if (sct->m_applet == applet) |
| 332 | break; |
| 333 | } |
| 334 | if (sct) { |
| 335 | mode_t m = sct->m_mode; |
| 336 | |
| 337 | if (sct->m_uid == ruid) /* same uid */ |
| 338 | m >>= 6; |
| 339 | else if ((sct->m_gid == rgid) || ingroup (ruid, sct->m_gid)) /* same group / in group */ |
| 340 | m >>= 3; |
| 341 | |
| 342 | if (!(m & S_IXOTH)) /* is x bit not set ? */ |
| 343 | bb_error_msg_and_die ("You have no permission to run this applet!"); |
| 344 | |
| 345 | if ((sct->m_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) { /* *both* have to be set for sgid */ |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 346 | xsetgid(sct->m_gid); |
| 347 | } else xsetgid(rgid); /* no sgid -> drop */ |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 348 | |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 349 | if (sct->m_mode & S_ISUID) xsetuid(sct->m_uid); |
| 350 | else xsetuid(ruid); /* no suid -> drop */ |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 351 | } else { |
Bernhard Reutner-Fischer | e0fe937 | 2006-03-23 16:52:59 +0000 | [diff] [blame] | 352 | /* default: drop all privileges */ |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 353 | xsetgid(rgid); |
| 354 | xsetuid(ruid); |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 355 | } |
| 356 | return; |
| 357 | } else { |
| 358 | #ifndef CONFIG_FEATURE_SUID_CONFIG_QUIET |
| 359 | static int onetime = 0; |
| 360 | |
| 361 | if (!onetime) { |
| 362 | onetime = 1; |
| 363 | fprintf (stderr, "Using fallback suid method\n"); |
| 364 | } |
| 365 | #endif |
| 366 | } |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 367 | #endif |
| 368 | |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 369 | if (applet->need_suid == _BB_SUID_ALWAYS) { |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 370 | if (geteuid()) bb_error_msg_and_die("Applet requires root privileges!"); |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 371 | } else if (applet->need_suid == _BB_SUID_NEVER) { |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 372 | xsetgid(rgid); /* drop all privileges */ |
| 373 | xsetuid(ruid); |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 374 | } |
| 375 | } |
| 376 | #else |
| 377 | #define check_suid(x) |
| 378 | #endif /* CONFIG_FEATURE_SUID */ |
| 379 | |
| 380 | |
| 381 | |
Rob Landley | e1a0f53 | 2006-07-26 15:38:46 +0000 | [diff] [blame] | 382 | #ifdef CONFIG_FEATURE_COMPRESS_USAGE |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 383 | |
Rob Landley | 7e21d5f | 2006-04-27 23:34:46 +0000 | [diff] [blame] | 384 | #include "usage_compressed.h" |
| 385 | #include "unarchive.h" |
| 386 | |
| 387 | static const char *unpack_usage_messages(void) |
| 388 | { |
| 389 | int input[2], output[2], pid; |
| 390 | char *buf; |
| 391 | |
| 392 | if(pipe(input) < 0 || pipe(output) < 0) |
| 393 | exit(1); |
| 394 | |
| 395 | pid = fork(); |
| 396 | switch (pid) { |
| 397 | case -1: /* error */ |
| 398 | exit(1); |
| 399 | case 0: /* child */ |
| 400 | close(input[1]); |
| 401 | close(output[0]); |
| 402 | uncompressStream(input[0], output[1]); |
| 403 | exit(0); |
| 404 | } |
| 405 | /* parent */ |
| 406 | |
| 407 | close(input[0]); |
| 408 | close(output[1]); |
| 409 | pid = fork(); |
| 410 | switch (pid) { |
| 411 | case -1: /* error */ |
| 412 | exit(1); |
| 413 | case 0: /* child */ |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 414 | full_write(input[1], packed_usage, sizeof(packed_usage)); |
Rob Landley | 7e21d5f | 2006-04-27 23:34:46 +0000 | [diff] [blame] | 415 | exit(0); |
| 416 | } |
| 417 | /* parent */ |
| 418 | close(input[1]); |
| 419 | |
| 420 | buf = xmalloc(SIZEOF_usage_messages); |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 421 | full_read(output[0], buf, SIZEOF_usage_messages); |
Rob Landley | 7e21d5f | 2006-04-27 23:34:46 +0000 | [diff] [blame] | 422 | return buf; |
| 423 | } |
| 424 | |
| 425 | #else |
Rob Landley | 1801e9c | 2006-05-03 20:19:14 +0000 | [diff] [blame] | 426 | #define unpack_usage_messages() usage_messages |
Rob Landley | 7e21d5f | 2006-04-27 23:34:46 +0000 | [diff] [blame] | 427 | #endif /* ENABLE_FEATURE_COMPRESS_USAGE */ |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 428 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 429 | void bb_show_usage (void) |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 430 | { |
Rob Landley | 7e21d5f | 2006-04-27 23:34:46 +0000 | [diff] [blame] | 431 | if (ENABLE_SHOW_USAGE) { |
| 432 | const char *format_string; |
| 433 | const char *usage_string = unpack_usage_messages(); |
| 434 | int i; |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 435 | |
Rob Landley | 7e21d5f | 2006-04-27 23:34:46 +0000 | [diff] [blame] | 436 | for (i = applet_using - applets; i > 0;) |
| 437 | if (!*usage_string++) --i; |
| 438 | |
| 439 | format_string = "%s\n\nUsage: %s %s\n\n"; |
| 440 | if (*usage_string == '\b') |
| 441 | format_string = "%s\n\nNo help available.\n\n"; |
| 442 | fprintf (stderr, format_string, bb_msg_full_version, |
| 443 | applet_using->name, usage_string); |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 444 | } |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 445 | |
Rob Landley | 046d6e7 | 2005-10-12 21:50:02 +0000 | [diff] [blame] | 446 | exit (bb_default_error_retval); |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 447 | } |
| 448 | |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 449 | static int applet_name_compare(const void *name, const void *vapplet) |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 450 | { |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 451 | const struct BB_applet *applet = vapplet; |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 452 | |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 453 | return strcmp(name, applet->name); |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | extern const size_t NUM_APPLETS; |
| 457 | |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 458 | struct BB_applet *find_applet_by_name(const char *name) |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 459 | { |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 460 | return bsearch(name, applets, NUM_APPLETS, sizeof(struct BB_applet), |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 461 | applet_name_compare); |
| 462 | } |
| 463 | |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 464 | void run_applet_by_name(const char *name, int argc, char **argv) |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 465 | { |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 466 | if (ENABLE_FEATURE_SUID_CONFIG) parse_config_file(); |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 467 | |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 468 | if (!strncmp(name, "busybox", 7)) busybox_main(argc, argv); |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 469 | /* Do a binary search to find the applet entry given the name. */ |
| 470 | applet_using = find_applet_by_name(name); |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 471 | if (applet_using) { |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 472 | bb_applet_name = applet_using->name; |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 473 | if(argc==2 && !strcmp(argv[1], "--help")) bb_show_usage(); |
| 474 | if(ENABLE_FEATURE_SUID) check_suid(applet_using); |
| 475 | exit((*(applet_using->main))(argc, argv)); |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 476 | } |
| 477 | } |