blob: 8bb36dd6a5140223c23ca595ad286673d9b694f0 [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
Eric Andersen2ccfef22001-03-19 19:30:24 +000035#undef APPLET
36#undef APPLET_NOUSAGE
37#undef PROTOTYPES
38#include "applets.h"
39
Eric Andersenaad1a882001-03-16 22:47:14 +000040struct BB_applet *applet_using;
41
Eric Andersen2ccfef22001-03-19 19:30:24 +000042/* The -1 arises because of the {0,NULL,0,-1} entry above. */
43const size_t NUM_APPLETS = (sizeof (applets) / sizeof (struct BB_applet) - 1);
44
Robert Grieblc9aca452002-06-04 20:06:25 +000045
46#ifdef CONFIG_FEATURE_SUID
47
Glenn L McGrath2faee7b2003-05-26 14:09:12 +000048static void check_suid (struct BB_applet *app);
Robert Grieblc9aca452002-06-04 20:06:25 +000049
50#ifdef CONFIG_FEATURE_SUID_CONFIG
51
52#include <sys/stat.h>
53#include <ctype.h>
Eric Andersen887ca792002-07-03 23:19:26 +000054#include "pwd_.h"
55#include "grp_.h"
Robert Grieblc9aca452002-06-04 20:06:25 +000056
Manuel Novoa III 31b98dd2004-02-01 10:03:05 +000057static void parse_config_file (void);
Robert Grieblc9aca452002-06-04 20:06:25 +000058
59#define CONFIG_FILE "/etc/busybox.conf"
60
Glenn L McGrathb37367a2002-08-22 13:12:40 +000061/* applets [] is const, so we have to define this "override" structure */
Glenn L McGrath2faee7b2003-05-26 14:09:12 +000062struct BB_suid_config
63{
64 struct BB_applet *m_applet;
Robert Grieblc9aca452002-06-04 20:06:25 +000065
Glenn L McGrath2faee7b2003-05-26 14:09:12 +000066 uid_t m_uid;
67 gid_t m_gid;
68 mode_t m_mode;
Glenn L McGrathb37367a2002-08-22 13:12:40 +000069
Glenn L McGrath2faee7b2003-05-26 14:09:12 +000070 struct BB_suid_config *m_next;
Robert Grieblc9aca452002-06-04 20:06:25 +000071};
72
73static struct BB_suid_config *suid_config;
Manuel Novoa III 7b565a02004-02-17 10:16:21 +000074static int suid_cfg_readable;
Robert Grieblc9aca452002-06-04 20:06:25 +000075
Glenn L McGrathb37367a2002-08-22 13:12:40 +000076#endif /* CONFIG_FEATURE_SUID_CONFIG */
Robert Grieblc9aca452002-06-04 20:06:25 +000077
Glenn L McGrathb37367a2002-08-22 13:12:40 +000078#endif /* CONFIG_FEATURE_SUID */
Robert Grieblc9aca452002-06-04 20:06:25 +000079
80
81
Glenn L McGrath2faee7b2003-05-26 14:09:12 +000082extern void
83bb_show_usage (void)
Eric Andersenaad1a882001-03-16 22:47:14 +000084{
Glenn L McGrath2faee7b2003-05-26 14:09:12 +000085 const char *format_string;
86 const char *usage_string = usage_messages;
87 int i;
Eric Andersenaad1a882001-03-16 22:47:14 +000088
Glenn L McGrath2faee7b2003-05-26 14:09:12 +000089 for (i = applet_using - applets; i > 0;) {
90 if (!*usage_string++) {
91 --i;
Eric Andersenaad1a882001-03-16 22:47:14 +000092 }
Glenn L McGrath2faee7b2003-05-26 14:09:12 +000093 }
Glenn L McGrath6ead3ab2002-11-28 08:33:04 +000094
Glenn L McGrath2faee7b2003-05-26 14:09:12 +000095 format_string = "%s\n\nUsage: %s %s\n\n";
96 if (*usage_string == '\b')
97 format_string = "%s\n\nNo help available.\n\n";
98 fprintf (stderr, format_string, bb_msg_full_version, applet_using->name,
99 usage_string);
Glenn L McGrath4d4ef192002-11-28 21:49:06 +0000100
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000101 exit (EXIT_FAILURE);
Eric Andersenaad1a882001-03-16 22:47:14 +0000102}
103
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000104static int
105applet_name_compare (const void *x, const void *y)
Eric Andersenaad1a882001-03-16 22:47:14 +0000106{
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000107 const char *name = x;
108 const struct BB_applet *applet = y;
Eric Andersenaad1a882001-03-16 22:47:14 +0000109
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000110 return strcmp (name, applet->name);
Eric Andersenaad1a882001-03-16 22:47:14 +0000111}
112
Eric Andersen15576262001-06-24 06:09:14 +0000113extern const size_t NUM_APPLETS;
Eric Andersenaad1a882001-03-16 22:47:14 +0000114
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000115struct BB_applet *
116find_applet_by_name (const char *name)
Eric Andersenaad1a882001-03-16 22:47:14 +0000117{
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000118 return bsearch (name, applets, NUM_APPLETS, sizeof (struct BB_applet),
119 applet_name_compare);
Eric Andersenaad1a882001-03-16 22:47:14 +0000120}
121
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000122void
123run_applet_by_name (const char *name, int argc, char **argv)
Eric Andersenaad1a882001-03-16 22:47:14 +0000124{
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000125 static int recurse_level = 0;
126 extern int been_there_done_that; /* From busybox.c */
Mark Whitleybd4b6212001-06-15 16:54:25 +0000127
Robert Grieblc9aca452002-06-04 20:06:25 +0000128#ifdef CONFIG_FEATURE_SUID_CONFIG
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000129 if (recurse_level == 0)
Manuel Novoa III 31b98dd2004-02-01 10:03:05 +0000130 parse_config_file ();
Robert Grieblc9aca452002-06-04 20:06:25 +0000131#endif
132
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000133 recurse_level++;
134 /* Do a binary search to find the applet entry given the name. */
135 if ((applet_using = find_applet_by_name (name)) != NULL) {
136 bb_applet_name = applet_using->name;
137 if (argv[1] && strcmp (argv[1], "--help") == 0) {
138 if (strcmp (applet_using->name, "busybox") == 0) {
139 if (argv[2])
140 applet_using = find_applet_by_name (argv[2]);
141 else
142 applet_using = NULL;
143 }
144 if (applet_using)
145 bb_show_usage ();
146 been_there_done_that = 1;
147 busybox_main (0, NULL);
148 }
Robert Grieblc9aca452002-06-04 20:06:25 +0000149#ifdef CONFIG_FEATURE_SUID
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000150 check_suid (applet_using);
Robert Grieblc9aca452002-06-04 20:06:25 +0000151#endif
Glenn L McGrathb37367a2002-08-22 13:12:40 +0000152
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000153 exit ((*(applet_using->main)) (argc, argv));
154 }
155 /* Just in case they have renamed busybox - Check argv[1] */
156 if (recurse_level == 1) {
157 run_applet_by_name ("busybox", argc, argv);
158 }
159 recurse_level--;
Eric Andersenaad1a882001-03-16 22:47:14 +0000160}
161
162
Robert Grieblc9aca452002-06-04 20:06:25 +0000163#ifdef CONFIG_FEATURE_SUID
164
165#ifdef CONFIG_FEATURE_SUID_CONFIG
166
Glenn L McGrathb37367a2002-08-22 13:12:40 +0000167/* check if u is member of group g */
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000168static int
169ingroup (uid_t u, gid_t g)
Robert Grieblc9aca452002-06-04 20:06:25 +0000170{
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000171 struct group *grp = getgrgid (g);
Glenn L McGrathb37367a2002-08-22 13:12:40 +0000172
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000173 if (grp) {
174 char **mem;
Glenn L McGrathb37367a2002-08-22 13:12:40 +0000175
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000176 for (mem = grp->gr_mem; *mem; mem++) {
177 struct passwd *pwd = getpwnam (*mem);
Glenn L McGrathb37367a2002-08-22 13:12:40 +0000178
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000179 if (pwd && (pwd->pw_uid == u))
180 return 1;
Robert Grieblc9aca452002-06-04 20:06:25 +0000181 }
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000182 }
183 return 0;
Robert Grieblc9aca452002-06-04 20:06:25 +0000184}
185
186#endif
187
188
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000189void
190check_suid (struct BB_applet *applet)
Robert Grieblc9aca452002-06-04 20:06:25 +0000191{
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000192 uid_t ruid = getuid (); /* real [ug]id */
193 uid_t rgid = getgid ();
Glenn L McGrathb37367a2002-08-22 13:12:40 +0000194
Robert Grieblc9aca452002-06-04 20:06:25 +0000195#ifdef CONFIG_FEATURE_SUID_CONFIG
Manuel Novoa III 7b565a02004-02-17 10:16:21 +0000196 if (suid_cfg_readable) {
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000197 struct BB_suid_config *sct;
Glenn L McGrathb37367a2002-08-22 13:12:40 +0000198
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000199 for (sct = suid_config; sct; sct = sct->m_next) {
200 if (sct->m_applet == applet)
201 break;
202 }
203 if (sct) {
204 mode_t m = sct->m_mode;
205
206 if (sct->m_uid == ruid) /* same uid */
207 m >>= 6;
208 else if ((sct->m_gid == rgid) || ingroup (ruid, sct->m_gid)) /* same group / in group */
209 m >>= 3;
210
211 if (!(m & S_IXOTH)) /* is x bit not set ? */
212 bb_error_msg_and_die ("You have no permission to run this applet!");
213
214 if ((sct->m_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) { /* *both* have to be set for sgid */
215 if (setegid (sct->m_gid))
216 bb_error_msg_and_die
217 ("BusyBox binary has insufficient rights to set proper GID for applet!");
218 } else
219 setgid (rgid); /* no sgid -> drop */
220
221 if (sct->m_mode & S_ISUID) {
222 if (seteuid (sct->m_uid))
223 bb_error_msg_and_die
224 ("BusyBox binary has insufficient rights to set proper UID for applet!");
225 } else
226 setuid (ruid); /* no suid -> drop */
227 } else {
228 /* default: drop all priviledges */
229 setgid (rgid);
230 setuid (ruid);
231 }
232 return;
233 } else {
234#ifndef CONFIG_FEATURE_SUID_CONFIG_QUIET
235 static int onetime = 0;
236
237 if (!onetime) {
238 onetime = 1;
239 fprintf (stderr, "Using fallback suid method\n");
240 }
241#endif
242 }
243#endif
244
245 if (applet->need_suid == _BB_SUID_ALWAYS) {
246 if (geteuid () != 0)
247 bb_error_msg_and_die ("This applet requires root priviledges!");
248 } else if (applet->need_suid == _BB_SUID_NEVER) {
249 setgid (rgid); /* drop all priviledges */
250 setuid (ruid);
251 }
252}
253
254#ifdef CONFIG_FEATURE_SUID_CONFIG
255
Manuel Novoa III 31b98dd2004-02-01 10:03:05 +0000256/* This should probably be a libbb routine. In that case,
257 * I'd probably rename it to something like bb_trimmed_slice.
258 */
259static char *get_trimmed_slice(char *s, char *e)
260{
261 /* First, consider the value at e to be nul and back up until we
262 * reach a non-space char. Set the char after that (possibly at
263 * the original e) to nul. */
264 while (e-- > s) {
265 if (!isspace(*e)) {
266 break;
267 }
268 }
269 e[1] = 0;
270
271 /* Next, advance past all leading space and return a ptr to the
272 * first non-space char; possibly the terminating nul. */
273 return (char *) bb_skip_whitespace(s);
274}
275
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000276
277#define parse_error(x) { err=x; goto pe_label; }
278
Manuel Novoa III 31b98dd2004-02-01 10:03:05 +0000279/* Don't depend on the tools to combine strings. */
280static const char config_file[] = CONFIG_FILE;
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000281
Manuel Novoa III 31b98dd2004-02-01 10:03:05 +0000282/* There are 4 chars + 1 nul for each of user/group/other. */
283static const char mode_chars[] = "Ssx-\0Ssx-\0Ttx-";
284
285/* We don't supply a value for the nul, so an index adjustment is
286 * necessary below. Also, we use unsigned short here to save some
287 * space even though these are really mode_t values. */
288static const unsigned short mode_mask[] = {
289 /* SST sst xxx --- */
290 S_ISUID, S_ISUID|S_IXUSR, S_IXUSR, 0, /* user */
291 S_ISGID, S_ISGID|S_IXGRP, S_IXGRP, 0, /* group */
292 0, S_IXOTH, S_IXOTH, 0 /* other */
293};
294
295static void parse_config_file(void)
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000296{
Manuel Novoa III 31b98dd2004-02-01 10:03:05 +0000297 struct BB_suid_config *sct_head;
298 struct BB_suid_config *sct;
299 struct BB_applet *applet;
300 FILE *f;
301 char *err;
302 char *s;
303 char *e;
304 int i, lc, section;
305 char buffer[256];
306 struct stat st;
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000307
Manuel Novoa III 31b98dd2004-02-01 10:03:05 +0000308 assert(!suid_config); /* Should be set to NULL by bss init. */
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000309
Manuel Novoa III 31b98dd2004-02-01 10:03:05 +0000310 if ((stat(config_file, &st) != 0) /* No config file? */
311 || !S_ISREG(st.st_mode) /* Not a regular file? */
312 || (st.st_uid != 0) /* Not owned by root? */
313 || (st.st_mode & (S_IWGRP | S_IWOTH)) /* Writable by non-root? */
314 || !(f = fopen(config_file, "r")) /* Can not open? */
315 ) {
316 return;
Robert Grieblc9aca452002-06-04 20:06:25 +0000317 }
Glenn L McGrathb37367a2002-08-22 13:12:40 +0000318
Manuel Novoa III 7b565a02004-02-17 10:16:21 +0000319 suid_cfg_readable = 1;
Manuel Novoa III 31b98dd2004-02-01 10:03:05 +0000320 sct_head = NULL;
321 section = lc = 0;
Robert Grieblc9aca452002-06-04 20:06:25 +0000322
Manuel Novoa III 31b98dd2004-02-01 10:03:05 +0000323 do {
324 s = buffer;
325
326 if (!fgets(s, sizeof(buffer), f)) { /* Are we done? */
327 if (ferror(f)) { /* Make sure it wasn't a read error. */
328 parse_error("reading");
329 }
330 fclose(f);
331 suid_config = sct_head; /* Success, so set the pointer. */
332 return;
333 }
334
335 lc++; /* Got a (partial) line. */
336
337 /* If a line is too long for our buffer, we consider it an error.
338 * The following test does mistreat one corner case though.
339 * If the final line of the file does not end with a newline and
340 * yet exactly fills the buffer, it will be treated as too long
341 * even though there isn't really a problem. But it isn't really
342 * worth adding code to deal with such an unlikely situation, and
343 * we do err on the side of caution. Besides, the line would be
344 * too long if it did end with a newline. */
345 if (!strchr(s, '\n') && !feof(f)) {
346 parse_error("line too long");
347 }
348
349 /* Trim leading and trailing whitespace, ignoring comments, and
350 * check if the resulting string is empty. */
351 if (!*(s = get_trimmed_slice(s, strchrnul(s, '#')))) {
352 continue;
353 }
354
355 /* Check for a section header. */
356
357 if (*s == '[') {
358 /* Unlike the old code, we ignore leading and trailing
359 * whitespace for the section name. We also require that
360 * there are no stray characters after the closing bracket. */
361 if (!(e = strchr(s, ']')) /* Missing right bracket? */
362 || e[1] /* Trailing characters? */
363 || !*(s = get_trimmed_slice(s+1, e)) /* Missing name? */
364 ) {
365 parse_error("section header");
366 }
367 /* Right now we only have one section so just check it.
368 * If more sections are added in the future, please don't
369 * resort to cascading ifs with multiple strcasecmp calls.
370 * That kind of bloated code is all too common. A loop
371 * and a string table would be a better choice unless the
372 * number of sections is very small. */
373 if (strcasecmp(s, "SUID") == 0) {
374 section = 1;
375 continue;
376 }
377 section = -1; /* Unknown section so set to skip. */
378 continue;
379 }
380
381 /* Process sections. */
382
383 if (section == 1) { /* SUID */
384 /* Since we trimmed leading and trailing space above, we're
385 * now looking for strings of the form
386 * <key>[::space::]*=[::space::]*<value>
387 * where both key and value could contain inner whitespace. */
388
389 /* First get the key (an applet name in our case). */
390 if (!!(e = strchr(s, '='))) {
391 s = get_trimmed_slice(s, e);
392 }
393 if (!e || !*s) { /* Missing '=' or empty key. */
394 parse_error("keyword");
395 }
396
397 /* Ok, we have an applet name. Process the rhs if this
398 * applet is currently built in and ignore it otherwise.
399 * Note: This can hide config file bugs which only pop
400 * up when the busybox configuration is changed. */
401 if ((applet = find_applet_by_name(s))) {
402 /* Note: We currently don't check for duplicates!
403 * The last config line for each applet will be the
404 * one used since we insert at the head of the list.
405 * I suppose this could be considered a feature. */
406 sct = xmalloc(sizeof(struct BB_suid_config));
407 sct->m_applet = applet;
408 sct->m_mode = 0;
409 sct->m_next = sct_head;
410 sct_head = sct;
411
412 /* Get the specified mode. */
413
414 e = (char *) bb_skip_whitespace(e+1);
415
416 for (i=0 ; i < 3 ; i++) {
417 const char *q;
418 if (!*(q = strchrnul(mode_chars + 5*i, *e++))) {
419 parse_error("mode");
420 }
421 /* Adjust by -i to account for nul. */
422 sct->m_mode |= mode_mask[(q - mode_chars) - i];
423 }
424
425 /* Now get the the user/group info. */
426
427 s = (char *) bb_skip_whitespace(e);
428
429 /* Note: We require whitespace between the mode and the
430 * user/group info. */
431 if ((s == e) || !(e = strchr(s, '.'))) {
432 parse_error("<uid>.<gid>");
433 }
434 *e++ = 0;
435
436 /* We can't use get_ug_id here since it would exit()
437 * if a uid or gid was not found. Oh well... */
438 {
439 char *e2;
440
441 sct->m_uid = strtoul(s, &e2, 10);
442 if (*e2 || (s == e2)) {
443 struct passwd *pwd;
444 if (!(pwd = getpwnam(s))) {
445 parse_error("user");
446 }
447 sct->m_uid = pwd->pw_uid;
448 }
449
450 sct->m_gid = strtoul(e, &e2, 10);
451 if (*e2 || (e == e2)) {
452 struct group *grp;
453 if (!(grp = getgrnam(e))) {
454 parse_error("group");
455 }
456 sct->m_gid = grp->gr_gid;
457 }
458 }
459 }
460 continue;
461 }
462
463 /* Unknown sections are ignored. */
464
465 /* Encountering configuration lines prior to seeing a
466 * section header is treated as an error. This is how
467 * the old code worked, but it may not be desireable.
468 * We may want to simply ignore such lines in case they
469 * are used in some future version of busybox. */
470 if (!section) {
471 parse_error("keyword outside section");
472 }
473
474 } while (1);
475
476 pe_label:
477 fprintf(stderr, "Parse error in %s, line %d: %s\n",
478 config_file, lc, err);
479
480 fclose(f);
481 /* Release any allocated memory before returning. */
482 while (sct_head) {
483 sct = sct_head->m_next;
484 free(sct_head);
485 sct_head = sct;
486 }
487 return;
Robert Grieblc9aca452002-06-04 20:06:25 +0000488}
489
490#endif
491
492#endif
493
Eric Andersenaad1a882001-03-16 22:47:14 +0000494/* END CODE */
495/*
Manuel Novoa III 31b98dd2004-02-01 10:03:05 +0000496 Local Variables:
497 c-file-style: "linux"
498 c-basic-offset: 4
499 tab-width: 4
Eric Andersenaad1a882001-03-16 22:47:14 +0000500End:
501*/