woozle.org/mail.cgi.c

77 lines
2.2 KiB
C
Raw Normal View History

2012-02-04 10:16:01 -07:00
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
2012-10-09 14:55:35 -06:00
const char *baseurl = "https://woozle.org/mail.cgi/";
const char *basepath = "/opt/roundcubemail";
2013-11-07 14:18:49 -07:00
//const char *basepath = "/usr/share/IlohaMail/source";
2012-02-04 10:16:01 -07:00
int
main(int argc, char *argv[])
{
char *pathinfo = getenv("PATH_INFO");
2012-10-09 14:55:35 -06:00
char *remaddr = getenv("REMOTE_ADDR");
size_t pathlen = pathinfo?strlen(pathinfo):0;
2012-02-04 10:16:01 -07:00
char filename[512];
2012-10-09 14:55:35 -06:00
char *ext;
2012-02-04 10:16:01 -07:00
2012-12-08 16:04:44 -07:00
if ((! pathinfo) || (! remaddr) || (! getenv("HTTPS"))) {
printf("Location: %s\n", baseurl);
2012-10-09 14:55:35 -06:00
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);
2012-02-04 10:16:01 -07:00
}
2012-10-09 14:55:35 -06:00
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);
2012-10-09 14:55:35 -06:00
} 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);
}
}
}
2012-02-04 10:16:01 -07:00
return 0;
}