1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
|
/* Lynx CGI support LYCgi.c
* ================
*
* Authors
* GL George Lindholm <George.Lindholm@ubc.ca>
*
* History
* 15 Jun 95 Created as way to provide a lynx based service with
* dynamic pages without the need for a http daemon. GL
* 27 Jun 95 Added <index> (command line) support. Various cleanup
* and bug fixes. GL
* 04 Sep 97 Added support for PATH_INFO scripts. JKT
*
* Bugs
* If the called scripts aborts before sending the mime headers then
* lynx hangs.
*
* Should do something about SIGPIPE, (but then it should never happen)
*
* No support for redirection. Or mime-types.
*
* Should try and parse for a HTTP 1.1 header in case we are "calling" a
* nph- script.
*/
#include <HTUtils.h>
#include <HTTP.h>
#include <HTParse.h>
#include <HTTCP.h>
#include <HTFormat.h>
#include <HTFile.h>
#include <HTAlert.h>
#include <HTMIME.h>
#include <HTAABrow.h>
#include <LYGlobalDefs.h>
#include <LYUtils.h>
#include <HTML.h>
#include <HTInit.h>
#include <LYGetFile.h>
#include <LYBookmark.h>
#include <GridText.h>
#include <LYCgi.h>
#include <LYStrings.h>
#include <LYLocal.h>
#include <LYLeaks.h>
#include <www_wait.h>
struct _HTStream {
HTStreamClass *isa;
};
static char **env = NULL; /* Environment variables */
static int envc_size = 0; /* Slots in environment array */
static int envc = 0; /* Slots used so far */
static HTList *alloced = NULL;
#ifdef LYNXCGI_LINKS
static char *user_agent = NULL;
static char *server_software = NULL;
static char *accept_language = NULL;
static char *post_len = NULL;
#endif /* LYNXCGI_LINKS */
static void add_environment_value(const char *env_value);
#define PERROR(msg) CTRACE((tfp, "LYNXCGI: %s: %s\n", msg, LYStrerror(errno)))
#define PUTS(buf) (*target->isa->put_block)(target, buf, strlen(buf))
#ifdef LY_FIND_LEAKS
static void free_alloced_lynxcgi(void)
{
void *ptr;
while ((ptr = HTList_removeLastObject(alloced)) != NULL) {
FREE(ptr);
}
FREE(alloced);
#ifdef LYNXCGI_LINKS
FREE(user_agent);
FREE(server_software);
#endif
}
#endif /* LY_FIND_LEAKS */
static void remember_alloced(void *ptr)
{
if (!alloced) {
alloced = HTList_new();
#ifdef LY_FIND_LEAKS
atexit(free_alloced_lynxcgi);
#endif
}
HTList_addObject(alloced, ptr);
}
/*
* Simple routine for expanding the environment array and adding a value to
* it
*/
static void add_environment_value(const char *env_value)
{
if (envc == envc_size) { /* Need some more slots */
envc_size += 10;
if (env) {
env = (char **) realloc(env,
sizeof(env[0]) * (envc_size + 2));
/* + terminator and base 0 */
} else {
env = (char **) malloc(sizeof(env[0]) * (envc_size + 2));
/* + terminator and base 0 */
remember_alloced(env);
}
if (env == NULL) {
outofmem(__FILE__, "LYCgi");
}
}
env[envc++] = (char *) env_value;
env[envc] = NULL; /* Make sure it is always properly terminated */
}
/*
* Add the value of an existing environment variable to those passed on to the
* lynxcgi script.
*/
void add_lynxcgi_environment(const char *variable_name)
{
char *env_value;
env_value = LYGetEnv(variable_name);
if (env_value != NULL) {
char *add_value = NULL;
HTSprintf0(&add_value, "%s=%s", variable_name, env_value);
add_environment_value(add_value);
remember_alloced(add_value);
}
}
#ifdef __MINGW32__
static int LYLoadCGI(const char *arg,
HTParentAnchor *anAnchor,
HTFormat format_out,
HTStream *sink)
{
return -1;
}
#else
static int LYLoadCGI(const char *arg,
HTParentAnchor *anAnchor,
HTFormat format_out,
HTStream *sink)
{
int status = 0;
#ifdef LYNXCGI_LINKS
#ifndef VMS
char *cp;
struct stat stat_buf;
char *pgm = NULL; /* executable */
char *pgm_args = NULL; /* and its argument(s) */
int statrv;
char *orig_pgm = NULL; /* Path up to ? as given, URL-escaped */
char *document_root = NULL; /* Corrected value of DOCUMENT_ROOT */
char *path_info = NULL; /* PATH_INFO extracted from pgm */
char *pgm_buff = NULL; /* PATH_INFO extraction buffer */
char *path_translated; /* From document_root/path_info */
if (isEmpty(arg) || strlen(arg) <= 8) {
HTAlert(BAD_REQUEST);
status = -2;
return (status);
} else {
if (strncmp(arg, "lynxcgi://localhost", 19) == 0) {
StrAllocCopy(pgm, arg + 19);
} else {
StrAllocCopy(pgm, arg + 8);
}
if ((cp = strchr(pgm, '?')) != NULL) { /* Need to terminate executable */
*cp++ = '\0';
pgm_args = cp;
}
}
StrAllocCopy(orig_pgm, pgm);
if ((cp = trimPoundSelector(pgm)) != NULL) {
/*
* Strip a #fragment from path. In this case any pgm_args found above
* will also be bogus, since the '?' came after the '#' and is part of
* the fragment. Note that we don't handle the case where a '#'
* appears after a '?' properly according to URL rules. - kw
*/
pgm_args = NULL;
}
HTUnEscape(pgm);
/* BEGIN WebSter Mods */
/* If pgm is not stat-able, see if PATH_INFO data is at the end of pgm */
if ((statrv = stat(pgm, &stat_buf)) < 0) {
StrAllocCopy(pgm_buff, pgm);
while (statrv < 0 || (statrv = stat(pgm_buff, &stat_buf)) < 0) {
if ((cp = strrchr(pgm_buff, '/')) != NULL) {
*cp = '\0';
statrv = 1; /* force new stat() - kw */
} else {
PERROR("strrchr(pgm_buff, '/') returned NULL");
break;
}
}
if (statrv < 0) {
/* Did not find PATH_INFO data */
PERROR("stat() of pgm_buff failed");
} else {
/* Found PATH_INFO data. Strip it off of pgm and into path_info. */
StrAllocCopy(path_info, pgm + strlen(pgm_buff));
/* The following is safe since pgm_buff was derived from pgm
by stripping stuff off its end and by HTUnEscaping, so we
know we have enough memory allocated for pgm. Note that
pgm_args may still point into that memory, so we cannot
reallocate pgm here. - kw */
strcpy(pgm, pgm_buff);
CTRACE((tfp,
"LYNXCGI: stat() of %s succeeded, path_info=\"%s\".\n",
pgm_buff, path_info));
}
FREE(pgm_buff);
}
/* END WebSter Mods */
if (statrv != 0) {
/*
* Neither the path as given nor any components examined by backing up
* were stat()able. - kw
*/
HTAlert(gettext("Unable to access cgi script"));
PERROR("stat() failed");
status = -4;
} else
#ifdef _WINDOWS /* 1998/01/14 (Wed) 09:16:04 */
#define isExecutable(mode) (mode & (S_IXUSR))
#else
#define isExecutable(mode) (mode & (S_IXUSR|S_IXGRP|S_IXOTH))
#endif
if (!(S_ISREG(stat_buf.st_mode) && isExecutable(stat_buf.st_mode))) {
/*
* Not a runnable file, See if we can load it using "file:" code.
*/
char *new_arg = NULL;
/*
* But try "file:" only if the file we are looking at is the path as
* given (no path_info was extracted), otherwise it will be to
* confusing to know just what file is loaded. - kw
*/
if (path_info) {
CTRACE((tfp,
"%s is not a file and %s not an executable, giving up.\n",
orig_pgm, pgm));
FREE(path_info);
FREE(pgm);
FREE(orig_pgm);
status = -4;
return (status);
}
LYLocalFileToURL(&new_arg, orig_pgm);
CTRACE((tfp, "%s is not an executable file, passing the buck.\n", arg));
status = HTLoadFile(new_arg, anAnchor, format_out, sink);
FREE(new_arg);
} else if (path_info &&
anAnchor != HTMainAnchor &&
!(reloading && anAnchor->document) &&
strcmp(arg, HTLoadedDocumentURL()) &&
HText_AreDifferent(anAnchor, arg) &&
HTUnEscape(orig_pgm) &&
!exec_ok(HTLoadedDocumentURL(), orig_pgm,
CGI_PATH)) { /* exec_ok gives out msg. */
/*
* If we have extra path info and are not just reloading the current,
* check the full file path (after unescaping) now to catch forbidden
* segments. - kw
*/
status = HT_NOT_LOADED;
} else if (no_lynxcgi) {
HTUserMsg(CGI_DISABLED);
status = HT_NOT_LOADED;
} else if (no_bookmark_exec &&
anAnchor != HTMainAnchor &&
!(reloading && anAnchor->document) &&
strcmp(arg, HTLoadedDocumentURL()) &&
HText_AreDifferent(anAnchor, arg) &&
HTLoadedDocumentBookmark()) {
/*
* If we are reloading a lynxcgi document that had already been loaded,
* the various checks above should allow it even if no_bookmark_exec is
* TRUE an we are not now coming from a bookmark page. - kw
*/
HTUserMsg(BOOKMARK_EXEC_DISABLED);
status = HT_NOT_LOADED;
} else if (anAnchor != HTMainAnchor &&
!(reloading && anAnchor->document) &&
strcmp(arg, HTLoadedDocumentURL()) &&
HText_AreDifferent(anAnchor, arg) &&
!exec_ok(HTLoadedDocumentURL(), pgm,
CGI_PATH)) { /* exec_ok gives out msg. */
/*
* If we are reloading a lynxcgi document that had already been loaded,
* the various checks above should allow it even if exec_ok() would
* reject it because we are not now coming from a document with a URL
* allowed by TRUSTED_LYNXCGI rules. - kw
*/
status = HT_NOT_LOADED;
} else {
HTFormat format_in;
HTStream *target = NULL; /* Unconverted data */
int fd1[2], fd2[2];
char buf[1024];
int pid;
#ifdef HAVE_TYPE_UNIONWAIT
union wait wstatus;
#else
int wstatus;
#endif
if (anAnchor->isHEAD || keep_mime_headers) {
/* Show output as plain text */
format_in = WWW_PLAINTEXT;
} else {
/* Decode full HTTP response */
format_in = HTAtom_for("www/mime");
}
target = HTStreamStack(format_in,
format_out,
sink, anAnchor);
if (!target || target == NULL) {
char *tmp = 0;
HTSprintf0(&tmp, CANNOT_CONVERT_I_TO_O,
HTAtom_name(format_in),
HTAtom_name(format_out));
HTAlert(tmp);
FREE(tmp);
status = HT_NOT_LOADED;
} else if (anAnchor->post_data && pipe(fd1) < 0) {
HTAlert(CONNECT_SET_FAILED);
PERROR("pipe() failed");
status = -3;
} else if (pipe(fd2) < 0) {
HTAlert(CONNECT_SET_FAILED);
PERROR("pipe() failed");
close(fd1[0]);
close(fd1[1]);
status = -3;
} else {
static BOOL first_time = TRUE; /* One time setup flag */
if (first_time) { /* Set up static environment variables */
first_time = FALSE; /* Only once */
add_environment_value("REMOTE_HOST=localhost");
add_environment_value("REMOTE_ADDR=127.0.0.1");
HTSprintf0(&user_agent, "HTTP_USER_AGENT=%s/%s libwww/%s",
LYNX_NAME, LYNX_VERSION, HTLibraryVersion);
add_environment_value(user_agent);
HTSprintf0(&server_software, "SERVER_SOFTWARE=%s/%s",
LYNX_NAME, LYNX_VERSION);
add_environment_value(server_software);
}
fflush(stdout);
fflush(stderr);
CTRACE_FLUSH(tfp);
if ((pid = fork()) > 0) { /* The good, */
int chars, total_chars;
close(fd2[1]);
if (anAnchor->post_data) {
int written, remaining, total_written = 0;
close(fd1[0]);
/* We have form data to push across the pipe */
if (TRACE) {
CTRACE((tfp,
"LYNXCGI: Doing post, content-type '%s'\n",
anAnchor->post_content_type));
CTRACE((tfp, "LYNXCGI: Writing:\n"));
trace_bstring(anAnchor->post_data);
CTRACE((tfp, "----------------------------------\n"));
}
remaining = BStrLen(anAnchor->post_data);
while ((written = write(fd1[1],
BStrData(anAnchor->post_data) + total_written,
remaining)) != 0) {
if (written < 0) {
#ifdef EINTR
if (errno == EINTR)
continue;
#endif /* EINTR */
#ifdef ERESTARTSYS
if (errno == ERESTARTSYS)
continue;
#endif /* ERESTARTSYS */
PERROR("write() of POST data failed");
break;
}
CTRACE((tfp, "LYNXCGI: Wrote %d bytes of POST data.\n",
written));
total_written += written;
remaining -= written;
if (remaining == 0)
break;
}
if (remaining != 0) {
CTRACE((tfp, "LYNXCGI: %d bytes remain unwritten!\n",
remaining));
}
close(fd1[1]);
}
HTReadProgress(total_chars = 0, 0);
while ((chars = read(fd2[0], buf, sizeof(buf))) != 0) {
if (chars < 0) {
#ifdef EINTR
if (errno == EINTR)
continue;
#endif /* EINTR */
#ifdef ERESTARTSYS
if (errno == ERESTARTSYS)
continue;
#endif /* ERESTARTSYS */
PERROR("read() of CGI output failed");
break;
}
HTReadProgress(total_chars += chars, 0);
CTRACE((tfp, "LYNXCGI: Rx: %.*s\n", chars, buf));
(*target->isa->put_block) (target, buf, chars);
}
if (chars < 0 && total_chars == 0) {
status = HT_NOT_LOADED;
(*target->isa->_abort) (target, NULL);
target = NULL;
} else if (chars != 0) {
status = HT_PARTIAL_CONTENT;
} else {
status = HT_LOADED;
}
#if !HAVE_WAITPID
while (wait(&wstatus) != pid) ; /* do nothing */
#else
while (-1 == waitpid(pid, &wstatus, 0)) { /* wait for child */
#ifdef EINTR
if (errno == EINTR)
continue;
#endif /* EINTR */
#ifdef ERESTARTSYS
if (errno == ERESTARTSYS)
continue;
#endif /* ERESTARTSYS */
break;
}
#endif /* !HAVE_WAITPID */
close(fd2[0]);
} else if (pid == 0) { /* The Bad, */
char **argv = NULL;
int argv_cnt = 3; /* name, one arg and terminator */
char **cur_argv = NULL;
int exec_errno;
/* Set up output pipe */
close(fd2[0]);
dup2(fd2[1], fileno(stdout)); /* Should check success code */
dup2(fd2[1], fileno(stderr));
close(fd2[1]);
if (non_empty(language)) {
HTSprintf0(&accept_language, "HTTP_ACCEPT_LANGUAGE=%s", language);
add_environment_value(accept_language);
}
if (non_empty(pref_charset)) {
cp = NULL;
StrAllocCopy(cp, "HTTP_ACCEPT_CHARSET=");
StrAllocCat(cp, pref_charset);
add_environment_value(cp);
}
if (anAnchor->post_data &&
anAnchor->post_content_type) {
cp = NULL;
StrAllocCopy(cp, "CONTENT_TYPE=");
StrAllocCat(cp, anAnchor->post_content_type);
add_environment_value(cp);
}
if (anAnchor->post_data) { /* post script, read stdin */
close(fd1[1]);
dup2(fd1[0], fileno(stdin));
close(fd1[0]);
/* Build environment variables */
add_environment_value("REQUEST_METHOD=POST");
HTSprintf0(&post_len, "CONTENT_LENGTH=%d",
BStrLen(anAnchor->post_data));
add_environment_value(post_len);
} else {
close(fileno(stdin));
if (anAnchor->isHEAD) {
add_environment_value("REQUEST_METHOD=HEAD");
}
}
/*
* Set up argument line, mainly for <index> scripts
*/
if (pgm_args != NULL) {
for (cp = pgm_args; *cp != '\0'; cp++) {
if (*cp == '+') {
argv_cnt++;
}
}
}
argv = (char **) malloc(argv_cnt * sizeof(char *));
if (argv == NULL) {
outofmem(__FILE__, "LYCgi");
}
cur_argv = argv + 1; /* For argv[0] */
if (pgm_args != NULL) {
char *cr;
/* Data for a get/search form */
if (is_www_index) {
add_environment_value("REQUEST_METHOD=SEARCH");
} else if (!anAnchor->isHEAD && !anAnchor->post_data) {
add_environment_value("REQUEST_METHOD=GET");
}
cp = NULL;
StrAllocCopy(cp, "QUERY_STRING=");
StrAllocCat(cp, pgm_args);
add_environment_value(cp);
/*
* Split up arguments into argv array
*/
cp = pgm_args;
cr = cp;
while (1) {
if (*cp == '\0') {
*(cur_argv++) = HTUnEscape(cr);
break;
} else if (*cp == '+') {
*cp++ = '\0';
*(cur_argv++) = HTUnEscape(cr);
cr = cp;
}
cp++;
}
} else if (!anAnchor->isHEAD && !anAnchor->post_data) {
add_environment_value("REQUEST_METHOD=GET");
}
*cur_argv = NULL; /* Terminate argv */
argv[0] = pgm;
/* Begin WebSter Mods -jkt */
if (LYCgiDocumentRoot != NULL) {
/* Add DOCUMENT_ROOT to env */
cp = NULL;
StrAllocCopy(cp, "DOCUMENT_ROOT=");
StrAllocCat(cp, LYCgiDocumentRoot);
add_environment_value(cp);
}
if (path_info != NULL) {
/* Add PATH_INFO to env */
cp = NULL;
StrAllocCopy(cp, "PATH_INFO=");
StrAllocCat(cp, path_info);
add_environment_value(cp);
}
if (LYCgiDocumentRoot != NULL && path_info != NULL) {
/* Construct and add PATH_TRANSLATED to env */
StrAllocCopy(document_root, LYCgiDocumentRoot);
LYTrimHtmlSep(document_root);
path_translated = document_root;
StrAllocCat(path_translated, path_info);
cp = NULL;
StrAllocCopy(cp, "PATH_TRANSLATED=");
StrAllocCat(cp, path_translated);
add_environment_value(cp);
FREE(path_translated);
}
/* End WebSter Mods -jkt */
execve(argv[0], argv, env);
exec_errno = errno;
PERROR("execve failed");
printf("Content-Type: text/plain\r\n\r\n");
if (!anAnchor->isHEAD) {
printf("exec of %s failed", pgm);
printf(": %s.\r\n", LYStrerror(exec_errno));
}
fflush(stdout);
fflush(stderr);
_exit(1);
} else { /* and the Ugly */
HTAlert(CONNECT_FAILED);
PERROR("fork() failed");
status = HT_NO_DATA;
close(fd1[0]);
close(fd1[1]);
close(fd2[0]);
close(fd2[1]);
status = -1;
}
}
if (target != NULL) {
(*target->isa->_free) (target);
}
}
FREE(path_info);
FREE(pgm);
FREE(orig_pgm);
#else /* VMS */
HTStream *target;
char *buf = 0;
target = HTStreamStack(WWW_HTML,
format_out,
sink, anAnchor);
HTSprintf0(&buf, "<html>\n<head>\n<title>%s</title>\n</head>\n<body>\n",
gettext("Good Advice"));
PUTS(buf);
HTSprintf0(&buf, "<h1>%s</h1>\n", gettext("Good Advice"));
PUTS(buf);
HTSprintf0(&buf, "%s <a\n",
gettext("An excellent http server for VMS is available via"));
PUTS(buf);
HTSprintf0(&buf,
"href=\"http://kcgl1.eng.ohio-state.edu/www/doc/serverinfo.html\"\n");
PUTS(buf);
HTSprintf0(&buf, ">%s</a>.\n", gettext("this link"));
PUTS(buf);
HTSprintf0(&buf, "<p>%s\n",
gettext("It provides state of the art CGI script support.\n"));
PUTS(buf);
HTSprintf0(&buf, "</body>\n</html>\n");
PUTS(buf);
(*target->isa->_free) (target);
FREE(buf);
status = HT_LOADED;
#endif /* VMS */
#else /* LYNXCGI_LINKS */
HTUserMsg(CGI_NOT_COMPILED);
status = HT_NOT_LOADED;
#endif /* LYNXCGI_LINKS */
return (status);
}
#endif /* __MINGW32__ */
#ifdef GLOBALDEF_IS_MACRO
#define _LYCGI_C_GLOBALDEF_1_INIT { "lynxcgi", LYLoadCGI, 0 }
GLOBALDEF(HTProtocol, LYLynxCGI, _LYCGI_C_GLOBALDEF_1_INIT);
#else
GLOBALDEF HTProtocol LYLynxCGI =
{"lynxcgi", LYLoadCGI, 0};
#endif /* GLOBALDEF_IS_MACRO */
|