1 Index: busybox-1.7.2/editors/awk.c
2 ===================================================================
3 --- busybox-1.7.2.orig/editors/awk.c 2007-10-30 15:35:03.000000000 -0500
4 +++ busybox-1.7.2/editors/awk.c 2007-10-30 15:35:06.000000000 -0500
6 /* these flags are static, don't change them when value is changed */
7 #define VF_DONTTOUCH (VF_ARRAY | VF_SPECIAL | VF_WALK | VF_CHILD | VF_DIRTY)
10 +#define fputs(s, stream) fputs_hook(s, stream)
11 +static inline int fputs_hook (__const char *__restrict __s, FILE *__restrict __stream);
15 typedef struct var_s {
16 unsigned type; /* flags */
21 +typedef var *(*awk_cfunc)(var *res, var *args, int nargs);
22 typedef struct func_s {
24 - struct chain_s body;
25 + enum { AWKFUNC, CFUNC } type;
28 + struct chain_s body;
34 next_token(TC_FUNCTION);
36 f = newfunc(t_string);
37 - f->body.first = NULL;
39 + f->x.body.first = NULL;
41 while (next_token(TC_VARIABLE | TC_SEQTERM) & TC_VARIABLE) {
42 v = findvar(ahash, t_string);
44 if (next_token(TC_COMMA | TC_SEQTERM) & TC_SEQTERM)
56 - if (!op->r.f->body.first)
57 + if ((op->r.f->type == AWKFUNC) &&
58 + !op->r.f->x.body.first)
59 syntax_error(EMSG_UNDEF_FUNC);
61 X.v = R.v = nvalloc(op->r.f->nargs+1);
62 @@ -2389,7 +2401,10 @@
66 - res = evaluate(op->r.f->body.first, res);
67 + if (op->r.f->type == AWKFUNC)
68 + res = evaluate(op->r.f->x.body.first, res);
69 + else if (op->r.f->type == CFUNC)
70 + res = op->r.f->x.cfunc(res, fnargs, op->r.f->nargs);
74 @@ -2753,6 +2768,13 @@
77 int awk_main(int argc, char **argv);
78 +int awx_main(int argc, char **argv);
81 +static int is_awx = 0;
85 int awk_main(int argc, char **argv)
88 @@ -2817,6 +2839,11 @@
97 opt_complementary = "v::f::";
98 opt = getopt32(argv, "F:v:f:W:", &opt_F, &opt_v, &opt_f, &opt_W);
100 Index: busybox-1.7.2/editors/awx.c
101 ===================================================================
102 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
103 +++ busybox-1.7.2/editors/awx.c 2007-10-30 15:35:06.000000000 -0500
106 + * awk web extension
108 + * Copyright (C) 2007 by Felix Fietkau <nbd@openwrt.org>
110 + * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
115 +#include "awx_parser.h"
117 +#define LINE_BUF 2048
118 +#define HASH_MAX 1536
119 +#define TR_START "@TR<<"
125 +static xhash *lstr = NULL;
126 +static xhash *formvar = NULL;
127 +static int lang_inuse = 0;
129 +/* look up a translation symbol from the hash */
130 +static inline const char *translate_lookup(char *str)
132 + char *name, *def, *p;
137 + if (((p = strchr(str, '|')) != NULL)
138 + || ((p = strchr(str, '#')) != NULL)) {
143 + hi = hash_search(lstr, name);
149 + return getvar_s(v);
152 +/* look for translation markers in the line and return the translated string */
153 +static char *translate_line(char *line)
155 + const char *tok[MAX_TR * 3];
156 + char *l, *p, *p2 = NULL, *res;
157 + int len = 0, _pos = 0, i, tr_abort = 0;
158 + static char *backlog = NULL;
160 + if (backlog && line) {
161 + backlog = xrealloc(backlog, strlen(backlog) + strlen(line) + 1);
162 + sprintf(backlog + strlen(backlog), line);
168 + while (l != NULL) {
169 + if ((p = strstr(l, TR_START)) == NULL) {
170 + len += strlen((tok[_pos++] = l));
174 + p2 = strstr(p, TR_END);
182 + len += strlen((tok[_pos++] = l));
184 + len += strlen((tok[_pos++] = translate_lookup(p + strlen(TR_START))));
187 + l += strlen(TR_END);
191 + p = xmalloc(len + 1);
194 + for (i = 0; i < _pos; i++) {
196 + p += strlen(tok[i]);
202 + if (tr_abort && p2)
208 +/* hook for intercepting awk's use of puts. used for running all printed strings
209 + * through the translation system */
210 +static inline int fputs_hook (__const char *__restrict __s, FILE *__restrict __stream)
212 + if (lang_inuse && (__stream == stdout)) {
216 + str = translate_line((char *) __s);
217 + ret = fputs(str, __stream);
223 + return fputs(__s, __stream);
226 +static var *init_lang(var *res, var *args, int nargs)
229 + lstr = hash_init();
236 +/* load and parse language file */
237 +static void load_lang_file(char *file)
240 + char *b, *name, *value;
241 + char buf1[LINE_BUF];
243 + if ((f = fopen(file, "r")) == NULL)
246 + while (!feof(f) && (fgets(buf1, LINE_BUF - 1, f) != NULL)) {
249 + continue; /* skip comments */
251 + while (isspace(*b))
252 + b++; /* skip leading spaces */
257 + if ((b = strstr(name, "=>")) == NULL)
258 + continue; /* separator not found */
265 + for (b--; isspace(*b); b--)
266 + *b = 0; /* remove trailing spaces */
268 + while (isspace(*value))
269 + value++; /* skip leading spaces */
271 + for (b = value + strlen(value) - 1; isspace(*b); b--)
272 + *b = 0; /* remove trailing spaces */
277 + setvar_s(findvar(lstr,name), value);
283 +static var *load_lang(var *res, var *args, int nargs)
285 + const char *langfmt = "/usr/lib/webif/lang/%s.txt";
286 + char lbuf[LINE_BUF];
290 + init_lang(res, args, nargs);
292 + lang = getvar_s(args);
293 + if (!lang || !strcmp(lang, ""))
296 + sprintf(lbuf, langfmt, lang);
297 + load_lang_file(lbuf);
302 +/* read the contents of an entire file */
303 +static char *get_file(const char *fname)
309 + F = fopen(fname, "r");
314 + if (fseek(F, 0, SEEK_END) == 0) {
316 + s = (char *)xmalloc(flen+4);
317 + fseek(F, 0, SEEK_SET);
318 + i = 1 + fread(s+1, 1, flen, F);
320 + for (i=j=1; j>0; i+=j) {
321 + s = (char *)xrealloc(s, i+4096);
322 + j = fread(s+i, 1, 4094, F);
334 + * taken from parse_program from awk.c
335 + * END{} is not parsed here, and BEGIN{} is executed immediately
337 +static void parse_include(char *p)
340 + chain *initseq = NULL;
346 + memset(&tmp, 0, sizeof(tmp));
349 + while ((tclass = next_token(TC_EOF | TC_OPSEQ |
350 + TC_OPTERM | TC_BEGIN | TC_FUNCDECL)) != TC_EOF) {
351 + if (tclass & TC_OPTERM)
355 + if (tclass & TC_BEGIN) {
356 + initseq = xzalloc(sizeof(chain));
359 + } else if (tclass & TC_FUNCDECL) {
360 + next_token(TC_FUNCTION);
362 + f = newfunc(t_string);
364 + f->x.body.first = NULL;
366 + while (next_token(TC_VARIABLE | TC_SEQTERM) & TC_VARIABLE) {
367 + v = findvar(ahash, t_string);
368 + v->x.aidx = (f->nargs)++;
370 + if (next_token(TC_COMMA | TC_SEQTERM) & TC_SEQTERM)
373 + seq = &(f->x.body);
375 + clear_array(ahash);
378 + if (initseq && initseq->first)
379 + tv = evaluate(initseq->first, tv);
384 +/* include an awk file and run its BEGIN{} section */
385 +static xhash *includes = NULL;
386 +static void include_file(const char *filename)
390 + int oldlnr = g_lineno;
391 + const char *oldprg = g_progname;
394 + includes = hash_init();
396 + /* find out if the file has been included already */
397 + v = findvar(includes, filename);
402 + /* read include file */
403 + s = get_file(filename);
405 + fprintf(stderr, "Could not open file.\n");
409 + g_progname = xstrdup(filename);
410 + parse_include(s+1);
413 + g_progname = oldprg;
416 +static var *include(var *res, var *args, int nargs)
420 + s = getvar_s(args);
421 + if (s && (strlen(s) > 0))
427 +/* parse an awk expression */
428 +static var *parse_awk(char *str, var *tv)
433 + memset(&body, 0, sizeof(body));
437 + /* end of expression, assume that there's going to be a free byte
438 + * at the end of the string that can be used for the ')' */
439 + strcat(str + strlen(str), "}");
440 + n = parse_expr(TC_GRPTERM);
444 + return evaluate(n, tv);
447 +static inline void print_translate(char *s)
451 + str = translate_line(s);
452 + fputs(str, stdout);
458 +static void render_element(struct template_cb *tcb, struct template_element *e)
466 + g_lineno = e->line;
469 + s = malloc(strlen(e->var) + 2);
471 + print_translate(s);
475 + s = malloc(strlen(e->var) + 2);
478 + s2 = strdup(getvar_s(parse_awk(s, v)));
480 + print_translate(s2);
485 + s = malloc(strlen(e->var) + 2);
488 + i = istrue(parse_awk(s, v));
493 + execute_template(tcb, e->sub);
495 + execute_template(tcb, e->sub2);
498 + v = newvar(e->var);
499 + hashwalk_init(v, iamarray(findvar(vhash, e->in)));
500 + while (hashwalk_next(v)) {
501 + execute_template(tcb, e->sub);
511 +/* awk method render(), which opens a template file and processes all awk ssi calls */
512 +static void render_file(const char *filename)
514 + struct template_cb tcb;
515 + struct template_element *e;
517 + const char *oldprg = g_progname;
518 + int oldlnr = g_lineno;
523 + f = fopen(filename, "r");
527 + g_progname = xstrdup(filename);
529 + memset(&tcb, 0, sizeof(tcb));
530 + tcb.handle_element = render_element;
531 + e = parse_template(&tcb, f);
532 + execute_template(&tcb, e);
533 + free_template(&tcb, e);
535 + g_progname = oldprg;
539 +static var *render(var *res, var *args, int nargs)
543 + s = getvar_s(args);
552 +/* Call render, but only if this function hasn't been called already */
553 +static int layout_rendered = 0;
554 +static var *render_layout(var *res, var *args, int nargs)
556 + if (layout_rendered)
558 + layout_rendered = 1;
559 + return render(res, args, nargs);
562 +/* registers a global c function for the awk interpreter */
563 +static void register_cfunc(const char *name, awk_cfunc cfunc, int nargs)
569 + f->x.cfunc = cfunc;
573 +static void putvar(vartype type, char *name, char *value)
575 + if (type != FORM_VAR)
578 + setvar_u(findvar(formvar, name), value);
581 +static const char *cgi_getvar(const char *name)
584 + formvar = hash_init();
588 + if (!formvar || !name)
591 + return getvar_s(findvar(formvar, name));
594 +/* function call for accessing cgi form variables */
595 +static var *getvar(var *res, var *args, int nargs)
597 + const char *s, *svar;
599 + s = getvar_s(args);
603 + svar = cgi_getvar(s);
607 + setvar_u(res, svar);
612 +/* call an awk function without arguments by string reference */
613 +static var *call(var *res, var *args, int nargs)
615 + const char *s = getvar_s(args);
622 + if (f && f->type == AWKFUNC && f->x.body.first)
623 + return evaluate(f->x.body.first, res);
630 +static int run_awxscript(char *name)
632 + var tv, *layout, *action;
633 + char *tmp, *s = NULL;
638 + /* read the main controller source */
639 + s = get_file(g_progname);
641 + fprintf(stderr, "Could not open file\n");
644 + parse_program(s+1);
648 + /* set some defaults for ACTION and LAYOUT, which will have special meaning */
649 + layout = newvar("LAYOUT");
650 + setvar_s(layout, "views/layout.ahtml");
652 + /* run the BEGIN {} block */
653 + evaluate(beginseq.first, &tv);
655 + action = newvar("ACTION");
656 + if (!(strlen(getvar_s(action)) > 0)) {
657 + tmp = (char *) cgi_getvar("action");
658 + if (!tmp || (strlen(tmp) <= 0))
659 + tmp = strdup("default");
661 + setvar_p(action, tmp);
664 + /* call the action (precedence: begin block override > cgi parameter > "default") */
665 + tmp = xmalloc(strlen(getvar_s(action)) + 7);
666 + sprintf(tmp, "handle_%s", getvar_s(action));
667 + setvar_s(action, tmp);
668 + call(&tv, action, 1);
671 + /* render the selected layout, will do nothing if render_layout has been called from awk */
672 + render_layout(&tv, layout, 1);
678 +/* main awx processing function. called from awk_main() */
679 +static int do_awx(int argc, char **argv)
684 + char **args = argv;
688 + /* register awk C callbacks */
689 + register_cfunc("getvar", getvar, 1);
690 + register_cfunc("render", render, 1);
691 + register_cfunc("render_layout", render_layout, 1);
692 + register_cfunc("call", call, 1);
693 + register_cfunc("include", include, 1);
694 + register_cfunc("init_lang", init_lang, 1);
695 + register_cfunc("load_lang", load_lang, 1);
700 + /* fill in ARGV array */
701 + setvar_i(intvar[ARGC], argc + 1);
702 + setari_u(intvar[ARGV], 0, "awx");
705 + setari_u(intvar[ARGV], ++i, *args++);
707 + while((c = getopt(argc, argv, "i:f:")) != EOF) {
710 + g_progname = optarg;
711 + include_file(optarg);
715 + g_progname = optarg;
716 + render_file(optarg);
724 + fprintf(stderr, "Invalid argument.\n");
728 + ret = run_awxscript(*argv);
734 +/* entry point for awx applet */
735 +int awx_main(int argc, char **argv)
738 + return awk_main(argc, argv);
741 Index: busybox-1.7.2/editors/awx_parser.h
742 ===================================================================
743 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
744 +++ busybox-1.7.2/editors/awx_parser.h 2007-10-30 15:35:06.000000000 -0500
746 +#ifndef __TEMPLATE_PARSER_H
747 +#define __TEMPLATE_PARSER_H
756 +struct template_element;
759 +struct template_cb {
760 + void *(*prepare_code)(struct template_element *);
761 + void (*handle_element)(struct template_cb *, struct template_element *);
762 + void (*free_code)(struct template_element *);
765 +struct template_element {
771 + struct template_element *parent;
772 + struct template_element *sub;
773 + struct template_element *sub2;
774 + struct template_element *prev;
775 + struct template_element *next;
779 +struct template_element *parse_template(struct template_cb *cb, FILE *in);
780 +void execute_template(struct template_cb *cb, struct template_element *e);
781 +void free_template(struct template_cb *cb, struct template_element *e);
784 Index: busybox-1.7.2/editors/awx_parser.l
785 ===================================================================
786 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
787 +++ busybox-1.7.2/editors/awx_parser.l 2007-10-30 15:35:06.000000000 -0500
793 +#include "busybox.h"
794 +#include "awx_parser.h"
811 +char *statestr[] = {
812 + [S_INIT] = "S_INIT",
813 + [S_TEXT] = "S_TEXT",
814 + [S_CODE] = "S_CODE",
815 + [S_IF_START] = "S_IF_START",
816 + [S_FOR_START] = "S_FOR_START",
817 + [S_FOR_IN] = "S_FOR_IN",
822 + [T_TEXT] = "T_TEXT",
825 + [T_CODE] = "T_CODE"
829 +static struct template_cb *parse_cb;
830 +static struct template_element *cur, *head;
831 +static char *textbuf;
832 +static unsigned int buflen;
833 +static unsigned int buf_offset;
834 +static int _lnr = 0;
836 +static void buf_realloc(void)
839 + textbuf = xrealloc(textbuf, buflen);
842 +static void parse_error(char *str)
844 + fprintf(stderr, "Parse error%s%s\n", (str ? ": " : "!"), (str ?: ""));
849 +static struct template_element *new_template_element(struct template_element *parent)
851 + struct template_element *ptr;
853 + ptr = xzalloc(sizeof(struct template_element));
854 + ptr->parent = parent;
858 +static inline void next_template_element(void)
860 + cur->next = new_template_element(cur->parent);
861 + cur->next->prev = cur;
865 +static void addtext(char *text)
867 + while(buf_offset + strlen(text) + 1 > buflen)
870 + buf_offset += sprintf(&textbuf[buf_offset], "%s", text);
873 +static void set_state(int newstate)
878 + static int _rec = 0;
879 + fprintf(stderr, "DEBUG(%d): %s => %s: %s\n", _rec, statestr[state], statestr[newstate], textbuf);
881 + ptr = xstrdup(textbuf);
882 + if (state == S_FOR_IN)
887 + if (parse_cb && (cur->t == T_CODE) && parse_cb->prepare_code)
888 + parse_cb->prepare_code(cur);
901 + if (ptr || !cur->prev)
902 + next_template_element();
906 + if (ptr || !cur->prev)
907 + next_template_element();
913 + parse_error("'@else' without parent element");
914 + cur->sub2 = new_template_element(cur);
924 + parse_error("'@end' without parent element");
926 + next_template_element();
933 + next_template_element();
940 + cur->sub = new_template_element(cur);
949 + if (ptr || !cur->prev)
950 + next_template_element();
963 +"<%"[ \n\t]*"@if"[ \n\t]+ {
964 + if (state == S_TEXT)
965 + set_state(S_IF_START);
970 +"<%"[ \n\t]*"@for"[ \n\t]+ {
971 + if (state == S_TEXT)
972 + set_state(S_FOR_START);
977 +[ \n\t]+"in"[ \n\t]+ {
978 + if (state == S_FOR_START)
979 + set_state(S_FOR_IN);
984 +"<%"[ \n\t]*"@end"[ \n\t]*%> {
985 + if (state != S_TEXT)
990 +"<%"[ \n\t]*"@else"[ \n\t]*%> {
991 + if (state != S_TEXT)
997 + if (state != S_TEXT)
998 + parse_error("'<%' cannot be nested");
1003 + if (state == S_TEXT)
1005 + set_state(S_TEXT);
1010 + if (state == S_TEXT)
1021 +void execute_template(struct template_cb *cb, struct template_element *e)
1023 + static int rec = 0;
1027 + fprintf(stderr, "DEBUG: execute(%d)\t%s\n", rec, typestr[e->t]);
1030 + if (cb->handle_element)
1031 + cb->handle_element(cb, e);
1043 +struct template_element *parse_template(struct template_cb *cb, FILE *in)
1051 + textbuf = xzalloc(buflen);
1053 + head = xzalloc(sizeof(struct template_element));
1063 +void free_template(struct template_cb *cb, struct template_element *e)
1065 + struct template_element *next;
1072 + if (cb->free_code)
1077 + free_template(cb, e->sub);
1089 + return free_template(cb, next);
1091 Index: busybox-1.7.2/editors/Config.in
1092 ===================================================================
1093 --- busybox-1.7.2.orig/editors/Config.in 2007-10-30 15:34:59.000000000 -0500
1094 +++ busybox-1.7.2/editors/Config.in 2007-10-30 15:35:06.000000000 -0500
1096 Awk is used as a pattern scanning and processing language. This is
1097 the BusyBox implementation of that programming language.
1100 + bool "Enable awx (awk web extension)"
1104 + awx - awk web extension
1106 config FEATURE_AWK_MATH
1107 bool "Enable math functions (requires libm)"
1109 Index: busybox-1.7.2/editors/Kbuild
1110 ===================================================================
1111 --- busybox-1.7.2.orig/editors/Kbuild 2007-10-30 15:34:59.000000000 -0500
1112 +++ busybox-1.7.2/editors/Kbuild 2007-10-30 15:35:06.000000000 -0500
1114 lib-$(CONFIG_PATCH) += patch.o
1115 lib-$(CONFIG_SED) += sed.o
1116 lib-$(CONFIG_VI) += vi.o
1117 +lib-$(CONFIG_AWX) += awx_parser.o
1119 +editors/awx_parser.c: editors/awx_parser.l editors/awx_parser.h
1123 +editors/awx_parser.o: editors/awx_parser.c FORCE
1124 + $(call cmd,force_checksrc)
1125 + $(call if_changed_rule,cc_o_c)
1126 Index: busybox-1.7.2/include/applets.h
1127 ===================================================================
1128 --- busybox-1.7.2.orig/include/applets.h 2007-10-30 15:35:05.000000000 -0500
1129 +++ busybox-1.7.2/include/applets.h 2007-10-30 15:35:06.000000000 -0500
1131 USE_ARPING(APPLET(arping, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
1132 USE_ASH(APPLET_NOUSAGE(ash, ash, _BB_DIR_BIN, _BB_SUID_NEVER))
1133 USE_AWK(APPLET_NOEXEC(awk, awk, _BB_DIR_USR_BIN, _BB_SUID_NEVER, awk))
1134 +USE_AWX(APPLET_NOUSAGE(awx, awx, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
1135 USE_BASENAME(APPLET_NOFORK(basename, basename, _BB_DIR_USR_BIN, _BB_SUID_NEVER, basename))
1136 USE_BBCONFIG(APPLET(bbconfig, _BB_DIR_BIN, _BB_SUID_NEVER))
1137 //USE_BBSH(APPLET(bbsh, _BB_DIR_BIN, _BB_SUID_NEVER))
1138 Index: busybox-1.7.2/include/cgi.h
1139 ===================================================================
1140 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
1141 +++ busybox-1.7.2/include/cgi.h 2007-10-30 15:35:06.000000000 -0500
1146 +typedef enum { FORM_VAR, COOKIE_VAR } vartype;
1147 +typedef void (*var_handler) (vartype, char *, char *);
1148 +int cgi_init(var_handler);
1151 Index: busybox-1.7.2/libbb/cgi.c
1152 ===================================================================
1153 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
1154 +++ busybox-1.7.2/libbb/cgi.c 2007-10-30 15:35:06.000000000 -0500
1156 +/* --------------------------------------------------------------------------
1157 + * functions for processing cgi form data
1158 + * Copyright (C) 2007 by Felix Fietkau <nbd@openwrt.org>
1160 + * parts taken from the core of haserl.cgi - a poor-man's php for embedded/lightweight environments
1161 + * $Id: haserl.c,v 1.13 2004/11/10 17:59:35 nangel Exp $
1162 + * Copyright (c) 2003,2004 Nathan Angelacos (nangel@users.sourceforge.net)
1164 + * This program is free software; you can redistribute it and/or modify
1165 + * it under the terms of the GNU General Public License as published by
1166 + * the Free Software Foundation; either version 2 of the License, or
1167 + * (at your option) any later version.
1169 + * This program is distributed in the hope that it will be useful,
1170 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1171 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1172 + * General Public License for more details.
1174 + * You should have received a copy of the GNU General Public License
1175 + * along with this program; if not, write to the Free Software
1176 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1179 + * The x2c() and unescape_url() routines were taken from
1180 + * http://www.jmarshall.com/easy/cgi/getcgi.c.txt
1182 + * The comments in that text file state:
1184 + *** Written in 1996 by James Marshall, james@jmarshall.com, except
1185 + *** that the x2c() and unescape_url() routines were lifted directly
1186 + *** from NCSA's sample program util.c, packaged with their HTTPD.
1187 + *** For the latest, see http://www.jmarshall.com/easy/cgi/
1190 + ------------------------------------------------------------------------- */
1193 +#include <unistd.h>
1195 +#include <sys/mman.h>
1196 +#include <sys/types.h>
1197 +#include <sys/wait.h>
1198 +#include <sys/stat.h>
1199 +#include <sys/fcntl.h>
1200 +#include <stdlib.h>
1201 +#include <string.h>
1204 +#ifndef MAX_UPLOAD_KB
1205 +#define MAX_UPLOAD_KB 2048
1207 +#define TEMPDIR "/tmp"
1209 +static int global_upload_size = 0;
1210 +static int ReadMimeEncodedInput(char *qs);
1211 +static var_handler do_putvar = NULL;
1214 + * Convert 2 char hex string into char it represents
1215 + * (from http://www.jmarshall.com/easy/cgi)
1217 +static char x2c (char *what) {
1220 + digit=(what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
1222 + digit+=(what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
1228 + * unsescape %xx to the characters they represent
1231 +static void unescape_url (char *url) {
1234 + for (i=0, j=0; url[j]; ++i, ++j) {
1235 + if ((url[i] = url[j]) == '%') {
1236 + url[i] = x2c(&url[j+1]);
1243 +static inline void put_var(vartype type, char *var)
1250 + val = strchr(var, '=');
1256 + do_putvar(type, var, val);
1263 + * if HTTP_COOKIE is passed as an environment variable,
1264 + * attempt to parse its values into environment variables
1266 +static void CookieVars (void)
1271 + if (getenv("HTTP_COOKIE") != NULL)
1272 + qs=strdup(getenv("HTTP_COOKIE"));
1276 + /** split on; to extract name value pairs */
1277 + token=strtok(qs, ";");
1279 + // skip leading spaces
1280 + while ( token[0] == ' ' )
1283 + put_var(COOKIE_VAR, token);
1285 + token = strtok(NULL, ";");
1291 + * Read cgi variables from query string, and put in environment
1293 +static int ReadCGIQueryString (void)
1299 + if (getenv("QUERY_STRING") != NULL)
1300 + qs=strdup(getenv("QUERY_STRING"));
1304 + /* change plusses into spaces */
1305 + for (i=0; qs[i]; i++ ) { if (qs[i] == '+' ) { qs[i] = ' ';} };
1307 + /** split on & and ; to extract name value pairs */
1309 + token=strtok(qs, "&;");
1311 + unescape_url(token);
1312 + put_var(FORM_VAR, token);
1313 + token=strtok(NULL, "&;");
1322 + * Read cgi variables from stdin (for POST queries)
1323 + * (oh... and if its mime-encoded file upload, we save the
1324 + * file to /tmp; and return the name of the tmp file
1325 + * the cgi script is responsible for disposing of the tmp file
1328 +static int ReadCGIPOSTValues (void) {
1330 + int content_length;
1335 + if (getenv("CONTENT_LENGTH") == NULL)
1338 + content_length = atoi(getenv("CONTENT_LENGTH"));
1340 + /* protect ourselves from 20GB file uploads */
1341 + if (content_length > MAX_UPLOAD_KB * 1024 ) {
1342 + /* But we need to finish reading the content */
1343 + while ( fread( &i, sizeof(int), 1, stdin) == 1 );
1347 + if (!(qs=malloc(content_length+1)))
1350 + /* set the buffer to null, so that a browser messing with less
1351 + data than content_length won't buffer underrun us */
1352 + memset(qs, 0 ,content_length+1);
1354 + if ((!fread(qs,content_length,1,stdin) &&
1355 + (content_length > 0)
1356 + && !feof(stdin))) {
1362 + if (getenv("CONTENT_TYPE") && (strncasecmp(getenv("CONTENT_TYPE"), "multipart/form-data", 19) == 0)) {
1363 + /* This is a mime request, we need to go to the mime handler */
1364 + i=ReadMimeEncodedInput(qs);
1370 + /* change plusses into spaces */
1371 + for (i=0; qs[i]; i++ ) { if (qs[i] == '+' ) { qs[i] = ' ';} };
1373 + /** split on & and ; to extract name value pairs */
1374 + token=strtok(qs, "&;");
1376 + unescape_url(token);
1377 + put_var(FORM_VAR, token);
1378 + token=strtok(NULL, "&;");
1387 + * LineToStr - Scans char and replaces the first "\n" with a "\0";
1388 + * If it finds a "\r", it does that to; (fix DOS brokennes) returns
1389 + * the length of the string;
1391 +static int LineToStr (char *string, size_t max) {
1394 + while ((offset < max) && (string[offset] != '\n') && (string[offset] != '\r'))
1397 + if (string[offset] == '\r') {
1398 + string[offset]='\0';
1401 + if (string[offset] == '\n') {
1402 + string[offset]='\0';
1411 + * ReadMimeEncodedInput - handles things that are mime encoded
1412 + * takes a pointer to the input; returns 0 on success
1415 +static int ReadMimeEncodedInput(char *qs)
1427 + char tmpname[] = TEMPDIR "/XXXXXX";
1429 + /* we should only get here if the content type was set. Segfaults happen
1430 + if Content_Type is null */
1432 + if (getenv("CONTENT_LENGTH") == NULL)
1433 + /* No content length?! */
1436 + cl=atoi(getenv("CONTENT_LENGTH"));
1438 + /* we do this 'cause we can't mess with the real env. variable - it would
1439 + * overwrite the environment - I tried.
1441 + i=strlen(getenv("CONTENT_TYPE"))+1;
1444 + memcpy(ct, getenv("CONTENT_TYPE"), i);
1450 + while (i < strlen(ct) && (strncmp("boundary=", &ct[i], 9) != 0))
1453 + if (i == strlen(ct)) {
1454 + /* no boundary informaiton found */
1458 + boundary=&ct[i+7];
1459 + /* add two leading -- to the boundary */
1463 + /* begin the big loop. Look for:
1465 + Content-Disposition: form-data; name="......."
1470 + Content-Disposition: form-data; name="....." filename="....."
1478 + while (offset < cl) {
1479 + /* first look for boundary */
1480 + while ((offset < cl) && (memcmp(&qs[offset], boundary, strlen(boundary))))
1483 + /* if we got here and we ran off the end, its an error */
1484 + if (offset >= cl) {
1489 + /* if the two characters following the boundary are --, */
1490 + /* then we are at the end, exit */
1491 + if (memcmp(&qs[offset+strlen(boundary)], "--", 2) == 0) {
1495 + /* find where the offset should be */
1496 + line=LineToStr(&qs[offset], cl-offset);
1499 + /* Now we're going to look for content-disposition */
1500 + line=LineToStr(&qs[offset], cl-offset);
1501 + if (strncasecmp(&qs[offset], "Content-Disposition", 19) != 0) {
1502 + /* hmm... content disposition was not where we expected it */
1506 + /* Found it, so let's go find "name=" */
1507 + if (!(envname=strstr(&qs[offset], "name="))) {
1508 + /* now name= is missing?! */
1514 + /* is there a filename tag? */
1515 + if ((filename=strstr(&qs[offset], "filename="))!= NULL)
1520 + /* make envname and filename ASCIIZ */
1521 + for (i=0; (envname[i] != '"') && (envname[i] != '\0'); i++);
1523 + envname[i] = '\0';
1525 + for (i=0; (filename[i] != '"') && (filename[i] != '\0'); i++);
1526 + filename[i] = '\0';
1530 + /* Ok, by some miracle, we have the name; let's skip till we */
1531 + /* come to a blank line */
1532 + line=LineToStr(&qs[offset], cl-offset);
1533 + while (strlen(&qs[offset]) > 1) {
1535 + line=LineToStr(&qs[offset], cl-offset);
1539 + /* And we go back to looking for a boundary */
1540 + while ((offset < cl) && (memcmp(&qs[offset], boundary, strlen(boundary))))
1543 + /* strip [cr] lf */
1544 + if ((qs[offset-1] == '\n') && (qs[offset-2] == '\r'))
1551 + /* ok, at this point, we know where the name is, and we know */
1552 + /* where the content is... we have to do one of two things */
1553 + /* based on whether its a file or not */
1554 + if (filename==NULL) { /* its not a file, so its easy */
1555 + /* just jam the content after the name */
1556 + memcpy(&envname[strlen(envname)+1], &qs[datastart], offset-datastart+1);
1557 + envname[strlen(envname)]='=';
1558 + put_var(FORM_VAR, envname);
1559 + } else { /* handle the fileupload case */
1560 + if (offset-datastart) { /* only if they uploaded */
1561 + if ( global_upload_size == 0 ) {
1564 + /* stuff in the filename */
1565 + ptr= calloc ( sizeof (char), strlen(envname)+strlen(filename)+2+5 );
1566 + sprintf (ptr, "%s_name=%s", envname, filename);
1567 + put_var(FORM_VAR, ptr);
1570 + fd=mkstemp(tmpname);
1575 + write(fd, &qs[datastart], offset-datastart);
1577 + ptr= calloc (sizeof(char), strlen(envname)+strlen(tmpname)+2);
1578 + sprintf (ptr, "%s=%s", envname, tmpname);
1579 + put_var(FORM_VAR, ptr);
1589 +/*-------------------------------------------------------------------------
1593 + *------------------------------------------------------------------------*/
1595 +int cgi_init(var_handler putvar_handler)
1599 + do_putvar = putvar_handler;
1601 + /* Read the current environment into our chain */
1603 + if (getenv("REQUEST_METHOD")) {
1604 + if (strcasecmp(getenv("REQUEST_METHOD"), "GET") == 0)
1605 + retval = ReadCGIQueryString();
1607 + if (strcasecmp(getenv("REQUEST_METHOD"), "POST") == 0)
1608 + retval = ReadCGIPOSTValues();
1613 Index: busybox-1.7.2/libbb/Kbuild
1614 ===================================================================
1615 --- busybox-1.7.2.orig/libbb/Kbuild 2007-10-30 15:35:06.000000000 -0500
1616 +++ busybox-1.7.2/libbb/Kbuild 2007-10-30 15:35:06.000000000 -0500
1618 lib-y += xreadlink.o
1620 # conditionally compiled objects:
1621 +lib-$(CONFIG_AWX) += cgi.o
1622 lib-$(CONFIG_FEATURE_MOUNT_LOOP) += loop.o
1623 lib-$(CONFIG_LOSETUP) += loop.o
1624 lib-$(CONFIG_FEATURE_MTAB_SUPPORT) += mtab.o