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