greylist.c 6.2 KB

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