-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddress.c
173 lines (149 loc) · 3.79 KB
/
address.c
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
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <syslog.h>
#if HAVE_MALLOC_H
# include <malloc.h>
#endif
#include "letter.h"
#include "mx.h"
#include "domain.h"
#include "public.h"
#include "mymalloc.h"
void
freeaddress(struct address *ptr)
{
int snooze = alarm(10);
if (ptr) {
/*syslog(LOG_DEBUG, "freeaddress(domain: %s)", ptr->domain);*/
if (ptr->domain) free(ptr->domain);
/*syslog(LOG_DEBUG, "freeaddress(user: %s)", ptr->user);*/
if (ptr->user) free(ptr->user);
/*syslog(LOG_DEBUG, "freeaddress(full: %s)", ptr->full);*/
if (ptr->full) free(ptr->full);
/*syslog(LOG_DEBUG, "freeaddress(alias: %s)", ptr->alias);*/
if (ptr->alias) free(ptr->alias);
free(ptr);
}
alarm(snooze);
}
struct address *
mkaddress(char *full)
{
char *q;
int size;
struct address *ret;
if (full == 0)
return 0;
if ( (ret = calloc(1, sizeof *ret)) == 0 )
return 0;
if ( ret->full = strdup(full) ) {
if ( q = strrchr(full, '@') ) {
/*syslog(LOG_DEBUG, "mkaddress @: user=|%.*s| domain=|%s|", q-full, full, q+1);*/
ret->domain = strdup(q+1);
size = 1+(q-full);
if ( ret->domain && (ret->user = malloc(size)) ) {
strlcpy(ret->user, full, size);
return ret;
}
}
else if (q = strchr(full, '!')) {
/*syslog(LOG_DEBUG, "mkaddress !: user=|%s| domain=|%.*s|", q+1, q-full, full);*/
ret->user = strdup(q+1);
size = 1+(q-full);
if ( ret->user && (ret->domain = malloc(size)) ) {
strlcpy(ret->domain, full, size);
return ret;
}
}
else if ( (strlen(full) < 1) || (ret->user = strdup(full)) ) {
/*syslog(LOG_DEBUG, "mkaddress nodomain: user=|%s|", full);*/
return ret;
}
}
freeaddress(ret);
return 0;
}
static int
okayanyhow(struct env *env, int flags)
{
return (flags & VF_FROM) ? (!env->verify_from) : env->forward_all;
}
int
localIP(ENV *e, struct ipa *a)
{
/*ENV *e2 = e;
struct ipa *a2 = a;*/
struct in_addr *lip;
if ( !a ) return 0;
lip = e->local_if;
while ( lip->s_addr ) {
/*if ( e2 != e ) abort();
if ( a2 != a ) abort();*/
if ( (lip++)->s_addr == a->addr.s_addr )
return 1;
}
return 0;
}
struct address *
verify(struct letter *let, struct domain *dom, char *p, int flags, int *reason)
{
char *e = p + strlen(p);
int bad = 0;
struct address *ret;
extern char *addr(char*,int*);
struct iplist mxes;
int i;
while ( (e > p) && (isspace(e[-1]) || e[-1] == '\r' || e[-1] == '\n') )
--e;
if (*e)
*e = 0;
if ((e > p) && (ret = mkaddress(addr(p, &bad))) ) {
if (ret->domain) {
ret->local = 0;
/* check that there is an mx (or A record) for the mail domain;
* if that fails we fail unless verify_from is off and it's
* a MAIL FROM:<> address
*/
if ( (getMXes(ret->domain, 1, &mxes) > 0) ) {
for (i=0; i < mxes.count; i++)
if ( localIP(let->env, &mxes.a[i] ) ) {
/* we are a legitimate mx for this address.
*/
ret->local = 1;
if ( (i == 0) || !(let->env->mxpool) ) {
/* If we're not mxpooling, we handle mail for
* this domain locally, but if we are mxpooling
* we only handle the mail locally if we're the
* best mx for the domain
*/
ret->deliver_here = 1;
ret->dom = getdomain(ret->domain);
}
break;
}
freeiplist(&mxes);
}
else if ( !okayanyhow(let->env,flags) ) {
if (reason) *reason = V_NOMX;
freeaddress(ret);
return 0;
}
}
else {
ret->deliver_here = ret->local = 1;
ret->dom = dom;
}
if (ret->deliver_here && (!userok(let,ret)) && (flags & VF_USER) ) {
if (reason) *reason = V_WRONG;
freeaddress(ret);
return 0;
}
return ret;
}
if (reason) { *reason = bad ? V_BOGUS : V_ERROR; }
return 0;
}