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 | * |
| 23 | * Based in part on code from sash, Copyright (c) 1999 by David I. Bell |
| 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> |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 32 | #include "busybox.h" |
| 33 | |
Eric Andersen | 2ccfef2 | 2001-03-19 19:30:24 +0000 | [diff] [blame] | 34 | #undef APPLET |
| 35 | #undef APPLET_NOUSAGE |
| 36 | #undef PROTOTYPES |
| 37 | #include "applets.h" |
| 38 | |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 39 | struct BB_applet *applet_using; |
| 40 | |
Eric Andersen | 2ccfef2 | 2001-03-19 19:30:24 +0000 | [diff] [blame] | 41 | /* The -1 arises because of the {0,NULL,0,-1} entry above. */ |
| 42 | const size_t NUM_APPLETS = (sizeof (applets) / sizeof (struct BB_applet) - 1); |
| 43 | |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 44 | |
| 45 | #ifdef CONFIG_FEATURE_SUID |
| 46 | |
| 47 | static void check_suid ( struct BB_applet *app ); |
| 48 | |
| 49 | #ifdef CONFIG_FEATURE_SUID_CONFIG |
| 50 | |
| 51 | #include <sys/stat.h> |
| 52 | #include <ctype.h> |
Eric Andersen | 887ca79 | 2002-07-03 23:19:26 +0000 | [diff] [blame] | 53 | #include "pwd_.h" |
| 54 | #include "grp_.h" |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 55 | |
Robert Griebl | 0c789a4 | 2002-06-06 17:30:16 +0000 | [diff] [blame] | 56 | static int parse_config_file ( void ); |
| 57 | |
| 58 | static int config_ok; |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 59 | |
| 60 | #define CONFIG_FILE "/etc/busybox.conf" |
| 61 | |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 62 | /* applets [] is const, so we have to define this "override" structure */ |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 63 | struct BB_suid_config { |
| 64 | struct BB_applet *m_applet; |
| 65 | |
| 66 | uid_t m_uid; |
| 67 | gid_t m_gid; |
| 68 | mode_t m_mode; |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 69 | |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 70 | struct BB_suid_config *m_next; |
| 71 | }; |
| 72 | |
| 73 | static struct BB_suid_config *suid_config; |
| 74 | |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 75 | #endif /* CONFIG_FEATURE_SUID_CONFIG */ |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 76 | |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 77 | #endif /* CONFIG_FEATURE_SUID */ |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 78 | |
| 79 | |
| 80 | |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 81 | extern void show_usage(void) |
| 82 | { |
| 83 | const char *format_string; |
| 84 | const char *usage_string = usage_messages; |
| 85 | int i; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 86 | |
| 87 | for (i = applet_using - applets; i > 0; ) { |
| 88 | if (!*usage_string++) { |
| 89 | --i; |
| 90 | } |
| 91 | } |
| 92 | format_string = "%s\n\nUsage: %s %s\n\n"; |
Eric Andersen | c38678d | 2002-09-16 06:22:25 +0000 | [diff] [blame] | 93 | if(*usage_string == '\b') |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 94 | format_string = "%s\n\nNo help available.\n\n"; |
| 95 | fprintf(stderr, format_string, |
| 96 | full_version, applet_using->name, usage_string); |
| 97 | exit(EXIT_FAILURE); |
| 98 | } |
| 99 | |
| 100 | static int applet_name_compare(const void *x, const void *y) |
| 101 | { |
| 102 | const char *name = x; |
| 103 | const struct BB_applet *applet = y; |
| 104 | |
| 105 | return strcmp(name, applet->name); |
| 106 | } |
| 107 | |
Eric Andersen | 1557626 | 2001-06-24 06:09:14 +0000 | [diff] [blame] | 108 | extern const size_t NUM_APPLETS; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 109 | |
| 110 | struct BB_applet *find_applet_by_name(const char *name) |
| 111 | { |
| 112 | return bsearch(name, applets, NUM_APPLETS, sizeof(struct BB_applet), |
| 113 | applet_name_compare); |
| 114 | } |
| 115 | |
| 116 | void run_applet_by_name(const char *name, int argc, char **argv) |
| 117 | { |
Mark Whitley | bd4b621 | 2001-06-15 16:54:25 +0000 | [diff] [blame] | 118 | static int recurse_level = 0; |
Matt Kraai | ab3d839 | 2001-08-27 17:19:38 +0000 | [diff] [blame] | 119 | extern int been_there_done_that; /* From busybox.c */ |
Mark Whitley | bd4b621 | 2001-06-15 16:54:25 +0000 | [diff] [blame] | 120 | |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 121 | #ifdef CONFIG_FEATURE_SUID_CONFIG |
| 122 | if ( recurse_level == 0 ) |
Robert Griebl | 0c789a4 | 2002-06-06 17:30:16 +0000 | [diff] [blame] | 123 | config_ok = parse_config_file ( ); |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 124 | #endif |
| 125 | |
Mark Whitley | bd4b621 | 2001-06-15 16:54:25 +0000 | [diff] [blame] | 126 | recurse_level++; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 127 | /* Do a binary search to find the applet entry given the name. */ |
| 128 | if ((applet_using = find_applet_by_name(name)) != NULL) { |
| 129 | applet_name = applet_using->name; |
Mark Whitley | bd4b621 | 2001-06-15 16:54:25 +0000 | [diff] [blame] | 130 | if (argv[1] && strcmp(argv[1], "--help") == 0) { |
Matt Kraai | ab3d839 | 2001-08-27 17:19:38 +0000 | [diff] [blame] | 131 | if (strcmp(applet_using->name, "busybox")==0) { |
| 132 | if(argv[2]) |
| 133 | applet_using = find_applet_by_name(argv[2]); |
| 134 | else |
| 135 | applet_using = NULL; |
| 136 | } |
| 137 | if(applet_using) |
| 138 | show_usage(); |
| 139 | been_there_done_that=1; |
| 140 | busybox_main(0, NULL); |
Mark Whitley | bd4b621 | 2001-06-15 16:54:25 +0000 | [diff] [blame] | 141 | } |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 142 | #ifdef CONFIG_FEATURE_SUID |
| 143 | check_suid ( applet_using ); |
| 144 | #endif |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 145 | |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 146 | exit((*(applet_using->main)) (argc, argv)); |
| 147 | } |
Mark Whitley | bd4b621 | 2001-06-15 16:54:25 +0000 | [diff] [blame] | 148 | /* Just in case they have renamed busybox - Check argv[1] */ |
| 149 | if (recurse_level == 1) { |
| 150 | run_applet_by_name("busybox", argc, argv); |
| 151 | } |
Matt Kraai | 861e624 | 2001-08-27 15:08:57 +0000 | [diff] [blame] | 152 | recurse_level--; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 156 | #ifdef CONFIG_FEATURE_SUID |
| 157 | |
| 158 | #ifdef CONFIG_FEATURE_SUID_CONFIG |
| 159 | |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 160 | /* check if u is member of group g */ |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 161 | static int ingroup ( uid_t u, gid_t g ) |
| 162 | { |
| 163 | struct group *grp = getgrgid ( g ); |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 164 | |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 165 | if ( grp ) { |
| 166 | char **mem; |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 167 | |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 168 | for ( mem = grp-> gr_mem; *mem; mem++ ) { |
| 169 | struct passwd *pwd = getpwnam ( *mem ); |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 170 | |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 171 | if ( pwd && ( pwd-> pw_uid == u )) |
| 172 | return 1; |
| 173 | } |
| 174 | } |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | #endif |
| 179 | |
| 180 | |
| 181 | void check_suid ( struct BB_applet *applet ) |
| 182 | { |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 183 | uid_t ruid = getuid ( ); /* real [ug]id */ |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 184 | uid_t rgid = getgid ( ); |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 185 | |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 186 | #ifdef CONFIG_FEATURE_SUID_CONFIG |
Robert Griebl | 0c789a4 | 2002-06-06 17:30:16 +0000 | [diff] [blame] | 187 | if ( config_ok ) { |
| 188 | struct BB_suid_config *sct; |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 189 | |
Robert Griebl | 0c789a4 | 2002-06-06 17:30:16 +0000 | [diff] [blame] | 190 | for ( sct = suid_config; sct; sct = sct-> m_next ) { |
| 191 | if ( sct-> m_applet == applet ) |
| 192 | break; |
| 193 | } |
| 194 | if ( sct ) { |
| 195 | mode_t m = sct-> m_mode; |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 196 | |
| 197 | if ( sct-> m_uid == ruid ) /* same uid */ |
Robert Griebl | 0c789a4 | 2002-06-06 17:30:16 +0000 | [diff] [blame] | 198 | m >>= 6; |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 199 | else if (( sct-> m_gid == rgid ) || ingroup ( ruid, sct-> m_gid )) /* same group / in group */ |
Robert Griebl | 0c789a4 | 2002-06-06 17:30:16 +0000 | [diff] [blame] | 200 | m >>= 3; |
| 201 | |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 202 | if (!( m & S_IXOTH )) /* is x bit not set ? */ |
Robert Griebl | 0c789a4 | 2002-06-06 17:30:16 +0000 | [diff] [blame] | 203 | error_msg_and_die ( "You have no permission to run this applet!" ); |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 204 | |
| 205 | if (( sct-> m_mode & ( S_ISGID | S_IXGRP )) == ( S_ISGID | S_IXGRP )) { /* *both* have to be set for sgid */ |
Robert Griebl | 0c789a4 | 2002-06-06 17:30:16 +0000 | [diff] [blame] | 206 | if ( setegid ( sct-> m_gid )) |
| 207 | error_msg_and_die ( "BusyBox binary has insufficient rights to set proper GID for applet!" ); |
| 208 | } |
| 209 | else |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 210 | setgid ( rgid ); /* no sgid -> drop */ |
| 211 | |
Robert Griebl | 0c789a4 | 2002-06-06 17:30:16 +0000 | [diff] [blame] | 212 | if ( sct-> m_mode & S_ISUID ) { |
| 213 | if ( seteuid ( sct-> m_uid )) |
| 214 | error_msg_and_die ( "BusyBox binary has insufficient rights to set proper UID for applet!" ); |
| 215 | } |
| 216 | else |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 217 | setuid ( ruid ); /* no suid -> drop */ |
Robert Griebl | 0c789a4 | 2002-06-06 17:30:16 +0000 | [diff] [blame] | 218 | } |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 219 | else { /* default: drop all priviledges */ |
Robert Griebl | 0c789a4 | 2002-06-06 17:30:16 +0000 | [diff] [blame] | 220 | setgid ( rgid ); |
| 221 | setuid ( ruid ); |
| 222 | } |
| 223 | return; |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 224 | } |
Robert Griebl | 0c789a4 | 2002-06-06 17:30:16 +0000 | [diff] [blame] | 225 | else { |
Robert Griebl | 88947dd | 2002-07-18 23:59:17 +0000 | [diff] [blame] | 226 | #ifndef CONFIG_FEATURE_SUID_CONFIG_QUIET |
Robert Griebl | 0c789a4 | 2002-06-06 17:30:16 +0000 | [diff] [blame] | 227 | static int onetime = 0; |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 228 | |
Robert Griebl | 0c789a4 | 2002-06-06 17:30:16 +0000 | [diff] [blame] | 229 | if ( !onetime ) { |
| 230 | onetime = 1; |
| 231 | fprintf ( stderr, "Using fallback suid method\n" ); |
| 232 | } |
Robert Griebl | 88947dd | 2002-07-18 23:59:17 +0000 | [diff] [blame] | 233 | #endif |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 234 | } |
Robert Griebl | 0c789a4 | 2002-06-06 17:30:16 +0000 | [diff] [blame] | 235 | #endif |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 236 | |
| 237 | if ( applet-> need_suid == _BB_SUID_ALWAYS ) { |
| 238 | if ( geteuid ( ) != 0 ) |
| 239 | error_msg_and_die ( "This applet requires root priviledges!" ); |
| 240 | } |
| 241 | else if ( applet-> need_suid == _BB_SUID_NEVER ) { |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 242 | setgid ( rgid ); /* drop all priviledges */ |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 243 | setuid ( ruid ); |
| 244 | } |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | #ifdef CONFIG_FEATURE_SUID_CONFIG |
| 248 | |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 249 | |
Robert Griebl | 0c789a4 | 2002-06-06 17:30:16 +0000 | [diff] [blame] | 250 | #define parse_error(x) { err=x; goto pe_label; } |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 251 | |
| 252 | |
Robert Griebl | 0c789a4 | 2002-06-06 17:30:16 +0000 | [diff] [blame] | 253 | int parse_config_file ( void ) |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 254 | { |
| 255 | struct stat st; |
Robert Griebl | 0c789a4 | 2002-06-06 17:30:16 +0000 | [diff] [blame] | 256 | char *err = 0; |
| 257 | FILE *f = 0; |
| 258 | int lc = 0; |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 259 | |
| 260 | suid_config = 0; |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 261 | |
| 262 | /* is there a config file ? */ |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 263 | if ( stat ( CONFIG_FILE, &st ) == 0 ) { |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 264 | /* is it owned by root with no write perm. for group and others ? */ |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 265 | if ( S_ISREG( st. st_mode ) && ( st. st_uid == 0 ) && (!( st. st_mode & ( S_IWGRP | S_IWOTH )))) { |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 266 | /* that's ok .. then try to open it */ |
Robert Griebl | 0c789a4 | 2002-06-06 17:30:16 +0000 | [diff] [blame] | 267 | f = fopen ( CONFIG_FILE, "r" ); |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 268 | |
| 269 | if ( f ) { |
Robert Griebl | 0c789a4 | 2002-06-06 17:30:16 +0000 | [diff] [blame] | 270 | char buffer [256]; |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 271 | int section = 0; |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 272 | |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 273 | while ( fgets ( buffer, sizeof( buffer ) - 1, f )) { |
| 274 | char c = buffer [0]; |
| 275 | char *p; |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 276 | |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 277 | lc++; |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 278 | |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 279 | p = strchr ( buffer, '#' ); |
| 280 | if ( p ) |
| 281 | *p = 0; |
| 282 | p = buffer + xstrlen ( buffer ); |
| 283 | while (( p > buffer ) && isspace ( *--p )) |
| 284 | *p = 0; |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 285 | |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 286 | if ( p == buffer ) |
| 287 | continue; |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 288 | |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 289 | if ( c == '[' ) { |
| 290 | p = strchr ( buffer, ']' ); |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 291 | |
| 292 | if ( !p || ( p == ( buffer + 1 ))) /* no matching ] or empty [] */ |
Robert Griebl | 0c789a4 | 2002-06-06 17:30:16 +0000 | [diff] [blame] | 293 | parse_error ( "malformed section header" ); |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 294 | |
| 295 | *p = 0; |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 296 | |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 297 | if ( strcasecmp ( buffer + 1, "SUID" ) == 0 ) |
| 298 | section = 1; |
| 299 | else |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 300 | section = -1; /* unknown section - just skip */ |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 301 | } |
| 302 | else if ( section ) { |
| 303 | switch ( section ) { |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 304 | case 1: { /* SUID */ |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 305 | int l; |
| 306 | struct BB_applet *applet; |
| 307 | |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 308 | p = strchr ( buffer, '=' ); /* <key>[::space::]*=[::space::]*<value> */ |
| 309 | |
| 310 | if ( !p || ( p == ( buffer + 1 ))) /* no = or key is empty */ |
Robert Griebl | 0c789a4 | 2002-06-06 17:30:16 +0000 | [diff] [blame] | 311 | parse_error ( "malformed keyword" ); |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 312 | |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 313 | l = p - buffer; |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 314 | while ( isspace ( buffer [--l] )) { } /* skip whitespace */ |
| 315 | |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 316 | buffer [l+1] = 0; |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 317 | |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 318 | if (( applet = find_applet_by_name ( buffer ))) { |
| 319 | struct BB_suid_config *sct = xmalloc ( sizeof( struct BB_suid_config )); |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 320 | |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 321 | sct-> m_applet = applet; |
| 322 | sct-> m_next = suid_config; |
| 323 | suid_config = sct; |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 324 | |
| 325 | while ( isspace ( *++p )) { } /* skip whitespace */ |
| 326 | |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 327 | sct-> m_mode = 0; |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 328 | |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 329 | switch ( *p++ ) { |
| 330 | case 'S': sct-> m_mode |= S_ISUID; break; |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 331 | case 's': sct-> m_mode |= S_ISUID; /* no break */ |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 332 | case 'x': sct-> m_mode |= S_IXUSR; break; |
| 333 | case '-': break; |
Robert Griebl | 0c789a4 | 2002-06-06 17:30:16 +0000 | [diff] [blame] | 334 | default : parse_error ( "invalid user mode" ); |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 335 | } |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 336 | |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 337 | switch ( *p++ ) { |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 338 | case 's': sct-> m_mode |= S_ISGID; /* no break */ |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 339 | case 'x': sct-> m_mode |= S_IXGRP; break; |
| 340 | case 'S': break; |
| 341 | case '-': break; |
Robert Griebl | 0c789a4 | 2002-06-06 17:30:16 +0000 | [diff] [blame] | 342 | default : parse_error ( "invalid group mode" ); |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 343 | } |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 344 | |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 345 | switch ( *p ) { |
| 346 | case 't': |
| 347 | case 'x': sct-> m_mode |= S_IXOTH; break; |
| 348 | case 'T': |
| 349 | case '-': break; |
Robert Griebl | 0c789a4 | 2002-06-06 17:30:16 +0000 | [diff] [blame] | 350 | default : parse_error ( "invalid other mode" ); |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 351 | } |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 352 | |
| 353 | while ( isspace ( *++p )) { } /* skip whitespace */ |
| 354 | |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 355 | if ( isdigit ( *p )) { |
| 356 | sct-> m_uid = strtol ( p, &p, 10 ); |
| 357 | if ( *p++ != '.' ) |
Robert Griebl | 0c789a4 | 2002-06-06 17:30:16 +0000 | [diff] [blame] | 358 | parse_error ( "parsing <uid>.<gid>" ); |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 359 | } |
| 360 | else { |
| 361 | struct passwd *pwd; |
| 362 | char *p2 = strchr ( p, '.' ); |
| 363 | |
| 364 | if ( !p2 ) |
Robert Griebl | 0c789a4 | 2002-06-06 17:30:16 +0000 | [diff] [blame] | 365 | parse_error ( "parsing <uid>.<gid>" ); |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 366 | |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 367 | *p2 = 0; |
| 368 | pwd = getpwnam ( p ); |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 369 | |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 370 | if ( !pwd ) |
Robert Griebl | 0c789a4 | 2002-06-06 17:30:16 +0000 | [diff] [blame] | 371 | parse_error ( "invalid user name" ); |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 372 | |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 373 | sct-> m_uid = pwd-> pw_uid; |
| 374 | p = p2 + 1; |
| 375 | } |
| 376 | if ( isdigit ( *p )) |
| 377 | sct-> m_gid = strtol ( p, &p, 10 ); |
| 378 | else { |
| 379 | struct group *grp = getgrnam ( p ); |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 380 | |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 381 | if ( !grp ) |
Robert Griebl | 0c789a4 | 2002-06-06 17:30:16 +0000 | [diff] [blame] | 382 | parse_error ( "invalid group name" ); |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 383 | |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 384 | sct-> m_gid = grp-> gr_gid; |
| 385 | } |
| 386 | } |
| 387 | break; |
| 388 | } |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 389 | default: /* unknown - skip */ |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 390 | break; |
| 391 | } |
| 392 | } |
| 393 | else |
Robert Griebl | 0c789a4 | 2002-06-06 17:30:16 +0000 | [diff] [blame] | 394 | parse_error ( "keyword not within section" ); |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 395 | } |
| 396 | fclose ( f ); |
Robert Griebl | e4f9f3a | 2002-07-16 21:53:59 +0000 | [diff] [blame] | 397 | return 1; |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 398 | } |
| 399 | } |
| 400 | } |
Glenn L McGrath | b37367a | 2002-08-22 13:12:40 +0000 | [diff] [blame] | 401 | return 0; /* no config file or not readable (not an error) */ |
| 402 | |
Robert Griebl | 0c789a4 | 2002-06-06 17:30:16 +0000 | [diff] [blame] | 403 | pe_label: |
| 404 | fprintf ( stderr, "Parse error in %s, line %d: %s\n", CONFIG_FILE, lc, err ); |
| 405 | |
| 406 | if ( f ) |
| 407 | fclose ( f ); |
| 408 | return 0; |
Robert Griebl | c9aca45 | 2002-06-04 20:06:25 +0000 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | #endif |
| 412 | |
| 413 | #endif |
| 414 | |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 415 | /* END CODE */ |
| 416 | /* |
| 417 | Local Variables: |
| 418 | c-file-style: "linux" |
| 419 | c-basic-offset: 4 |
| 420 | tab-width: 4 |
| 421 | End: |
| 422 | */ |