Remove old CGI

This commit is contained in:
Neale Pickett 2014-02-18 22:46:04 +00:00
parent f758de8d17
commit 8fff572213
6 changed files with 1 additions and 640 deletions

View File

@ -1,25 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
const char *basepath = "/usr/share/IlohaMail/source";
int
main(int argc, char *argv[])
{
char *pathinfo = getenv("PATH_INFO");
char filename[512];
if ((! pathinfo) ||
(! strcmp(pathinfo, "/")) ||
(0 == strcmp(pathinfo, "/index.html"))) {
pathinfo = "/index.php";
}
snprintf(filename, sizeof filename, "%s%s", basepath, pathinfo);
setenv("SCRIPT_FILENAME", filename, 1);
setenv("REDIRECT_STATUS", "fuck me", 1);
execl("/usr/bin/php-cgi", filename, NULL);
return 0;
}

376
l.cgi.c
View File

@ -1,376 +0,0 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <values.h>
#include <time.h>
#include <glob.h>
const char *BASE_DIR = "/tmp/clicko";
const char *BASE_URL = "http://woozle.org/l.cgi";
#define POST_MAX 1024
/*
* CGI
*/
static int is_cgi = 0;
static char **argv = NULL;
static int
read_char_argv()
{
static int arg = 0;
static char *p;
if (NULL == argv) {
return EOF;
}
if (0 == arg) {
arg = 1;
p = argv[1];
}
if (! p) {
return EOF;
} else if (! *p) {
arg += 1;
p = argv[arg];
return '&';
}
return *(p++);
}
static int
read_char_stdin()
{
static int inlen = -1;
if (-1 == inlen) {
char *p = getenv("CONTENT_LENGTH");
if (p) {
inlen = atoi(p);
if (inlen > POST_MAX) {
inlen = POST_MAX;
}
if (inlen < 0) {
inlen = 0;
}
} else {
inlen = 0;
}
}
if (inlen) {
inlen -= 1;
return getchar();
}
return EOF;
}
static int
read_char_query_string()
{
static char *p = (char *)-1;
if ((char *)-1 == p) {
p = getenv("QUERY_STRING");
}
if (! p) {
return EOF;
} else if (! *p) {
return EOF;
} else {
return *(p++);
}
}
static int (* read_char)() = read_char_argv;
int
cgi_init(char *global_argv[])
{
char *rm = getenv("REQUEST_METHOD");
if (! rm) {
read_char = read_char_argv;
argv = global_argv;
} else if (0 == strcmp(rm, "POST")) {
read_char = read_char_stdin;
is_cgi = 1;
} else if (0 == strcmp(rm, "GET")) {
read_char = read_char_query_string;
is_cgi = 1;
} else {
printf(("405 Method not allowed\r\n"
"Allow: GET, POST\r\n"
"Content-type: text/plain\r\n"
"\r\n"
"%s is not allowed.\n"),
rm);
return -1;
}
return 0;
}
static char
tonum(int c)
{
if ((c >= '0') && (c <= '9')) {
return c - '0';
}
if ((c >= 'a') && (c <= 'f')) {
return 10 + c - 'a';
}
if ((c >= 'A') && (c <= 'F')) {
return 10 + c - 'A';
}
return 0;
}
static char
read_hex()
{
int a = read_char();
int b = read_char();
return tonum(a)*16 + tonum(b);
}
/* Read a key or a value. Since & and = aren't supposed to appear
outside of boundaries, we can use the same function for both.
*/
size_t
cgi_item(char *str, size_t maxlen)
{
int c;
size_t pos = 0;
while (1) {
c = read_char();
switch (c) {
case EOF:
case '=':
case '&':
str[pos] = '\0';
return pos;
case '%':
c = read_hex();
break;
case '+':
c = ' ';
break;
}
if (pos < maxlen - 1) {
str[pos] = c;
pos += 1;
}
}
}
void
cgi_head(char *title)
{
if (is_cgi) {
printf("Content-type: text/html\r\n\r\n");
}
printf(("<!DOCTYPE html>\n"
"<html>\n"
" <head><title>%s</title></head>\n"
" <body>\n"
" <h1>%s</h1>\n"),
title, title);
}
void
cgi_foot()
{
printf("\n"
" </body>\n"
"</html>\n");
}
void
cgi_result(int code, char *desc, char *fmt, ...)
{
va_list ap;
if (is_cgi) {
printf("%d %s\r\n", code, desc);
}
cgi_head(desc);
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
cgi_foot();
exit(0);
}
void
cgi_page(char *title, char *fmt, ...)
{
va_list ap;
cgi_head(title);
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
cgi_foot();
exit(0);
}
void
cgi_error(char *text)
{
cgi_result(500, "Internal error", "<p>%s</p>", text);
}
static FILE *
open_file(char *shorty, char *mode)
{
char fn[256];
FILE *f;
snprintf(fn, sizeof fn, "%s/%s.url", BASE_DIR, shorty);
f = fopen(fn, mode);
if (! f) {
cgi_error("Unable to open database");
}
return f;
}
void
shorten_url()
{
char u[4096];
size_t ulen;
char shorty[16];
ulen = cgi_item(u, sizeof u);
if (ulen == 0) {
cgi_error("No URL specified");
}
snprintf(shorty, sizeof shorty, "%08x.%04x", (unsigned int)time(NULL), getpid());
/* Put the URL into a file */
{
FILE *f = open_file(shorty, "w");
fprintf(f, "%s", u);
fclose(f);
}
/* Report back */
printf("Content-type: text/plain\r\n\r\n");
printf("%s/%s\n", BASE_URL, shorty);
}
void
redirect(char *pi)
{
int i;
if (! pi) {
cgi_error("No short URL provided");
}
if (pi[0] != '/') {
cgi_error("Invalid PATH_INFO");
}
pi += 1;
for (i = 0; pi[i]; i += 1) {
if ((! isalnum(pi[i])) &&
(pi[i] != '.')) {
cgi_error("Bad short URL");
}
}
/* Open file */
{
char u[4096];
FILE *f = open_file(pi, "r");
fgets(u, sizeof u, f);
fclose(f);
printf("Location: %s\r\n", u);
}
}
void
list_links()
{
char g[256];
glob_t globbuf;
int i;
snprintf(g, sizeof g, "%s/*.url", BASE_DIR);
glob(g, 0, NULL, &globbuf);
cgi_head("Clicko history");
printf("<ul>\n");
for (i = globbuf.gl_pathc - 1; i >= 0; i -= 1) {
FILE *f = fopen(globbuf.gl_pathv[i], "r");
char url[4096];
fgets(url, sizeof url, f);
fclose(f);
printf("<li><a href=\"%s\">%s</a></li>\n", url, url);
}
printf("</ul>\n");
cgi_foot();
}
int
main(int argc, char *argv[])
{
if (-1 == cgi_init(argv)) {
fprintf(stderr, "Unable to initialize CGI.\n");
return -1;
}
while (1) {
char key[12];
size_t klen;
klen = cgi_item(key, sizeof key);
if (klen == 0) {
break;
}
switch (key[0]) {
case 'u':
shorten_url();
return 0;
}
}
{
char *pi = getenv("PATH_INFO");
if (pi) {
redirect(pi);
}
}
list_links();
return 0;
}

