blob: 21b0c3dbce060ffaad06689620f881b8d699bf12 [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 Andersen674b08a2004-04-06 14:28:35 +000035const char usage_messages[] =
36
37#define MAKE_USAGE
38#include "usage.h"
39
40#include "applets.h"
41
42;
43
44#undef MAKE_USAGE
Eric Andersen2ccfef22001-03-19 19:30:24 +000045#undef APPLET
46#undef APPLET_NOUSAGE
47#undef PROTOTYPES
48#include "applets.h"
49
Eric Andersen674b08a2004-04-06 14:28:35 +000050
Glenn L McGrath7fc504c2004-02-22 11:13:28 +000051static struct BB_applet *applet_using;
Eric Andersenaad1a882001-03-16 22:47:14 +000052
Eric Andersen2ccfef22001-03-19 19:30:24 +000053/* The -1 arises because of the {0,NULL,0,-1} entry above. */
54const size_t NUM_APPLETS = (sizeof (applets) / sizeof (struct BB_applet) - 1);
55
Robert Grieblc9aca452002-06-04 20:06:25 +000056
57#ifdef CONFIG_FEATURE_SUID
58
Glenn L McGrath2faee7b2003-05-26 14:09:12 +000059static void check_suid (struct BB_applet *app);
Robert Grieblc9aca452002-06-04 20:06:25 +000060
61#ifdef CONFIG_FEATURE_SUID_CONFIG
62
63#include <sys/stat.h>
64#include <ctype.h>
Eric Andersen887ca792002-07-03 23:19:26 +000065#include "pwd_.h"
66#include "grp_.h"
Robert Grieblc9aca452002-06-04 20:06:25 +000067
Manuel Novoa III 31b98dd2004-02-01 10:03:05 +000068static void parse_config_file (void);
Robert Grieblc9aca452002-06-04 20:06:25 +000069
70#define CONFIG_FILE "/etc/busybox.conf"
71
Glenn L McGrathb37367a2002-08-22 13:12:40 +000072/* applets [] is const, so we have to define this "override" structure */
Glenn L McGrath2faee7b2003-05-26 14:09:12 +000073struct BB_suid_config
74{
75 struct BB_applet *m_applet;
Robert Grieblc9aca452002-06-04 20:06:25 +000076
Glenn L McGrath2faee7b2003-05-26 14:09:12 +000077 uid_t m_uid;
78 gid_t m_gid;
79 mode_t m_mode;
Glenn L McGrathb37367a2002-08-22 13:12:40 +000080
Glenn L McGrath2faee7b2003-05-26 14:09:12 +000081 struct BB_suid_config *m_next;
Robert Grieblc9aca452002-06-04 20:06:25 +000082};
83
84static struct BB_suid_config *suid_config;
Manuel Novoa III 7b565a02004-02-17 10:16:21 +000085static int suid_cfg_readable;
Robert Grieblc9aca452002-06-04 20:06:25 +000086
Glenn L McGrathb37367a2002-08-22 13:12:40 +000087#endif /* CONFIG_FEATURE_SUID_CONFIG */
Robert Grieblc9aca452002-06-04 20:06:25 +000088
Glenn L McGrathb37367a2002-08-22 13:12:40 +000089#endif /* CONFIG_FEATURE_SUID */
Robert Grieblc9aca452002-06-04 20:06:25 +000090
91
92
Rob Landleyb766c392005-09-04 11:10:37 +000093extern void bb_show_usage (void)
Eric Andersenaad1a882001-03-16 22:47:14 +000094{
Glenn L McGrath2faee7b2003-05-26 14:09:12 +000095 const char *format_string;
96 const char *usage_string = usage_messages;
97 int i;
Eric Andersenaad1a882001-03-16 22:47:14 +000098
Glenn L McGrath2faee7b2003-05-26 14:09:12 +000099 for (i = applet_using - applets; i > 0;) {
100 if (!*usage_string++) {
101 --i;
Eric Andersenaad1a882001-03-16 22:47:14 +0000102 }
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000103 }
Glenn L McGrath6ead3ab2002-11-28 08:33:04 +0000104
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000105 format_string = "%s\n\nUsage: %s %s\n\n";
106 if (*usage_string == '\b')
107 format_string = "%s\n\nNo help available.\n\n";
108 fprintf (stderr, format_string, bb_msg_full_version, applet_using->name,
109 usage_string);
Glenn L McGrath4d4ef192002-11-28 21:49:06 +0000110
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000111 exit (EXIT_FAILURE);
Eric Andersenaad1a882001-03-16 22:47:14 +0000112}
113
Rob Landleyb766c392005-09-04 11:10:37 +0000114static int applet_name_compare (const void *x, const void *y)
Eric Andersenaad1a882001-03-16 22:47:14 +0000115{
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000116 const char *name = x;
117 const struct BB_applet *applet = y;
Eric Andersenaad1a882001-03-16 22:47:14 +0000118
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000119 return strcmp (name, applet->name);
Eric Andersenaad1a882001-03-16 22:47:14 +0000120}
121
Eric Andersen15576262001-06-24 06:09:14 +0000122extern const size_t NUM_APPLETS;
Eric Andersenaad1a882001-03-16 22:47:14 +0000123
Rob Landleyb766c392005-09-04 11:10:37 +0000124struct BB_applet *find_applet_by_name (const char *name)
Eric Andersenaad1a882001-03-16 22:47:14 +0000125{
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000126 return bsearch (name, applets, NUM_APPLETS, sizeof (struct BB_applet),
127 applet_name_compare);
Eric Andersenaad1a882001-03-16 22:47:14 +0000128}
129
Rob Landleyb766c392005-09-04 11:10:37 +0000130void run_applet_by_name (const char *name, int argc, char **argv)
Eric Andersenaad1a882001-03-16 22:47:14 +0000131{
Rob Landleyb766c392005-09-04 11:10:37 +0000132 if(ENABLE_FEATURE_SUID_CONFIG) parse_config_file ();
Mark Whitleybd4b6212001-06-15 16:54:25 +0000133
Rob Landleyb766c392005-09-04 11:10:37 +0000134 if(!strncmp(name, "busybox", 7)) busybox_main(argc, argv);
135 /* Do a binary search to find the applet entry given the name. */
136 applet_using = find_applet_by_name(name);
137 if(applet_using) {
138 bb_applet_name = applet_using->name;
139 if(argc==2 && !strcmp(argv[1], "--help")) bb_show_usage ();
140 if(ENABLE_FEATURE_SUID) check_suid (applet_using);
141 exit ((*(applet_using->main)) (argc, argv));
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000142 }
Eric Andersenaad1a882001-03-16 22:47:14 +0000143}
144
145
Robert Grieblc9aca452002-06-04 20:06:25 +0000146#ifdef CONFIG_FEATURE_SUID
147
148#ifdef CONFIG_FEATURE_SUID_CONFIG
149
Glenn L McGrathb37367a2002-08-22 13:12:40 +0000150/* check if u is member of group g */
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000151static int
152ingroup (uid_t u, gid_t g)
Robert Grieblc9aca452002-06-04 20:06:25 +0000153{
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000154 struct group *grp = getgrgid (g);
Glenn L McGrathb37367a2002-08-22 13:12:40 +0000155
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000156 if (grp) {
157 char **mem;
Glenn L McGrathb37367a2002-08-22 13:12:40 +0000158
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000159 for (mem = grp->gr_mem; *mem; mem++) {
160 struct passwd *pwd = getpwnam (*mem);
Glenn L McGrathb37367a2002-08-22 13:12:40 +0000161
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000162 if (pwd && (pwd->pw_uid == u))
163 return 1;
Robert Grieblc9aca452002-06-04 20:06:25 +0000164 }
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000165 }
166 return 0;
Robert Grieblc9aca452002-06-04 20:06:25 +0000167}
168
169#endif
170
171
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000172void
173check_suid (struct BB_applet *applet)
Robert Grieblc9aca452002-06-04 20:06:25 +0000174{
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000175 uid_t ruid = getuid (); /* real [ug]id */
176 uid_t rgid = getgid ();
Glenn L McGrathb37367a2002-08-22 13:12:40 +0000177
Robert Grieblc9aca452002-06-04 20:06:25 +0000178#ifdef CONFIG_FEATURE_SUID_CONFIG
Manuel Novoa III 7b565a02004-02-17 10:16:21 +0000179 if (suid_cfg_readable) {
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000180 struct BB_suid_config *sct;
Glenn L McGrathb37367a2002-08-22 13:12:40 +0000181
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000182 for (sct = suid_config; sct; sct = sct->m_next) {
183 if (sct->m_applet == applet)
184 break;
185 }
186 if (sct) {
187 mode_t m = sct->m_mode;
188
189 if (sct->m_uid == ruid) /* same uid */
190 m >>= 6;
191 else if ((sct->m_gid == rgid) || ingroup (ruid, sct->m_gid)) /* same group / in group */
192 m >>= 3;
193
194 if (!(m & S_IXOTH)) /* is x bit not set ? */
195 bb_error_msg_and_die ("You have no permission to run this applet!");
196
197 if ((sct->m_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) { /* *both* have to be set for sgid */
198 if (setegid (sct->m_gid))
199 bb_error_msg_and_die
200 ("BusyBox binary has insufficient rights to set proper GID for applet!");
201 } else
202 setgid (rgid); /* no sgid -> drop */
203
204 if (sct->m_mode & S_ISUID) {
205 if (seteuid (sct->m_uid))
206 bb_error_msg_and_die
207 ("BusyBox binary has insufficient rights to set proper UID for applet!");
208 } else
209 setuid (ruid); /* no suid -> drop */
210 } else {
211 /* default: drop all priviledges */
212 setgid (rgid);
213 setuid (ruid);
214 }
215 return;
216 } else {
217#ifndef CONFIG_FEATURE_SUID_CONFIG_QUIET
218 static int onetime = 0;
219
220 if (!onetime) {
221 onetime = 1;
222 fprintf (stderr, "Using fallback suid method\n");
223 }
224#endif
225 }
226#endif
227
228 if (applet->need_suid == _BB_SUID_ALWAYS) {
229 if (geteuid () != 0)
230 bb_error_msg_and_die ("This applet requires root priviledges!");
231 } else if (applet->need_suid == _BB_SUID_NEVER) {
232 setgid (rgid); /* drop all priviledges */
233 setuid (ruid);
234 }
235}
236
237#ifdef CONFIG_FEATURE_SUID_CONFIG
238
Manuel Novoa III 31b98dd2004-02-01 10:03:05 +0000239/* This should probably be a libbb routine. In that case,
240 * I'd probably rename it to something like bb_trimmed_slice.
241 */
242static char *get_trimmed_slice(char *s, char *e)
243{
244 /* First, consider the value at e to be nul and back up until we
245 * reach a non-space char. Set the char after that (possibly at
246 * the original e) to nul. */
247 while (e-- > s) {
248 if (!isspace(*e)) {
249 break;
250 }
251 }
252 e[1] = 0;
253
254 /* Next, advance past all leading space and return a ptr to the
255 * first non-space char; possibly the terminating nul. */
256 return (char *) bb_skip_whitespace(s);
257}
258
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000259
260#define parse_error(x) { err=x; goto pe_label; }
261
Manuel Novoa III 31b98dd2004-02-01 10:03:05 +0000262/* Don't depend on the tools to combine strings. */
263static const char config_file[] = CONFIG_FILE;
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000264
Manuel Novoa III 31b98dd2004-02-01 10:03:05 +0000265/* There are 4 chars + 1 nul for each of user/group/other. */
266static const char mode_chars[] = "Ssx-\0Ssx-\0Ttx-";
267
268/* We don't supply a value for the nul, so an index adjustment is
269 * necessary below. Also, we use unsigned short here to save some
270 * space even though these are really mode_t values. */
271static const unsigned short mode_mask[] = {
272 /* SST sst xxx --- */
273 S_ISUID, S_ISUID|S_IXUSR, S_IXUSR, 0, /* user */
274 S_ISGID, S_ISGID|S_IXGRP, S_IXGRP, 0, /* group */
275 0, S_IXOTH, S_IXOTH, 0 /* other */
276};
277
278static void parse_config_file(void)
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000279{
Manuel Novoa III 31b98dd2004-02-01 10:03:05 +0000280 struct BB_suid_config *sct_head;
281 struct BB_suid_config *sct;
282 struct BB_applet *applet;
283 FILE *f;
284 char *err;
285 char *s;
286 char *e;
287 int i, lc, section;
288 char buffer[256];
289 struct stat st;
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000290
Manuel Novoa III 31b98dd2004-02-01 10:03:05 +0000291 assert(!suid_config); /* Should be set to NULL by bss init. */
Glenn L McGrath2faee7b2003-05-26 14:09:12 +0000292
Manuel Novoa III 31b98dd2004-02-01 10:03:05 +0000293 if ((stat(config_file, &st) != 0) /* No config file? */
294 || !S_ISREG(st.st_mode) /* Not a regular file? */
295 || (st.st_uid != 0) /* Not owned by root? */
296 || (st.st_mode & (S_IWGRP | S_IWOTH)) /* Writable by non-root? */
297 || !(f = fopen(config_file, "r")) /* Can not open? */
298 ) {
299 return;
Robert Grieblc9aca452002-06-04 20:06:25 +0000300 }
Glenn L McGrathb37367a2002-08-22 13:12:40 +0000301
Manuel Novoa III 7b565a02004-02-17 10:16:21 +0000302 suid_cfg_readable = 1;
Manuel Novoa III 31b98dd2004-02-01 10:03:05 +0000303 sct_head = NULL;
304 section = lc = 0;
Robert Grieblc9aca452002-06-04 20:06:25 +0000305
Manuel Novoa III 31b98dd2004-02-01 10:03:05 +0000306 do {
307 s = buffer;
308
309 if (!fgets(s, sizeof(buffer), f)) { /* Are we done? */
310 if (ferror(f)) { /* Make sure it wasn't a read error. */
311 parse_error("reading");
312 }
313 fclose(f);
314 suid_config = sct_head; /* Success, so set the pointer. */
315 return;
316 }
317
318 lc++; /* Got a (partial) line. */
319
320 /* If a line is too long for our buffer, we consider it an error.
321 * The following test does mistreat one corner case though.
322 * If the final line of the file does not end with a newline and
323 * yet exactly fills the buffer, it will be treated as too long
324 * even though there isn't really a problem. But it isn't really
325 * worth adding code to deal with such an unlikely situation, and
326 * we do err on the side of caution. Besides, the line would be
327 * too long if it did end with a newline. */
328 if (!strchr(s, '\n') && !feof(f)) {
329 parse_error("line too long");
330 }
331
332 /* Trim leading and trailing whitespace, ignoring comments, and
333 * check if the resulting string is empty. */
334 if (!*(s = get_trimmed_slice(s, strchrnul(s, '#')))) {
335 continue;
336 }
337
338 /* Check for a section header. */
339
340 if (*s == '[') {
341 /* Unlike the old code, we ignore leading and trailing
342 * whitespace for the section name. We also require that
343 * there are no stray characters after the closing bracket. */
344 if (!(e = strchr(s, ']')) /* Missing right bracket? */
345 || e[1] /* Trailing characters? */
346 || !*(s = get_trimmed_slice(s+1, e)) /* Missing name? */
347 ) {
348 parse_error("section header");
349 }
350 /* Right now we only have one section so just check it.
351 * If more sections are added in the future, please don't
352 * resort to cascading ifs with multiple strcasecmp calls.
353 * That kind of bloated code is all too common. A loop
354 * and a string table would be a better choice unless the
355 * number of sections is very small. */
356 if (strcasecmp(s, "SUID") == 0) {
357 section = 1;
358 continue;
359 }
360 section = -1; /* Unknown section so set to skip. */
361 continue;
362 }
363
364 /* Process sections. */
365
366 if (section == 1) { /* SUID */
367 /* Since we trimmed leading and trailing space above, we're
368 * now looking for strings of the form
369 * <key>[::space::]*=[::space::]*<value>
370 * where both key and value could contain inner whitespace. */
371
372 /* First get the key (an applet name in our case). */
373 if (!!(e = strchr(s, '='))) {
374 s = get_trimmed_slice(s, e);
375 }
376 if (!e || !*s) { /* Missing '=' or empty key. */
377 parse_error("keyword");
378 }
379
380 /* Ok, we have an applet name. Process the rhs if this
381 * applet is currently built in and ignore it otherwise.
382 * Note: This can hide config file bugs which only pop
383 * up when the busybox configuration is changed. */
384 if ((applet = find_applet_by_name(s))) {
385 /* Note: We currently don't check for duplicates!
386 * The last config line for each applet will be the
387 * one used since we insert at the head of the list.
388 * I suppose this could be considered a feature. */
389 sct = xmalloc(sizeof(struct BB_suid_config));
390 sct->m_applet = applet;
391 sct->m_mode = 0;
392 sct->m_next = sct_head;
393 sct_head = sct;
394
395 /* Get the specified mode. */
396
397 e = (char *) bb_skip_whitespace(e+1);
398
399 for (i=0 ; i < 3 ; i++) {
400 const char *q;
401 if (!*(q = strchrnul(mode_chars + 5*i, *e++))) {
402 parse_error("mode");
403 }
404 /* Adjust by -i to account for nul. */
405 sct->m_mode |= mode_mask[(q - mode_chars) - i];
406 }
407
408 /* Now get the the user/group info. */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000409
Manuel Novoa III 31b98dd2004-02-01 10:03:05 +0000410 s = (char *) bb_skip_whitespace(e);
411
412 /* Note: We require whitespace between the mode and the
413 * user/group info. */
414 if ((s == e) || !(e = strchr(s, '.'))) {
415 parse_error("<uid>.<gid>");
416 }
417 *e++ = 0;
418
419 /* We can't use get_ug_id here since it would exit()
420 * if a uid or gid was not found. Oh well... */
421 {
422 char *e2;
423
424 sct->m_uid = strtoul(s, &e2, 10);
425 if (*e2 || (s == e2)) {
426 struct passwd *pwd;
427 if (!(pwd = getpwnam(s))) {
428 parse_error("user");
429 }
430 sct->m_uid = pwd->pw_uid;
431 }
432
433 sct->m_gid = strtoul(e, &e2, 10);
434 if (*e2 || (e == e2)) {
435 struct group *grp;
436 if (!(grp = getgrnam(e))) {
437 parse_error("group");
438 }
439 sct->m_gid = grp->gr_gid;
440 }
441 }
442 }
443 continue;
444 }
445
446 /* Unknown sections are ignored. */
447
448 /* Encountering configuration lines prior to seeing a
449 * section header is treated as an error. This is how
Eric Andersenaff114c2004-04-14 17:51:38 +0000450 * the old code worked, but it may not be desirable.
Manuel Novoa III 31b98dd2004-02-01 10:03:05 +0000451 * We may want to simply ignore such lines in case they
452 * are used in some future version of busybox. */
453 if (!section) {
454 parse_error("keyword outside section");
455 }
456
457 } while (1);
458
459 pe_label:
460 fprintf(stderr, "Parse error in %s, line %d: %s\n",
461 config_file, lc, err);
462
463 fclose(f);
464 /* Release any allocated memory before returning. */
465 while (sct_head) {
466 sct = sct_head->m_next;
467 free(sct_head);
468 sct_head = sct;
469 }
470 return;
Robert Grieblc9aca452002-06-04 20:06:25 +0000471}
472
473#endif
474
475#endif
476
Eric Andersenaad1a882001-03-16 22:47:14 +0000477/* END CODE */
478/*
Manuel Novoa III 31b98dd2004-02-01 10:03:05 +0000479 Local Variables:
480 c-file-style: "linux"
481 c-basic-offset: 4
482 tab-width: 4
Eric Andersenaad1a882001-03-16 22:47:14 +0000483End:
484*/