1 Index: busybox-1.8.1/editors/awk.c
2 ===================================================================
3 --- busybox-1.8.1.orig/editors/awk.c 2007-11-10 16:55:07.032260312 +0100
4 +++ busybox-1.8.1/editors/awk.c 2007-11-10 17:07:04.493146078 +0100
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,12 @@
77 int awk_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
78 +int awx_main(int argc, char **argv);
81 +static int is_awx = 0;
84 int awk_main(int argc, char **argv)
87 @@ -2817,6 +2838,11 @@
96 opt_complementary = "v::f::";
97 opt = getopt32(argv, "F:v:f:W:", &opt_F, &opt_v, &opt_f, &opt_W);
99 Index: busybox-1.8.1/editors/awx.c
100 ===================================================================
101 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
102 +++ busybox-1.8.1/editors/awx.c 2007-11-10 17:06:19.258568308 +0100
105 + * awk web extension
107 + * Copyright (C) 2007 by Felix Fietkau <nbd@openwrt.org>
109 + * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
114 +#include "awx_parser.h"
116 +#define LINE_BUF 2048
117 +#define HASH_MAX 1536
118 +#define TR_START "@TR<<"
124 +static xhash *lstr = NULL;
125 +static xhash *formvar = NULL;
126 +static int lang_inuse = 0;
128 +/* look up a translation symbol from the hash */
129 +static inline const char *translate_lookup(char *str)
131 + char *name, *def, *p;
136 + if (((p = strchr(str, '|')) != NULL)
137 + || ((p = strchr(str, '#')) != NULL)) {
142 + hi = hash_search(lstr, name);
148 + return getvar_s(v);
151 +/* look for translation markers in the line and return the translated string */
152 +static char *translate_line(char *line)
154 + const char *tok[MAX_TR * 3];
155 + char *l, *p, *p2 = NULL, *res;
156 + int len = 0, _pos = 0, i, tr_abort = 0;
157 + static char *backlog = NULL;
159 + if (backlog && line) {
160 + backlog = xrealloc(backlog, strlen(backlog) + strlen(line) + 1);
161 + sprintf(backlog + strlen(backlog), line);
167 + while (l != NULL) {
168 + if ((p = strstr(l, TR_START)) == NULL) {
169 + len += strlen((tok[_pos++] = l));
173 + p2 = strstr(p, TR_END);
181 + len += strlen((tok[_pos++] = l));
183 + len += strlen((tok[_pos++] = translate_lookup(p + strlen(TR_START))));
186 + l += strlen(TR_END);
190 + p = xmalloc(len + 1);
193 + for (i = 0; i < _pos; i++) {
195 + p += strlen(tok[i]);
201 + if (tr_abort && p2)
207 +/* hook for intercepting awk's use of puts. used for running all printed strings
208 + * through the translation system */
209 +static inline int fputs_hook (__const char *__restrict __s, FILE *__restrict __stream)
211 + if (lang_inuse && (__stream == stdout)) {
215 + str = translate_line((char *) __s);
216 + ret = fputs(str, __stream);
222 + return fputs(__s, __stream);
225 +static var *init_lang(var *res, var *args, int nargs)
228 + lstr = hash_init();
235 +/* load and parse language file */
236 +static void load_lang_file(char *file)
239 + char *b, *name, *value;
240 + char buf1[LINE_BUF];
242 + if ((f = fopen(file, "r")) == NULL)
245 + while (!feof(f) && (fgets(buf1, LINE_BUF - 1, f) != NULL)) {
248 + continue; /* skip comments */
250 + while (isspace(*b))
251 + b++; /* skip leading spaces */
256 + if ((b = strstr(name, "=>")) == NULL)
257 + continue; /* separator not found */
264 + for (b--; isspace(*b); b--)
265 + *b = 0; /* remove trailing spaces */
267 + while (isspace(*value))
268 + value++; /* skip leading spaces */
270 + for (b = value + strlen(value) - 1; isspace(*b); b--)
271 + *b = 0; /* remove trailing spaces */
276 + setvar_s(findvar(lstr,name), value);
282 +static var *load_lang(var *res, var *args, int nargs)
284 + const char *langfmt = "/usr/lib/webif/lang/%s.txt";
285 + char lbuf[LINE_BUF];
289 + init_lang(res, args, nargs);
291 + lang = getvar_s(args);
292 + if (!lang || !strcmp(lang, ""))
295 + sprintf(lbuf, langfmt, lang);
296 + load_lang_file(lbuf);
301 +/* read the contents of an entire file */
302 +static char *get_file(const char *fname)
308 + F = fopen(fname, "r");
313 + if (fseek(F, 0, SEEK_END) == 0) {
315 + s = (char *)xmalloc(flen+4);
316 + fseek(F, 0, SEEK_SET);
317 + i = 1 + fread(s+1, 1, flen, F);
319 + for (i=j=1; j>0; i+=j) {
320 + s = (char *)xrealloc(s, i+4096);
321 + j = fread(s+i, 1, 4094, F);
333 + * taken from parse_program from awk.c
334 + * END{} is not parsed here, and BEGIN{} is executed immediately
336 +static void parse_include(char *p)
339 + chain *initseq = NULL;
345 + memset(&tmp, 0, sizeof(tmp));
348 + while ((tclass = next_token(TC_EOF | TC_OPSEQ |
349 + TC_OPTERM | TC_BEGIN | TC_FUNCDECL)) != TC_EOF) {
350 + if (tclass & TC_OPTERM)
354 + if (tclass & TC_BEGIN) {
355 + initseq = xzalloc(sizeof(chain));
358 + } else if (tclass & TC_FUNCDECL) {
359 + next_token(TC_FUNCTION);
361 + f = newfunc(t_string);
363 + f->x.body.first = NULL;
365 + while (next_token(TC_VARIABLE | TC_SEQTERM) & TC_VARIABLE) {
366 + v = findvar(ahash, t_string);
367 + v->x.aidx = (f->nargs)++;
369 + if (next_token(TC_COMMA | TC_SEQTERM) & TC_SEQTERM)
372 + seq = &(f->x.body);
374 + clear_array(ahash);
377 + if (initseq && initseq->first)
378 + tv = evaluate(initseq->first, tv);
383 +/* include an awk file and run its BEGIN{} section */
384 +static xhash *includes = NULL;
385 +static void include_file(const char *filename)
389 + int oldlnr = g_lineno;
390 + const char *oldprg = g_progname;
393 + includes = hash_init();
395 + /* find out if the file has been included already */
396 + v = findvar(includes, filename);
401 + /* read include file */
402 + s = get_file(filename);
404 + fprintf(stderr, "Could not open file.\n");
408 + g_progname = xstrdup(filename);
409 + parse_include(s+1);
412 + g_progname = oldprg;
415 +static var *include(var *res, var *args, int nargs)
419 + s = getvar_s(args);
420 + if (s && (strlen(s) > 0))
426 +/* parse an awk expression */
427 +static var *parse_awk(char *str, var *tv)
432 + memset(&body, 0, sizeof(body));
436 + /* end of expression, assume that there's going to be a free byte
437 + * at the end of the string that can be used for the ')' */
438 + strcat(str + strlen(str), "}");
439 + n = parse_expr(TC_GRPTERM);
443 + return evaluate(n, tv);
446 +static inline void print_translate(char *s)
450 + str = translate_line(s);
451 + fputs(str, stdout);
457 +static void render_element(struct template_cb *tcb, struct template_element *e)
465 + g_lineno = e->line;
468 + s = malloc(strlen(e->var) + 2);
470 + print_translate(s);
474 + s = malloc(strlen(e->var) + 2);
477 + s2 = strdup(getvar_s(parse_awk(s, v)));
479 + print_translate(s2);
484 + s = malloc(strlen(e->var) + 2);
487 + i = istrue(parse_awk(s, v));
492 + execute_template(tcb, e->sub);
494 + execute_template(tcb, e->sub2);
497 + v = newvar(e->var);
498 + hashwalk_init(v, iamarray(findvar(vhash, e->in)));
499 + while (hashwalk_next(v)) {
500 + execute_template(tcb, e->sub);
510 +/* awk method render(), which opens a template file and processes all awk ssi calls */
511 +static void render_file(const char *filename)
513 + struct template_cb tcb;
514 + struct template_element *e;
516 + const char *oldprg = g_progname;
517 + int oldlnr = g_lineno;
522 + f = fopen(filename, "r");
526 + g_progname = xstrdup(filename);
528 + memset(&tcb, 0, sizeof(tcb));
529 + tcb.handle_element = render_element;
530 + e = parse_template(&tcb, f);
531 + execute_template(&tcb, e);
532 + free_template(&tcb, e);
534 + g_progname = oldprg;
538 +static var *render(var *res, var *args, int nargs)
542 + s = getvar_s(args);
551 +/* Call render, but only if this function hasn't been called already */
552 +static int layout_rendered = 0;
553 +static var *render_layout(var *res, var *args, int nargs)
555 + if (layout_rendered)
557 + layout_rendered = 1;
558 + return render(res, args, nargs);
561 +/* registers a global c function for the awk interpreter */
562 +static void register_cfunc(const char *name, awk_cfunc cfunc, int nargs)
568 + f->x.cfunc = cfunc;
572 +static void putvar(vartype type, char *name, char *value)
574 + if (type != FORM_VAR)
577 + setvar_u(findvar(formvar, name), value);
580 +static const char *cgi_getvar(const char *name)
583 + formvar = hash_init();
587 + if (!formvar || !name)
590 + return getvar_s(findvar(formvar, name));
593 +/* function call for accessing cgi form variables */
594 +static var *getvar(var *res, var *args, int nargs)
596 + const char *s, *svar;
598 + s = getvar_s(args);
602 + svar = cgi_getvar(s);
606 + setvar_u(res, svar);
611 +/* call an awk function without arguments by string reference */
612 +static var *call(var *res, var *args, int nargs)
614 + const char *s = getvar_s(args);
621 + if (f && f->type == AWKFUNC && f->x.body.first)
622 + return evaluate(f->x.body.first, res);
629 +static int run_awxscript(char *name)
631 + var tv, *layout, *action;
632 + char *tmp, *s = NULL;
637 + /* read the main controller source */
638 + s = get_file(g_progname);
640 + fprintf(stderr, "Could not open file\n");
643 + parse_program(s+1);
647 + /* set some defaults for ACTION and LAYOUT, which will have special meaning */
648 + layout = newvar("LAYOUT");
649 + setvar_s(layout, "views/layout.ahtml");
651 + /* run the BEGIN {} block */
652 + evaluate(beginseq.first, &tv);
654 + action = newvar("ACTION");
655 + if (!(strlen(getvar_s(action)) > 0)) {
656 + tmp = (char *) cgi_getvar("action");
657 + if (!tmp || (strlen(tmp) <= 0))
658 + tmp = strdup("default");
660 + setvar_p(action, tmp);
663 + /* call the action (precedence: begin block override > cgi parameter > "default") */
664 + tmp = xmalloc(strlen(getvar_s(action)) + 7);
665 + sprintf(tmp, "handle_%s", getvar_s(action));
666 + setvar_s(action, tmp);
667 + call(&tv, action, 1);
670 + /* render the selected layout, will do nothing if render_layout has been called from awk */
671 + render_layout(&tv, layout, 1);
677 +/* main awx processing function. called from awk_main() */
678 +static int do_awx(int argc, char **argv)
683 + char **args = argv;
687 + /* register awk C callbacks */
688 + register_cfunc("getvar", getvar, 1);
689 + register_cfunc("render", render, 1);
690 + register_cfunc("render_layout", render_layout, 1);
691 + register_cfunc("call", call, 1);
692 + register_cfunc("include", include, 1);
693 + register_cfunc("init_lang", init_lang, 1);
694 + register_cfunc("load_lang", load_lang, 1);
699 + /* fill in ARGV array */
700 + setvar_i(intvar[ARGC], argc + 1);
701 + setari_u(intvar[ARGV], 0, "awx");
704 + setari_u(intvar[ARGV], ++i, *args++);
706 + while((c = getopt(argc, argv, "i:f:")) != EOF) {
709 + g_progname = optarg;
710 + include_file(optarg);
714 + g_progname = optarg;
715 + render_file(optarg);
723 + fprintf(stderr, "Invalid argument.\n");
727 + ret = run_awxscript(*argv);
733 +/* entry point for awx applet */
734 +int awx_main(int argc, char **argv)
737 + return awk_main(argc, argv);
740 Index: busybox-1.8.1/editors/awx_parser.h
741 ===================================================================
742 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
743 +++ busybox-1.8.1/editors/awx_parser.h 2007-11-10 17:06:19.266568760 +0100
745 +#ifndef __TEMPLATE_PARSER_H
746 +#define __TEMPLATE_PARSER_H
755 +struct template_element;
758 +struct template_cb {
759 + void *(*prepare_code)(struct template_element *);
760 + void (*handle_element)(struct template_cb *, struct template_element *);
761 + void (*free_code)(struct template_element *);
764 +struct template_element {
770 + struct template_element *parent;
771 + struct template_element *sub;
772 + struct template_element *sub2;
773 + struct template_element *prev;
774 + struct template_element *next;
778 +struct template_element *parse_template(struct template_cb *cb, FILE *in);
779 +void execute_template(struct template_cb *cb, struct template_element *e);
780 +void free_template(struct template_cb *cb, struct template_element *e);
783 Index: busybox-1.8.1/editors/awx_parser.l
784 ===================================================================
785 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
786 +++ busybox-1.8.1/editors/awx_parser.l 2007-11-10 17:06:19.270568989 +0100
792 +#include "busybox.h"
793 +#include "awx_parser.h"
810 +char *statestr[] = {
811 + [S_INIT] = "S_INIT",
812 + [S_TEXT] = "S_TEXT",
813 + [S_CODE] = "S_CODE",
814 + [S_IF_START] = "S_IF_START",
815 + [S_FOR_START] = "S_FOR_START",
816 + [S_FOR_IN] = "S_FOR_IN",
821 + [T_TEXT] = "T_TEXT",
824 + [T_CODE] = "T_CODE"
828 +static struct template_cb *parse_cb;
829 +static struct template_element *cur, *head;
830 +static char *textbuf;
831 +static unsigned int buflen;
832 +static unsigned int buf_offset;
833 +static int _lnr = 0;
835 +static void buf_realloc(void)
838 + textbuf = xrealloc(textbuf, buflen);
841 +static void parse_error(char *str)
843 + fprintf(stderr, "Parse error%s%s\n", (str ? ": " : "!"), (str ?: ""));
848 +static struct template_element *new_template_element(struct template_element *parent)
850 + struct template_element *ptr;
852 + ptr = xzalloc(sizeof(struct template_element));
853 + ptr->parent = parent;
857 +static inline void next_template_element(void)
859 + cur->next = new_template_element(cur->parent);
860 + cur->next->prev = cur;
864 +static void addtext(char *text)
866 + while(buf_offset + strlen(text) + 1 > buflen)
869 + buf_offset += sprintf(&textbuf[buf_offset], "%s", text);
872 +static void set_state(int newstate)
877 + static int _rec = 0;
878 + fprintf(stderr, "DEBUG(%d): %s => %s: %s\n", _rec, statestr[state], statestr[newstate], textbuf);
880 + ptr = xstrdup(textbuf);
881 + if (state == S_FOR_IN)
886 + if (parse_cb && (cur->t == T_CODE) && parse_cb->prepare_code)
887 + parse_cb->prepare_code(cur);
900 + if (ptr || !cur->prev)
901 + next_template_element();
905 + if (ptr || !cur->prev)
906 + next_template_element();
912 + parse_error("'@else' without parent element");
913 + cur->sub2 = new_template_element(cur);
923 + parse_error("'@end' without parent element");
925 + next_template_element();
932 + next_template_element();
939 + cur->sub = new_template_element(cur);
948 + if (ptr || !cur->prev)
949 + next_template_element();
962 +"<%"[ \n\t]*"@if"[ \n\t]+ {
963 + if (state == S_TEXT)
964 + set_state(S_IF_START);
969 +"<%"[ \n\t]*"@for"[ \n\t]+ {
970 + if (state == S_TEXT)
971 + set_state(S_FOR_START);
976 +[ \n\t]+"in"[ \n\t]+ {
977 + if (state == S_FOR_START)
978 + set_state(S_FOR_IN);
983 +"<%"[ \n\t]*"@end"[ \n\t]*%> {
984 + if (state != S_TEXT)
989 +"<%"[ \n\t]*"@else"[ \n\t]*%> {
990 + if (state != S_TEXT)
996 + if (state != S_TEXT)
997 + parse_error("'<%' cannot be nested");
1002 + if (state == S_TEXT)
1004 + set_state(S_TEXT);
1009 + if (state == S_TEXT)
1020 +void execute_template(struct template_cb *cb, struct template_element *e)
1022 + static int rec = 0;
1026 + fprintf(stderr, "DEBUG: execute(%d)\t%s\n", rec, typestr[e->t]);
1029 + if (cb->handle_element)
1030 + cb->handle_element(cb, e);
1042 +struct template_element *parse_template(struct template_cb *cb, FILE *in)
1050 + textbuf = xzalloc(buflen);
1052 + head = xzalloc(sizeof(struct template_element));
1062 +void free_template(struct template_cb *cb, struct template_element *e)
1064 + struct template_element *next;
1071 + if (cb->free_code)
1076 + free_template(cb, e->sub);
1088 + return free_template(cb, next);
1090 Index: busybox-1.8.1/editors/Config.in
1091 ===================================================================
1092 --- busybox-1.8.1.orig/editors/Config.in 2007-11-10 02:40:54.000000000 +0100
1093 +++ busybox-1.8.1/editors/Config.in 2007-11-10 17:06:19.274569218 +0100
1095 Awk is used as a pattern scanning and processing language. This is
1096 the BusyBox implementation of that programming language.
1099 + bool "Enable awx (awk web extension)"
1103 + awx - awk web extension
1105 config FEATURE_AWK_MATH
1106 bool "Enable math functions (requires libm)"
1108 Index: busybox-1.8.1/editors/Kbuild
1109 ===================================================================
1110 --- busybox-1.8.1.orig/editors/Kbuild 2007-11-10 02:40:54.000000000 +0100
1111 +++ busybox-1.8.1/editors/Kbuild 2007-11-10 17:06:19.278569448 +0100
1113 lib-$(CONFIG_PATCH) += patch.o
1114 lib-$(CONFIG_SED) += sed.o
1115 lib-$(CONFIG_VI) += vi.o
1116 +lib-$(CONFIG_AWX) += awx_parser.o
1118 +editors/awx_parser.c: editors/awx_parser.l editors/awx_parser.h
1122 +editors/awx_parser.o: editors/awx_parser.c FORCE
1123 + $(call cmd,force_checksrc)
1124 + $(call if_changed_rule,cc_o_c)
1125 Index: busybox-1.8.1/include/applets.h
1126 ===================================================================
1127 --- busybox-1.8.1.orig/include/applets.h 2007-11-10 17:03:38.957433264 +0100
1128 +++ busybox-1.8.1/include/applets.h 2007-11-10 17:06:19.282569674 +0100
1130 USE_ARPING(APPLET(arping, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
1131 USE_ASH(APPLET_NOUSAGE(ash, ash, _BB_DIR_BIN, _BB_SUID_NEVER))
1132 USE_AWK(APPLET_NOEXEC(awk, awk, _BB_DIR_USR_BIN, _BB_SUID_NEVER, awk))
1133 +USE_AWX(APPLET_NOUSAGE(awx, awx, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
1134 USE_BASENAME(APPLET_NOFORK(basename, basename, _BB_DIR_USR_BIN, _BB_SUID_NEVER, basename))
1135 USE_BBCONFIG(APPLET(bbconfig, _BB_DIR_BIN, _BB_SUID_NEVER))
1136 //USE_BBSH(APPLET(bbsh, _BB_DIR_BIN, _BB_SUID_NEVER))
1137 Index: busybox-1.8.1/include/cgi.h
1138 ===================================================================
1139 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
1140 +++ busybox-1.8.1/include/cgi.h 2007-11-10 17:06:19.282569674 +0100
1145 +typedef enum { FORM_VAR, COOKIE_VAR } vartype;
1146 +typedef void (*var_handler) (vartype, char *, char *);
1147 +int cgi_init(var_handler);
1150 Index: busybox-1.8.1/libbb/cgi.c
1151 ===================================================================
1152 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
1153 +++ busybox-1.8.1/libbb/cgi.c 2007-11-10 17:06:19.282569674 +0100
1155 +/* --------------------------------------------------------------------------
1156 + * functions for processing cgi form data
1157 + * Copyright (C) 2007 by Felix Fietkau <nbd@openwrt.org>
1159 + * parts taken from the core of haserl.cgi - a poor-man's php for embedded/lightweight environments
1160 + * $Id: haserl.c,v 1.13 2004/11/10 17:59:35 nangel Exp $
1161 + * Copyright (c) 2003,2004 Nathan Angelacos (nangel@users.sourceforge.net)
1163 + * This program is free software; you can redistribute it and/or modify
1164 + * it under the terms of the GNU General Public License as published by
1165 + * the Free Software Foundation; either version 2 of the License, or
1166 + * (at your option) any later version.
1168 + * This program is distributed in the hope that it will be useful,
1169 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1170 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1171 + * General Public License for more details.
1173 + * You should have received a copy of the GNU General Public License
1174 + * along with this program; if not, write to the Free Software
1175 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1178 + * The x2c() and unescape_url() routines were taken from
1179 + * http://www.jmarshall.com/easy/cgi/getcgi.c.txt
1181 + * The comments in that text file state:
1183 + *** Written in 1996 by James Marshall, james@jmarshall.com, except
1184 + *** that the x2c() and unescape_url() routines were lifted directly
1185 + *** from NCSA's sample program util.c, packaged with their HTTPD.
1186 + *** For the latest, see http://www.jmarshall.com/easy/cgi/
1189 + ------------------------------------------------------------------------- */
1192 +#include <unistd.h>
1194 +#include <sys/mman.h>
1195 +#include <sys/types.h>
1196 +#include <sys/wait.h>
1197 +#include <sys/stat.h>
1198 +#include <sys/fcntl.h>
1199 +#include <stdlib.h>
1200 +#include <string.h>
1203 +#ifndef MAX_UPLOAD_KB
1204 +#define MAX_UPLOAD_KB 2048
1206 +#define TEMPDIR "/tmp"
1208 +static int global_upload_size = 0;
1209 +static int ReadMimeEncodedInput(char *qs);
1210 +static var_handler do_putvar = NULL;
1213 + * Convert 2 char hex string into char it represents
1214 + * (from http://www.jmarshall.com/easy/cgi)
1216 +static char x2c (char *what) {
1219 + digit=(what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
1221 + digit+=(what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
1227 + * unsescape %xx to the characters they represent
1230 +static void unescape_url (char *url) {
1233 + for (i=0, j=0; url[j]; ++i, ++j) {
1234 + if ((url[i] = url[j]) == '%') {
1235 + url[i] = x2c(&url[j+1]);
1242 +static inline void put_var(vartype type, char *var)
1249 + val = strchr(var, '=');
1255 + do_putvar(type, var, val);
1262 + * if HTTP_COOKIE is passed as an environment variable,
1263 + * attempt to parse its values into environment variables
1265 +static void CookieVars (void)
1270 + if (getenv("HTTP_COOKIE") != NULL)
1271 + qs=strdup(getenv("HTTP_COOKIE"));
1275 + /** split on; to extract name value pairs */
1276 + token=strtok(qs, ";");
1278 + // skip leading spaces
1279 + while ( token[0] == ' ' )
1282 + put_var(COOKIE_VAR, token);
1284 + token = strtok(NULL, ";");
1290 + * Read cgi variables from query string, and put in environment
1292 +static int ReadCGIQueryString (void)
1298 + if (getenv("QUERY_STRING") != NULL)
1299 + qs=strdup(getenv("QUERY_STRING"));
1303 + /* change plusses into spaces */
1304 + for (i=0; qs[i]; i++ ) { if (qs[i] == '+' ) { qs[i] = ' ';} };
1306 + /** split on & and ; to extract name value pairs */
1308 + token=strtok(qs, "&;");
1310 + unescape_url(token);
1311 + put_var(FORM_VAR, token);
1312 + token=strtok(NULL, "&;");
1321 + * Read cgi variables from stdin (for POST queries)
1322 + * (oh... and if its mime-encoded file upload, we save the
1323 + * file to /tmp; and return the name of the tmp file
1324 + * the cgi script is responsible for disposing of the tmp file
1327 +static int ReadCGIPOSTValues (void) {
1329 + int content_length;
1334 + if (getenv("CONTENT_LENGTH") == NULL)
1337 + content_length = atoi(getenv("CONTENT_LENGTH"));
1339 + /* protect ourselves from 20GB file uploads */
1340 + if (content_length > MAX_UPLOAD_KB * 1024 ) {
1341 + /* But we need to finish reading the content */
1342 + while ( fread( &i, sizeof(int), 1, stdin) == 1 );
1346 + if (!(qs=malloc(content_length+1)))
1349 + /* set the buffer to null, so that a browser messing with less
1350 + data than content_length won't buffer underrun us */
1351 + memset(qs, 0 ,content_length+1);
1353 + if ((!fread(qs,content_length,1,stdin) &&
1354 + (content_length > 0)
1355 + && !feof(stdin))) {
1361 + if (getenv("CONTENT_TYPE") && (strncasecmp(getenv("CONTENT_TYPE"), "multipart/form-data", 19) == 0)) {
1362 + /* This is a mime request, we need to go to the mime handler */
1363 + i=ReadMimeEncodedInput(qs);
1369 + /* change plusses into spaces */
1370 + for (i=0; qs[i]; i++ ) { if (qs[i] == '+' ) { qs[i] = ' ';} };
1372 + /** split on & and ; to extract name value pairs */
1373 + token=strtok(qs, "&;");
1375 + unescape_url(token);
1376 + put_var(FORM_VAR, token);
1377 + token=strtok(NULL, "&;");
1386 + * LineToStr - Scans char and replaces the first "\n" with a "\0";
1387 + * If it finds a "\r", it does that to; (fix DOS brokennes) returns
1388 + * the length of the string;
1390 +static int LineToStr (char *string, size_t max) {
1393 + while ((offset < max) && (string[offset] != '\n') && (string[offset] != '\r'))
1396 + if (string[offset] == '\r') {
1397 + string[offset]='\0';
1400 + if (string[offset] == '\n') {
1401 + string[offset]='\0';
1410 + * ReadMimeEncodedInput - handles things that are mime encoded
1411 + * takes a pointer to the input; returns 0 on success
1414 +static int ReadMimeEncodedInput(char *qs)
1426 + char tmpname[] = TEMPDIR "/XXXXXX";
1428 + /* we should only get here if the content type was set. Segfaults happen
1429 + if Content_Type is null */
1431 + if (getenv("CONTENT_LENGTH") == NULL)
1432 + /* No content length?! */
1435 + cl=atoi(getenv("CONTENT_LENGTH"));
1437 + /* we do this 'cause we can't mess with the real env. variable - it would
1438 + * overwrite the environment - I tried.
1440 + i=strlen(getenv("CONTENT_TYPE"))+1;
1443 + memcpy(ct, getenv("CONTENT_TYPE"), i);
1449 + while (i < strlen(ct) && (strncmp("boundary=", &ct[i], 9) != 0))
1452 + if (i == strlen(ct)) {
1453 + /* no boundary informaiton found */
1457 + boundary=&ct[i+7];
1458 + /* add two leading -- to the boundary */
1462 + /* begin the big loop. Look for:
1464 + Content-Disposition: form-data; name="......."
1469 + Content-Disposition: form-data; name="....." filename="....."
1477 + while (offset < cl) {
1478 + /* first look for boundary */
1479 + while ((offset < cl) && (memcmp(&qs[offset], boundary, strlen(boundary))))
1482 + /* if we got here and we ran off the end, its an error */
1483 + if (offset >= cl) {
1488 + /* if the two characters following the boundary are --, */
1489 + /* then we are at the end, exit */
1490 + if (memcmp(&qs[offset+strlen(boundary)], "--", 2) == 0) {
1494 + /* find where the offset should be */
1495 + line=LineToStr(&qs[offset], cl-offset);
1498 + /* Now we're going to look for content-disposition */
1499 + line=LineToStr(&qs[offset], cl-offset);
1500 + if (strncasecmp(&qs[offset], "Content-Disposition", 19) != 0) {
1501 + /* hmm... content disposition was not where we expected it */
1505 + /* Found it, so let's go find "name=" */
1506 + if (!(envname=strstr(&qs[offset], "name="))) {
1507 + /* now name= is missing?! */
1513 + /* is there a filename tag? */
1514 + if ((filename=strstr(&qs[offset], "filename="))!= NULL)
1519 + /* make envname and filename ASCIIZ */
1520 + for (i=0; (envname[i] != '"') && (envname[i] != '\0'); i++);
1522 + envname[i] = '\0';
1524 + for (i=0; (filename[i] != '"') && (filename[i] != '\0'); i++);
1525 + filename[i] = '\0';
1529 + /* Ok, by some miracle, we have the name; let's skip till we */
1530 + /* come to a blank line */
1531 + line=LineToStr(&qs[offset], cl-offset);
1532 + while (strlen(&qs[offset]) > 1) {
1534 + line=LineToStr(&qs[offset], cl-offset);
1538 + /* And we go back to looking for a boundary */
1539 + while ((offset < cl) && (memcmp(&qs[offset], boundary, strlen(boundary))))
1542 + /* strip [cr] lf */
1543 + if ((qs[offset-1] == '\n') && (qs[offset-2] == '\r'))
1550 + /* ok, at this point, we know where the name is, and we know */
1551 + /* where the content is... we have to do one of two things */
1552 + /* based on whether its a file or not */
1553 + if (filename==NULL) { /* its not a file, so its easy */
1554 + /* just jam the content after the name */
1555 + memcpy(&envname[strlen(envname)+1], &qs[datastart], offset-datastart+1);
1556 + envname[strlen(envname)]='=';
1557 + put_var(FORM_VAR, envname);
1558 + } else { /* handle the fileupload case */
1559 + if (offset-datastart) { /* only if they uploaded */
1560 + if ( global_upload_size == 0 ) {
1563 + /* stuff in the filename */
1564 + ptr= calloc ( sizeof (char), strlen(envname)+strlen(filename)+2+5 );
1565 + sprintf (ptr, "%s_name=%s", envname, filename);
1566 + put_var(FORM_VAR, ptr);
1569 + fd=mkstemp(tmpname);
1574 + write(fd, &qs[datastart], offset-datastart);
1576 + ptr= calloc (sizeof(char), strlen(envname)+strlen(tmpname)+2);
1577 + sprintf (ptr, "%s=%s", envname, tmpname);
1578 + put_var(FORM_VAR, ptr);
1588 +/*-------------------------------------------------------------------------
1592 + *------------------------------------------------------------------------*/
1594 +int cgi_init(var_handler putvar_handler)
1598 + do_putvar = putvar_handler;
1600 + /* Read the current environment into our chain */
1602 + if (getenv("REQUEST_METHOD")) {
1603 + if (strcasecmp(getenv("REQUEST_METHOD"), "GET") == 0)
1604 + retval = ReadCGIQueryString();
1606 + if (strcasecmp(getenv("REQUEST_METHOD"), "POST") == 0)
1607 + retval = ReadCGIPOSTValues();
1612 Index: busybox-1.8.1/libbb/Kbuild
1613 ===================================================================
1614 --- busybox-1.8.1.orig/libbb/Kbuild 2007-11-10 17:04:07.547062497 +0100
1615 +++ busybox-1.8.1/libbb/Kbuild 2007-11-10 17:06:19.282569674 +0100
1617 lib-y += xreadlink.o
1619 # conditionally compiled objects:
1620 +lib-$(CONFIG_AWX) += cgi.o
1621 lib-$(CONFIG_FEATURE_MOUNT_LOOP) += loop.o
1622 lib-$(CONFIG_LOSETUP) += loop.o
1623 lib-$(CONFIG_FEATURE_MTAB_SUPPORT) += mtab.o