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 | * |
| 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 McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 23 | * 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] | 24 | * Permission has been granted to redistribute this code under the GPL. |
| 25 | * |
| 26 | */ |
| 27 | |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 28 | #include <unistd.h> |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 29 | #include <stdio.h> |
| 30 | #include <stdlib.h> |
Eric Andersen | 08ff8a4 | 2001-03-23 17:02:05 +0000 | [diff] [blame] | 31 | #include <string.h> |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 32 | #include <assert.h> |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 33 | #include "busybox.h" |
| 34 | |
Eric Andersen | 674b08a | 2004-04-06 14:28:35 +0000 | [diff] [blame] | 35 | const 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 Andersen | 2ccfef2 | 2001-03-19 19:30:24 +0000 | [diff] [blame] | 45 | #undef APPLET |
| 46 | #undef APPLET_NOUSAGE |
| 47 | #undef PROTOTYPES |
| 48 | #include "applets.h" |
| 49 | |
Eric Andersen | 674b08a | 2004-04-06 14:28:35 +0000 | [diff] [blame] | 50 | |
Glenn L McGrath | 7fc504c | 2004-02-22 11:13:28 +0000 | [diff] [blame] | 51 | static struct BB_applet *applet_using; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 52 | |
Eric Andersen | 2ccfef2 | 2001-03-19 19:30:24 +0000 | [diff] [blame] | 53 | /* The -1 arises because of the {0,NULL,0,-1} entry above. */ |
| 54 | const size_t NUM_APPLETS = (sizeof (applets) / sizeof (struct BB_applet) - 1); |
| 55 | |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 56 | |
| 57 | #ifdef CONFIG_FEATURE_SUID |
| 58 | |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 59 | static void check_suid (struct BB_applet *app); |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 60 | |
| 61 | #ifdef CONFIG_FEATURE_SUID_CONFIG |
| 62 | |
| 63 | #include <sys/stat.h> |
| 64 | #include <ctype.h> |
Eric Andersen | 887ca79 | 2002-07-03 23:19:26 +0000 | [diff] [blame] | 65 | #include "pwd_.h" |
| 66 | #include "grp_.h" |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 67 | |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 68 | static void parse_config_file (void); |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 69 | |
| 70 | #define CONFIG_FILE "/etc/busybox.conf" |
| 71 | |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 72 | /* applets [] is const, so we have to define this "override" structure */ |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 73 | struct BB_suid_config |
| 74 | { |
| 75 | struct BB_applet *m_applet; |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 76 | |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 77 | uid_t m_uid; |
| 78 | gid_t m_gid; |
| 79 | mode_t m_mode; |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 80 | |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 81 | struct BB_suid_config *m_next; |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | static struct BB_suid_config *suid_config; |
Manuel Novoa III | 7b565a0 | 2004-02-17 10:16:21 +0000 | [diff] [blame] | 85 | static int suid_cfg_readable; |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 86 | |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 87 | #endif /* CONFIG_FEATURE_SUID_CONFIG */ |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 88 | |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 89 | #endif /* CONFIG_FEATURE_SUID */ |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 90 | |
| 91 | |
| 92 | |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 93 | extern void |
| 94 | bb_show_usage (void) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 95 | { |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 96 | const char *format_string; |
| 97 | const char *usage_string = usage_messages; |
| 98 | int i; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 99 | |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 100 | for (i = applet_using - applets; i > 0;) { |
| 101 | if (!*usage_string++) { |
| 102 | --i; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 103 | } |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 104 | } |
Glenn L McGrath | 6ead3ab | 2002-11-28 08:33:04 +0000 | [diff] [blame] | 105 | |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 106 | format_string = "%s\n\nUsage: %s %s\n\n"; |
| 107 | if (*usage_string == '\b') |
| 108 | format_string = "%s\n\nNo help available.\n\n"; |
| 109 | fprintf (stderr, format_string, bb_msg_full_version, applet_using->name, |
| 110 | usage_string); |
Glenn L McGrath | 4d4ef19 | 2002-11-28 21:49:06 +0000 | [diff] [blame] | 111 | |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 112 | exit (EXIT_FAILURE); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 115 | static int |
| 116 | applet_name_compare (const void *x, const void *y) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 117 | { |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 118 | const char *name = x; |
| 119 | const struct BB_applet *applet = y; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 120 | |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 121 | return strcmp (name, applet->name); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Eric Andersen | 1557626 | 2001-06-24 06:09:14 +0000 | [diff] [blame] | 124 | extern const size_t NUM_APPLETS; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 125 | |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 126 | struct BB_applet * |
| 127 | find_applet_by_name (const char *name) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 128 | { |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 129 | return bsearch (name, applets, NUM_APPLETS, sizeof (struct BB_applet), |
| 130 | applet_name_compare); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 133 | void |
| 134 | run_applet_by_name (const char *name, int argc, char **argv) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 135 | { |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 136 | static int recurse_level = 0; |
| 137 | extern int been_there_done_that; /* From busybox.c */ |
Mark Whitley | bd4b621 | 2001-06-15 16:54:25 +0000 | [diff] [blame] | 138 | |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 139 | #ifdef CONFIG_FEATURE_SUID_CONFIG |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 140 | if (recurse_level == 0) |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 141 | parse_config_file (); |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 142 | #endif |
| 143 | |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 144 | recurse_level++; |
| 145 | /* Do a binary search to find the applet entry given the name. */ |
| 146 | if ((applet_using = find_applet_by_name (name)) != NULL) { |
| 147 | bb_applet_name = applet_using->name; |
| 148 | if (argv[1] && strcmp (argv[1], "--help") == 0) { |
| 149 | if (strcmp (applet_using->name, "busybox") == 0) { |
| 150 | if (argv[2]) |
| 151 | applet_using = find_applet_by_name (argv[2]); |
| 152 | else |
| 153 | applet_using = NULL; |
| 154 | } |
| 155 | if (applet_using) |
| 156 | bb_show_usage (); |
| 157 | been_there_done_that = 1; |
| 158 | busybox_main (0, NULL); |
| 159 | } |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 160 | #ifdef CONFIG_FEATURE_SUID |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 161 | check_suid (applet_using); |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 162 | #endif |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 163 | |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 164 | exit ((*(applet_using->main)) (argc, argv)); |
| 165 | } |
| 166 | /* Just in case they have renamed busybox - Check argv[1] */ |
| 167 | if (recurse_level == 1) { |
| 168 | run_applet_by_name ("busybox", argc, argv); |
| 169 | } |
| 170 | recurse_level--; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 174 | #ifdef CONFIG_FEATURE_SUID |
| 175 | |
| 176 | #ifdef CONFIG_FEATURE_SUID_CONFIG |
| 177 | |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 178 | /* check if u is member of group g */ |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 179 | static int |
| 180 | ingroup (uid_t u, gid_t g) |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 181 | { |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 182 | struct group *grp = getgrgid (g); |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 183 | |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 184 | if (grp) { |
| 185 | char **mem; |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 186 | |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 187 | for (mem = grp->gr_mem; *mem; mem++) { |
| 188 | struct passwd *pwd = getpwnam (*mem); |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 189 | |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 190 | if (pwd && (pwd->pw_uid == u)) |
| 191 | return 1; |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 192 | } |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 193 | } |
| 194 | return 0; |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | #endif |
| 198 | |
| 199 | |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 200 | void |
| 201 | check_suid (struct BB_applet *applet) |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 202 | { |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 203 | uid_t ruid = getuid (); /* real [ug]id */ |
| 204 | uid_t rgid = getgid (); |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 205 | |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 206 | #ifdef CONFIG_FEATURE_SUID_CONFIG |
Manuel Novoa III | 7b565a0 | 2004-02-17 10:16:21 +0000 | [diff] [blame] | 207 | if (suid_cfg_readable) { |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 208 | struct BB_suid_config *sct; |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 209 | |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 210 | for (sct = suid_config; sct; sct = sct->m_next) { |
| 211 | if (sct->m_applet == applet) |
| 212 | break; |
| 213 | } |
| 214 | if (sct) { |
| 215 | mode_t m = sct->m_mode; |
| 216 | |
| 217 | if (sct->m_uid == ruid) /* same uid */ |
| 218 | m >>= 6; |
| 219 | else if ((sct->m_gid == rgid) || ingroup (ruid, sct->m_gid)) /* same group / in group */ |
| 220 | m >>= 3; |
| 221 | |
| 222 | if (!(m & S_IXOTH)) /* is x bit not set ? */ |
| 223 | bb_error_msg_and_die ("You have no permission to run this applet!"); |
| 224 | |
| 225 | if ((sct->m_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) { /* *both* have to be set for sgid */ |
| 226 | if (setegid (sct->m_gid)) |
| 227 | bb_error_msg_and_die |
| 228 | ("BusyBox binary has insufficient rights to set proper GID for applet!"); |
| 229 | } else |
| 230 | setgid (rgid); /* no sgid -> drop */ |
| 231 | |
| 232 | if (sct->m_mode & S_ISUID) { |
| 233 | if (seteuid (sct->m_uid)) |
| 234 | bb_error_msg_and_die |
| 235 | ("BusyBox binary has insufficient rights to set proper UID for applet!"); |
| 236 | } else |
| 237 | setuid (ruid); /* no suid -> drop */ |
| 238 | } else { |
| 239 | /* default: drop all priviledges */ |
| 240 | setgid (rgid); |
| 241 | setuid (ruid); |
| 242 | } |
| 243 | return; |
| 244 | } else { |
| 245 | #ifndef CONFIG_FEATURE_SUID_CONFIG_QUIET |
| 246 | static int onetime = 0; |
| 247 | |
| 248 | if (!onetime) { |
| 249 | onetime = 1; |
| 250 | fprintf (stderr, "Using fallback suid method\n"); |
| 251 | } |
| 252 | #endif |
| 253 | } |
| 254 | #endif |
| 255 | |
| 256 | if (applet->need_suid == _BB_SUID_ALWAYS) { |
| 257 | if (geteuid () != 0) |
| 258 | bb_error_msg_and_die ("This applet requires root priviledges!"); |
| 259 | } else if (applet->need_suid == _BB_SUID_NEVER) { |
| 260 | setgid (rgid); /* drop all priviledges */ |
| 261 | setuid (ruid); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | #ifdef CONFIG_FEATURE_SUID_CONFIG |
| 266 | |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 267 | /* This should probably be a libbb routine. In that case, |
| 268 | * I'd probably rename it to something like bb_trimmed_slice. |
| 269 | */ |
| 270 | static char *get_trimmed_slice(char *s, char *e) |
| 271 | { |
| 272 | /* First, consider the value at e to be nul and back up until we |
| 273 | * reach a non-space char. Set the char after that (possibly at |
| 274 | * the original e) to nul. */ |
| 275 | while (e-- > s) { |
| 276 | if (!isspace(*e)) { |
| 277 | break; |
| 278 | } |
| 279 | } |
| 280 | e[1] = 0; |
| 281 | |
| 282 | /* Next, advance past all leading space and return a ptr to the |
| 283 | * first non-space char; possibly the terminating nul. */ |
| 284 | return (char *) bb_skip_whitespace(s); |
| 285 | } |
| 286 | |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 287 | |
| 288 | #define parse_error(x) { err=x; goto pe_label; } |
| 289 | |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 290 | /* Don't depend on the tools to combine strings. */ |
| 291 | static const char config_file[] = CONFIG_FILE; |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 292 | |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 293 | /* There are 4 chars + 1 nul for each of user/group/other. */ |
| 294 | static const char mode_chars[] = "Ssx-\0Ssx-\0Ttx-"; |
| 295 | |
| 296 | /* We don't supply a value for the nul, so an index adjustment is |
| 297 | * necessary below. Also, we use unsigned short here to save some |
| 298 | * space even though these are really mode_t values. */ |
| 299 | static const unsigned short mode_mask[] = { |
| 300 | /* SST sst xxx --- */ |
| 301 | S_ISUID, S_ISUID|S_IXUSR, S_IXUSR, 0, /* user */ |
| 302 | S_ISGID, S_ISGID|S_IXGRP, S_IXGRP, 0, /* group */ |
| 303 | 0, S_IXOTH, S_IXOTH, 0 /* other */ |
| 304 | }; |
| 305 | |
| 306 | static void parse_config_file(void) |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 307 | { |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 308 | struct BB_suid_config *sct_head; |
| 309 | struct BB_suid_config *sct; |
| 310 | struct BB_applet *applet; |
| 311 | FILE *f; |
| 312 | char *err; |
| 313 | char *s; |
| 314 | char *e; |
| 315 | int i, lc, section; |
| 316 | char buffer[256]; |
| 317 | struct stat st; |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 318 | |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 319 | assert(!suid_config); /* Should be set to NULL by bss init. */ |
Glenn L McGrath | 2faee7b | 2003-05-26 14:09:12 +0000 | [diff] [blame] | 320 | |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 321 | if ((stat(config_file, &st) != 0) /* No config file? */ |
| 322 | || !S_ISREG(st.st_mode) /* Not a regular file? */ |
| 323 | || (st.st_uid != 0) /* Not owned by root? */ |
| 324 | || (st.st_mode & (S_IWGRP | S_IWOTH)) /* Writable by non-root? */ |
| 325 | || !(f = fopen(config_file, "r")) /* Can not open? */ |
| 326 | ) { |
| 327 | return; |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 328 | } |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 329 | |
Manuel Novoa III | 7b565a0 | 2004-02-17 10:16:21 +0000 | [diff] [blame] | 330 | suid_cfg_readable = 1; |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 331 | sct_head = NULL; |
| 332 | section = lc = 0; |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 333 | |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 334 | do { |
| 335 | s = buffer; |
| 336 | |
| 337 | if (!fgets(s, sizeof(buffer), f)) { /* Are we done? */ |
| 338 | if (ferror(f)) { /* Make sure it wasn't a read error. */ |
| 339 | parse_error("reading"); |
| 340 | } |
| 341 | fclose(f); |
| 342 | suid_config = sct_head; /* Success, so set the pointer. */ |
| 343 | return; |
| 344 | } |
| 345 | |
| 346 | lc++; /* Got a (partial) line. */ |
| 347 | |
| 348 | /* If a line is too long for our buffer, we consider it an error. |
| 349 | * The following test does mistreat one corner case though. |
| 350 | * If the final line of the file does not end with a newline and |
| 351 | * yet exactly fills the buffer, it will be treated as too long |
| 352 | * even though there isn't really a problem. But it isn't really |
| 353 | * worth adding code to deal with such an unlikely situation, and |
| 354 | * we do err on the side of caution. Besides, the line would be |
| 355 | * too long if it did end with a newline. */ |
| 356 | if (!strchr(s, '\n') && !feof(f)) { |
| 357 | parse_error("line too long"); |
| 358 | } |
| 359 | |
| 360 | /* Trim leading and trailing whitespace, ignoring comments, and |
| 361 | * check if the resulting string is empty. */ |
| 362 | if (!*(s = get_trimmed_slice(s, strchrnul(s, '#')))) { |
| 363 | continue; |
| 364 | } |
| 365 | |
| 366 | /* Check for a section header. */ |
| 367 | |
| 368 | if (*s == '[') { |
| 369 | /* Unlike the old code, we ignore leading and trailing |
| 370 | * whitespace for the section name. We also require that |
| 371 | * there are no stray characters after the closing bracket. */ |
| 372 | if (!(e = strchr(s, ']')) /* Missing right bracket? */ |
| 373 | || e[1] /* Trailing characters? */ |
| 374 | || !*(s = get_trimmed_slice(s+1, e)) /* Missing name? */ |
| 375 | ) { |
| 376 | parse_error("section header"); |
| 377 | } |
| 378 | /* Right now we only have one section so just check it. |
| 379 | * If more sections are added in the future, please don't |
| 380 | * resort to cascading ifs with multiple strcasecmp calls. |
| 381 | * That kind of bloated code is all too common. A loop |
| 382 | * and a string table would be a better choice unless the |
| 383 | * number of sections is very small. */ |
| 384 | if (strcasecmp(s, "SUID") == 0) { |
| 385 | section = 1; |
| 386 | continue; |
| 387 | } |
| 388 | section = -1; /* Unknown section so set to skip. */ |
| 389 | continue; |
| 390 | } |
| 391 | |
| 392 | /* Process sections. */ |
| 393 | |
| 394 | if (section == 1) { /* SUID */ |
| 395 | /* Since we trimmed leading and trailing space above, we're |
| 396 | * now looking for strings of the form |
| 397 | * <key>[::space::]*=[::space::]*<value> |
| 398 | * where both key and value could contain inner whitespace. */ |
| 399 | |
| 400 | /* First get the key (an applet name in our case). */ |
| 401 | if (!!(e = strchr(s, '='))) { |
| 402 | s = get_trimmed_slice(s, e); |
| 403 | } |
| 404 | if (!e || !*s) { /* Missing '=' or empty key. */ |
| 405 | parse_error("keyword"); |
| 406 | } |
| 407 | |
| 408 | /* Ok, we have an applet name. Process the rhs if this |
| 409 | * applet is currently built in and ignore it otherwise. |
| 410 | * Note: This can hide config file bugs which only pop |
| 411 | * up when the busybox configuration is changed. */ |
| 412 | if ((applet = find_applet_by_name(s))) { |
| 413 | /* Note: We currently don't check for duplicates! |
| 414 | * The last config line for each applet will be the |
| 415 | * one used since we insert at the head of the list. |
| 416 | * I suppose this could be considered a feature. */ |
| 417 | sct = xmalloc(sizeof(struct BB_suid_config)); |
| 418 | sct->m_applet = applet; |
| 419 | sct->m_mode = 0; |
| 420 | sct->m_next = sct_head; |
| 421 | sct_head = sct; |
| 422 | |
| 423 | /* Get the specified mode. */ |
| 424 | |
| 425 | e = (char *) bb_skip_whitespace(e+1); |
| 426 | |
| 427 | for (i=0 ; i < 3 ; i++) { |
| 428 | const char *q; |
| 429 | if (!*(q = strchrnul(mode_chars + 5*i, *e++))) { |
| 430 | parse_error("mode"); |
| 431 | } |
| 432 | /* Adjust by -i to account for nul. */ |
| 433 | sct->m_mode |= mode_mask[(q - mode_chars) - i]; |
| 434 | } |
| 435 | |
| 436 | /* Now get the the user/group info. */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 437 | |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 438 | s = (char *) bb_skip_whitespace(e); |
| 439 | |
| 440 | /* Note: We require whitespace between the mode and the |
| 441 | * user/group info. */ |
| 442 | if ((s == e) || !(e = strchr(s, '.'))) { |
| 443 | parse_error("<uid>.<gid>"); |
| 444 | } |
| 445 | *e++ = 0; |
| 446 | |
| 447 | /* We can't use get_ug_id here since it would exit() |
| 448 | * if a uid or gid was not found. Oh well... */ |
| 449 | { |
| 450 | char *e2; |
| 451 | |
| 452 | sct->m_uid = strtoul(s, &e2, 10); |
| 453 | if (*e2 || (s == e2)) { |
| 454 | struct passwd *pwd; |
| 455 | if (!(pwd = getpwnam(s))) { |
| 456 | parse_error("user"); |
| 457 | } |
| 458 | sct->m_uid = pwd->pw_uid; |
| 459 | } |
| 460 | |
| 461 | sct->m_gid = strtoul(e, &e2, 10); |
| 462 | if (*e2 || (e == e2)) { |
| 463 | struct group *grp; |
| 464 | if (!(grp = getgrnam(e))) { |
| 465 | parse_error("group"); |
| 466 | } |
| 467 | sct->m_gid = grp->gr_gid; |
| 468 | } |
| 469 | } |
| 470 | } |
| 471 | continue; |
| 472 | } |
| 473 | |
| 474 | /* Unknown sections are ignored. */ |
| 475 | |
| 476 | /* Encountering configuration lines prior to seeing a |
| 477 | * section header is treated as an error. This is how |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 478 | * the old code worked, but it may not be desirable. |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 479 | * We may want to simply ignore such lines in case they |
| 480 | * are used in some future version of busybox. */ |
| 481 | if (!section) { |
| 482 | parse_error("keyword outside section"); |
| 483 | } |
| 484 | |
| 485 | } while (1); |
| 486 | |
| 487 | pe_label: |
| 488 | fprintf(stderr, "Parse error in %s, line %d: %s\n", |
| 489 | config_file, lc, err); |
| 490 | |
| 491 | fclose(f); |
| 492 | /* Release any allocated memory before returning. */ |
| 493 | while (sct_head) { |
| 494 | sct = sct_head->m_next; |
| 495 | free(sct_head); |
| 496 | sct_head = sct; |
| 497 | } |
| 498 | return; |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | #endif |
| 502 | |
| 503 | #endif |
| 504 | |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 505 | /* END CODE */ |
| 506 | /* |
Manuel Novoa III | 31b98dd | 2004-02-01 10:03:05 +0000 | [diff] [blame] | 507 | Local Variables: |
| 508 | c-file-style: "linux" |
| 509 | c-basic-offset: 4 |
| 510 | tab-width: 4 |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 511 | End: |
| 512 | */ |