summaryrefslogtreecommitdiff
path: root/tools/ltgrey.c
blob: 577cbe92c591ca22d0e016dc72605ae562ec9b8d (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <iostream>
#include <stdlib.h>     /* for exit(), atoi() */

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

using namespace std;
pid_t mypid;
string progname;

#define exeunt exit

typedef const char cc;
typedef cc* str;

class argParser{
public:
  list<str> argv;
  string current_arg;

  argParser(const int _argc, const str _argv[])
  {
    for (int ii=0; ii < _argc; ii++) {
      argv.push_back(_argv[ii]);
    }
  }
  string shift() {
    string rslt = argv.front();
    argv.pop_front();
    return rslt;
  }
  void next(){
    current_arg = shift();
  }
  size_t size() const {
    return argv.size();
  }
  int prefix(const string longer, const size_t required = 0){
    if (argv.size() < required) {
      if (required==1)
        cerr << "Option '" << current_arg
          << "' requires an argument." << endl;
      else
        cerr << "Option '" << current_arg
          << "' requires " << required << " arguments." << endl;
      exeunt(ex_syserr);
    }
    return current_arg == longer.substr(0, current_arg.length());
  }
};

void usage(const string parent_dir){
  cout <<
"Usage:  ltgrey [options]\n"
"\n"
"Options include\n"
"  -scan40      # scan the quarantine directory\n"
"  -scanrep     # scan the reputation directory\n"
"  -copy        # no idea\n"
"  -verbose     # increase the verbosity\n"
"  -setrep mid  # set the reputation for the given message-id\n"
"  -getrep mid  # look up the reputation\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;
}

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_mid;
  string set_rep_mid;
  argParser ARGS(argc, argv);

  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_mid = ARGS.shift();
    } else if (ARGS.prefix("-getrep", 1)) {
      get_rep_mid = 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;
      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_mid.length()){
    box_state rslt = foo.get_rep(get_rep_mid);
    cerr << foo.decode_40[rslt] << endl;
    return 0;
  }

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

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

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

  return 0;
}