summaryrefslogtreecommitdiff
path: root/tools/ltgrey.c
blob: be947faf630ec3bc3f5619f5c4e500e79f263eb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <iostream>
#include <stdlib.h>     /* for exit(), atoi() */

#include <sys/types.h>          /* for getpid() */
#include <unistd.h>

#include "libltgrey.h"
#include "qq_exit_codes.h"
#include "utils.h"
#include <list>

using namespace std;
pid_t mypid;
string progname;

#define exeunt exit

void usage(const string parent_dir){
  cout <<
"Usage:  ltgrey [options]\n"
"\n"
"Options include\n"
"  -set40 mid   # start quarantine for given message-id\n"
"  -get40 mid   # look up the quarantine status for the given mID\n"
"  -scan40      # scan the quarantine directory\n"
"\n"
"  -setrep dom  # set the reputation for the given domain\n"
"  -getrep dom  # look up the reputation for the given domain\n"
"  -scanrep     # scan the reputation directory\n"
"\n"
"  -copy        # increment the 'copies' variable\n"
"  -dns_mode    # enable DNS checks\n"
"  -verbose     # increase the verbosity\n"
"  -shift ???   # no idea\n"
"  -stain ???   # not implemented\n"
"  -suffix ???  # no idea\n"
"  -help        # print this msg (and exit immediately)\n"
"\n"
;

  cout << "parent dir is " << parent_dir << endl;
  cout << "box_40.dir is "  << box_40.dir << endl;
  cout << "box_rep.dir is " << box_rep.dir << endl;

  cout <<
"\n"
"Principles of operation:\n"
"\n"
"1) Quarantine applies to a particular message.  Messages\n"
" are identified by their MID i.e. message-ID.\n"
"\n"
"2a) Ideally, reputation applies to a sending-domain.\n"
"\n"
"2b) If we can't identify a domain that will take responsibility\n"
"  for sending the message, then reputation applies to the host.\n"
;
  return;
}

int main(int _argc, char** _argv){

  mypid = getpid();
  int argc(_argc);
  char** argv(_argv);
  const string parent_dir("/var/qmail/ltgrey");
  whatsit foo(argv[0], parent_dir); argc--; argv++;

  if (foo.setup()) return ex_syserr;

  int rslt;
  rslt = foo.maybe_mkdir(".", "parent dir");
  if (rslt) return rslt;
  rslt = foo.maybe_mkdir(box_40.dir, "quarantine dir");
  if (rslt) return rslt;
  rslt = foo.maybe_mkdir(box_rep.dir,   "reputation dir");
  if (rslt) return rslt;

  int scan40mode(0);
  int scanrepmode(0);
  int copies(1);
  int shift(0);
  int stain(0);
  int dns_mode(0);
  string get_40_mid;
  string set_40_mid;
  string get_rep_domain;
  string set_rep_domain;
  argParser ARGS(argc, argv);

  try {while (ARGS.size()) {
    ARGS.next();
    if (0){
    } else if (ARGS.prefix("-help")) {
      usage(foo.parent_dir);
      exit(0);
    } else if (ARGS.prefix("-scan40")) {
      scan40mode++;
    } else if (ARGS.prefix("-scanrep")) {
      scanrepmode++;
    } else if (ARGS.prefix("-copy")) {
      copies++;
    } else if (ARGS.prefix("-verbose")) {
      foo.verbosity++;
    } else if (ARGS.prefix("-dns_mode")) {
      dns_mode++;
    } else if (ARGS.prefix("-get40", 1)) {
      get_40_mid = ARGS.shift();
    } else if (ARGS.prefix("-set40", 1)) {
      set_40_mid = ARGS.shift();
    } else if (ARGS.prefix("-setrep", 1)) {
      set_rep_domain = ARGS.shift();
    } else if (ARGS.prefix("-getrep", 1)) {
      get_rep_domain = ARGS.shift();
    } else if (ARGS.prefix("-shift"), 1) {
      shift = atoi(*argv++);  argc--;
    } else if (ARGS.prefix("-stain", 1)) {
      stain = atoi(*argv++);  argc--;
      if (stain) {}             /* FIXME */
    } else if (ARGS.prefix("-suffix", 1)) {
      foo.suffix += ARGS.shift();
    } else {
      cerr << "Unrecognized arg: " << ARGS.current_arg << endl;
      throw int(1);
    }
  }}
  catch (int){
    exeunt(ex_syserr);
  }

// dns_mode mode ...
// Probably it would be better to make more thorough DNS checks.
//
  if (dns_mode) {
    char* ipvar = getenv("TCPREMOTEIP");
    char* namevar = getenv("TCPREMOTEHOST");
    exeunt(foo.check_dns(ipvar, namevar));
  }

  if (get_40_mid.length()){
    box_state rslt = foo.get_40(get_40_mid);
    cerr << foo.decode_40[rslt] << endl;
    return 0;
  }

  if (set_40_mid.length()){
    return foo.set_40(set_40_mid, shift);
  }

  if (get_rep_domain.length()){
    box_state rslt = foo.get_rep(get_rep_domain);
    cerr << foo.decode_40[rslt] << endl;
    return 0;
  }

  if (set_rep_domain.length()){
    return foo.set_rep(set_rep_domain, shift);
  }

  if (scan40mode) {
    foo.scan_box(box_40, copies);
    return 0;
  }

  if (scanrepmode) {
    foo.scan_box(box_rep, copies);
    return 0;
  }

  return 0;
}