cfcff3a532ad3c32be182c9abcb145a8a633a122
[oweals/busybox.git] / applets / applets.c
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
28 #include <unistd.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include "busybox.h"
33
34 #undef APPLET
35 #undef APPLET_NOUSAGE
36 #undef PROTOTYPES
37 #include "applets.h"
38
39 struct BB_applet *applet_using;
40
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
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>
53 #include "pwd_.h"
54 #include "grp_.h"
55
56 static int parse_config_file ( void );
57
58 static int config_ok;
59
60 #define CONFIG_FILE "/etc/busybox.conf"
61
62 // applets [] is const, so we have to define this "override" structure
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;
69         
70         struct BB_suid_config *m_next;
71 };
72
73 static struct BB_suid_config *suid_config;
74
75 #endif // CONFIG_FEATURE_SUID_CONFIG
76
77 #endif // CONFIG_FEATURE_SUID
78
79
80
81 extern void show_usage(void)
82 {
83         const char *format_string;
84         const char *usage_string = usage_messages;
85         int i;
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";
93         if(*usage_string == 0)
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
108 extern const size_t NUM_APPLETS;
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 {
118         static int recurse_level = 0;
119         extern int been_there_done_that; /* From busybox.c */
120
121 #ifdef CONFIG_FEATURE_SUID_CONFIG
122         if ( recurse_level == 0 )
123                 config_ok = parse_config_file ( );
124 #endif
125
126         recurse_level++;
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;
130                 if (argv[1] && strcmp(argv[1], "--help") == 0) {
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);
141                 }
142 #ifdef CONFIG_FEATURE_SUID
143                 check_suid ( applet_using );
144 #endif
145                         
146                 exit((*(applet_using->main)) (argc, argv));
147         }
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         }
152         recurse_level--;
153 }
154
155
156 #ifdef CONFIG_FEATURE_SUID
157
158 #ifdef CONFIG_FEATURE_SUID_CONFIG
159
160 // check if u is member of group g
161 static int ingroup ( uid_t u, gid_t g )
162 {
163         struct group *grp = getgrgid ( g );
164         
165         if ( grp ) {
166                 char **mem;
167                 
168                 for ( mem = grp-> gr_mem; *mem; mem++ ) {
169                         struct passwd *pwd = getpwnam ( *mem );
170                 
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 {
183         uid_t ruid = getuid ( ); // real [ug]id
184         uid_t rgid = getgid ( );
185                         
186 #ifdef CONFIG_FEATURE_SUID_CONFIG
187         if ( config_ok ) {
188                 struct BB_suid_config *sct;
189                 
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;
196                         
197                         if ( sct-> m_uid == ruid ) // same uid
198                                 m >>= 6;
199                         else if (( sct-> m_gid == rgid ) || ingroup ( ruid, sct-> m_gid )) // same group / in group
200                                 m >>= 3;
201
202                         if (!( m & S_IXOTH ))   // is x bit not set ?
203                                 error_msg_and_die ( "You have no permission to run this applet!" );
204                 
205                         if (( sct-> m_mode & ( S_ISGID | S_IXGRP )) == ( S_ISGID | S_IXGRP )) { // *both* have to be set for sgid
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
210                                 setgid ( rgid ); // no sgid -> drop
211                                 
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
217                                 setuid ( ruid ); // no suid -> drop
218                 }
219                 else { // default: drop all priviledges
220                         setgid ( rgid );
221                         setuid ( ruid );
222                 }
223                 return;
224         }
225         else {
226                 static int onetime = 0;
227                 
228                 if ( !onetime ) {
229                         onetime = 1;
230                         fprintf ( stderr, "Using fallback suid method\n" );
231                 }
232         }
233 #endif
234
235         if ( applet-> need_suid == _BB_SUID_ALWAYS ) {
236                 if ( geteuid ( ) != 0 ) 
237                         error_msg_and_die ( "This applet requires root priviledges!" );
238         } 
239         else if ( applet-> need_suid == _BB_SUID_NEVER ) {
240                 setgid ( rgid ); // drop all priviledges
241                 setuid ( ruid );
242         }       
243 }
244
245 #ifdef CONFIG_FEATURE_SUID_CONFIG
246
247
248 #define parse_error(x)  { err=x; goto pe_label; }
249
250
251 int parse_config_file ( void )
252 {
253         struct stat st;
254         char *err = 0;
255         FILE *f = 0;
256         int lc = 0;
257
258         suid_config = 0;
259         
260         // is there a config file ?
261         if ( stat ( CONFIG_FILE, &st ) == 0 ) {
262                 // is it owned by root with no write perm. for group and others ?
263                 if ( S_ISREG( st. st_mode ) && ( st. st_uid == 0 )      && (!( st. st_mode & ( S_IWGRP | S_IWOTH )))) {
264                         // that's ok .. then try to open it
265                         f = fopen ( CONFIG_FILE, "r" );
266
267                         if ( f ) {
268                                 char buffer [256];
269                                 int section = 0;
270                                 
271                                 while ( fgets ( buffer, sizeof( buffer ) - 1, f )) {
272                                         char c = buffer [0];
273                                         char *p;
274                                 
275                                         lc++;
276                                         
277                                         p = strchr ( buffer, '#' );
278                                         if ( p )
279                                                 *p = 0;
280                                         p = buffer + xstrlen ( buffer );
281                                         while (( p > buffer ) && isspace ( *--p ))
282                                                 *p = 0;         
283                                                 
284                                         if ( p == buffer )
285                                                 continue;                                       
286                                 
287                                         if ( c == '[' ) {
288                                                 p = strchr ( buffer, ']' );
289                                                 
290                                                 if ( !p || ( p == ( buffer + 1 )))  // no matching ] or empty []
291                                                         parse_error ( "malformed section header" );
292
293                                                 *p = 0;
294                                                 
295                                                 if ( strcasecmp ( buffer + 1, "SUID" ) == 0 )
296                                                         section = 1;
297                                                 else
298                                                         section = -1; // unknown section - just skip
299                                         }
300                                         else if ( section ) {
301                                                 switch ( section ) {
302                                                         case 1: { // SUID
303                                                                 int l;
304                                                                 struct BB_applet *applet;
305
306                                                                 p = strchr ( buffer, '=' ); // <key>[::space::]*=[::space::]*<value>
307                                                                 
308                                                                 if ( !p || ( p == ( buffer + 1 ))) // no = or key is empty
309                                                                         parse_error ( "malformed keyword" );
310                                                                 
311                                                                 l = p - buffer;
312                                                                 while ( isspace ( buffer [--l] )) { } // skip whitespace
313                                                                 
314                                                                 buffer [l+1] = 0;
315                                                                 
316                                                                 if (( applet = find_applet_by_name ( buffer ))) {
317                                                                         struct BB_suid_config *sct = xmalloc ( sizeof( struct BB_suid_config ));
318                                                                         
319                                                                         sct-> m_applet = applet;
320                                                                         sct-> m_next = suid_config;
321                                                                         suid_config = sct;
322                                                                         
323                                                                         while ( isspace ( *++p )) { } // skip whitespace
324                                                                                                 
325                                                                         sct-> m_mode = 0;                       
326                                                                         
327                                                                         switch ( *p++ ) {
328                                                                                 case 'S': sct-> m_mode |= S_ISUID; break;
329                                                                                 case 's': sct-> m_mode |= S_ISUID; // no break
330                                                                                 case 'x': sct-> m_mode |= S_IXUSR; break;
331                                                                                 case '-': break;
332                                                                                 default : parse_error ( "invalid user mode" );
333                                                                         }
334                                                                                 
335                                                                         switch ( *p++ ) {
336                                                                                 case 's': sct-> m_mode |= S_ISGID; // no break
337                                                                                 case 'x': sct-> m_mode |= S_IXGRP; break;
338                                                                                 case 'S': break;
339                                                                                 case '-': break;
340                                                                                 default : parse_error ( "invalid group mode" );
341                                                                         }
342                                                                                 
343                                                                         switch ( *p ) {
344                                                                                 case 't': 
345                                                                                 case 'x': sct-> m_mode |= S_IXOTH; break;
346                                                                                 case 'T':
347                                                                                 case '-': break;
348                                                                                 default : parse_error ( "invalid other mode" );
349                                                                         }
350                                                                                 
351                                                                         while ( isspace ( *++p )) { } // skip whitespace
352                                                                         
353                                                                         if ( isdigit ( *p )) {
354                                                                                 sct-> m_uid = strtol ( p, &p, 10 );
355                                                                                 if ( *p++ != '.' )
356                                                                                         parse_error ( "parsing <uid>.<gid>" );                                                                          
357                                                                         }
358                                                                         else {
359                                                                                 struct passwd *pwd;
360                                                                                 char *p2 = strchr ( p, '.' );
361                                                                                 
362                                                                                 if ( !p2 ) 
363                                                                                         parse_error ( "parsing <uid>.<gid>" );                                                                          
364         
365                                                                                 *p2 = 0;
366                                                                                 pwd = getpwnam ( p );
367                                                 
368                                                                                 if ( !pwd ) 
369                                                                                         parse_error ( "invalid user name" );
370                                                                                 
371                                                                                 sct-> m_uid = pwd-> pw_uid;
372                                                                                 p = p2 + 1;
373                                                                         }               
374                                                                         if ( isdigit ( *p )) 
375                                                                                 sct-> m_gid = strtol ( p, &p, 10 );
376                                                                         else {
377                                                                                 struct group *grp = getgrnam ( p );
378                                                 
379                                                                                 if ( !grp ) 
380                                                                                         parse_error ( "invalid group name" );
381                                                                                 
382                                                                                 sct-> m_gid = grp-> gr_gid;
383                                                                         }                                                                                                                                               
384                                                                 }
385                                                                 break;
386                                                         }
387                                                         default: // unknown - skip
388                                                                 break;
389                                                 }                                       
390                                         }
391                                         else
392                                                 parse_error ( "keyword not within section" );
393                                 }                               
394                                 fclose ( f );
395                         }
396                 }
397         }
398         return 1;
399         
400 pe_label:       
401         fprintf ( stderr, "Parse error in %s, line %d: %s\n", CONFIG_FILE, lc, err );
402
403         if ( f )
404                 fclose ( f );
405         return 0;
406 }
407
408 #endif
409
410 #endif
411
412 /* END CODE */
413 /*
414 Local Variables:
415 c-file-style: "linux"
416 c-basic-offset: 4
417 tab-width: 4
418 End:
419 */