summaryrefslogtreecommitdiff
path: root/tools/ltgrey.c
blob: bb43b800fa2baf34bca6c4fdee061c7136099ccc (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
#include <iostream>
#include <stdlib.h>     /* for exit(), atoi() */

#include "libltgrey.h"
#include "utils.h"
#include "qq_exit_codes.h"

using namespace std;
pid_t mypid;
string progname;

#define exeunt exit

// forward reference:
void scan(const string progid, const string p, const int copies=1);


int main(int _argc, char** _argv){
  std::string hostname;
  std::string ipname;

  mypid = getpid();
  int argc(_argc);
  char** argv(_argv);
  const string parent_dir("/var/qmail/ltgrey");
  whatsit foo(argv[0], parent_dir); argc--; argv++;
  int scanmode(0);
  int copies(1);
  int shift(0);
  int stain(0);
  int dns_mode(0);
  string get40_mid;
  string set40_mid;
  while (argc > 0) {
    string arg = argv[0]; argc--; argv++;
    if (prefix(arg, "-scan")) {
      scanmode++;
    } else if (prefix(arg, "-copy")) {
      copies++;
    } else if (prefix(arg, "-verbose")) {
      foo.verbosity++;
    } else if (prefix(arg, "-dns_mode")) {
      dns_mode++;
    } else if (prefix(arg, "-get40")) {
      if (!argc){
        cerr << "Option '" << arg << "' requires an argument" << endl;
        exeunt(ex_syserr);
      }
      get40_mid = *argv++;  argc--;
    } else if (prefix(arg, "-set40")) {
      if (!argc){
        cerr << "Option '" << arg << "' requires an argument" << endl;
        exeunt(ex_syserr);
      }
      set40_mid = *argv++;  argc--;
    } else if (prefix(arg, "-shift")
        || prefix(arg, "-shift")) {
      if (!argc){
        cerr << "Option '" << arg << "' requires an argument" << endl;
        exeunt(ex_syserr);
      }
      shift = atoi(*argv++);  argc--;
    } else if (prefix(arg, "-stain")) {
      if (!argc){
        cerr << "Option '" << arg << "' requires an argument" << endl;
        exeunt(ex_syserr);
      }
      stain = atoi(*argv++);  argc--;
    } else if (prefix(arg, "-suffix")) {
      if (!argc){
        cerr << "Option '" << arg << "' requires an argument" << endl;
        exeunt(ex_syserr);
      }
      foo.suffix += *argv++;  argc--;
    } else {
      cerr << "Unrecognized arg: " << arg << endl;
      exeunt(ex_syserr);
    }
  }
  if (foo.setup()) return 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 (get40_mid.length()){
    state_40 rslt = foo.get40(get40_mid);
    cerr << foo.decode_40[rslt] << endl;
    return 0;
  }

  if (set40_mid.length()){
    return foo.set40(set40_mid, shift);
  }

  if (scanmode) {
    string dirname = parent_dir + "/quarante";
    scan(foo.progid, dirname, copies);
    return 0;
  }

  int sts = foo.doit(shift, stain);
  return sts;

}

//////////////////////////////////////////////////////////////////////
// requires apt-get install libboost-filesystem-dev:
#include <boost/filesystem.hpp>
#include <iomanip>
#include <sys/types.h>          /* for stat(), getaddrinfo() */
#include <sys/stat.h>           /* for stat() */
#include <unistd.h>             /* for stat() */
#include <stdio.h>              /* for perror */
#include <sstream>

const int minute(60);
const int hour(60*minute);
const int day(24*hour);

const int minimum_age(15*minute);
const int maximum_age(32*day);
const int probation(4*hour);

void scan(const string progid, const string p, const int copies){
  timeval now;
  gettimeofday(&now, NULL);
  using namespace boost::filesystem;

  if (is_directory(p)) {
    for (directory_iterator itr(p); itr!=directory_iterator(); ++itr) {
      string basename = itr->path().filename();
      for (int ii = 0; ii < copies; ii++)
        cout << setw(20) << left << basename << ' '; // display filename only
      if (is_regular_file(itr->status())) {
//        cout << " [" << file_size(itr->path()) << ']';
        struct stat mystat;
        string fn = p + "/" + basename;
        int rslt = stat(fn.c_str(), &mystat);
        if (rslt != 0){
            cerr << progid << ": stat failed for '"
              << fn << "' : ";
            perror(0);
        }
        int mod_age = now.tv_sec - mystat.st_mtime;
        int ac_age = now.tv_sec - mystat.st_atime;
        cout << setw(10) << time_out(mod_age)
                << " " << setw(10) << time_out(ac_age);
        if (0) {

        } else if (mod_age < 0) {
          cout << " penalty";
        } else if (mod_age < ac_age) {
          cout << " parole";
        } else if (mod_age - ac_age < minimum_age    // early bird, or completely unused
          && mod_age > probation) {           // did not diligently resubmit
          cout << " disprobation";
          if (mod_age != ac_age) cout << "!";
        } else if (mod_age < minimum_age) {
          cout << " young";
          if (mod_age != ac_age) cout << "!";
        } else if (mod_age == ac_age) {
          cout << " unused";
        } else if (mod_age > maximum_age) {
          cout << " expired";
        } else {
          cout << " OK";
        }
      }
      cout << '\n';
    }
  }
  else {
    // starting point is not a directory:
    cout << (exists(p) ? "Found: " : "Not found: ") << p << '\n';
  }
}