104
lists.cgi
View File

@ -1,104 +0,0 @@
#! /usr/bin/python
import cgitb; cgitb.enable()
import cgi
import os
import sys
import glob
import re
import smtplib
f = cgi.FieldStorage()
l = f.getfirst('l')
if not l:
l = os.environ.get('PATH_INFO', '/')[1:]
desc_re = re.compile(r'List-Id: "?([^<]*)"? <')
def getdesc(d):
fn = '%s/control/customheaders' % d
try:
hdrs = file(fn).read()
except IOError:
return '(none)'
ret = desc_re.search(hdrs)
if ret:
return cgi.escape(ret.group(1)).replace('#', '')
else:
return '(none)'
listdir = '/var/spool/mlmmj/%s' % l
if l and os.path.isdir(os.path.join(listdir, 'control')):
title = '%s membership' % l
a = f.getfirst('a')
addr = f.getfirst('addr')
if ((a == 'subscribe') or (a == 'unsubscribe')) and addr:
server = smtplib.SMTP('localhost')
faddr = addr
taddr = '%s-%s@woozle.org' % (l, a)
try:
server.sendmail(faddr,
[taddr],
'From: %s\nTo: %s\n\n' % (faddr, taddr))
content = '<p>I have sent a confirmation message to <tt>%s</tt>. ' % addr
content += 'It should be in your mailbox shortly.</p>'
except Exception, err:
content = "<p>Uh oh. That didn't work.</p>"
content += "<pre>%s</pre>" % cgi.escape(str(err))
else:
content = '<h2>%s@woozle.org' % l
content += ": %s" % getdesc(listdir)
content += '</h2>'
content += '<p>To subscribe to or unsubscribe from the %s list,' % l
content += ' just enter your email address in this handy dandy form!</p>'
content += '<form method="post" action="/lists.cgi">'
content += ' <input type="hidden" name="l" value="%s" />' % l
content += ' Email address: <input name="addr" />'
content += ' <input type="submit" name="a" value="subscribe" />'
content += ' <input type="submit" name="a" value="unsubscribe" />'
content += '</form>'
if os.path.exists(os.path.join(listdir, 'control', 'archive')):
content += " <p><a href='http://woozle.org/list-archives/%s/threads.html'>Message archive</a></p>" % l
else:
title = 'Email lists'
content = '<h2>Public email lists on this host</h2>'
content += '<table>'
content += ' <thead>'
content += ' <tr>'
content += ' <th>list</th>'
content += ' <th>description</th>'
content += ' <th>actions</th>'
content += ' </tr>'
content += ' </thead>'
content += ' <tbody>'
for d in sorted(glob.glob('/var/spool/mlmmj/*')):
if os.path.islink(d):
continue
if os.path.exists('%s/control/private' % d):
continue
l = os.path.basename(d)
content += '<tr><td><a href="/lists.cgi/%s">%s</a></td>' % (l, l)
content += '<td>%s</td>' % getdesc(d)
content += '<td><form action="/lists.cgi">'
content += ' <input name="addr" />'
content += ' <input type="hidden" name="l" value="%s" /><br />' % l
content += ' <input type="submit" name="a" value="subscribe" />'
content += ' <input type="submit" name="a" value="unsubscribe" />'
content += '</form>'
if os.path.exists('%s/control/archive' % d):
content += '<a href="http://woozle.org/list-archives/%s/threads.html">view&nbsp;archive</a>' % l
content += '</td></tr>\n'
content += ' </tbody>'
content += '</table>'
print 'Content-type: text/html'
print
sys.stdout.flush()
p = os.popen('m4 -DTITLE="%s" template.html.m4 -' % title, 'w')
p.write(content)
p.close()

