greylist.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. #include "common.h"
  2. #include "smtpd.h"
  3. #include "smtp.h"
  4. #include <ctype.h>
  5. #include <ip.h>
  6. #include <ndb.h>
  7. typedef struct {
  8. int existed; /* these two are distinct to cope with errors */
  9. int created;
  10. int noperm;
  11. long mtime; /* mod time, iff it already existed */
  12. } Greysts;
  13. /*
  14. * There's a bit of a problem with yahoo; they apparently have a vast
  15. * pool of machines that all run the same queue(s), so a 451 retry can
  16. * come from a different IP address for many, many retries, and it can
  17. * take ~5 hours for the same IP to call us back. Various other goofballs,
  18. * notably the IEEE, try to send mail just before 9 AM, then refuse to try
  19. * again until after 5 PM. Doh!
  20. */
  21. enum {
  22. Nonspammax = 14*60*60, /* must call back within this time if real */
  23. };
  24. static char whitelist[] = "/mail/lib/whitelist";
  25. /*
  26. * matches ip addresses or subnets in whitelist against nci->rsys.
  27. * ignores comments and blank lines in /mail/lib/whitelist.
  28. */
  29. static int
  30. onwhitelist(void)
  31. {
  32. int lnlen;
  33. char *line, *parse;
  34. char input[128];
  35. uchar ip[IPaddrlen], ipmasked[IPaddrlen];
  36. uchar mask4[IPaddrlen], addr4[IPaddrlen];
  37. uchar mask[IPaddrlen], addr[IPaddrlen], addrmasked[IPaddrlen];
  38. Biobuf *wl;
  39. static int beenhere;
  40. static allzero[IPaddrlen];
  41. if (!beenhere) {
  42. beenhere = 1;
  43. fmtinstall('I', eipfmt);
  44. }
  45. parseip(ip, nci->rsys);
  46. wl = Bopen(whitelist, OREAD);
  47. if (wl == nil)
  48. return 1;
  49. while ((line = Brdline(wl, '\n')) != nil) {
  50. if (line[0] == '#' || line[0] == '\n')
  51. continue;
  52. lnlen = Blinelen(wl);
  53. line[lnlen-1] = '\0'; /* clobber newline */
  54. /* default mask is /32 (v4) or /128 (v6) for bare IP */
  55. parse = line;
  56. if (strchr(line, '/') == nil) {
  57. strncpy(input, line, sizeof input - 5);
  58. if (strchr(line, '.') != nil)
  59. strcat(input, "/32");
  60. else
  61. strcat(input, "/128");
  62. parse = input;
  63. }
  64. /* sorry, dave; where's parsecidr for v4 or v6? */
  65. v4parsecidr(addr4, mask4, parse);
  66. v4tov6(addr, addr4);
  67. v4tov6(mask, mask4);
  68. maskip(addr, mask, addrmasked);
  69. maskip(ip, mask, ipmasked);
  70. if (memcmp(ipmasked, addrmasked, IPaddrlen) == 0)
  71. break;
  72. }
  73. Bterm(wl);
  74. return line != nil;
  75. }
  76. static int mkdirs(char *);
  77. /*
  78. * if any directories leading up to path don't exist, create them.
  79. * modifies but restores path.
  80. */
  81. static int
  82. mkpdirs(char *path)
  83. {
  84. int rv = 0;
  85. char *sl = strrchr(path, '/');
  86. if (sl != nil) {
  87. *sl = '\0';
  88. rv = mkdirs(path);
  89. *sl = '/';
  90. }
  91. return rv;
  92. }
  93. /*
  94. * if path or any directories leading up to it don't exist, create them.
  95. * modifies but restores path.
  96. */
  97. static int
  98. mkdirs(char *path)
  99. {
  100. int fd;
  101. if (access(path, AEXIST) >= 0)
  102. return 0;
  103. /* make presumed-missing intermediate directories */
  104. if (mkpdirs(path) < 0)
  105. return -1;
  106. /* make final directory */
  107. fd = create(path, OREAD, 0777|DMDIR);
  108. if (fd < 0)
  109. /*
  110. * we may have lost a race; if the directory now exists,
  111. * it's okay.
  112. */
  113. return access(path, AEXIST) < 0? -1: 0;
  114. close(fd);
  115. return 0;
  116. }
  117. static long
  118. getmtime(char *file)
  119. {
  120. long mtime = -1;
  121. Dir *ds = dirstat(file);
  122. if (ds != nil) {
  123. mtime = ds->mtime;
  124. free(ds);
  125. }
  126. return mtime;
  127. }
  128. static void
  129. tryaddgrey(char *file, Greysts *gsp)
  130. {
  131. int fd = create(file, OWRITE|OEXCL, 0444|DMEXCL);
  132. gsp->created = (fd >= 0);
  133. if (fd >= 0) {
  134. close(fd);
  135. gsp->existed = 0; /* just created; couldn't have existed */
  136. } else {
  137. /*
  138. * why couldn't we create file? it must have existed
  139. * (or we were denied perm on parent dir.).
  140. * if it existed, fill in gsp->mtime; otherwise
  141. * make presumed-missing intermediate directories.
  142. */
  143. gsp->existed = access(file, AEXIST) >= 0;
  144. if (gsp->existed)
  145. gsp->mtime = getmtime(file);
  146. else if (mkpdirs(file) < 0)
  147. gsp->noperm = 1;
  148. }
  149. }
  150. static void
  151. addgreylist(char *file, Greysts *gsp)
  152. {
  153. tryaddgrey(file, gsp);
  154. if (!gsp->created && !gsp->existed && !gsp->noperm)
  155. /* retry the greylist entry with parent dirs created */
  156. tryaddgrey(file, gsp);
  157. }
  158. static int
  159. recentcall(Greysts *gsp)
  160. {
  161. long delay = time(0) - gsp->mtime;
  162. if (!gsp->existed)
  163. return 0;
  164. /* reject immediate call-back; spammers are doing that now */
  165. return delay >= 30 && delay <= Nonspammax;
  166. }
  167. /*
  168. * policy: if (caller-IP, my-IP, rcpt) is not on the greylist,
  169. * reject this message as "451 temporary failure". if the caller is real,
  170. * he'll retry soon, otherwise he's a spammer.
  171. * at the first rejection, create a greylist entry for (my-ip, caller-ip,
  172. * rcpt, time), where time is the file's mtime. if they call back and there's
  173. * already a greylist entry, and it's within the allowed interval,
  174. * add their IP to the append-only whitelist.
  175. *
  176. * greylist files can be removed at will; at worst they'll cause a few
  177. * extra retries.
  178. */
  179. static int
  180. isrcptrecent(char *rcpt)
  181. {
  182. char *user;
  183. char file[256];
  184. Greysts gs;
  185. Greysts *gsp = &gs;
  186. if (rcpt[0] == '\0' || strchr(rcpt, '/') != nil ||
  187. strcmp(rcpt, ".") == 0 || strcmp(rcpt, "..") == 0)
  188. return 0;
  189. /* shorten names to fit pre-fossil or pre-9p2000 file servers */
  190. user = strrchr(rcpt, '!');
  191. if (user == nil)
  192. user = rcpt;
  193. else
  194. user++;
  195. /* check & try to update the grey list entry */
  196. snprint(file, sizeof file, "/mail/grey/%s/%s/%s",
  197. nci->lsys, nci->rsys, user);
  198. memset(gsp, 0, sizeof *gsp);
  199. addgreylist(file, gsp);
  200. /* if on greylist already and prior call was recent, add to whitelist */
  201. if (gsp->existed && recentcall(gsp)) {
  202. syslog(0, "smtpd",
  203. "%s/%s was grey; adding IP to white", nci->rsys, rcpt);
  204. return 1;
  205. } else if (gsp->existed)
  206. syslog(0, "smtpd", "call for %s/%s was seconds ago or long ago",
  207. nci->rsys, rcpt);
  208. else
  209. syslog(0, "smtpd", "no call registered for %s/%s; registering",
  210. nci->rsys, rcpt);
  211. return 0;
  212. }
  213. void
  214. vfysenderhostok(void)
  215. {
  216. char *fqdn;
  217. int recent = 0;
  218. Link *l;
  219. if (onwhitelist())
  220. return;
  221. for (l = rcvers.first; l; l = l->next)
  222. if (isrcptrecent(s_to_c(l->p)))
  223. recent = 1;
  224. /* if on greylist already and prior call was recent, add to whitelist */
  225. if (recent) {
  226. int fd = create(whitelist, OWRITE, 0666|DMAPPEND);
  227. if (fd >= 0) {
  228. seek(fd, 0, 2); /* paranoia */
  229. if ((fqdn = csgetvalue(nil, "ip", nci->rsys, "dom", nil)) != nil)
  230. fprint(fd, "# %s\n%s\n\n", fqdn, nci->rsys);
  231. else
  232. fprint(fd, "# unknown\n%s\n\n", nci->rsys);
  233. close(fd);
  234. }
  235. } else {
  236. syslog(0, "smtpd",
  237. "no recent call from %s for a rcpt; rejecting with temporary failure",
  238. nci->rsys);
  239. reply("451 please try again soon from the same IP.\r\n");
  240. exits("no recent call for a rcpt");
  241. }
  242. }