Download | Plain Text | No Line Numbers


  1. diff -Naur ucspi-tcp6-1.00.orig/package/commands-base ucspi-tcp6-1.00/package/commands-base
  2. --- ucspi-tcp6-1.00.orig/package/commands-base 2014-05-18 21:32:58.424457097 +0200
  3. +++ ucspi-tcp6-1.00/package/commands-base 2014-05-18 22:23:35.808824900 +0200
  4. @@ -15,3 +15,4 @@
  5. finger@
  6. http@
  7. who@
  8. +rblspp
  9. diff -Naur ucspi-tcp6-1.00.orig/src/it-base=d ucspi-tcp6-1.00/src/it-base=d
  10. --- ucspi-tcp6-1.00.orig/src/it-base=d 2014-05-18 21:32:58.428457166 +0200
  11. +++ ucspi-tcp6-1.00/src/it-base=d 2014-05-18 21:33:29.085965413 +0200
  12. @@ -20,3 +20,4 @@
  13. http@4
  14. who@
  15. who@4
  16. +rblspp
  17. diff -Naur ucspi-tcp6-1.00.orig/src/Makefile ucspi-tcp6-1.00/src/Makefile
  18. --- ucspi-tcp6-1.00.orig/src/Makefile 2014-05-18 21:32:58.425457118 +0200
  19. +++ ucspi-tcp6-1.00/src/Makefile 2014-05-18 22:04:19.670646890 +0200
  20. @@ -405,7 +405,7 @@
  21.  
  22. it-base: \
  23. tcpserver tcprules tcprulescheck argv0 recordio tcpclient who@ date@ \
  24. -finger@ http@ tcpcat mconnect mconnect-io addcr delcr fixcrio rblsmtpd \
  25. +finger@ http@ tcpcat mconnect mconnect-io addcr delcr fixcrio rblsmtpd rblspp \
  26. sysdeps
  27.  
  28. load: \
  29. @@ -503,6 +503,17 @@
  30. str.h stralloc.h strerr.h subgetopt.h tai.h taia.h uint64.h
  31. ./compile rblsmtpd.c
  32.  
  33. +rblspp: \
  34. +load rblspp.o byte.a dns.a socket.lib time.a unix.a
  35. + ./load rblspp dns.a time.a unix.a byte.a \
  36. + `cat socket.lib`
  37. +
  38. +rblspp.o: \
  39. +compile rblspp.c buffer.h byte.h dns.h env.h exit.h \
  40. +fmt.h gen_alloc.h iopause.h ip6.h readwrite.h scan.h sgetopt.h \
  41. +str.h stralloc.h strerr.h subgetopt.h tai.h taia.h uint64.h
  42. + ./compile rblspp.c
  43. +
  44. readclose.o: \
  45. compile readclose.c error.h gen_alloc.h readclose.h readwrite.h stralloc.h
  46. ./compile readclose.c
  47. @@ -934,4 +945,4 @@
  48. | sed s}HOME}"`head -1 home`"}g \
  49. > who@
  50. chmod 755 who@
  51. -
  52. \ No newline at end of file
  53. +
  54. diff -Naur ucspi-tcp6-1.00.orig/src/rblspp.c ucspi-tcp6-1.00/src/rblspp.c
  55. --- ucspi-tcp6-1.00.orig/src/rblspp.c 1970-01-01 01:00:00.000000000 +0100
  56. +++ ucspi-tcp6-1.00/src/rblspp.c 2014-05-18 23:21:26.462421956 +0200
  57. @@ -0,0 +1,275 @@
  58. +#include "byte.h"
  59. +#include "str.h"
  60. +#include "scan.h"
  61. +#include "fmt.h"
  62. +#include "env.h"
  63. +#include "exit.h"
  64. +#include "buffer.h"
  65. +#include "readwrite.h"
  66. +#include "sgetopt.h"
  67. +#include "strerr.h"
  68. +#include "stralloc.h"
  69. +#include "dns.h"
  70. +#include "ip6.h"
  71. +
  72. +#define FATAL "rblspp: fatal: "
  73. +
  74. +void nomem(void)
  75. +{
  76. + strerr_die2x(111,FATAL,"out of memory");
  77. +}
  78. +void usage(void)
  79. +{
  80. + strerr_die1x(100,"rblspp: usage: rblspp [ -B ] [ -b ] [ -C ] [ -c ] [ -i ] [ -r base ] [ -a base ]");
  81. +}
  82. +
  83. +char *tcp_proto;
  84. +char *ip_env;
  85. +static stralloc ip_reverse;
  86. +int flagip6 = 0;
  87. +
  88. +static inline char tohex(char c) { return c>=10?c-10+'a':c+'0'; }
  89. +
  90. +void ip_init(void)
  91. +{
  92. + unsigned int i;
  93. + unsigned int j;
  94. + char hexval;
  95. + char remoteip[16];
  96. +
  97. + tcp_proto = env_get("PROTO");
  98. + if (!tcp_proto) tcp_proto = "";
  99. + ip_env = env_get("TCPREMOTEIP");
  100. + if (!ip_env) ip_env = "";
  101. + if (str_diff(tcp_proto,"TCP6") == 0) {
  102. + if (byte_equal(ip_env,7,V4MAPPREFIX))
  103. + ip_env = ip_env + 7;
  104. + else
  105. + flagip6 = 1;
  106. + }
  107. +
  108. + if (!stralloc_copys(&ip_reverse,"")) nomem();
  109. +
  110. + if (flagip6) {
  111. + if ((ip6_scan(ip_env,remoteip)) == 0) return;
  112. +
  113. + for (j = 16; j > 0; j--) {
  114. + hexval = tohex(remoteip[j - 1] & 15);
  115. + if (!stralloc_catb(&ip_reverse,&hexval,1)) nomem();
  116. + if (!stralloc_cats(&ip_reverse,".")) nomem();
  117. +
  118. + hexval = tohex(remoteip[j - 1] >> 4);
  119. + if (!stralloc_catb(&ip_reverse,&hexval,1)) nomem();
  120. + if (!stralloc_cats(&ip_reverse,".")) nomem();
  121. + }
  122. + } else {
  123. + i = str_len(ip_env);
  124. + while (i) {
  125. + for (j = i;j > 0;--j) if (ip_env[j - 1] == '.') break;
  126. + if (!stralloc_catb(&ip_reverse,ip_env + j,i - j)) nomem();
  127. + if (!stralloc_cats(&ip_reverse,".")) nomem();
  128. + if (!j) break;
  129. + i = j - 1;
  130. + }
  131. + }
  132. +}
  133. +
  134. +int flagrblbounce = 0;
  135. +int flagfailclosed = 0;
  136. +int flagmustnotbounce = 0;
  137. +int flagrblinfo = 0;
  138. +
  139. +int decision = 0; /* 0 undecided, 1 accept, 2 direct refuse, 3 direct bounce, 4 rbl refuse, 5 rbl bounce */
  140. +static stralloc text; /* defined if decision is > 2 */
  141. +
  142. +static stralloc tmp;
  143. +
  144. +void rbl(char *base)
  145. +{
  146. + int i;
  147. + char *altreply = 0;
  148. + if (decision) return;
  149. + if (!stralloc_copy(&tmp,&ip_reverse)) nomem();
  150. + i = str_chr(base, ':');
  151. + if (base[i]) {
  152. + base[i] = 0;
  153. + altreply = base+i+1;
  154. + }
  155. + if (!stralloc_cats(&tmp,base)) nomem();
  156. + if (altreply) {
  157. + if (dns_ip4(&text,&tmp) == -1) {
  158. + flagmustnotbounce = 1;
  159. + if (flagfailclosed) {
  160. + if (!stralloc_copys(&text,"temporary RBL lookup error")) nomem();
  161. + decision = 4;
  162. + }
  163. + return;
  164. + }
  165. + if (text.len) {
  166. + if(!stralloc_copys(&text, "")) nomem();
  167. + while(*altreply) {
  168. + char *x;
  169. + i = str_chr(altreply, '%');
  170. + if(!stralloc_catb(&text, altreply, i)) nomem();
  171. + if(altreply[i] &&
  172. + altreply[i+1]=='I' &&
  173. + altreply[i+2]=='P' &&
  174. + altreply[i+3]=='%') {
  175. + if(!stralloc_catb(&text, ip_env, str_len(ip_env))) nomem();
  176. + altreply+=i+4;
  177. + } else if(altreply[i]) {
  178. + if(!stralloc_cats(&text, "%")) nomem();
  179. + altreply+=i+1;
  180. + } else {
  181. + altreply+=i;
  182. + }
  183. + }
  184. + }
  185. + } else {
  186. + if (dns_txt(&text,&tmp) == -1) {
  187. + flagmustnotbounce = 1;
  188. + if (flagfailclosed) {
  189. + if (!stralloc_copys(&text,"temporary RBL lookup error")) nomem();
  190. + decision = 4;
  191. + }
  192. + return;
  193. + }
  194. + }
  195. + if (text.len)
  196. + if (flagrblbounce)
  197. + decision = 5;
  198. + else
  199. + decision = 4;
  200. +}
  201. +
  202. +void antirbl(char *base)
  203. +{
  204. + if (decision) return;
  205. + int flagip;
  206. +
  207. + if (!stralloc_copy(&tmp,&ip_reverse)) nomem();
  208. + if (!stralloc_cats(&tmp,base)) nomem();
  209. +
  210. + if (flagip6)
  211. + flagip = dns_ip6(&text,&tmp);
  212. + else
  213. + flagip = dns_ip4(&text,&tmp);
  214. +
  215. + if (flagip == -1) {
  216. + flagmustnotbounce = 1;
  217. + if (!flagfailclosed)
  218. + decision = 1;
  219. + return;
  220. + }
  221. + if (text.len)
  222. + decision = 1;
  223. +}
  224. +
  225. +char strnum[FMT_ULONG];
  226. +static stralloc message;
  227. +
  228. +char outspace[1]; buffer out = BUFFER_INIT(write,1,outspace,sizeof outspace);
  229. +
  230. +void rblinfo(void)
  231. +{
  232. + int i;
  233. +
  234. + if (!stralloc_copys(&message,"[RBL info] ")) nomem();
  235. +
  236. + if (text.len > 200) text.len = 200;
  237. + if (!stralloc_cat(&message,&text)) nomem();
  238. +
  239. + for (i = 0;i < message.len;++i)
  240. + if ((message.s[i] < 32) || (message.s[i] > 126))
  241. + message.s[i] = '?';
  242. +
  243. + buffer_puts(buffer_2,"rblspp: ");
  244. + buffer_puts(buffer_2,ip_env);
  245. + buffer_puts(buffer_2," pid ");
  246. + buffer_put(buffer_2,strnum,fmt_ulong(strnum,getpid()));
  247. + buffer_puts(buffer_2,": ");
  248. + buffer_put(buffer_2,message.s,message.len);
  249. + buffer_puts(buffer_2,"\n");
  250. + buffer_flush(buffer_2);
  251. +}
  252. +
  253. +void rblspp(void)
  254. +{
  255. + int i;
  256. +
  257. + if (flagmustnotbounce || (decision == 2) || (decision == 4)) {
  258. + if (!stralloc_copys(&message,"E451 ")) nomem();
  259. + }
  260. + else
  261. + if (!stralloc_copys(&message,"E553 ")) nomem();
  262. +
  263. + if (text.len > 200) text.len = 200;
  264. + if (!stralloc_cat(&message,&text)) nomem();
  265. + for (i = 0;i < message.len;++i)
  266. + if ((message.s[i] < 32) || (message.s[i] > 126))
  267. + message.s[i] = '?';
  268. +
  269. + buffer_puts(buffer_2,"rblspp: ");
  270. + buffer_puts(buffer_2,ip_env);
  271. + buffer_puts(buffer_2," pid ");
  272. + buffer_put(buffer_2,strnum,fmt_ulong(strnum,getppid()));
  273. + buffer_puts(buffer_2,": ");
  274. + buffer_put(buffer_2,message.s,message.len);
  275. + buffer_puts(buffer_2,"\n");
  276. + buffer_flush(buffer_2);
  277. +
  278. + if (!stralloc_cats(&message,"\r\n")) nomem();
  279. +
  280. + buffer_putflush(&out,message.s,message.len);
  281. +
  282. + _exit(0);
  283. +}
  284. +
  285. +int main(int argc,char **argv,char **envp)
  286. +{
  287. + char *x;
  288. + int opt;
  289. + int rcount;
  290. +
  291. + x = env_get("SMTPRCPTCOUNT");
  292. + if (x) {
  293. + rcount = atoi(x);
  294. + if (rcount > 0)
  295. + return 0;
  296. + }
  297. +
  298. + ip_init();
  299. +
  300. + x = env_get("RBLSPP");
  301. + if (x) {
  302. + if (!*x)
  303. + decision = 1;
  304. + else if (*x == '-') {
  305. + if (!stralloc_copys(&text,x + 1)) nomem();
  306. + decision = 3;
  307. + }
  308. + else {
  309. + if (!stralloc_copys(&text,x)) nomem();
  310. + decision = 2;
  311. + }
  312. + }
  313. +
  314. + while ((opt = getopt(argc,argv,"bBcCir:a:")) != opteof)
  315. + switch(opt) {
  316. + case 'b': flagrblbounce = 1; break;
  317. + case 'B': flagrblbounce = 0; break;
  318. + case 'c': flagfailclosed = 1; break;
  319. + case 'C': flagfailclosed = 0; break;
  320. + case 'i': flagrblinfo = 1; break;
  321. + case 'r': rbl(optarg); break;
  322. + case 'a': antirbl(optarg); break;
  323. + default: usage();
  324. + }
  325. +
  326. + if (flagrblinfo && decision >= 4)
  327. + rblinfo();
  328. + else if (decision >= 2)
  329. + rblspp();
  330. +
  331. + return 0;
  332. +}
  333.