#include #include #include #include #define BUFSIZE 8192 int main(int ac, char *av[]) { int fd; char *iaddr, cmd[100]; char *filename; iaddr = getenv("REMOTE_ADDR"); if (convert_html(av[1]) == 0) { fprintf(stdout, "Your empty message from '%s' " "has not been delivered.", iaddr); exit(1); } if (has_email(av[1]) == 0) { fprintf(stdout, "Your message from '%s', " "without an email address, has not been delivered.", iaddr); exit(1); } filename = tempnam("/tmp", "MAILW"); fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, 0644); write(fd, av[1], strlen(av[1])); close (fd); sprintf(cmd, "Mail johna -s 'web form' <%s\n", filename); system(cmd); unlink(filename); fprintf(stdout, "Your message has been delivered."); } uint tobin(char c) { if ((c >= '0') && (c <= '9')) return c - '0'; c = toupper(c); return c - 'A' + 10; } int convert_html(char *str) { int chars=0; char *p, *q; uint x; p = &str[5]; // skip over 'body=' q = str; while (*p) { chars++; switch (*p) { case '%': x = (tobin(p[1]) << 4) | tobin(p[2]); *q++ = x; p += 3; break; case '+': *q++ = ' '; p++; break; default: *q++ = *p++; break; } } *q = 0; return chars; } int has_email(char *str) { char *p, *q; p = strchr(str, '@'); if ((p == NULL) || (p == str)) return 0; q = strchr(p++, '.'); if ((q == NULL) || (q == p)) return 0; return 1; // valid email? (x@y.z) }