9 int existed; /* these two are distinct to cope with errors */
12 long mtime; /* mod time, iff it already existed */
16 * There's a bit of a problem with yahoo; they apparently have a vast
17 * pool of machines that all run the same queue(s), so a 451 retry can
18 * come from a different IP address for many, many retries, and it can
19 * take ~5 hours for the same IP to call us back. Various other goofballs,
20 * notably the IEEE, try to send mail just before 9 AM, then refuse to try
21 * again until after 5 PM. Doh!
24 Nonspammax = 14*60*60, /* must call back within this time if real */
26 static char *whitelist = "#9/mail/lib/whitelist";
29 * matches ip addresses or subnets in whitelist against nci->rsys.
30 * ignores comments and blank lines in /mail/lib/whitelist.
38 uchar ip[IPaddrlen], ipmasked[IPaddrlen];
39 uchar mask4[IPaddrlen], addr4[IPaddrlen];
40 uchar mask[IPaddrlen], addr[IPaddrlen], addrmasked[IPaddrlen];
46 fmtinstall('I', eipfmt);
47 whitelist = unsharp(whitelist);
50 parseip(ip, nci->rsys);
51 wl = Bopen(whitelist, OREAD);
54 while ((line = Brdline(wl, '\n')) != nil) {
55 if (line[0] == '#' || line[0] == '\n')
58 line[lnlen-1] = '\0'; /* clobber newline */
60 /* default mask is /32 (v4) or /128 (v6) for bare IP */
62 if (strchr(line, '/') == nil) {
63 strncpy(input, line, sizeof input - 5);
64 if (strchr(line, '.') != nil)
67 strcat(input, "/128");
70 /* sorry, dave; where's parsecidr for v4 or v6? */
71 v4parsecidr(addr4, mask4, parse);
75 maskip(addr, mask, addrmasked);
76 maskip(ip, mask, ipmasked);
77 if (memcmp(ipmasked, addrmasked, IPaddrlen) == 0)
84 static int mkdirs(char *);
87 * if any directories leading up to path don't exist, create them.
88 * modifies but restores path.
94 char *sl = strrchr(path, '/');
105 * if path or any directories leading up to it don't exist, create them.
106 * modifies but restores path.
113 if (access(path, AEXIST) >= 0)
116 /* make presumed-missing intermediate directories */
117 if (mkpdirs(path) < 0)
120 /* make final directory */
121 fd = create(path, OREAD, 0777|DMDIR);
124 * we may have lost a race; if the directory now exists,
127 return access(path, AEXIST) < 0? -1: 0;
136 Dir *ds = dirstat(file);
146 tryaddgrey(char *file, Greysts *gsp)
148 int fd = create(file, OWRITE|OEXCL, 0444|DMEXCL);
150 gsp->created = (fd >= 0);
153 gsp->existed = 0; /* just created; couldn't have existed */
156 * why couldn't we create file? it must have existed
157 * (or we were denied perm on parent dir.).
158 * if it existed, fill in gsp->mtime; otherwise
159 * make presumed-missing intermediate directories.
161 gsp->existed = access(file, AEXIST) >= 0;
163 gsp->mtime = getmtime(file);
164 else if (mkpdirs(file) < 0)
170 addgreylist(char *file, Greysts *gsp)
172 tryaddgrey(file, gsp);
173 if (!gsp->created && !gsp->existed && !gsp->noperm)
174 /* retry the greylist entry with parent dirs created */
175 tryaddgrey(file, gsp);
179 recentcall(Greysts *gsp)
181 long delay = time(0) - gsp->mtime;
185 /* reject immediate call-back; spammers are doing that now */
186 return delay >= 30 && delay <= Nonspammax;
190 * policy: if (caller-IP, my-IP, rcpt) is not on the greylist,
191 * reject this message as "451 temporary failure". if the caller is real,
192 * he'll retry soon, otherwise he's a spammer.
193 * at the first rejection, create a greylist entry for (my-ip, caller-ip,
194 * rcpt, time), where time is the file's mtime. if they call back and there's
195 * already a greylist entry, and it's within the allowed interval,
196 * add their IP to the append-only whitelist.
198 * greylist files can be removed at will; at worst they'll cause a few
203 isrcptrecent(char *rcpt)
210 if (rcpt[0] == '\0' || strchr(rcpt, '/') != nil ||
211 strcmp(rcpt, ".") == 0 || strcmp(rcpt, "..") == 0)
214 /* shorten names to fit pre-fossil or pre-9p2000 file servers */
215 user = strrchr(rcpt, '!');
221 /* check & try to update the grey list entry */
222 snprint(file, sizeof file, "%s/mail/grey/%s/%s/%s",
223 get9root(), nci->lsys, nci->rsys, user);
224 memset(gsp, 0, sizeof *gsp);
225 addgreylist(file, gsp);
227 /* if on greylist already and prior call was recent, add to whitelist */
228 if (gsp->existed && recentcall(gsp)) {
230 "%s/%s was grey; adding IP to white", nci->rsys, rcpt);
232 } else if (gsp->existed)
233 syslog(0, "smtpd", "call for %s/%s was seconds ago or long ago",
236 syslog(0, "smtpd", "no call registered for %s/%s; registering",
242 vfysenderhostok(void)
250 for (l = rcvers.first; l; l = l->next)
251 if (isrcptrecent(s_to_c(l->p)))
254 /* if on greylist already and prior call was recent, add to whitelist */
256 int fd = create(whitelist, OWRITE, 0666|DMAPPEND);
259 seek(fd, 0, 2); /* paranoia */
260 fprint(fd, "# unknown\n%s\n\n", nci->rsys);
265 "no recent call from %s for a rcpt; rejecting with temporary failure",
267 reply("451 please try again soon from the same IP.\r\n");
268 exits("no recent call for a rcpt");