+
+static const char RFC1123FMT[] = "%a, %d %b %Y %H:%M:%S GMT";
+static const char Content_length[] = "Content-length:";
+
+
+static int
+scan_ip (const char **ep, unsigned int *ip, unsigned char endc)
+{
+ const char *p = *ep;
+ int auto_mask = 8;
+ int j;
+
+ *ip = 0;
+ for (j = 0; j < 4; j++) {
+ unsigned int octet;
+
+ if ((*p < '0' || *p > '9') && (*p != '/' || j == 0) && *p != 0)
+ return -auto_mask;
+ octet = 0;
+ while (*p >= '0' && *p <= '9') {
+ octet *= 10;
+ octet += *p - '0';
+ if (octet > 255)
+ return -auto_mask;
+ p++;
+ }
+ if (*p == '.')
+ p++;
+ if (*p != '/' && *p != 0)
+ auto_mask += 8;
+ *ip = ((*ip) << 8) | octet;
+ }
+ if (*p != 0) {
+ if (*p != endc)
+ return -auto_mask;
+ p++;
+ if(*p == 0)
+ return -auto_mask;
+ }
+ *ep = p;
+ return auto_mask;
+}
+
+static int
+scan_ip_mask (const char *ipm, unsigned int *ip, unsigned int *mask)
+{
+ int i;
+ unsigned int msk;
+
+ i = scan_ip(&ipm, ip, '/');
+ if(i < 0)
+ return i;
+ if(*ipm) {
+ const char *p = ipm;
+
+ i = 0;
+ while (*p) {
+ if (*p < '0' || *p > '9') {
+ if (*p == '.') {
+ i = scan_ip (&ipm, mask, 0);
+ return i != 32;
+ }
+ return -1;
+ }
+ i *= 10;
+ i += *p - '0';
+ p++;
+ }
+ }
+ if (i > 32 || i < 0)
+ return -1;
+ msk = 0x80000000;
+ *mask = 0;
+ while (i > 0) {
+ *mask |= msk;
+ msk >>= 1;
+ i--;
+ }
+ return 0;
+}
+
+#if defined(CONFIG_FEATURE_HTTPD_BASIC_AUTH) || defined(CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES)
+static void free_config_lines(Htaccess **pprev)
+{
+ Htaccess *prev = *pprev;
+
+ while( prev ) {
+ Htaccess *cur = prev;
+
+ prev = cur->next;
+ free(cur);
+ }
+ *pprev = NULL;
+}
+#endif
+
+/* flag */
+#define FIRST_PARSE 0
+#define SUBDIR_PARSE 1
+#define SIGNALED_PARSE 2
+#define FIND_FROM_HTTPD_ROOT 3
+/****************************************************************************
+ *
+ > $Function: parse_conf()
+ *
+ * $Description: parse configuration file into in-memory linked list.
+ *
+ * The first non-white character is examined to determine if the config line
+ * is one of the following:
+ * .ext:mime/type # new mime type not compiled into httpd
+ * [adAD]:from # ip address allow/deny, * for wildcard
+ * /path:user:pass # username/password
+ *
+ * Any previous IP rules are discarded.
+ * If the flag argument is not SUBDIR_PARSE then all /path and mime rules
+ * are also discarded. That is, previous settings are retained if flag is
+ * SUBDIR_PARSE.
+ *
+ * $Parameters:
+ * (const char *) path . . null for ip address checks, path for password
+ * checks.
+ * (int) flag . . . . . . the source of the parse request.
+ *
+ * $Return: (None)
+ *
+ ****************************************************************************/
+static void parse_conf(const char *path, int flag)
+{
+ FILE *f;
+#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
+ Htaccess *prev, *cur;
+#elif CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
+ Htaccess *cur;
+#endif
+
+ const char *cf = config->configFile;
+ char buf[160];
+ char *p0 = NULL;
+ char *c, *p;
+
+ /* free previous ip setup if present */
+ Htaccess_IP *pip = config->ip_a_d;
+
+ while( pip ) {
+ Htaccess_IP *cur_ipl = pip;
+
+ pip = cur_ipl->next;
+ free(cur_ipl);
+ }
+ config->ip_a_d = NULL;
+
+ config->flg_deny_all = 0;
+
+#if defined(CONFIG_FEATURE_HTTPD_BASIC_AUTH) || defined(CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES)
+ /* retain previous auth and mime config only for subdir parse */
+ if(flag != SUBDIR_PARSE) {
+#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
+ free_config_lines(&config->auth);
+#endif
+#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
+ free_config_lines(&config->mime_a);
+#endif
+ }
+#endif
+
+ if(flag == SUBDIR_PARSE || cf == NULL) {
+ cf = alloca(strlen(path) + sizeof(httpd_conf) + 2);
+ if(cf == NULL) {
+ if(flag == FIRST_PARSE)
+ bb_error_msg_and_die(bb_msg_memory_exhausted);
+ return;
+ }
+ sprintf((char *)cf, "%s/%s", path, httpd_conf);
+ }
+
+ while((f = fopen(cf, "r")) == NULL) {
+ if(flag == SUBDIR_PARSE || flag == FIND_FROM_HTTPD_ROOT) {
+ /* config file not found, no changes to config */
+ return;
+ }
+ if(config->configFile && flag == FIRST_PARSE) /* if -c option given */
+ bb_perror_msg_and_die("%s", cf);
+ flag = FIND_FROM_HTTPD_ROOT;
+ cf = httpd_conf;
+ }
+
+#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
+ prev = config->auth;
+#endif
+ /* This could stand some work */
+ while ( (p0 = fgets(buf, sizeof(buf), f)) != NULL) {
+ c = NULL;
+ for(p = p0; *p0 != 0 && *p0 != '#'; p0++) {
+ if(!isspace(*p0)) {
+ *p++ = *p0;
+ if(*p0 == ':' && c == NULL)
+ c = p;
+ }
+ }
+ *p = 0;
+
+ /* test for empty or strange line */
+ if (c == NULL || *c == 0)
+ continue;
+ p0 = buf;
+ if(*p0 == 'd')
+ *p0 = 'D';
+ if(*c == '*') {
+ if(*p0 == 'D') {
+ /* memorize deny all */
+ config->flg_deny_all++;
+ }
+ /* skip default other "word:*" config lines */
+ continue;
+ }
+
+ if(*p0 == 'a')
+ *p0 = 'A';
+ else if(*p0 != 'D' && *p0 != 'A'
+#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
+ && *p0 != '/'
+#endif
+#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
+ && *p0 != '.'
+#endif
+ )
+ continue;
+ if(*p0 == 'A' || *p0 == 'D') {
+ /* storing current config IP line */
+ pip = calloc(1, sizeof(Htaccess_IP));
+ if(pip) {
+ if(scan_ip_mask (c, &(pip->ip), &(pip->mask))) {
+ /* syntax IP{/mask} error detected, protect all */
+ *p0 = 'D';
+ pip->mask = 0;
+ }
+ pip->allow_deny = *p0;
+ if(*p0 == 'D') {
+ /* Deny:form_IP move top */
+ pip->next = config->ip_a_d;
+ config->ip_a_d = pip;
+ } else {
+ /* add to bottom A:form_IP config line */
+ Htaccess_IP *prev_IP = config->ip_a_d;
+
+ if(prev_IP == NULL) {
+ config->ip_a_d = pip;
+ } else {
+ while(prev_IP->next)
+ prev_IP = prev_IP->next;
+ prev_IP->next = pip;
+ }
+ }
+ }
+ continue;
+ }
+#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
+ if(*p0 == '/') {
+ /* make full path from httpd root / curent_path / config_line_path */
+ cf = flag == SUBDIR_PARSE ? path : "";
+ p0 = malloc(strlen(cf) + (c - buf) + 2 + strlen(c));
+ if(p0 == NULL)
+ continue;
+ c[-1] = 0;
+ sprintf(p0, "/%s%s", cf, buf);
+
+ /* another call bb_simplify_path */
+ cf = p = p0;
+
+ do {
+ if (*p == '/') {
+ if (*cf == '/') { /* skip duplicate (or initial) slash */
+ continue;
+ } else if (*cf == '.') {
+ if (cf[1] == '/' || cf[1] == 0) { /* remove extra '.' */
+ continue;
+ } else if ((cf[1] == '.') && (cf[2] == '/' || cf[2] == 0)) {
+ ++cf;
+ if (p > p0) {
+ while (*--p != '/'); /* omit previous dir */
+ }
+ continue;
+ }
+ }
+ }
+ *++p = *cf;
+ } while (*++cf);
+
+ if ((p == p0) || (*p != '/')) { /* not a trailing slash */
+ ++p; /* so keep last character */
+ }
+ *p = 0;
+ sprintf(p0, "%s:%s", p0, c);
+ }
+#endif
+
+#if defined(CONFIG_FEATURE_HTTPD_BASIC_AUTH) || defined(CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES)
+ /* storing current config line */
+ cur = calloc(1, sizeof(Htaccess) + strlen(p0));
+ if(cur) {
+ cf = strcpy(cur->before_colon, p0);
+ c = strchr(cf, ':');
+ *c++ = 0;
+ cur->after_colon = c;
+#ifdef CONFIG_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES
+ if(*cf == '.') {
+ /* config .mime line move top for overwrite previous */
+ cur->next = config->mime_a;
+ config->mime_a = cur;
+ continue;
+ }
+#endif
+#ifdef CONFIG_FEATURE_HTTPD_BASIC_AUTH
+ free(p0);
+ if(prev == NULL) {
+ /* first line */
+ config->auth = prev = cur;
+ } else {
+ /* sort path, if current lenght eq or bigger then move up */
+ Htaccess *prev_hti = config->auth;
+ int l = strlen(cf);
+ Htaccess *hti;
+
+ for(hti = prev_hti; hti; hti = hti->next) {
+ if(l >= strlen(hti->before_colon)) {
+ /* insert before hti */
+ cur->next = hti;
+ if(prev_hti != hti) {
+ prev_hti->next = cur;
+ } else {
+ /* insert as top */
+ config->auth = cur;
+ }
+ break;
+ }
+ if(prev_hti != hti)
+ prev_hti = prev_hti->next;
+ }
+ if(!hti) { /* not inserted, add to bottom */
+ prev->next = cur;
+ prev = cur;
+ }
+ }
+#endif
+ }
+#endif
+ }
+ fclose(f);
+}
+
+#ifdef CONFIG_FEATURE_HTTPD_ENCODE_URL_STR