ifupdown: stop emitting annoying/misleading error messages.
[oweals/busybox.git] / networking / httpd.c
index 7388bb67ed4ba04ebc9ee036197348240f8168c0..bbb2dfe7f29ecd15f5e61df964e788da9163e2bb 100644 (file)
@@ -97,14 +97,6 @@ static const char default_path_httpd_conf[] = "/etc";
 static const char httpd_conf[] = "httpd.conf";
 static const char home[] = "./";
 
-#if ENABLE_LFS
-# define cont_l_fmt "%lld"
-# define cont_l_type (long long)
-#else
-# define cont_l_fmt "%ld"
-# define cont_l_type (long)
-#endif
-
 #define TIMEOUT 60
 
 // Note: busybox xfuncs are not used because we want the server to keep running
@@ -325,20 +317,20 @@ static int scan_ip_mask(const char *ipm, unsigned int *ip, unsigned int *mask)
 
                i = 0;
                while (*p) {
-                               if (*p < '0' || *p > '9') {
-                                               if (*p == '.') {
-                                                   i = scan_ip(&ipm, mask, 0);
-                                                   return i != 32;
-                                               }
-                                               return -1;
+                       if (*p < '0' || *p > '9') {
+                               if (*p == '.') {
+                                       i = scan_ip(&ipm, mask, 0);
+                                       return i != 32;
                                }
-                               i *= 10;
-                               i += *p - '0';
-                               p++;
+                               return -1;
+                       }
+                       i *= 10;
+                       i += *p - '0';
+                       p++;
                }
        }
        if (i > 32 || i < 0)
-                       return -1;
+               return -1;
        msk = 0x80000000;
        *mask = 0;
        while (i > 0) {
@@ -553,7 +545,7 @@ static void parse_conf(const char *path, int flag)
                                                } else if ((cf[1] == '.') && (cf[2] == '/' || cf[2] == 0)) {
                                                        ++cf;
                                                        if (p > p0) {
-                                                               while (*--p != '/');    /* omit previous dir */
+                                                               while (*--p != '/') /* omit previous dir */;
                                                        }
                                                        continue;
                                                }
@@ -927,8 +919,8 @@ static int sendHeaders(HttpResponseNum responseNum)
 
        if (config->ContentLength != -1) {    /* file */
                strftime(timeStr, sizeof(timeStr), RFC1123FMT, gmtime(&config->last_mod));
-               len += sprintf(buf+len, "Last-Modified: %s\r\n%s " cont_l_fmt "\r\n",
-                                                     timeStr, Content_length, cont_l_type config->ContentLength);
+               len += sprintf(buf+len, "Last-Modified: %s\r\n%s "OFF_FMT"\r\n",
+                               timeStr, Content_length, (off_t) config->ContentLength);
        }
        strcat(buf, "\r\n");
        len += 2;
@@ -1571,8 +1563,8 @@ BAD_REQUEST:
                                                        /* protect out root */
                                                        goto BAD_REQUEST;
                                                }
-                                               while (*--purl != '/');    /* omit previous dir */
-                                                       continue;
+                                               while (*--purl != '/') /* omit previous dir */;
+                                               continue;
                                        }
                                }
                        }
@@ -1909,15 +1901,15 @@ static const char httpd_opts[] = "c:d:h:"
 
 int httpd_main(int argc, char *argv[])
 {
-       unsigned long opt;
+       unsigned opt;
        const char *home_httpd = home;
        char *url_for_decode;
        USE_FEATURE_HTTPD_ENCODE_URL_STR(const char *url_for_encode;)
        USE_FEATURE_HTTPD_WITHOUT_INETD(const char *s_port;)
        USE_FEATURE_HTTPD_WITHOUT_INETD(int server;)
 
-       USE_FEATURE_HTTPD_SETUID(const char *s_uid;)
-       USE_FEATURE_HTTPD_SETUID(long uid = -1;)
+       USE_FEATURE_HTTPD_SETUID(const char *s_ugid = NULL;)
+       USE_FEATURE_HTTPD_SETUID(struct bb_uidgid_t ugid;)
 
        USE_FEATURE_HTTPD_AUTH_MD5(const char *pass;)
 
@@ -1932,12 +1924,12 @@ int httpd_main(int argc, char *argv[])
 
        config->ContentLength = -1;
 
-       opt = bb_getopt_ulflags(argc, argv, httpd_opts,
+       opt = getopt32(argc, argv, httpd_opts,
                        &(config->configFile), &url_for_decode, &home_httpd
                        USE_FEATURE_HTTPD_ENCODE_URL_STR(, &url_for_encode)
                        USE_FEATURE_HTTPD_BASIC_AUTH(, &(config->realm))
                        USE_FEATURE_HTTPD_AUTH_MD5(, &pass)
-                       USE_FEATURE_HTTPD_SETUID(, &s_uid)
+                       USE_FEATURE_HTTPD_SETUID(, &s_ugid)
                        USE_FEATURE_HTTPD_WITHOUT_INETD(, &s_port)
                );
 
@@ -1959,15 +1951,22 @@ int httpd_main(int argc, char *argv[])
 #endif
 #if ENABLE_FEATURE_HTTPD_WITHOUT_INETD
        if (opt & OPT_PORT)
-               config->port = bb_xgetlarg(s_port, 10, 1, 0xffff);
+               config->port = xatou16(s_port);
 #if ENABLE_FEATURE_HTTPD_SETUID
        if (opt & OPT_SETUID) {
                char *e;
-
-               uid = strtol(s_uid, &e, 0);
+               // FIXME: what the default group should be?
+               ugid.gid = -1;
+               ugid.uid = strtoul(s_ugid, &e, 0);
+               if (*e == ':') {
+                       e++;
+                       ugid.gid = strtoul(e, &e, 0);
+               }
                if (*e != '\0') {
                        /* not integer */
-                       uid = bb_xgetpwnam(s_uid);
+                       if (!uidgid_get(&ugid, s_ugid))
+                               bb_error_msg_and_die("unrecognized user[:group] "
+                                               "name '%s'", s_ugid);
                }
        }
 #endif
@@ -1978,8 +1977,15 @@ int httpd_main(int argc, char *argv[])
        server = openServer();
 # ifdef CONFIG_FEATURE_HTTPD_SETUID
        /* drop privileges */
-       if (uid > 0)
-               xsetuid(uid);
+       if (opt & OPT_SETUID) {
+               if (ugid.gid != (gid_t)-1) {
+                       // FIXME: needed?
+                       //if (setgroups(1, &ugid.gid) == -1)
+                       //      bb_perror_msg_and_die("setgroups");
+                       xsetgid(ugid.gid);
+               }
+               xsetuid(ugid.uid);
+       }
 # endif
 #endif