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" |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 16 | #include <assert.h> |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 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 | df51892 | 2006-10-20 13:42:57 +0000 | [diff] [blame] | 26 | #endif |
| 27 | |
Rob Landley | 7e21d5f | 2006-04-27 23:34:46 +0000 | [diff] [blame] | 28 | #if ENABLE_SHOW_USAGE && !ENABLE_FEATURE_COMPRESS_USAGE |
| 29 | static const char usage_messages[] = |
Eric Andersen | 674b08a | 2004-04-06 14:28:35 +0000 | [diff] [blame] | 30 | #define MAKE_USAGE |
| 31 | #include "usage.h" |
Eric Andersen | 674b08a | 2004-04-06 14:28:35 +0000 | [diff] [blame] | 32 | #include "applets.h" |
Eric Andersen | 674b08a | 2004-04-06 14:28:35 +0000 | [diff] [blame] | 33 | ; |
Eric Andersen | 674b08a | 2004-04-06 14:28:35 +0000 | [diff] [blame] | 34 | #undef MAKE_USAGE |
Rob Landley | 73f5470 | 2006-05-01 00:53:40 +0000 | [diff] [blame] | 35 | #else |
| 36 | #define usage_messages 0 |
Bernhard Reutner-Fischer | 81901a0 | 2006-03-31 18:43:55 +0000 | [diff] [blame] | 37 | #endif /* ENABLE_SHOW_USAGE */ |
Rob Landley | 7e21d5f | 2006-04-27 23:34:46 +0000 | [diff] [blame] | 38 | |
Eric Andersen | 2ccfef2 | 2001-03-19 19:30:24 +0000 | [diff] [blame] | 39 | #undef APPLET |
| 40 | #undef APPLET_NOUSAGE |
| 41 | #undef PROTOTYPES |
| 42 | #include "applets.h" |
| 43 | |
Glenn L McGrath | 7fc504c | 2004-02-22 11:13:28 +0000 | [diff] [blame] | 44 | static struct BB_applet *applet_using; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 45 | |
Eric Andersen | 2ccfef2 | 2001-03-19 19:30:24 +0000 | [diff] [blame] | 46 | /* The -1 arises because of the {0,NULL,0,-1} entry above. */ |
Denis Vlasenko | 0ee3999 | 2006-12-24 15:23:28 +0000 | [diff] [blame] | 47 | const unsigned short NUM_APPLETS = (sizeof (applets) / sizeof (struct BB_applet) - 1); |
Eric Andersen | 2ccfef2 | 2001-03-19 19:30:24 +0000 | [diff] [blame] | 48 | |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 49 | |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 50 | #ifdef CONFIG_FEATURE_SUID_CONFIG |
| 51 | |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 52 | #include <ctype.h> |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 53 | |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 54 | #define CONFIG_FILE "/etc/busybox.conf" |
| 55 | |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 56 | /* applets [] is const, so we have to define this "override" structure */ |
Bernhard Reutner-Fischer | 6973abc | 2005-10-28 09:45:07 +0000 | [diff] [blame] | 57 | static struct BB_suid_config |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 58 | { |
Denis Vlasenko | 01a74f9 | 2006-09-23 16:34:39 +0000 | [diff] [blame] | 59 | struct BB_applet *m_applet; |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 60 | |
Denis Vlasenko | 01a74f9 | 2006-09-23 16:34:39 +0000 | [diff] [blame] | 61 | uid_t m_uid; |
| 62 | gid_t m_gid; |
| 63 | mode_t m_mode; |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 64 | |
Denis Vlasenko | 01a74f9 | 2006-09-23 16:34:39 +0000 | [diff] [blame] | 65 | struct BB_suid_config *m_next; |
Bernhard Reutner-Fischer | 6973abc | 2005-10-28 09:45:07 +0000 | [diff] [blame] | 66 | } *suid_config; |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 67 | |
Manuel Novoa III | 7b565a0 | 2004-02-17 10:16:21 +0000 | [diff] [blame] | 68 | static int suid_cfg_readable; |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 69 | |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 70 | /* check if u is member of group g */ |
Denis Vlasenko | 01a74f9 | 2006-09-23 16:34:39 +0000 | [diff] [blame] | 71 | static int ingroup(uid_t u, gid_t g) |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 72 | { |
Denis Vlasenko | 01a74f9 | 2006-09-23 16:34:39 +0000 | [diff] [blame] | 73 | struct group *grp = getgrgid(g); |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 74 | |
Denis Vlasenko | 01a74f9 | 2006-09-23 16:34:39 +0000 | [diff] [blame] | 75 | if (grp) { |
| 76 | char **mem; |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 77 | |
Denis Vlasenko | 01a74f9 | 2006-09-23 16:34:39 +0000 | [diff] [blame] | 78 | for (mem = grp->gr_mem; *mem; mem++) { |
| 79 | struct passwd *pwd = getpwnam(*mem); |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 80 | |
Denis Vlasenko | 01a74f9 | 2006-09-23 16:34:39 +0000 | [diff] [blame] | 81 | if (pwd && (pwd->pw_uid == u)) |
| 82 | return 1; |
| 83 | } |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 84 | } |
Denis Vlasenko | 01a74f9 | 2006-09-23 16:34:39 +0000 | [diff] [blame] | 85 | return 0; |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 88 | /* This should probably be a libbb routine. In that case, |
| 89 | * I'd probably rename it to something like bb_trimmed_slice. |
| 90 | */ |
| 91 | static 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 Landley | ea224be | 2006-06-18 20:20:07 +0000 | [diff] [blame] | 105 | return skip_whitespace(s); |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 108 | |
| 109 | #define parse_error(x) { err=x; goto pe_label; } |
| 110 | |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 111 | /* Don't depend on the tools to combine strings. */ |
| 112 | static const char config_file[] = CONFIG_FILE; |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 113 | |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 114 | /* There are 4 chars + 1 nul for each of user/group/other. */ |
| 115 | static 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. */ |
| 120 | static 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 | |
| 127 | static void parse_config_file(void) |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 128 | { |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 129 | 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 McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 139 | |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 140 | assert(!suid_config); /* Should be set to NULL by bss init. */ |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 141 | |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 142 | 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 Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame] | 146 | || !(f = fopen(config_file, "r")) /* Cannot open? */ |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 147 | ) { |
| 148 | return; |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 149 | } |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 150 | |
Manuel Novoa III | 7b565a0 | 2004-02-17 10:16:21 +0000 | [diff] [blame] | 151 | suid_cfg_readable = 1; |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 152 | sct_head = NULL; |
| 153 | section = lc = 0; |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 154 | |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 155 | 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 Landley | ea224be | 2006-06-18 20:20:07 +0000 | [diff] [blame] | 246 | e = skip_whitespace(e+1); |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 247 | |
| 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-Fischer | 7ca61b6 | 2006-01-15 14:04:57 +0000 | [diff] [blame] | 258 | |
Rob Landley | ea224be | 2006-06-18 20:20:07 +0000 | [diff] [blame] | 259 | s = skip_whitespace(e); |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 260 | |
| 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 Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 275 | struct passwd *pwd = getpwnam(s); |
| 276 | if (!pwd) { |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 277 | 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 Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 299 | * the old code worked, but it may not be desirable. |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 300 | * 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 Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 322 | #else |
Rob Landley | cc59aae | 2005-12-07 23:17:28 +0000 | [diff] [blame] | 323 | #define parse_config_file() |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 324 | #endif /* CONFIG_FEATURE_SUID_CONFIG */ |
| 325 | |
| 326 | #ifdef CONFIG_FEATURE_SUID |
Denis Vlasenko | 13c5a68 | 2006-10-16 22:39:51 +0000 | [diff] [blame] | 327 | static void check_suid(struct BB_applet *applet) |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 328 | { |
Denis Vlasenko | 13c5a68 | 2006-10-16 22:39:51 +0000 | [diff] [blame] | 329 | uid_t ruid = getuid(); /* real [ug]id */ |
| 330 | uid_t rgid = getgid(); |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 331 | |
| 332 | #ifdef CONFIG_FEATURE_SUID_CONFIG |
Denis Vlasenko | 01a74f9 | 2006-09-23 16:34:39 +0000 | [diff] [blame] | 333 | if (suid_cfg_readable) { |
| 334 | struct BB_suid_config *sct; |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 335 | |
Denis Vlasenko | 01a74f9 | 2006-09-23 16:34:39 +0000 | [diff] [blame] | 336 | 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 Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 342 | |
Denis Vlasenko | 01a74f9 | 2006-09-23 16:34:39 +0000 | [diff] [blame] | 343 | if (sct->m_uid == ruid) /* same uid */ |
| 344 | m >>= 6; |
Denis Vlasenko | 13c5a68 | 2006-10-16 22:39:51 +0000 | [diff] [blame] | 345 | else if ((sct->m_gid == rgid) || ingroup(ruid, sct->m_gid)) /* same group / in group */ |
Denis Vlasenko | 01a74f9 | 2006-09-23 16:34:39 +0000 | [diff] [blame] | 346 | m >>= 3; |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 347 | |
Denis Vlasenko | 01a74f9 | 2006-09-23 16:34:39 +0000 | [diff] [blame] | 348 | if (!(m & S_IXOTH)) /* is x bit not set ? */ |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame] | 349 | 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] | 350 | |
Denis Vlasenko | 01a74f9 | 2006-09-23 16:34:39 +0000 | [diff] [blame] | 351 | 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 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 | 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 Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 363 | } else { |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 364 | #ifndef CONFIG_FEATURE_SUID_CONFIG_QUIET |
Denis Vlasenko | 01a74f9 | 2006-09-23 16:34:39 +0000 | [diff] [blame] | 365 | static int onetime = 0; |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 366 | |
Denis Vlasenko | 01a74f9 | 2006-09-23 16:34:39 +0000 | [diff] [blame] | 367 | if (!onetime) { |
| 368 | onetime = 1; |
Denis Vlasenko | 13c5a68 | 2006-10-16 22:39:51 +0000 | [diff] [blame] | 369 | fprintf(stderr, "Using fallback suid method\n"); |
Denis Vlasenko | 01a74f9 | 2006-09-23 16:34:39 +0000 | [diff] [blame] | 370 | } |
| 371 | #endif |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 372 | } |
| 373 | #endif |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 374 | |
Denis Vlasenko | 01a74f9 | 2006-09-23 16:34:39 +0000 | [diff] [blame] | 375 | if (applet->need_suid == _BB_SUID_ALWAYS) { |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame] | 376 | if (geteuid()) bb_error_msg_and_die("applet requires root privileges!"); |
Denis Vlasenko | 01a74f9 | 2006-09-23 16:34:39 +0000 | [diff] [blame] | 377 | } else if (applet->need_suid == _BB_SUID_NEVER) { |
| 378 | xsetgid(rgid); /* drop all privileges */ |
| 379 | xsetuid(ruid); |
| 380 | } |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 381 | } |
| 382 | #else |
| 383 | #define check_suid(x) |
| 384 | #endif /* CONFIG_FEATURE_SUID */ |
| 385 | |
| 386 | |
| 387 | |
Rob Landley | e1a0f53 | 2006-07-26 15:38:46 +0000 | [diff] [blame] | 388 | #ifdef CONFIG_FEATURE_COMPRESS_USAGE |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 389 | |
Rob Landley | 7e21d5f | 2006-04-27 23:34:46 +0000 | [diff] [blame] | 390 | #include "usage_compressed.h" |
| 391 | #include "unarchive.h" |
| 392 | |
| 393 | static 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 Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 420 | full_write(input[1], packed_usage, sizeof(packed_usage)); |
Rob Landley | 7e21d5f | 2006-04-27 23:34:46 +0000 | [diff] [blame] | 421 | exit(0); |
| 422 | } |
| 423 | /* parent */ |
| 424 | close(input[1]); |
| 425 | |
| 426 | buf = xmalloc(SIZEOF_usage_messages); |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 427 | full_read(output[0], buf, SIZEOF_usage_messages); |
Rob Landley | 7e21d5f | 2006-04-27 23:34:46 +0000 | [diff] [blame] | 428 | return buf; |
| 429 | } |
| 430 | |
| 431 | #else |
Rob Landley | 1801e9c | 2006-05-03 20:19:14 +0000 | [diff] [blame] | 432 | #define unpack_usage_messages() usage_messages |
Rob Landley | 7e21d5f | 2006-04-27 23:34:46 +0000 | [diff] [blame] | 433 | #endif /* ENABLE_FEATURE_COMPRESS_USAGE */ |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 434 | |
Denis Vlasenko | 01a74f9 | 2006-09-23 16:34:39 +0000 | [diff] [blame] | 435 | void bb_show_usage(void) |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 436 | { |
Rob Landley | 7e21d5f | 2006-04-27 23:34:46 +0000 | [diff] [blame] | 437 | if (ENABLE_SHOW_USAGE) { |
| 438 | const char *format_string; |
| 439 | const char *usage_string = unpack_usage_messages(); |
| 440 | int i; |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 441 | |
Rob Landley | 7e21d5f | 2006-04-27 23:34:46 +0000 | [diff] [blame] | 442 | 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 Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 448 | fprintf(stderr, format_string, bb_msg_full_version, |
Rob Landley | 7e21d5f | 2006-04-27 23:34:46 +0000 | [diff] [blame] | 449 | applet_using->name, usage_string); |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 450 | } |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 451 | |
Denis Vlasenko | 4092082 | 2006-10-03 20:28:06 +0000 | [diff] [blame] | 452 | exit(xfunc_error_retval); |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 453 | } |
| 454 | |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 455 | static int applet_name_compare(const void *name, const void *vapplet) |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 456 | { |
Denis Vlasenko | 01a74f9 | 2006-09-23 16:34:39 +0000 | [diff] [blame] | 457 | const struct BB_applet *applet = vapplet; |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 458 | |
Denis Vlasenko | 01a74f9 | 2006-09-23 16:34:39 +0000 | [diff] [blame] | 459 | return strcmp(name, applet->name); |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 462 | struct BB_applet *find_applet_by_name(const char *name) |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 463 | { |
Denis Vlasenko | 01a74f9 | 2006-09-23 16:34:39 +0000 | [diff] [blame] | 464 | return bsearch(name, applets, NUM_APPLETS, sizeof(struct BB_applet), |
| 465 | applet_name_compare); |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 466 | } |
| 467 | |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 468 | void run_applet_by_name(const char *name, int argc, char **argv) |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 469 | { |
Denis Vlasenko | 0ee3999 | 2006-12-24 15:23:28 +0000 | [diff] [blame] | 470 | if (ENABLE_FEATURE_SUID_CONFIG) |
| 471 | parse_config_file(); |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 472 | |
Denis Vlasenko | 0ee3999 | 2006-12-24 15:23:28 +0000 | [diff] [blame] | 473 | if (!strncmp(name, "busybox", 7)) |
| 474 | exit(busybox_main(argc, argv)); |
Rob Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 475 | /* Do a binary search to find the applet entry given the name. */ |
| 476 | applet_using = find_applet_by_name(name); |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 477 | if (applet_using) { |
Denis Vlasenko | 8f8f268 | 2006-10-03 21:00:43 +0000 | [diff] [blame] | 478 | applet_name = applet_using->name; |
Denis Vlasenko | 0ee3999 | 2006-12-24 15:23:28 +0000 | [diff] [blame] | 479 | 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 Landley | 8a7a678 | 2005-09-05 04:13:33 +0000 | [diff] [blame] | 484 | } |
| 485 | } |