applets.c, xfunc.c: style cleanup
[oweals/busybox.git] / applets / applets.c
index 00102daa315b44781a25d24d25bec94e661b471f..b4580a5b46357982c839a53e5adb6bccb22bd7ad 100644 (file)
  * Licensed under GPLv2 or later, see file License in this tarball for details.
  */
 
-#include "busybox.h"
-#include <unistd.h>
-#include <string.h>
 #include <assert.h>
+#include "busybox.h"
+
+#define PROTOTYPES
+#include "applets.h"
+#undef PROTOTYPES
+
+
+/* Apparently uclibc defines __GLIBC__ (compat trick?). Oh well. */
+#if ENABLE_STATIC && defined(__GLIBC__) && !defined(__UCLIBC__)
+#warning Static linking against glibc produces buggy executables
+#warning (glibc does not cope well with ld --gc-sections).
+#warning See sources.redhat.com/bugzilla/show_bug.cgi?id=3400
+#warning Note that glibc is unsuitable for static linking anyway.
+#warning If you still want to do it, remove -Wl,--gc-sections
+#warning from top-level Makefile and remove this warning.
+#endif
 
 #if ENABLE_SHOW_USAGE && !ENABLE_FEATURE_COMPRESS_USAGE
 static const char usage_messages[] =
@@ -35,16 +48,14 @@ static const char usage_messages[] =
 
 static struct BB_applet *applet_using;
 
-/* The -1 arises because of the {0,NULL,0,-1} entry above. */
-const size_t NUM_APPLETS = (sizeof (applets) / sizeof (struct BB_applet) - 1);
+/* The -1 arises because of the {0,NULL,0,-1} entry. */
+const unsigned short NUM_APPLETS = (sizeof(applets) / sizeof(struct BB_applet) - 1);
 
 
-#ifdef CONFIG_FEATURE_SUID_CONFIG
+#if ENABLE_FEATURE_SUID_CONFIG
 
 #include <ctype.h>
 
-#define CONFIG_FILE "/etc/busybox.conf"
-
 /* applets [] is const, so we have to define this "override" structure */
 static struct BB_suid_config
 {
@@ -98,10 +109,10 @@ static char *get_trimmed_slice(char *s, char *e)
 }
 
 
-#define parse_error(x)  { err=x; goto pe_label; }
+#define parse_error(x)  do { errmsg = x; goto pe_label; } while(0)
 
 /* Don't depend on the tools to combine strings. */
-static const char config_file[] = CONFIG_FILE;
+static const char config_file[] = "/etc/busybox.conf";
 
 /* There are 4 chars + 1 nul for each of user/group/other. */
 static const char mode_chars[] = "Ssx-\0Ssx-\0Ttx-";
@@ -122,21 +133,21 @@ static void parse_config_file(void)
        struct BB_suid_config *sct;
        struct BB_applet *applet;
        FILE *f;
-       char *err;
+       const char *errmsg;
        char *s;
        char *e;
        int i, lc, section;
        char buffer[256];
        struct stat st;
 
-       assert(!suid_config);           /* Should be set to NULL by bss init. */
+       assert(!suid_config); /* Should be set to NULL by bss init. */
 
