httpd: sanitize indentation

This commit is contained in:
Denis Vlasenko 2006-09-26 10:07:41 +00:00
parent a552eeb498
commit 8b8c75e6ab

View File

@ -133,8 +133,7 @@ typedef struct HT_ACCESS_IP {
struct HT_ACCESS_IP *next; struct HT_ACCESS_IP *next;
} Htaccess_IP; } Htaccess_IP;
typedef struct typedef struct {
{
char buf[MAX_MEMORY_BUFF]; char buf[MAX_MEMORY_BUFF];
USE_FEATURE_HTTPD_BASIC_AUTH(const char *realm;) USE_FEATURE_HTTPD_BASIC_AUTH(const char *realm;)
@ -208,7 +207,7 @@ static const char* const suffixTable [] = {
".vrml.wrl", "model/vrml", ".vrml.wrl", "model/vrml",
#endif #endif
0, "application/octet-stream" /* default */ 0, "application/octet-stream" /* default */
}; };
typedef enum typedef enum
{ {
@ -234,7 +233,7 @@ typedef enum
HTTP_PAYMENT_REQUIRED = 402, HTTP_PAYMENT_REQUIRED = 402,
HTTP_BAD_GATEWAY = 502, HTTP_BAD_GATEWAY = 502,
HTTP_SERVICE_UNAVAILABLE = 503, /* overload, maintenance */ HTTP_SERVICE_UNAVAILABLE = 503, /* overload, maintenance */
HTTP_RESPONSE_SETSIZE=0xffffffff HTTP_RESPONSE_SETSIZE = 0xffffffff
#endif #endif
} HttpResponseNum; } HttpResponseNum;
@ -309,7 +308,7 @@ scan_ip (const char **ep, unsigned int *ip, unsigned char endc)
if (*p != endc) if (*p != endc)
return -auto_mask; return -auto_mask;
p++; p++;
if(*p == 0) if (*p == 0)
return -auto_mask; return -auto_mask;
} }
*ep = p; *ep = p;
@ -323,9 +322,9 @@ scan_ip_mask (const char *ipm, unsigned int *ip, unsigned int *mask)
unsigned int msk; unsigned int msk;
i = scan_ip(&ipm, ip, '/'); i = scan_ip(&ipm, ip, '/');
if(i < 0) if (i < 0)
return i; return i;
if(*ipm) { if (*ipm) {
const char *p = ipm; const char *p = ipm;
i = 0; i = 0;
@ -359,7 +358,7 @@ static void free_config_lines(Htaccess **pprev)
{ {
Htaccess *prev = *pprev; Htaccess *prev = *pprev;
while( prev ) { while (prev) {
Htaccess *cur = prev; Htaccess *cur = prev;
prev = cur->next; prev = cur->next;
@ -416,7 +415,7 @@ static void parse_conf(const char *path, int flag)
/* free previous ip setup if present */ /* free previous ip setup if present */
Htaccess_IP *pip = config->ip_a_d; Htaccess_IP *pip = config->ip_a_d;
while( pip ) { while (pip) {
Htaccess_IP *cur_ipl = pip; Htaccess_IP *cur_ipl = pip;
pip = cur_ipl->next; pip = cur_ipl->next;
@ -428,7 +427,7 @@ static void parse_conf(const char *path, int flag)
#if defined(CONFIG_FEATURE_HTTPD_BASIC_AUTH) || defined(CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES) || defined(CONFIG_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR) #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 */ /* retain previous auth and mime config only for subdir parse */
if(flag != SUBDIR_PARSE) { if (flag != SUBDIR_PARSE) {
#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH #ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
free_config_lines(&config->auth); free_config_lines(&config->auth);
#endif #endif
@ -441,22 +440,22 @@ static void parse_conf(const char *path, int flag)
} }
#endif #endif
if(flag == SUBDIR_PARSE || cf == NULL) { if (flag == SUBDIR_PARSE || cf == NULL) {
cf = alloca(strlen(path) + sizeof(httpd_conf) + 2); cf = alloca(strlen(path) + sizeof(httpd_conf) + 2);
if(cf == NULL) { if (cf == NULL) {
if(flag == FIRST_PARSE) if (flag == FIRST_PARSE)
bb_error_msg_and_die(bb_msg_memory_exhausted); bb_error_msg_and_die(bb_msg_memory_exhausted);
return; return;
} }
sprintf((char *)cf, "%s/%s", path, httpd_conf); sprintf((char *)cf, "%s/%s", path, httpd_conf);
} }
while((f = fopen(cf, "r")) == NULL) { while ((f = fopen(cf, "r")) == NULL) {
if(flag == SUBDIR_PARSE || flag == FIND_FROM_HTTPD_ROOT) { if (flag == SUBDIR_PARSE || flag == FIND_FROM_HTTPD_ROOT) {
/* config file not found, no changes to config */ /* config file not found, no changes to config */
return; return;
} }
if(config->configFile && flag == FIRST_PARSE) /* if -c option given */ if (config->configFile && flag == FIRST_PARSE) /* if -c option given */
bb_perror_msg_and_die("%s", cf); bb_perror_msg_and_die("%s", cf);
flag = FIND_FROM_HTTPD_ROOT; flag = FIND_FROM_HTTPD_ROOT;
cf = httpd_conf; cf = httpd_conf;
@ -468,10 +467,10 @@ static void parse_conf(const char *path, int flag)
/* This could stand some work */ /* This could stand some work */
while ( (p0 = fgets(buf, sizeof(buf), f)) != NULL) { while ( (p0 = fgets(buf, sizeof(buf), f)) != NULL) {
c = NULL; c = NULL;
for(p = p0; *p0 != 0 && *p0 != '#'; p0++) { for (p = p0; *p0 != 0 && *p0 != '#'; p0++) {
if(!isspace(*p0)) { if (!isspace(*p0)) {
*p++ = *p0; *p++ = *p0;
if(*p0 == ':' && c == NULL) if (*p0 == ':' && c == NULL)
c = p; c = p;
} }
} }
@ -481,10 +480,10 @@ static void parse_conf(const char *path, int flag)
if (c == NULL || *c == 0) if (c == NULL || *c == 0)
continue; continue;
p0 = buf; p0 = buf;
if(*p0 == 'd') if (*p0 == 'd')
*p0 = 'D'; *p0 = 'D';
if(*c == '*') { if (*c == '*') {
if(*p0 == 'D') { if (*p0 == 'D') {
/* memorize deny all */ /* memorize deny all */
config->flg_deny_all++; config->flg_deny_all++;
} }
@ -492,9 +491,9 @@ static void parse_conf(const char *path, int flag)
continue; continue;
} }
if(*p0 == 'a') if (*p0 == 'a')
*p0 = 'A'; *p0 = 'A';
else if(*p0 != 'D' && *p0 != 'A' else if (*p0 != 'D' && *p0 != 'A'
#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH #ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
&& *p0 != '/' && *p0 != '/'
#endif #endif
@ -506,17 +505,17 @@ static void parse_conf(const char *path, int flag)
#endif #endif
) )
continue; continue;
if(*p0 == 'A' || *p0 == 'D') { if (*p0 == 'A' || *p0 == 'D') {
/* storing current config IP line */ /* storing current config IP line */
pip = calloc(1, sizeof(Htaccess_IP)); pip = calloc(1, sizeof(Htaccess_IP));
if(pip) { if (pip) {
if(scan_ip_mask (c, &(pip->ip), &(pip->mask))) { if (scan_ip_mask (c, &(pip->ip), &(pip->mask))) {
/* syntax IP{/mask} error detected, protect all */ /* syntax IP{/mask} error detected, protect all */
*p0 = 'D'; *p0 = 'D';
pip->mask = 0; pip->mask = 0;
} }
pip->allow_deny = *p0; pip->allow_deny = *p0;
if(*p0 == 'D') { if (*p0 == 'D') {
/* Deny:form_IP move top */ /* Deny:form_IP move top */
pip->next = config->ip_a_d; pip->next = config->ip_a_d;
config->ip_a_d = pip; config->ip_a_d = pip;
@ -524,10 +523,10 @@ static void parse_conf(const char *path, int flag)
/* add to bottom A:form_IP config line */ /* add to bottom A:form_IP config line */
Htaccess_IP *prev_IP = config->ip_a_d; Htaccess_IP *prev_IP = config->ip_a_d;
if(prev_IP == NULL) { if (prev_IP == NULL) {
config->ip_a_d = pip; config->ip_a_d = pip;
} else { } else {
while(prev_IP->next) while (prev_IP->next)
prev_IP = prev_IP->next; prev_IP = prev_IP->next;
prev_IP->next = pip; prev_IP->next = pip;
} }
@ -536,11 +535,11 @@ static void parse_conf(const char *path, int flag)
continue; continue;
} }
#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH #ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
if(*p0 == '/') { if (*p0 == '/') {
/* make full path from httpd root / curent_path / config_line_path */ /* make full path from httpd root / curent_path / config_line_path */
cf = flag == SUBDIR_PARSE ? path : ""; cf = flag == SUBDIR_PARSE ? path : "";
p0 = malloc(strlen(cf) + (c - buf) + 2 + strlen(c)); p0 = malloc(strlen(cf) + (c - buf) + 2 + strlen(c));
if(p0 == NULL) if (p0 == NULL)
continue; continue;
c[-1] = 0; c[-1] = 0;
sprintf(p0, "/%s%s", cf, buf); sprintf(p0, "/%s%s", cf, buf);
@ -578,13 +577,13 @@ static void parse_conf(const char *path, int flag)
#if defined(CONFIG_FEATURE_HTTPD_BASIC_AUTH) || defined(CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES) || defined(CONFIG_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR) #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 */ /* storing current config line */
cur = calloc(1, sizeof(Htaccess) + strlen(p0)); cur = calloc(1, sizeof(Htaccess) + strlen(p0));
if(cur) { if (cur) {
cf = strcpy(cur->before_colon, p0); cf = strcpy(cur->before_colon, p0);
c = strchr(cf, ':'); c = strchr(cf, ':');
*c++ = 0; *c++ = 0;
cur->after_colon = c; cur->after_colon = c;
#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES #ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
if(*cf == '.') { if (*cf == '.') {
/* config .mime line move top for overwrite previous */ /* config .mime line move top for overwrite previous */
cur->next = config->mime_a; cur->next = config->mime_a;
config->mime_a = cur; config->mime_a = cur;
@ -592,7 +591,7 @@ static void parse_conf(const char *path, int flag)
} }
#endif #endif
#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR #ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
if(*cf == '*' && cf[1] == '.') { if (*cf == '*' && cf[1] == '.') {
/* config script interpreter line move top for overwrite previous */ /* config script interpreter line move top for overwrite previous */
cur->next = config->script_i; cur->next = config->script_i;
config->script_i = cur; config->script_i = cur;
@ -601,7 +600,7 @@ static void parse_conf(const char *path, int flag)
#endif #endif
#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH #ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
free(p0); free(p0);
if(prev == NULL) { if (prev == NULL) {
/* first line */ /* first line */
config->auth = prev = cur; config->auth = prev = cur;
} else { } else {
@ -610,11 +609,11 @@ static void parse_conf(const char *path, int flag)
size_t l = strlen(cf); size_t l = strlen(cf);
Htaccess *hti; Htaccess *hti;
for(hti = prev_hti; hti; hti = hti->next) { for (hti = prev_hti; hti; hti = hti->next) {
if(l >= strlen(hti->before_colon)) { if (l >= strlen(hti->before_colon)) {
/* insert before hti */ /* insert before hti */
cur->next = hti; cur->next = hti;
if(prev_hti != hti) { if (prev_hti != hti) {
prev_hti->next = cur; prev_hti->next = cur;
} else { } else {
/* insert as top */ /* insert as top */
@ -622,10 +621,10 @@ static void parse_conf(const char *path, int flag)
} }
break; break;
} }
if(prev_hti != hti) if (prev_hti != hti)
prev_hti = prev_hti->next; prev_hti = prev_hti->next;
} }
if(!hti) { /* not inserted, add to bottom */ if (!hti) { /* not inserted, add to bottom */
prev->next = cur; prev->next = cur;
prev = cur; prev = cur;
} }
@ -661,7 +660,7 @@ static char *encodeString(const char *string)
/* could possibly scan once to get length. */ /* could possibly scan once to get length. */
int len = strlen(string); int len = strlen(string);
char *out = malloc(len * 6 + 1); char *out = malloc(len * 6 + 1);
char *p=out; char *p = out;
char ch; char ch;
if (!out) return ""; if (!out) return "";
@ -670,7 +669,7 @@ static char *encodeString(const char *string)
if (isalnum(ch)) *p++ = ch; if (isalnum(ch)) *p++ = ch;
else p += sprintf(p, "&#%d;", (unsigned char) ch); else p += sprintf(p, "&#%d;", (unsigned char) ch);
} }
*p=0; *p = 0;
return out; return out;
} }
#endif /* CONFIG_FEATURE_HTTPD_ENCODE_URL_STR */ #endif /* CONFIG_FEATURE_HTTPD_ENCODE_URL_STR */
@ -700,22 +699,21 @@ static char *decodeString(char *orig, int flag_plus_to_space)
char *string = orig; char *string = orig;
char *ptr = string; char *ptr = string;
while (*ptr) while (*ptr) {
{
if (*ptr == '+' && flag_plus_to_space) { *string++ = ' '; ptr++; } if (*ptr == '+' && flag_plus_to_space) { *string++ = ' '; ptr++; }
else if (*ptr != '%') *string++ = *ptr++; else if (*ptr != '%') *string++ = *ptr++;
else { else {
unsigned int value1, value2; unsigned int value1, value2;
ptr++; ptr++;
if(sscanf(ptr, "%1X", &value1) != 1 || if (sscanf(ptr, "%1X", &value1) != 1 ||
sscanf(ptr+1, "%1X", &value2) != 1) { sscanf(ptr+1, "%1X", &value2) != 1) {
if(!flag_plus_to_space) if (!flag_plus_to_space)
return NULL; return NULL;
*string++ = '%'; *string++ = '%';
} else { } else {
value1 = value1 * 16 + value2; value1 = value1 * 16 + value2;
if(value1 == '/' || value1 == 0) if (value1 == '/' || value1 == 0)
return orig+1; return orig+1;
*string++ = value1; *string++ = value1;
ptr += 2; ptr += 2;
@ -757,7 +755,7 @@ static void addEnv(const char *name_before_underline,
underline = *name_after_underline ? "_" : ""; underline = *name_after_underline ? "_" : "";
asprintf(&s, "%s%s%s=%s", name_before_underline, underline, asprintf(&s, "%s%s%s=%s", name_before_underline, underline,
name_after_underline, value); name_after_underline, value);
if(s) { if (s) {
putenv(s); putenv(s);
} }
} }
@ -805,17 +803,17 @@ static void decodeBase64(char *Data)
while (*in) { while (*in) {
int t = *in++; int t = *in++;
if(t >= '0' && t <= '9') if (t >= '0' && t <= '9')
t = t - '0' + 52; t = t - '0' + 52;
else if(t >= 'A' && t <= 'Z') else if (t >= 'A' && t <= 'Z')
t = t - 'A'; t = t - 'A';
else if(t >= 'a' && t <= 'z') else if (t >= 'a' && t <= 'z')
t = t - 'a' + 26; t = t - 'a' + 26;
else if(t == '+') else if (t == '+')
t = 62; t = 62;
else if(t == '/') else if (t == '/')
t = 63; t = 63;
else if(t == '=') else if (t == '=')
t = 0; t = 0;
else else
continue; continue;
@ -924,7 +922,7 @@ static int sendHeaders(HttpResponseNum responseNum)
config->realm); config->realm);
} }
#endif #endif
if(responseNum == HTTP_MOVED_TEMPORARILY) { if (responseNum == HTTP_MOVED_TEMPORARILY) {
len += sprintf(buf+len, "Location: %s/%s%s\r\n", len += sprintf(buf+len, "Location: %s/%s%s\r\n",
config->httpd_found.found_moved_temporarily, config->httpd_found.found_moved_temporarily,
(config->query ? "?" : ""), (config->query ? "?" : ""),
@ -973,7 +971,7 @@ static int getLine(void)
buf[count] = 0; buf[count] = 0;
return count; return count;
} }
if(count < (MAX_MEMORY_BUFF-1)) /* check owerflow */ if (count < (MAX_MEMORY_BUFF-1)) /* check owerflow */
count++; count++;
} }
if (count) return count; if (count) return count;
@ -1033,10 +1031,10 @@ static int sendCgi(const char *url,
if (!pid) { if (!pid) {
/* child process */ /* child process */
char *script; char *script;
char *purl = strdup( url ); char *purl = strdup(url);
char realpath_buff[MAXPATHLEN]; char realpath_buff[MAXPATHLEN];
if(purl == NULL) if (purl == NULL)
_exit(242); _exit(242);
inFd = toCgi[0]; inFd = toCgi[0];
@ -1044,7 +1042,7 @@ static int sendCgi(const char *url,
dup2(inFd, 0); // replace stdin with the pipe dup2(inFd, 0); // replace stdin with the pipe
dup2(outFd, 1); // replace stdout with the pipe dup2(outFd, 1); // replace stdout with the pipe
if(!DEBUG) if (!DEBUG)
dup2(outFd, 2); // replace stderr with the pipe dup2(outFd, 2); // replace stderr with the pipe
close(toCgi[0]); close(toCgi[0]);
@ -1056,12 +1054,12 @@ static int sendCgi(const char *url,
* Find PATH_INFO. * Find PATH_INFO.
*/ */
script = purl; script = purl;
while((script = strchr( script + 1, '/' )) != NULL) { while ((script = strchr(script + 1, '/')) != NULL) {
/* have script.cgi/PATH_INFO or dirs/script.cgi[/PATH_INFO] */ /* have script.cgi/PATH_INFO or dirs/script.cgi[/PATH_INFO] */
struct stat sb; struct stat sb;
*script = '\0'; *script = '\0';
if(is_directory(purl + 1, 1, &sb) == 0) { if (is_directory(purl + 1, 1, &sb) == 0) {
/* not directory, found script.cgi/PATH_INFO */ /* not directory, found script.cgi/PATH_INFO */
*script = '/'; *script = '/';
break; break;
@ -1071,18 +1069,18 @@ static int sendCgi(const char *url,
addEnv("PATH", "INFO", script); /* set /PATH_INFO or NULL */ addEnv("PATH", "INFO", script); /* set /PATH_INFO or NULL */
addEnv("PATH", "", getenv("PATH")); addEnv("PATH", "", getenv("PATH"));
addEnv("REQUEST", "METHOD", request); addEnv("REQUEST", "METHOD", request);
if(config->query) { if (config->query) {
char *uri = alloca(strlen(purl) + 2 + strlen(config->query)); char *uri = alloca(strlen(purl) + 2 + strlen(config->query));
if(uri) if (uri)
sprintf(uri, "%s?%s", purl, config->query); sprintf(uri, "%s?%s", purl, config->query);
addEnv("REQUEST", "URI", uri); addEnv("REQUEST", "URI", uri);
} else { } else {
addEnv("REQUEST", "URI", purl); addEnv("REQUEST", "URI", purl);
} }
if(script != NULL) if (script != NULL)
*script = '\0'; /* reduce /PATH_INFO */ *script = '\0'; /* reduce /PATH_INFO */
/* SCRIPT_FILENAME required by PHP in CGI mode */ /* SCRIPT_FILENAME required by PHP in CGI mode */
if(realpath(purl + 1, realpath_buff)) if (realpath(purl + 1, realpath_buff))
addEnv("SCRIPT", "FILENAME", realpath_buff); addEnv("SCRIPT", "FILENAME", realpath_buff);
else else
*realpath_buff = 0; *realpath_buff = 0;
@ -1096,33 +1094,33 @@ static int sendCgi(const char *url,
#ifdef CONFIG_FEATURE_HTTPD_SET_REMOTE_PORT_TO_ENV #ifdef CONFIG_FEATURE_HTTPD_SET_REMOTE_PORT_TO_ENV
addEnvPort("REMOTE"); addEnvPort("REMOTE");
#endif #endif
if(bodyLen) { if (bodyLen) {
char sbl[32]; char sbl[32];
sprintf(sbl, "%d", bodyLen); sprintf(sbl, "%d", bodyLen);
addEnv("CONTENT", "LENGTH", sbl); addEnv("CONTENT", "LENGTH", sbl);
} }
if(cookie) if (cookie)
addEnv("HTTP", "COOKIE", cookie); addEnv("HTTP", "COOKIE", cookie);
if(content_type) if (content_type)
addEnv("CONTENT", "TYPE", content_type); addEnv("CONTENT", "TYPE", content_type);
#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH #ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
if(config->remoteuser) { if (config->remoteuser) {
addEnv("REMOTE", "USER", config->remoteuser); addEnv("REMOTE", "USER", config->remoteuser);
addEnv("AUTH_TYPE", "", "Basic"); addEnv("AUTH_TYPE", "", "Basic");
} }
#endif #endif
if(config->referer) if (config->referer)
addEnv("HTTP", "REFERER", config->referer); addEnv("HTTP", "REFERER", config->referer);
/* set execve argp[0] without path */ /* set execve argp[0] without path */
argp[0] = strrchr( purl, '/' ) + 1; argp[0] = strrchr(purl, '/') + 1;
/* but script argp[0] must have absolute path and chdiring to this */ /* but script argp[0] must have absolute path and chdiring to this */
if(*realpath_buff) { if (*realpath_buff) {
script = strrchr(realpath_buff, '/'); script = strrchr(realpath_buff, '/');
if(script) { if (script) {
*script = '\0'; *script = '\0';
if(chdir(realpath_buff) == 0) { if (chdir(realpath_buff) == 0) {
// now run the program. If it fails, // now run the program. If it fails,
// use _exit() so no destructors // use _exit() so no destructors
// get called and make a mess. // get called and make a mess.
@ -1130,10 +1128,10 @@ static int sendCgi(const char *url,
char *interpr = NULL; char *interpr = NULL;
char *suffix = strrchr(purl, '.'); char *suffix = strrchr(purl, '.');
if(suffix) { if (suffix) {
Htaccess * cur; Htaccess * cur;
for (cur = config->script_i; cur; cur = cur->next) for (cur = config->script_i; cur; cur = cur->next)
if(strcmp(cur->before_colon + 1, suffix) == 0) { if (strcmp(cur->before_colon + 1, suffix) == 0) {
interpr = cur->after_colon; interpr = cur->after_colon;
break; break;
} }
@ -1179,18 +1177,18 @@ static int sendCgi(const char *url,
FD_ZERO(&readSet); FD_ZERO(&readSet);
FD_ZERO(&writeSet); FD_ZERO(&writeSet);
FD_SET(inFd, &readSet); FD_SET(inFd, &readSet);
if(bodyLen > 0 || post_readed_size > 0) { if (bodyLen > 0 || post_readed_size > 0) {
FD_SET(outFd, &writeSet); FD_SET(outFd, &writeSet);
nfound = outFd > inFd ? outFd : inFd; nfound = outFd > inFd ? outFd : inFd;
if(post_readed_size == 0) { if (post_readed_size == 0) {
FD_SET(a_c_r, &readSet); FD_SET(a_c_r, &readSet);
if(nfound < a_c_r) if (nfound < a_c_r)
nfound = a_c_r; nfound = a_c_r;
} }
/* Now wait on the set of sockets! */ /* Now wait on the set of sockets! */
nfound = select(nfound + 1, &readSet, &writeSet, 0, NULL); nfound = select(nfound + 1, &readSet, &writeSet, 0, NULL);
} else { } else {
if(!bodyLen) { if (!bodyLen) {
close(outFd); close(outFd);
bodyLen = -1; bodyLen = -1;
} }
@ -1208,27 +1206,27 @@ static int sendCgi(const char *url,
#endif #endif
break; break;
} }
} else if(post_readed_size > 0 && FD_ISSET(outFd, &writeSet)) { } else if (post_readed_size > 0 && FD_ISSET(outFd, &writeSet)) {
count = full_write(outFd, wbuf + post_readed_idx, post_readed_size); count = full_write(outFd, wbuf + post_readed_idx, post_readed_size);
if(count > 0) { if (count > 0) {
post_readed_size -= count; post_readed_size -= count;
post_readed_idx += count; post_readed_idx += count;
if(post_readed_size == 0) if (post_readed_size == 0)
post_readed_idx = 0; post_readed_idx = 0;
} else { } else {
post_readed_size = post_readed_idx = bodyLen = 0; /* broken pipe to CGI */ 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)) { } else if (bodyLen > 0 && post_readed_size == 0 && FD_ISSET(a_c_r, &readSet)) {
count = bodyLen > (int)sizeof(wbuf) ? (int)sizeof(wbuf) : bodyLen; count = bodyLen > (int)sizeof(wbuf) ? (int)sizeof(wbuf) : bodyLen;
count = safe_read(a_c_r, wbuf, count); count = safe_read(a_c_r, wbuf, count);
if(count > 0) { if (count > 0) {
post_readed_size += count; post_readed_size += count;
bodyLen -= count; bodyLen -= count;
} else { } else {
bodyLen = 0; /* closed */ bodyLen = 0; /* closed */
} }
} }
if(FD_ISSET(inFd, &readSet)) { if (FD_ISSET(inFd, &readSet)) {
int s = a_c_w; int s = a_c_w;
char *rbuf = config->buf; char *rbuf = config->buf;
@ -1249,7 +1247,7 @@ static int sendCgi(const char *url,
if (firstLine) { if (firstLine) {
rbuf[count] = 0; rbuf[count] = 0;
/* check to see if the user script added headers */ /* check to see if the user script added headers */
if(strncmp(rbuf, "HTTP/1.0 200 OK\r\n", 4) != 0) { if (strncmp(rbuf, "HTTP/1.0 200 OK\r\n", 4) != 0) {
full_write(s, "HTTP/1.0 200 OK\r\n", 17); full_write(s, "HTTP/1.0 200 OK\r\n", 17);
} }
if (strstr(rbuf, "ontent-") == 0) { if (strstr(rbuf, "ontent-") == 0) {
@ -1293,9 +1291,9 @@ static int sendFile(const char *url)
suffix = strrchr(url, '.'); suffix = strrchr(url, '.');
for (table = suffixTable; *table; table += 2) for (table = suffixTable; *table; table += 2)
if(suffix != NULL && (try_suffix = strstr(*table, suffix)) != 0) { if (suffix != NULL && (try_suffix = strstr(*table, suffix)) != 0) {
try_suffix += strlen(suffix); try_suffix += strlen(suffix);
if(*try_suffix == 0 || *try_suffix == '.') if (*try_suffix == 0 || *try_suffix == '.')
break; break;
} }
/* also, if not found, set default as "application/octet-stream"; */ /* also, if not found, set default as "application/octet-stream"; */
@ -1305,7 +1303,7 @@ static int sendFile(const char *url)
Htaccess * cur; Htaccess * cur;
for (cur = config->mime_a; cur; cur = cur->next) { for (cur = config->mime_a; cur; cur = cur->next) {
if(strcmp(cur->before_colon, suffix) == 0) { if (strcmp(cur->before_colon, suffix) == 0) {
config->httpd_found.found_mime_type = cur->after_colon; config->httpd_found.found_mime_type = cur->after_colon;
break; break;
} }
@ -1357,7 +1355,7 @@ static int checkPermIP(void)
(unsigned char)(cur->mask >> 8), (unsigned char)(cur->mask >> 8),
cur->mask & 0xff); cur->mask & 0xff);
#endif #endif
if((config->rmt_ip & cur->mask) == cur->ip) if ((config->rmt_ip & cur->mask) == cur->ip)
return cur->allow_deny == 'A'; /* Allow/Deny */ return cur->allow_deny == 'A'; /* Allow/Deny */
} }
@ -1394,7 +1392,7 @@ static int checkPerm(const char *path, const char *request)
/* This could stand some work */ /* This could stand some work */
for (cur = config->auth; cur; cur = cur->next) { for (cur = config->auth; cur; cur = cur->next) {
p0 = cur->before_colon; p0 = cur->before_colon;
if(prev != NULL && strcmp(prev, p0) != 0) if (prev != NULL && strcmp(prev, p0) != 0)
continue; /* find next identical */ continue; /* find next identical */
p = cur->after_colon; p = cur->after_colon;
#if DEBUG #if DEBUG
@ -1403,14 +1401,14 @@ static int checkPerm(const char *path, const char *request)
{ {
size_t l = strlen(p0); size_t l = strlen(p0);
if(strncmp(p0, path, l) == 0 && if (strncmp(p0, path, l) == 0 &&
(l == 1 || path[l] == '/' || path[l] == 0)) { (l == 1 || path[l] == '/' || path[l] == 0)) {
char *u; char *u;
/* path match found. Check request */ /* path match found. Check request */
/* for check next /path:user:password */ /* for check next /path:user:password */
prev = p0; prev = p0;
u = strchr(request, ':'); u = strchr(request, ':');
if(u == NULL) { if (u == NULL) {
/* bad request, ':' required */ /* bad request, ':' required */
break; break;
} }
@ -1420,12 +1418,12 @@ static int checkPerm(const char *path, const char *request)
char *cipher; char *cipher;
char *pp; char *pp;
if(strncmp(p, request, u-request) != 0) { if (strncmp(p, request, u-request) != 0) {
/* user uncompared */ /* user uncompared */
continue; continue;
} }
pp = strchr(p, ':'); pp = strchr(p, ':');
if(pp && pp[1] == '$' && pp[2] == '1' && if (pp && pp[1] == '$' && pp[2] == '1' &&
pp[3] == '$' && pp[4]) { pp[3] == '$' && pp[4]) {
pp++; pp++;
cipher = pw_encrypt(u+1, pp); cipher = pw_encrypt(u+1, pp);
@ -1441,7 +1439,7 @@ static int checkPerm(const char *path, const char *request)
set_remoteuser_var: set_remoteuser_var:
#endif #endif
config->remoteuser = strdup(request); config->remoteuser = strdup(request);
if(config->remoteuser) if (config->remoteuser)
config->remoteuser[(u - request)] = 0; config->remoteuser[(u - request)] = 0;
return 1; /* Ok */ return 1; /* Ok */
} }
@ -1463,8 +1461,7 @@ set_remoteuser_var:
* *
****************************************************************************/ ****************************************************************************/
static void static void handle_sigalrm(int sig)
handle_sigalrm( int sig )
{ {
sendHeaders(HTTP_REQUEST_TIMEOUT); sendHeaders(HTTP_REQUEST_TIMEOUT);
config->alarm_signaled = sig; config->alarm_signaled = sig;
@ -1509,27 +1506,27 @@ static void handleIncoming(void)
do { do {
int count; int count;
(void) alarm( TIMEOUT ); (void) alarm(TIMEOUT);
if (getLine() <= 0) if (getLine() <= 0)
break; /* closed */ break; /* closed */
purl = strpbrk(buf, " \t"); purl = strpbrk(buf, " \t");
if(purl == NULL) { if (purl == NULL) {
BAD_REQUEST: BAD_REQUEST:
sendHeaders(HTTP_BAD_REQUEST); sendHeaders(HTTP_BAD_REQUEST);
break; break;
} }
*purl = 0; *purl = 0;
#ifdef CONFIG_FEATURE_HTTPD_CGI #ifdef CONFIG_FEATURE_HTTPD_CGI
if(strcasecmp(buf, prequest) != 0) { if (strcasecmp(buf, prequest) != 0) {
prequest = "POST"; prequest = "POST";
if(strcasecmp(buf, prequest) != 0) { if (strcasecmp(buf, prequest) != 0) {
sendHeaders(HTTP_NOT_IMPLEMENTED); sendHeaders(HTTP_NOT_IMPLEMENTED);
break; break;
} }
} }
#else #else
if(strcasecmp(buf, request_GET) != 0) { if (strcasecmp(buf, request_GET) != 0) {
sendHeaders(HTTP_NOT_IMPLEMENTED); sendHeaders(HTTP_NOT_IMPLEMENTED);
break; break;
} }
@ -1542,7 +1539,7 @@ BAD_REQUEST:
goto BAD_REQUEST; goto BAD_REQUEST;
} }
url = alloca(strlen(buf) + 12); /* + sizeof("/index.html\0") */ url = alloca(strlen(buf) + 12); /* + sizeof("/index.html\0") */
if(url == NULL) { if (url == NULL) {
sendHeaders(HTTP_INTERNAL_SERVER_ERROR); sendHeaders(HTTP_INTERNAL_SERVER_ERROR);
break; break;
} }
@ -1555,9 +1552,9 @@ BAD_REQUEST:
} }
test = decodeString(url, 0); test = decodeString(url, 0);
if(test == NULL) if (test == NULL)
goto BAD_REQUEST; goto BAD_REQUEST;
if(test == (buf+1)) { if (test == (buf+1)) {
sendHeaders(HTTP_NOT_FOUND); sendHeaders(HTTP_NOT_FOUND);
break; break;
} }
@ -1590,8 +1587,8 @@ BAD_REQUEST:
test = purl; /* end ptr */ test = purl; /* end ptr */
/* If URL is directory, adding '/' */ /* If URL is directory, adding '/' */
if(test[-1] != '/') { if (test[-1] != '/') {
if ( is_directory(url + 1, 1, &sb) ) { if (is_directory(url + 1, 1, &sb)) {
config->httpd_found.found_moved_temporarily = url; config->httpd_found.found_moved_temporarily = url;
} }
} }
@ -1601,22 +1598,22 @@ BAD_REQUEST:
test = url; test = url;
ip_allowed = checkPermIP(); ip_allowed = checkPermIP();
while(ip_allowed && (test = strchr( test + 1, '/' )) != NULL) { while (ip_allowed && (test = strchr(test + 1, '/')) != NULL) {
/* have path1/path2 */ /* have path1/path2 */
*test = '\0'; *test = '\0';
if( is_directory(url + 1, 1, &sb) ) { if (is_directory(url + 1, 1, &sb)) {
/* may be having subdir config */ /* may be having subdir config */
parse_conf(url + 1, SUBDIR_PARSE); parse_conf(url + 1, SUBDIR_PARSE);
ip_allowed = checkPermIP(); ip_allowed = checkPermIP();
} }
*test = '/'; *test = '/';
} }
if(blank >= 0) { if (blank >= 0) {
// read until blank line for HTTP version specified, else parse immediate // read until blank line for HTTP version specified, else parse immediate
while(1) { while (1) {
alarm(TIMEOUT); alarm(TIMEOUT);
count = getLine(); count = getLine();
if(count <= 0) if (count <= 0)
break; break;
#if DEBUG #if DEBUG
@ -1626,18 +1623,18 @@ BAD_REQUEST:
#ifdef CONFIG_FEATURE_HTTPD_CGI #ifdef CONFIG_FEATURE_HTTPD_CGI
/* try and do our best to parse more lines */ /* try and do our best to parse more lines */
if ((strncasecmp(buf, Content_length, 15) == 0)) { if ((strncasecmp(buf, Content_length, 15) == 0)) {
if(prequest != request_GET) if (prequest != request_GET)
length = strtol(buf + 15, 0, 0); // extra read only for POST length = strtol(buf + 15, 0, 0); // extra read only for POST
} else if ((strncasecmp(buf, "Cookie:", 7) == 0)) { } else if ((strncasecmp(buf, "Cookie:", 7) == 0)) {
for(test = buf + 7; isspace(*test); test++) for (test = buf + 7; isspace(*test); test++)
; ;
cookie = strdup(test); cookie = strdup(test);
} else if ((strncasecmp(buf, "Content-Type:", 13) == 0)) { } else if ((strncasecmp(buf, "Content-Type:", 13) == 0)) {
for(test = buf + 13; isspace(*test); test++) for (test = buf + 13; isspace(*test); test++)
; ;
content_type = strdup(test); content_type = strdup(test);
} else if ((strncasecmp(buf, "Referer:", 8) == 0)) { } else if ((strncasecmp(buf, "Referer:", 8) == 0)) {
for(test = buf + 8; isspace(*test); test++) for (test = buf + 8; isspace(*test); test++)
; ;
config->referer = strdup(test); config->referer = strdup(test);
} }
@ -1649,7 +1646,7 @@ BAD_REQUEST:
* It shows up as "Authorization: Basic <userid:password>" where * It shows up as "Authorization: Basic <userid:password>" where
* the userid:password is base64 encoded. * the userid:password is base64 encoded.
*/ */
for(test = buf + 14; isspace(*test); test++) for (test = buf + 14; isspace(*test); test++)
; ;
if (strncasecmp(test, "Basic", 5) != 0) if (strncasecmp(test, "Basic", 5) != 0)
continue; continue;
@ -1662,8 +1659,8 @@ BAD_REQUEST:
} /* while extra header reading */ } /* while extra header reading */
} }
(void) alarm( 0 ); (void) alarm(0);
if(config->alarm_signaled) if (config->alarm_signaled)
break; break;
if (strcmp(strrchr(url, '/') + 1, httpd_conf) == 0 || ip_allowed == 0) { if (strcmp(strrchr(url, '/') + 1, httpd_conf) == 0 || ip_allowed == 0) {
@ -1682,7 +1679,7 @@ FORBIDDEN: /* protect listing /cgi-bin */
} }
#endif #endif
if(config->httpd_found.found_moved_temporarily) { if (config->httpd_found.found_moved_temporarily) {
sendHeaders(HTTP_MOVED_TEMPORARILY); sendHeaders(HTTP_MOVED_TEMPORARILY);
#if DEBUG #if DEBUG
/* clear unforked memory flag */ /* clear unforked memory flag */
@ -1699,7 +1696,7 @@ FORBIDDEN: /* protect listing /cgi-bin */
break; break;
if (strncmp(test, "cgi-bin", 7) == 0) { if (strncmp(test, "cgi-bin", 7) == 0) {
if(test[7] == '/' && test[8] == 0) if (test[7] == '/' && test[8] == 0)
goto FORBIDDEN; // protect listing cgi-bin/ goto FORBIDDEN; // protect listing cgi-bin/
sendCgi(url, prequest, length, cookie, content_type); sendCgi(url, prequest, length, cookie, content_type);
} else { } else {
@ -1707,9 +1704,9 @@ FORBIDDEN: /* protect listing /cgi-bin */
sendHeaders(HTTP_NOT_IMPLEMENTED); sendHeaders(HTTP_NOT_IMPLEMENTED);
else { else {
#endif /* CONFIG_FEATURE_HTTPD_CGI */ #endif /* CONFIG_FEATURE_HTTPD_CGI */
if(purl[-1] == '/') if (purl[-1] == '/')
strcpy(purl, "index.html"); strcpy(purl, "index.html");
if ( stat(test, &sb ) == 0 ) { if (stat(test, &sb) == 0) {
config->ContentLength = sb.st_size; config->ContentLength = sb.st_size;
config->last_mod = sb.st_mtime; config->last_mod = sb.st_mtime;
} }
@ -1868,8 +1865,7 @@ static void sighup_handler(int sig)
/* set and reset */ /* set and reset */
struct sigaction sa; struct sigaction sa;
parse_conf(default_path_httpd_conf, parse_conf(default_path_httpd_conf, sig == SIGHUP ? SIGNALED_PARSE : FIRST_PARSE);
sig == SIGHUP ? SIGNALED_PARSE : FIRST_PARSE);
sa.sa_handler = sighup_handler; sa.sa_handler = sighup_handler;
sigemptyset(&sa.sa_mask); sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART; sa.sa_flags = SA_RESTART;
@ -1949,31 +1945,31 @@ int httpd_main(int argc, char *argv[])
USE_FEATURE_HTTPD_WITHOUT_INETD(, &s_port) USE_FEATURE_HTTPD_WITHOUT_INETD(, &s_port)
); );
if(opt & OPT_DECODE_URL) { if (opt & OPT_DECODE_URL) {
printf("%s", decodeString(url_for_decode, 1)); printf("%s", decodeString(url_for_decode, 1));
return 0; return 0;
} }
#ifdef CONFIG_FEATURE_HTTPD_ENCODE_URL_STR #ifdef CONFIG_FEATURE_HTTPD_ENCODE_URL_STR
if(opt & OPT_ENCODE_URL) { if (opt & OPT_ENCODE_URL) {
printf("%s", encodeString(url_for_encode)); printf("%s", encodeString(url_for_encode));
return 0; return 0;
} }
#endif #endif
#ifdef CONFIG_FEATURE_HTTPD_AUTH_MD5 #ifdef CONFIG_FEATURE_HTTPD_AUTH_MD5
if(opt & OPT_MD5) { if (opt & OPT_MD5) {
printf("%s\n", pw_encrypt(pass, "$1$")); printf("%s\n", pw_encrypt(pass, "$1$"));
return 0; return 0;
} }
#endif #endif
#ifdef CONFIG_FEATURE_HTTPD_WITHOUT_INETD #ifdef CONFIG_FEATURE_HTTPD_WITHOUT_INETD
if(opt & OPT_PORT) if (opt & OPT_PORT)
config->port = bb_xgetlarg(s_port, 10, 1, 0xffff); config->port = bb_xgetlarg(s_port, 10, 1, 0xffff);
#ifdef CONFIG_FEATURE_HTTPD_SETUID #ifdef CONFIG_FEATURE_HTTPD_SETUID
if(opt & OPT_SETUID) { if (opt & OPT_SETUID) {
char *e; char *e;
uid = strtol(s_uid, &e, 0); uid = strtol(s_uid, &e, 0);
if(*e != '\0') { if (*e != '\0') {
/* not integer */ /* not integer */
uid = bb_xgetpwnam(s_uid); uid = bb_xgetpwnam(s_uid);
} }
@ -1986,7 +1982,7 @@ int httpd_main(int argc, char *argv[])
server = openServer(); server = openServer();
# ifdef CONFIG_FEATURE_HTTPD_SETUID # ifdef CONFIG_FEATURE_HTTPD_SETUID
/* drop privileges */ /* drop privileges */
if(uid > 0) if (uid > 0)
xsetuid(uid); xsetuid(uid);
# endif # endif
#endif #endif
@ -1994,11 +1990,11 @@ int httpd_main(int argc, char *argv[])
#ifdef CONFIG_FEATURE_HTTPD_CGI #ifdef CONFIG_FEATURE_HTTPD_CGI
{ {
char *p = getenv("PATH"); char *p = getenv("PATH");
if(p) { if (p) {
p = xstrdup(p); p = xstrdup(p);
} }
clearenv(); clearenv();
if(p) if (p)
setenv("PATH", p, 1); setenv("PATH", p, 1);
# ifdef CONFIG_FEATURE_HTTPD_WITHOUT_INETD # ifdef CONFIG_FEATURE_HTTPD_WITHOUT_INETD
addEnvPort("SERVER"); addEnvPort("SERVER");