View File

@ -1,76 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
const char *baseurl = "https://woozle.org/mail.cgi/";
const char *basepath = "/opt/roundcubemail";
//const char *basepath = "/usr/share/IlohaMail/source";
int
main(int argc, char *argv[])
{
char *pathinfo = getenv("PATH_INFO");
char *remaddr = getenv("REMOTE_ADDR");
size_t pathlen = pathinfo?strlen(pathinfo):0;
char filename[512];
char *ext;
if ((! pathinfo) || (! remaddr) || (! getenv("HTTPS"))) {
printf("Location: %s\n", baseurl);
return 0;
} else if (0 == strcmp(pathinfo, "/index.html")) {
snprintf(filename, sizeof filename, "%s/index.php", basepath);
} else if (pathinfo[pathlen-1] == '/') {
snprintf(filename, sizeof filename, "%s%sindex.php", basepath, pathinfo);
} else {
snprintf(filename, sizeof filename, "%s%s", basepath, pathinfo);
}
ext = strrchr(filename, '.');
if (! ext) {
ext = "";
}
if (0 == strcmp(ext, ".php")) {
setenv("SCRIPT_FILENAME", filename, 1);
setenv("REDIRECT_STATUS", "fuck me", 1);
execl("/usr/bin/php-cgi", filename, NULL);
} else if (strstr(filename, "/config/") ||
strstr(filename, "/logs/") ||
strstr(filename, "/temp/")) {
printf("Content-type: text/plain\n\n[MESSAGE REDACTED]\n");
} else {
FILE *f = fopen(filename, "r");
char *ct = "application/octet-stream";
if (0 == strcmp(ext, ".css")) {
ct = "text/css";
} else if (0 == strcmp(ext, ".html")) {
ct = "text/html";
} else if (0 == strcmp(ext, ".js")) {
ct = "application/javascript";
} else if (0 == strcmp(ext, ".png")) {
ct = "image/png";
} else if (0 == strcmp(ext, ".jpg")) {
ct = "image/jpeg";
} else if (0 == strcmp(ext, ".gif")) {
ct = "image/gif";
}
printf("Content-type: %s\n\n", ct);
while (! feof(f)) {
char buf[4096];
size_t len;
len = fread(buf, 1, sizeof buf, f);
if (len) {
fwrite(buf, 1, len, stdout);
}
}
}
return 0;
}

