/*mailto - a CGI program that substitutes for HTML+'s mailto capability.
Copyright (c) 1994 by Matthew Belmonte
Licence is hereby granted to use, to copy, and to distribute this software,
in its original form or in modified form, provided that such copies and
distributions include this copyright and licence notice in its entirety,
and provided that any additions, modifications, or other features not
present in the original program are clearly noted. No fee may be charged
for a distribution of this program or of any program based upon it, except
for reasonable fees to cover the costs of storage media and handling.
This software is provided 'as is', with no warranty, express or implied,
including but not limited to warranties of merchantability or fitness for a
particular purpose. The user of this software assumes liability for any and
all damages, whether direct or consequential, arising from its use. The
author of this software will not be liable for any such damages.
Compile this program and install the resulting executable code in httpd/cgi-bin.
Reference it as http://HOSTNAME/cgi-bin/mailto/ADDRESS, where HOSTNAME is the
name of the machine on which httpd is running and ADDRESS is the recipient's
address. In other words, your input form should look something like this:
*/
#include
#include
#include
#define MAX_ADDR_LEN 512 /*maximum length of recipient's address*/
static int length;
static FILE *sendmail;
/*convert a single hexademical digit to an integer*/
int hex(c)
int c;
{
return(c <= '9'? c-'0': c <= 'F'? c-'A'+10: c-'a'+10);
}
/*copy and decode an HTML-encoded field from stdin to the outgoing message*/
void copy_text()
{
int c;
while((length-- > 0) && ((c=getchar()) != '&'))
{
if(c == '+')
putc(' ', sendmail);
else if(c == '%')
{
c = 16*hex(getchar());
putc(c+hex(getchar()), sendmail);
length -= 2;
}
else
putc(c, sendmail);
}
}
/*doesn't parse the address, but does verify that it doesn't contain any weird
characters and won't overrun the buffer*/
int is_legal_addr(addr)
char *addr;
{
register int i;
for(i = 0; ((i <= MAX_ADDR_LEN)
&& (addr[i] >= 33) && (addr[i] <= 122)
&& (addr[i] != '"')
&& (addr[i] != '&')
&& (addr[i] != '\'')
&& (addr[i] != '(') && (addr[i] != ')')
&& (addr[i] != '*')
&& (addr[i] != ';')
&& (addr[i] != '<')
&& (addr[i] != '>')
&& (addr[i] != '?')
&& (addr[i] != '`')
&& (addr[i] != '[') && (addr[i] != '\\') && (addr[i] != ']')
&& (addr[i] != '^')); i++)
;
return(addr[i] == '\0');
}
void main()
{
int c;
char *recipient;
char sendmail_cmd[20+MAX_ADDR_LEN];
printf("Content-Type: text/plain\n\n");
length = atoi(getenv("CONTENT_LENGTH"));
recipient = getenv("PATH_INFO");
if((recipient == NULL) || (*recipient == '\0'))
printf("Internal error: no recipient specified in CGI path.\n");
else if(!is_legal_addr(++recipient))
printf("Error: malformed address.\n");
else
{
sprintf(sendmail_cmd, "/usr/lib/sendmail %s", recipient);
if((sendmail = popen(sendmail_cmd, "w")) == NULL)
printf("ERROR %d - unable to deliver your message.\n", errno);
else
{
scanf("address="); length -= 8; /*From*/
fprintf(sendmail, "From: ");
copy_text();
fprintf(sendmail, "\nTo: %s\n", recipient); /*To*/
c = getchar(); ungetc(c, stdin); /*optional Subject*/
if(c=='s')
{
scanf("subject="); length -= 8;
fprintf(sendmail, "Subject: ");
copy_text();
putc('\n', sendmail);
}
putc('\n', sendmail); /*end of header*/
scanf("message="); length -= 8; /*message body*/
copy_text();
fprintf(sendmail, "\n.\n");
pclose(sendmail);
printf("Your message has been sent.\n");
}
}
exit(0);
}