summaryrefslogtreecommitdiff
path: root/tools/skrewt.c
diff options
context:
space:
mode:
Diffstat (limited to 'tools/skrewt.c')
-rw-r--r--tools/skrewt.c158
1 files changed, 87 insertions, 71 deletions
diff --git a/tools/skrewt.c b/tools/skrewt.c
index d2e1bbc..6749a01 100644
--- a/tools/skrewt.c
+++ b/tools/skrewt.c
@@ -1,4 +1,4 @@
-///////////////////
+//////////////////
// skrewt.c
//
// scrutinize email
@@ -13,6 +13,7 @@
#include <stdio.h> /* perror */
#include <sstream>
#include <vector>
+#include <list>
using namespace std;
@@ -28,6 +29,7 @@ void usage(const int sts){
" Options\n"
" -help print this msg (and exit immediately).\n"
" -maxsize ii msg size in bytes; anything bigger will be rejected.\n"
+" -error-exit exit early if errors have been detected.\n"
"\n"
" Messages containing the string '-please-bounce-this-' will be rejected.\n"
" Messages with no date will be rejected.\n"
@@ -35,20 +37,8 @@ void usage(const int sts){
exit(sts);
}
-// error exit codes, mostly as stated in qmail.c
-#define bar \
-foo(good, 0) ;\
-foo(spam, 21) ;\
-foo(permerr, 31) ;\
-foo(usage, 39) ;\
-foo(greylisting, 70) ;\
-foo(syserr, 71) ;\
-foo(comerr, 74) ;
-
-#define foo(name, num) const int ex_ ## name = num
-bar
-#undef foo
-
+#include "qq_exit_codes.h"
+#include "utils.h"
/////////////////////////////////////////////////////////
// Case insensitive comparison of strings
@@ -93,31 +83,19 @@ int cmp_casefold(const std::string& a, const std::string& b) {
return 0;
}
-
-string toLower(const std::string& a){
- string rslt = a;
- string::iterator rr;
- for (rr = rslt.begin(); rr != rslt.end(); rr++){
- *rr = tolower(*rr);
+string noCR(const string bar){
+ string foo(bar);
+ int len = foo.length();
+ if (len){
+ if (foo[len-1] == '\r') {
+ foo.erase(len-1);
+ }
}
- return rslt;
+ return foo;
}
-////////////////
-string ltrim(string foo){
- size_t where = foo.find_first_not_of(" \t\r\n");
- if (where == foo.npos) return foo;
- return foo.substr(where);
-}
-
-////////////////
-// little utility to help with argument parsing:
-//
-int prefix(const string shorter, const string longer){
- return shorter == longer.substr(0, shorter.length());
-}
-
-void exeunt(const int sts){
+void maybe_exeunt(const int sts, const int really){
+ if (!really) return;
if (sts == ex_good) exit(sts);
const char* foo = getenv("HI_Q_GROUP");
@@ -136,10 +114,8 @@ void exeunt(const int sts){
exit(sts);
}
-string basename(const string path){
- size_t where = path.rfind("/");
- if (where != string::npos) return path.substr(1+where);
- return path;
+void exeunt(const int sts){
+ maybe_exeunt(sts, 1);
}
string progname, progid;
@@ -149,7 +125,7 @@ int mypid;
/* Content-Type: multipart/mixed; boundary="1170861315-1262462055-1341954763=:92165" */
//
void parse_content(const string type_spec_line, string &maintype, string &boundary) {
- cerr << "parser called with: " << type_spec_line << endl;
+ //xxx cerr << "parser called with: " << type_spec_line << endl;
string get_type(type_spec_line);
size_t where = get_type.find_first_of(" \t;\n");
@@ -192,6 +168,15 @@ void parse_content(const string type_spec_line, string &maintype, string &bounda
}
}
+string join(const string sep, const list<string> stuff){
+ string rslt;
+ for (list<string>::const_iterator ptr = stuff.begin();
+ ptr != stuff.end(); ptr++){
+ if (rslt.length()) rslt += sep;
+ rslt += *ptr;
+ }
+ return rslt;
+}
////////////////////////////////////////////////////////////
int main(int _argc, const char** _argv){
@@ -209,6 +194,8 @@ int main(int _argc, const char** _argv){
}
int maxsize(1000*1000);
+ int error_exit(0);
+ int mid_required(0);
while (argc) {
string arg(*argv); argv++; argc--;
@@ -216,14 +203,18 @@ int main(int _argc, const char** _argv){
if (prefix(arg, "-help")) {
usage(0);
}
- if (prefix(arg, "-maxsize")) {
+ if (0) {
+ } else if (prefix(arg, "-mid-required")) {
+ mid_required++;
+ } else if (prefix(arg, "-error-exit")) {
+ error_exit++;
+ } else if (prefix(arg, "-maxsize")) {
if (!argc) {
cerr << "Option -maxsize requires an argument" << endl;
exit(ex_usage);
}
maxsize = atoi(*argv); argv++; argc--;
- }
- if (arg.substr(0,1) == "-") {
+ } else if (arg.substr(0,1) == "-") {
cerr << "Unrecognized option '" << arg << "'" << endl;
cerr << "For help, try: " << progname << " -help" << endl;
exit(ex_usage);
@@ -236,28 +227,32 @@ int main(int _argc, const char** _argv){
int saw_blank_line(0);
string boundary("x-xx-x");
- string date;
+ string to;
+ string from;
string subject;
- string content_type;
+ string date;
string message_id;
+ string content_type;
int msgsize(0);
vector<string> bigbuf;
- cerr << "hi there" << endl;
+ int recno(0);
+ //xxxx cerr << progid << " begins" << endl;
for (;;){ // outer loop over all records in the header
if (cin.eof()) break;
if (cin.bad()) return 1;
- string headrec;
+ string line;
// on fail, go back to top of outer loop and check for eof versus bad
- if (getline(cin, headrec).fail()) continue;
- msgsize += headrec.length()+1;
+ if (getline(cin, line).fail()) continue;
+ msgsize += line.length()+1;
if (msgsize > maxsize) {
cerr << progid << " rejection: bigger than " << maxsize << endl;
exeunt(ex_spam);
}
- cout << headrec << endl;
- bigbuf.push_back(headrec); // for a folded record, this is the first line
+ cout << line << endl;
+ bigbuf.push_back(line);
+ string headrec = noCR(line); // for a folded record, this is the first line
for (;;) { // inner loop to build a multi-line record e.g. folded record:
if (cin.eof()) break;
@@ -276,16 +271,11 @@ int main(int _argc, const char** _argv){
}
cout << line << endl;
bigbuf.push_back(line);
- string cooked(line);
- if (cooked.length()){
- string::iterator ptr = cooked.end()-1;
- if (*ptr == '\r') cooked.erase(ptr);
- }
- headrec += "\n" + cooked;
+ headrec += "\n" + noCR(line);
}
// here with a fully assembled header record
+// headrec (unlike line) contains no DOS CR characters
int len = headrec.length();
- if (len && headrec[len-1] == '\r') len--; // reduced length, not counting <cr>
if (len == 0) {
saw_blank_line = 1;
break; // no more headers in this message
@@ -301,6 +291,12 @@ int main(int _argc, const char** _argv){
}
headword = toLower(headword);
if (0){
+ } else if (headword == "from") {
+ from = rest;
+ } else if (headword == "to") {
+ to = rest;
+ } else if (headword == "message-id") {
+ message_id = rest;
} else if (headword == "date") {
date = rest;
} else if (headword == "subject") {
@@ -309,30 +305,50 @@ int main(int _argc, const char** _argv){
content_type = rest;
}
//xxxx cout << headrec.length() << " ... ";
+ recno++;
+ if (0) if (recno <= 6) cerr << progid << "#" << recno
+ << " " << headrec << endl;
}
- cerr << "headers are done. Delimited: " << saw_blank_line << endl;
+ if (saw_blank_line) {/* ignore */}
+ cerr << progid <<" Mid '" << message_id << "'" << endl;
// Headers are done.
// Do some early-stage thinking.
+ list<string> badnews;
+
if (subject.find("-please-bounce-this-") != string::npos) {
- cerr << progid << " rejection: by request" << endl;
- exeunt(ex_spam);
+ badnews.push_back("by request");
}
if (!date.length()) {
- cerr << progid << " rejection: no date" << endl;
- exeunt(ex_spam); // disallow mail with no date
+ badnews.push_back("no date");
+ }
+
+ if (mid_required && !message_id.length()) {
+ badnews.push_back("no message-id");
+ }
+
+ if (badnews.size()){
+ cerr << progid << " " << join(", ", badnews) << endl;
+ if (error_exit){
+ cerr << progid << " '" << from
+ << "' to '" << to
+ << "'" << endl;
+ exeunt(ex_spam);
+ }
}
string main_contype;
- parse_content(content_type, main_contype, boundary);
+ if (content_type.length())
+ parse_content(content_type, main_contype, boundary);
+// some slightly-useful booleans:
int currently_text = main_contype == "text";
int main_multipart = main_contype == "multipart";
// early-stage thinking has been done.
// Now spew the rest of the message
- cerr << "body begins: " << main_contype << " " << currently_text << " " << boundary << endl;
+ //xxxx cerr << "body begins: " << main_contype << " " << currently_text << " " << boundary << endl;
int in_subheads(0);
int textlines(0);
@@ -345,7 +361,7 @@ int main(int _argc, const char** _argv){
msgsize += line.length()+1;
if (msgsize > maxsize) {
cerr << progid << " rejection: bigger than " << maxsize << endl;
- exeunt(ex_spam);
+ maybe_exeunt(ex_spam, error_exit);
}
bigbuf.push_back(line);
cout << line << endl;
@@ -368,7 +384,7 @@ int main(int _argc, const char** _argv){
if (headword == "content-type") {
parse_content(rest, sub_contype, junk);
currently_text = sub_contype == "text";
- cerr << "setting contype '" << sub_contype << "' " << currently_text << " ... " << textlines << endl;
+ //xxxx cerr << "setting contype '" << sub_contype << "' " << currently_text << " ... " << textlines << endl;
}
} else {
if (main_multipart && line == "--" + boundary) {
@@ -380,10 +396,10 @@ int main(int _argc, const char** _argv){
}
}
- if (1) cerr << "textlines: " << textlines << endl;
+ if (0) cerr << "textlines: " << textlines << endl;
if (1 && !textlines) {
cerr << progid << " rejection: no text" << endl;
-// exeunt(ex_spam);
+// maybe_exeunt(ex_spam, error_exit);
}
cerr << progid << " normal completion" << endl;
exit(ex_good);