-       if ((stat(config_file, &st) != 0)                       /* No config file? */
-               || !S_ISREG(st.st_mode)                                 /* Not a regular file? */
-               || (st.st_uid != 0)                                             /* Not owned by root? */
-               || (st.st_mode & (S_IWGRP | S_IWOTH))   /* Writable by non-root? */
-               || !(f = fopen(config_file, "r"))               /* Can not open? */
-               ) {
+       if ((stat(config_file, &st) != 0)       /* No config file? */
+        || !S_ISREG(st.st_mode)                /* Not a regular file? */
+        || (st.st_uid != 0)                    /* Not owned by root? */
+        || (st.st_mode & (S_IWGRP | S_IWOTH))  /* Writable by non-root? */
+        || !(f = fopen(config_file, "r"))      /* Cannot open? */
+       ) {
                return;
        }
 
@@ -172,7 +183,8 @@ static void parse_config_file(void)
 
                /* Trim leading and trailing whitespace, ignoring comments, and
                 * check if the resulting string is empty. */
-               if (!*(s = get_trimmed_slice(s, strchrnul(s, '#')))) {
+               s = get_trimmed_slice(s, strchrnul(s, '#'));
+               if (!*s) {
                        continue;
                }
 
@@ -182,10 +194,11 @@ static void parse_config_file(void)
                        /* Unlike the old code, we ignore leading and trailing
                         * whitespace for the section name.  We also require that
                         * there are no stray characters after the closing bracket. */
-                       if (!(e = strchr(s, ']'))       /* Missing right bracket? */
-                               || e[1]                                 /* Trailing characters? */
-                               || !*(s = get_trimmed_slice(s+1, e)) /* Missing name? */
-                               ) {
+                       e = strchr(s, ']');
+                       if (!e   /* Missing right bracket? */
+                        || e[1] /* Trailing characters? */
+                        || !*(s = get_trimmed_slice(s+1, e)) /* Missing name? */
+                       ) {
                                parse_error("section header");
                        }
                        /* Right now we only have one section so just check it.
@@ -211,7 +224,8 @@ static void parse_config_file(void)
                         * where both key and value could contain inner whitespace. */
 
                        /* First get the key (an applet name in our case). */
-                       if (!!(e = strchr(s, '='))) {
+                       e = strchr(s, '=');
+                       if (e) {
                                s = get_trimmed_slice(s, e);
                        }
                        if (!e || !*s) {        /* Missing '=' or empty key. */
@@ -222,7 +236,8 @@ static void parse_config_file(void)
                         * applet is currently built in and ignore it otherwise.
                         * Note: This can hide config file bugs which only pop
                         * up when the busybox configuration is changed. */
-                       if ((applet = find_applet_by_name(s))) {
+                       applet = find_applet_by_name(s);
+                       if (applet) {
                                /* Note: We currently don't check for duplicates!
                                 * The last config line for each applet will be the
                                 * one used since we insert at the head of the list.
@@ -237,9 +252,10 @@ static void parse_config_file(void)
 
                                e = skip_whitespace(e+1);
 
-                               for (i=0 ; i < 3 ; i++) {
+                               for (i = 0; i < 3; i++) {
                                        const char *q;
-                                       if (!*(q = strchrnul(mode_chars + 5*i, *e++))) {
+                                       q = strchrnul(mode_chars + 5*i, *e++);
+                                       if (!*q) {
                                                parse_error("mode");
                                        }
                                        /* Adjust by -i to account for nul. */
@@ -255,30 +271,27 @@ static void parse_config_file(void)
                                if ((s == e) || !(e = strchr(s, '.'))) {
                                        parse_error("<uid>.<gid>");
                                }
-                               *e++ = 0;
+                               *e++ = '\0';
 
                                /* We can't use get_ug_id here since it would exit()
                                 * if a uid or gid was not found.  Oh well... */
-                               {
-                                       char *e2;
-
-                                       sct->m_uid = strtoul(s, &e2, 10);
-                                       if (*e2 || (s == e2)) {
-                                               struct passwd *pwd = getpwnam(s);
-                                               if (!pwd) {
-                                                       parse_error("user");
-                                               }
-                                               sct->m_uid = pwd->pw_uid;
+                               sct->m_uid = bb_strtoul(s, NULL, 10);
+                               if (errno) {
+                                       struct passwd *pwd = getpwnam(s);
+                                       if (!pwd) {
+                                               parse_error("user");
                                        }
+                                       sct->m_uid = pwd->pw_uid;
+                               }
 
-                                       sct->m_gid = strtoul(e, &e2, 10);
-                                       if (*e2 || (e == e2)) {
-                                               struct group *grp;
-                                               if (!(grp = getgrnam(e))) {
-                                                       parse_error("group");
-                                               }
-                                               sct->m_gid = grp->gr_gid;
+                               sct->m_gid = bb_strtoul(e, NULL, 10);
+                               if (errno) {
+                                       struct group *grp;
+                                       grp = getgrnam(e);
+                                       if (!grp) {
+                                               parse_error("group");
                                        }
+                                       sct->m_gid = grp->gr_gid;
                                }
                        }
                        continue;
@@ -299,7 +312,7 @@ static void parse_config_file(void)
 
  pe_label:
        fprintf(stderr, "Parse error in %s, line %d: %s\n",
-                       config_file, lc, err);
+                       config_file, lc, errmsg);
 
        fclose(f);
        /* Release any allocated memory before returning. */
@@ -308,64 +321,70 @@ static void parse_config_file(void)
                free(sct_head);
                sct_head = sct;
        }
-       return;
 }
 
 #else
-#define parse_config_file()
+#define parse_config_file() ((void)0)
 #endif /* CONFIG_FEATURE_SUID_CONFIG */
 
-#ifdef CONFIG_FEATURE_SUID
+#if ENABLE_FEATURE_SUID
 static void check_suid(struct BB_applet *applet)
 {
        uid_t ruid = getuid();               /* real [ug]id */
        uid_t rgid = getgid();
 
-#ifdef CONFIG_FEATURE_SUID_CONFIG
+#if ENABLE_FEATURE_SUID_CONFIG
        if (suid_cfg_readable) {
                struct BB_suid_config *sct;
+               mode_t m;
 
                for (sct = suid_config; sct; sct = sct->m_next) {
                        if (sct->m_applet == applet)
-                               break;
+                               goto found;
                }
-               if (sct) {
-                       mode_t m = sct->m_mode;
-
-                       if (sct->m_uid == ruid)       /* same uid */
-                               m >>= 6;
-                       else if ((sct->m_gid == rgid) || ingroup(ruid, sct->m_gid))  /* same group / in group */
-                               m >>= 3;
-
-                       if (!(m & S_IXOTH))           /* is x bit not set ? */
-                               bb_error_msg_and_die ("You have no permission to run this applet!");
-
-                       if ((sct->m_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {     /* *both* have to be set for sgid */
+               /* default: drop all privileges */
+               xsetgid(rgid);
+               xsetuid(ruid);
+               return;
+ found:
+               m = sct->m_mode;
+               if (sct->m_uid == ruid)
+                       /* same uid */
+                       m >>= 6;
+               else if ((sct->m_gid == rgid) || ingroup(ruid, sct->m_gid))
+                       /* same group / in group */
+                       m >>= 3;
+
+               if (!(m & S_IXOTH))           /* is x bit not set ? */
+                       bb_error_msg_and_die("you have no permission to run this applet!");
+
+               if (sct->m_gid != 0) {
+                       /* _both_ have to be set for sgid */
+                       if ((sct->m_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
                                xsetgid(sct->m_gid);
-                       } else xsetgid(rgid);                /* no sgid -> drop */
-
+                       } else xsetgid(rgid); /* no sgid -> drop */
+               }
+               if (sct->m_uid != 0) {
                        if (sct->m_mode & S_ISUID) xsetuid(sct->m_uid);
-                       else xsetuid(ruid);                  /* no suid -> drop */
-               } else {
-                       /* default: drop all privileges */
-                       xsetgid(rgid);
-                       xsetuid(ruid);
+                       else xsetuid(ruid); /* no suid -> drop */
                }
                return;
-       } else {
-#ifndef CONFIG_FEATURE_SUID_CONFIG_QUIET
-               static int onetime = 0;
+       }
+#if !ENABLE_FEATURE_SUID_CONFIG_QUIET
+       {
+               static smallint onetime = 0;
 
                if (!onetime) {
                        onetime = 1;
                        fprintf(stderr, "Using fallback suid method\n");
                }
-#endif
        }
+#endif
 #endif
 
        if (applet->need_suid == _BB_SUID_ALWAYS) {
-               if (geteuid()) bb_error_msg_and_die("Applet requires root privileges!");
+               if (geteuid())
+                       bb_error_msg_and_die("applet requires root privileges!");
        } else if (applet->need_suid == _BB_SUID_NEVER) {
                xsetgid(rgid);                          /* drop all privileges */
                xsetuid(ruid);
@@ -377,7 +396,7 @@ static void check_suid(struct BB_applet *applet)
 
 
 
-#ifdef CONFIG_FEATURE_COMPRESS_USAGE
+#if ENABLE_FEATURE_COMPRESS_USAGE
 
 #include "usage_compressed.h"
 #include "unarchive.h"
@@ -387,7 +406,7 @@ static const char *unpack_usage_messages(void)
        int input[2], output[2], pid;
        char *buf;
 
-       if(pipe(input) < 0 || pipe(output) < 0)
+       if (pipe(input) < 0 || pipe(output) < 0)
                exit(1);
 
        pid = fork();
@@ -437,7 +456,7 @@ void bb_show_usage(void)
                format_string = "%s\n\nUsage: %s %s\n\n";
                if (*usage_string == '\b')
                        format_string = "%s\n\nNo help available.\n\n";
-               fprintf (stderr, format_string, bb_msg_full_version,
+               fprintf(stderr, format_string, bb_msg_full_version,
                        applet_using->name, usage_string);
        }
 
@@ -451,8 +470,6 @@ static int applet_name_compare(const void *name, const void *vapplet)
        return strcmp(name, applet->name);
 }
 
-extern const size_t NUM_APPLETS;
-
 struct BB_applet *find_applet_by_name(const char *name)
 {
        return bsearch(name, applets, NUM_APPLETS, sizeof(struct BB_applet),
@@ -461,15 +478,19 @@ struct BB_applet *find_applet_by_name(const char *name)
 
 void run_applet_by_name(const char *name, int argc, char **argv)
 {
-       if (ENABLE_FEATURE_SUID_CONFIG) parse_config_file();
+       if (ENABLE_FEATURE_SUID_CONFIG)
+               parse_config_file();
 
-       if (!strncmp(name, "busybox", 7)) busybox_main(argc, argv);
        /* Do a binary search to find the applet entry given the name. */
        applet_using = find_applet_by_name(name);
        if (applet_using) {
                applet_name = applet_using->name;
-               if(argc==2 && !strcmp(argv[1], "--help")) bb_show_usage();
-               if(ENABLE_FEATURE_SUID) check_suid(applet_using);
-               exit((*(applet_using->main))(argc, argv));
+               if (argc == 2 && !strcmp(argv[1], "--help"))
+                       bb_show_usage();
+               if (ENABLE_FEATURE_SUID)
+                       check_suid(applet_using);
+               exit(applet_using->main(argc, argv));
        }
+       if (!strncmp(name, "busybox", 7))
+               exit(busybox_main(argc, argv));
 }