summaryrefslogtreecommitdiff
path: root/sqmail-4.3.07/src/received.c
blob: 51339a92a0d44deb4cad0bd6007d8eaaf34b2ec8 (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
#include "fmt.h"
#include "qmail.h"
#include "now.h"
#include "datetime.h"
#include "date822fmt.h"
#include "received.h"
#include "str.h"
#include "stralloc.h"
#include "byte.h"
#include "str.h"

static int issafe(char ch)
{
  if (ch == ' ') return 1;  /* accept empty spaces */
  if (ch == '.') return 1;
  if (ch == '@') return 1;
  if (ch == '%') return 1;
  if (ch == '+') return 1;
  if (ch == '/') return 1;
  if (ch == '=') return 1;
  if (ch == ':') return 1;
  if (ch == '-') return 1;
  if ((ch >= 'a') && (ch <= 'z')) return 1;
  if ((ch >= 'A') && (ch <= 'Z')) return 1;
  if ((ch >= '0') && (ch <= '9')) return 1;
  return 0;
}

void safeput(struct qmail *qqt,char *s)
{
  char ch;
  while ((ch = *s++)) {
    if (!issafe(ch)) ch = '?';
    qmail_put(qqt,&ch,1);
  }
}

static char buf[DATE822FMT];

/* "Received: from relay1.uu.net ([E]HELO uunet.uu.net) (user@192.48.96.5)" */
/* "  de/crypted with tls-version: cipher [used/perm] DN=dn" */
/* "  by silverton.berkeley.edu with [UTF8][E]SMTP[SA]; 26 Sep 1995 04:46:54 -0000" */
/* "X-RBL-Info: http://www.spamhaus.org/query/bl?ip=127.0.0.2 */

void received(struct qmail *qqt,char *protocol,char *local,char *remoteip,char *remotehost,char *remoteinfo,char *helo,char *tlsinfo,char *rblinfo)
{
  struct datetime dt;
  int i;

  qmail_puts(qqt,"Received: from ");
  safeput(qqt,remotehost);
  if (helo) {
    qmail_puts(qqt," (HELO ");
    safeput(qqt,helo);
    qmail_puts(qqt,")");
  }
  qmail_puts(qqt," (");
  if (remoteinfo) {
    safeput(qqt,remoteinfo);
    qmail_puts(qqt,"@");
  }
  safeput(qqt,remoteip);
  qmail_puts(qqt,")");

  if (tlsinfo) {
    qmail_puts(qqt,"\n  de/crypted with ");
    qmail_puts(qqt,tlsinfo);
  }
  qmail_puts(qqt,"\n  by ");
  safeput(qqt,local);
  qmail_puts(qqt," with ");
  qmail_puts(qqt,protocol);
  qmail_puts(qqt,"; ");
  datetime_tai(&dt,now());
  qmail_put(qqt,buf,date822fmt(buf,&dt));

  if (rblinfo) {
    i = str_chr(rblinfo,']'); 
    if (rblinfo[i] == ']') { 
      qmail_puts(qqt,"X-RBL-Info: "); 
      safeput(qqt,rblinfo + i + 2);
      qmail_puts(qqt,"\n");
    }
  }
}

/* "Received-SPF: pass (Helogreeting: domain of Identity " */
/* "  designates Clientip as permitted sender) receiver=Hostname " */
/* "  client-ip=Clientip; envelope-from=Mailfrom; " */

void spfheader(struct qmail *qqt,char *spfinfo,char *local,char *remoteip,char *helohost,char *mailfrom)
{
  char *result = 0;
  char *identity = 0;
  char *clientip = 0;
  char *helo = 0;
  char *envelopefrom = 0;
  char *receiver = 0;
  char *problem = 0;
  char *mechanism = 0;
  int i, j = 0;
  int len;

  len = str_len(spfinfo);
  if (!len) return;

  for (i = 0; i < len; i++)
    if (spfinfo[i] == ' ') spfinfo[i] = '\0'; 
 
  for (i = 0; i < len; i++) {
    if (spfinfo[i] == '\0') {
      switch (spfinfo[i + 1]) {   
        case 'S': clientip = spfinfo + i + 3; break;
        case 'O': envelopefrom = spfinfo + i + 3; break;
        case 'C': identity = spfinfo + i + 3; break;
        case 'H': helo = spfinfo + i + 3; break;
        case 'T': receiver = spfinfo + i + 3; break;
        case 'P': problem = spfinfo + i + 3; break;
        case 'M': if ((j = str_chr(spfinfo + i,'='))) spfinfo[i + j] = '\0';  
                  mechanism = spfinfo + i + 1; break;
        case 'R': result = spfinfo + i + 3; break;
        default: break;
      }
    }
  }

  if (!result || *result == 0) result = "o";
  if (!clientip || *clientip == 0) clientip = remoteip;
  if (!helo || *helo == 0) helo = helohost;
  if (!envelopefrom || *envelopefrom == 0) envelopefrom = mailfrom;
  if (!receiver || *receiver == 0) receiver = local;
  if (!problem || *problem == 0) problem = "unknown";
  if (!mechanism || *mechanism == 0) mechanism = "unknown";
  if (!identity || *identity == 0) {
    j = str_rchr(envelopefrom,'@');
    if (envelopefrom[j] == '@') identity = envelopefrom + j + 1;
    else identity = "unknown";
  }

  qmail_puts(qqt,"Received-SPF: ");
  switch (*result) {
    case '+': qmail_puts(qqt," pass ("); safeput(qqt,helo);
              qmail_puts(qqt,": domain of "); safeput(qqt,identity); qmail_puts(qqt,"\n");
              qmail_puts(qqt,"  designates "); safeput(qqt,clientip); qmail_puts(qqt," as permitted sender)\n");
              qmail_puts(qqt,"  receiver="); safeput(qqt,receiver); 
              qmail_puts(qqt,"; client-ip="); safeput(qqt,clientip); qmail_puts(qqt,"\n");
              qmail_puts(qqt,"  envelope-from="); safeput(qqt,envelopefrom); qmail_puts(qqt,";\n"); break;
    case '-': qmail_puts(qqt," fail ("); safeput(qqt,helo);
              qmail_puts(qqt,": domain of "); safeput(qqt,identity); qmail_puts(qqt,"\n");
              qmail_puts(qqt,"  does not designate "); safeput(qqt,clientip); qmail_puts(qqt," as permitted sender)\n"); break;
    case '~': qmail_puts(qqt," softfail ("); safeput(qqt,helo);
              qmail_puts(qqt,": domain of transitioning "); safeput(qqt,identity); qmail_puts(qqt,"\n");
              qmail_puts(qqt,"  does not designate "); safeput(qqt,clientip); qmail_puts(qqt," as permitted sender)\n"); break;
    case '?': qmail_puts(qqt," neutral ("); safeput(qqt,helo); qmail_puts(qqt,"; client-ip="); safeput(qqt,clientip);
              qmail_puts(qqt," is neither permitted \n"); qmail_puts(qqt,"  nor denied by domain of "); safeput(qqt,identity),
              qmail_puts(qqt,")\n"); break;
    case 'o': qmail_puts(qqt," none ("); safeput(qqt,helo);
              qmail_puts(qqt,": domain of "); safeput(qqt,identity); qmail_puts(qqt," does\n");
              qmail_puts(qqt,"  not designate permitted sender hosts)\n"); break;
    case 't': qmail_puts(qqt," temperror ("); safeput(qqt,helo);
              qmail_puts(qqt,": domain of "); safeput(qqt,identity); qmail_puts(qqt," evaluated\n"); 
              qmail_puts(qqt,"  with error: "); safeput(qqt,problem); qmail_puts(qqt," for mechanism: "); safeput(qqt,mechanism);
              qmail_puts(qqt,")\n"); break;
    case 'e': qmail_puts(qqt," permerror ("); safeput(qqt,helo);
              qmail_puts(qqt,": domain of "); safeput(qqt,identity); qmail_puts(qqt," evaluated\n");
              qmail_puts(qqt,"  with error: "); safeput(qqt,problem); qmail_puts(qqt," for mechanism: "); safeput(qqt,mechanism);
              qmail_puts(qqt,")\n"); break;
    default:  qmail_puts(qqt," unknown (results for "); safeput(qqt,helo);
              qmail_puts(qqt,": domain of "); safeput(qqt,identity);
              qmail_puts(qqt,"  follow an unknown mechanism: "); safeput(qqt,mechanism); qmail_puts(qqt,")\n"); break;
  }
}