blob: 77e4fdbfe515483320aff53fa442e333558598f5 [file] [log] [blame]
Eric Andersenaad1a882001-03-16 22:47:14 +00001/* 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 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
Glenn L McGrath2faee7b2003-05-26 14:09:12 +000023 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
Eric Andersenaad1a882001-03-16 22:47:14 +000024 * Permission has been granted to redistribute this code under the GPL.
25 *
26 */
27
Robert Grieblc9aca452002-06-04 20:06:25 +000028#include <unistd.h>
Eric Andersenaad1a882001-03-16 22:47:14 +000029#include <stdio.h>
30#include <stdlib.h>
Eric Andersen08ff8a42001-03-23 17:02:05 +000031#include <string.h>
Manuel Novoa III 31b98dd2004-02-01 10:03:05 +000032#include <assert.h>
Eric Andersenaad1a882001-03-16 22:47:14 +000033#include "busybox.h"
34
Bernhard Reutner-Fischer81901a02006-03-31 18:43:55 +000035#if ENABLE_SHOW_USAGE
Eric Andersen674b08a2004-04-06 14:28:35 +000036const char usage_messages[] =
37
38#define MAKE_USAGE
39#include "usage.h"
40
41#include "applets.h"
42
43;
44
45#undef MAKE_USAGE
Bernhard Reutner-Fischer81901a02006-03-31 18:43:55 +000046#endif /* ENABLE_SHOW_USAGE */
Eric Andersen2ccfef22001-03-19 19:30:24 +000047#undef APPLET
48#undef APPLET_NOUSAGE
49#undef PROTOTYPES
50#include "applets.h"
51
Eric Andersen674b08a2004-04-06 14:28:35 +000052
Glenn L McGrath7fc504c2004-02-22 11:13:28 +000053static struct BB_applet *applet_using;
Eric Andersenaad1a882001-03-16 22:47:14 +000054
Eric Andersen2ccfef22001-03-19 19:30:24 +000055/* The -1 arises because of the {0,NULL,0,-1} entry above. */
56const size_t NUM_APPLETS = (sizeof (applets) / sizeof (struct BB_applet) - 1);
57
Robert Grieblc9aca452002-06-04 20:06:25 +000058
Robert Grieblc9aca452002-06-04 20:06:25 +000059#ifdef CONFIG_FEATURE_SUID_CONFIG
60
61#include <sys/stat.h>
62#include <ctype.h>
Eric Andersen887ca792002-07-03 23:19:26 +000063#include "pwd_.h"
64#include "grp_.h"
Robert Grieblc9aca452002-06-04 20:06:25 +000065
Robert Grieblc9aca452002-06-04 20:06:25 +000066#define CONFIG_FILE "/etc/busybox.conf"
67
Glenn L McGrathb37367a2002-08-22 13:12:40 +000068/* applets [] is const, so we have to define this "override" structure */
Bernhard Reutner-Fischer6973abc2005-10-28 09:45:07 +000069static struct BB_suid_config
Glenn L McGrath2faee7b2003-05-26 14:09:12 +000070{
71 struct BB_applet *m_applet;
Robert Grieblc9aca452002-06-04 20:06:25 +000072
Glenn L McGrath2faee7b2003-05-26 14:09:12 +000073 uid_t m_uid;
74 gid_t m_gid;
75 mode_t m_mode;
Glenn L McGrathb37367a2002-08-22 13:12:40 +000076
Glenn L McGrath2faee7b2003-05-26 14:09:12 +000077 struct BB_suid_config *m_next;
Bernhard Reutner-Fischer6973abc2005-10-28 09:45:07 +000078} *suid_config;
Robert Grieblc9aca452002-06-04 20:06:25 +000079
Manuel Novoa III 7b565a02004-02-17 10:16:21 +000080static int suid_cfg_readable;
Robert Grieblc9aca452002-06-04 20:06:25 +000081
Glenn L McGrathb37367a2002-08-22 13:12:40 +000082/* check if u is member of group g */
Rob Landley8a7a6782005-09-05 04:13:33 +000083static int ingroup (uid_t u, gid_t g)
Robert Grieblc9aca452002-06-04 20:06:25 +000084{
Glenn L McGrath2faee7b2003-05-26 14:09:12 +000085 struct group *grp = getgrgid (g);
Glenn L McGrathb37367a2002-08-22 13:12:40 +000086
Glenn L McGrath2faee7b2003-05-26 14:09:12 +000087 if (grp) {
88 char **mem;
Glenn L McGrathb37367a2002-08-22 13:12:40 +000089
Glenn L McGrath2faee7b2003-05-26 14:09:12 +000090 for (mem = grp->gr_mem; *mem; mem++) {
91 struct passwd *pwd = getpwnam (*mem);
Glenn L McGrathb37367a2002-08-22 13:12:40 +000092
Glenn L McGrath2faee7b2003-05-26 14:09:12 +000093 if (pwd && (pwd->pw_uid == u))
94 return 1;
Robert Grieblc9aca452002-06-04 20:06:25 +000095 }
Glenn L McGrath2faee7b2003-05-26 14:09:12 +000096 }
97 return 0;
Robert Grieblc9aca452002-06-04 20:06:25 +000098}
99
Manuel Novoa III 31b98dd2004-02-01 10:03:05 +0000100/* This should probably be a libbb routine. In that case,
101 * I'd probably rename it to something like bb_trimmed_slice.
102 */
103static char *get_trimmed_slice(char *s, char *e)
104{
105 /* First, consider the value at e to be nul and back up until we
106 * reach a non-space char. Set the char after that (possibly at
107 * the original e) to nul. */
108 while (e-- > s) {
109 if (!isspace(*e)) {
110 break;
111 }
112 }
113 e[1] = 0;
114
115 /* Next, advance past all leading space and return a ptr to the
116 * first non-space char; possibly the terminating nul. */
117 return (char *) bb_skip_whitespace(s);
118}
119
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000120
121#define parse_error(x) { err=x; goto pe_label; }
122
Manuel Novoa III 31b98dd2004-02-01 10:03:05 +0000123/* Don't depend on the tools to combine strings. */
124static const char config_file[] = CONFIG_FILE;
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000125
Manuel Novoa III 31b98dd2004-02-01 10:03:05 +0000126/* There are 4 chars + 1 nul for each of user/group/other. */
127static const char mode_chars[] = "Ssx-\0Ssx-\0Ttx-";
128
129/* We don't supply a value for the nul, so an index adjustment is
130 * necessary below. Also, we use unsigned short here to save some
131 * space even though these are really mode_t values. */
132static const unsigned short mode_mask[] = {
133 /* SST sst xxx --- */
134 S_ISUID, S_ISUID|S_IXUSR, S_IXUSR, 0, /* user */
135 S_ISGID, S_ISGID|S_IXGRP, S_IXGRP, 0, /* group */
136 0, S_IXOTH, S_IXOTH, 0 /* other */
137};
138
139static void parse_config_file(void)
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000140{
Manuel Novoa III 31b98dd2004-02-01 10:03:05 +0000141 struct BB_suid_config *sct_head;
142 struct BB_suid_config *sct;
143 struct BB_applet *applet;
144 FILE *f;
145 char *err;
146 char *s;
147 char *e;
148 int i, lc, section;
149 char buffer[256];
150 struct stat st;
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000151
Manuel Novoa III 31b98dd2004-02-01 10:03:05 +0000152 assert(!suid_config); /* Should be set to NULL by bss init. */
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000153
Manuel Novoa III 31b98dd2004-02-01 10:03:05 +0000154 if ((stat(config_file, &st) != 0) /* No config file? */
155 || !S_ISREG(st.st_mode) /* Not a regular file? */
156 || (st.st_uid != 0) /* Not owned by root? */
157 || (st.st_mode & (S_IWGRP | S_IWOTH)) /* Writable by non-root? */
158 || !(f = fopen(config_file, "r")) /* Can not open? */
159 ) {
160 return;
Robert Grieblc9aca452002-06-04 20:06:25 +0000161 }
Glenn L McGrathb37367a2002-08-22 13:12:40 +0000162
Manuel Novoa III 7b565a02004-02-17 10:16:21 +0000163 suid_cfg_readable = 1;
Manuel Novoa III 31b98dd2004-02-01 10:03:05 +0000164 sct_head = NULL;
165 section = lc = 0;
Robert Grieblc9aca452002-06-04 20:06:25 +0000166
Manuel Novoa III 31b98dd2004-02-01 10:03:05 +0000167 do {
168 s = buffer;
169
170 if (!fgets(s, sizeof(buffer), f)) { /* Are we done? */
171 if (ferror(f)) { /* Make sure it wasn't a read error. */
172 parse_error("reading");
173 }
174 fclose(f);
175 suid_config = sct_head; /* Success, so set the pointer. */
176 return;
177 }
178
179 lc++; /* Got a (partial) line. */
180
181 /* If a line is too long for our buffer, we consider it an error.
182 * The following test does mistreat one corner case though.
183 * If the final line of the file does not end with a newline and
184 * yet exactly fills the buffer, it will be treated as too long
185 * even though there isn't really a problem. But it isn't really
186 * worth adding code to deal with such an unlikely situation, and
187 * we do err on the side of caution. Besides, the line would be
188 * too long if it did end with a newline. */
189 if (!strchr(s, '\n') && !feof(f)) {
190 parse_error("line too long");
191 }
192
193 /* Trim leading and trailing whitespace, ignoring comments, and
194 * check if the resulting string is empty. */
195 if (!*(s = get_trimmed_slice(s, strchrnul(s, '#')))) {
196 continue;
197 }
198
199 /* Check for a section header. */
200
201 if (*s == '[') {
202 /* Unlike the old code, we ignore leading and trailing
203 * whitespace for the section name. We also require that
204 * there are no stray characters after the closing bracket. */
205 if (!(e = strchr(s, ']')) /* Missing right bracket? */
206 || e[1] /* Trailing characters? */
207 || !*(s = get_trimmed_slice(s+1, e)) /* Missing name? */
208 ) {
209 parse_error("section header");
210 }
211 /* Right now we only have one section so just check it.
212 * If more sections are added in the future, please don't
213 * resort to cascading ifs with multiple strcasecmp calls.
214 * That kind of bloated code is all too common. A loop
215 * and a string table would be a better choice unless the
216 * number of sections is very small. */
217 if (strcasecmp(s, "SUID") == 0) {
218 section = 1;
219 continue;
220 }
221 section = -1; /* Unknown section so set to skip. */
222 continue;
223 }
224
225 /* Process sections. */
226
227 if (section == 1) { /* SUID */
228 /* Since we trimmed leading and trailing space above, we're
229 * now looking for strings of the form
230 * <key>[::space::]*=[::space::]*<value>
231 * where both key and value could contain inner whitespace. */
232
233 /* First get the key (an applet name in our case). */
234 if (!!(e = strchr(s, '='))) {
235 s = get_trimmed_slice(s, e);
236 }
237 if (!e || !*s) { /* Missing '=' or empty key. */
238 parse_error("keyword");
239 }
240
241 /* Ok, we have an applet name. Process the rhs if this
242 * applet is currently built in and ignore it otherwise.
243 * Note: This can hide config file bugs which only pop
244 * up when the busybox configuration is changed. */
245 if ((applet = find_applet_by_name(s))) {
246 /* Note: We currently don't check for duplicates!
247 * The last config line for each applet will be the
248 * one used since we insert at the head of the list.
249 * I suppose this could be considered a feature. */
250 sct = xmalloc(sizeof(struct BB_suid_config));
251 sct->m_applet = applet;
252 sct->m_mode = 0;
253 sct->m_next = sct_head;
254 sct_head = sct;
255
256 /* Get the specified mode. */
257
258 e = (char *) bb_skip_whitespace(e+1);
259
260 for (i=0 ; i < 3 ; i++) {
261 const char *q;
262 if (!*(q = strchrnul(mode_chars + 5*i, *e++))) {
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-Fischer7ca61b62006-01-15 14:04:57 +0000270
Manuel Novoa III 31b98dd2004-02-01 10:03:05 +0000271 s = (char *) bb_skip_whitespace(e);
272
273 /* Note: We require whitespace between the mode and the
274 * user/group info. */
275 if ((s == e) || !(e = strchr(s, '.'))) {
276 parse_error("<uid>.<gid>");
277 }
278 *e++ = 0;
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... */
282 {
283 char *e2;
284
285 sct->m_uid = strtoul(s, &e2, 10);
286 if (*e2 || (s == e2)) {
287 struct passwd *pwd;
288 if (!(pwd = getpwnam(s))) {
289 parse_error("user");
290 }
291 sct->m_uid = pwd->pw_uid;
292 }
293
294 sct->m_gid = strtoul(e, &e2, 10);
295 if (*e2 || (e == e2)) {
296 struct group *grp;
297 if (!(grp = getgrnam(e))) {
298 parse_error("group");
299 }
300 sct->m_gid = grp->gr_gid;
301 }
302 }
303 }
304 continue;
305 }
306
307 /* Unknown sections are ignored. */
308
309 /* Encountering configuration lines prior to seeing a
310 * section header is treated as an error. This is how
Eric Andersenaff114c2004-04-14 17:51:38 +0000311 * the old code worked, but it may not be desirable.
Manuel Novoa III 31b98dd2004-02-01 10:03:05 +0000312 * We may want to simply ignore such lines in case they
313 * are used in some future version of busybox. */
314 if (!section) {
315 parse_error("keyword outside section");
316 }
317
318 } while (1);
319
320 pe_label:
321 fprintf(stderr, "Parse error in %s, line %d: %s\n",
322 config_file, lc, err);
323
324 fclose(f);
325 /* Release any allocated memory before returning. */
326 while (sct_head) {
327 sct = sct_head->m_next;
328 free(sct_head);
329 sct_head = sct;
330 }
331 return;
Robert Grieblc9aca452002-06-04 20:06:25 +0000332}
333
Rob Landley8a7a6782005-09-05 04:13:33 +0000334#else
Rob Landleycc59aae2005-12-07 23:17:28 +0000335#define parse_config_file()
Rob Landley8a7a6782005-09-05 04:13:33 +0000336#endif /* CONFIG_FEATURE_SUID_CONFIG */
337
338#ifdef CONFIG_FEATURE_SUID
339static void check_suid (struct BB_applet *applet)
340{
341 uid_t ruid = getuid (); /* real [ug]id */
342 uid_t rgid = getgid ();
343
344#ifdef CONFIG_FEATURE_SUID_CONFIG
345 if (suid_cfg_readable) {
346 struct BB_suid_config *sct;
347
348 for (sct = suid_config; sct; sct = sct->m_next) {
349 if (sct->m_applet == applet)
350 break;
351 }
352 if (sct) {
353 mode_t m = sct->m_mode;
354
355 if (sct->m_uid == ruid) /* same uid */
356 m >>= 6;
357 else if ((sct->m_gid == rgid) || ingroup (ruid, sct->m_gid)) /* same group / in group */
358 m >>= 3;
359
360 if (!(m & S_IXOTH)) /* is x bit not set ? */
361 bb_error_msg_and_die ("You have no permission to run this applet!");
362
363 if ((sct->m_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) { /* *both* have to be set for sgid */
364 if (setegid (sct->m_gid))
365 bb_error_msg_and_die
366 ("BusyBox binary has insufficient rights to set proper GID for applet!");
367 } else
368 setgid (rgid); /* no sgid -> drop */
369
370 if (sct->m_mode & S_ISUID) {
371 if (seteuid (sct->m_uid))
372 bb_error_msg_and_die
373 ("BusyBox binary has insufficient rights to set proper UID for applet!");
374 } else
375 setuid (ruid); /* no suid -> drop */
376 } else {
Bernhard Reutner-Fischere0fe9372006-03-23 16:52:59 +0000377 /* default: drop all privileges */
Rob Landley8a7a6782005-09-05 04:13:33 +0000378 setgid (rgid);
379 setuid (ruid);
380 }
381 return;
382 } else {
383#ifndef CONFIG_FEATURE_SUID_CONFIG_QUIET
384 static int onetime = 0;
385
386 if (!onetime) {
387 onetime = 1;
388 fprintf (stderr, "Using fallback suid method\n");
389 }
390#endif
391 }
Robert Grieblc9aca452002-06-04 20:06:25 +0000392#endif
393
Rob Landley8a7a6782005-09-05 04:13:33 +0000394 if (applet->need_suid == _BB_SUID_ALWAYS) {
395 if (geteuid () != 0)
Bernhard Reutner-Fischere0fe9372006-03-23 16:52:59 +0000396 bb_error_msg_and_die ("This applet requires root privileges!");
Rob Landley8a7a6782005-09-05 04:13:33 +0000397 } else if (applet->need_suid == _BB_SUID_NEVER) {
Bernhard Reutner-Fischere0fe9372006-03-23 16:52:59 +0000398 setgid (rgid); /* drop all privileges */
Rob Landley8a7a6782005-09-05 04:13:33 +0000399 setuid (ruid);
400 }
401}
402#else
403#define check_suid(x)
404#endif /* CONFIG_FEATURE_SUID */
405
406
407
408
409
Rob Landleydfba7412006-03-06 20:47:33 +0000410void bb_show_usage (void)
Rob Landley8a7a6782005-09-05 04:13:33 +0000411{
Bernhard Reutner-Fischer81901a02006-03-31 18:43:55 +0000412#if ENABLE_SHOW_USAGE
Rob Landley8a7a6782005-09-05 04:13:33 +0000413 const char *format_string;
414 const char *usage_string = usage_messages;
415 int i;
416
417 for (i = applet_using - applets; i > 0;) {
418 if (!*usage_string++) {
419 --i;
420 }
421 }
422
423 format_string = "%s\n\nUsage: %s %s\n\n";
424 if (*usage_string == '\b')
425 format_string = "%s\n\nNo help available.\n\n";
426 fprintf (stderr, format_string, bb_msg_full_version, applet_using->name,
427 usage_string);
Bernhard Reutner-Fischer81901a02006-03-31 18:43:55 +0000428#endif /* ENABLE_SHOW_USAGE */
Rob Landley8a7a6782005-09-05 04:13:33 +0000429
Rob Landley046d6e72005-10-12 21:50:02 +0000430 exit (bb_default_error_retval);
Rob Landley8a7a6782005-09-05 04:13:33 +0000431}
432
433static int applet_name_compare (const void *x, const void *y)
434{
435 const char *name = x;
436 const struct BB_applet *applet = y;
437
438 return strcmp (name, applet->name);
439}
440
441extern const size_t NUM_APPLETS;
442
443struct BB_applet *find_applet_by_name (const char *name)
444{
445 return bsearch (name, applets, NUM_APPLETS, sizeof (struct BB_applet),
446 applet_name_compare);
447}
448
449void run_applet_by_name (const char *name, int argc, char **argv)
450{
451 if(ENABLE_FEATURE_SUID_CONFIG) parse_config_file ();
452
453 if(!strncmp(name, "busybox", 7)) busybox_main(argc, argv);
454 /* Do a binary search to find the applet entry given the name. */
455 applet_using = find_applet_by_name(name);
456 if(applet_using) {
457 bb_applet_name = applet_using->name;
458 if(argc==2 && !strcmp(argv[1], "--help")) bb_show_usage ();
459 if(ENABLE_FEATURE_SUID) check_suid (applet_using);
460 exit ((*(applet_using->main)) (argc, argv));
461 }
462}
463
Robert Grieblc9aca452002-06-04 20:06:25 +0000464
Eric Andersenaad1a882001-03-16 22:47:14 +0000465/* END CODE */
466/*
Manuel Novoa III 31b98dd2004-02-01 10:03:05 +0000467 Local Variables:
468 c-file-style: "linux"
469 c-basic-offset: 4
470 tab-width: 4
Eric Andersenaad1a882001-03-16 22:47:14 +0000471End:
472*/