Make sure we have a show_usage function prototype
[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
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
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
109 extern const size_t NUM_APPLETS;
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 {
119         static int recurse_level = 0;
120         extern int been_there_done_that; /* From busybox.c */
121
122 #ifdef CONFIG_FEATURE_SUID_CONFIG
123         if ( recurse_level == 0 )
124                 config_ok = parse_config_file ( );
125 #endif
126
127         recurse_level++;
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;
131                 if (argv[1] && strcmp(argv[1], "--help") == 0) {
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);
142                 }
143 #ifdef CONFIG_FEATURE_SUID
144                 check_suid ( applet_using );
145 #endif
146
147                 exit((*(applet_using->main)) (argc, argv));
148         }
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         }
153         recurse_level--;
154 }
155
156
157 #ifdef CONFIG_FEATURE_SUID
158
159 #ifdef CONFIG_FEATURE_SUID_CONFIG
160
161 /* check if u is member of group g */
162 static int ingroup ( uid_t u, gid_t g )
163 {
164         struct group *grp = getgrgid ( g );
165
166         if ( grp ) {
167                 char **mem;
168
169                 for ( mem = grp-> gr_mem; *mem; mem++ ) {
170                         struct passwd *pwd = getpwnam ( *mem );
171
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 {
184         uid_t ruid = getuid ( ); /* real [ug]id */
185         uid_t rgid = getgid ( );
186
187 #ifdef CONFIG_FEATURE_SUID_CONFIG
188         if ( config_ok ) {
189                 struct BB_suid_config *sct;
190
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;
197
198                         if ( sct-> m_uid == ruid ) /* same uid */
199                                 m >>= 6;
200                         else if (( sct-> m_gid == rgid ) || ingroup ( ruid, sct-> m_gid )) /* same group / in group */
201                                 m >>= 3;
202
203                         if (!( m & S_IXOTH ))   /* is x bit not set ? */
204                                 error_msg_and_die ( "You have no permission to run this applet!" );
205
206                         if (( sct-> m_mode & ( S_ISGID | S_IXGRP )) == ( S_ISGID | S_IXGRP )) { /* *both* have to be set for sgid */
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
211                                 setgid ( rgid ); /* no sgid -> drop */
212
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
218                                 setuid ( ruid ); /* no suid -> drop */
219                 }
220                 else { /* default: drop all priviledges */
221                         setgid ( rgid );
222                         setuid ( ruid );
223                 }
224                 return;
225         }
226         else {
227 #ifndef CONFIG_FEATURE_SUID_CONFIG_QUIET        
228                 static int onetime = 0;
229
230                 if ( !onetime ) {
231                         onetime = 1;
232                         fprintf ( stderr, "Using fallback suid method\n" );
233                 }
234 #endif          
235         }
236 #endif
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 ) {
243                 setgid ( rgid ); /* drop all priviledges */
244                 setuid ( ruid );
245         }       
246 }
247
248 #ifdef CONFIG_FEATURE_SUID_CONFIG
249
250
251 #define parse_error(x)  { err=x; goto pe_label; }
252
253
254 int parse_config_file ( void )
255 {
256         struct stat st;
257         char *err = 0;
258         FILE *f = 0;
259         int lc = 0;
260
261         suid_config = 0;
262
263         /* is there a config file ? */
264         if ( stat ( CONFIG_FILE, &st ) == 0 ) {
265                 /* is it owned by root with no write perm. for group and others ? */
266                 if ( S_ISREG( st. st_mode ) && ( st. st_uid == 0 )      && (!( st. st_mode & ( S_IWGRP | S_IWOTH )))) {
267                         /* that's ok .. then try to open it */
268                         f = fopen ( CONFIG_FILE, "r" );
269
270                         if ( f ) {
271                                 char buffer [256];
272                                 int section = 0;
273
274                                 while ( fgets ( buffer, sizeof( buffer ) - 1, f )) {
275                                         char c = buffer [0];
276                                         char *p;
277
278                                         lc++;
279
280                                         p = strchr ( buffer, '#' );
281                                         if ( p )
282                                                 *p = 0;
283                                         p = buffer + xstrlen ( buffer );
284                                         while (( p > buffer ) && isspace ( *--p ))
285                                                 *p = 0;         
286
287                                         if ( p == buffer )
288                                                 continue;                                       
289
290                                         if ( c == '[' ) {
291                                                 p = strchr ( buffer, ']' );
292
293                                                 if ( !p || ( p == ( buffer + 1 )))  /* no matching ] or empty [] */
294                                                         parse_error ( "malformed section header" );
295
296                                                 *p = 0;
297
298                                                 if ( strcasecmp ( buffer + 1, "SUID" ) == 0 )
299                                                         section = 1;
300                                                 else
301                                                         section = -1; /* unknown section - just skip */
302                                         }
303                                         else if ( section ) {
304                                                 switch ( section ) {
305                                                         case 1: { /* SUID */
306                                                                 int l;
307                                                                 struct BB_applet *applet;
308
309                                                                 p = strchr ( buffer, '=' ); /* <key>[::space::]*=[::space::]*<value> */
310
311                                                                 if ( !p || ( p == ( buffer + 1 ))) /* no = or key is empty */
312                                                                         parse_error ( "malformed keyword" );
313
314                                                                 l = p - buffer;
315                                                                 while ( isspace ( buffer [--l] )) { } /* skip whitespace */
316
317                                                                 buffer [l+1] = 0;
318
319                                                                 if (( applet = find_applet_by_name ( buffer ))) {
320                                                                         struct BB_suid_config *sct = xmalloc ( sizeof( struct BB_suid_config ));
321
322                                                                         sct-> m_applet = applet;
323                                                                         sct-> m_next = suid_config;
324                                                                         suid_config = sct;
325
326                                                                         while ( isspace ( *++p )) { } /* skip whitespace */
327
328                                                                         sct-> m_mode = 0;                       
329
330                                                                         switch ( *p++ ) {
331                                                                                 case 'S': sct-> m_mode |= S_ISUID; break;
332                                                                                 case 's': sct-> m_mode |= S_ISUID; /* no break */
333                                                                                 case 'x': sct-> m_mode |= S_IXUSR; break;
334                                                                                 case '-': break;
335                                                                                 default : parse_error ( "invalid user mode" );
336                                                                         }
337
338                                                                         switch ( *p++ ) {
339                                                                                 case 's': sct-> m_mode |= S_ISGID; /* no break */
340                                                                                 case 'x': sct-> m_mode |= S_IXGRP; break;
341                                                                                 case 'S': break;
342                                                                                 case '-': break;
343                                                                                 default : parse_error ( "invalid group mode" );
344                                                                         }
345
346                                                                         switch ( *p ) {
347                                                                                 case 't': 
348                                                                                 case 'x': sct-> m_mode |= S_IXOTH; break;
349                                                                                 case 'T':
350                                                                                 case '-': break;
351                                                                                 default : parse_error ( "invalid other mode" );
352                                                                         }
353
354                                                                         while ( isspace ( *++p )) { } /* skip whitespace */
355
356                                                                         if ( isdigit ( *p )) {
357                                                                                 sct-> m_uid = strtol ( p, &p, 10 );
358                                                                                 if ( *p++ != '.' )
359                                                                                         parse_error ( "parsing <uid>.<gid>" );                                                                          
360                                                                         }
361                                                                         else {
362                                                                                 struct passwd *pwd;
363                                                                                 char *p2 = strchr ( p, '.' );
364                                                                                 
365                                                                                 if ( !p2 ) 
366                                                                                         parse_error ( "parsing <uid>.<gid>" );                                                                          
367
368                                                                                 *p2 = 0;
369                                                                                 pwd = getpwnam ( p );
370
371                                                                                 if ( !pwd ) 
372                                                                                         parse_error ( "invalid user name" );
373
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 );
381
382                                                                                 if ( !grp ) 
383                                                                                         parse_error ( "invalid group name" );
384
385                                                                                 sct-> m_gid = grp-> gr_gid;
386                                                                         }                                                                                                                                               
387                                                                 }
388                                                                 break;
389                                                         }
390                                                         default: /* unknown - skip */
391                                                                 break;
392                                                 }                                       
393                                         }
394                                         else
395                                                 parse_error ( "keyword not within section" );
396                                 }                               
397                                 fclose ( f );
398                                 return 1;
399                         }
400                 }
401         }
402         return 0; /* no config file or not readable (not an error) */
403
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;
410 }
411
412 #endif
413
414 #endif
415
416 /* END CODE */
417 /*
418 Local Variables:
419 c-file-style: "linux"
420 c-basic-offset: 4
421 tab-width: 4
422 End:
423 */