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 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127
|
/**********************************************************************
* cgi.c -- libcgi
*
* Copyright 1994, 1996, 1998
* by the Massachusetts Institute of Technology
* For copying and distribution information, please see the file
* <mit-copyright.h>.
**********************************************************************/
#include "mit-copyright.h"
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <signal.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
#include "cgi.h"
#ifdef ENABLE_OWNER_BOUNCE
#include <pwd.h>
#endif /* ENABLE_OWNER_BOUNCE */
char *cgi_query=(char*)0;
time_t received_time;
char datebuf[80];
int cgi_alloc_form(formp)
cgi_form *formp;
{
char *env_var;
/* zero out everything */
memset(formp, 0, sizeof(cgi_form));
/* get query string from environment */
if (!cgi_query)
{
env_var=getenv("REQUEST_METHOD");
if (!env_var)
{
formp->errcond=1;
strcpy(formp->errmsg, "400 REQUEST_METHOD not set.");
return(1);
}
if (!strcmp(env_var, "GET"))
{
cgi_query= getenv("QUERY_STRING");
if (!cgi_query)
{
formp->errcond=1;
strcpy(formp->errmsg,
"400 REQUEST_METHOD is GET, but QUERY_STRING is not set.");
return(1);
}
}
else
{
int content_length, total=0, bytes_read;
content_length= atoi(getenv("CONTENT_LENGTH"));
cgi_query=malloc(content_length+1);
if (!cgi_query)
{
formp->errcond=1;
sprintf(formp->errmsg,
"503 Couldn't allocate %d bytes of memory.",
content_length+1);
return(1);
}
while (total < content_length)
{
bytes_read = read(0, cgi_query+total, content_length);
/* If the client is gone, just exit */
if (bytes_read <= 0)
{
char *progname;
progname=getenv("SCRIPT_NAME");
if (!progname) progname="cgiemail";
if (bytes_read < 0) perror(progname);
else fprintf(stderr, "%s: read() returned 0.\n", progname);
return(1);
}
total += bytes_read;
}
cgi_query[content_length]='\0';
}
}
formp->source = cgi_query;
if (! formp->source)
{
formp->errcond=1;
strcpy(formp->errmsg, "400 Could not read form input.");
return(1);
}
/* decide how much to allocate */
formp->maxstorage = strlen(formp->source)+1;
formp->maxfields = formp->maxstorage / 4 + 1;
/* allocate */
formp->storage = (char *) malloc(formp->maxstorage);
if (!formp->storage)
{
formp->errcond=1;
sprintf(formp->errmsg, "503 Couldn't allocate %d bytes of memory.",
formp->maxstorage);
return(1);
}
formp->fields = (cgi_field *) malloc(formp->maxfields * sizeof(cgi_field));
if (!formp->fields)
{
free(formp->storage);
formp->errcond=1;
sprintf(formp->errmsg, "503 Couldn't allocate %d bytes of memory.",
formp->maxfields * sizeof(cgi_field));
return(1);
}
return(0);
}
void
cgi_free_form(formp)
cgi_form *formp;
{
if (formp)
{
if (formp->storage) free(formp->storage);
if (formp->fields) free(formp->fields);
}
return;
}
/* Get the full pathname of a file parallel to the PATH_TRANSLATED file */
/* Return 0 on success, nonzero on failure. */
int
cgi_parallel_fname(basename, buf, buflen)
char *basename, *buf;
int buflen;
{
int dlen;
char *env_var, *dir_end;
/* Get directory */
env_var = getenv("PATH_TRANSLATED");
if (!env_var)
return 1;
dir_end = strrchr(env_var, '/');
if (!dir_end)
return 1;
dlen = dir_end - env_var + 1;
if (1 + dlen + strlen(basename) > buflen)
return 1;
strncpy(buf, env_var, dlen);
strcpy(buf+dlen, basename);
return 0;
}
/* converts cr/lf pairs or cr by itself to lf, which Unix will handle better */
void
cgi_fix_crlf(string)
char *string;
{
char *ptr;
while ((ptr=strchr(string, '\r')) != NULL)
{
if (*(ptr+1) == '\n') strcpy(ptr, ptr+1);
else *ptr = '\n';
}
return;
}
/* returns 1 if string is filled-in filed, 0 if left blank */
int
cgi_nonblank(string)
char *string;
{
char *ptr;
for(ptr=string; *ptr; ptr++)
if (isgraph((int)(*ptr))) return(1);
return(0);
}
/*
* parsing has 4 states:
* 0: beginning of name
* 1: after beginning of name
* 2: beginning of value
* 3: after beginning of value
*/
int
cgi_parse_form(formp)
cgi_form *formp;
{
char *from_ptr, *to_ptr;
cgi_field *field_ptr;
char hex_string[3];
int parse_state=0;
int hex_int, i;
struct tm *timeptr;
/* Get date-time (RFC 822 sec. 5, RFC 1123 sec. 5.2.14) */
/* FIXME: not compliant if lang is not English or TZ unknown */
time(&received_time);
timeptr=localtime(&received_time);
strftime(datebuf, 79, "%a, %d %b %Y %H:%M:%S %Z", timeptr);
/* initialize variables */
field_ptr=formp->fields;
formp->nfields=0;
hex_string[2]='\0';
/* iterate through all characters in query string */
for (from_ptr=formp->source, to_ptr=formp->storage;
*from_ptr != '\0' && to_ptr - formp->storage < formp->maxstorage;
from_ptr++)
{
if (parse_state==0)
{
/* we're at the 'n' in "name=value&name2=value2..." */
field_ptr->name = to_ptr;
formp->nfields++;
parse_state++;
}
if (parse_state==2)
{
/* we're at the 'v' in "name=value&name2=value2..." */
field_ptr->value = to_ptr;
parse_state++;
}
if (from_ptr[0] == '%' && cgi_ishex((int)from_ptr[1]))
{
/* hex representation like "%2B" in "four=2%2B2" */
hex_string[0] = from_ptr[1];
hex_string[1] = from_ptr[2];
sscanf(hex_string, "%x", &hex_int);
*to_ptr++ = (char)hex_int;
from_ptr += 1 + cgi_ishex((int)from_ptr[2]);
continue;
}
if (from_ptr[0] == '+')
{
*to_ptr++ = ' ';
continue;
}
if (parse_state==1 && from_ptr[0] == '=')
{
/* we're at the '=' in "name=value&name2=value2..." */
*to_ptr++ = '\0';
parse_state++;
continue;
}
if (from_ptr[0] == '&')
{
*to_ptr++ = '\0';
field_ptr++;
if (field_ptr > formp->fields + formp->maxfields)
{
formp->errcond=1;
sprintf(formp->errmsg, "400 Exceeded %d fields.",
formp->maxfields);
return(1);
}
parse_state=0;
continue;
}
/* default */
*to_ptr++ = *from_ptr;
}
/* success */
*to_ptr='\0';
if (parse_state==2) field_ptr->value = to_ptr;
/* fix CR/LF */
for (i=0; i<formp->nfields; i++)
cgi_fix_crlf(formp->fields[i].value);
/* Add "special" output variables */
if (formp->maxfields - formp->nfields > 4)
{
formp->fields[formp->nfields].name = CGI_ERRMSG;
formp->fields[formp->nfields++].value = formp->errmsg;
formp->fields[formp->nfields].name = CGI_ERRINFO;
formp->fields[formp->nfields++].value = formp->errinfo;
formp->fields[formp->nfields].name = CGI_RELEASE;
formp->fields[formp->nfields++].value = CGIEMAIL_RELEASE;
formp->fields[formp->nfields].name = CGI_DATE;
formp->fields[formp->nfields++].value = datebuf;
}
return(0);
}
/* only for debugging purposes */
void
cgi_print_form(formp, outstream)
cgi_form *formp;
FILE *outstream;
{
int i;
fprintf(outstream,
"source=\t%s\nmaxstorage=\t%d\nmaxfields=\t%d\nnfields=\t%d\n",
formp->source, formp->maxstorage, formp->maxfields, formp->nfields);
for (i=0; i<formp->nfields; i++)
printf("Field %s=\t%s\n", formp->fields[i].name, formp->fields[i].value);
return;
}
char *
cgi_value(formp, name)
cgi_form *formp;
char *name;
{
int i;
for (i=0; i<formp->nfields; i++)
{
if (!strcmp(formp->fields[i].name, name))
return(formp->fields[i].value);
}
return("");
}
/* Function to translate an integer character into an HTML entity string */
char *
cgi_char2entity(c)
int c;
{
static char retval[2];
switch (c)
{
case (int)'&':
return("&");
case (int)'<':
return("<");
case (int)'>':
return(">");
case (int)'"':
return(""");
default:
sprintf(retval, "%c", c);
return(retval);
}
}
/*
* Non-alphanumeric chars that don't need %nn encoding in form values.
* See also: http://www.w3.org/Addressing/URL/5_BNF.html
*/
static char url_safe[]="/:$-_@.";
void
cgi_string2url(instring, outstream)
char *instring;
FILE *outstream;
{
while (*instring)
{
if (*instring == ' ')
fputc('+', outstream);
else
{
if (isalnum(*instring) || strchr(url_safe, *instring))
fputc(*instring, outstream);
else
fprintf(outstream, "%%%02x", (int)*instring);
}
instring++;
}
return;
}
void
cgi_string2html(instring, outstream)
char *instring;
FILE *outstream;
{
while (*instring)
fputs(cgi_char2entity(*instring++), outstream);
return;
}
void
cgi_stream2html(instream, outstream)
FILE *instream, *outstream;
{
int c;
while ((c=fgetc(instream)) != EOF)
fputs(cgi_char2entity(c), outstream);
return;
}
/* Output the value(s) of a CGI field name; return number found */
int
cgi_output_value(formp, name, formatstr, outstream)
cgi_form *formp;
char *name;
char *formatstr;
FILE *outstream;
{
int i, nfound=0;
for (i=0; i < formp->nfields; i++)
{
if (!strcmp(formp->fields[i].name, name))
{
if (nfound) fputc(' ', outstream);
if (formatstr)
{
if (!strcmp(formatstr, CGI_HTMLENCODE))
cgi_string2html(formp->fields[i].value, outstream);
else
{
if (!strcmp(formatstr, CGI_URLENCODE))
cgi_string2url(formp->fields[i].value, outstream);
else
fprintf(outstream, formatstr, formp->fields[i].value);
}
}
else
fputs(formp->fields[i].value, outstream);
if (cgi_nonblank(formp->fields[i].value)) nfound++;
}
}
return(nfound);
}
void
cgi_concat_errno(string)
char *string;
{
#if HAVE_STRERROR
strcat(strcat(string, " - "), strerror(errno));
#else
char errstring[16];
sprintf(errstring, " (errno: %d)", errno);
strcat(string, errstring);
#endif
return;
}
int
cgi_template_fill(formp, templatefile)
cgi_form *formp;
char *templatefile;
{
FILE *tfp;
char varname[CGI_VARNAME_MAX];
char formatstr[CGI_VARNAME_MAX];
int varnamelen=0, formatlen=0, nfound=0, substitutions=0;
int inchar, parse_state=0;
#if ENABLE_CGIENV
int cgienv=0;
char *envval;
#endif /* ENABLE_CGIENV */
/* open template file */
tfp = fopen(templatefile, "r");
if (!tfp)
{
formp->errcond=1;
strcpy(formp->errmsg, "500 Could not open template");
cgi_concat_errno(formp->errmsg);
strncpy(formp->errinfo, templatefile, CGI_ERRMSG_MAX);
return(1);
}
/* open temporary file for filled-in template */
if (formp->tmpf) rewind(formp->tmpf);
else formp->tmpf = tmpfile();
if (!formp->tmpf)
{
formp->errcond=1;
strcpy(formp->errmsg, "500 Could not open temporary file.");
/* Try to make the error message more informative */
if (access(P_tmpdir, W_OK))
{
strcpy(formp->errinfo, P_tmpdir);
cgi_concat_errno(formp->errinfo);
}
return(1);
}
while ((inchar=fgetc(tfp)) != EOF
&& isspace(inchar))
{ /* Skip leading whitespace */ }
if (inchar == EOF)
{
formp->errcond=1;
strcpy(formp->errmsg, "500 Empty template file");
strncpy(formp->errinfo, templatefile, CGI_ERRMSG_MAX);
return(1);
}
/*
* parsing states:
* 0: echo sending literally except for CRLF conversion or \, [
* 1: quote sending literally no matter what
* 2: variable reading variable name
* 3: format reading format
*/
do
{
if (parse_state==1) /* quote */
{
fputc(inchar, formp->tmpf);
parse_state=0;
continue;
}
if (parse_state==0) /* echo */
{
/* Convert CR or CRLF to LF */
if (inchar == (int)'\r')
{
fputc('\n', formp->tmpf);
inchar=fgetc(tfp);
if (inchar == (int)'\n') continue;
}
if (inchar == (int)'\\')
{
parse_state=1;
continue;
}
if (inchar == (int)'[')
{
#if ENABLE_CGIENV
cgienv=0;
#endif
parse_state=2;
varnamelen=0;
formatlen=0;
continue;
}
fputc(inchar, formp->tmpf);
continue;
}
if (parse_state==2) /* variable name */
{
if (inchar == (int)'%')
{
formatlen=1;
strcpy(formatstr, "%");
parse_state=3;
continue;
}
#if ENABLE_CGIENV
if (inchar == (int)'$' && varnamelen==0)
{
cgienv=1;
continue;
}
#endif /* ENABLE_CGIENV */
if (inchar == (int)']')
{
varname[varnamelen]='\0';
#if ENABLE_CGIENV
if (cgienv)
{
cgienv=0;
envval=getenv(varname);
if (!envval) envval="";
else substitutions++;
if (formatlen > 0)
fprintf(formp->tmpf, formatstr, envval);
else fputs(envval, formp->tmpf);
}
else
#endif /* ENABLE_CGIENV */
{
if (formatlen > 0)
nfound = cgi_output_value(formp,
varname,
formatstr,
formp->tmpf);
else nfound = cgi_output_value(formp,
varname,
NULL,
formp->tmpf);
if (!nfound && cgi_required(varname))
{
formp->errcond=1;
strcpy(formp->errmsg,
"400 Required field left blank");
strcpy(formp->errinfo, varname);
return(1);
}
substitutions += nfound;
}
parse_state=0;
continue;
}
varname[varnamelen++]=(char) inchar;
if (varnamelen > CGI_VARNAME_MAX)
{
fclose(tfp);
formp->errcond=1;
strcpy(formp->errmsg, "403 Variable Name too long");
strncpy(formp->errinfo, varname, varnamelen);
strcpy(formp->errinfo + varnamelen, "...");
return(1);
}
continue;
}
if (parse_state==3) /* format */
{
if (inchar == (int)',')
{
formatstr[formatlen]='\0';
parse_state=2;
continue;
}
formatstr[formatlen++]=(char) inchar;
if (formatlen > CGI_VARNAME_MAX)
{
fclose(tfp);
formp->errcond=1;
strcpy(formp->errmsg, "403 Format String too long");
strncpy(formp->errinfo, formatstr, formatlen);
strcpy(formp->errinfo + formatlen, "...");
return(1);
}
continue;
}
} while ((inchar=fgetc(tfp)) != EOF);
fclose(tfp);
/* If there were no variable substitutions in this template,
someone may be trying to use cgiemail to get at restricted pages */
if (!substitutions)
{
formp->errcond=1;
strcpy(formp->errmsg, "403 No variable substitutions in template");
strncpy(formp->errinfo, templatefile, CGI_ERRMSG_MAX);
return(1);
}
return 0;
}
/*
* Make sure pclose() can get the exit status of the child process.
* Some HTTP servers block SIGCHLD, causing pclose() to always return -1.
*
* Thank you Guido van Rossum (guido@CNRI.Reston.VA.US)
http://www-db.stanford.edu/~hassan/hymail/pythonlist/python_1995_q4/1317.html
*/
void
cgi_pclose_fix()
{
#ifdef HAVE_SIGPROCMASK
/* POSIX signal mask */
sigset_t mysigmask;
sigemptyset(&mysigmask);
sigaddset(&mysigmask, SIGCHLD);
sigprocmask(SIG_UNBLOCK, &mysigmask, 0);
/* Under Irix 5.3 we can be killed if sendmail died prematurely -brlewis */
sigemptyset(&mysigmask);
sigaddset(&mysigmask, SIGPIPE);
sigprocmask(SIG_BLOCK, &mysigmask, 0);
#else
/* Hopefully this works for old versions of SunOS. We'll see. */
signal(SIGCHLD, SIG_DFL);
#endif
}
int
cgi_mail_template(formp, templatefile)
cgi_form *formp;
char *templatefile;
{
FILE *mypipe, *errorfp;
int retval;
char buf[BUFSIZ], command[BUFSIZ];
int nbytes;
char *envvar, errorfile[L_tmpnam];
int old_stdout, old_stderr, errorfd;
#ifdef ENABLE_OWNER_BOUNCE
char *owner = NULL;
struct passwd *pw;
struct stat st;
/* Get owner of template */
if (0 == stat(templatefile, &st))
{
pw = getpwuid(st.st_uid);
if (pw)
owner = pw->pw_name;
}
#endif /* ENABLE_OWNER_BOUNCE */
/* fill in template */
retval=cgi_template_fill(formp, templatefile);
if (retval) return(retval);
/* Get a temporary file for error messages */
tmpnam(errorfile);
errorfd=open(errorfile, O_WRONLY|O_CREAT|O_EXCL, 0644);
if (errorfd != -1)
{
old_stdout=dup(1);
dup2(errorfd, 1);
old_stderr=dup(2);
dup2(errorfd, 2);
}
/* open pipe to sendmail */
cgi_pclose_fix();
strcat(strcpy(command, PATH_SENDMAIL), " -oi -t");
if (strstr(cgi_value(formp, "cgiemail-mailopt"), "sync"))
strcat(command, " -odi"); /* synchronous delivery */
#ifdef ENABLE_OWNER_BOUNCE
if (owner)
strcat(strcat(command, " -f "), owner); /* envelope set to owner */
#endif /* ENABLE_OWNER_BOUNCE */
mypipe = popen(command, "w");
/* Restore stdout/stderr */
if (errorfd != -1)
{
dup2(old_stdout, 1);
close(old_stdout);
dup2(old_stderr, 2);
close(old_stderr);
close(errorfd);
}
/* Check for failed popen */
if (!mypipe)
{
formp->errcond=1;
strcpy(formp->errmsg, "500 Could not open sendmail pipe");
cgi_concat_errno(formp->errmsg);
strncpy(formp->errinfo, command, CGI_ERRMSG_MAX);
return(1);
}
/* put "Received: from HOST with HTTP;\n\tdate\n" */
/* See RFC 822 appendix D. */
envvar = getenv("REMOTE_HOST");
if (!envvar || !(*envvar)) envvar = getenv("REMOTE_ADDR");
if (envvar && *envvar)
{
fputs("Received: from ", mypipe);
fputs(envvar, mypipe);
envvar=getenv("SERVER_NAME");
if (envvar && *envvar)
{
fputs(" by ", mypipe);
fputs(envvar, mypipe);
}
fputs(" with HTTP;\n\t", mypipe);
fputs(datebuf, mypipe);
fputs("\n", mypipe);
}
#ifdef ENABLE_XHEADERS
/* Add the X-Mailer: header */
fputs("X-Mailer: cgiemail ", mypipe);
fputs(CGIEMAIL_RELEASE, mypipe);
if (NULL != (envvar = getenv("HTTP_REFERER")))
{
fputs("\n\t(form=\"", mypipe);
fputs(getenv("HTTP_REFERER"), mypipe);
fputs("\")", mypipe);
}
/* SCRIPT_NAME is always set; skip envvar check */
fputs("\n\t(action=\"", mypipe);
fputs(getenv("SCRIPT_NAME"), mypipe);
if (NULL != (envvar = getenv("PATH_INFO")))
fputs(getenv("PATH_INFO"), mypipe);
fputs("\")\n", mypipe);
#endif
/* send filled-in template */
rewind(formp->tmpf);
do {
if ((nbytes = fread(buf, sizeof(char), BUFSIZ, formp->tmpf)) > 0)
fwrite(buf, sizeof(char), nbytes, mypipe);
} while (nbytes == BUFSIZ);
/* close */
retval = pclose(mypipe);
/*
* Get exit status. Note that on SunOS 4, WEXITSTATUS(stat)
* only works if &stat is legal, so WEXITSTATUS(pclose(mypipe))
* won't compile.
*/
retval = WEXITSTATUS(retval);
if (retval)
{
/*
* The return value of pclose is not always meaningful.
* However, sendmail usually does not write to stdout/stderr on success.
*/
/* If we can't open the errorfile, something went wrong. */
errorfp = fopen(errorfile, "r");
if (!errorfp)
{
formp->errcond=1;
sprintf(formp->errmsg,
"500 sendmail exit %d - check httpd error logs", retval);
strcpy(formp->errinfo, command);
}
else
{
/* See if there was any error message */
fread(formp->errinfo, sizeof(char), CGI_ERRMSG_MAX, errorfp);
if (cgi_nonblank(formp->errinfo))
{
formp->errcond=1;
sprintf(formp->errmsg, "500 sendmail exit %d with error message",
retval);
}
}
}
unlink(errorfile);
return(formp->errcond);
}
void
cgi_output_failure(formp, msg)
cgi_form *formp;
char *msg;
{
char *failure;
/* maybe use customized failure message */
failure=cgi_value(formp, CGI_FAILURE);
if (failure && *failure)
{
if (strpbrk(failure, ":/"))
{ /* Probably a URL. Redirect. */
cgi_redirect(failure);
return;
}
else
{ /* Probably a template file. Use it. */
char buf[BUFSIZ];
int nbytes;
if (0 == cgi_parallel_fname(failure, buf, BUFSIZ)
&& 0 == cgi_template_fill(formp, buf))
{
rewind(formp->tmpf);
puts("Content-Type: text/html\r\n\r");
do
{
nbytes = fread(buf, sizeof(char), BUFSIZ, formp->tmpf);
if (nbytes)
{
if (!fwrite(buf, sizeof(char), nbytes, stdout))
break;
}
}
while (nbytes == BUFSIZ);
return;
}
}
}
/* use generic failure message */
puts("Content-Type: text/html\r");
printf("Status: %s\r\n\r\n", formp->errmsg);
puts("<HEAD><TITLE>Error</TITLE></HEAD>");
printf("<BODY><H1>Error</H1>%s<P>", msg);
puts("<BLOCKQUOTE><STRONG><SAMP>");
puts(formp->errmsg);
puts("</SAMP></STRONG><P>");
if (*(formp->errinfo))
{
puts("<PRE>");
puts(formp->errinfo);
puts("</PRE>");
}
puts("</BLOCKQUOTE>");
(void) cgi_output_value(formp, CGI_ADDENDUM, NULL, stdout);
puts("<P><EM>cgiemail ");
puts(CGIEMAIL_RELEASE);
puts("</EM></BODY>");
return;
}
/*
* See RFC 2068, section 10.3 for information on how HTTP redirection
* should work. As of 1997, an explicit Status: 303 message doesn't
* do the job. For now, just hope httpd does the right thing with
* Location (see <URL:http://hoohoo.ncsa.uiuc.edu/cgi/out.html>).
*/
void
cgi_redirect(url)
char *url;
{
printf("Location: %s\r\n\r\n", url);
return;
}
void
cgi_output_success(formp, msg)
cgi_form *formp;
char *msg;
{
char *success;
/* maybe use customized success message */
success=cgi_value(formp, CGI_SUCCESS);
if (success && *success)
{
if (strpbrk(success, ":/"))
{ /* Probably a URL. Redirect. */
cgi_redirect(success);
return;
}
else
{ /* Probably a template file. Use it. */
char buf[BUFSIZ];
int nbytes;
if (0 == cgi_parallel_fname(success, buf, BUFSIZ)
&& 0 == cgi_template_fill(formp, buf))
{
rewind(formp->tmpf);
puts("Content-Type: text/html\r\n\r");
do
{
nbytes = fread(buf, sizeof(char), BUFSIZ, formp->tmpf);
if (nbytes)
{
if (!fwrite(buf, sizeof(char), nbytes, stdout))
break;
}
}
while (nbytes == BUFSIZ);
return;
}
else
{
return cgi_output_failure(formp, msg);
}
}
}
/* use generic success message */
puts("Content-Type: text/html\r\n\r");
puts("<HEAD><TITLE>Success</TITLE></HEAD>");
printf("<BODY>%s<P><HR>", msg);
puts("<PRE>");
rewind(formp->tmpf);
cgi_stream2html(formp->tmpf, stdout);
if (*cgi_value(formp, "debug-source"))
{
puts("<HR>");
puts(formp->source);
puts("<HR>");
}
puts("</PRE><P>");
(void) cgi_output_value(formp, CGI_ADDENDUM, NULL, stdout);
puts("<P><EM>cgiemail ");
puts(CGIEMAIL_RELEASE);
puts("</EM></BODY>");
return;
}
int
cgi_standard_email()
{
cgi_form form;
char *template_filename;
template_filename = getenv("PATH_TRANSLATED");
if (!template_filename || !(*template_filename))
{
cgi_redirect(CGI_NOPATH);
return(1);
}
if (cgi_alloc_form(&form) ||
cgi_parse_form(&form) ||
cgi_mail_template(&form, template_filename))
{
cgi_output_failure(&form, "No email was sent due to an error.");
cgi_free_form(&form);
return(1);
}
cgi_output_success(&form, "The following email message was sent.");
cgi_free_form(&form);
return(0);
}
int
cgi_standard_echo()
{
cgi_form form;
char *template_filename;
template_filename = getenv("PATH_TRANSLATED");
if (!template_filename || !(*template_filename))
{
cgi_redirect(CGI_NOPATH);
return(1);
}
if (cgi_alloc_form(&form) ||
cgi_parse_form(&form) ||
cgi_template_fill(&form, template_filename))
{
cgi_output_failure(&form, "Form was not processed due to an error.");
cgi_free_form(&form);
return(1);
}
cgi_output_success(&form, "Processed form looks like this:");
cgi_free_form(&form);
return(0);
}
int
cgi_standard_file()
{
cgi_form form;
char *template_filename, incoming_filename[BUFSIZ];
int retval;
template_filename = getenv("PATH_TRANSLATED");
if (!template_filename || !(*template_filename))
{
cgi_redirect(CGI_NOPATH);
return(1);
}
if (cgi_alloc_form(&form) ||
cgi_parse_form(&form) ||
cgi_template_fill(&form, template_filename))
{
cgi_output_failure(&form, "Form was not processed due to an error.");
cgi_free_form(&form);
return(1);
}
if (0 == cgi_parallel_fname(CGI_INFILE, incoming_filename, BUFSIZ))
{
if (!access(incoming_filename, W_OK))
{
FILE *fp;
char buf[BUFSIZ];
int nbytes;
rewind(form.tmpf);
fp = fopen(incoming_filename, "a");
if (fp)
{
do
{
if ((nbytes = fread(buf, sizeof(char),
BUFSIZ, form.tmpf)) > 0)
fwrite(buf, sizeof(char), nbytes, fp);
} while (nbytes == BUFSIZ);
if (fclose(fp))
{
form.errcond=1;
strcpy(form.errmsg, "500 Write to file failed");
cgi_concat_errno(form.errmsg);
strncpy(form.errinfo, incoming_filename, CGI_ERRMSG_MAX);
}
}
else
{
form.errcond=1;
strcpy(form.errmsg, "500 Could not append file");
cgi_concat_errno(form.errmsg);
strncpy(form.errinfo, incoming_filename, CGI_ERRMSG_MAX);
}
}
else
{
form.errcond=1;
strcpy(form.errmsg, "500 No write access to file");
cgi_concat_errno(form.errmsg);
strncpy(form.errinfo, incoming_filename, CGI_ERRMSG_MAX);
}
}
if (form.errcond)
cgi_output_failure(&form, "Form was not processed due to an error.");
else
cgi_output_success(&form, "The following information was written:");
retval = form.errcond;
cgi_free_form(&form);
return retval;
}
|