Download | Plain Text | No Line Numbers


  1. /*
  2.  * Copyright (C) 2004 Pawel Foremski <pjf@asn.pl>
  3.  *
  4.  * This program is free software; you can redistribute it and/or
  5.  * modify it under the terms of the GNU General Public License
  6.  * as published by the Free Software Foundation; either
  7.  * version 2 of the License, or (at your option) any later
  8.  * version.
  9.  *
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software Foundation,
  17.  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18.  *
  19.  ***
  20.  *
  21.  * Implements tarpitting
  22.  *
  23.  * Compile with: gcc -O2 -s -o tarpit ./tarpit.c
  24.  * Set in tcprules: TARPITCOUNT=n,TARPITDELAY={n, NORMAL, MEDIUM, HARD}
  25.  *
  26.  */
  27.  
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #include <unistd.h>
  31.  
  32. #ifndef MIN
  33. #define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
  34. #endif
  35.  
  36. int main()
  37. {
  38. char *tcount, *tdelay, *rcount, *sender, *tmax, *rok;
  39. int tc, rc;
  40. /* some servers have a smtp command timeout of 60 seconds so let's be on
  41.   * the safe side and make a default tarpit maximum of 50 seconds
  42.   */
  43. int max_err = 50, max_ok = 10;
  44. int max;
  45.  
  46. if (!(tcount = getenv("TARPITCOUNT")) ||
  47. !(tdelay = getenv("TARPITDELAY"))) return 0;
  48.  
  49. if (!(rcount = getenv("SMTPRCPTCOUNT")))
  50. { fprintf(stderr, "tarpit: error: can't get number of envelope recipients\n"); return 0; }
  51.  
  52. if ((tmax = getenv("TARPITMAXDELAY")))
  53. max_err = atoi(tmax);
  54. if ((tmax = getenv("TARPITMAXDELAYOK")))
  55. max_ok = atoi(tmax);
  56. max = ((rok = getenv("SMTPRCPTHOSTSOK")) && *rok == '1') ? max_ok : max_err;
  57.  
  58. tc = atoi(tcount); rc = atoi(rcount);
  59. if (rc < tc) return 0; /* under limit */
  60.  
  61. if (!(sender = getenv("SMTPMAILFROM")) || !*sender) sender = "unknown";
  62.  
  63. if (rc == tc)
  64. fprintf(stderr, "tarpit: started tarpitting mail from <%s>\n", sender);
  65.  
  66. switch (*tdelay) {
  67. case 'N': /* NORMAL */
  68. sleep(MIN((rc - tc + 1) * 2, max));
  69. break;
  70. case 'M': /* MEDIUM */
  71. sleep(MIN((rc - tc + 1) * 5, max));
  72. break;
  73. case 'H': /* HARD */
  74. sleep(MIN((rc - tc + 1) * (rc - tc + 1), max));
  75. break;
  76. default:
  77. sleep(MIN(atoi(tdelay), max));
  78. break;
  79. }
  80.  
  81. return 0;
  82. }
  83.