httpd: reduce ifdef forest. comment out redundant PATH setting

This commit is contained in:
Denis Vlasenko 2006-11-16 18:04:43 +00:00
parent d4f3d1a4bd
commit b3a071543d
2 changed files with 76 additions and 86 deletions

View File

@ -1196,7 +1196,7 @@ USE_FEATURE_DATE_ISOFMT( \
"Options:\n" \ "Options:\n" \
"\t-c FILE\t\tSpecifies configuration file. (default httpd.conf)\n" \ "\t-c FILE\t\tSpecifies configuration file. (default httpd.conf)\n" \
"\t-p PORT\t\tServer port (default 80)\n" \ "\t-p PORT\t\tServer port (default 80)\n" \
"\t-i\t\tAssume that we are started frim inetd\n" \ "\t-i\t\tAssume that we are started from inetd\n" \
"\t-f\t\tDo not daemonize\n" \ "\t-f\t\tDo not daemonize\n" \
USE_FEATURE_HTTPD_SETUID("\t-u USER[:GRP]\tSet uid/gid after binding to port\n") \ USE_FEATURE_HTTPD_SETUID("\t-u USER[:GRP]\tSet uid/gid after binding to port\n") \
USE_FEATURE_HTTPD_BASIC_AUTH("\t-r REALM\tAuthentication Realm for Basic Authentication\n") \ USE_FEATURE_HTTPD_BASIC_AUTH("\t-r REALM\tAuthentication Realm for Basic Authentication\n") \

View File

@ -652,7 +652,7 @@ static char *encodeString(const char *string)
*p = 0; *p = 0;
return out; return out;
} }
#endif /* CONFIG_FEATURE_HTTPD_ENCODE_URL_STR */ #endif /* FEATURE_HTTPD_ENCODE_URL_STR */
/**************************************************************************** /****************************************************************************
* *
@ -1018,7 +1018,7 @@ static int sendCgi(const char *url,
*script = '/'; /* is directory, find next '/' */ *script = '/'; /* is directory, find next '/' */
} }
setenv1("PATH_INFO", script); /* set /PATH_INFO or "" */ setenv1("PATH_INFO", script); /* set /PATH_INFO or "" */
setenv1("PATH", getenv("PATH")); /* Huh?? */ /* setenv1("PATH", getenv("PATH")); redundant */
setenv1("REQUEST_METHOD", request); setenv1("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));
@ -1145,12 +1145,10 @@ static int sendCgi(const char *url,
if (nfound <= 0) { if (nfound <= 0) {
if (waitpid(pid, &status, WNOHANG) > 0) { if (waitpid(pid, &status, WNOHANG) > 0) {
close(inFd); close(inFd);
#if DEBUG if (DEBUG && WIFEXITED(status))
if (WIFEXITED(status))
bb_error_msg("piped has exited with status=%d", WEXITSTATUS(status)); bb_error_msg("piped has exited with status=%d", WEXITSTATUS(status));
if (WIFSIGNALED(status)) if (DEBUG && WIFSIGNALED(status))
bb_error_msg("piped has exited with signal=%d", WTERMSIG(status)); bb_error_msg("piped has exited with signal=%d", WTERMSIG(status));
#endif
break; break;
} }
} else if (post_readed_size > 0 && FD_ISSET(outFd, &writeSet)) { } else if (post_readed_size > 0 && FD_ISSET(outFd, &writeSet)) {
@ -1205,16 +1203,15 @@ static int sendCgi(const char *url,
if (full_write(s, rbuf, count) != count) if (full_write(s, rbuf, count) != count)
break; break;
#if DEBUG if (DEBUG)
fprintf(stderr, "cgi read %d bytes\n", count); fprintf(stderr, "cgi read %d bytes\n", count);
#endif
} }
} }
} }
} }
return 0; return 0;
} }
#endif /* CONFIG_FEATURE_HTTPD_CGI */ #endif /* FEATURE_HTTPD_CGI */
/**************************************************************************** /****************************************************************************
* *
@ -1256,12 +1253,11 @@ static int sendFile(const char *url)
} }
} }
} }
#endif /* CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES */ #endif /* FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES */
#if DEBUG if (DEBUG)
fprintf(stderr, "sending file '%s' content-type: %s\n", fprintf(stderr, "sending file '%s' content-type: %s\n",
url, config->found_mime_type); url, config->found_mime_type);
#endif
f = open(url, O_RDONLY); f = open(url, O_RDONLY);
if (f >= 0) { if (f >= 0) {
@ -1269,15 +1265,15 @@ static int sendFile(const char *url)
char *buf = config->buf; char *buf = config->buf;
sendHeaders(HTTP_OK); sendHeaders(HTTP_OK);
/* TODO: sendfile() */
while ((count = full_read(f, buf, MAX_MEMORY_BUFF)) > 0) { while ((count = full_read(f, buf, MAX_MEMORY_BUFF)) > 0) {
if (full_write(config->accepted_socket, buf, count) != count) if (full_write(config->accepted_socket, buf, count) != count)
break; break;
} }
close(f); close(f);
} else { } else {
#if DEBUG if (DEBUG)
bb_perror_msg("cannot open '%s'", url); bb_perror_msg("cannot open '%s'", url);
#endif
sendHeaders(HTTP_NOT_FOUND); sendHeaders(HTTP_NOT_FOUND);
} }
@ -1290,18 +1286,18 @@ static int checkPermIP(void)
/* This could stand some work */ /* This could stand some work */
for (cur = config->ip_a_d; cur; cur = cur->next) { for (cur = config->ip_a_d; cur; cur = cur->next) {
#if DEBUG if (DEBUG)
fprintf(stderr, "checkPermIP: '%s' ? ", config->rmt_ip_str); fprintf(stderr, "checkPermIP: '%s' ? ", config->rmt_ip_str);
fprintf(stderr, "'%u.%u.%u.%u/%u.%u.%u.%u'\n", if (DEBUG)
fprintf(stderr, "'%u.%u.%u.%u/%u.%u.%u.%u'\n",
(unsigned char)(cur->ip >> 24), (unsigned char)(cur->ip >> 24),
(unsigned char)(cur->ip >> 16), (unsigned char)(cur->ip >> 16),
(unsigned char)(cur->ip >> 8), (unsigned char)(cur->ip >> 8),
cur->ip & 0xff, cur->ip & 0xff,
(unsigned char)(cur->mask >> 24), (unsigned char)(cur->mask >> 24),
(unsigned char)(cur->mask >> 16), (unsigned char)(cur->mask >> 16),
(unsigned char)(cur->mask >> 8), (unsigned char)(cur->mask >> 8),
cur->mask & 0xff); cur->mask & 0xff);
#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 */
} }
@ -1338,67 +1334,64 @@ 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) {
size_t l;
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)
fprintf(stderr, "checkPerm: '%s' ? '%s'\n", p0, request); fprintf(stderr, "checkPerm: '%s' ? '%s'\n", p0, request);
#endif
{
size_t l = strlen(p0);
if (strncmp(p0, path, l) == 0 && l = strlen(p0);
(l == 1 || path[l] == '/' || path[l] == 0)) { if (strncmp(p0, path, l) == 0
char *u; && (l == 1 || path[l] == '/' || path[l] == '\0')
/* path match found. Check request */ ) {
/* for check next /path:user:password */ char *u;
prev = p0; /* path match found. Check request */
u = strchr(request, ':'); /* for check next /path:user:password */
if (u == NULL) { prev = p0;
/* bad request, ':' required */ u = strchr(request, ':');
break; if (u == NULL) {
} /* bad request, ':' required */
break;
}
#if ENABLE_FEATURE_HTTPD_AUTH_MD5 if (ENABLE_FEATURE_HTTPD_AUTH_MD5) {
{ 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, ':');
if (pp && pp[1] == '$' && pp[2] == '1' &&
pp[3] == '$' && pp[4]) {
pp++;
cipher = pw_encrypt(u+1, pp);
if (strcmp(cipher, pp) == 0)
goto set_remoteuser_var; /* Ok */
/* unauthorized */
continue;
}
} }
#endif pp = strchr(p, ':');
if (strcmp(p, request) == 0) { if (pp && pp[1] == '$' && pp[2] == '1' &&
#if ENABLE_FEATURE_HTTPD_AUTH_MD5 pp[3] == '$' && pp[4]) {
set_remoteuser_var: pp++;
#endif cipher = pw_encrypt(u+1, pp);
config->remoteuser = strdup(request); if (strcmp(cipher, pp) == 0)
if (config->remoteuser) goto set_remoteuser_var; /* Ok */
config->remoteuser[(u - request)] = 0; /* unauthorized */
return 1; /* Ok */ continue;
} }
/* unauthorized */
} }
if (strcmp(p, request) == 0) {
set_remoteuser_var:
config->remoteuser = strdup(request);
if (config->remoteuser)
config->remoteuser[(u - request)] = 0;
return 1; /* Ok */
}
/* unauthorized */
} }
} /* for */ } /* for */
return prev == NULL; return prev == NULL;
} }
#endif /* CONFIG_FEATURE_HTTPD_BASIC_AUTH */ #endif /* FEATURE_HTTPD_BASIC_AUTH */
/**************************************************************************** /****************************************************************************
* *
@ -1539,9 +1532,8 @@ BAD_REQUEST:
config->found_moved_temporarily = url; config->found_moved_temporarily = url;
} }
} }
#if DEBUG if (DEBUG)
fprintf(stderr, "url='%s', args=%s\n", url, config->query); fprintf(stderr, "url='%s', args=%s\n", url, config->query);
#endif
test = url; test = url;
ip_allowed = checkPermIP(); ip_allowed = checkPermIP();
@ -1563,9 +1555,8 @@ BAD_REQUEST:
if (count <= 0) if (count <= 0)
break; break;
#if DEBUG if (DEBUG)
fprintf(stderr, "Header: '%s'\n", buf); fprintf(stderr, "header: '%s'\n", buf);
#endif
#if ENABLE_FEATURE_HTTPD_CGI #if ENABLE_FEATURE_HTTPD_CGI
/* try and do our best to parse more lines */ /* try and do our best to parse more lines */
@ -1602,7 +1593,7 @@ BAD_REQUEST:
decodeBase64(test); decodeBase64(test);
credentials = checkPerm(url, test); credentials = checkPerm(url, test);
} }
#endif /* CONFIG_FEATURE_HTTPD_BASIC_AUTH */ #endif /* FEATURE_HTTPD_BASIC_AUTH */
} /* while extra header reading */ } /* while extra header reading */
} }
@ -1611,9 +1602,9 @@ BAD_REQUEST:
break; break;
if (strcmp(strrchr(url, '/') + 1, httpd_conf) == 0 || ip_allowed == 0) { if (strcmp(strrchr(url, '/') + 1, httpd_conf) == 0 || ip_allowed == 0) {
/* protect listing [/path]/httpd_conf or IP deny */ /* protect listing [/path]/httpd_conf or IP deny */
#if ENABLE_FEATURE_HTTPD_CGI #if ENABLE_FEATURE_HTTPD_CGI
FORBIDDEN: /* protect listing /cgi-bin */ FORBIDDEN: /* protect listing /cgi-bin */
#endif #endif
sendHeaders(HTTP_FORBIDDEN); sendHeaders(HTTP_FORBIDDEN);
break; break;
@ -1648,7 +1639,7 @@ FORBIDDEN: /* protect listing /cgi-bin */
if (prequest != request_GET) if (prequest != request_GET)
sendHeaders(HTTP_NOT_IMPLEMENTED); sendHeaders(HTTP_NOT_IMPLEMENTED);
else { else {
#endif /* CONFIG_FEATURE_HTTPD_CGI */ #endif /* 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) {
@ -1663,17 +1654,16 @@ FORBIDDEN: /* protect listing /cgi-bin */
#endif #endif
} while (0); } while (0);
# if DEBUG if (DEBUG)
fprintf(stderr, "closing socket\n\n"); fprintf(stderr, "closing socket\n\n");
# endif #if ENABLE_FEATURE_HTTPD_CGI
# if ENABLE_FEATURE_HTTPD_CGI
free(cookie); free(cookie);
free(content_type); free(content_type);
free(config->referer); config->referer = NULL; free(config->referer); config->referer = NULL;
# if ENABLE_FEATURE_HTTPD_BASIC_AUTH # if ENABLE_FEATURE_HTTPD_BASIC_AUTH
free(config->remoteuser); config->remoteuser = NULL; free(config->remoteuser); config->remoteuser = NULL;
# endif
# endif # endif
#endif
shutdown(config->accepted_socket, SHUT_WR); shutdown(config->accepted_socket, SHUT_WR);
/* Properly wait for remote to closed */ /* Properly wait for remote to closed */
@ -1742,7 +1732,7 @@ static int miniHttpd(int server)
bb_error_msg("connection from IP=%s, port %u", bb_error_msg("connection from IP=%s, port %u",
config->rmt_ip_str, config->port); config->rmt_ip_str, config->port);
#endif #endif
#endif /* CONFIG_FEATURE_HTTPD_CGI */ #endif /* FEATURE_HTTPD_CGI */
/* set the KEEPALIVE option to cull dead connections */ /* set the KEEPALIVE option to cull dead connections */
on = 1; on = 1;
@ -1779,7 +1769,7 @@ static int miniHttpd_inetd(void)
(unsigned char)(config->rmt_ip >> 24), (unsigned char)(config->rmt_ip >> 24),
(unsigned char)(config->rmt_ip >> 16), (unsigned char)(config->rmt_ip >> 16),
(unsigned char)(config->rmt_ip >> 8), (unsigned char)(config->rmt_ip >> 8),
config->rmt_ip & 0xff); config->rmt_ip & 0xff);
#endif #endif
config->port = ntohs(fromAddrLen.sin_port); config->port = ntohs(fromAddrLen.sin_port);
handleIncoming(); handleIncoming();