summaryrefslogtreecommitdiff
path: root/tools/sepofra.c
blob: 7ee9c8aa060f314066f04a30b6a8b6847f86204b (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include <iostream>
#include <sstream>

#include "sepofra.h"
#include "utils.h"              /* for trim() */

#ifndef HOST_NAME_MAX
#define HOST_NAME_MAX 255
#endif

///// Important reference:
///// http://www.ietf.org/rfc/rfc4408.txt
/////

using namespace std;

string domain_part(const string ema){
  size_t where = ema.find('@');
  if (where != ema.npos){
    return ema.substr(1+where);
  }
  return ema;
}

// strips off one level of domain name,
//  provided that at least two levels remain.
//      foo.bar.com --> bar.com
//          bar.com --> bar.com
//    ..foo.bar.com --> bar.com
string subdomain2plus(const string ema){
  string sub(ema);
  while (sub[0] == '.') sub = sub.substr(1);
  size_t where = sub.find('.');
  if (where == string::npos) return ema;
  sub = sub.substr(1+where);
  if (sub.find(".") == string::npos) return ema;
  return sub;
}

SPF_result_t sepofra::check1(const string _host,
                const string msg, const int debug){
      string host(_host);
      size_t where = host.find("@");
      if (where != string::npos){
        host = host.substr(1+where);
      }
      if (!host.length()) return SPF_RESULT_INVALID;
      if (seen.find(host) != seen.end()) {
        // already checked this one
        return seen[host];
      }
      seen[host] = SPF_RESULT_TEMPERROR;

      authorities.push_back(host);
      if (SPF_request_set_env_from( spf_request,
              host.c_str() ) ) {
        throw bad_thing (("Invalid " + msg + ": " + host).c_str());
      }

      if (spf_response) SPF_response_free(spf_response);
      SPF_request_query_mailfrom(spf_request, &spf_response);
      if (debug) dumpit(debug, spf_response, "");

      return seen[host] = SPF_response_result(spf_response);
}


void sepofra::check(
            const string opt_ip,
            const string opt_helo,
            const string opt_mailfrom,
            const string opt_rcpt_to,
            const int opt_debug)
{
  SPF_server_t*     spf_server = NULL;
  sepofra rslt;
  ip = opt_ip;
  helo = trim(opt_helo, " \t\r\n<.>");
  mailfrom = trim(opt_mailfrom, " \t\r\n<.>");
  string mailfrom_domain = domain_part(opt_mailfrom);

  do {
    spf_server = SPF_server_new(SPF_DNS_CACHE, opt_debug);
    if (spf_server == NULL) {
      cerr << "SPF_server_new failed" << endl;
      break;
    }

    spf_request = SPF_request_new(spf_server);
    if (spf_request == NULL) {
      cerr << "sepofra::check SPF_request_new failed" << endl;
      break;
    }

    if ( SPF_request_set_ipv4_str( spf_request, opt_ip.c_str() )
      && SPF_request_set_ipv6_str( spf_request, opt_ip.c_str() ) ) {
      cerr << "sepofra::check: Invalid IP address: '"
      << opt_ip << "'" << endl;
      break;
    }

    result = check1(opt_helo, "HELO domain", opt_debug);
    if (result == SPF_RESULT_PASS) break;
    if (result == SPF_RESULT_FAIL) break;

    result = check1(mailfrom, "MAILFROM domain", opt_debug);
    if (result == SPF_RESULT_PASS) break;
    if (result == SPF_RESULT_FAIL) break;

    result = check1(subdomain2plus(opt_helo), "HELO subdomain", opt_debug);
    if (result == SPF_RESULT_PASS) break;
    if (result == SPF_RESULT_FAIL) break;

    result = check1(subdomain2plus(mailfrom), "MAILFROM domain", opt_debug);
    if (result == SPF_RESULT_PASS) break;
    if (result == SPF_RESULT_FAIL) break;

  } while(0);

  if (spf_response)     SPF_response_free(spf_response);
  if (spf_request)      SPF_request_free(spf_request);
  if (spf_server)       SPF_server_free(spf_server);

  return;
}

// return "" if result is invalid
// otherwise build a string explaining how SPF reached its conclusion
string sepofra::explain() const {
    if (result == SPF_RESULT_INVALID) return "";
    stringstream build;
    string summary = SPF_strresult(result);
    if (result != SPF_RESULT_PASS
     && result != SPF_RESULT_FAIL
     && result != SPF_RESULT_INVALID) summary = "neutral";

    char hostname[1+HOST_NAME_MAX];
    gethostname(hostname, 1+HOST_NAME_MAX);
    build << "Received-SPF: ";
    build << summary;
    build << " (" << hostname;  /* ) */
    build << ": sender " << ip;
    if (result == SPF_RESULT_PASS){
       build << " is approved by {"
       << authorities.back()
       << "}";
    } else if (result == SPF_RESULT_FAIL){
       build << " is forbidden by {"
       << authorities.back()
       << "}";
    } else {
      build << " is neither approved nor forbidden by {";
      int didsome(0);
      for (list<string>::const_iterator ptr = authorities.begin();
          ptr != authorities.end(); ptr++) {
        if (didsome++) build << " ";
        build << *ptr;
      }
      build << "}";
    }
    /* ( */  build << ")";
    if (ip != "")       build << " client-ip=" << ip << ";";
    if (mailfrom != "") build << " envelope-from=" << mailfrom << ";";
    if (helo != "")     build << " helo=" << helo;
    return build.str();
}

void dumpit(const int opt_debug,
        SPF_response_t* spf_response,
        const char* msg) {
  int i;
  printf("---- %s\n", msg);
  if ( opt_debug > 0 ) {
          printf ( "result = %s (%d)\n",
                  SPF_strresult(SPF_response_result(spf_response)),
                          SPF_response_result(spf_response));
          printf ( "err = %s (%d)\n",
                  SPF_strerror(SPF_response_errcode(spf_response)),
                          SPF_response_errcode(spf_response));
          for (i = 0; i < SPF_response_messages(spf_response); i++) {
                  SPF_error_t     *err = SPF_response_message(spf_response, i);
                  printf ( "%s_msg = (%d) %s\n",
                          (SPF_error_errorp(err) ? "warn" : "err"),
                          SPF_error_code(err),
                          SPF_error_message(err));
          }
  }

#define VALID_STR(x) (x ? x : "")

  printf("Bottom line result: %s\n",
        SPF_strresult( SPF_response_result(spf_response)));

  printf("Reason: %s\n", SPF_strreason(spf_response->reason));

  printf("SMTP comment: %s\n",
      VALID_STR(SPF_response_get_smtp_comment(spf_response)));

  printf("Header comment: %s\n",
        VALID_STR(SPF_response_get_header_comment(spf_response)));

  printf("Received spf: %s\n",
        VALID_STR(SPF_response_get_received_spf(spf_response)));

}