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