View File

@ -1,57 +0,0 @@
#! /usr/bin/python
import cgitb; cgitb.enable()
import cgi
import glob
import os
import sys
import markdown
BASE = '/var/lib/wishlists'
f = cgi.FieldStorage()
title = 'Wishlists'
content = []
u = f.getfirst('u')
p = f.getfirst('p')
if u:
if p:
if p.lower() not in ('dingo', 'jada'):
content.append("<p>I'm sorry but that is not the right answer.</p>")
else:
txt = f.getfirst('txt')
open(os.path.join(BASE, u), 'w').write(txt)
content.append('<p>Okay, thanks!</p>')
content.append('<p><a href="wishlist.cgi">Back to wishlists</a></p>')
else:
title = "%s's Wishlist" % cgi.escape(u)
txt = open(os.path.join(BASE, u)).read()
content.append('<form action="wishlist.cgi" method="post" accept-charset="utf-8">')
content.append('<input type="hidden" name="u" value="%s">' % u)
content.append('<textarea name="txt" rows="15" cols="60">%s</textarea>' % cgi.escape(txt))
content.append('<br>')
content.append('What is the name of Amy and Neale\'s dog?')
content.append('<input name="p">')
content.append('<input type="submit" value="update">')
content.append('</form>')
content.append('<p>Formatting overview:</p>')
content.append('<pre>* Item')
content.append('* Second item')
content.append('* Item with [a link](http://example.com/)')
content.append('* &lt;del&gt;A spoken-for item&lt;/del&gt;</pre>')
else:
for fn in sorted(glob.glob(os.path.join(BASE, '*'))):
u = os.path.basename(fn)
content.append("<h1>%s</h1>" % cgi.escape(u))
content.append(markdown.markdown(open(fn).read()))
content.append('<p><a href="wishlist.cgi?u=%s">edit</a></p>' % u)
print 'Content-type: text/html'
print
sys.stdout.flush()
p = os.popen('m4 -DTITLE="%s" template.html.m4 -' % title, 'w')
p.write('\n'.join(content))
p.close()

View File

@ -1,6 +1,5 @@
PLAIN += .
COPY += icon.png style.css style-black.css lists.cgi wishlist.cgi set.cgi $(TEMPLATE)
COPY += mail.cgi ilohamail.cgi l.cgi
COPY += icon.png style.css style-black.css set.cgi $(TEMPLATE)
COPY += google7f698b9893809122.html
HTML += people.html
COPY += robots.txt