xref: /freebsd/sys/netpfil/ipfilter/netinet/ip_irc_pxy.c (revision 79d23845179a534f533185763cb92032202729a7)
1 /*
2  * Copyright (C) 2012 by Darren Reed.
3  *
4  * See the IPFILTER.LICENCE file for details on licencing.
5  *
6  * $Id$
7  */
8 
9 #define	IPF_IRC_PROXY
10 
11 #define	IPF_IRCBUFSZ	96	/* This *MUST* be >= 64! */
12 
13 
14 void ipf_p_irc_main_load(void);
15 void ipf_p_irc_main_unload(void);
16 int ipf_p_irc_new(void *, fr_info_t *, ap_session_t *, nat_t *);
17 int ipf_p_irc_out(void *, fr_info_t *, ap_session_t *, nat_t *);
18 int ipf_p_irc_send(fr_info_t *, nat_t *);
19 int ipf_p_irc_complete(ircinfo_t *, char *, size_t);
20 u_short ipf_irc_atoi(char **);
21 
22 static	frentry_t	ircnatfr;
23 
24 int	irc_proxy_init = 0;
25 
26 
27 /*
28  * Initialize local structures.
29  */
30 void
ipf_p_irc_main_load(void)31 ipf_p_irc_main_load(void)
32 {
33 	bzero((char *)&ircnatfr, sizeof(ircnatfr));
34 	ircnatfr.fr_ref = 1;
35 	ircnatfr.fr_flags = FR_INQUE|FR_PASS|FR_QUICK|FR_KEEPSTATE;
36 	MUTEX_INIT(&ircnatfr.fr_lock, "IRC proxy rule lock");
37 	irc_proxy_init = 1;
38 }
39 
40 
41 void
ipf_p_irc_main_unload(void)42 ipf_p_irc_main_unload(void)
43 {
44 	if (irc_proxy_init == 1) {
45 		MUTEX_DESTROY(&ircnatfr.fr_lock);
46 		irc_proxy_init = 0;
47 	}
48 }
49 
50 
51 const char *ipf_p_irc_dcctypes[] = {
52 	"CHAT ",	/* CHAT chat ipnumber portnumber */
53 	"SEND ",	/* SEND filename ipnumber portnumber */
54 	"MOVE ",
55 	"TSEND ",
56 	"SCHAT ",
57 	NULL,
58 };
59 
60 
61 /*
62  * :A PRIVMSG B :^ADCC CHAT chat 0 0^A\r\n
63  * PRIVMSG B ^ADCC CHAT chat 0 0^A\r\n
64  */
65 
66 
67 int
ipf_p_irc_complete(ircinfo_t * ircp,char * buf,size_t len)68 ipf_p_irc_complete(ircinfo_t *ircp, char *buf, size_t len)
69 {
70 	register char *s, c;
71 	register size_t i;
72 	u_32_t l;
73 	int j, k;
74 
75 	ircp->irc_ipnum = 0;
76 	ircp->irc_port = 0;
77 
78 	if (len < 31)
79 		return (0);
80 	s = buf;
81 	c = *s++;
82 	i = len - 1;
83 
84 	if ((c != ':') && (c != 'P'))
85 		return (0);
86 
87 	if (c == ':') {
88 		/*
89 		 * Loosely check that the source is a nickname of some sort
90 		 */
91 		s++;
92 		c = *s;
93 		ircp->irc_snick = s;
94 		if (!ISALPHA(c))
95 			return (0);
96 		i--;
97 		for (c = *s; !ISSPACE(c) && (i > 0); i--)
98 			c = *s++;
99 		if (i < 31)
100 			return (0);
101 		if (c != 'P')
102 			return (0);
103 	} else
104 		ircp->irc_snick = NULL;
105 
106 	/*
107 	 * Check command string
108 	 */
109 	if (strncmp(s, "PRIVMSG ", 8))
110 		return (0);
111 	i -= 8;
112 	s += 8;
113 	c = *s;
114 	ircp->irc_dnick = s;
115 
116 	/*
117 	 * Loosely check that the destination is a nickname of some sort
118 	 */
119 	if (!ISALPHA(c))
120 		return (0);
121 	for (; !ISSPACE(c) && (i > 0); i--)
122 		c = *s++;
123 	if (i < 20)
124 		return (0);
125 	s++,
126 	i--;
127 
128 	/*
129 	 * Look for a ^A to start the DCC
130 	 */
131 	c = *s;
132 	if (c == ':') {
133 		s++;
134 		c = *s;
135 	}
136 
137 	if (strncmp(s, "\001DCC ", 4))
138 		return (0);
139 
140 	i -= 4;
141 	s += 4;
142 
143 	/*
144 	 * Check for a recognised DCC command
145 	 */
146 	for (j = 0, k = 0; ipf_p_irc_dcctypes[j]; j++) {
147 		k = MIN(strlen(ipf_p_irc_dcctypes[j]), i);
148 		if (!strncmp(ipf_p_irc_dcctypes[j], s, k))
149 			break;
150 	}
151 	if (!ipf_p_irc_dcctypes[j])
152 		return (0);
153 
154 	ircp->irc_type = s;
155 	i -= k;
156 	s += k;
157 
158 	if (i < 11)
159 		return (0);
160 
161 	/*
162 	 * Check for the arg
163 	 */
164 	c = *s;
165 	if (ISSPACE(c))
166 		return (0);
167 	ircp->irc_arg = s;
168 	for (; (c != ' ') && (c != '\001') && (i > 0); i--)
169 		c = *s++;
170 
171 	if (c == '\001')	/* In reality a ^A can quote another ^A...*/
172 		return (0);
173 
174 	if (i < 5)
175 		return (0);
176 
177 	s++;
178 	i--;
179 	c = *s;
180 	if (!ISDIGIT(c))
181 		return (0);
182 	ircp->irc_addr = s;
183 	/*
184 	 * Get the IP#
185 	 */
186 	for (l = 0; ISDIGIT(c) && (i > 0); i--) {
187 		l *= 10;
188 		l += c - '0';
189 		c = *s++;
190 	}
191 
192 	if (i < 4)
193 		return (0);
194 
195 	if (c != ' ')
196 		return (0);
197 
198 	ircp->irc_ipnum = l;
199 	s++;
200 	i--;
201 	c = *s;
202 	if (!ISDIGIT(c))
203 		return (0);
204 	/*
205 	 * Get the port#
206 	 */
207 	for (l = 0; ISDIGIT(c) && (i > 0); i--) {
208 		l *= 10;
209 		l += c - '0';
210 		c = *s++;
211 	}
212 	if (i < 3)
213 		return (0);
214 	if (strncmp(s, "\001\r\n", 3))
215 		return (0);
216 	s += 3;
217 	ircp->irc_len = s - buf;
218 	ircp->irc_port = l;
219 	return (1);
220 }
221 
222 
223 int
ipf_p_irc_new(void * arg,fr_info_t * fin,ap_session_t * aps,nat_t * nat __unused)224 ipf_p_irc_new(void *arg, fr_info_t *fin, ap_session_t *aps, nat_t *nat __unused)
225 {
226 	ircinfo_t *irc;
227 
228 	if (fin->fin_v != 4)
229 		return (-1);
230 
231 	KMALLOC(irc, ircinfo_t *);
232 	if (irc == NULL)
233 		return (-1);
234 
235 	aps->aps_data = irc;
236 	aps->aps_psiz = sizeof(ircinfo_t);
237 
238 	bzero((char *)irc, sizeof(*irc));
239 	return (0);
240 }
241 
242 
243 int
ipf_p_irc_send(fr_info_t * fin,nat_t * nat)244 ipf_p_irc_send(fr_info_t *fin, nat_t *nat)
245 {
246 	char ctcpbuf[IPF_IRCBUFSZ], newbuf[IPF_IRCBUFSZ];
247 	tcphdr_t *tcp, tcph, *tcp2 = &tcph;
248 	int off, inc = 0, i, dlen;
249 	ipf_main_softc_t *softc;
250 	size_t nlen = 0, olen;
251 	struct in_addr swip;
252 	u_short a5, sp;
253 	ircinfo_t *irc;
254 	fr_info_t fi;
255 	nat_t *nat2;
256 	u_int a1;
257 	ip_t *ip;
258 	mb_t *m;
259 #if SOLARIS
260 	mb_t *m1;
261 #endif
262 	softc = fin->fin_main_soft;
263 
264 	m = fin->fin_m;
265 	ip = fin->fin_ip;
266 	tcp = (tcphdr_t *)fin->fin_dp;
267 	bzero(ctcpbuf, sizeof(ctcpbuf));
268 	off = (char *)tcp - (char *)ip + (TCP_OFF(tcp) << 2) + fin->fin_ipoff;
269 
270 	dlen = MSGDSIZE(m) - off;
271 	if (dlen <= 0)
272 		return (0);
273 	COPYDATA(m, off, MIN(sizeof(ctcpbuf), dlen), ctcpbuf);
274 
275 	if (dlen <= 0)
276 		return (0);
277 	ctcpbuf[sizeof(ctcpbuf) - 1] = '\0';
278 	*newbuf = '\0';
279 
280 	irc = nat->nat_aps->aps_data;
281 	if (ipf_p_irc_complete(irc, ctcpbuf, dlen) == 0)
282 		return (0);
283 
284 	/*
285 	 * check that IP address in the DCC reply is the same as the
286 	 * sender of the command - prevents use for port scanning.
287 	 */
288 	if (irc->irc_ipnum != ntohl(nat->nat_osrcaddr))
289 		return (0);
290 
291 	a5 = irc->irc_port;
292 
293 	/*
294 	 * Calculate new address parts for the DCC command
295 	 */
296 	a1 = ntohl(ip->ip_src.s_addr);
297 	olen = irc->irc_len;
298 	i = irc->irc_addr - ctcpbuf;
299 	i++;
300 	(void) strncpy(newbuf, ctcpbuf, i);
301 	/* DO NOT change these! */
302 	(void) snprintf(newbuf, sizeof(newbuf), "%u %u\001\r\n", a1, a5);
303 
304 	nlen = strlen(newbuf);
305 	inc = nlen - olen;
306 
307 	if ((inc + fin->fin_plen) > 65535)
308 		return (0);
309 
310 #if SOLARIS
311 	for (m1 = m; m1->b_cont; m1 = m1->b_cont)
312 		;
313 	if ((inc > 0) && (m1->b_datap->db_lim - m1->b_wptr < inc)) {
314 		mblk_t *nm;
315 
316 		/* alloc enough to keep same trailer space for lower driver */
317 		nm = allocb(nlen, BPRI_MED);
318 		PANIC((!nm),("ipf_p_irc_out: allocb failed"));
319 
320 		nm->b_band = m1->b_band;
321 		nm->b_wptr += nlen;
322 
323 		m1->b_wptr -= olen;
324 		PANIC((m1->b_wptr < m1->b_rptr),
325 		      ("ipf_p_irc_out: cannot handle fragmented data block"));
326 
327 		linkb(m1, nm);
328 	} else {
329 # if SOLARIS && defined(ICK_VALID)
330 		if (m1->b_datap->db_struiolim == m1->b_wptr)
331 			m1->b_datap->db_struiolim += inc;
332 		m1->b_datap->db_struioflag &= ~STRUIO_IP;
333 # endif
334 		m1->b_wptr += inc;
335 	}
336 #else
337 	if (inc < 0)
338 		m_adj(m, inc);
339 	/* the mbuf chain will be extended if necessary by m_copyback() */
340 #endif
341 	COPYBACK(m, off, nlen, newbuf);
342 	fin->fin_flx |= FI_DOCKSUM;
343 
344 	if (inc != 0) {
345 #if SOLARIS
346 		register u_32_t	sum1, sum2;
347 
348 		sum1 = fin->fin_plen;
349 		sum2 = fin->fin_plen + inc;
350 
351 		/* Because ~1 == -2, We really need ~1 == -1 */
352 		if (sum1 > sum2)
353 			sum2--;
354 		sum2 -= sum1;
355 		sum2 = (sum2 & 0xffff) + (sum2 >> 16);
356 
357 		ipf_fix_outcksum(0, &ip->ip_sum, sum2, 0);
358 #endif
359 		fin->fin_plen += inc;
360 		ip->ip_len = htons(fin->fin_plen);
361 		fin->fin_dlen += inc;
362 	}
363 
364 	/*
365 	 * Add skeleton NAT entry for connection which will come back the
366 	 * other way.
367 	 */
368 	sp = htons(a5);
369 	/*
370 	 * Don't allow the PORT command to specify a port < 1024 due to
371 	 * security crap.
372 	 */
373 	if (ntohs(sp) < 1024)
374 		return (0);
375 
376 	/*
377 	 * The server may not make the connection back from port 20, but
378 	 * it is the most likely so use it here to check for a conflicting
379 	 * mapping.
380 	 */
381 	bcopy((caddr_t)fin, (caddr_t)&fi, sizeof(fi));
382 	fi.fin_data[0] = sp;
383 	fi.fin_data[1] = fin->fin_data[1];
384 	nat2 = ipf_nat_outlookup(fin, IPN_TCP, nat->nat_pr[1], nat->nat_nsrcip,
385 			     ip->ip_dst);
386 	if (nat2 == NULL) {
387 #ifdef USE_MUTEXES
388 		ipf_nat_softc_t *softn = softc->ipf_nat_soft;
389 #endif
390 
391 		bcopy((caddr_t)fin, (caddr_t)&fi, sizeof(fi));
392 		bzero((char *)tcp2, sizeof(*tcp2));
393 		tcp2->th_win = htons(8192);
394 		tcp2->th_sport = sp;
395 		tcp2->th_dport = 0; /* XXX - don't specify remote port */
396 		fi.fin_data[0] = ntohs(sp);
397 		fi.fin_data[1] = 0;
398 		fi.fin_dp = (char *)tcp2;
399 		fi.fin_fr = &ircnatfr;
400 		fi.fin_dlen = sizeof(*tcp2);
401 		fi.fin_plen = fi.fin_hlen + sizeof(*tcp2);
402 		swip = ip->ip_src;
403 		ip->ip_src = nat->nat_nsrcip;
404 		MUTEX_ENTER(&softn->ipf_nat_new);
405 		nat2 = ipf_nat_add(&fi, nat->nat_ptr, NULL,
406 			       NAT_SLAVE|IPN_TCP|SI_W_DPORT, NAT_OUTBOUND);
407 		MUTEX_EXIT(&softn->ipf_nat_new);
408 		if (nat2 != NULL) {
409 			(void) ipf_nat_proto(&fi, nat2, 0);
410 			MUTEX_ENTER(&nat2->nat_lock);
411 			ipf_nat_update(&fi, nat2);
412 			MUTEX_EXIT(&nat2->nat_lock);
413 
414 			(void) ipf_state_add(softc, &fi, NULL, SI_W_DPORT);
415 		}
416 		ip->ip_src = swip;
417 	}
418 	return (inc);
419 }
420 
421 
422 int
ipf_p_irc_out(void * arg,fr_info_t * fin,ap_session_t * aps __unused,nat_t * nat)423 ipf_p_irc_out(void *arg, fr_info_t *fin, ap_session_t *aps __unused, nat_t *nat)
424 {
425 	return (ipf_p_irc_send(fin, nat));
426 }
427