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