* httpd implementation for busybox
*
* Copyright (C) 2002,2003 Glenn Engel <glenne@engel.org>
- * Copyright (C) 2003,2004 Vladimir Oleynik <dzo@simtreas.ru>
+ * Copyright (C) 2003-2006 Vladimir Oleynik <dzo@simtreas.ru>
*
* simplify patch stolen from libbb without using strdup
*
* /adm:admin:setup # Require user admin, pwd setup on urls starting with /adm/
* /adm:toor:PaSsWd # or user toor, pwd PaSsWd on urls starting with /adm/
* .au:audio/basic # additional mime type for audio.au files
+ * *.php:/path/php # running cgi.php scripts through an interpreter
*
* A/D may be as a/d or allow/deny - first char case insensitive
* Deny IP rules take precedence over allow rules.
#ifdef CONFIG_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
#undef CONFIG_FEATURE_HTTPD_CGI
#undef CONFIG_FEATURE_HTTPD_SETUID
#undef CONFIG_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
+#undef ENABLE_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
+#undef ENABLE_FEATURE_HTTPD_BASIC_AUTH
+#undef ENABLE_FEATURE_HTTPD_AUTH_MD5
+#undef ENABLE_FEATURE_HTTPD_ENCODE_URL_STR
+#undef ENABLE_FEATURE_HTTPD_SET_REMOTE_PORT_TO_ENV
+#undef ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
+#undef ENABLE_FEATURE_HTTPD_CGI
+#undef ENABLE_FEATURE_HTTPD_SETUID
+#undef ENABLE_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
/* enable all features now */
#define CONFIG_FEATURE_HTTPD_BASIC_AUTH
#define CONFIG_FEATURE_HTTPD_AUTH_MD5
#define CONFIG_FEATURE_HTTPD_CGI
#define CONFIG_FEATURE_HTTPD_SETUID
#define CONFIG_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
+#define ENABLE_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY 0
+#define ENABLE_FEATURE_HTTPD_BASIC_AUTH 1
+#define ENABLE_FEATURE_HTTPD_AUTH_MD5 1
+#define ENABLE_FEATURE_HTTPD_ENCODE_URL_STR 1
+#define ENABLE_FEATURE_HTTPD_SET_REMOTE_PORT_TO_ENV 1
+#define ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES 1
+#define ENABLE_FEATURE_HTTPD_CGI 1
+#define ENABLE_FEATURE_HTTPD_SETUID 1
+#define ENABLE_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP 1
/* require from libbb.a for linking */
const char *bb_applet_name = "httpd";
#undef DEBUG
#endif
+#ifndef DEBUG
+# define DEBUG 0
+#endif
+
#define MAX_MEMORY_BUFF 8192 /* IO buffer */
typedef struct HT_ACCESS {
{
char buf[MAX_MEMORY_BUFF];
-#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
- const char *realm;
- char *remoteuser;
-#endif
+ USE_FEATURE_HTTPD_BASIC_AUTH(const char *realm;)
+ USE_FEATURE_HTTPD_BASIC_AUTH(char *remoteuser;)
const char *query;
-#ifdef CONFIG_FEATURE_HTTPD_CGI
- char *referer;
-#endif
+ USE_FEATURE_HTTPD_CGI(char *referer;)
const char *configFile;
unsigned int rmt_ip;
-#if defined(CONFIG_FEATURE_HTTPD_CGI) || defined(DEBUG)
+#if ENABLE_FEATURE_HTTPD_CGI || DEBUG
char rmt_ip_str[16]; /* for set env REMOTE_ADDR */
#endif
unsigned port; /* server initial port and for
#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
int accepted_socket;
-#define a_c_r config->accepted_socket
-#define a_c_w config->accepted_socket
- int debugHttpd; /* if seted, don`t stay daemon */
+# define a_c_r config->accepted_socket
+# define a_c_w config->accepted_socket
#else
-#define a_c_r 0
-#define a_c_w 1
+# define a_c_r 0
+# define a_c_w 1
#endif
volatile int alarm_signaled;
+#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
+ Htaccess *script_i; /* config script interpreters */
+#endif
} HttpdConfig;
static HttpdConfig *config;
} HttpEnumString;
static const HttpEnumString httpResponseNames[] = {
- { HTTP_OK, "OK" },
+ { HTTP_OK, "OK", NULL },
{ HTTP_MOVED_TEMPORARILY, "Found", "Directories must end with a slash." },
{ HTTP_REQUEST_TIMEOUT, "Request Timeout",
"No request appeared within a reasonable time period." },
config->flg_deny_all = 0;
-#if defined(CONFIG_FEATURE_HTTPD_BASIC_AUTH) || defined(CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES)
+#if defined(CONFIG_FEATURE_HTTPD_BASIC_AUTH) || defined(CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES) || defined(CONFIG_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR)
/* retain previous auth and mime config only for subdir parse */
if(flag != SUBDIR_PARSE) {
#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
#endif
#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
free_config_lines(&config->mime_a);
+#endif
+#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
+ free_config_lines(&config->script_i);
#endif
}
#endif
#endif
#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
&& *p0 != '.'
+#endif
+#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
+ && *p0 != '*'
#endif
)
continue;
}
#endif
-#if defined(CONFIG_FEATURE_HTTPD_BASIC_AUTH) || defined(CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES)
+#if defined(CONFIG_FEATURE_HTTPD_BASIC_AUTH) || defined(CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES) || defined(CONFIG_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR)
/* storing current config line */
cur = calloc(1, sizeof(Htaccess) + strlen(p0));
if(cur) {
continue;
}
#endif
+#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
+ if(*cf == '*' && cf[1] == '.') {
+ /* config script interpreter line move top for overwrite previous */
+ cur->next = config->script_i;
+ config->script_i = cur;
+ continue;
+ }
+#endif
#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
free(p0);
if(prev == NULL) {
} else {
/* sort path, if current lenght eq or bigger then move up */
Htaccess *prev_hti = config->auth;
- int l = strlen(cf);
+ size_t l = strlen(cf);
Htaccess *hti;
for(hti = prev_hti; hti; hti = hti->next) {
/* take the simple route and encode everything */
/* could possibly scan once to get length. */
int len = strlen(string);
- char *out = malloc(len*5 +1);
+ char *out = malloc(len * 6 + 1);
char *p=out;
char ch;
if (*ptr == '+' && flag_plus_to_space) { *string++ = ' '; ptr++; }
else if (*ptr != '%') *string++ = *ptr++;
else {
- unsigned int value;
- sscanf(ptr+1, "%2X", &value);
- *string++ = value;
- ptr += 3;
+ unsigned int value1, value2;
+
+ ptr++;
+ if(sscanf(ptr, "%1X", &value1) != 1 ||
+ sscanf(ptr+1, "%1X", &value2) != 1) {
+ if(!flag_plus_to_space)
+ return NULL;
+ *string++ = '%';
+ } else {
+ value1 = value1 * 16 + value2;
+ if(value1 == '/' || value1 == 0)
+ return orig+1;
+ *string++ = value1;
+ ptr += 2;
+ }
}
}
*string = '\0';
static void decodeBase64(char *Data)
{
- const unsigned char *in = Data;
+ const unsigned char *in = (const unsigned char *)Data;
// The decoded size will be at most 3/4 the size of the encoded
unsigned long ch = 0;
int i = 0;
/* emit the current date */
strftime(timeStr, sizeof(timeStr), RFC1123FMT, gmtime(&timer));
len = sprintf(buf,
- "HTTP/1.0 %d %s\nContent-type: %s\r\n"
+ "HTTP/1.0 %d %s\r\nContent-type: %s\r\n"
"Date: %s\r\nConnection: close\r\n",
responseNum, responseString, mime_type, timeStr);
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, config->ContentLength);
+ timeStr, Content_length, cont_l_type config->ContentLength);
}
strcat(buf, "\r\n");
len += 2;
responseNum, responseString,
responseNum, responseString, infoString);
}
-#ifdef DEBUG
- if (config->debugHttpd) fprintf(stderr, "Headers: '%s'", buf);
+#if DEBUG
+ fprintf(stderr, "Headers: '%s'", buf);
#endif
return bb_full_write(a_c_w, buf, len);
}
dup2(inFd, 0); // replace stdin with the pipe
dup2(outFd, 1); // replace stdout with the pipe
-
-#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
- if (!config->debugHttpd)
-#endif
+ if(!DEBUG)
dup2(outFd, 2); // replace stderr with the pipe
close(toCgi[0]);
}
if(script != NULL)
*script = '\0'; /* reduce /PATH_INFO */
+ /* SCRIPT_FILENAME required by PHP in CGI mode */
+ if(realpath(purl + 1, realpath_buff))
+ addEnv("SCRIPT", "FILENAME", realpath_buff);
+ else
+ *realpath_buff = 0;
/* set SCRIPT_NAME as full path: /cgi-bin/dirs/script.cgi */
addEnv("SCRIPT_NAME", "", purl);
addEnv("QUERY_STRING", "", config->query);
/* set execve argp[0] without path */
argp[0] = strrchr( purl, '/' ) + 1;
/* but script argp[0] must have absolute path and chdiring to this */
- if(realpath(purl + 1, realpath_buff) != NULL) {
+ if(*realpath_buff) {
script = strrchr(realpath_buff, '/');
if(script) {
*script = '\0';
if(chdir(realpath_buff) == 0) {
- *script = '/';
// now run the program. If it fails,
// use _exit() so no destructors
// get called and make a mess.
- execv(realpath_buff, argp);
+#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
+ char *interpr = NULL;
+ char *suffix = strrchr(purl, '.');
+
+ if(suffix) {
+ Htaccess * cur;
+ for (cur = config->script_i; cur; cur = cur->next)
+ if(strcmp(cur->before_colon + 1, suffix) == 0) {
+ interpr = cur->after_colon;
+ break;
+ }
+ }
+#endif
+ *script = '/';
+#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
+ if (interpr)
+ execv(interpr, argp);
+ else
+#endif
+ execv(realpath_buff, argp);
}
}
}
if (nfound <= 0) {
if (waitpid(pid, &status, WNOHANG) > 0) {
close(inFd);
-#ifdef DEBUG
- if (config->debugHttpd) {
- if (WIFEXITED(status))
+#if DEBUG
+ if (WIFEXITED(status))
bb_error_msg("piped has exited with status=%d", WEXITSTATUS(status));
- if (WIFSIGNALED(status))
+ if (WIFSIGNALED(status))
bb_error_msg("piped has exited with signal=%d", WTERMSIG(status));
- }
#endif
break;
}
post_readed_idx += count;
if(post_readed_size == 0)
post_readed_idx = 0;
+ } else {
+ post_readed_size = post_readed_idx = bodyLen = 0; /* broken pipe to CGI */
}
} else if(bodyLen > 0 && post_readed_size == 0 && FD_ISSET(a_c_r, &readSet)) {
- count = bodyLen > sizeof(wbuf) ? sizeof(wbuf) : bodyLen;
+ count = bodyLen > (int)sizeof(wbuf) ? (int)sizeof(wbuf) : bodyLen;
count = safe_read(a_c_r, wbuf, count);
if(count > 0) {
post_readed_size += count;
bodyLen -= count;
- } else {
+ } else {
bodyLen = 0; /* closed */
}
}
if (firstLine) {
rbuf[count] = 0;
/* check to see if the user script added headers */
- if(strncmp(rbuf, "HTTP/1.0 200 OK\n", 4) != 0) {
- bb_full_write(s, "HTTP/1.0 200 OK\n", 16);
+ if(strncmp(rbuf, "HTTP/1.0 200 OK\r\n", 4) != 0) {
+ bb_full_write(s, "HTTP/1.0 200 OK\r\n", 17);
}
if (strstr(rbuf, "ontent-") == 0) {
- bb_full_write(s, "Content-type: text/plain\n\n", 26);
+ bb_full_write(s, "Content-type: text/plain\r\n\r\n", 28);
}
firstLine = 0;
}
if (bb_full_write(s, rbuf, count) != count)
break;
-#ifdef DEBUG
- if (config->debugHttpd)
- fprintf(stderr, "cgi read %d bytes\n", count);
+#if DEBUG
+ fprintf(stderr, "cgi read %d bytes\n", count);
#endif
}
}
}
#endif /* CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES */
-#ifdef DEBUG
- if (config->debugHttpd)
- fprintf(stderr, "Sending file '%s' Content-type: %s\n",
- url, config->httpd_found.found_mime_type);
+#if DEBUG
+ fprintf(stderr, "Sending file '%s' Content-type: %s\n",
+ url, config->httpd_found.found_mime_type);
#endif
f = open(url, O_RDONLY);
}
close(f);
} else {
-#ifdef DEBUG
- if (config->debugHttpd)
- bb_perror_msg("Unable to open '%s'", url);
+#if DEBUG
+ bb_perror_msg("Unable to open '%s'", url);
#endif
sendHeaders(HTTP_NOT_FOUND);
}
/* This could stand some work */
for (cur = config->ip_a_d; cur; cur = cur->next) {
-#ifdef DEBUG
- if (config->debugHttpd) {
- fprintf(stderr, "checkPermIP: '%s' ? ", config->rmt_ip_str);
- fprintf(stderr, "'%u.%u.%u.%u/%u.%u.%u.%u'\n",
+#if DEBUG
+ fprintf(stderr, "checkPermIP: '%s' ? ", config->rmt_ip_str);
+ fprintf(stderr, "'%u.%u.%u.%u/%u.%u.%u.%u'\n",
(unsigned char)(cur->ip >> 24),
(unsigned char)(cur->ip >> 16),
(unsigned char)(cur->ip >> 8),
(unsigned char)(cur->mask >> 16),
(unsigned char)(cur->mask >> 8),
cur->mask & 0xff);
- }
#endif
if((config->rmt_ip & cur->mask) == cur->ip)
return cur->allow_deny == 'A'; /* Allow/Deny */
if(prev != NULL && strcmp(prev, p0) != 0)
continue; /* find next identical */
p = cur->after_colon;
-#ifdef DEBUG
- if (config->debugHttpd)
- fprintf(stderr,"checkPerm: '%s' ? '%s'\n", p0, request);
+#if DEBUG
+ fprintf(stderr,"checkPerm: '%s' ? '%s'\n", p0, request);
#endif
{
- int l = strlen(p0);
+ size_t l = strlen(p0);
if(strncmp(p0, path, l) == 0 &&
(l == 1 || path[l] == '/' || path[l] == 0)) {
/****************************************************************************
*
- > $Function: handleIncoming()
+ > $Function: handle_sigalrm()
*
- * $Description: Handle an incoming http request.
+ * $Description: Handle timeouts
*
****************************************************************************/
*purl = ' ';
count = sscanf(purl, " %[^ ] HTTP/%d.%*d", buf, &blank);
- decodeString(buf, 0);
if (count < 1 || buf[0] != '/') {
/* Garbled request/URL */
goto BAD_REQUEST;
config->query = test;
}
+ test = decodeString(url, 0);
+ if(test == NULL)
+ goto BAD_REQUEST;
+ if(test == (buf+1)) {
+ sendHeaders(HTTP_NOT_FOUND);
+ break;
+ }
/* algorithm stolen from libbb bb_simplify_path(),
but don`t strdup and reducing trailing slash and protect out root */
purl = test = url;
*++purl = 0; /* so keep last character */
test = purl; /* end ptr */
- /* If URL is directory, adding '/' */
/* If URL is directory, adding '/' */
if(test[-1] != '/') {
if ( is_directory(url + 1, 1, &sb) ) {
config->httpd_found.found_moved_temporarily = url;
}
}
-#ifdef DEBUG
- if (config->debugHttpd)
- fprintf(stderr, "url='%s', args=%s\n", url, config->query);
+#if DEBUG
+ fprintf(stderr, "url='%s', args=%s\n", url, config->query);
#endif
test = url;
}
*test = '/';
}
+ if(blank >= 0) {
+ // read until blank line for HTTP version specified, else parse immediate
+ while(1) {
+ alarm(TIMEOUT);
+ count = getLine();
+ if(count <= 0)
+ break;
- // read until blank line for HTTP version specified, else parse immediate
- while (blank >= 0 && alarm(TIMEOUT) >= 0 && (count = getLine()) > 0) {
-
-#ifdef DEBUG
- if (config->debugHttpd) fprintf(stderr, "Header: '%s'\n", buf);
+#if DEBUG
+ fprintf(stderr, "Header: '%s'\n", buf);
#endif
#ifdef CONFIG_FEATURE_HTTPD_CGI
- /* try and do our best to parse more lines */
- if ((strncasecmp(buf, Content_length, 15) == 0)) {
- if(prequest != request_GET)
- length = strtol(buf + 15, 0, 0); // extra read only for POST
- } else if ((strncasecmp(buf, "Cookie:", 7) == 0)) {
- for(test = buf + 7; isspace(*test); test++)
- ;
- cookie = strdup(test);
- } else if ((strncasecmp(buf, "Content-Type:", 13) == 0)) {
- for(test = buf + 13; isspace(*test); test++)
- ;
- content_type = strdup(test);
- } else if ((strncasecmp(buf, "Referer:", 8) == 0)) {
- for(test = buf + 8; isspace(*test); test++)
- ;
- config->referer = strdup(test);
- }
+ /* try and do our best to parse more lines */
+ if ((strncasecmp(buf, Content_length, 15) == 0)) {
+ if(prequest != request_GET)
+ length = strtol(buf + 15, 0, 0); // extra read only for POST
+ } else if ((strncasecmp(buf, "Cookie:", 7) == 0)) {
+ for(test = buf + 7; isspace(*test); test++)
+ ;
+ cookie = strdup(test);
+ } else if ((strncasecmp(buf, "Content-Type:", 13) == 0)) {
+ for(test = buf + 13; isspace(*test); test++)
+ ;
+ content_type = strdup(test);
+ } else if ((strncasecmp(buf, "Referer:", 8) == 0)) {
+ for(test = buf + 8; isspace(*test); test++)
+ ;
+ config->referer = strdup(test);
+ }
#endif
#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
- if (strncasecmp(buf, "Authorization:", 14) == 0) {
- /* We only allow Basic credentials.
- * It shows up as "Authorization: Basic <userid:password>" where
- * the userid:password is base64 encoded.
- */
- for(test = buf + 14; isspace(*test); test++)
- ;
- if (strncasecmp(test, "Basic", 5) != 0)
- continue;
-
- test += 5; /* decodeBase64() skiping space self */
- decodeBase64(test);
- credentials = checkPerm(url, test);
- }
+ if (strncasecmp(buf, "Authorization:", 14) == 0) {
+ /* We only allow Basic credentials.
+ * It shows up as "Authorization: Basic <userid:password>" where
+ * the userid:password is base64 encoded.
+ */
+ for(test = buf + 14; isspace(*test); test++)
+ ;
+ if (strncasecmp(test, "Basic", 5) != 0)
+ continue;
+
+ test += 5; /* decodeBase64() skiping space self */
+ decodeBase64(test);
+ credentials = checkPerm(url, test);
+ }
#endif /* CONFIG_FEATURE_HTTPD_BASIC_AUTH */
- } /* while extra header reading */
-
+ } /* while extra header reading */
+ }
(void) alarm( 0 );
if(config->alarm_signaled)
break;
if(config->httpd_found.found_moved_temporarily) {
sendHeaders(HTTP_MOVED_TEMPORARILY);
-#ifdef DEBUG
+#if DEBUG
/* clear unforked memory flag */
- if(config->debugHttpd)
- config->httpd_found.found_moved_temporarily = NULL;
+ config->httpd_found.found_moved_temporarily = NULL;
#endif
break;
}
#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
/* from inetd don`t looping: freeing, closing automatic from exit always */
-# ifdef DEBUG
- if (config->debugHttpd) fprintf(stderr, "closing socket\n");
+# if DEBUG
+ fprintf(stderr, "closing socket\n");
# endif
# ifdef CONFIG_FEATURE_HTTPD_CGI
free(cookie);
}
config->accepted_socket = s;
config->rmt_ip = ntohl(fromAddr.sin_addr.s_addr);
-#if defined(CONFIG_FEATURE_HTTPD_CGI) || defined(DEBUG)
+#if ENABLE_FEATURE_HTTPD_CGI || DEBUG
sprintf(config->rmt_ip_str, "%u.%u.%u.%u",
(unsigned char)(config->rmt_ip >> 24),
(unsigned char)(config->rmt_ip >> 16),
(unsigned char)(config->rmt_ip >> 8),
config->rmt_ip & 0xff);
config->port = ntohs(fromAddr.sin_port);
-#ifdef DEBUG
- if (config->debugHttpd) {
- bb_error_msg("connection from IP=%s, port %u\n",
+#if DEBUG
+ bb_error_msg("connection from IP=%s, port %u\n",
config->rmt_ip_str, config->port);
- }
#endif
#endif /* CONFIG_FEATURE_HTTPD_CGI */
on = 1;
setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, (void *)&on, sizeof (on));
- if (config->debugHttpd || fork() == 0) {
+#if !DEBUG
+ if (fork() == 0)
+#endif
+ {
/* This is the spawned thread */
#ifdef CONFIG_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
/* protect reload config, may be confuse checking */
signal(SIGHUP, SIG_IGN);
#endif
handleIncoming();
- if(!config->debugHttpd)
- exit(0);
+#if !DEBUG
+ exit(0);
+#endif
}
close(s);
}
getpeername (0, (struct sockaddr *)&fromAddrLen, &sinlen);
config->rmt_ip = ntohl(fromAddrLen.sin_addr.s_addr);
-#if defined(CONFIG_FEATURE_HTTPD_CGI) || defined(DEBUG)
+#if ENABLE_FEATURE_HTTPD_CGI
sprintf(config->rmt_ip_str, "%u.%u.%u.%u",
(unsigned char)(config->rmt_ip >> 24),
(unsigned char)(config->rmt_ip >> 16),
}
#endif
+enum httpd_opts_nums {
+ c_opt_config_file = 0,
+ d_opt_decode_url,
+ h_opt_home_httpd,
+ USE_FEATURE_HTTPD_ENCODE_URL_STR(e_opt_encode_url,)
+ USE_FEATURE_HTTPD_BASIC_AUTH(r_opt_realm,)
+ USE_FEATURE_HTTPD_AUTH_MD5(m_opt_md5,)
+ USE_FEATURE_HTTPD_SETUID(u_opt_setuid,)
+ SKIP_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY(p_opt_port,)
+};
static const char httpd_opts[]="c:d:h:"
-#ifdef CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
- "e:"
-#define OPT_INC_1 1
-#else
-#define OPT_INC_1 0
-#endif
-#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
- "r:"
-# ifdef CONFIG_FEATURE_HTTPD_AUTH_MD5
- "m:"
-# define OPT_INC_2 2
-# else
-# define OPT_INC_2 1
-#endif
-#else
-#define OPT_INC_2 0
-#endif
-#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
- "p:v"
-#ifdef CONFIG_FEATURE_HTTPD_SETUID
- "u:"
-#endif
-#endif /* CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY */
- ;
+ USE_FEATURE_HTTPD_ENCODE_URL_STR("e:")
+ USE_FEATURE_HTTPD_BASIC_AUTH("r:")
+ USE_FEATURE_HTTPD_AUTH_MD5("m:")
+ USE_FEATURE_HTTPD_SETUID("u:")
+ SKIP_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY("p:");
+
+#define OPT_CONFIG_FILE (1<<c_opt_config_file)
+#define OPT_DECODE_URL (1<<d_opt_decode_url)
+#define OPT_HOME_HTTPD (1<<h_opt_home_httpd)
+
+#define OPT_ENCODE_URL USE_FEATURE_HTTPD_ENCODE_URL_STR((1<<e_opt_encode_url)) \
+ SKIP_FEATURE_HTTPD_ENCODE_URL_STR(0)
-#define OPT_CONFIG_FILE (1<<0)
-#define OPT_DECODE_URL (1<<1)
-#define OPT_HOME_HTTPD (1<<2)
-#define OPT_ENCODE_URL (1<<(2+OPT_INC_1))
-#define OPT_REALM (1<<(3+OPT_INC_1))
-#define OPT_MD5 (1<<(4+OPT_INC_1))
-#define OPT_PORT (1<<(3+OPT_INC_1+OPT_INC_2))
-#define OPT_DEBUG (1<<(4+OPT_INC_1+OPT_INC_2))
-#define OPT_SETUID (1<<(5+OPT_INC_1+OPT_INC_2))
+#define OPT_REALM USE_FEATURE_HTTPD_BASIC_AUTH((1<<r_opt_realm)) \
+ SKIP_FEATURE_HTTPD_BASIC_AUTH(0)
+
+#define OPT_MD5 USE_FEATURE_HTTPD_AUTH_MD5((1<<m_opt_md5)) \
+ SKIP_FEATURE_HTTPD_AUTH_MD5(0)
+
+#define OPT_SETUID USE_FEATURE_HTTPD_SETUID((1<<u_opt_setuid)) \
+ SKIP_FEATURE_HTTPD_SETUID(0)
+
+#define OPT_PORT SKIP_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY((1<<p_opt_port)) \
+ USE_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY(0)
#ifdef HTTPD_STANDALONE
unsigned long opt;
const char *home_httpd = home;
char *url_for_decode;
-#ifdef CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
- const char *url_for_encode;
-#endif
-#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
- const char *s_port;
-#endif
+ USE_FEATURE_HTTPD_ENCODE_URL_STR(const char *url_for_encode;)
+ SKIP_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY(const char *s_port;)
+ SKIP_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY(int server;)
-#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
- int server;
-#endif
+ USE_FEATURE_HTTPD_SETUID(const char *s_uid;)
+ USE_FEATURE_HTTPD_SETUID(long uid = -1;)
-#ifdef CONFIG_FEATURE_HTTPD_SETUID
- const char *s_uid;
- long uid = -1;
-#endif
-
-#ifdef CONFIG_FEATURE_HTTPD_AUTH_MD5
- const char *pass;
-#endif
+ USE_FEATURE_HTTPD_AUTH_MD5(const char *pass;)
config = xcalloc(1, sizeof(*config));
#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
opt = bb_getopt_ulflags(argc, argv, httpd_opts,
&(config->configFile), &url_for_decode, &home_httpd
-#ifdef CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
- , &url_for_encode
-#endif
-#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
- , &(config->realm)
-# ifdef CONFIG_FEATURE_HTTPD_AUTH_MD5
- , &pass
-# endif
-#endif
-#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
- , &s_port
-#ifdef CONFIG_FEATURE_HTTPD_SETUID
- , &s_uid
-#endif
-#endif
- );
+ 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)
+ SKIP_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY(, &s_port)
+ );
if(opt & OPT_DECODE_URL) {
printf("%s", decodeString(url_for_decode, 1));
#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
if(opt & OPT_PORT)
config->port = bb_xgetlarg(s_port, 10, 1, 0xffff);
- config->debugHttpd = opt & OPT_DEBUG;
#ifdef CONFIG_FEATURE_HTTPD_SETUID
if(opt & OPT_SETUID) {
char *e;
uid = strtol(s_uid, &e, 0);
if(*e != '\0') {
/* not integer */
- uid = my_getpwnam(s_uid);
+ uid = bb_xgetpwnam(s_uid);
}
}
#endif
parse_conf(default_path_httpd_conf, FIRST_PARSE);
#endif
-#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
- if (!config->debugHttpd) {
- if (daemon(1, 0) < 0) /* don`t change curent directory */
+#if !ENABLE_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
+# if !DEBUG
+ if (daemon(1, 0) < 0) /* don`t change curent directory */
bb_perror_msg_and_die("daemon");
- }
+# endif
return miniHttpd(server);
#else
return miniHttpd();